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>
34inline Eigen::Matrix<value_type_t<T>, Eigen::Dynamic, 1> corr_matrix_free(
35 const T& y) {
36 using Eigen::Array;
37 using Eigen::Dynamic;
38
39 check_square("corr_matrix_free", "y", y);
40 check_nonzero_size("corr_matrix_free", "y", y);
41
42 Eigen::Index k = y.rows();
43 Eigen::Index k_choose_2 = (k * (k - 1)) / 2;
44 Eigen::Matrix<value_type_t<T>, Dynamic, 1> x(k_choose_2);
45 Array<value_type_t<T>, Dynamic, 1> sds(k);
46 bool successful = factor_cov_matrix(y, x.array(), sds);
47 if (!successful) {
48 throw_domain_error("corr_matrix_free", "factor_cov_matrix failed on y", y,
49 "");
50 }
51 check_bounded("corr_matrix_free", "log(sd)", sds, -CONSTRAINT_TOLERANCE,
53 return x;
54}
55
63template <typename T, require_std_vector_t<T>* = nullptr>
64inline auto corr_matrix_free(T&& x) {
65 return apply_vector_unary<T>::apply(std::forward<T>(x), [](auto&& v) {
66 return corr_matrix_free(std::forward<decltype(v)>(v));
67 });
68}
69
70} // namespace math
71} // namespace stan
72
73#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 ...