Stan Math Library  2.6.3
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
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 
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
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:15
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
fvar< T > square(const fvar< T > &x)
Definition: square.hpp:15
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.

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