Stan Math Library  2.11.0
reverse mode automatic differentiation
multi_gp_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_MULTI_GP_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MULTI_GP_LOG_HPP
3 
11 
19 
20 namespace stan {
21  namespace math {
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
47  <T_covar, Eigen::Dynamic, Eigen::Dynamic>& Sigma,
48  const Eigen::Matrix<T_w, Eigen::Dynamic, 1>& w) {
49  static const char* function("stan::math::multi_gp_log");
50  typedef typename boost::math::tools::promote_args<T_y, T_covar, T_w>::type
51  T_lp;
52  T_lp lp(0.0);
53 
54  using stan::math::sum;
55  using stan::math::log;
59 
67 
68  check_positive(function, "Kernel rows", Sigma.rows());
69  check_finite(function, "Kernel", Sigma);
70  check_symmetric(function, "Kernel", Sigma);
71 
73  check_ldlt_factor(function, "LDLT_Factor of Sigma", ldlt_Sigma);
74 
75  check_size_match(function,
76  "Size of random variable (rows y)", y.rows(),
77  "Size of kernel scales (w)", w.size());
78  check_size_match(function,
79  "Size of random variable", y.cols(),
80  "rows of covariance parameter", Sigma.rows());
81  check_positive_finite(function, "Kernel scales", w);
82  check_finite(function, "Random variable", y);
83 
84  if (y.rows() == 0)
85  return lp;
86 
88  lp += NEG_LOG_SQRT_TWO_PI * y.rows() * y.cols();
89  }
90 
92  lp -= 0.5 * log_determinant_ldlt(ldlt_Sigma) * y.rows();
93  }
94 
96  lp += (0.5 * y.cols()) * sum(log(w));
97  }
98 
100  Eigen::Matrix<T_w, Eigen::Dynamic, Eigen::Dynamic>
101  w_mat(w.asDiagonal());
102  Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic> yT(y.transpose());
103  lp -= 0.5 * trace_gen_inv_quad_form_ldlt(w_mat, ldlt_Sigma, yT);
104  }
105 
106  return lp;
107  }
108 
109  template <typename T_y, typename T_covar, typename T_w>
110  inline
111  typename boost::math::tools::promote_args<T_y, T_covar, T_w>::type
112  multi_gp_log(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
113  const Eigen::Matrix<T_covar, Eigen::Dynamic, Eigen::Dynamic>&
114  Sigma,
115  const Eigen::Matrix<T_w, Eigen::Dynamic, 1>& w) {
116  return multi_gp_log<false>(y, Sigma, w);
117  }
118  }
119 }
120 
121 #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
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:15
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...
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
bool check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Return true if the provided sizes match.
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.
const double NEG_LOG_SQRT_TWO_PI
Definition: constants.hpp:184
bool check_finite(const char *function, const char *name, const T_y &y)
Return true if y is finite.
bool check_symmetric(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is symmetric.
T log_determinant_ldlt(stan::math::LDLT_factor< T, R, C > &A)
bool check_positive_finite(const char *function, const char *name, const T_y &y)
Return true if y is positive and finite.
bool check_ldlt_factor(const char *function, const char *name, stan::math::LDLT_factor< T, R, C > &A)
Return true if the argument is a valid stan::math::LDLT_factor.

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