Skip to content
Merged
1 change: 1 addition & 0 deletions stan/math/fwd/fun.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
#include <stan/math/fwd/fun/log1p.hpp>
#include <stan/math/fwd/fun/log1p_exp.hpp>
#include <stan/math/fwd/fun/log2.hpp>
#include <stan/math/fwd/fun/log_cosh.hpp>
#include <stan/math/fwd/fun/log_determinant.hpp>
#include <stan/math/fwd/fun/log_diff_exp.hpp>
#include <stan/math/fwd/fun/log_falling_factorial.hpp>
Expand Down
27 changes: 27 additions & 0 deletions stan/math/fwd/fun/log_cosh.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef STAN_MATH_FWD_FUN_LOG_COSH_HPP
#define STAN_MATH_FWD_FUN_LOG_COSH_HPP

#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/fun/tanh.hpp>
#include <stan/math/prim/fun/log_cosh.hpp>
#include <stan/math/prim/fun/tanh.hpp>

namespace stan {
namespace math {

/**
* Return the natural logarithm of the hyperbolic cosine of the argument.
*
* @tparam T inner type of the forward-mode autodiff variable
* @param x argument
* @return `log(cosh(x))`, with derivative `tanh(x)`
*/
template <typename T>
inline fvar<T> log_cosh(const fvar<T>& x) {
return fvar<T>(log_cosh(x.val_), x.d_ * tanh(x.val_));
}

} // namespace math
} // namespace stan
#endif
26 changes: 23 additions & 3 deletions stan/math/fwd/fun/tanh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,37 @@
#include <stan/math/fwd/meta.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/fun/exp.hpp>
#include <stan/math/fwd/fun/value_of_rec.hpp>
#include <stan/math/prim/fun/exp.hpp>
#include <stan/math/prim/fun/fabs.hpp>
#include <stan/math/prim/fun/tanh.hpp>
#include <cmath>
#include <stan/math/prim/fun/value_of_rec.hpp>
#include <complex>

