stan-dev / math

The Stan Math Library is a C++ template library for automatic differentiation of any order using forward, reverse, and mixed modes. It includes a range of built-in functions for probabilistic modeling, linear algebra, and equation solving.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
753 stars 188 forks source link

Add exp_quad_chol function #501

Open rtrangucci opened 7 years ago

rtrangucci commented 7 years ago

Summary:

Generate the cholesky factor of an exponentiated quadratic covariance matrix.

Description:

The prim implementation will look something like this:

template<typename T_x, typename T_alpha, typename T_rho, typename T_sigma>
Eigen::Matrix exp_quad_chol(std::vector<T_x> x,
                            T_alpha alpha,
                            T_rho rho,
                            T_sigma sigma) {
  check_positive("exp_quad_chol", "marginal variance", alpha);
  check_positive("exp_quad_chol", "length-scale", rho);
  check_positive("exp_quad_chol", sigma);
  typedef typename stan::return_type<T_x, T_alpha, T_rho, T_sigma>::type T_scal;
  Eigen::Matrix<T_scal, -1, -1> cov = cov_exp_quad(x, alpha, rho);
  for (int i = 0; i < cov.rows(); ++i)
    cov(i, i) = cov(i, i) + square(sigma);
  Eigen::Matrix<T_scal, -1, -1> L_cov = cholesky_decompose(cov);
  return L_cov
}

Additional Information:

Will need to be coupled with a rename of the parameters in cov_exp_quad. See #495.

Current Version:

v2.14.0

betanalpha commented 7 years ago

For adding the diagonal you’ll want to use Eigen as much as possible, in particular to cast sigma^2 to a diagonal matrix and add it on one go.

On Mar 3, 2017, at 3:53 PM, Rob Trangucci notifications@github.com wrote:

Summary:

Generate the cholesky factor of an exponentiated quadratic covariance matrix.

Description:

The prim implementation will look something like this:

template<typename T_x, typename T_alpha, typename T_rho, typename T_sigma> Eigen::Matrix exp_quad_chol(std::vector x, T_alpha alpha, T_rho rho, T_sigma sigma) { check_positive("exp_quad_chol", "marginal variance", alpha); check_positive("exp_quad_chol", "length-scale", rho); check_positive("exp_quad_chol", sigma); typedef typename stan::return_type<T_x, T_alpha, T_rho, T_sigma>::type T_scal; Eigen::Matrix<T_scal, -1, -1> cov = cov_exp_quad(x, alpha, rho); for (int i = 0; i < cov.rows(); ++i) cov(i, i) = cov(i, i) + square(sigma); Eigen::Matrix<T_scal, -1, -1> L_cov = cholesky_decompose(cov); return L_cov } Additional Information:

Will need to be coupled with a rename of the parameters in cov_exp_quad.

Current Version:

v2.14.0

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/stan-dev/math/issues/501, or mute the thread https://github.com/notifications/unsubscribe-auth/ABdNlizC2k4e6l69ghbslbeXugMJ1mq6ks5riH3jgaJpZM4MSvBV.

bob-carpenter commented 7 years ago

Is there an expression template in Eigen to view a single scalar as a diagonal matrix? I know there's a way to view a vector as a diagonal matrix. Can't do this with a C++ cast either way, but I assume Michael was speaking loosely here.

On Mar 4, 2017, at 9:57 AM, Michael Betancourt notifications@github.com wrote:

For adding the diagonal you’ll want to use Eigen as much as possible, in particular to cast sigma^2 to a diagonal matrix and add it on one go.

On Mar 3, 2017, at 3:53 PM, Rob Trangucci notifications@github.com wrote:

Summary:

Generate the cholesky factor of an exponentiated quadratic covariance matrix.

Description:

The prim implementation will look something like this:

template<typename T_x, typename T_alpha, typename T_rho, typename T_sigma> Eigen::Matrix exp_quad_chol(std::vector x, T_alpha alpha, T_rho rho, T_sigma sigma) { check_positive("exp_quad_chol", "marginal variance", alpha); check_positive("exp_quad_chol", "length-scale", rho); check_positive("exp_quad_chol", sigma); typedef typename stan::return_type<T_x, T_alpha, T_rho, T_sigma>::type T_scal; Eigen::Matrix<T_scal, -1, -1> cov = cov_exp_quad(x, alpha, rho); for (int i = 0; i < cov.rows(); ++i) cov(i, i) = cov(i, i) + square(sigma); Eigen::Matrix<T_scal, -1, -1> L_cov = cholesky_decompose(cov); return L_cov } Additional Information:

Will need to be coupled with a rename of the parameters in cov_exp_quad.

Current Version:

v2.14.0

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/stan-dev/math/issues/501, or mute the thread https://github.com/notifications/unsubscribe-auth/ABdNlizC2k4e6l69ghbslbeXugMJ1mq6ks5riH3jgaJpZM4MSvBV.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.