Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
multi_gp_cholesky.hpp
Go to the documentation of this file.
1 #ifndef STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__MULTI_GP_CHOLESKY_HPP
2 #define STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__MULTI_GP_CHOLESKY_HPP
3 
8 #include <stan/prob/traits.hpp>
9 #include <stan/agrad/rev.hpp>
10 #include <stan/meta/traits.hpp>
12 #include <stan/math/matrix/row.hpp>
14 #include <stan/math/matrix/log.hpp>
16 #include <stan/math/matrix/sum.hpp>
18 
19 namespace stan {
20  namespace prob {
21  // MultiGPCholesky(y|L,w) [y.rows() = w.size(), y.cols() = Sigma.rows();
22  // Sigma symmetric, non-negative, definite]
42  template <bool propto,
43  typename T_y, typename T_covar, typename T_w>
44  typename boost::math::tools::promote_args<T_y,T_covar,T_w>::type
45  multi_gp_cholesky_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
46  const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& L,
47  const Eigen::Matrix<T_w,Eigen::Dynamic,1>& w) {
48  static const char* function = "stan::prob::multi_gp_cholesky_log(%1%)";
49  typedef typename boost::math::tools::promote_args<T_y,T_covar,T_w>::type T_lp;
50  T_lp lp(0.0);
51 
54  using stan::math::sum;
55  using stan::math::log;
56 
60 
61  check_size_match(function,
62  y.rows(), "Size of random variable (rows y)",
63  w.size(), "Size of kernel scales (w)",
64  &lp);
65  check_size_match(function,
66  y.cols(), "Size of random variable",
67  L.rows(), "rows of covariance parameter",
68  &lp);
69  check_finite(function, w, "Kernel scales", &lp);
70  check_positive(function, w, "Kernel scales", &lp);
71  check_finite(function, y, "Random variable", &lp);
72 
73  if (y.rows() == 0)
74  return lp;
75 
77  lp += NEG_LOG_SQRT_TWO_PI * y.rows() * y.cols();
78  }
79 
81  lp -= L.diagonal().array().log().sum() * y.rows();
82  }
83 
85  lp += 0.5 * y.cols() * sum(log(w));
86  }
87 
89  T_lp sum_lp_vec(0.0);
90  for (int i = 0; i < y.rows(); i++) {
91  Eigen::Matrix<T_y, Eigen::Dynamic, 1> y_row( y.row(i) );
92  Eigen::Matrix<typename boost::math::tools::promote_args<T_y,T_covar>::type,
93  Eigen::Dynamic, 1>
94  half(mdivide_left_tri_low(L,y_row));
95  sum_lp_vec += w(i) * dot_self(half);
96  }
97  lp -= 0.5*sum_lp_vec;
98  }
99 
100  return lp;
101  }
102 
103  template <typename T_y, typename T_covar, typename T_w>
104  inline
105  typename boost::math::tools::promote_args<T_y,T_covar,T_w>::type
106  multi_gp_cholesky_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
107  const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& L,
108  const Eigen::Matrix<T_w,Eigen::Dynamic,1>& w) {
109  return multi_gp_cholesky_log<false>(y,L,w);
110  }
111  }
112 }
113 
114 #endif
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R1, C2 > mdivide_left_tri_low(const Eigen::Matrix< T1, R1, C1 > &A, const Eigen::Matrix< T2, R2, C2 > &b)
fvar< T > dot_self(const Eigen::Matrix< fvar< T >, R, C > &v)
Definition: dot_self.hpp:16
double dot_self(const std::vector< double > &x)
Definition: dot_self.hpp:11
boost::math::tools::promote_args< T_y, T_covar, T_w >::type multi_gp_cholesky_log(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 ...
bool check_size_match(const char *function, T_size1 i, const char *name_i, T_size2 j, const char *name_j, T_result *result)
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is finite.
fvar< T > sum(const Eigen::Matrix< fvar< T >, R, C > &m)
Definition: sum.hpp:14
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:35
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result)
Eigen::Matrix< T, Rows, Cols > log(const Eigen::Matrix< T, Rows, Cols > &m)
Return the element-wise logarithm of the matrix or vector.
Definition: log.hpp:16
double sum(std::vector< double > &x)
Definition: sum.hpp:10
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)
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15

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