| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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 <boost/throw_exception.hpp>
8 #include <cmath>
9 #include <sstream>
10 #include <stdexcept>
11
12 namespace stan {
13
14 namespace math {
15
36 template <typename T>
37 Eigen::Matrix<T, Eigen::Dynamic, 1>
38 corr_matrix_free(const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>&
39 y) {
40 using Eigen::Array;
41 using Eigen::Dynamic;
42 using Eigen::Matrix;
43 using stan::math::index_type;
44 typedef typename index_type<Matrix<T, Dynamic, 1> >::type size_type;
45
46 size_type k = y.rows();
47 if (y.cols() != k)
48 throw std::domain_error("y is not a square matrix");
49 if (k == 0)
50 throw std::domain_error("y has no elements");
51 size_type k_choose_2 = (k * (k-1)) / 2;
52 Array<T, Dynamic, 1> x(k_choose_2);
53 Array<T, Dynamic, 1> sds(k);
54 bool successful = factor_cov_matrix(y, x, sds);
55 if (!successful)
56 throw std::runtime_error("factor_cov_matrix failed on y");
57 for (size_type i = 0; i < k; ++i) {
58 // sds on log scale unconstrained
59 if (fabs(sds[i] - 0.0) >= CONSTRAINT_TOLERANCE) {
60 std::stringstream s;
61 s << "all standard deviations must be zero."
62 << " found log(sd[" << i << "])=" << sds[i] << std::endl;
63 BOOST_THROW_EXCEPTION(std::runtime_error(s.str()));
64 }
65 }
66 return x.matrix();
67 }
68 }
69
70 }
71
72 #endif
stan::math::fabs
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:14
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
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:38

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