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

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