Stan Math Library  2.14.0
reverse mode automatic differentiation
multi_normal_cholesky_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_CHOLESKY_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_MULTI_NORMAL_CHOLESKY_LOG_HPP
3 
23 #include <boost/random/normal_distribution.hpp>
24 #include <boost/random/variate_generator.hpp>
25 
26 namespace stan {
27  namespace math {
45  template <bool propto,
46  typename T_y, typename T_loc, typename T_covar>
49  const T_loc& mu,
50  const T_covar& L) {
51  static const char* function("multi_normal_cholesky_log");
52  typedef typename scalar_type<T_covar>::type T_covar_elem;
53  typedef typename return_type<T_y, T_loc, T_covar>::type lp_type;
54  lp_type lp(0.0);
55 
56 
57  VectorViewMvt<const T_y> y_vec(y);
58  VectorViewMvt<const T_loc> mu_vec(mu);
59  size_t size_vec = max_size_mvt(y, mu);
60 
61  int size_y = y_vec[0].size();
62  int size_mu = mu_vec[0].size();
63  if (size_vec > 1) {
64  int size_y_old = size_y;
65  int size_y_new;
66  for (size_t i = 1, size_ = length_mvt(y); i < size_; i++) {
67  int size_y_new = y_vec[i].size();
68  check_size_match(function,
69  "Size of one of the vectors of "
70  "the random variable", size_y_new,
71  "Size of another vector of the "
72  "random variable", size_y_old);
73  size_y_old = size_y_new;
74  }
75  int size_mu_old = size_mu;
76  int size_mu_new;
77  for (size_t i = 1, size_ = length_mvt(mu); i < size_; i++) {
78  int size_mu_new = mu_vec[i].size();
79  check_size_match(function,
80  "Size of one of the vectors of "
81  "the location variable", size_mu_new,
82  "Size of another vector of the "
83  "location variable", size_mu_old);
84  size_mu_old = size_mu_new;
85  }
86  (void) size_y_old;
87  (void) size_y_new;
88  (void) size_mu_old;
89  (void) size_mu_new;
90  }
91 
92  check_size_match(function,
93  "Size of random variable", size_y,
94  "size of location parameter", size_mu);
95  check_size_match(function,
96  "Size of random variable", size_y,
97  "rows of covariance parameter", L.rows());
98  check_size_match(function,
99  "Size of random variable", size_y,
100  "columns of covariance parameter", L.cols());
101 
102  for (size_t i = 0; i < size_vec; i++) {
103  check_finite(function, "Location parameter", mu_vec[i]);
104  check_not_nan(function, "Random variable", y_vec[i]);
105  }
106 
107  if (size_y == 0)
108  return lp;
109 
111  lp += NEG_LOG_SQRT_TWO_PI * size_y * size_vec;
112 
114  lp -= L.diagonal().array().log().sum() * size_vec;
115 
117  lp_type sum_lp_vec(0.0);
118  for (size_t i = 0; i < size_vec; i++) {
119  Eigen::Matrix<typename return_type<T_y, T_loc>::type,
120  Eigen::Dynamic, 1> y_minus_mu(size_y);
121  for (int j = 0; j < size_y; j++)
122  y_minus_mu(j) = y_vec[i](j)-mu_vec[i](j);
123  Eigen::Matrix<typename return_type<T_y, T_loc, T_covar>::type,
124  Eigen::Dynamic, 1>
125  half(mdivide_left_tri_low(L, y_minus_mu));
126  // FIXME: this code does not compile. revert after fixing subtract()
127  // Eigen::Matrix<typename
128  // boost::math::tools::promote_args<T_covar,
129  // typename value_type<T_loc>::type,
130  // typename value_type<T_y>::type>::type>::type,
131  // Eigen::Dynamic, 1>
132  // half(mdivide_left_tri_low(L, subtract(y, mu)));
133  sum_lp_vec += dot_self(half);
134  }
135  lp -= 0.5*sum_lp_vec;
136  }
137  return lp;
138  }
139 
140  template <typename T_y, typename T_loc, typename T_covar>
141  inline
143  multi_normal_cholesky_log(const T_y& y, const T_loc& mu, const T_covar& L) {
144  return multi_normal_cholesky_log<false>(y, mu, L);
145  }
146 
147  }
148 }
149 #endif
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
size_t max_size_mvt(const T1 &x1, const T2 &x2)
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: scalar_type.hpp:34
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:16
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type
Definition: return_type.hpp:27
return_type< T_y, T_loc, T_covar >::type multi_normal_cholesky_log(const T_y &y, const T_loc &mu, const T_covar &L)
The log of the multivariate normal density for the given y, mu, and a Cholesky factor L of the varian...
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
size_t size_
Definition: dot_self.hpp:18
const double NEG_LOG_SQRT_TWO_PI
Definition: constants.hpp:181
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)
VectorViewMvt is a template expression that wraps either an Eigen::Matrix or a std::vector<Eigen::Mat...
size_t length_mvt(const Eigen::Matrix< T, R, C > &)
Definition: length_mvt.hpp:12

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