Stan Math Library  2.6.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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 
4 #include <boost/random/normal_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
24 
25 namespace stan {
26 
27  namespace math {
28  using Eigen::Dynamic;
29 
47  template <bool propto,
48  typename T_y, typename T_loc, typename T_covar>
49  typename return_type<T_y, T_loc, T_covar>::type
51  const T_loc& mu,
52  const T_covar& L) {
53  static const char* function("stan::math::multi_normal_cholesky_log");
54  typedef typename scalar_type<T_covar>::type T_covar_elem;
55  typedef typename return_type<T_y, T_loc, T_covar>::type lp_type;
56  lp_type lp(0.0);
57 
62  using stan::math::sum;
63 
67 
68  VectorViewMvt<const T_y> y_vec(y);
69  VectorViewMvt<const T_loc> mu_vec(mu);
70  // size of std::vector of Eigen vectors
71  size_t size_vec = max_size_mvt(y, mu);
72 
73  // Check if every vector of the array has the same size
74  int size_y = y_vec[0].size();
75  int size_mu = mu_vec[0].size();
76  if (size_vec > 1) {
77  int size_y_old = size_y;
78  int size_y_new;
79  for (size_t i = 1, size_ = length_mvt(y); i < size_; i++) {
80  int size_y_new = y_vec[i].size();
81  check_size_match(function,
82  "Size of one of the vectors of "
83  "the random variable", size_y_new,
84  "Size of another vector of the "
85  "random variable", size_y_old);
86  size_y_old = size_y_new;
87  }
88  int size_mu_old = size_mu;
89  int size_mu_new;
90  for (size_t i = 1, size_ = length_mvt(mu); i < size_; i++) {
91  int size_mu_new = mu_vec[i].size();
92  check_size_match(function,
93  "Size of one of the vectors of "
94  "the location variable", size_mu_new,
95  "Size of another vector of the "
96  "location variable", size_mu_old);
97  size_mu_old = size_mu_new;
98  }
99  (void) size_y_old;
100  (void) size_y_new;
101  (void) size_mu_old;
102  (void) size_mu_new;
103  }
104 
105  check_size_match(function,
106  "Size of random variable", size_y,
107  "size of location parameter", size_mu);
108  check_size_match(function,
109  "Size of random variable", size_y,
110  "rows of covariance parameter", L.rows());
111  check_size_match(function,
112  "Size of random variable", size_y,
113  "columns of covariance parameter", L.cols());
114 
115  for (size_t i = 0; i < size_vec; i++) {
116  check_finite(function, "Location parameter", mu_vec[i]);
117  check_not_nan(function, "Random variable", y_vec[i]);
118  }
119 
120  if (size_y == 0)
121  return lp;
122 
124  lp += NEG_LOG_SQRT_TWO_PI * size_y * size_vec;
125 
127  lp -= L.diagonal().array().log().sum() * size_vec;
128 
130  lp_type sum_lp_vec(0.0);
131  for (size_t i = 0; i < size_vec; i++) {
132  Eigen::Matrix<typename return_type<T_y, T_loc>::type, Dynamic, 1>
133  y_minus_mu(size_y);
134  for (int j = 0; j < size_y; j++)
135  y_minus_mu(j) = y_vec[i](j)-mu_vec[i](j);
136  Eigen::Matrix<typename return_type<T_y, T_loc, T_covar>::type,
137  Dynamic, 1>
138  half(mdivide_left_tri_low(L, y_minus_mu));
139  // FIXME: this code does not compile. revert after fixing subtract()
140  // Eigen::Matrix<typename
141  // boost::math::tools::promote_args<T_covar,
142  // typename value_type<T_loc>::type,
143  // typename value_type<T_y>::type>::type>::type,
144  // Dynamic, 1>
145  // half(mdivide_left_tri_low(L, subtract(y, mu)));
146  sum_lp_vec += dot_self(half);
147  }
148  lp -= 0.5*sum_lp_vec;
149  }
150  return lp;
151  }
152 
153  template <typename T_y, typename T_loc, typename T_covar>
154  inline
156  multi_normal_cholesky_log(const T_y& y, const T_loc& mu, const T_covar& L) {
157  return multi_normal_cholesky_log<false>(y, mu, L);
158  }
159 
160  }
161 }
162 
163 #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
size_t max_size_mvt(const T1 &x1, const T2 &x2)
bool check_not_nan(const char *function, const char *name, const T_y &y)
Return true if y is not NaN.
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R, C > subtract(const Eigen::Matrix< T1, R, C > &m1, const Eigen::Matrix< T2, R, C > &m2)
Return the result of subtracting the second specified matrix from the first specified matrix...
Definition: subtract.hpp:27
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:20
scalar_type_helper< is_vector< T >::value, T >::type type
Definition: scalar_type.hpp:38
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...
size_t size_
Definition: dot_self.hpp:18
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.
size_t length_mvt(const T &)
Definition: length_mvt.hpp:12
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.
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)

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