Stan Math Library: stan/math/prim/mat/fun/corr_matrix_free.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

corr_matrix_free.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_CORR_MATRIX_FREE_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_CORR_MATRIX_FREE_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/meta/index_type.hpp>
6 #include <stan/math/prim/mat/err/constraint_tolerance.hpp>
7 #include <stan/math/prim/mat/fun/factor_cov_matrix.hpp>
8 #include <boost/throw_exception.hpp>
9 #include <cmath>
10 #include <sstream>
11 #include <stdexcept>
12
13 namespace stan {
14
15 namespace math {
16
37 template <typename T>
38 Eigen::Matrix<T, Eigen::Dynamic, 1>
39 corr_matrix_free(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>&
40 y) {
41 using Eigen::Array;
42 using Eigen::Dynamic;
43 using Eigen::Matrix;
44 using stan::math::index_type;
45 typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
46
47 size_type k = y.rows();
48 if (y.cols() != k)
49 throw std::domain_error("y is not a square matrix");
50 if (k == 0)
51 throw std::domain_error("y has no elements");
52 size_type k_choose_2 = (k * (k-1)) / 2;
53 Array<T, Dynamic, 1> x(k_choose_2);
54 Array<T, Dynamic, 1> sds(k);
55 bool successful = factor_cov_matrix(y, x, sds);
56 if (!successful)
57 throw std::runtime_error("factor_cov_matrix failed on y");
58 for (size_type i = 0; i < k; ++i) {
59 // sds on log scale unconstrained
60 if (fabs(sds[i] - 0.0) >= CONSTRAINT_TOLERANCE) {
61 std::stringstream s;
62 s << "all standard deviations must be zero."
63 << " found log(sd[" << i << "])=" << sds[i] << std::endl;
64 BOOST_THROW_EXCEPTION(std::runtime_error(s.str()));
65 }
66 }
67 return x.matrix();
68 }
69 }
70
71 }
72
73 #endif
stan::math::fabs
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:14
stan
Definition: log_sum_exp.hpp:8
constraint_tolerance.hpp
stan::math::CONSTRAINT_TOLERANCE
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds In rank and in simplexes.
Definition: constraint_tolerance.hpp:11
stan::math::size_type
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Type for sizes and indexes in an Eigen matrix with double e.
Definition: typedefs.hpp:13
stan::math::index_type
Primary template class for the metaprogram to compute the index type of a container.
Definition: index_type.hpp:19
stan::math::factor_cov_matrix
bool factor_cov_matrix(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &Sigma, Eigen::Array< T, Eigen::Dynamic, 1 > &CPCs, Eigen::Array< T, Eigen::Dynamic, 1 > &sds)
This function is intended to make starting values, given a covariance matrix Sigma.
Definition: factor_cov_matrix.hpp:27
index_type.hpp
stan::math::domain_error
void 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.
Definition: domain_error.hpp:32
Eigen.hpp
factor_cov_matrix.hpp
stan::math::corr_matrix_free
Eigen::Matrix< T, Eigen::Dynamic, 1 > corr_matrix_free(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &y)
Return the vector of unconstrained partial correlations that define the specified correlation matrix ...
Definition: corr_matrix_free.hpp:39

[ Stan Home Page ] © 2011–2016, Stan Development Team.