read_corr_L.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_PRIM_MAT_FUN_READ_CORR_L_HPP
#define STAN_MATH_PRIM_MAT_FUN_READ_CORR_L_HPP
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/scal/fun/log1m.hpp>
#include <stan/math/prim/scal/fun/square.hpp>
#include <stan/math/prim/mat/fun/sum.hpp>
#include <cstddef>
#include <iostream>
namespace stan {
namespace math {
template <typename T>
Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
read_corr_L(const Eigen::Array<T, Eigen::Dynamic, 1>& CPCs, // on (-1, 1)
size_t K) {
Eigen::Array<T, Eigen::Dynamic, 1> temp;
Eigen::Array<T, Eigen::Dynamic, 1> acc(K-1);
acc.setOnes();
// Cholesky factor of correlation matrix
Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic> L(K, K);
L.setZero();
size_t position = 0;
size_t pull = K - 1;
L(0, 0) = 1.0;
L.col(0).tail(pull) = temp = CPCs.head(pull);
acc.tail(pull) = T(1.0) - temp.square();
for (size_t i = 1; i < (K - 1); i++) {
position += pull;
pull--;
temp = CPCs.segment(position, pull);
L(i, i) = sqrt(acc(i-1));
L.col(i).tail(pull) = temp * acc.tail(pull).sqrt();
acc.tail(pull) *= T(1.0) - temp.square();
}
L(K-1, K-1) = sqrt(acc(K-2));
return L.matrix();
}
template <typename T>
Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>
read_corr_L(const Eigen::Array<T, Eigen::Dynamic, 1>& CPCs,
size_t K,
T& log_prob) {
Eigen::Matrix<T, Eigen::Dynamic, 1> values(CPCs.rows() - 1);
size_t pos = 0;
// no need to abs() because this Jacobian determinant
// is strictly positive (and triangular)
// see inverse of Jacobian in equation 11 of LKJ paper
for (size_t k = 1; k <= (K - 2); k++)
for (size_t i = k + 1; i <= K; i++) {
values(pos) = (K - k - 1) * log1m(square(CPCs(pos)));
pos++;
}
log_prob += 0.5 * sum(values);
return read_corr_L(CPCs, K);
}
}
}
#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
sum.hpp
stan::math::log1m
fvar< T > log1m(const fvar< T > &x)
Definition: log1m.hpp:13
stan::math::read_corr_L
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > read_corr_L(const Eigen::Array< T, Eigen::Dynamic, 1 > &CPCs, size_t K)
Return the Cholesky factor of the correlation matrix of the specified dimensionality corresponding to...
Definition: read_corr_L.hpp:37

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