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
10#include <cmath>
11
12namespace stan {
13namespace math {
14
15template <typename EigVec, require_eigen_col_vector_t<EigVec>* = nullptr>
16inline Eigen::Matrix<value_type_t<EigVec>, Eigen::Dynamic, Eigen::Dynamic>
17cholesky_corr_constrain(const EigVec& y, int K) {
18 using Eigen::Dynamic;
19 using Eigen::Matrix;
20 using std::sqrt;
21 using T_scalar = value_type_t<EigVec>;
22 int k_choose_2 = (K * (K - 1)) / 2;
23 check_size_match("cholesky_corr_constrain", "constrain size", y.size(),
24 "k_choose_2", k_choose_2);
25 Matrix<T_scalar, Dynamic, 1> z = corr_constrain(y);
26 Matrix<T_scalar, Dynamic, Dynamic> x(K, K);
27 if (K == 0) {
28 return x;
29 }
30 x.setZero();
31 x.coeffRef(0, 0) = 1;
32 int k = 0;
33 for (int i = 1; i < K; ++i) {
34 x.coeffRef(i, 0) = z.coeff(k++);
35 T_scalar sum_sqs = square(x.coeff(i, 0));
36 for (int j = 1; j < i; ++j) {
37 x.coeffRef(i, j) = z.coeff(k++) * sqrt(1.0 - sum_sqs);
38 sum_sqs += square(x.coeff(i, j));
39 }
40 x.coeffRef(i, i) = sqrt(1.0 - sum_sqs);
41 }
42 return x;
43}
44
45// FIXME to match above after debugged
46template <typename EigVec, require_eigen_vector_t<EigVec>* = nullptr>
47inline Eigen::Matrix<value_type_t<EigVec>, Eigen::Dynamic, Eigen::Dynamic>
48cholesky_corr_constrain(const EigVec& y, int K, return_type_t<EigVec>& lp) {
49 using Eigen::Dynamic;
50 using Eigen::Matrix;
51 using std::sqrt;
52 using T_scalar = value_type_t<EigVec>;
53 int k_choose_2 = (K * (K - 1)) / 2;
54 check_size_match("cholesky_corr_constrain", "y.size()", y.size(),
55 "k_choose_2", k_choose_2);
56 Matrix<T_scalar, Dynamic, 1> z = corr_constrain(y, lp);
57 Matrix<T_scalar, Dynamic, Dynamic> x(K, K);
58 if (K == 0) {
59 return x;
60 }
61 x.setZero();
62 x.coeffRef(0, 0) = 1;
63 int k = 0;
64 for (int i = 1; i < K; ++i) {
65 x.coeffRef(i, 0) = z.coeff(k++);
66 T_scalar sum_sqs = square(x.coeff(i, 0));
67 for (int j = 1; j < i; ++j) {
68 lp += 0.5 * log1m(sum_sqs);
69 x.coeffRef(i, j) = z.coeff(k++) * sqrt(1.0 - sum_sqs);
70 sum_sqs += square(x.coeff(i, j));
71 }
72 x.coeffRef(i, i) = sqrt(1.0 - sum_sqs);
73 }
74 return x;
75}
76
93template <bool Jacobian, typename T, require_not_std_vector_t<T>* = nullptr>
94inline auto cholesky_corr_constrain(const T& y, int K, return_type_t<T>& lp) {
95 if (Jacobian) {
96 return cholesky_corr_constrain(y, K, lp);
97 } else {
98 return cholesky_corr_constrain(y, K);
99 }
100}
101
118template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
119inline auto cholesky_corr_constrain(const T& y, int K, return_type_t<T>& lp) {
120 return apply_vector_unary<T>::apply(y, [&lp, K](auto&& v) {
121 return cholesky_corr_constrain<Jacobian>(v, K, lp);
122 });
123}
124
125} // namespace math
126} // namespace stan
127#endif
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
fvar< T > sqrt(const fvar< T > &x)
Definition sqrt.hpp:17
Eigen::Matrix< value_type_t< EigVec >, Eigen::Dynamic, Eigen::Dynamic > cholesky_corr_constrain(const EigVec &y, int K)
plain_type_t< T > corr_constrain(const T &x)
Return the result of transforming the specified scalar or container of values to have a valid correla...
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.
fvar< T > log1m(const fvar< T > &x)
Definition log1m.hpp:12
fvar< T > square(const fvar< T > &x)
Definition square.hpp:12
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...