namespace stan {
namespace math {

/**
* Return the hyperbolic tangent of the argument. For `|x| >= 0.5` the
* derivative `1 - tanh(x)^2` is computed as `4 e / (1 + e)^2` with
* `e = exp(-2 |x|)`, which neither cancels nor overflows.
*
* @tparam T inner type of the forward-mode autodiff variable
* @param[in] x argument
* @return hyperbolic tangent of the argument
*/
template <typename T>
inline fvar<T> tanh(const fvar<T>& x) {
T u = tanh(x.val_);
return fvar<T>(u, x.d_ * (1 - u * u));
const T tanh_x = tanh(x.val_);
const double x_val = value_of_rec(x.val_);
if (fabs(x_val) < 0.5) {
return fvar<T>(tanh_x, x.d_ * (1 - tanh_x * tanh_x));
}
// exp(-2 |x|), with the sign of x fixed in this branch
const T exp_neg_2abs_x = exp(x_val > 0 ? -2 * x.val_ : 2 * x.val_);
const T one_plus_exp = 1 + exp_neg_2abs_x;
return fvar<T>(tanh_x,
x.d_ * (4 * exp_neg_2abs_x / (one_plus_exp * one_plus_exp)));
}

/**
Expand Down
216 changes: 163 additions & 53 deletions stan/math/prim/constraint/cholesky_corr_constrain.hpp
Original file line number Diff line number Diff line change
@@ -1,79 +1,189 @@
#ifndef STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP
#define STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP

#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/err.hpp>
#include <stan/math/prim/fun/Eigen.hpp>
#include <stan/math/prim/fun/log1m.hpp>
#include <stan/math/prim/fun/sqrt.hpp>
#include <stan/math/prim/fun/square.hpp>
#include <stan/math/prim/constraint/corr_constrain.hpp>
#include <cmath>
#include <stan/math/prim/fun/dot_product.hpp>
#include <stan/math/prim/fun/exp.hpp>
#include <stan/math/prim/fun/log_cosh.hpp>
#include <stan/math/prim/fun/tanh.hpp>
#include <stan/math/prim/fun/to_ref.hpp>
#include <cstdint>
#include <utility>

namespace stan {
namespace math {

template <typename EigVec, require_eigen_col_vector_t<EigVec>* = nullptr>
namespace internal {
/**
* Order of the partial correlations in the unconstrained vector: `row` for
* `cholesky_corr_constrain` (row 1, then row 2, ...), `column` for
* `corr_matrix_constrain` and `cov_matrix_constrain_lkj` (column 0, then
* column 1, ...).
*/
enum class CorrOrder : uint8_t { row = 0, column = 1 };

/**
* Return the position in the unconstrained vector of the partial
* correlation for element `(row, col)`, `col < row`, of the Cholesky factor.
*
* @tparam Order order of the partial correlations in the vector
* @param row row
* @param col column
* @param K number of rows
* @return position in the unconstrained vector
*/
template <CorrOrder Order>
inline Eigen::Index cholesky_corr_position(Eigen::Index row, Eigen::Index col,
Eigen::Index K) {
if constexpr (Order == CorrOrder::column) {
return col * (K - 1) - (col * (col - 1)) / 2 + (row - col - 1);
} else {
return (row * (row - 1)) / 2 + col;
}
}

/**
* Return a lazy expression of the coefficients `c` for which the log
* absolute Jacobian determinant of `cholesky_corr_constrain` is
* `-sum_n c_n log(cosh(y_n))`, in row order: the coefficient of element
* `(row, col)` is `row + 1 - col`, with `row` recovered from the position by
* inverting `cholesky_corr_position<CorrOrder::row>`. The inverse is exact,
* since at the first position of a row the radicand `1 + 8 pos` is the
* perfect square `(2 row - 1)^2`, for which `sqrt` is exact, so `floor`
* never lands one row too low.
*
* @param K number of rows
* @return coefficients in the order of the unconstrained vector
*/
inline auto cholesky_corr_log_jacobian_coefficients(Eigen::Index K) {
const Eigen::Index k_choose_2 = (K * (K - 1)) / 2;
return make_holder(
[](auto&& pos) {
const auto row = (0.5 * (1.0 + (1.0 + 8.0 * pos).sqrt())).floor();
return (row + 1.0 - (pos - 0.5 * row * (row - 1.0))).matrix();
},
Eigen::ArrayXd::LinSpaced(k_choose_2, 0.0, k_choose_2 - 1.0));
}

/**
* Return a lazy expression of the coefficients `c` for which the log
* absolute Jacobian determinant of `corr_matrix_constrain` is
* `-sum_n c_n log(cosh(y_n))`, in column order: the coefficient of every
* element of column `col` is `K - col` (equation 11 of Lewandowski,
* Kurowicka and Joe, 2009), with `col` recovered from the position by
* inverting `cholesky_corr_position<CorrOrder::column>`. The inverse is
* exact, since at the first position of a column the radicand
* `(2 K - 1)^2 - 8 pos` is the perfect square `(2 (K - col) - 1)^2`, for
* which `sqrt` is exact, so `floor` never lands one column too low.
*
* @param K number of rows
* @return coefficients in the order of the unconstrained vector
*/
inline auto corr_matrix_log_jacobian_coefficients(Eigen::Index K) {
const Eigen::Index k_choose_2 = (K * (K - 1)) / 2;
const double two_K_minus_one = 2.0 * K - 1.0;
return make_holder(
[K = static_cast<double>(K), two_K_minus_one](auto&& pos) {
return (K
- (0.5
* (two_K_minus_one
- (two_K_minus_one * two_K_minus_one - 8.0 * pos).sqrt()))
.floor())
.matrix();
},
Eigen::ArrayXd::LinSpaced(k_choose_2, 0.0, k_choose_2 - 1.0));
}

/**
* Return the Cholesky factor of a correlation matrix from unconstrained
* values `y`, whose partial correlations are `tanh(y)`. Each element is
* `L(row, col) = tanh(y_col) exp(-acc)` with `acc` the sum of
* `log(cosh(y))` over the earlier columns of the row, which has no
* cancellation.
*
* @tparam Order order of the partial correlations in `y`
* @tparam EigVec type of the vector
* @tparam LogCoshVec type of the vector of `log(cosh(y))`
* @param y unconstrained values, `K (K - 1) / 2` of them
* @param log_cosh_y `log_cosh(y)`, shared with the log Jacobian
* @param K number of rows
* @return Cholesky factor of a correlation matrix
*/
template <CorrOrder Order, typename EigVec, typename LogCoshVec,
require_all_eigen_vector_t<EigVec, LogCoshVec>* = nullptr>
inline Eigen::Matrix<value_type_t<EigVec>, Eigen::Dynamic, Eigen::Dynamic>
cholesky_corr_constrain(const EigVec& y, int K) {
using Eigen::Dynamic;
using Eigen::Matrix;
using std::sqrt;
cholesky_corr_factor(EigVec&& y, LogCoshVec&& log_cosh_y, Eigen::Index K) {
using T_scalar = value_type_t<EigVec>;
int k_choose_2 = (K * (K - 1)) / 2;
check_size_match("cholesky_corr_constrain", "constrain size", y.size(),
"k_choose_2", k_choose_2);
Matrix<T_scalar, Dynamic, 1> z = corr_constrain(y);
Matrix<T_scalar, Dynamic, Dynamic> x(K, K);
Eigen::Matrix<T_scalar, Eigen::Dynamic, Eigen::Dynamic> L
= Eigen::Matrix<T_scalar, Eigen::Dynamic, Eigen::Dynamic>::Zero(K, K);
if (K == 0) {
return x;
return L;
}
x.setZero();
x.coeffRef(0, 0) = 1;
int k = 0;
for (int i = 1; i < K; ++i) {
x.coeffRef(i, 0) = z.coeff(k++);
T_scalar sum_sqs = square(x.coeff(i, 0));
for (int j = 1; j < i; ++j) {
x.coeffRef(i, j) = z.coeff(k++) * sqrt(1.0 - sum_sqs);
sum_sqs += square(x.coeff(i, j));
decltype(auto) y_ref = to_ref(std::forward<EigVec>(y));
decltype(auto) log_cosh_y_ref = to_ref(std::forward<LogCoshVec>(log_cosh_y));
L.coeffRef(0, 0) = 1;
for (Eigen::Index row = 1; row < K; ++row) {
T_scalar acc = 0;
for (Eigen::Index col = 0; col < row; ++col) {
const Eigen::Index pos = cholesky_corr_position<Order>(row, col, K);
L.coeffRef(row, col) = tanh(y_ref.coeff(pos)) * exp(-acc);
acc += log_cosh_y_ref.coeff(pos);
}
x.coeffRef(i, i) = sqrt(1.0 - sum_sqs);
L.coeffRef(row, row) = exp(-acc);
}
return x;
return L;
}
} // namespace internal

/**
* Return the Cholesky factor of a `K x K` correlation matrix from the
* `K (K - 1) / 2` unconstrained values `y`, whose partial correlations are
* `tanh(y)` in row order.
*
* @tparam EigVec type of the vector
* @param y unconstrained values
* @param K number of rows
* @return Cholesky factor of a correlation matrix
*/
template <typename EigVec, require_eigen_col_vector_t<EigVec>* = nullptr>
inline Eigen::Matrix<value_type_t<EigVec>, Eigen::Dynamic, Eigen::Dynamic>
cholesky_corr_constrain(EigVec&& y, int K) {
int k_choose_2 = (K * (K - 1)) / 2;
check_size_match("cholesky_corr_constrain", "constrain size", y.size(),
"k_choose_2", k_choose_2);
decltype(auto) y_ref = to_ref(std::forward<EigVec>(y));
return internal::cholesky_corr_factor<internal::CorrOrder::row>(
y_ref, log_cosh(y_ref), K);
}

// FIXME to match above after debugged
/**
* Return the Cholesky factor of a `K x K` correlation matrix from the
* `K (K - 1) / 2` unconstrained values `y`, and add the log absolute
* Jacobian determinant of the transform to `lp`.
*
* @tparam EigVec type of the vector
* @tparam Lp scalar type of the log density accumulator
* @param y unconstrained values
* @param K number of rows
* @param[in,out] lp log density accumulator
* @return Cholesky factor of a correlation matrix
*/
template <typename EigVec, typename Lp,
require_eigen_vector_t<EigVec>* = nullptr,
require_eigen_col_vector_t<EigVec>* = nullptr,
require_convertible_t<return_type_t<EigVec>, Lp>* = nullptr>
inline Eigen::Matrix<value_type_t<EigVec>, Eigen::Dynamic, Eigen::Dynamic>
cholesky_corr_constrain(const EigVec& y, int K, Lp& lp) {
using Eigen::Dynamic;
using Eigen::Matrix;
using std::sqrt;
using T_scalar = value_type_t<EigVec>;
cholesky_corr_constrain(EigVec&& y, int K, Lp& lp) {
int k_choose_2 = (K * (K - 1)) / 2;
check_size_match("cholesky_corr_constrain", "y.size()", y.size(),
"k_choose_2", k_choose_2);
Matrix<T_scalar, Dynamic, 1> z = corr_constrain(y, lp);
Matrix<T_scalar, Dynamic, Dynamic> x(K, K);
if (K == 0) {
return x;
}
x.setZero();
x.coeffRef(0, 0) = 1;
int k = 0;
for (int i = 1; i < K; ++i) {
x.coeffRef(i, 0) = z.coeff(k++);
T_scalar sum_sqs = square(x.coeff(i, 0));
for (int j = 1; j < i; ++j) {
lp += 0.5 * log1m(sum_sqs);
x.coeffRef(i, j) = z.coeff(k++) * sqrt(1.0 - sum_sqs);
sum_sqs += square(x.coeff(i, j));
}
x.coeffRef(i, i) = sqrt(1.0 - sum_sqs);
}
return x;
decltype(auto) y_ref = to_ref(std::forward<EigVec>(y));
decltype(auto) log_cosh_y = to_ref(log_cosh(y_ref));
lp -= dot_product(internal::cholesky_corr_log_jacobian_coefficients(K),
log_cosh_y);
return internal::cholesky_corr_factor<internal::CorrOrder::row>(
y_ref, std::move(log_cosh_y), K);
}

/**
Expand Down
15 changes: 8 additions & 7 deletions stan/math/prim/constraint/corr_constrain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#define STAN_MATH_PRIM_CONSTRAINT_CORR_CONSTRAIN_HPP

#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/fun/log1m.hpp>
#include <stan/math/prim/fun/square.hpp>
#include <stan/math/prim/fun/log_cosh.hpp>
#include <stan/math/prim/fun/sum.hpp>
#include <stan/math/prim/fun/tanh.hpp>
#include <cmath>
#include <stan/math/prim/fun/to_ref.hpp>
#include <utility>

namespace stan {
namespace math {
Expand Down Expand Up @@ -36,17 +36,18 @@ inline plain_type_t<T> corr_constrain(T&& x) {
* <code>corr_constrain(T)</code>. The log absolute Jacobian
* determinant is
*
* <p>\f$\log | \frac{d}{dx} \tanh x | = \log (1 - \tanh^2 x)\f$.
* <p>\f$\log | \frac{d}{dx} \tanh x | = \log (1 - \tanh^2 x)
* = -2 \log \cosh x\f$.
*
* @tparam T_x Type of scalar or container
* @param[in] x value or container
* @param[in,out] lp log density accumulator
*/
template <typename T_x, typename T_lp>
inline auto corr_constrain(T_x&& x, T_lp& lp) {
plain_type_t<T_x> tanh_x = tanh(std::forward<T_x>(x));
lp += sum(log1m(square(tanh_x)));
return tanh_x;
decltype(auto) x_ref = to_ref(std::forward<T_x>(x));
lp -= 2.0 * sum(log_cosh(x_ref));
return corr_constrain(x_ref);
}

/**
Expand Down
Loading
Loading