| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

cholesky_corr_constrain.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_CHOLESKY_CORR_CONSTRAIN_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_CHOLESKY_CORR_CONSTRAIN_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/scal/err/check_size_match.hpp>
6 #include <stan/math/prim/scal/fun/log1m.hpp>
7 #include <stan/math/prim/scal/fun/square.hpp>
8 #include <stan/math/prim/scal/fun/corr_constrain.hpp>
9 #include <cmath>
10 #include <iostream>
11
12 namespace stan {
13 namespace math {
14
15 template <typename T>
16 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
17 cholesky_corr_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& y,
18 int K) {
19 using std::sqrt;
20 using Eigen::Matrix;
21 using Eigen::Dynamic;
22 int k_choose_2 = (K * (K - 1)) / 2;
23 check_size_match("cholesky_corr_constrain",
24 "y.size()", y.size(),
25 "k_choose_2", k_choose_2);
26 Matrix<T, Dynamic, 1> z(k_choose_2);
27 for (int i = 0; i < k_choose_2; ++i)
28 z(i) = corr_constrain(y(i));
29 Matrix<T, Dynamic, Dynamic> x(K, K);
30 if (K == 0) return x;
31 T zero(0);
32 for (int j = 1; j < K; ++j)
33 for (int i = 0; i < j; ++i)
34 x(i, j) = zero;
35 x(0, 0) = 1;
36 int k = 0;
37 for (int i = 1; i < K; ++i) {
38 x(i, 0) = z(k++);
39 T sum_sqs(square(x(i, 0)));
40 for (int j = 1; j < i; ++j) {
41 x(i, j) = z(k++) * sqrt(1.0 - sum_sqs);
42 sum_sqs += square(x(i, j));
43 }
44 x(i, i) = sqrt(1.0 - sum_sqs);
45 }
46 return x;
47 }
48
49 // FIXME to match above after debugged
50 template <typename T>
51 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
52 cholesky_corr_constrain(const Eigen::Matrix<T, Eigen::Dynamic, 1>& y,
53 int K,
54 T& lp) {
55 using std::sqrt;
56 using Eigen::Matrix;
57 using Eigen::Dynamic;
58 int k_choose_2 = (K * (K - 1)) / 2;
59 check_size_match("cholesky_corr_constrain",
60 "y.size()", y.size(),
61 "k_choose_2", k_choose_2);
62 Matrix<T, Dynamic, 1> z(k_choose_2);
63 for (int i = 0; i < k_choose_2; ++i)
64 z(i) = corr_constrain(y(i), lp);
65 Matrix<T, Dynamic, Dynamic> x(K, K);
66 if (K == 0) return x;
67 T zero(0);
68 for (int j = 1; j < K; ++j)
69 for (int i = 0; i < j; ++i)
70 x(i, j) = zero;
71 x(0, 0) = 1;
72 int k = 0;
73 for (int i = 1; i < K; ++i) {
74 x(i, 0) = z(k++);
75 T sum_sqs = square(x(i, 0));
76 for (int j = 1; j < i; ++j) {
77 lp += 0.5 * log1m(sum_sqs);
78 x(i, j) = z(k++) * sqrt(1.0 - sum_sqs);
79 sum_sqs += square(x(i, j));
80 }
81 x(i, i) = sqrt(1.0 - sum_sqs);
82 }
83 return x;
84 }
85
86 }
87 }
88 #endif
stan::math::sqrt
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:14
check_size_match.hpp
stan
Definition: log_sum_exp.hpp:8
square.hpp
stan::math::square
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:14
corr_constrain.hpp
stan::math::check_size_match
bool check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Return true if the provided sizes match.
Definition: check_size_match.hpp:30
log1m.hpp
stan::math::cholesky_corr_constrain
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > cholesky_corr_constrain(const Eigen::Matrix< T, Eigen::Dynamic, 1 > &y, int K)
Definition: cholesky_corr_constrain.hpp:17
Eigen.hpp
stan::math::log1m
fvar< T > log1m(const fvar< T > &x)
Definition: log1m.hpp:15
stan::math::corr_constrain
T corr_constrain(const T x)
Return the result of transforming the specified scalar to have a valid correlation value between -1 a...
Definition: corr_constrain.hpp:24

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