Stan Math Library  2.14.0
reverse mode automatic differentiation
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 
49 
50 namespace stan {
51  namespace math {
52 
53  // LKJ_Corr(y|eta) [ y correlation matrix (not covariance matrix)
54  // eta > 0; eta == 1 <-> uniform]
55  template <bool propto,
56  typename T_y, typename T_shape>
57  typename boost::math::tools::promote_args<T_y, T_shape>::type
58  lkj_corr_log(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
59  const T_shape& eta) {
60  static const char* function("lkj_corr_log");
61 
62  using boost::math::tools::promote_args;
63 
64  typename promote_args<T_y, T_shape>::type lp(0.0);
65  check_positive(function, "Shape parameter", eta);
66  check_corr_matrix(function, "Correlation matrix", y);
67 
68  const unsigned int K = y.rows();
69  if (K == 0)
70  return 0.0;
71 
73  lp += do_lkj_constant(eta, K);
74 
75  if ( (eta == 1.0) &&
77  return lp;
78 
80  return lp;
81 
82  Eigen::Matrix<T_y, Eigen::Dynamic, 1> values =
83  y.ldlt().vectorD().array().log().matrix();
84  lp += (eta - 1.0) * sum(values);
85  return lp;
86  }
87 
88  template <typename T_y, typename T_shape>
89  inline
90  typename boost::math::tools::promote_args<T_y, T_shape>::type
91  lkj_corr_log(const Eigen::Matrix<T_y, Eigen::Dynamic, Eigen::Dynamic>& y,
92  const T_shape& eta) {
93  return lkj_corr_log<false>(y, eta);
94  }
95 
96  }
97 }
98 #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
Metaprogram structure to determine the base scalar type of a template argument.
Definition: scalar_type.hpp:33
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)
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
void check_corr_matrix(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Check if the specified matrix is a valid correlation matrix.
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.