| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
lkj_corr_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_LKJ_CORR_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_LKJ_CORR_LOG_HPP
3
4 #include <stan/math/prim/mat/err/check_corr_matrix.hpp>
5 #include <stan/math/prim/scal/err/check_finite.hpp>
6 #include <stan/math/prim/scal/err/check_positive.hpp>
7 #include <stan/math/prim/scal/fun/constants.hpp>
8 #include <stan/math/prim/scal/meta/include_summand.hpp>
9 #include <stan/math/prim/scal/fun/lgamma.hpp>
10 #include <stan/math/prim/mat/fun/factor_cov_matrix.hpp>
11 #include <stan/math/prim/mat/fun/factor_U.hpp>
12 #include <stan/math/prim/mat/fun/read_corr_L.hpp>
13 #include <stan/math/prim/mat/fun/read_corr_matrix.hpp>
14 #include <stan/math/prim/mat/fun/read_cov_L.hpp>
15 #include <stan/math/prim/mat/fun/read_cov_matrix.hpp>
16 #include <stan/math/prim/mat/fun/make_nu.hpp>
17 #include <stan/math/prim/scal/fun/identity_constrain.hpp>
18 #include <stan/math/prim/scal/fun/identity_free.hpp>
19 #include <stan/math/prim/scal/fun/positive_constrain.hpp>
20 #include <stan/math/prim/scal/fun/positive_free.hpp>
21 #include <stan/math/prim/scal/fun/lb_constrain.hpp>
22 #include <stan/math/prim/scal/fun/lb_free.hpp>
23 #include <stan/math/prim/scal/fun/ub_constrain.hpp>
24 #include <stan/math/prim/scal/fun/ub_free.hpp>
25 #include <stan/math/prim/scal/fun/lub_constrain.hpp>
26 #include <stan/math/prim/scal/fun/lub_free.hpp>
27 #include <stan/math/prim/scal/fun/prob_constrain.hpp>
28 #include <stan/math/prim/scal/fun/prob_free.hpp>
29 #include <stan/math/prim/scal/fun/corr_constrain.hpp>
30 #include <stan/math/prim/scal/fun/corr_free.hpp>
31 #include <stan/math/prim/mat/fun/unit_vector_constrain.hpp>
32 #include <stan/math/prim/mat/fun/unit_vector_free.hpp>
33 #include <stan/math/prim/mat/fun/simplex_constrain.hpp>
34 #include <stan/math/prim/mat/fun/simplex_free.hpp>
35 #include <stan/math/prim/mat/fun/ordered_constrain.hpp>
36 #include <stan/math/prim/mat/fun/ordered_free.hpp>
37 #include <stan/math/prim/mat/fun/positive_ordered_constrain.hpp>
38 #include <stan/math/prim/mat/fun/positive_ordered_free.hpp>
39 #include <stan/math/prim/mat/fun/cholesky_factor_constrain.hpp>
40 #include <stan/math/prim/mat/fun/cholesky_factor_free.hpp>
41 #include <stan/math/prim/mat/fun/cholesky_corr_constrain.hpp>
42 #include <stan/math/prim/mat/fun/cholesky_corr_free.hpp>
43 #include <stan/math/prim/mat/fun/corr_matrix_constrain.hpp>
44 #include <stan/math/prim/mat/fun/corr_matrix_free.hpp>
45 #include <stan/math/prim/mat/fun/cov_matrix_constrain.hpp>
46 #include <stan/math/prim/mat/fun/cov_matrix_free.hpp>
47 #include <stan/math/prim/mat/fun/cov_matrix_constrain_lkj.hpp>
48 #include <stan/math/prim/mat/fun/cov_matrix_free_lkj.hpp>
49 #include <stan/math/prim/mat/fun/sum.hpp>
50
51 namespace stan {
52 namespace math {
53
54 template <typename T_shape>
55 T_shape do_lkj_constant(const T_shape& eta, const unsigned int& K) {
56 using stan::math::sum;
57 using stan::math::lgamma;
58
59 // Lewandowski, Kurowicka, and Joe (2009) theorem 5
60 T_shape constant;
61 const int Km1 = K - 1;
62 if (eta == 1.0) {
63 // C++ integer division is appropriate in this block
64 Eigen::VectorXd numerator(Km1 / 2);
65 for (int k = 1; k <= numerator.rows(); k++)
66 numerator(k-1) = lgamma(2 * k);
67 constant = sum(numerator);
68 if ( (K % 2) == 1 )
69 constant += 0.25 * (K * K - 1) * LOG_PI
70 - 0.25 * (Km1 * Km1) * LOG_TWO - Km1 * lgamma((K + 1) / 2);
71 else
72 constant += 0.25 * K * (K - 2) * LOG_PI
73 + 0.25 * (3 * K * K - 4 * K) * LOG_TWO
74 + K * lgamma(K / 2) - Km1 * lgamma(K);
75 } else {
76 constant = -Km1 * lgamma(eta + 0.5 * Km1);
77 for (int k = 1; k <= Km1; k++)
78 constant += 0.5 * k * LOG_PI + lgamma(eta + 0.5 * (Km1 - k));
79 }
80 return constant;
81 }
82
83 // LKJ_Corr(y|eta) [ y correlation matrix (not covariance matrix)
84 // eta > 0; eta == 1 <-> uniform]
85 template <bool propto,
86 typename T_y, typename T_shape>
87 typename boost::math::tools::promote_args<T_y, T_shape>::type
88 lkj_corr_log(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
89 const T_shape& eta) {
90 static const char* function("stan::math::lkj_corr_log");
91
92 using stan::math::check_positive;
93 using stan::math::check_corr_matrix;
94 using stan::math::sum;
95 using boost::math::tools::promote_args;
96
97 typename promote_args<T_y, T_shape>::type lp(0.0);
98 check_positive(function, "Shape parameter", eta);
99 check_corr_matrix(function, "Correlation matrix", y);
100
101 const unsigned int K = y.rows();
102 if (K == 0)
103 return 0.0;
104
105 if (include_summand<propto, T_shape>::value)
106 lp += do_lkj_constant(eta, K);
107
108 if ( (eta == 1.0) &&
109 stan::is_constant<typename stan::scalar_type<T_shape> >::value )
110 return lp;
111
112 if (!include_summand<propto, T_y, T_shape>::value)
113 return lp;
114
115 Eigen::Matrix<T_y, Eigen::Dynamic, 1> values =
116 y.ldlt().vectorD().array().log().matrix();
117 lp += (eta - 1.0) * sum(values);
118 return lp;
119 }
120
121 template <typename T_y, typename T_shape>
122 inline
123 typename boost::math::tools::promote_args<T_y, T_shape>::type
124 lkj_corr_log(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
125 const T_shape& eta) {
126 return lkj_corr_log<false>(y, eta);
127 }
128
129 }
130 }
131 #endif
ub_constrain.hpp
stan::math::sum
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
factor_U.hpp
stan::is_constant
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
Definition: is_constant.hpp:22
lgamma.hpp
stan::scalar_type
Metaprogram structure to determine the base scalar type of a template argument.
Definition: scalar_type.hpp:37
stan::math::lkj_corr_log
boost::math::tools::promote_args< T_y, T_shape >::type lkj_corr_log(const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const T_shape &eta)
Definition: lkj_corr_log.hpp:88
stan::math::lgamma
fvar< T > lgamma(const fvar< T > &x)
Definition: lgamma.hpp:15
cov_matrix_free_lkj.hpp
positive_free.hpp
check_positive.hpp
stan::math::LOG_PI
const double LOG_PI
Definition: constants.hpp:170
lb_free.hpp
cov_matrix_free.hpp
corr_matrix_constrain.hpp
cholesky_factor_free.hpp
stan::math::include_summand
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: include_summand.hpp:36
stan::math::LOG_TWO
const double LOG_TWO
Definition: constants.hpp:177
unit_vector_free.hpp
include_summand.hpp
corr_constrain.hpp
prob_free.hpp
ordered_constrain.hpp
check_corr_matrix.hpp
cov_matrix_constrain_lkj.hpp
read_cov_L.hpp
simplex_free.hpp
cholesky_corr_constrain.hpp
stan::math::check_positive
bool check_positive(const char *function, const char *name, const T_y &y)
Return true if y is positive.
Definition: check_positive.hpp:68
identity_free.hpp
simplex_constrain.hpp
prob_constrain.hpp
cholesky_factor_constrain.hpp
factor_cov_matrix.hpp
ub_free.hpp
cholesky_corr_free.hpp
constants.hpp
check_finite.hpp
cov_matrix_constrain.hpp
identity_constrain.hpp
sum.hpp
positive_ordered_constrain.hpp
positive_constrain.hpp
lub_constrain.hpp
read_cov_matrix.hpp
read_corr_matrix.hpp
stan::math::do_lkj_constant
T_shape do_lkj_constant(const T_shape &eta, const unsigned int &K)
Definition: lkj_corr_log.hpp:55
read_corr_L.hpp
unit_vector_constrain.hpp
lub_free.hpp
positive_ordered_free.hpp
corr_matrix_free.hpp
make_nu.hpp
lb_constrain.hpp
stan::math::check_corr_matrix
bool check_corr_matrix(const char *function, const char *name, const Eigen::Matrix< T_y, Dynamic, Dynamic > &y)
Return true if the specified matrix is a valid correlation matrix.
Definition: check_corr_matrix.hpp:46
ordered_free.hpp
corr_free.hpp

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