Stan  1.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs 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 
4 #include <boost/random/normal_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
6 
10 #include <stan/prob/traits.hpp>
11 #include <stan/agrad/agrad.hpp>
12 #include <stan/meta/traits.hpp>
13 #include <stan/agrad/matrix.hpp>
15 #include <stan/math/matrix/log.hpp>
21 #include <stan/math/matrix/sum.hpp>
22 
23 namespace stan {
24  namespace prob {
25  // MultiGP(y|Sigma,w) [y.rows() = w.size(), y.cols() = Sigma.rows();
26  // Sigma symmetric, non-negative, definite]
45  template <bool propto,
46  typename T_y, typename T_covar, typename T_w,
47  class Policy>
48  typename boost::math::tools::promote_args<T_y,T_w,T_covar>::type
49  multi_gp_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
50  const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& Sigma,
51  const Eigen::Matrix<T_w,Eigen::Dynamic,1>& w,
52  const Policy&) {
53  static const char* function = "stan::prob::multi_gp_log(%1%)";
54  typename boost::math::tools::promote_args<T_y,T_w,T_covar>::type lp(0.0);
55 
56  using stan::math::log;
57  using stan::math::sum;
68 
69  if (!check_size_match(function,
70  Sigma.rows(), "Rows of kernel matrix",
71  Sigma.cols(), "columns of kernel matrix",
72  &lp, Policy()))
73  return lp;
74  if (!check_positive(function, Sigma.rows(), "Kernel matrix rows", &lp, Policy()))
75  return lp;
76  if (!check_finite(function, Sigma, "Kernel", &lp, Policy()))
77  return lp;
78  if (!check_symmetric(function, Sigma, "Kernel matrix", &lp, Policy()))
79  return lp;
80  if (!check_pos_definite(function, Sigma, "Kernel matrix", &lp, Policy()))
81  return lp;
82  if (!check_size_match(function,
83  y.rows(), "Size of random variable",
84  w.size(), "Size of location parameter",
85  &lp, Policy()))
86  return lp;
87  if (!check_size_match(function,
88  y.cols(), "Size of random variable",
89  Sigma.rows(), "rows of covariance parameter",
90  &lp, Policy()))
91  return lp;
92  if (!check_finite(function, w, "Kernel scales", &lp, Policy()))
93  return lp;
94  if (!check_positive(function, w, "Kernel scales", &lp, Policy()))
95  return lp;
96  if (!check_not_nan(function, y, "Random variable", &lp, Policy()))
97  return lp;
98 
99  if (y.rows() == 0)
100  return lp;
101 
103  lp += NEG_LOG_SQRT_TWO_PI * y.rows() * y.cols();
104  }
105 
107  lp -= (0.5 * y.rows()) * log_determinant_spd(Sigma);
108  }
109 
111  lp += (0.5 * y.cols()) * sum(log(w));
112  }
113 
115  Eigen::Matrix<typename
116  boost::math::tools::promote_args<T_covar,T_y>::type,
117  Eigen::Dynamic, Eigen::Dynamic> y_Kinv(mdivide_right_spd(y,Sigma));
118 
119  lp -= 0.5 * dot_product(rows_dot_product(y_Kinv,y),w);
120  }
121  return lp;
122  }
123 
124  template <bool propto,
125  typename T_y, typename T_loc, typename T_covar>
126  inline
127  typename boost::math::tools::promote_args<T_y,T_loc,T_covar>::type
128  multi_gp_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
129  const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& Sigma,
130  const Eigen::Matrix<T_loc,Eigen::Dynamic,1>& w) {
131  return multi_gp_log<propto>(y,Sigma,w,stan::math::default_policy());
132  }
133 
134 
135  template <typename T_y, typename T_loc, typename T_covar,
136  class Policy>
137  inline
138  typename boost::math::tools::promote_args<T_y,T_loc,T_covar>::type
139  multi_gp_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
140  const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& Sigma,
141  const Eigen::Matrix<T_loc,Eigen::Dynamic,1>& w,
142  const Policy&){
143  return multi_gp_log<false>(y,Sigma,w,Policy());
144  }
145 
146 
147  template <typename T_y, typename T_loc, typename T_covar>
148  inline
149  typename boost::math::tools::promote_args<T_y,T_loc,T_covar>::type
150  multi_gp_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
151  const Eigen::Matrix<T_covar,Eigen::Dynamic,Eigen::Dynamic>& Sigma,
152  const Eigen::Matrix<T_loc,Eigen::Dynamic,1>& w) {
153  return multi_gp_log<false>(y,Sigma,w,stan::math::default_policy());
154  }
155  }
156 }
157 
158 #endif

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