Automatic Differentiation
 
Loading...
Searching...
No Matches
factor_cov_matrix.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_FACTOR_COV_MATRIX_HPP
2#define STAN_MATH_PRIM_FUN_FACTOR_COV_MATRIX_HPP
3
6#include <cstddef>
7
8namespace stan {
9namespace math {
10
24template <typename T_Sigma, typename T_CPCs, typename T_sds,
25 require_eigen_t<T_Sigma>* = nullptr,
26 require_all_eigen_vector_t<T_CPCs, T_sds>* = nullptr,
27 require_all_vt_same<T_Sigma, T_CPCs, T_sds>* = nullptr>
28bool factor_cov_matrix(const T_Sigma& Sigma, T_CPCs&& CPCs, T_sds&& sds) {
29 using T_scalar = value_type_t<T_Sigma>;
30 size_t K = sds.rows();
31 const Eigen::Ref<const plain_type_t<T_Sigma>>& Sigma_ref = Sigma;
32 sds = Sigma_ref.diagonal().array();
33 if ((sds <= 0.0).any()) {
34 return false;
35 }
36 sds = sds.sqrt();
37
38 Eigen::DiagonalMatrix<T_scalar, Eigen::Dynamic> D(K);
39 D.diagonal() = sds.inverse();
40 sds = sds.log(); // now unbounded
41
42 Eigen::Matrix<T_scalar, Eigen::Dynamic, Eigen::Dynamic> R = D * Sigma_ref * D;
43 // to hopefully prevent pivoting due to floating point error
44 R.diagonal().setOnes();
45 Eigen::LDLT<Eigen::Matrix<T_scalar, Eigen::Dynamic, Eigen::Dynamic>> ldlt;
46 ldlt = R.ldlt();
47 if (!ldlt.isPositive()) {
48 return false;
49 }
50 Eigen::Matrix<T_scalar, Eigen::Dynamic, Eigen::Dynamic> U = ldlt.matrixU();
51 factor_U(U, CPCs);
52 return true;
53}
54
55} // namespace math
56} // namespace stan
57
58#endif
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
constexpr bool any(T x)
Return true if any values in the input are true.
Definition any.hpp:21
void factor_U(const T_U &U, T_CPCs &&CPCs)
This function is intended to make starting values, given a unit upper-triangular matrix U such that U...
Definition factor_U.hpp:25
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 ...