Automatic Differentiation
 
Loading...
Searching...
No Matches
cholesky_corr_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP
3
12#include <cstdint>
13#include <utility>
14
15namespace stan {
16namespace math {
17
18namespace internal {
25enum class CorrOrder : uint8_t { row = 0, column = 1 };
26
37template <CorrOrder Order>
38inline Eigen::Index cholesky_corr_position(Eigen::Index row, Eigen::Index col,
39 Eigen::Index K) {
40 if constexpr (Order == CorrOrder::column) {
41 return col * (K - 1) - (col * (col - 1)) / 2 + (row - col - 1);
42 } else {
43 return (row * (row - 1)) / 2 + col;
44 }
45}
46
60inline auto cholesky_corr_log_jacobian_coefficients(Eigen::Index K) {
61 const Eigen::Index k_choose_2 = (K * (K - 1)) / 2;
62 return make_holder(
63 [](auto&& pos) {
64 const auto row = (0.5 * (1.0 + (1.0 + 8.0 * pos).sqrt())).floor();
65 return (row + 1.0 - (pos - 0.5 * row * (row - 1.0))).matrix();
66 },
67 Eigen::ArrayXd::LinSpaced(k_choose_2, 0.0, k_choose_2 - 1.0));
68}
69
84inline auto corr_matrix_log_jacobian_coefficients(Eigen::Index K) {
85 const Eigen::Index k_choose_2 = (K * (K - 1)) / 2;
86 const double two_K_minus_one = 2.0 * K - 1.0;
87 return make_holder(
88 [K = static_cast<double>(K), two_K_minus_one](auto&& pos) {
89 return (K
90 - (0.5
91 * (two_K_minus_one
92 - (two_K_minus_one * two_K_minus_one - 8.0 * pos).sqrt()))
93 .floor())
94 .matrix();
95 },
96 Eigen::ArrayXd::LinSpaced(k_choose_2, 0.0, k_choose_2 - 1.0));
97}
98
114template <CorrOrder Order, typename EigVec, typename LogCoshVec,
116inline Eigen::Matrix<value_type_t<EigVec>, Eigen::Dynamic, Eigen::Dynamic>
117cholesky_corr_factor(EigVec&& y, LogCoshVec&& log_cosh_y, Eigen::Index K) {
118 using T_scalar = value_type_t<EigVec>;
119 Eigen::Matrix<T_scalar, Eigen::Dynamic, Eigen::Dynamic> L
120 = Eigen::Matrix<T_scalar, Eigen::Dynamic, Eigen::Dynamic>::Zero(K, K);
121 if (K == 0) {
122 return L;
123 }
124 decltype(auto) y_ref = to_ref(std::forward<EigVec>(y));
125 decltype(auto) log_cosh_y_ref = to_ref(std::forward<LogCoshVec>(log_cosh_y));
126 L.coeffRef(0, 0) = 1;
127 for (Eigen::Index row = 1; row < K; ++row) {
128 T_scalar acc = 0;
129 for (Eigen::Index col = 0; col < row; ++col) {
130 const Eigen::Index pos = cholesky_corr_position<Order>(row, col, K);
131 L.coeffRef(row, col) = tanh(y_ref.coeff(pos)) * exp(-acc);
132 acc += log_cosh_y_ref.coeff(pos);
133 }
134 L.coeffRef(row, row) = exp(-acc);
135 }
136 return L;
137}
138} // namespace internal
139
150template <typename EigVec, require_eigen_col_vector_t<EigVec>* = nullptr>
151inline Eigen::Matrix<value_type_t<EigVec>, Eigen::Dynamic, Eigen::Dynamic>
152cholesky_corr_constrain(EigVec&& y, int K) {
153 int k_choose_2 = (K * (K - 1)) / 2;
154 check_size_match("cholesky_corr_constrain", "constrain size", y.size(),
155 "k_choose_2", k_choose_2);
156 decltype(auto) y_ref = to_ref(std::forward<EigVec>(y));
157 return internal::cholesky_corr_factor<internal::CorrOrder::row>(
158 y_ref, log_cosh(y_ref), K);
159}
160
173template <typename EigVec, typename Lp,
176inline Eigen::Matrix<value_type_t<EigVec>, Eigen::Dynamic, Eigen::Dynamic>
177cholesky_corr_constrain(EigVec&& y, int K, Lp& lp) {
178 int k_choose_2 = (K * (K - 1)) / 2;
179 check_size_match("cholesky_corr_constrain", "y.size()", y.size(),
180 "k_choose_2", k_choose_2);
181 decltype(auto) y_ref = to_ref(std::forward<EigVec>(y));
182 decltype(auto) log_cosh_y = to_ref(log_cosh(y_ref));
184 log_cosh_y);
185 return internal::cholesky_corr_factor<internal::CorrOrder::row>(
186 y_ref, std::move(log_cosh_y), K);
187}
188
199template <typename T, require_std_vector_t<T>* = nullptr>
200inline auto cholesky_corr_constrain(T&& y, int K) {
201 return apply_vector_unary<T>::apply(std::forward<T>(y), [K](auto&& v) {
202 return cholesky_corr_constrain(std::forward<decltype(v)>(v), K);
203 });
204}
205
219template <typename T, typename Lp, require_std_vector_t<T>* = nullptr,
220 require_convertible_t<return_type_t<T>, Lp>* = nullptr>
221inline auto cholesky_corr_constrain(T&& y, int K, Lp& lp) {
222 return apply_vector_unary<T>::apply(std::forward<T>(y), [&lp, K](auto&& v) {
223 return cholesky_corr_constrain(std::forward<decltype(v)>(v), K, lp);
224 });
225}
226
245template <bool Jacobian, typename T, typename Lp,
247inline auto cholesky_corr_constrain(T&& y, int K, Lp& lp) {
248 if constexpr (Jacobian) {
249 return cholesky_corr_constrain(std::forward<T>(y), K, lp);
250 } else {
251 return cholesky_corr_constrain(std::forward<T>(y), K);
252 }
253}
254
255} // namespace math
256} // namespace stan
257#endif
require_t< std::is_convertible< std::decay_t< T >, std::decay_t< S > > > require_convertible_t
Require types T and S satisfies std::is_convertible.
require_t< is_eigen_col_vector< std::decay_t< T > > > require_eigen_col_vector_t
Require type satisfies is_eigen_col_vector.
Definition is_vector.hpp:98
require_all_t< is_eigen_vector< std::decay_t< Types > >... > require_all_eigen_vector_t
Require all of the types satisfy is_eigen_vector.
auto col(T_x &&x, size_t j)
Return the specified column of the specified kernel generator expression using start-at-1 indexing.
Definition col.hpp:23
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
auto corr_matrix_log_jacobian_coefficients(Eigen::Index K)
Return a lazy expression of the coefficients c for which the log absolute Jacobian determinant of cor...
CorrOrder
Order of the partial correlations in the unconstrained vector: row for cholesky_corr_constrain (row 1...
auto cholesky_corr_log_jacobian_coefficients(Eigen::Index K)
Return a lazy expression of the coefficients c for which the log absolute Jacobian determinant of cho...
Eigen::Index cholesky_corr_position(Eigen::Index row, Eigen::Index col, Eigen::Index K)
Return the position in the unconstrained vector of the partial correlation for element (row,...
Eigen::Matrix< value_type_t< EigVec >, Eigen::Dynamic, Eigen::Dynamic > cholesky_corr_factor(EigVec &&y, LogCoshVec &&log_cosh_y, Eigen::Index K)
Return the Cholesky factor of a correlation matrix from unconstrained values y, whose partial correla...
Eigen::Matrix< value_type_t< EigVec >, Eigen::Dynamic, Eigen::Dynamic > cholesky_corr_constrain(EigVec &&y, int K)
Return the Cholesky factor of a K x K correlation matrix from the K (K - 1) / 2 unconstrained values ...
auto make_holder(F &&func, Args &&... args)
Calls given function with given arguments.
Definition holder.hpp:488
fvar< T > sqrt(const fvar< T > &x)
Definition sqrt.hpp:18
fvar< T > floor(const fvar< T > &x)
Definition floor.hpp:13
fvar< T > tanh(const fvar< T > &x)
Return the hyperbolic tangent of the argument.
Definition tanh.hpp:27
fvar< T > log_cosh(const fvar< T > &x)
Return the natural logarithm of the hyperbolic cosine of the argument.
Definition log_cosh.hpp:21
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
void check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Check if the provided sizes match.
auto dot_product(const T_a &a, const T_b &b)
Returns the dot product of the specified vectors.
fvar< T > exp(const fvar< T > &x)
Definition exp.hpp:15
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...