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

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

cholesky_corr_free.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_CHOLESKY_CORR_FREE_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_CHOLESKY_CORR_FREE_HPP
3
4 #include <stan/math/prim/mat/err/check_square.hpp>
5 #include <stan/math/prim/mat/fun/Eigen.hpp>
6 #include <stan/math/prim/scal/fun/corr_constrain.hpp>
7 #include <stan/math/prim/scal/fun/square.hpp>
8 #include <stan/math/prim/scal/fun/corr_free.hpp>
9 #include <cmath>
10
11 namespace stan {
12
13 namespace math {
14
15
16 template <typename T>
17 Eigen::Matrix<T, Eigen::Dynamic, 1>
18 cholesky_corr_free(const Eigen::Matrix
19 <T, Eigen::Dynamic, Eigen::Dynamic>& x) {
20 using std::sqrt;
21 using Eigen::Matrix;
22 using Eigen::Dynamic;
23 using stan::math::square;
24
25 stan::math::check_square("cholesky_corr_free", "x", x);
26 // should validate lower-triangular, unit lengths
27
28 int K = (x.rows() * (x.rows() - 1)) / 2;
29 Matrix<T, Dynamic, 1> z(K);
30 int k = 0;
31 for (int i = 1; i < x.rows(); ++i) {
32 z(k++) = corr_free(x(i, 0));
33 double sum_sqs = square(x(i, 0));
34 for (int j = 1; j < i; ++j) {
35 z(k++) = corr_free(x(i, j) / sqrt(1.0 - sum_sqs));
36 sum_sqs += square(x(i, j));
37 }
38 }
39 return z;
40 }
41 }
42
43 }
44
45 #endif
stan::math::cholesky_corr_free
Eigen::Matrix< T, Eigen::Dynamic, 1 > cholesky_corr_free(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &x)
Definition: cholesky_corr_free.hpp:18
stan::math::sqrt
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:15
stan::math::corr_free
T corr_free(const T y)
Return the unconstrained scalar that when transformed to a valid correlation produces the specified v...
Definition: corr_free.hpp:29
stan
Definition: log_sum_exp.hpp:8
square.hpp
stan::math::square
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:15
corr_constrain.hpp
Eigen.hpp
stan::math::check_square
bool check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is square.
Definition: check_square.hpp:28
check_square.hpp
corr_free.hpp

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