Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
matrix_normal.hpp
Go to the documentation of this file.
1 #ifndef STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__MATRIX_NORMAL_HPP
2 #define STAN__PROB__DISTRIBUTIONS__MULTIVARIATE__CONTINUOUS__MATRIX_NORMAL_HPP
3 
7 #include <stan/prob/traits.hpp>
8 #include <stan/agrad/rev.hpp>
9 #include <stan/meta/traits.hpp>
11 #include <stan/math/matrix/log.hpp>
17 
18 namespace stan {
19  namespace prob {
38  template <bool propto,
39  typename T_y, typename T_Mu, typename T_Sigma, typename T_D>
40  typename boost::math::tools::promote_args<T_y,T_Mu,T_Sigma,T_D>::type
41  matrix_normal_prec_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
42  const Eigen::Matrix<T_Mu,Eigen::Dynamic,Eigen::Dynamic>& Mu,
43  const Eigen::Matrix<T_Sigma,Eigen::Dynamic,Eigen::Dynamic>& Sigma,
44  const Eigen::Matrix<T_D,Eigen::Dynamic,Eigen::Dynamic>& D) {
45  static const char* function = "stan::prob::matrix_normal_prec_log(%1%)";
46  typename boost::math::tools::promote_args<T_y,T_Mu,T_Sigma,T_D>::type lp(0.0);
47 
58 
59  check_size_match(function,
60  Sigma.rows(), "Rows of Sigma",
61  Sigma.cols(), "columns of Sigma",
62  &lp);
63  check_positive(function, Sigma.rows(), "Sigma rows", &lp);
64  check_finite(function, Sigma, "Sigma", &lp);
65  check_symmetric(function, Sigma, "Sigma", &lp);
66 
67  LDLT_factor<T_Sigma,Eigen::Dynamic,Eigen::Dynamic> ldlt_Sigma(Sigma);
68  check_ldlt_factor(function,ldlt_Sigma,"LDLT_Factor of Sigma",&lp);
69  check_size_match(function,
70  D.rows(), "Rows of D",
71  D.cols(), "Columns of D",
72  &lp);
73  check_positive(function, D.rows(), "D rows", &lp);
74  check_finite(function, D, "D", &lp);
75  check_symmetric(function, D, "Sigma", &lp);
76 
77  LDLT_factor<T_D,Eigen::Dynamic,Eigen::Dynamic> ldlt_D(D);
78  check_ldlt_factor(function,ldlt_D,"LDLT_Factor of D",&lp);
79  check_size_match(function,
80  y.rows(), "Rows of random variable",
81  Mu.rows(), "Rows of location parameter",
82  &lp);
83  check_size_match(function,
84  y.cols(), "Columns of random variable",
85  Mu.cols(), "Columns of location parameter",
86  &lp);
87  check_size_match(function,
88  y.rows(), "Rows of random variable",
89  Sigma.rows(), "Rows of Sigma",
90  &lp);
91  check_size_match(function,
92  y.cols(), "Columns of random variable",
93  D.rows(), "Rows of D",
94  &lp);
95  check_finite(function, Mu, "Location parameter", &lp);
96  check_finite(function, y, "Random variable", &lp);
97 
99  lp += NEG_LOG_SQRT_TWO_PI * y.cols() * y.rows();
100 
102  lp += log_determinant_ldlt(ldlt_Sigma) * (0.5 * y.rows());
103  }
104 
106  lp += log_determinant_ldlt(ldlt_D) * (0.5 * y.cols());
107  }
108 
110  lp -= 0.5 * trace_gen_quad_form(D,Sigma,subtract(y,Mu));
111  }
112  return lp;
113  }
114 
115  template <typename T_y, typename T_Mu, typename T_Sigma, typename T_D>
116  typename boost::math::tools::promote_args<T_y,T_Mu,T_Sigma,T_D>::type
117  matrix_normal_prec_log(const Eigen::Matrix<T_y,Eigen::Dynamic,Eigen::Dynamic>& y,
118  const Eigen::Matrix<T_Mu,Eigen::Dynamic,Eigen::Dynamic>& Mu,
119  const Eigen::Matrix<T_Sigma,Eigen::Dynamic,Eigen::Dynamic>& Sigma,
120  const Eigen::Matrix<T_D,Eigen::Dynamic,Eigen::Dynamic>& D) {
121  return matrix_normal_prec_log<false>(y,Mu,Sigma,D);
122  }
123  }
124 }
125 
126 #endif
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 > trace_gen_quad_form(const Eigen::Matrix< fvar< T >, RD, CD > &D, const Eigen::Matrix< fvar< T >, RA, CA > &A, const Eigen::Matrix< fvar< T >, RB, CB > &B)
boost::math::tools::promote_args< T_y, T_Mu, T_Sigma, T_D >::type matrix_normal_prec_log(const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const Eigen::Matrix< T_Mu, Eigen::Dynamic, Eigen::Dynamic > &Mu, const Eigen::Matrix< T_Sigma, Eigen::Dynamic, Eigen::Dynamic > &Sigma, const Eigen::Matrix< T_D, Eigen::Dynamic, Eigen::Dynamic > &D)
The log of the matrix normal density for the given y, mu, Sigma and D where Sigma and D are given as ...
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.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:35
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
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result)
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)
double trace_gen_quad_form(const Eigen::Matrix< double, RD, CD > &D, const Eigen::Matrix< double, RA, CA > &A, const Eigen::Matrix< double, RB, CB > &B)
Compute trace(D B^T A B).

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