| 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_cholesky_log.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_LKJ_CORR_CHOLESKY_LOG_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_LKJ_CORR_CHOLESKY_LOG_HPP
3
4 #include <stan/math/prim/scal/err/check_finite.hpp>
5 #include <stan/math/prim/scal/err/check_positive.hpp>
6 #include <stan/math/prim/scal/fun/constants.hpp>
7 #include <stan/math/prim/scal/meta/include_summand.hpp>
8 #include <stan/math/prim/mat/fun/factor_cov_matrix.hpp>
9 #include <stan/math/prim/mat/fun/factor_U.hpp>
10 #include <stan/math/prim/mat/fun/read_corr_L.hpp>
11 #include <stan/math/prim/mat/fun/read_corr_matrix.hpp>
12 #include <stan/math/prim/mat/fun/read_cov_L.hpp>
13 #include <stan/math/prim/mat/fun/read_cov_matrix.hpp>
14 #include <stan/math/prim/mat/fun/make_nu.hpp>
15 #include <stan/math/prim/scal/fun/identity_constrain.hpp>
16 #include <stan/math/prim/scal/fun/identity_free.hpp>
17 #include <stan/math/prim/scal/fun/positive_constrain.hpp>
18 #include <stan/math/prim/scal/fun/positive_free.hpp>
19 #include <stan/math/prim/scal/fun/lb_constrain.hpp>
20 #include <stan/math/prim/scal/fun/lb_free.hpp>
21 #include <stan/math/prim/scal/fun/ub_constrain.hpp>
22 #include <stan/math/prim/scal/fun/ub_free.hpp>
23 #include <stan/math/prim/scal/fun/lub_constrain.hpp>
24 #include <stan/math/prim/scal/fun/lub_free.hpp>
25 #include <stan/math/prim/scal/fun/prob_constrain.hpp>
26 #include <stan/math/prim/scal/fun/prob_free.hpp>
27 #include <stan/math/prim/scal/fun/corr_constrain.hpp>
28 #include <stan/math/prim/scal/fun/corr_free.hpp>
29 #include <stan/math/prim/mat/fun/unit_vector_constrain.hpp>
30 #include <stan/math/prim/mat/fun/unit_vector_free.hpp>
31 #include <stan/math/prim/mat/fun/simplex_constrain.hpp>
32 #include <stan/math/prim/mat/fun/simplex_free.hpp>
33 #include <stan/math/prim/mat/fun/ordered_constrain.hpp>
34 #include <stan/math/prim/mat/fun/ordered_free.hpp>
35 #include <stan/math/prim/mat/fun/positive_ordered_constrain.hpp>
36 #include <stan/math/prim/mat/fun/positive_ordered_free.hpp>
37 #include <stan/math/prim/mat/fun/cholesky_factor_constrain.hpp>
38 #include <stan/math/prim/mat/fun/cholesky_factor_free.hpp>
39 #include <stan/math/prim/mat/fun/cholesky_corr_constrain.hpp>
40 #include <stan/math/prim/mat/fun/cholesky_corr_free.hpp>
41 #include <stan/math/prim/mat/fun/corr_matrix_constrain.hpp>
42 #include <stan/math/prim/mat/fun/corr_matrix_free.hpp>
43 #include <stan/math/prim/mat/fun/cov_matrix_constrain.hpp>
44 #include <stan/math/prim/mat/fun/cov_matrix_free.hpp>
45 #include <stan/math/prim/mat/fun/cov_matrix_constrain_lkj.hpp>
46 #include <stan/math/prim/mat/fun/cov_matrix_free_lkj.hpp>
47 #include <stan/math/prim/mat/prob/lkj_corr_log.hpp>
48
49 namespace stan {
50 namespace math {
51
52 // LKJ_Corr(L|eta) [ L Cholesky factor of correlation matrix
53 // eta > 0; eta == 1 <-> uniform]
54 template <bool propto,
55 typename T_covar, typename T_shape>
56 typename boost::math::tools::promote_args<T_covar, T_shape>::type
57 lkj_corr_cholesky_log(const Eigen::Matrix
58 <T_covar, Eigen::Dynamic, Eigen::Dynamic>& L,
59 const T_shape& eta) {
60 static const char* function("stan::math::lkj_corr_cholesky_log");
61
62 using boost::math::tools::promote_args;
63 using stan::math::check_positive;
64 using stan::math::check_lower_triangular;
65 using stan::math::sum;
66
67 typename promote_args<T_covar, T_shape>::type lp(0.0);
68 check_positive(function, "Shape parameter", eta);
69 check_lower_triangular(function, "Random variable", L);
70
71 const unsigned int K = L.rows();
72 if (K == 0)
73 return 0.0;
74
75 if (include_summand<propto, T_shape>::value)
76 lp += do_lkj_constant(eta, K);
77 if (include_summand<propto, T_covar, T_shape>::value) {
78 const int Km1 = K - 1;
79 Eigen::Matrix<T_covar, Eigen::Dynamic, 1> log_diagonals =
80 L.diagonal().tail(Km1).array().log();
81 Eigen::Matrix<T_covar, Eigen::Dynamic, 1> values(Km1);
82 for (int k = 0; k < Km1; k++)
83 values(k) = (Km1 - k - 1) * log_diagonals(k);
84 if ( (eta == 1.0) &&
85 stan::is_constant<typename stan::scalar_type<T_shape> >::value) {
86 lp += sum(values);
87 return(lp);
88 }
89 values += (2.0 * eta - 2.0) * log_diagonals;
90 lp += sum(values);
91 }
92
93 return lp;
94 }
95
96 template <typename T_covar, typename T_shape>
97 inline
98 typename boost::math::tools::promote_args<T_covar, T_shape>::type
99 lkj_corr_cholesky_log(const Eigen::Matrix
100 <T_covar, Eigen::Dynamic, Eigen::Dynamic>& L,
101 const T_shape& eta) {
102 return lkj_corr_cholesky_log<false>(L, eta);
103 }
104
105 }
106 }
107 #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
lkj_corr_log.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
cov_matrix_free_lkj.hpp
positive_free.hpp
check_positive.hpp
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
unit_vector_free.hpp
include_summand.hpp
corr_constrain.hpp
prob_free.hpp
ordered_constrain.hpp
stan::math::lkj_corr_cholesky_log
boost::math::tools::promote_args< T_covar, T_shape >::type lkj_corr_cholesky_log(const Eigen::Matrix< T_covar, Eigen::Dynamic, Eigen::Dynamic > &L, const T_shape &eta)
Definition: lkj_corr_cholesky_log.hpp:57
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
stan::math::check_lower_triangular
bool check_lower_triangular(const char *function, const char *name, const Eigen::Matrix< T_y, Dynamic, Dynamic > &y)
Return true if the specified matrix is lower triangular.
Definition: check_lower_triangular.hpp:35
cholesky_corr_free.hpp
constants.hpp
check_finite.hpp
cov_matrix_constrain.hpp
identity_constrain.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
ordered_free.hpp
corr_free.hpp

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