Stan Math Library  2.20.0
reverse mode automatic differentiation
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 
8 #include <cmath>
9 
10 namespace stan {
11 namespace math {
12 
13 template <typename T>
14 Eigen::Matrix<T, Eigen::Dynamic, 1> cholesky_corr_free(
15  const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& x) {
16  using Eigen::Dynamic;
17  using Eigen::Matrix;
18  using std::sqrt;
19 
20  check_square("cholesky_corr_free", "x", x);
21  // should validate lower-triangular, unit lengths
22 
23  int K = (x.rows() * (x.rows() - 1)) / 2;
24  Matrix<T, Dynamic, 1> z(K);
25  int k = 0;
26  for (int i = 1; i < x.rows(); ++i) {
27  z(k++) = corr_free(x(i, 0));
28  double sum_sqs = square(x(i, 0));
29  for (int j = 1; j < i; ++j) {
30  z(k++) = corr_free(x(i, j) / sqrt(1.0 - sum_sqs));
31  sum_sqs += square(x(i, j));
32  }
33  }
34  return z;
35 }
36 
37 } // namespace math
38 } // namespace stan
39 #endif
Eigen::Matrix< T, Eigen::Dynamic, 1 > cholesky_corr_free(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &x)
fvar< T > sqrt(const fvar< T > &x)
Definition: sqrt.hpp:13
void check_square(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is square.
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:12
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:28

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