Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
multi_gp.hpp
Go to the documentation of this file.
1 #ifndef STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__MULTI_GP_HPP
2 #define STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__MULTI_GP_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/log.hpp>
14 #include <stan/math/matrix/sum.hpp>
15 
19 
20 namespace stan {
21  namespace prob {
22  // MultiGP(y|Sigma,w) [y.rows() = w.size(), y.cols() = Sigma.rows();
23  // 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_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
46  const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& Sigma,
47  const Eigen::Matrix<T_w,Eigen::Dynamic,1>& w) {
48  static const char* function = "stan::prob::multi_gp_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 
52  using stan::math::sum;
53  using stan::math::log;
57 
65 
66  check_size_match(function,
67  Sigma.rows(), "Rows of kernel matrix",
68  Sigma.cols(), "columns of kernel matrix",
69  &lp);
70  check_positive(function, Sigma.rows(), "Kernel rows", &lp);
71  check_finite(function, Sigma, "Kernel", &lp);
72  check_symmetric(function, Sigma, "Kernel", &lp);
73 
74  LDLT_factor<T_covar,Eigen::Dynamic,Eigen::Dynamic> ldlt_Sigma(Sigma);
75  check_ldlt_factor(function,ldlt_Sigma,"LDLT_Factor of Sigma",&lp);
76 
77  check_size_match(function,
78  y.rows(), "Size of random variable (rows y)",
79  w.size(), "Size of kernel scales (w)",
80  &lp);
81  check_size_match(function,
82  y.cols(), "Size of random variable",
83  Sigma.rows(), "rows of covariance parameter",
84  &lp);
85  check_positive_finite(function, w, "Kernel scales", &lp);
86  check_finite(function, y, "Random variable", &lp);
87 
88  if (y.rows() == 0)
89  return lp;
90 
92  lp += NEG_LOG_SQRT_TWO_PI * y.rows() * y.cols();
93  }
94 
96  lp -= 0.5 * log_determinant_ldlt(ldlt_Sigma) * y.rows();
97  }
98 
100  lp += (0.5 * y.cols()) * sum(log(w));
101  }
102 
104  Eigen::Matrix<T_w,Eigen::Dynamic,Eigen::Dynamic> w_mat(w.asDiagonal());
105  Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic> yT(y.transpose());
106  lp -= 0.5 * trace_gen_inv_quad_form_ldlt(w_mat,ldlt_Sigma,yT);
107  }
108 
109  return lp;
110  }
111 
112  template <typename T_y, typename T_covar, typename T_w>
113  inline
114  typename boost::math::tools::promote_args<T_y,T_covar,T_w>::type
115  multi_gp_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
116  const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& Sigma,
117  const Eigen::Matrix<T_w,Eigen::Dynamic,1>& w) {
118  return multi_gp_log<false>(y,Sigma,w);
119  }
120  }
121 }
122 
123 #endif
boost::math::tools::promote_args< T_y, T_covar, T_w >::type multi_gp_log(const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const Eigen::Matrix< T_covar, Eigen::Dynamic, Eigen::Dynamic > &Sigma, const Eigen::Matrix< T_w, Eigen::Dynamic, 1 > &w)
The log of a multivariate Gaussian Process for the given y, Sigma, and w.
Definition: multi_gp.hpp:45
bool check_positive_finite(const char *function, const T_y &y, const char *name, T_result *result)
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
bool check_symmetric(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result)
Return true if the specified matrix is symmetric.
bool check_ldlt_factor(const char *function, stan::math::LDLT_factor< T, R, C > &A, const char *name, T_result *result)
Return true if the underlying matrix is positive definite.
boost::enable_if_c<!stan::is_var< T1 >::value &&!stan::is_var< T2 >::value &&!stan::is_var< T3 >::value, typename boost::math::tools::promote_args< T1, T2, T3 >::type >::type trace_gen_inv_quad_form_ldlt(const Eigen::Matrix< T1, R1, C1 > &D, const stan::math::LDLT_factor< T2, R2, C2 > &A, const Eigen::Matrix< T3, R3, C3 > &B)
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
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is nan.
var log_determinant_ldlt(stan::math::LDLT_factor< var, R, C > &A)
T log_determinant_ldlt(stan::math::LDLT_factor< T, R, C > &A)
boost::enable_if_c< boost::is_same< T1, var >::value||boost::is_same< T2, var >::value||boost::is_same< T3, var >::value, var >::type trace_gen_inv_quad_form_ldlt(const Eigen::Matrix< T1, R1, C1 > &D, const stan::math::LDLT_factor< T2, R2, C2 > &A, const Eigen::Matrix< T3, R3, C3 > &B)
Compute the trace of an inverse quadratic form.
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15

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