Stan Math Library  2.20.0
reverse mode automatic differentiation
multi_gp_cholesky_lpdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_MULTI_GP_CHOLESKY_LPDF_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MULTI_GP_CHOLESKY_LPDF_HPP
3 
13 
14 namespace stan {
15 namespace math {
37 template <bool propto, typename T_y, typename T_covar, typename T_w>
38 typename boost::math::tools::promote_args<T_y, T_covar, T_w>::type
40  const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
41  const Eigen::Matrix<T_covar, Eigen::Dynamic, Eigen::Dynamic>& L,
42  const Eigen::Matrix<T_w, Eigen::Dynamic, 1>& w) {
43  static const char* function = "multi_gp_cholesky_lpdf";
44  typedef
45  typename boost::math::tools::promote_args<T_y, T_covar, T_w>::type T_lp;
46 
47  check_size_match(function, "Size of random variable (rows y)", y.rows(),
48  "Size of kernel scales (w)", w.size());
49  check_size_match(function, "Size of random variable", y.cols(),
50  "rows of covariance parameter", L.rows());
51  check_finite(function, "Kernel scales", w);
52  check_positive(function, "Kernel scales", w);
53  check_finite(function, "Random variable", y);
54 
55  if (y.rows() == 0)
56  return 0;
57 
58  T_lp lp(0);
60  lp += NEG_LOG_SQRT_TWO_PI * y.rows() * y.cols();
61  }
62 
64  lp -= L.diagonal().array().log().sum() * y.rows();
65  }
66 
68  lp += 0.5 * y.cols() * sum(log(w));
69  }
70 
72  T_lp sum_lp_vec(0);
73  for (int i = 0; i < y.rows(); i++) {
74  Eigen::Matrix<T_y, Eigen::Dynamic, 1> y_row(y.row(i));
75  Eigen::Matrix<
76  typename boost::math::tools::promote_args<T_y, T_covar>::type,
77  Eigen::Dynamic, 1>
78  half(mdivide_left_tri_low(L, y_row));
79  sum_lp_vec += w(i) * dot_self(half);
80  }
81  lp -= 0.5 * sum_lp_vec;
82  }
83 
84  return lp;
85 }
86 
87 template <typename T_y, typename T_covar, typename T_w>
88 inline typename boost::math::tools::promote_args<T_y, T_covar, T_w>::type
90  const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
91  const Eigen::Matrix<T_covar, Eigen::Dynamic, Eigen::Dynamic>& L,
92  const Eigen::Matrix<T_w, Eigen::Dynamic, 1>& w) {
93  return multi_gp_cholesky_lpdf<false>(y, L, w);
94 }
95 
96 } // namespace math
97 } // namespace stan
98 #endif
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
boost::math::tools::promote_args< T_y, T_covar, T_w >::type multi_gp_cholesky_lpdf(const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const Eigen::Matrix< T_covar, Eigen::Dynamic, Eigen::Dynamic > &L, const Eigen::Matrix< T_w, Eigen::Dynamic, 1 > &w)
The log of a multivariate Gaussian Process for the given y, w, and a Cholesky factor L of the kernel ...
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
fvar< T > dot_self(const Eigen::Matrix< fvar< T >, R, C > &v)
Definition: dot_self.hpp:13
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
const double NEG_LOG_SQRT_TWO_PI
Definition: constants.hpp:156
Eigen::Matrix< fvar< T >, R1, C1 > mdivide_left_tri_low(const Eigen::Matrix< fvar< T >, R1, C1 > &A, const Eigen::Matrix< fvar< T >, R2, C2 > &b)
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.

     [ Stan Home Page ] © 2011–2018, Stan Development Team.