Automatic Differentiation
 
Loading...
Searching...
No Matches
corr_matrix_free.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_CONSTRAINT_CORR_MATRIX_FREE_HPP
2#define STAN_MATH_PRIM_CONSTRAINT_CORR_MATRIX_FREE_HPP
3
8#include <cmath>
9
10namespace stan {
11namespace math {
12
33template <typename T, require_eigen_t<T>* = nullptr>
34Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, 1> corr_matrix_free(const T& y) {
35 using Eigen::Array;
36 using Eigen::Dynamic;
37
38 check_square("corr_matrix_free", "y", y);
39 check_nonzero_size("corr_matrix_free", "y", y);
40
41 Eigen::Index k = y.rows();
42 Eigen::Index k_choose_2 = (k * (k - 1)) / 2;
43 Eigen::Matrix<value_type_t<T>, Dynamic, 1> x(k_choose_2);
44 Array<value_type_t<T>, Dynamic, 1> sds(k);
45 bool successful = factor_cov_matrix(y, x.array(), sds);
46 if (!successful) {
47 throw_domain_error("corr_matrix_free", "factor_cov_matrix failed on y", y,
48 "");
49 }
50 check_bounded("corr_matrix_free", "log(sd)", sds, -CONSTRAINT_TOLERANCE,
52 return x;
53}
54
62template <typename T, require_std_vector_t<T>* = nullptr>
63auto corr_matrix_free(const T& x) {
65 x, [](auto&& v) { return corr_matrix_free(v); });
66}
67
68} // namespace math
69} // namespace stan
70
71#endif
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
Eigen::Matrix< value_type_t< T >, Eigen::Dynamic, 1 > corr_matrix_free(const T &y)
Return the vector of unconstrained partial correlations that define the specified correlation matrix ...
void throw_domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
void check_nonzero_size(const char *function, const char *name, const T_y &y)
Check if the specified matrix/vector is of non-zero size.
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds in rank and in simplexes.
bool factor_cov_matrix(const T_Sigma &Sigma, T_CPCs &&CPCs, T_sds &&sds)
This function is intended to make starting values, given a covariance matrix Sigma.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...