Stan Math Library  2.14.0
reverse mode automatic differentiation
lkj_corr_cholesky_lpdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_PROB_LKJ_CORR_CHOLESKY_LPDF_HPP
2 #define STAN_MATH_PRIM_MAT_PROB_LKJ_CORR_CHOLESKY_LPDF_HPP
3 
47 
48 namespace stan {
49  namespace math {
50 
51  // LKJ_Corr(L|eta) [ L Cholesky factor of correlation matrix
52  // eta > 0; eta == 1 <-> uniform]
53  template <bool propto,
54  typename T_covar, typename T_shape>
55  typename boost::math::tools::promote_args<T_covar, T_shape>::type
56  lkj_corr_cholesky_lpdf(const Eigen::Matrix
57  <T_covar, Eigen::Dynamic, Eigen::Dynamic>& L,
58  const T_shape& eta) {
59  static const char* function("lkj_corr_cholesky_lpdf");
60 
61  using boost::math::tools::promote_args;
62 
63  typedef typename promote_args<T_covar, T_shape>::type lp_ret;
64  lp_ret lp(0.0);
65  check_positive(function, "Shape parameter", eta);
66  check_lower_triangular(function, "Random variable", L);
67 
68  const unsigned int K = L.rows();
69  if (K == 0)
70  return 0.0;
71 
73  lp += do_lkj_constant(eta, K);
75  const int Km1 = K - 1;
76  Eigen::Matrix<T_covar, Eigen::Dynamic, 1> log_diagonals =
77  L.diagonal().tail(Km1).array().log();
78  Eigen::Matrix<lp_ret, Eigen::Dynamic, 1> values(Km1);
79  for (int k = 0; k < Km1; k++)
80  values(k) = (Km1 - k - 1) * log_diagonals(k);
81  if ( (eta == 1.0) &&
83  lp += sum(values);
84  return(lp);
85  }
86  values += multiply(2.0 * eta - 2.0, log_diagonals);
87  lp += sum(values);
88  }
89 
90  return lp;
91  }
92 
93  template <typename T_covar, typename T_shape>
94  inline
95  typename boost::math::tools::promote_args<T_covar, T_shape>::type
96  lkj_corr_cholesky_lpdf(const Eigen::Matrix
97  <T_covar, Eigen::Dynamic, Eigen::Dynamic>& L,
98  const T_shape& eta) {
99  return lkj_corr_cholesky_lpdf<false>(L, eta);
100  }
101 
102  }
103 }
104 #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
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
Definition: is_constant.hpp:22
void check_lower_triangular(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Check if the specified matrix is lower triangular.
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:20
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
boost::math::tools::promote_args< T_covar, T_shape >::type lkj_corr_cholesky_lpdf(const Eigen::Matrix< T_covar, Eigen::Dynamic, Eigen::Dynamic > &L, const T_shape &eta)
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
T_shape do_lkj_constant(const T_shape &eta, const unsigned int &K)

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