| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

read_corr_L.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_READ_CORR_L_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_READ_CORR_L_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/scal/fun/log1m.hpp>
6 #include <stan/math/prim/scal/fun/square.hpp>
7 #include <stan/math/prim/mat/fun/sum.hpp>
8 #include <cstddef>
9 #include <iostream>
10
11 namespace stan {
12 namespace math {
13
35 template <typename T>
36 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
37 read_corr_L(const Eigen::Array<T, Eigen::Dynamic, 1>& CPCs, // on (-1, 1)
38 const size_t K) {
39 Eigen::Array<T, Eigen::Dynamic, 1> temp;
40 Eigen::Array<T, Eigen::Dynamic, 1> acc(K-1);
41 acc.setOnes();
42 // Cholesky factor of correlation matrix
43 Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic> L(K, K);
44 L.setZero();
45
46 size_t position = 0;
47 size_t pull = K - 1;
48
49 L(0, 0) = 1.0;
50 L.col(0).tail(pull) = temp = CPCs.head(pull);
51 acc.tail(pull) = T(1.0) - temp.square();
52 for (size_t i = 1; i < (K - 1); i++) {
53 position += pull;
54 pull--;
55 temp = CPCs.segment(position, pull);
56 L(i, i) = sqrt(acc(i-1));
57 L.col(i).tail(pull) = temp * acc.tail(pull).sqrt();
58 acc.tail(pull) *= T(1.0) - temp.square();
59 }
60 L(K-1, K-1) = sqrt(acc(K-2));
61 return L.matrix();
62 }
63
88 template <typename T>
89 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
90 read_corr_L(const Eigen::Array<T, Eigen::Dynamic, 1>& CPCs,
91 const size_t K,
92 T& log_prob) {
93 Eigen::Matrix<T, Eigen::Dynamic, 1> values(CPCs.rows() - 1);
94 size_t pos = 0;
95 // no need to abs() because this Jacobian determinant
96 // is strictly positive (and triangular)
97 // see inverse of Jacobian in equation 11 of LKJ paper
98 for (size_t k = 1; k <= (K - 2); k++)
99 for (size_t i = k + 1; i <= K; i++) {
100 values(pos) = (K - k - 1) * log1m(square(CPCs(pos)));
101 pos++;
102 }
103
104 log_prob += 0.5 * sum(values);
105 return read_corr_L(CPCs, K);
106 }
107
108 }
109 }
110 #endif
stan::math::sum
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
stan::math::sqrt
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:14
stan
Definition: log_sum_exp.hpp:8
square.hpp
stan::math::square
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:14
log1m.hpp
Eigen.hpp
stan::math::read_corr_L
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > read_corr_L(const Eigen::Array< T, Eigen::Dynamic, 1 > &CPCs, const size_t K)
Return the Cholesky factor of the correlation matrix of the specified dimensionality corresponding to...
Definition: read_corr_L.hpp:37
sum.hpp
stan::math::log1m
fvar< T > log1m(const fvar< T > &x)
Definition: log1m.hpp:15

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