From ba898c306e95da8bf882c8810ff53fd03652a1ea Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Mon, 12 Oct 2026 23:03:11 +0300 Subject: [PATCH 1/7] Repair the forward-mode tanh derivative and the correlation log Jacobians --- stan/math/fwd/fun.hpp | 1 + stan/math/fwd/fun/log_cosh.hpp | 27 ++++++ stan/math/fwd/fun/tanh.hpp | 28 +++++- .../constraint/cholesky_corr_constrain.hpp | 35 ++++++- stan/math/prim/constraint/corr_constrain.hpp | 14 +-- .../prim/constraint/corr_matrix_constrain.hpp | 35 ++++++- stan/math/prim/fun.hpp | 1 + stan/math/prim/fun/log_cosh.hpp | 92 +++++++++++++++++++ .../constraint/cholesky_corr_constrain.hpp | 14 +-- .../rev/constraint/corr_matrix_constrain.hpp | 7 +- stan/math/rev/fun.hpp | 1 + stan/math/rev/fun/log_cosh.hpp | 55 +++++++++++ .../cholesky_corr_constrain_test.cpp | 82 +++++++++++++++++ .../mix/constraint/corr_constrain_test.cpp | 47 ++++++++++ .../constraint/corr_matrix_constrain_test.cpp | 82 +++++++++++++++++ test/unit/math/mix/fun/log_cosh_test.cpp | 63 +++++++++++++ test/unit/math/mix/fun/tanh_test.cpp | 68 ++++++++++++++ 17 files changed, 631 insertions(+), 21 deletions(-) create mode 100644 stan/math/fwd/fun/log_cosh.hpp create mode 100644 stan/math/prim/fun/log_cosh.hpp create mode 100644 stan/math/rev/fun/log_cosh.hpp create mode 100644 test/unit/math/mix/fun/log_cosh_test.cpp diff --git a/stan/math/fwd/fun.hpp b/stan/math/fwd/fun.hpp index ffd56a9297e..16647f1d144 100644 --- a/stan/math/fwd/fun.hpp +++ b/stan/math/fwd/fun.hpp @@ -72,6 +72,7 @@ #include #include #include +#include #include #include #include diff --git a/stan/math/fwd/fun/log_cosh.hpp b/stan/math/fwd/fun/log_cosh.hpp new file mode 100644 index 00000000000..580e6efd368 --- /dev/null +++ b/stan/math/fwd/fun/log_cosh.hpp @@ -0,0 +1,27 @@ +#ifndef STAN_MATH_FWD_FUN_LOG_COSH_HPP +#define STAN_MATH_FWD_FUN_LOG_COSH_HPP + +#include +#include +#include +#include +#include + +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 +inline fvar log_cosh(const fvar& x) { + return fvar(log_cosh(x.val_), x.d_ * tanh(x.val_)); +} + +} // namespace math +} // namespace stan +#endif diff --git a/stan/math/fwd/fun/tanh.hpp b/stan/math/fwd/fun/tanh.hpp index 9699163b226..7ab38dceb97 100644 --- a/stan/math/fwd/fun/tanh.hpp +++ b/stan/math/fwd/fun/tanh.hpp @@ -4,17 +4,41 @@ #include #include #include +#include +#include #include +#include #include #include 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|): the difference 1 - tanh(x)^2 loses + * all digits for large |x|, and 1 / cosh(x)^2 makes the next derivative NaN + * once cosh(x)^2 overflows. For |x| < 0.5 the difference is accurate. + * Neither branch uses fabs(x), whose derivative is 0 at x = 0 and would + * make the third derivative of tanh at 0 vanish in nested forward mode. + * + * @tparam T inner type of the forward-mode autodiff variable + * @param[in] x argument + * @return hyperbolic tangent of the argument + */ template inline fvar tanh(const fvar& x) { - T u = tanh(x.val_); - return fvar(u, x.d_ * (1 - u * u)); + const T u = tanh(x.val_); + const double x_val = value_of_rec(x.val_); + if (std::fabs(x_val) < 0.5) { + return fvar(u, x.d_ * (1 - u * u)); + } + // -2 |x|, with the sign of x fixed in this branch + const T e = exp(x_val > 0 ? -2 * x.val_ : 2 * x.val_); + const T one_p_e = 1 + e; + return fvar(u, x.d_ * (4 * e / (one_p_e * one_p_e))); } /** diff --git a/stan/math/prim/constraint/cholesky_corr_constrain.hpp b/stan/math/prim/constraint/cholesky_corr_constrain.hpp index 8ed2b1aa454..d03890a8e47 100644 --- a/stan/math/prim/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/prim/constraint/cholesky_corr_constrain.hpp @@ -3,7 +3,8 @@ #include #include -#include +#include +#include #include #include #include @@ -12,6 +13,33 @@ namespace stan { namespace math { +namespace internal { +/** + * Return the coefficients `c` for which the log absolute Jacobian + * determinant of `cholesky_corr_constrain(y, K)` is + * `-sum_n c_n log(cosh(y_n))`. + * + * With `z = tanh(y)`, row `i` holds `z_m sqrt(prod_{l < m} (1 - z_l^2))` in + * column `m < i`. The Jacobian has a factor `1 - z_m^2` for each element + * and a factor `sqrt(prod_{l < j} (1 - z_l^2))` for each column + * `0 < j < i`. With `log(1 - z_m^2) = -2 log(cosh(y_m))`, the coefficient + * of element `m` of row `i` is `2 + (i - 1 - m) = i + 1 - m`. + * + * @param K number of rows of the Cholesky factor + * @return coefficients in the order of the unconstrained vector + */ +inline Eigen::VectorXd cholesky_corr_log_jacobian_coefficients(int K) { + Eigen::VectorXd c((K * (K - 1)) / 2); + int k = 0; + for (int i = 1; i < K; ++i) { + for (int m = 0; m < i; ++m) { + c.coeffRef(k++) = i + 1 - m; + } + } + return c; +} +} // namespace internal + template * = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cholesky_corr_constrain(const EigVec& y, int K) { @@ -55,11 +83,13 @@ cholesky_corr_constrain(const 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 z = corr_constrain(y, lp); + Matrix z = corr_constrain(y); Matrix x(K, K); if (K == 0) { return x; } + lp -= dot_product(internal::cholesky_corr_log_jacobian_coefficients(K), + log_cosh(y)); x.setZero(); x.coeffRef(0, 0) = 1; int k = 0; @@ -67,7 +97,6 @@ cholesky_corr_constrain(const EigVec& y, int K, Lp& lp) { 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)); } diff --git a/stan/math/prim/constraint/corr_constrain.hpp b/stan/math/prim/constraint/corr_constrain.hpp index c5cd1862139..dfc424b0a3b 100644 --- a/stan/math/prim/constraint/corr_constrain.hpp +++ b/stan/math/prim/constraint/corr_constrain.hpp @@ -2,8 +2,7 @@ #define STAN_MATH_PRIM_CONSTRAINT_CORR_CONSTRAIN_HPP #include -#include -#include +#include #include #include #include @@ -36,7 +35,11 @@ inline plain_type_t corr_constrain(T&& x) { * corr_constrain(T). The log absolute Jacobian * determinant is * - *

\f$\log | \frac{d}{dx} \tanh x | = \log (1 - \tanh^2 x)\f$. + *

\f$\log | \frac{d}{dx} \tanh x | = \log (1 - \tanh^2 x) + * = -2 \log \cosh x\f$. + * + * The difference \f$1 - \tanh^2 x\f$ has no correct digits from + * \f$|x| = 19\f$; `log_cosh` has a small relative error for every `x`. * * @tparam T_x Type of scalar or container * @param[in] x value or container @@ -44,9 +47,8 @@ inline plain_type_t corr_constrain(T&& x) { */ template inline auto corr_constrain(T_x&& x, T_lp& lp) { - plain_type_t tanh_x = tanh(std::forward(x)); - lp += sum(log1m(square(tanh_x))); - return tanh_x; + lp -= 2.0 * sum(log_cosh(x)); + return corr_constrain(std::forward(x)); } /** diff --git a/stan/math/prim/constraint/corr_matrix_constrain.hpp b/stan/math/prim/constraint/corr_matrix_constrain.hpp index a2d54131f6f..b937e929e51 100644 --- a/stan/math/prim/constraint/corr_matrix_constrain.hpp +++ b/stan/math/prim/constraint/corr_matrix_constrain.hpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include #include #include @@ -12,6 +14,33 @@ namespace stan { namespace math { +namespace internal { +/** + * Return the coefficients `c` for which the log absolute Jacobian + * determinant of `corr_matrix_constrain(x, K)` is + * `-sum_n c_n log(cosh(x_n))`. + * + * The transform is `tanh` to canonical partial correlations, with + * `log(1 - tanh(x)^2) = -2 log(cosh(x))`, then `read_corr_matrix`, whose + * log Jacobian is `(K - j - 2) / 2 log(1 - z^2)` for a partial correlation + * `z` of column `j` (equation 11 of Lewandowski, Kurowicka and Joe, 2009). + * Column `j` holds `K - 1 - j` values, each with the coefficient + * `2 + (K - j - 2) = K - j`. + * + * @param K number of rows of the correlation matrix + * @return coefficients in the order of the unconstrained vector + */ +inline Eigen::VectorXd corr_matrix_log_jacobian_coefficients(Eigen::Index K) { + Eigen::VectorXd c((K * (K - 1)) / 2); + Eigen::Index pos = 0; + for (Eigen::Index j = 0; j < K - 1; ++j) { + c.segment(pos, K - 1 - j).setConstant(K - j); + pos += K - 1 - j; + } + return c; +} +} // namespace internal + /** * Return the correlation matrix of the specified dimensionality * derived from the specified vector of unconstrained values. The @@ -79,9 +108,11 @@ corr_matrix_constrain(T&& x, Eigen::Index k, Lp& lp) { decltype(auto) x_ref = to_ref(std::forward(x)); check_size_match("cov_matrix_constrain", "x.size()", x_ref.size(), "k_choose_2", k_choose_2); + lp -= dot_product(internal::corr_matrix_log_jacobian_coefficients(k), + log_cosh(x_ref)); decltype(auto) cpc_ref - = to_ref(corr_constrain(std::forward(x_ref), lp)); - return read_corr_matrix(cpc_ref, k, lp); + = to_ref(corr_constrain(std::forward(x_ref))); + return read_corr_matrix(cpc_ref, k); } /** diff --git a/stan/math/prim/fun.hpp b/stan/math/prim/fun.hpp index 8ef4204c81b..5fb8708e5e9 100644 --- a/stan/math/prim/fun.hpp +++ b/stan/math/prim/fun.hpp @@ -176,6 +176,7 @@ #include #include #include +#include #include #include #include diff --git a/stan/math/prim/fun/log_cosh.hpp b/stan/math/prim/fun/log_cosh.hpp new file mode 100644 index 00000000000..cbf2f0be941 --- /dev/null +++ b/stan/math/prim/fun/log_cosh.hpp @@ -0,0 +1,92 @@ +#ifndef STAN_MATH_PRIM_FUN_LOG_COSH_HPP +#define STAN_MATH_PRIM_FUN_LOG_COSH_HPP + +#include +#include +#include +#include + +namespace stan { +namespace math { + +namespace internal { +/** + * Return `log(cosh(x))` and its derivative `tanh(x)`. + * + * For `|x| < 1`, with `u = expm1(|x|)`, `cosh(x) - 1 = u^2 / (2 (1 + u))` + * and `tanh(|x|) = u (u + 2) / (u (u + 2) + 2)`. For `|x| >= 1`, with + * `t = expm1(-2 |x|)`, `log(cosh(x)) = |x| + log1p(t / 2)` and + * `tanh(|x|) = -t / (2 + t)`. No branch subtracts two nearly equal numbers + * or overflows, so both results have a small relative error for every `x`. + * + * @param x argument + * @return pair of `log(cosh(x))` and `tanh(x)` + */ +inline std::pair log_cosh_and_tanh(double x) { + const double a = std::fabs(x); + double value; + double tanh_a; + if (a < 1.0) { + const double u = std::expm1(a); + const double w = u * (u + 2.0); + value = std::log1p(u * u / (2.0 * (1.0 + u))); + tanh_a = w / (w + 2.0); + } else { + const double t = std::expm1(-2.0 * a); + value = a + std::log1p(0.5 * t); + tanh_a = -t / (2.0 + t); + } + return {value, std::copysign(tanh_a, x)}; +} +} // namespace internal + +/** + * Return the natural logarithm of the hyperbolic cosine of the argument. + * + * The result does not overflow for large `|x|` and has a small relative + * error for small `|x|`. The derivative is `tanh(x)`. + * + * @tparam T arithmetic type + * @param x argument + * @return `log(cosh(x))` + */ +template * = nullptr> +inline double log_cosh(T x) { + return internal::log_cosh_and_tanh(static_cast(x)).first; +} + +/** + * Structure to wrap `log_cosh()` so it can be vectorized. + */ +struct log_cosh_fun { + /** + * Return the natural logarithm of the hyperbolic cosine of the argument. + * + * @tparam T type of argument + * @param x argument + * @return `log(cosh(x))` + */ + template + static inline auto fun(const T& x) { + return log_cosh(x); + } +}; + +/** + * Return the elementwise `log_cosh()` of the specified container. + * + * @tparam T type of container + * @param x container + * @return elementwise `log(cosh(x))` + */ +template * = nullptr, + require_not_var_matrix_t* = nullptr, + require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> +inline auto log_cosh(const T& x) { + return apply_scalar_unary::apply(x); +} + +} // namespace math +} // namespace stan + +#endif diff --git a/stan/math/rev/constraint/cholesky_corr_constrain.hpp b/stan/math/rev/constraint/cholesky_corr_constrain.hpp index 576a1bb962e..c2a87c2505e 100644 --- a/stan/math/rev/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/rev/constraint/cholesky_corr_constrain.hpp @@ -2,11 +2,13 @@ #define STAN_MATH_REV_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP #include +#include +#include #include #include -#include #include #include +#include #include #include @@ -102,18 +104,18 @@ inline var_value cholesky_corr_constrain( return Eigen::MatrixXd(); } - var_value z = corr_constrain(y, lp); + lp -= dot_product(internal::cholesky_corr_log_jacobian_coefficients(K), + log_cosh(y)); + var_value z = corr_constrain(y); arena_t x_val = Eigen::MatrixXd::Zero(K, K); x_val.coeffRef(0, 0) = 1; int k = 0; - double lp_val = 0.0; for (int i = 1; i < K; ++i) { x_val.coeffRef(i, 0) = z.val().coeff(k); k++; double sum_sqs = square(x_val.coeff(i, 0)); for (int j = 1; j < i; ++j) { - lp_val += 0.5 * log1m(sum_sqs); x_val.coeffRef(i, j) = z.val().coeff(k) * sqrt(1.0 - sum_sqs); k++; sum_sqs += square(x_val.coeff(i, j)); @@ -121,10 +123,9 @@ inline var_value cholesky_corr_constrain( x_val.coeffRef(i, i) = sqrt(1.0 - sum_sqs); } - lp += lp_val; var_value x = x_val; - reverse_pass_callback([z, K, x, lp]() mutable { + reverse_pass_callback([z, K, x]() mutable { size_t k = z.size(); for (int i = K - 1; i > 0; --i) { double sum_sqs_val = 1.0 - square(x.val().coeffRef(i, i)); @@ -137,7 +138,6 @@ inline var_value cholesky_corr_constrain( sum_sqs_adj += -0.5 * x.adj().coeffRef(i, j) * z.val().coeff(k) / sqrt(1.0 - sum_sqs_val); z.adj().coeffRef(k) += x.adj().coeffRef(i, j) * sqrt(1.0 - sum_sqs_val); - sum_sqs_adj -= 0.5 * lp.adj() / (1 - sum_sqs_val); } x.adj().coeffRef(i, 0) += 2 * sum_sqs_adj * x.val().coeff(i, 0); k--; diff --git a/stan/math/rev/constraint/corr_matrix_constrain.hpp b/stan/math/rev/constraint/corr_matrix_constrain.hpp index 8b8a028c9a5..4694e0f49b7 100644 --- a/stan/math/rev/constraint/corr_matrix_constrain.hpp +++ b/stan/math/rev/constraint/corr_matrix_constrain.hpp @@ -2,10 +2,13 @@ #define STAN_MATH_REV_CONSTRAINT_CORR_MATRIX_CONSTRAIN_HPP #include +#include +#include #include #include #include #include +#include #include #include @@ -73,7 +76,9 @@ inline var_value corr_matrix_constrain(const T& x, Eigen::Index k_choose_2 = (k * (k - 1)) / 2; check_size_match("cov_matrix_constrain", "x.size()", x.size(), "k_choose_2", k_choose_2); - return read_corr_matrix(corr_constrain(x, lp), k, lp); + lp -= dot_product(internal::corr_matrix_log_jacobian_coefficients(k), + log_cosh(x)); + return read_corr_matrix(corr_constrain(x), k); } } // namespace math diff --git a/stan/math/rev/fun.hpp b/stan/math/rev/fun.hpp index e5ef938a302..ccb7f813778 100644 --- a/stan/math/rev/fun.hpp +++ b/stan/math/rev/fun.hpp @@ -105,6 +105,7 @@ #include #include #include +#include #include #include #include diff --git a/stan/math/rev/fun/log_cosh.hpp b/stan/math/rev/fun/log_cosh.hpp new file mode 100644 index 00000000000..d3b820656c1 --- /dev/null +++ b/stan/math/rev/fun/log_cosh.hpp @@ -0,0 +1,55 @@ +#ifndef STAN_MATH_REV_FUN_LOG_COSH_HPP +#define STAN_MATH_REV_FUN_LOG_COSH_HPP + +#include +#include +#include +#include + +namespace stan { +namespace math { + +/** + * Return the natural logarithm of the hyperbolic cosine of the argument. + * + * @tparam T arithmetic type + * @param x `var_value` with inner arithmetic type + * @return `log(cosh(x))`, with derivative `tanh(x)` + */ +template * = nullptr> +inline var log_cosh(const var_value& x) { + const std::pair value_and_tanh + = internal::log_cosh_and_tanh(x.val()); + const double tanh_x = value_and_tanh.second; + return make_callback_var(value_and_tanh.first, [x, tanh_x](auto& vi) mutable { + x.adj() += vi.adj() * tanh_x; + }); +} + +/** + * Return the elementwise natural logarithm of the hyperbolic cosine of the + * argument. + * + * @tparam T a type derived from `Eigen::EigenBase` + * @param x `var_value` with inner Eigen type + * @return elementwise `log(cosh(x))`, with derivative `tanh(x)` + */ +template * = nullptr> +inline auto log_cosh(const var_value& x) { + using val_t = plain_type_t; + val_t value(x.rows(), x.cols()); + arena_t tanh_x(x.rows(), x.cols()); + for (Eigen::Index i = 0; i < x.size(); ++i) { + const std::pair value_and_tanh + = internal::log_cosh_and_tanh(x.val().coeff(i)); + value.coeffRef(i) = value_and_tanh.first; + tanh_x.coeffRef(i) = value_and_tanh.second; + } + return make_callback_var(std::move(value), [x, tanh_x](auto& vi) mutable { + x.adj().array() += vi.adj().array() * tanh_x.array(); + }); +} + +} // namespace math +} // namespace stan +#endif diff --git a/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp b/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp index 89e1ebb7e39..200ff43daf9 100644 --- a/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp +++ b/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp @@ -114,3 +114,85 @@ TEST(mathMixMatFun, cholesky_corr_constrain_lp) { stan::test::expect_ad_matvar(f2(4), x1); stan::test::expect_ad_matvar(f2(3), x2); } + +namespace cholesky_corr_constrain_test { +// the strictly lower triangular elements of the Cholesky factor +struct strict_lower_of_constrained { + int K; + template + Eigen::Matrix operator()( + const Eigen::Matrix& y) const { + auto L = stan::math::cholesky_corr_constrain(y, K); + Eigen::Matrix v((K * (K - 1)) / 2); + int k = 0; + for (int j = 0; j < K; ++j) { + for (int i = j + 1; i < K; ++i) { + v(k++) = L(i, j); + } + } + return v; + } +}; +} // namespace cholesky_corr_constrain_test + +// lp is log |det J| of the map from y to the strictly lower triangular +// elements of the Cholesky factor. +TEST(mathMixMatFun, cholesky_corr_constrain_lp_is_log_det_jacobian) { + Eigen::VectorXd y(6); + y << 0.3, -1.2, 0.8, 2.1, -0.4, 1.5; + Eigen::VectorXd fx; + Eigen::MatrixXd J; + stan::math::jacobian( + cholesky_corr_constrain_test::strict_lower_of_constrained{4}, y, fx, J); + double lp = 0; + stan::math::cholesky_corr_constrain(y, 4, lp); + EXPECT_NEAR(std::log(std::fabs(J.determinant())), lp, 1e-12); +} + +// lp = sum_n w_n log(1 - tanh(y_n)^2) with weights (1, 1.5, 1) for K = 3, +// and its gradient, at large y against mpmath. Forming 1 - tanh(y)^2 and +// 1 - sum of squares loses digits from |y| = 8. +TEST(mathMixMatFun, cholesky_corr_constrain_lp_large_argument) { + using stan::math::fvar; + using stan::math::var; + Eigen::VectorXd y(3); + y << 15.0, 12.0, -0.5; + const double lp_ref = -62.77449311123027; + Eigen::VectorXd grad_ref(3); + grad_ref << -1.9999999999996257, -2.9999999997734919, 0.92423431452001952; + + double lp_d = 0; + stan::math::cholesky_corr_constrain(y, 3, lp_d); + EXPECT_NEAR(lp_ref, lp_d, 1e-13); + + Eigen::Matrix y_v = stan::math::to_var(y); + var lp_v = 0; + stan::math::cholesky_corr_constrain(y_v, 3, lp_v); + lp_v.grad(); + EXPECT_NEAR(lp_ref, lp_v.val(), 1e-13); + for (int i = 0; i < 3; ++i) { + EXPECT_NEAR(grad_ref(i), y_v(i).adj(), 1e-14); + } + stan::math::recover_memory(); + + stan::math::var_value y_m(y); + var lp_m = 0; + stan::math::cholesky_corr_constrain(y_m, 3, lp_m); + lp_m.grad(); + EXPECT_NEAR(lp_ref, lp_m.val(), 1e-13); + for (int i = 0; i < 3; ++i) { + EXPECT_NEAR(grad_ref(i), y_m.adj()(i), 1e-14); + } + stan::math::recover_memory(); + + for (int n = 0; n < 3; ++n) { + Eigen::Matrix, Eigen::Dynamic, 1> y_f(3); + for (int i = 0; i < 3; ++i) { + y_f(i) = fvar(y(i), i == n ? 1.0 : 0.0); + } + fvar lp_f = 0; + stan::math::cholesky_corr_constrain(y_f, 3, lp_f); + EXPECT_NEAR(lp_ref, lp_f.val_, 1e-13); + EXPECT_NEAR(grad_ref(n), lp_f.d_, 1e-14); + } +} diff --git a/test/unit/math/mix/constraint/corr_constrain_test.cpp b/test/unit/math/mix/constraint/corr_constrain_test.cpp index 609062f9f73..73f19c0bbd1 100644 --- a/test/unit/math/mix/constraint/corr_constrain_test.cpp +++ b/test/unit/math/mix/constraint/corr_constrain_test.cpp @@ -1,5 +1,52 @@ #include +// log(1 - tanh(x)^2) and its derivative -2 tanh(x) at large |x|, against +// mpmath. Forming 1 - tanh(x)^2 loses all digits from |x| = 19.06. +TEST(mathMixMatFun, corr_constrain_lp_large_argument) { + using stan::math::fvar; + using stan::math::var; + Eigen::VectorXd x(3); + x << 15.0, -19.5, 30.0; + const double lp_ref = -124.84111691664052; + Eigen::VectorXd grad_ref(3); + grad_ref << -1.9999999999996257, 2.0, -2.0; + + double lp_d = 0; + stan::math::corr_constrain(x, lp_d); + EXPECT_NEAR(lp_ref, lp_d, 1e-13); + + Eigen::Matrix x_v = stan::math::to_var(x); + var lp_v = 0; + stan::math::corr_constrain(x_v, lp_v); + lp_v.grad(); + EXPECT_NEAR(lp_ref, lp_v.val(), 1e-13); + for (int i = 0; i < 3; ++i) { + EXPECT_NEAR(grad_ref(i), x_v(i).adj(), 1e-14); + } + stan::math::recover_memory(); + + stan::math::var_value x_m(x); + var lp_m = 0; + stan::math::corr_constrain(x_m, lp_m); + lp_m.grad(); + EXPECT_NEAR(lp_ref, lp_m.val(), 1e-13); + for (int i = 0; i < 3; ++i) { + EXPECT_NEAR(grad_ref(i), x_m.adj()(i), 1e-14); + } + stan::math::recover_memory(); + + for (int n = 0; n < 3; ++n) { + Eigen::Matrix, Eigen::Dynamic, 1> x_f(3); + for (int i = 0; i < 3; ++i) { + x_f(i) = fvar(x(i), i == n ? 1.0 : 0.0); + } + fvar lp_f = 0; + stan::math::corr_constrain(x_f, lp_f); + EXPECT_NEAR(lp_ref, lp_f.val_, 1e-13); + EXPECT_NEAR(grad_ref(n), lp_f.d_, 1e-14); + } +} + TEST(mathMixMatFun, corr_constrain) { auto f = [](const auto& x1) { stan::scalar_type_t lp = 0.0; diff --git a/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp b/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp index 2b766dd7660..688d1acb39b 100644 --- a/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp +++ b/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp @@ -117,3 +117,85 @@ TEST(mathMixMatFun, corr_matrix_constrain_lp) { stan::test::expect_ad_matvar(f2(4), x1); stan::test::expect_ad_matvar(f2(3), x2); } + +namespace corr_matrix_constrain_test { +// the strictly lower triangular elements of the correlation matrix +struct strict_lower_of_constrained { + int K; + template + Eigen::Matrix operator()( + const Eigen::Matrix& y) const { + auto Omega = stan::math::corr_matrix_constrain(y, K); + Eigen::Matrix v((K * (K - 1)) / 2); + int k = 0; + for (int j = 0; j < K; ++j) { + for (int i = j + 1; i < K; ++i) { + v(k++) = Omega(i, j); + } + } + return v; + } +}; +} // namespace corr_matrix_constrain_test + +// lp is log |det J| of the map from y to the strictly lower triangular +// elements of the correlation matrix. +TEST(mathMixMatFun, corr_matrix_constrain_lp_is_log_det_jacobian) { + Eigen::VectorXd y(6); + y << 0.3, -1.2, 0.8, 2.1, -0.4, 1.5; + Eigen::VectorXd fx; + Eigen::MatrixXd J; + stan::math::jacobian( + corr_matrix_constrain_test::strict_lower_of_constrained{4}, y, fx, J); + double lp = 0; + stan::math::corr_matrix_constrain(y, 4, lp); + EXPECT_NEAR(std::log(std::fabs(J.determinant())), lp, 1e-12); +} + +// lp = sum_n w_n log(1 - tanh(y_n)^2) with weights (1.5, 1.5, 1) for K = 3, +// and its gradient, at large y against mpmath. Forming 1 - tanh(y)^2 loses +// digits from |y| = 8. +TEST(mathMixMatFun, corr_matrix_constrain_lp_large_argument) { + using stan::math::fvar; + using stan::math::var; + Eigen::VectorXd y(3); + y << 15.0, 12.0, -0.5; + const double lp_ref = -77.081345930670418; + Eigen::VectorXd grad_ref(3); + grad_ref << -2.9999999999994385, -2.9999999997734919, 0.92423431452001952; + + double lp_d = 0; + stan::math::corr_matrix_constrain(y, 3, lp_d); + EXPECT_NEAR(lp_ref, lp_d, 1e-13); + + Eigen::Matrix y_v = stan::math::to_var(y); + var lp_v = 0; + stan::math::corr_matrix_constrain(y_v, 3, lp_v); + lp_v.grad(); + EXPECT_NEAR(lp_ref, lp_v.val(), 1e-13); + for (int i = 0; i < 3; ++i) { + EXPECT_NEAR(grad_ref(i), y_v(i).adj(), 1e-14); + } + stan::math::recover_memory(); + + stan::math::var_value y_m(y); + var lp_m = 0; + stan::math::corr_matrix_constrain(y_m, 3, lp_m); + lp_m.grad(); + EXPECT_NEAR(lp_ref, lp_m.val(), 1e-13); + for (int i = 0; i < 3; ++i) { + EXPECT_NEAR(grad_ref(i), y_m.adj()(i), 1e-14); + } + stan::math::recover_memory(); + + for (int n = 0; n < 3; ++n) { + Eigen::Matrix, Eigen::Dynamic, 1> y_f(3); + for (int i = 0; i < 3; ++i) { + y_f(i) = fvar(y(i), i == n ? 1.0 : 0.0); + } + fvar lp_f = 0; + stan::math::corr_matrix_constrain(y_f, 3, lp_f); + EXPECT_NEAR(lp_ref, lp_f.val_, 1e-13); + EXPECT_NEAR(grad_ref(n), lp_f.d_, 1e-14); + } +} diff --git a/test/unit/math/mix/fun/log_cosh_test.cpp b/test/unit/math/mix/fun/log_cosh_test.cpp new file mode 100644 index 00000000000..0eb052845fe --- /dev/null +++ b/test/unit/math/mix/fun/log_cosh_test.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include + +TEST(mathMixMatFun, logCosh) { + auto f = [](const auto& x1) { return stan::math::log_cosh(x1); }; + stan::test::expect_common_unary_vectorized( + f); + stan::test::expect_unary_vectorized( + f, -30.0, -2.6, -1.0, -0.5, 0.0, 0.5, 0.999, 1.0, 1.001, 2.6, 30.0); +} + +TEST(mathMixMatFun, logCoshVarMat) { + using stan::math::vec_concat; + using stan::test::expect_ad_vector_matvar; + using stan::test::internal::common_nonzero_args; + auto f = [](const auto& x1) { return stan::math::log_cosh(x1); }; + std::vector com_args = common_nonzero_args(); + std::vector args{-30.0, -2.6, -0.5, 0.999, 1.001, 2.6, 30.0}; + auto all_args = vec_concat(com_args, args); + Eigen::VectorXd A(all_args.size()); + for (int i = 0; i < all_args.size(); ++i) { + A(i) = all_args[i]; + } + expect_ad_vector_matvar(f, A); +} + +// Values and derivatives against mpmath, on both sides of the branch at +// |x| = 1, at a tiny argument (where log(cosh(x)) = x^2 / 2), and where +// cosh(x) overflows. +TEST(mathMixMatFun, logCoshReference) { + using stan::math::fvar; + using stan::math::var; + // x, log(cosh(x)), tanh(x) + std::vector> cases + = {{1e-10, 5.0e-21, 1.0e-10}, + {0.5, 0.12011450695827752, 0.46211715726000976}, + {0.999, 0.43301944642088479, 0.76117386166056995}, + {1.0, 0.43378083048302719, 0.76159415595576489}, + {-3.0, 2.3093285045777851, -0.99505475368673045}, + {30.0, 29.306852819440055, 1.0}, + {800.0, 799.30685281944005, 1.0}}; + for (const auto& c : cases) { + const double x = c[0]; + const double tol_v = 4 * std::numeric_limits::epsilon() * c[1]; + const double tol_d + = 4 * std::numeric_limits::epsilon() * std::fabs(c[2]); + EXPECT_NEAR(c[1], stan::math::log_cosh(x), tol_v) << "x = " << x; + + var x_v = x; + var y_v = stan::math::log_cosh(x_v); + y_v.grad(); + EXPECT_NEAR(c[1], y_v.val(), tol_v) << "x = " << x; + EXPECT_NEAR(c[2], x_v.adj(), tol_d) << "x = " << x; + stan::math::recover_memory(); + + fvar y_f = stan::math::log_cosh(fvar(x, 1.0)); + EXPECT_NEAR(c[1], y_f.val_, tol_v) << "x = " << x; + EXPECT_NEAR(c[2], y_f.d_, tol_d) << "x = " << x; + } +} diff --git a/test/unit/math/mix/fun/tanh_test.cpp b/test/unit/math/mix/fun/tanh_test.cpp index d9e4ad5d567..9a894406fb9 100644 --- a/test/unit/math/mix/fun/tanh_test.cpp +++ b/test/unit/math/mix/fun/tanh_test.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include TEST(mathMixMatFun, tanh) { auto f = [](const auto& x1) { @@ -13,6 +16,71 @@ TEST(mathMixMatFun, tanh) { stan::test::expect_complex_common(f); } +// First and second derivatives at large |x| against mpmath. 1 - tanh(x)^2 +// is 0 in double from |x| = 19.06, and 1 / cosh(x)^2 makes the second +// derivative NaN once cosh(x)^2 overflows (|x| > 355). +TEST(mathMixMatFun, tanh_derivatives_large_argument) { + using stan::math::fvar; + using stan::math::var; + // x, d/dx tanh(x), d2/dx2 tanh(x) + std::vector> cases + = {{3.0, 0.0098660371654401913, -0.019634494363042436}, + {-20.0, 1.6993417021166356e-17, 3.3986834042332711e-17}, + {20.0, 1.6993417021166356e-17, -3.3986834042332711e-17}, + {300.0, 1.0601586212017243e-260, -2.1203172424034487e-260}, + {1000.0, 0.0, 0.0}}; + for (const auto& c : cases) { + const double x = c[0]; + const double d1 = c[1]; + const double d2 = c[2]; + + fvar y1 = stan::math::tanh(fvar(x, 1.0)); + EXPECT_NEAR(d1, y1.d_, 1e-14 * std::fabs(d1)) << "x = " << x; + + fvar> y2 = stan::math::tanh( + fvar>(fvar(x, 1.0), fvar(1.0, 0.0))); + EXPECT_NEAR(d1, y2.d_.val_, 1e-14 * std::fabs(d1)) << "x = " << x; + EXPECT_NEAR(d2, y2.d_.d_, 1e-14 * std::fabs(d2)) << "x = " << x; + + fvar x3(var(x), 1.0); + fvar y3 = stan::math::tanh(x3); + y3.d_.grad(); + EXPECT_NEAR(d1, y3.d_.val(), 1e-14 * std::fabs(d1)) << "x = " << x; + EXPECT_NEAR(d2, x3.val_.adj(), 1e-14 * std::fabs(d2)) << "x = " << x; + stan::math::recover_memory(); + } +} + +// First, second and third derivatives in fvar> against mpmath, +// at 0 (where an fabs(x) in the derivative would make the third derivative +// 0), on both sides of 0.5, and in the tail. +TEST(mathMixMatFun, tanh_third_derivative) { + using stan::math::fvar; + using stan::math::var; + // x, tanh'(x), tanh''(x), tanh'''(x) + std::vector> cases = { + {0.0, 1.0, 0.0, -2.0}, + {0.25, 0.94001484880637796, -0.46045435881856592, -1.5417081006333538}, + {0.4999, 0.78652041633736061, -0.72680544069311918, -0.56560452654352799}, + {0.5, 0.78644773296592741, -0.72686198138358728, -0.56520928825977036}, + {-0.5, 0.78644773296592741, 0.72686198138358728, -0.56520928825977036}, + {3.0, 0.0098660371654401913, -0.019634494363042436, 0.038880116525661682}, + {-20.0, 1.6993417021166356e-17, 3.3986834042332711e-17, + 6.7973668084665422e-17}}; + for (const auto& c : cases) { + const double x = c[0]; + var x_v = x; + fvar> x_ffv(fvar(x_v, 1.0), fvar(1.0, 0.0)); + fvar> y = stan::math::tanh(x_ffv); + y.d_.d_.grad(); + EXPECT_NEAR(c[1], y.d_.val_.val(), 1e-14 * std::fabs(c[1])) << "x = " << x; + EXPECT_NEAR(c[2], y.d_.d_.val(), 1e-14 * std::fabs(c[2]) + 1e-300) + << "x = " << x; + EXPECT_NEAR(c[3], x_v.adj(), 1e-14 * std::fabs(c[3])) << "x = " << x; + stan::math::recover_memory(); + } +} + TEST(mathMixMatFun, tanh_varmat) { using stan::math::vec_concat; using stan::test::expect_ad_vector_matvar; From 7cf096ef0841444238619a8569b505a63a7e0e6e Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Tue, 13 Oct 2026 11:31:08 +0300 Subject: [PATCH 2/7] Compute the correlation transform values without cancellation --- .../constraint/cholesky_corr_constrain.hpp | 175 +++++++++++++----- .../prim/constraint/corr_matrix_constrain.hpp | 18 +- .../constraint/cholesky_corr_constrain.hpp | 158 +++++++--------- .../rev/constraint/corr_matrix_constrain.hpp | 14 +- .../cholesky_corr_constrain_test.cpp | 98 ++++++++++ .../constraint/corr_matrix_constrain_test.cpp | 92 +++++++++ test/unit/math/rev/prob/lkj_corr_test.cpp | 5 +- 7 files changed, 411 insertions(+), 149 deletions(-) diff --git a/stan/math/prim/constraint/cholesky_corr_constrain.hpp b/stan/math/prim/constraint/cholesky_corr_constrain.hpp index d03890a8e47..a964b716387 100644 --- a/stan/math/prim/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/prim/constraint/cholesky_corr_constrain.hpp @@ -4,11 +4,12 @@ #include #include #include +#include #include -#include -#include -#include +#include +#include #include +#include namespace stan { namespace math { @@ -38,71 +39,151 @@ inline Eigen::VectorXd cholesky_corr_log_jacobian_coefficients(int K) { } return c; } + +/** + * Return `tanh(y)` and `sech(y) = 1 / cosh(y)` from one call of `expm1` or + * `exp`, with a relative error of a few ulp each. + * + * For `|y| < 1`, with `u = expm1(|y|)`, `cosh = 1 + u^2 / (2 (1 + u))` and + * `sinh = u (u + 2) / (2 (1 + u))`. For `|y| >= 1`, with `e = exp(-|y|)`, + * `tanh = (1 - e^2) / (1 + e^2)` and `sech = 2 e / (1 + e^2)`. + * + * @param y argument + * @return pair of `tanh(y)` and `sech(y)` + */ +inline std::pair tanh_and_sech(double y) { + const double a = std::fabs(y); + double tanh_a; + double sech; + if (a < 1.0) { + const double u = std::expm1(a); + const double cosh = 1.0 + u * u / (2.0 * (1.0 + u)); + tanh_a = u * (u + 2.0) / (2.0 * (1.0 + u)) / cosh; + sech = 1.0 / cosh; + } else { + const double e = std::exp(-a); + const double e2 = e * e; + tanh_a = (1.0 - e2) / (1.0 + e2); + sech = 2.0 * e / (1.0 + e2); + } + return {std::copysign(tanh_a, y), sech}; +} + +/** + * Return the position in the unconstrained vector of the partial + * correlation for element `(r, c)`, `c < r`, of the Cholesky factor. + * + * @tparam ColumnOrder `false` for the row order of + * `cholesky_corr_constrain` (row 1, then row 2, ...), `true` for the column + * order of `corr_matrix_constrain` (column 0, then column 1, ...) + * @param r row + * @param c column + * @param K number of rows + * @return position in the unconstrained vector + */ +template +inline Eigen::Index cholesky_corr_position(Eigen::Index r, Eigen::Index c, + Eigen::Index K) { + if constexpr (ColumnOrder) { + return c * (K - 1) - (c * (c - 1)) / 2 + (r - c - 1); + } else { + return (r * (r - 1)) / 2 + c; + } +} + +/** + * Return the Cholesky factor of a correlation matrix from unconstrained + * values `y`, whose partial correlations are `tanh(y)`. + * + * In row `r`, `1 - sum` of the squares of the elements before column `c` + * is `prod_{m < c} (1 - tanh(y_m)^2) = exp(-2 a)` with + * `a = sum_{m < c} log(cosh(y_m))`, so + * `L(r, c) = tanh(y_c) exp(-a)` and `L(r, r) = exp(-a)` over the whole + * row. Unlike `sqrt(1 - sum)`, this has no subtraction: the elements keep + * a small relative error when partial correlations are close to ±1 or a + * row is long, and the diagonal stays positive unless `a` exceeds about + * 745. + * + * @tparam ColumnOrder order of the partial correlations in `y`, see + * `cholesky_corr_position` + * @tparam EigVec type of the vector + * @param y unconstrained values, `K (K - 1) / 2` of them + * @param K number of rows + * @return Cholesky factor of a correlation matrix + */ +template * = nullptr> +inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> +cholesky_corr_factor(const EigVec& y, Eigen::Index K) { + using T_scalar = value_type_t; + Eigen::Matrix L + = Eigen::Matrix::Zero(K, K); + if (K == 0) { + return L; + } + const auto& y_ref = to_ref(y); + L.coeffRef(0, 0) = 1; + for (Eigen::Index r = 1; r < K; ++r) { + T_scalar a = 0; + for (Eigen::Index c = 0; c < r; ++c) { + const auto& y_rc + = y_ref.coeff(cholesky_corr_position(r, c, K)); + L.coeffRef(r, c) = tanh(y_rc) * exp(-a); + a += log_cosh(y_rc); + } + L.coeffRef(r, r) = exp(-a); + } + 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 * = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cholesky_corr_constrain(const EigVec& y, int K) { - using Eigen::Dynamic; - using Eigen::Matrix; - using std::sqrt; - using T_scalar = value_type_t; 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 z = corr_constrain(y); - Matrix 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) { - 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; + return internal::cholesky_corr_factor(y, 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 * = nullptr, require_convertible_t, Lp>* = nullptr> inline Eigen::Matrix, 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; 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 z = corr_constrain(y); - Matrix x(K, K); if (K == 0) { - return x; + return Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic>( + 0, 0); } + const auto& y_ref = to_ref(y); lp -= dot_product(internal::cholesky_corr_log_jacobian_coefficients(K), - log_cosh(y)); - 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)); - } - x.coeffRef(i, i) = sqrt(1.0 - sum_sqs); - } - return x; + log_cosh(y_ref)); + return internal::cholesky_corr_factor(y_ref, K); } /** diff --git a/stan/math/prim/constraint/corr_matrix_constrain.hpp b/stan/math/prim/constraint/corr_matrix_constrain.hpp index b937e929e51..2b9c3cae8bd 100644 --- a/stan/math/prim/constraint/corr_matrix_constrain.hpp +++ b/stan/math/prim/constraint/corr_matrix_constrain.hpp @@ -4,9 +4,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -73,9 +75,11 @@ corr_matrix_constrain(T&& x, Eigen::Index k) { decltype(auto) x_ref = to_ref(std::forward(x)); check_size_match("cov_matrix_constrain", "x.size()", x_ref.size(), "k_choose_2", k_choose_2); - decltype(auto) cpc_ref - = to_ref(corr_constrain(std::forward(x_ref))); - return read_corr_matrix(cpc_ref, k); + if (k == 0) { + return {}; + } + return multiply_lower_tri_self_transpose( + internal::cholesky_corr_factor(x_ref, k)); } /** @@ -108,11 +112,13 @@ corr_matrix_constrain(T&& x, Eigen::Index k, Lp& lp) { decltype(auto) x_ref = to_ref(std::forward(x)); check_size_match("cov_matrix_constrain", "x.size()", x_ref.size(), "k_choose_2", k_choose_2); + if (k == 0) { + return {}; + } lp -= dot_product(internal::corr_matrix_log_jacobian_coefficients(k), log_cosh(x_ref)); - decltype(auto) cpc_ref - = to_ref(corr_constrain(std::forward(x_ref))); - return read_corr_matrix(cpc_ref, k); + return multiply_lower_tri_self_transpose( + internal::cholesky_corr_factor(x_ref, k)); } /** diff --git a/stan/math/rev/constraint/cholesky_corr_constrain.hpp b/stan/math/rev/constraint/cholesky_corr_constrain.hpp index c2a87c2505e..dab6933210b 100644 --- a/stan/math/rev/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/rev/constraint/cholesky_corr_constrain.hpp @@ -6,15 +6,75 @@ #include #include #include -#include -#include +#include #include -#include #include +#include namespace stan { namespace math { +namespace internal { +/** + * Return the Cholesky factor of a correlation matrix from unconstrained + * values `y`, whose partial correlations are `tanh(y)`. See the `prim` + * version for the formulas. + * + * Reverse pass: in row `r`, element `c` depends on its own `y` through + * `tanh(y) exp(-a)`, with derivative `exp(-2 log(cosh(y)) - a)`, and every + * later element of the row, the diagonal included, depends on it through + * `exp(-a)`, with derivative `-tanh(y)` times the element. + * + * @tparam ColumnOrder order of the partial correlations in `y`, see + * `cholesky_corr_position` + * @tparam T type of the `var_value` vector + * @param y unconstrained values, `K (K - 1) / 2` of them + * @param K number of rows + * @return Cholesky factor of a correlation matrix + */ +template * = nullptr> +inline var_value cholesky_corr_factor(const T& y, + Eigen::Index K) { + using Eigen::Index; + if (K == 0) { + return Eigen::MatrixXd(); + } + const Index n = y.size(); + arena_t tanh_y(n); + // derivative of each element with respect to its own unconstrained value + arena_t d_own(n); + arena_t L_val = Eigen::MatrixXd::Zero(K, K); + L_val.coeffRef(0, 0) = 1; + for (Index r = 1; r < K; ++r) { + // exp(-a), as the product of sech(y) over the earlier elements of the row + double exp_minus_a = 1.0; + for (Index c = 0; c < r; ++c) { + const Index k = cholesky_corr_position(r, c, K); + const std::pair t_s = tanh_and_sech(y.val().coeff(k)); + tanh_y.coeffRef(k) = t_s.first; + d_own.coeffRef(k) = t_s.second * t_s.second * exp_minus_a; + L_val.coeffRef(r, c) = t_s.first * exp_minus_a; + exp_minus_a *= t_s.second; + } + L_val.coeffRef(r, r) = exp_minus_a; + } + var_value L = L_val; + reverse_pass_callback([y, L, tanh_y, d_own, K]() mutable { + for (Index r = 1; r < K; ++r) { + double later = L.adj().coeff(r, r) * L.val().coeff(r, r); + for (Index c = r - 1; c >= 0; --c) { + const Index k = cholesky_corr_position(r, c, K); + const double adj_rc = L.adj().coeff(r, c); + y.adj().coeffRef(k) + += adj_rc * d_own.coeff(k) - tanh_y.coeff(k) * later; + later += adj_rc * L.val().coeff(r, c); + } + } + }); + return L; +} +} // namespace internal + /** * Return the Cholesky factor of the correlation matrix of the specified * size read from the unconstrained vector `y`. A total of K choose 2 @@ -28,54 +88,10 @@ namespace math { */ template * = nullptr> inline var_value cholesky_corr_constrain(const T& y, int K) { - using std::sqrt; int k_choose_2 = (K * (K - 1)) / 2; check_size_match("cholesky_corr_constrain", "y.size()", y.size(), "k_choose_2", k_choose_2); - - if (K == 0) { - return Eigen::MatrixXd(); - } - - var_value z = corr_constrain(y); - arena_t x_val = Eigen::MatrixXd::Zero(K, K); - - x_val.coeffRef(0, 0) = 1; - int k = 0; - for (int i = 1; i < K; ++i) { - x_val.coeffRef(i, 0) = z.val().coeff(k); - k++; - double sum_sqs = square(x_val.coeff(i, 0)); - for (int j = 1; j < i; ++j) { - x_val.coeffRef(i, j) = z.val().coeff(k) * sqrt(1.0 - sum_sqs); - k++; - sum_sqs += square(x_val.coeff(i, j)); - } - x_val.coeffRef(i, i) = sqrt(1.0 - sum_sqs); - } - - var_value x = x_val; - - reverse_pass_callback([z, K, x]() mutable { - size_t k = z.size(); - for (int i = K - 1; i > 0; --i) { - double sum_sqs_val = 1.0 - square(x.val().coeffRef(i, i)); - double sum_sqs_adj = -0.5 * x.adj().coeff(i, i) / x.val().coeff(i, i); - for (int j = i - 1; j > 0; --j) { - x.adj().coeffRef(i, j) += 2 * sum_sqs_adj * x.val().coeff(i, j); - sum_sqs_val -= square(x.val().coeff(i, j)); - k--; - sum_sqs_adj += -0.5 * x.adj().coeffRef(i, j) * z.val().coeff(k) - / sqrt(1.0 - sum_sqs_val); - z.adj().coeffRef(k) += x.adj().coeffRef(i, j) * sqrt(1.0 - sum_sqs_val); - } - x.adj().coeffRef(i, 0) += 2 * sum_sqs_adj * x.val().coeff(i, 0); - k--; - z.adj().coeffRef(k) += x.adj().coeffRef(i, 0); - } - }); - - return x; + return internal::cholesky_corr_factor(y, K); } /** @@ -93,59 +109,15 @@ inline var_value cholesky_corr_constrain(const T& y, int K) { template * = nullptr> inline var_value cholesky_corr_constrain( const T& y, int K, scalar_type_t& lp) { - using Eigen::Dynamic; - using Eigen::Matrix; - using std::sqrt; int k_choose_2 = (K * (K - 1)) / 2; check_size_match("cholesky_corr_constrain", "y.size()", y.size(), "k_choose_2", k_choose_2); - if (K == 0) { return Eigen::MatrixXd(); } - lp -= dot_product(internal::cholesky_corr_log_jacobian_coefficients(K), log_cosh(y)); - var_value z = corr_constrain(y); - arena_t x_val = Eigen::MatrixXd::Zero(K, K); - - x_val.coeffRef(0, 0) = 1; - int k = 0; - for (int i = 1; i < K; ++i) { - x_val.coeffRef(i, 0) = z.val().coeff(k); - k++; - double sum_sqs = square(x_val.coeff(i, 0)); - for (int j = 1; j < i; ++j) { - x_val.coeffRef(i, j) = z.val().coeff(k) * sqrt(1.0 - sum_sqs); - k++; - sum_sqs += square(x_val.coeff(i, j)); - } - x_val.coeffRef(i, i) = sqrt(1.0 - sum_sqs); - } - - var_value x = x_val; - - reverse_pass_callback([z, K, x]() mutable { - size_t k = z.size(); - for (int i = K - 1; i > 0; --i) { - double sum_sqs_val = 1.0 - square(x.val().coeffRef(i, i)); - double sum_sqs_adj = -0.5 * x.adj().coeff(i, i) / x.val().coeff(i, i); - for (int j = i - 1; j > 0; --j) { - x.adj().coeffRef(i, j) += 2 * sum_sqs_adj * x.val().coeff(i, j); - sum_sqs_val -= square(x.val().coeff(i, j)); - - k--; - sum_sqs_adj += -0.5 * x.adj().coeffRef(i, j) * z.val().coeff(k) - / sqrt(1.0 - sum_sqs_val); - z.adj().coeffRef(k) += x.adj().coeffRef(i, j) * sqrt(1.0 - sum_sqs_val); - } - x.adj().coeffRef(i, 0) += 2 * sum_sqs_adj * x.val().coeff(i, 0); - k--; - z.adj().coeffRef(k) += x.adj().coeffRef(i, 0); - } - }); - - return x; + return internal::cholesky_corr_factor(y, K); } } // namespace math diff --git a/stan/math/rev/constraint/corr_matrix_constrain.hpp b/stan/math/rev/constraint/corr_matrix_constrain.hpp index 4694e0f49b7..d29898cabe3 100644 --- a/stan/math/rev/constraint/corr_matrix_constrain.hpp +++ b/stan/math/rev/constraint/corr_matrix_constrain.hpp @@ -2,8 +2,10 @@ #define STAN_MATH_REV_CONSTRAINT_CORR_MATRIX_CONSTRAIN_HPP #include +#include #include #include +#include #include #include #include @@ -46,7 +48,11 @@ inline var_value corr_matrix_constrain(const T& x, Eigen::Index k_choose_2 = (k * (k - 1)) / 2; check_size_match("cov_matrix_constrain", "x.size()", x.size(), "k_choose_2", k_choose_2); - return read_corr_matrix(corr_constrain(x), k); + if (k == 0) { + return Eigen::MatrixXd(); + } + return multiply_lower_tri_self_transpose( + internal::cholesky_corr_factor(x, k)); } /** @@ -76,9 +82,13 @@ inline var_value corr_matrix_constrain(const T& x, Eigen::Index k_choose_2 = (k * (k - 1)) / 2; check_size_match("cov_matrix_constrain", "x.size()", x.size(), "k_choose_2", k_choose_2); + if (k == 0) { + return Eigen::MatrixXd(); + } lp -= dot_product(internal::corr_matrix_log_jacobian_coefficients(k), log_cosh(x)); - return read_corr_matrix(corr_constrain(x), k); + return multiply_lower_tri_self_transpose( + internal::cholesky_corr_factor(x, k)); } } // namespace math diff --git a/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp b/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp index 200ff43daf9..7618098e847 100644 --- a/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp +++ b/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp @@ -1,4 +1,5 @@ #include +#include namespace cholesky_corr_constrain_test { // easier than fiddling the quadratic equation @@ -196,3 +197,100 @@ TEST(mathMixMatFun, cholesky_corr_constrain_lp_large_argument) { EXPECT_NEAR(grad_ref(n), lp_f.d_, 1e-14); } } + +namespace cholesky_corr_constrain_test { +// lower triangle (diagonal included), row by row +template +Eigen::VectorXd lower_triangle(const M& L) { + const int K = L.rows(); + Eigen::VectorXd v((K * (K + 1)) / 2); + int k = 0; + for (int i = 0; i < K; ++i) { + for (int j = 0; j <= i; ++j) { + v(k++) = stan::math::value_of_rec(L(i, j)); + } + } + return v; +} +} // namespace cholesky_corr_constrain_test + +// Values of L at large |y| against mpmath. sqrt(1 - sum of squares) had a +// relative error of 3.8e-4 in L(1, 1) at y = 15. +TEST(mathMixMatFun, cholesky_corr_constrain_values_large_argument) { + using cholesky_corr_constrain_test::lower_triangle; + using stan::math::fvar; + using stan::math::var; + Eigen::VectorXd y(6); + y << 15.0, 12.0, -0.5, 9.0, 2.0, -14.0; + Eigen::VectorXd ref(10); + ref << 1.0, 0.99999999999981285, 6.1180464100359433e-7, 0.99999999992449731, + -5.6786918924293556e-6, 1.0897607083695929e-5, 0.99999996954004097, + 0.00023794090595878951, -6.5605200973176475e-5, 1.091052174635906e-10; + auto expect_ref = [&ref](const Eigen::VectorXd& v, const char* mode) { + for (int i = 0; i < ref.size(); ++i) { + EXPECT_NEAR(ref(i), v(i), 4e-15 * std::fabs(ref(i))) + << mode << ", element " << i; + } + }; + expect_ref(lower_triangle(stan::math::cholesky_corr_constrain(y, 4)), + "double"); + Eigen::Matrix y_v = stan::math::to_var(y); + expect_ref(lower_triangle(stan::math::cholesky_corr_constrain(y_v, 4)), + "var"); + stan::math::var_value y_m(y); + expect_ref( + lower_triangle(stan::math::cholesky_corr_constrain(y_m, 4).val().eval()), + "var_value"); + Eigen::Matrix, Eigen::Dynamic, 1> y_f(6); + for (int i = 0; i < 6; ++i) { + y_f(i) = fvar(y(i), 1.0); + } + expect_ref(lower_triangle(stan::math::cholesky_corr_constrain(y_f, 4)), + "fvar"); + stan::math::recover_memory(); +} + +// K = 40 with unconstrained values in [-1.9, 1.9], as default inits: a row +// of 39 moderate values makes 1 - sum of squares about 1e-20, which +// sqrt(1 - sum) rounded to 0. The diagonal must stay positive, the rows +// must have unit norm, lp and the LKJ density must be finite, and the +// var_value gradient must equal the Eigen gradient. +TEST(mathMixMatFun, cholesky_corr_constrain_long_rows) { + using stan::math::var; + const int K = 40; + Eigen::VectorXd y((K * (K - 1)) / 2); + for (int i = 0; i < y.size(); ++i) { + y(i) = 1.9 * std::sin(1.3 * i + 0.7); + } + double lp = 0; + Eigen::MatrixXd L = stan::math::cholesky_corr_constrain(y, K, lp); + EXPECT_GT(L.diagonal().minCoeff(), 0.0); + EXPECT_LT((L.rowwise().squaredNorm().array() - 1).abs().maxCoeff(), 1e-14); + EXPECT_TRUE(std::isfinite(lp)); + EXPECT_TRUE(std::isfinite(stan::math::lkj_corr_cholesky_lpdf(L, 1.0))); + + // gradient of 2 sum(log(diag(L))) + lp: Eigen through the prim code, + // var_value through the hand-written reverse pass + Eigen::Matrix y_v = stan::math::to_var(y); + var lp_v = 0; + Eigen::Matrix L_v + = stan::math::cholesky_corr_constrain(y_v, K, lp_v); + var f_v = 2.0 * stan::math::sum(stan::math::log(L_v.diagonal())) + lp_v; + f_v.grad(); + Eigen::VectorXd g_v = stan::math::value_of(y_v.adj()); + stan::math::recover_memory(); + + stan::math::var_value y_m(y); + var lp_m = 0; + stan::math::var_value L_m + = stan::math::cholesky_corr_constrain(y_m, K, lp_m); + var f_m = 2.0 * stan::math::sum(stan::math::log(L_m.diagonal())) + lp_m; + f_m.grad(); + Eigen::VectorXd g_m = y_m.adj(); + stan::math::recover_memory(); + + EXPECT_TRUE(g_v.allFinite()); + EXPECT_TRUE(g_m.allFinite()); + EXPECT_LT((g_v - g_m).cwiseAbs().maxCoeff(), + 1e-12 * g_v.cwiseAbs().maxCoeff()); +} diff --git a/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp b/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp index 688d1acb39b..b0539788946 100644 --- a/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp +++ b/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp @@ -1,4 +1,5 @@ #include +#include namespace corr_matrix_constrain_test { // easier than fiddling the quadratic equation @@ -199,3 +200,94 @@ TEST(mathMixMatFun, corr_matrix_constrain_lp_large_argument) { EXPECT_NEAR(grad_ref(n), lp_f.d_, 1e-14); } } + +namespace corr_matrix_constrain_test { +// strictly lower triangle, row by row +template +Eigen::VectorXd strict_lower(const M& Omega) { + const int K = Omega.rows(); + Eigen::VectorXd v((K * (K - 1)) / 2); + int k = 0; + for (int i = 0; i < K; ++i) { + for (int j = 0; j < i; ++j) { + v(k++) = stan::math::value_of_rec(Omega(i, j)); + } + } + return v; +} +} // namespace corr_matrix_constrain_test + +// Correlations at large |y| against mpmath. With sqrt(1 - z^2) from a +// rounded z, the small elements of the Cholesky factor had relative errors +// up to 3.8e-4, which reached the correlations close to 1. +TEST(mathMixMatFun, corr_matrix_constrain_values_large_argument) { + using corr_matrix_constrain_test::strict_lower; + using stan::math::fvar; + using stan::math::var; + Eigen::VectorXd y(6); + y << 15.0, 12.0, -0.5, 9.0, 2.0, -14.0; + Eigen::VectorXd ref(6); + ref << 0.99999999999981285, 0.99999999992449731, 0.99999999993182827, + -0.46211715726000976, -0.46211663421720721, -0.46210665234659286; + auto expect_ref = [&ref](const Eigen::VectorXd& v, const char* mode) { + for (int i = 0; i < ref.size(); ++i) { + EXPECT_NEAR(ref(i), v(i), 4e-16) << mode << ", element " << i; + } + }; + expect_ref(strict_lower(stan::math::corr_matrix_constrain(y, 4)), "double"); + Eigen::Matrix y_v = stan::math::to_var(y); + expect_ref(strict_lower(stan::math::corr_matrix_constrain(y_v, 4)), "var"); + stan::math::var_value y_m(y); + expect_ref( + strict_lower(stan::math::corr_matrix_constrain(y_m, 4).val().eval()), + "var_value"); + Eigen::Matrix, Eigen::Dynamic, 1> y_f(6); + for (int i = 0; i < 6; ++i) { + y_f(i) = fvar(y(i), 1.0); + } + expect_ref(strict_lower(stan::math::corr_matrix_constrain(y_f, 4)), "fvar"); + stan::math::recover_memory(); +} + +// K = 40 with unconstrained values in [-1.9, 1.9], as default inits: the +// diagonal must be 1, lp finite, and the var_value gradient must equal the +// Eigen gradient and be finite. +TEST(mathMixMatFun, corr_matrix_constrain_long_rows) { + using stan::math::var; + const int K = 40; + Eigen::VectorXd y((K * (K - 1)) / 2); + for (int i = 0; i < y.size(); ++i) { + y(i) = 1.9 * std::sin(1.3 * i + 0.7); + } + Eigen::MatrixXd W(K, K); + for (int i = 0; i < W.size(); ++i) { + W(i) = std::cos(0.7 * i); + } + double lp = 0; + Eigen::MatrixXd Omega = stan::math::corr_matrix_constrain(y, K, lp); + EXPECT_LT((Omega.diagonal().array() - 1).abs().maxCoeff(), 1e-14); + EXPECT_TRUE(std::isfinite(lp)); + + Eigen::Matrix y_v = stan::math::to_var(y); + var lp_v = 0; + Eigen::Matrix Omega_v + = stan::math::corr_matrix_constrain(y_v, K, lp_v); + var f_v = (W.array() * Omega_v.array()).sum() + lp_v; + f_v.grad(); + Eigen::VectorXd g_v = stan::math::value_of(y_v.adj()); + stan::math::recover_memory(); + + stan::math::var_value y_m(y); + var lp_m = 0; + stan::math::var_value Omega_m + = stan::math::corr_matrix_constrain(y_m, K, lp_m); + var f_m = stan::math::sum(stan::math::elt_multiply(W, Omega_m)) + lp_m; + f_m.grad(); + Eigen::VectorXd g_m = y_m.adj(); + stan::math::recover_memory(); + + EXPECT_TRUE(g_v.allFinite()); + EXPECT_TRUE(g_m.allFinite()); + EXPECT_LT((g_v - g_m).cwiseAbs().maxCoeff(), + 1e-12 * g_v.cwiseAbs().maxCoeff()); +} diff --git a/test/unit/math/rev/prob/lkj_corr_test.cpp b/test/unit/math/rev/prob/lkj_corr_test.cpp index 6c912bd949a..c56c7972bf0 100644 --- a/test/unit/math/rev/prob/lkj_corr_test.cpp +++ b/test/unit/math/rev/prob/lkj_corr_test.cpp @@ -65,7 +65,10 @@ TEST_F(AgradRev, ProbDistributionsLkjCorrCholesky_gradients1) { double fx_ad_1; finite_diff_gradient(test_func_1, x1, fx_1, grad_1); gradient(test_func_1, x1, fx_ad_1, grad_ad_1); - test_grad_eq(grad_1, grad_ad_1); + // x1(0) = 0, where the exact derivative is 0; the finite difference has + // rounding noise of about 1e-13 there, so compare with an absolute + // tolerance instead of float equality + test_grad_near(grad_1, grad_ad_1, 1e-10); EXPECT_FLOAT_EQ(fx_1, fx_ad_1); } From d8182c8c9dbc1c83dcafde452dc16c779569159c Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 11 Nov 2026 11:03:52 -0500 Subject: [PATCH 3/7] Address review findings on the correlation transforms - Fix the log Jacobian of cov_matrix_constrain_lkj(x, k, lp): it omitted the tanh term of the partial correlations and the exp term of the standard deviations. lp now gets the corr_matrix_constrain Jacobian plus k log 2 + (k + 1) sum(x.tail(k)), checked against log|det J| of the map to the lower triangle. - Route cov_matrix_constrain_lkj through the cancellation-free Cholesky factor instead of read_cov_L. - Merge the double helpers into log_cosh_tanh_sech and call the stan::math wrappers instead of std:: math. - Replace the bool template on the factor and position helpers with the CorrOrder enum; split the coefficient builder into row and column versions returning lazy make_holder expressions over LinSpaced indices. - Share the log_cosh vector between the Jacobian and the factor so the Eigen lp overloads evaluate it once; evaluate the lazy input of corr_constrain(x, lp) once. - Narrow the lp overload of cholesky_corr_constrain and both cov_matrix_constrain_lkj overloads to column vectors, matching the non-lp overload; row vectors broke the rev reverse pass. - Perfect-forward container arguments, drop the redundant K == 0 guards and stale includes, and trim the new docs to two sentences. - Move the repeated four-mode lp, value, and log-det-Jacobian test blocks into corr_transform_helpers.hpp. --- stan/math/fwd/fun/tanh.hpp | 15 +- .../constraint/cholesky_corr_constrain.hpp | 164 ++++++++---------- stan/math/prim/constraint/corr_constrain.hpp | 11 +- .../prim/constraint/corr_matrix_constrain.hpp | 46 +---- .../constraint/cov_matrix_constrain_lkj.hpp | 68 +++++--- stan/math/prim/fun/log_cosh.hpp | 60 ++++--- .../constraint/cholesky_corr_constrain.hpp | 54 +++--- .../rev/constraint/corr_matrix_constrain.hpp | 22 +-- .../constraint/cov_matrix_constrain_lkj.hpp | 74 ++++---- stan/math/rev/fun/log_cosh.hpp | 38 ++-- .../cholesky_corr_constrain_test.cpp | 140 ++++----------- .../mix/constraint/corr_constrain_test.cpp | 43 +---- .../constraint/corr_matrix_constrain_test.cpp | 118 ++----------- .../mix/constraint/corr_transform_helpers.hpp | 146 ++++++++++++++++ .../cov_matrix_constrain_lkj_test.cpp | 74 +++++++- 15 files changed, 532 insertions(+), 541 deletions(-) create mode 100644 test/unit/math/mix/constraint/corr_transform_helpers.hpp diff --git a/stan/math/fwd/fun/tanh.hpp b/stan/math/fwd/fun/tanh.hpp index 7ab38dceb97..a887879a383 100644 --- a/stan/math/fwd/fun/tanh.hpp +++ b/stan/math/fwd/fun/tanh.hpp @@ -6,23 +6,18 @@ #include #include #include +#include #include #include -#include #include 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|): the difference 1 - tanh(x)^2 loses - * all digits for large |x|, and 1 / cosh(x)^2 makes the next derivative NaN - * once cosh(x)^2 overflows. For |x| < 0.5 the difference is accurate. - * Neither branch uses fabs(x), whose derivative is 0 at x = 0 and would - * make the third derivative of tanh at 0 vanish in nested forward mode. + * 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 @@ -32,7 +27,7 @@ template inline fvar tanh(const fvar& x) { const T u = tanh(x.val_); const double x_val = value_of_rec(x.val_); - if (std::fabs(x_val) < 0.5) { + if (fabs(x_val) < 0.5) { return fvar(u, x.d_ * (1 - u * u)); } // -2 |x|, with the sign of x fixed in this branch diff --git a/stan/math/prim/constraint/cholesky_corr_constrain.hpp b/stan/math/prim/constraint/cholesky_corr_constrain.hpp index a964b716387..71a65dba01e 100644 --- a/stan/math/prim/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/prim/constraint/cholesky_corr_constrain.hpp @@ -1,6 +1,7 @@ #ifndef STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP #define STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP +#include #include #include #include @@ -8,7 +9,7 @@ #include #include #include -#include +#include #include namespace stan { @@ -16,75 +17,27 @@ namespace math { namespace internal { /** - * Return the coefficients `c` for which the log absolute Jacobian - * determinant of `cholesky_corr_constrain(y, K)` is - * `-sum_n c_n log(cosh(y_n))`. - * - * With `z = tanh(y)`, row `i` holds `z_m sqrt(prod_{l < m} (1 - z_l^2))` in - * column `m < i`. The Jacobian has a factor `1 - z_m^2` for each element - * and a factor `sqrt(prod_{l < j} (1 - z_l^2))` for each column - * `0 < j < i`. With `log(1 - z_m^2) = -2 log(cosh(y_m))`, the coefficient - * of element `m` of row `i` is `2 + (i - 1 - m) = i + 1 - m`. - * - * @param K number of rows of the Cholesky factor - * @return coefficients in the order of the unconstrained vector - */ -inline Eigen::VectorXd cholesky_corr_log_jacobian_coefficients(int K) { - Eigen::VectorXd c((K * (K - 1)) / 2); - int k = 0; - for (int i = 1; i < K; ++i) { - for (int m = 0; m < i; ++m) { - c.coeffRef(k++) = i + 1 - m; - } - } - return c; -} - -/** - * Return `tanh(y)` and `sech(y) = 1 / cosh(y)` from one call of `expm1` or - * `exp`, with a relative error of a few ulp each. - * - * For `|y| < 1`, with `u = expm1(|y|)`, `cosh = 1 + u^2 / (2 (1 + u))` and - * `sinh = u (u + 2) / (2 (1 + u))`. For `|y| >= 1`, with `e = exp(-|y|)`, - * `tanh = (1 - e^2) / (1 + e^2)` and `sech = 2 e / (1 + e^2)`. - * - * @param y argument - * @return pair of `tanh(y)` and `sech(y)` + * 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, ...). */ -inline std::pair tanh_and_sech(double y) { - const double a = std::fabs(y); - double tanh_a; - double sech; - if (a < 1.0) { - const double u = std::expm1(a); - const double cosh = 1.0 + u * u / (2.0 * (1.0 + u)); - tanh_a = u * (u + 2.0) / (2.0 * (1.0 + u)) / cosh; - sech = 1.0 / cosh; - } else { - const double e = std::exp(-a); - const double e2 = e * e; - tanh_a = (1.0 - e2) / (1.0 + e2); - sech = 2.0 * e / (1.0 + e2); - } - return {std::copysign(tanh_a, y), sech}; -} +enum class CorrOrder : uint8_t { row = 0, column = 1 }; /** * Return the position in the unconstrained vector of the partial * correlation for element `(r, c)`, `c < r`, of the Cholesky factor. * - * @tparam ColumnOrder `false` for the row order of - * `cholesky_corr_constrain` (row 1, then row 2, ...), `true` for the column - * order of `corr_matrix_constrain` (column 0, then column 1, ...) + * @tparam Order order of the partial correlations in the vector * @param r row * @param c column * @param K number of rows * @return position in the unconstrained vector */ -template +template inline Eigen::Index cholesky_corr_position(Eigen::Index r, Eigen::Index c, Eigen::Index K) { - if constexpr (ColumnOrder) { + if constexpr (Order == CorrOrder::column) { return c * (K - 1) - (c * (c - 1)) / 2 + (r - c - 1); } else { return (r * (r - 1)) / 2 + c; @@ -92,44 +45,79 @@ inline Eigen::Index cholesky_corr_position(Eigen::Index r, Eigen::Index c, } /** - * Return the Cholesky factor of a correlation matrix from unconstrained - * values `y`, whose partial correlations are `tanh(y)`. + * 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 + * `(r, c)` is `r + 1 - c`, with `r` recovered from the position by + * inverting `cholesky_corr_position`. * - * In row `r`, `1 - sum` of the squares of the elements before column `c` - * is `prod_{m < c} (1 - tanh(y_m)^2) = exp(-2 a)` with - * `a = sum_{m < c} log(cosh(y_m))`, so - * `L(r, c) = tanh(y_c) exp(-a)` and `L(r, r) = exp(-a)` over the whole - * row. Unlike `sqrt(1 - sum)`, this has no subtraction: the elements keep - * a small relative error when partial correlations are close to ±1 or a - * row is long, and the diagonal stays positive unless `a` exceeds about - * 745. + * @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 n = (K * (K - 1)) / 2; + return make_holder( + [](const auto& i) { + const auto r = (0.5 * (1.0 + (1.0 + 8.0 * i).sqrt())).floor(); + return (r + 1.0 - (i - 0.5 * r * (r - 1.0))).matrix(); + }, + Eigen::ArrayXd::LinSpaced(n, 0.0, n - 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 `c` is `K - c` (equation 11 of Lewandowski, Kurowicka + * and Joe, 2009), with `c` recovered from the position by inverting + * `cholesky_corr_position`. * - * @tparam ColumnOrder order of the partial correlations in `y`, see - * `cholesky_corr_position` + * @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 n = (K * (K - 1)) / 2; + const double b = 2.0 * K - 1.0; + return make_holder( + [K = static_cast(K), b](const auto& i) { + return (K - (0.5 * (b - (b * b - 8.0 * i).sqrt())).floor()).matrix(); + }, + Eigen::ArrayXd::LinSpaced(n, 0.0, n - 1.0)); +} + +/** + * Return the Cholesky factor of a correlation matrix from unconstrained + * values `y`, whose partial correlations are `tanh(y)`. Each element is + * `L(r, c) = tanh(y_c) exp(-a)` with `a` 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 * = nullptr> +template * = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> -cholesky_corr_factor(const EigVec& y, Eigen::Index K) { +cholesky_corr_factor(EigVec&& y, LogCoshVec&& log_cosh_y, Eigen::Index K) { using T_scalar = value_type_t; Eigen::Matrix L = Eigen::Matrix::Zero(K, K); if (K == 0) { return L; } - const auto& y_ref = to_ref(y); + decltype(auto) y_ref = to_ref(std::forward(y)); + decltype(auto) log_cosh_y_ref = to_ref(std::forward(log_cosh_y)); L.coeffRef(0, 0) = 1; for (Eigen::Index r = 1; r < K; ++r) { T_scalar a = 0; for (Eigen::Index c = 0; c < r; ++c) { - const auto& y_rc - = y_ref.coeff(cholesky_corr_position(r, c, K)); - L.coeffRef(r, c) = tanh(y_rc) * exp(-a); - a += log_cosh(y_rc); + const Eigen::Index k = cholesky_corr_position(r, c, K); + L.coeffRef(r, c) = tanh(y_ref.coeff(k)) * exp(-a); + a += log_cosh_y_ref.coeff(k); } L.coeffRef(r, r) = exp(-a); } @@ -149,11 +137,13 @@ cholesky_corr_factor(const EigVec& y, Eigen::Index K) { */ template * = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> -cholesky_corr_constrain(const EigVec& y, int K) { +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); - return internal::cholesky_corr_factor(y, K); + decltype(auto) y_ref = to_ref(std::forward(y)); + return internal::cholesky_corr_factor( + y_ref, log_cosh(y_ref), K); } /** @@ -169,21 +159,19 @@ cholesky_corr_constrain(const EigVec& y, int K) { * @return Cholesky factor of a correlation matrix */ template * = nullptr, + require_eigen_col_vector_t* = nullptr, require_convertible_t, Lp>* = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> -cholesky_corr_constrain(const EigVec& y, int K, Lp& lp) { +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); - if (K == 0) { - return Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic>( - 0, 0); - } - const auto& y_ref = to_ref(y); + decltype(auto) y_ref = to_ref(std::forward(y)); + const auto& log_cosh_y = to_ref(log_cosh(y_ref)); lp -= dot_product(internal::cholesky_corr_log_jacobian_coefficients(K), - log_cosh(y_ref)); - return internal::cholesky_corr_factor(y_ref, K); + log_cosh_y); + return internal::cholesky_corr_factor( + y_ref, log_cosh_y, K); } /** diff --git a/stan/math/prim/constraint/corr_constrain.hpp b/stan/math/prim/constraint/corr_constrain.hpp index dfc424b0a3b..e79be4de43d 100644 --- a/stan/math/prim/constraint/corr_constrain.hpp +++ b/stan/math/prim/constraint/corr_constrain.hpp @@ -5,7 +5,8 @@ #include #include #include -#include +#include +#include namespace stan { namespace math { @@ -38,17 +39,15 @@ inline plain_type_t corr_constrain(T&& x) { *

\f$\log | \frac{d}{dx} \tanh x | = \log (1 - \tanh^2 x) * = -2 \log \cosh x\f$. * - * The difference \f$1 - \tanh^2 x\f$ has no correct digits from - * \f$|x| = 19\f$; `log_cosh` has a small relative error for every `x`. - * * @tparam T_x Type of scalar or container * @param[in] x value or container * @param[in,out] lp log density accumulator */ template inline auto corr_constrain(T_x&& x, T_lp& lp) { - lp -= 2.0 * sum(log_cosh(x)); - return corr_constrain(std::forward(x)); + decltype(auto) x_ref = to_ref(std::forward(x)); + lp -= 2.0 * sum(log_cosh(x_ref)); + return corr_constrain(x_ref); } /** diff --git a/stan/math/prim/constraint/corr_matrix_constrain.hpp b/stan/math/prim/constraint/corr_matrix_constrain.hpp index 2b9c3cae8bd..4cfb4ab3d6c 100644 --- a/stan/math/prim/constraint/corr_matrix_constrain.hpp +++ b/stan/math/prim/constraint/corr_matrix_constrain.hpp @@ -5,44 +5,15 @@ #include #include #include -#include #include #include #include -#include #include -#include +#include namespace stan { namespace math { -namespace internal { -/** - * Return the coefficients `c` for which the log absolute Jacobian - * determinant of `corr_matrix_constrain(x, K)` is - * `-sum_n c_n log(cosh(x_n))`. - * - * The transform is `tanh` to canonical partial correlations, with - * `log(1 - tanh(x)^2) = -2 log(cosh(x))`, then `read_corr_matrix`, whose - * log Jacobian is `(K - j - 2) / 2 log(1 - z^2)` for a partial correlation - * `z` of column `j` (equation 11 of Lewandowski, Kurowicka and Joe, 2009). - * Column `j` holds `K - 1 - j` values, each with the coefficient - * `2 + (K - j - 2) = K - j`. - * - * @param K number of rows of the correlation matrix - * @return coefficients in the order of the unconstrained vector - */ -inline Eigen::VectorXd corr_matrix_log_jacobian_coefficients(Eigen::Index K) { - Eigen::VectorXd c((K * (K - 1)) / 2); - Eigen::Index pos = 0; - for (Eigen::Index j = 0; j < K - 1; ++j) { - c.segment(pos, K - 1 - j).setConstant(K - j); - pos += K - 1 - j; - } - return c; -} -} // namespace internal - /** * Return the correlation matrix of the specified dimensionality * derived from the specified vector of unconstrained values. The @@ -75,11 +46,9 @@ corr_matrix_constrain(T&& x, Eigen::Index k) { decltype(auto) x_ref = to_ref(std::forward(x)); check_size_match("cov_matrix_constrain", "x.size()", x_ref.size(), "k_choose_2", k_choose_2); - if (k == 0) { - return {}; - } return multiply_lower_tri_self_transpose( - internal::cholesky_corr_factor(x_ref, k)); + internal::cholesky_corr_factor( + x_ref, log_cosh(x_ref), k)); } /** @@ -112,13 +81,12 @@ corr_matrix_constrain(T&& x, Eigen::Index k, Lp& lp) { decltype(auto) x_ref = to_ref(std::forward(x)); check_size_match("cov_matrix_constrain", "x.size()", x_ref.size(), "k_choose_2", k_choose_2); - if (k == 0) { - return {}; - } + const auto& log_cosh_x = to_ref(log_cosh(x_ref)); lp -= dot_product(internal::corr_matrix_log_jacobian_coefficients(k), - log_cosh(x_ref)); + log_cosh_x); return multiply_lower_tri_self_transpose( - internal::cholesky_corr_factor(x_ref, k)); + internal::cholesky_corr_factor( + x_ref, log_cosh_x, k)); } /** diff --git a/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp b/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp index 798b930247f..d26a94b8b0f 100644 --- a/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp +++ b/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp @@ -1,52 +1,58 @@ #ifndef STAN_MATH_PRIM_CONSTRAINT_COV_MATRIX_CONSTRAIN_LKJ_HPP #define STAN_MATH_PRIM_CONSTRAINT_COV_MATRIX_CONSTRAIN_LKJ_HPP +#include +#include #include -#include +#include #include -#include +#include +#include +#include +#include +#include +#include #include +#include namespace stan { namespace math { /** - * Return the covariance matrix of the specified dimensionality - * derived from constraining the specified vector of unconstrained - * values. The input vector must be of length \f$k \choose 2 + - * k\f$. The first \f$k \choose 2\f$ values in the input - * represent unconstrained (partial) correlations and the last - * \f$k\f$ are unconstrained standard deviations of the dimensions. - * - *

The transform scales the correlation matrix transform defined - * in corr_matrix_constrain(Matrix, size_t) - * with the constrained deviations. + * Return the `k x k` covariance matrix `(diag(sds) L) (diag(sds) L)^T`, with + * `L` the Cholesky factor of the correlation matrix of the first + * `k choose 2` unconstrained values and `sds = exp(x.tail(k))`. See + * `corr_matrix_constrain` for the correlation transform. * * @tparam T type of the vector (must be derived from \c Eigen::MatrixBase and - * have one compile-time dimension equal to 1) + * have one compile-time column) * @param x Input vector of unconstrained partial correlations and * standard deviations. * @param k Dimensionality of returned covariance matrix. * @return Covariance matrix derived from the unconstrained partial * correlations and deviations. */ -template * = nullptr> +template * = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov_matrix_constrain_lkj(T&& x, size_t k) { size_t k_choose_2 = (k * (k - 1)) / 2; - auto&& x_ref = to_ref(std::forward(x)); - return read_cov_matrix(corr_constrain(x_ref.head(k_choose_2)), - positive_constrain(x_ref.tail(k))); + decltype(auto) x_ref = to_ref(std::forward(x)); + check_size_match("cov_matrix_constrain_lkj", "x.size()", x_ref.size(), + "k_choose_2 + k", k_choose_2 + k); + const auto& cpcs = x_ref.head(k_choose_2); + return multiply_lower_tri_self_transpose(diag_pre_multiply( + to_ref(positive_constrain(x_ref.tail(k))), + internal::cholesky_corr_factor( + cpcs, log_cosh(cpcs), k))); } /** - * Return the covariance matrix of the specified dimensionality - * derived from constraining the specified vector of unconstrained - * values and increment the specified log probability reference - * with the log absolute Jacobian determinant. + * Return the covariance matrix as `cov_matrix_constrain_lkj(x, k)` and add + * the log absolute Jacobian determinant to `lp`. The Jacobian is the one of + * `corr_matrix_constrain` plus `k log 2 + (k + 1) sum(x.tail(k))`. * * @tparam T type of the vector (must be derived from \c Eigen::MatrixBase and - * have one compile-time dimension equal to 1) + * have one compile-time column) * @param x Input vector of unconstrained partial correlations and * standard deviations. * @param k Dimensionality of returned covariance matrix. @@ -54,13 +60,23 @@ cov_matrix_constrain_lkj(T&& x, size_t k) { * @return Covariance matrix derived from the unconstrained partial * correlations and deviations. */ -template * = nullptr> +template * = nullptr> inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> cov_matrix_constrain_lkj(T&& x, size_t k, return_type_t& lp) { size_t k_choose_2 = (k * (k - 1)) / 2; - auto&& x_ref = to_ref(std::forward(x)); - return read_cov_matrix(corr_constrain(x_ref.head(k_choose_2)), - positive_constrain(x_ref.tail(k)), lp); + decltype(auto) x_ref = to_ref(std::forward(x)); + check_size_match("cov_matrix_constrain_lkj", "x.size()", x_ref.size(), + "k_choose_2 + k", k_choose_2 + k); + const auto& cpcs = x_ref.head(k_choose_2); + const auto& log_sds = x_ref.tail(k); + const auto& log_cosh_cpcs = to_ref(log_cosh(cpcs)); + lp -= dot_product(internal::corr_matrix_log_jacobian_coefficients(k), + log_cosh_cpcs); + lp += (k + 1.0) * sum(log_sds) + k * LOG_TWO; + return multiply_lower_tri_self_transpose(diag_pre_multiply( + to_ref(positive_constrain(log_sds)), + internal::cholesky_corr_factor( + cpcs, log_cosh_cpcs, k))); } /** diff --git a/stan/math/prim/fun/log_cosh.hpp b/stan/math/prim/fun/log_cosh.hpp index cbf2f0be941..08c6d676dd0 100644 --- a/stan/math/prim/fun/log_cosh.hpp +++ b/stan/math/prim/fun/log_cosh.hpp @@ -2,8 +2,12 @@ #define STAN_MATH_PRIM_FUN_LOG_COSH_HPP #include +#include +#include +#include +#include +#include #include -#include #include namespace stan { @@ -11,40 +15,40 @@ namespace math { namespace internal { /** - * Return `log(cosh(x))` and its derivative `tanh(x)`. - * - * For `|x| < 1`, with `u = expm1(|x|)`, `cosh(x) - 1 = u^2 / (2 (1 + u))` - * and `tanh(|x|) = u (u + 2) / (u (u + 2) + 2)`. For `|x| >= 1`, with - * `t = expm1(-2 |x|)`, `log(cosh(x)) = |x| + log1p(t / 2)` and - * `tanh(|x|) = -t / (2 + t)`. No branch subtracts two nearly equal numbers - * or overflows, so both results have a small relative error for every `x`. + * `log(cosh(x))`, `tanh(x)` and `sech(x) = 1 / cosh(x)` of one argument. + */ +struct log_cosh_parts { + double log_cosh; + double tanh; + double sech; +}; + +/** + * Return `log(cosh(x))`, its derivative `tanh(x)`, and `sech(x)` from one + * call of `expm1` (`|x| < 1`) or `exp` (`|x| >= 1`). Neither branch + * subtracts two nearly equal numbers or overflows. * * @param x argument - * @return pair of `log(cosh(x))` and `tanh(x)` + * @return `log(cosh(x))`, `tanh(x)` and `sech(x)` */ -inline std::pair log_cosh_and_tanh(double x) { - const double a = std::fabs(x); - double value; - double tanh_a; +inline log_cosh_parts log_cosh_tanh_sech(double x) { + const double a = fabs(x); if (a < 1.0) { - const double u = std::expm1(a); + const double u = expm1(a); + const double d = u * u / (2.0 * (1.0 + u)); const double w = u * (u + 2.0); - value = std::log1p(u * u / (2.0 * (1.0 + u))); - tanh_a = w / (w + 2.0); - } else { - const double t = std::expm1(-2.0 * a); - value = a + std::log1p(0.5 * t); - tanh_a = -t / (2.0 + t); + return {log1p(d), copysign(w / (w + 2.0), x), 1.0 / (1.0 + d)}; } - return {value, std::copysign(tanh_a, x)}; + const double e = exp(-a); + const double e2 = e * e; + return {a + log1p(0.5 * (e2 - 1.0)), copysign((1.0 - e2) / (1.0 + e2), x), + 2.0 * e / (1.0 + e2)}; } } // namespace internal /** - * Return the natural logarithm of the hyperbolic cosine of the argument. - * - * The result does not overflow for large `|x|` and has a small relative - * error for small `|x|`. The derivative is `tanh(x)`. + * Return the natural logarithm of the hyperbolic cosine of the argument, + * without overflow for large `|x|`. The derivative is `tanh(x)`. * * @tparam T arithmetic type * @param x argument @@ -52,7 +56,7 @@ inline std::pair log_cosh_and_tanh(double x) { */ template * = nullptr> inline double log_cosh(T x) { - return internal::log_cosh_and_tanh(static_cast(x)).first; + return internal::log_cosh_tanh_sech(static_cast(x)).log_cosh; } /** @@ -82,8 +86,8 @@ struct log_cosh_fun { template * = nullptr, require_not_var_matrix_t* = nullptr, require_not_nonscalar_prim_or_rev_kernel_expression_t* = nullptr> -inline auto log_cosh(const T& x) { - return apply_scalar_unary::apply(x); +inline auto log_cosh(T&& x) { + return apply_scalar_unary::apply(std::forward(x)); } } // namespace math diff --git a/stan/math/rev/constraint/cholesky_corr_constrain.hpp b/stan/math/rev/constraint/cholesky_corr_constrain.hpp index dab6933210b..d6b5d4f955c 100644 --- a/stan/math/rev/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/rev/constraint/cholesky_corr_constrain.hpp @@ -4,11 +4,11 @@ #include #include #include +#include #include #include #include #include -#include #include namespace stan { @@ -17,29 +17,24 @@ namespace math { namespace internal { /** * Return the Cholesky factor of a correlation matrix from unconstrained - * values `y`, whose partial correlations are `tanh(y)`. See the `prim` - * version for the formulas. + * values `y`, whose partial correlations are `tanh(y)`, as the `prim` + * version does. The reverse pass propagates each adjoint to its own `y` and, + * through the shared `exp(-a)`, to every earlier `y` of the row. * - * Reverse pass: in row `r`, element `c` depends on its own `y` through - * `tanh(y) exp(-a)`, with derivative `exp(-2 log(cosh(y)) - a)`, and every - * later element of the row, the diagonal included, depends on it through - * `exp(-a)`, with derivative `-tanh(y)` times the element. - * - * @tparam ColumnOrder order of the partial correlations in `y`, see - * `cholesky_corr_position` + * @tparam Order order of the partial correlations in `y` * @tparam T type of the `var_value` vector * @param y unconstrained values, `K (K - 1) / 2` of them * @param K number of rows * @return Cholesky factor of a correlation matrix */ -template * = nullptr> -inline var_value cholesky_corr_factor(const T& y, - Eigen::Index K) { +template * = nullptr> +inline var_value cholesky_corr_factor(T&& y, Eigen::Index K) { using Eigen::Index; if (K == 0) { return Eigen::MatrixXd(); } - const Index n = y.size(); + auto arena_y = to_arena(std::forward(y)); + const Index n = arena_y.size(); arena_t tanh_y(n); // derivative of each element with respect to its own unconstrained value arena_t d_own(n); @@ -49,23 +44,23 @@ inline var_value cholesky_corr_factor(const T& y, // exp(-a), as the product of sech(y) over the earlier elements of the row double exp_minus_a = 1.0; for (Index c = 0; c < r; ++c) { - const Index k = cholesky_corr_position(r, c, K); - const std::pair t_s = tanh_and_sech(y.val().coeff(k)); - tanh_y.coeffRef(k) = t_s.first; - d_own.coeffRef(k) = t_s.second * t_s.second * exp_minus_a; - L_val.coeffRef(r, c) = t_s.first * exp_minus_a; - exp_minus_a *= t_s.second; + const Index k = cholesky_corr_position(r, c, K); + const log_cosh_parts parts = log_cosh_tanh_sech(arena_y.val().coeff(k)); + tanh_y.coeffRef(k) = parts.tanh; + d_own.coeffRef(k) = parts.sech * parts.sech * exp_minus_a; + L_val.coeffRef(r, c) = parts.tanh * exp_minus_a; + exp_minus_a *= parts.sech; } L_val.coeffRef(r, r) = exp_minus_a; } var_value L = L_val; - reverse_pass_callback([y, L, tanh_y, d_own, K]() mutable { + reverse_pass_callback([arena_y, L, tanh_y, d_own, K]() mutable { for (Index r = 1; r < K; ++r) { double later = L.adj().coeff(r, r) * L.val().coeff(r, r); for (Index c = r - 1; c >= 0; --c) { - const Index k = cholesky_corr_position(r, c, K); + const Index k = cholesky_corr_position(r, c, K); const double adj_rc = L.adj().coeff(r, c); - y.adj().coeffRef(k) + arena_y.adj().coeffRef(k) += adj_rc * d_own.coeff(k) - tanh_y.coeff(k) * later; later += adj_rc * L.val().coeff(r, c); } @@ -87,11 +82,12 @@ inline var_value cholesky_corr_factor(const T& y, * @return Cholesky factor of correlation matrix */ template * = nullptr> -inline var_value cholesky_corr_constrain(const T& y, int K) { +inline var_value cholesky_corr_constrain(T&& y, int K) { int k_choose_2 = (K * (K - 1)) / 2; check_size_match("cholesky_corr_constrain", "y.size()", y.size(), "k_choose_2", k_choose_2); - return internal::cholesky_corr_factor(y, K); + return internal::cholesky_corr_factor( + std::forward(y), K); } /** @@ -108,16 +104,14 @@ inline var_value cholesky_corr_constrain(const T& y, int K) { */ template * = nullptr> inline var_value cholesky_corr_constrain( - const T& y, int K, scalar_type_t& lp) { + T&& y, int K, scalar_type_t& 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); - if (K == 0) { - return Eigen::MatrixXd(); - } lp -= dot_product(internal::cholesky_corr_log_jacobian_coefficients(K), log_cosh(y)); - return internal::cholesky_corr_factor(y, K); + return internal::cholesky_corr_factor( + std::forward(y), K); } } // namespace math diff --git a/stan/math/rev/constraint/corr_matrix_constrain.hpp b/stan/math/rev/constraint/corr_matrix_constrain.hpp index d29898cabe3..b4f63a038c6 100644 --- a/stan/math/rev/constraint/corr_matrix_constrain.hpp +++ b/stan/math/rev/constraint/corr_matrix_constrain.hpp @@ -9,10 +9,8 @@ #include #include #include -#include #include -#include -#include +#include namespace stan { namespace math { @@ -43,16 +41,13 @@ namespace math { * matrix. */ template * = nullptr> -inline var_value corr_matrix_constrain(const T& x, - Eigen::Index k) { +inline var_value corr_matrix_constrain(T&& x, Eigen::Index k) { Eigen::Index k_choose_2 = (k * (k - 1)) / 2; check_size_match("cov_matrix_constrain", "x.size()", x.size(), "k_choose_2", k_choose_2); - if (k == 0) { - return Eigen::MatrixXd(); - } return multiply_lower_tri_self_transpose( - internal::cholesky_corr_factor(x, k)); + internal::cholesky_corr_factor( + std::forward(x), k)); } /** @@ -76,19 +71,16 @@ inline var_value corr_matrix_constrain(const T& x, * @param lp Log probability reference to increment. */ template * = nullptr> -inline var_value corr_matrix_constrain(const T& x, - Eigen::Index k, +inline var_value corr_matrix_constrain(T&& x, Eigen::Index k, scalar_type_t& lp) { Eigen::Index k_choose_2 = (k * (k - 1)) / 2; check_size_match("cov_matrix_constrain", "x.size()", x.size(), "k_choose_2", k_choose_2); - if (k == 0) { - return Eigen::MatrixXd(); - } lp -= dot_product(internal::corr_matrix_log_jacobian_coefficients(k), log_cosh(x)); return multiply_lower_tri_self_transpose( - internal::cholesky_corr_factor(x, k)); + internal::cholesky_corr_factor( + std::forward(x), k)); } } // namespace math diff --git a/stan/math/rev/constraint/cov_matrix_constrain_lkj.hpp b/stan/math/rev/constraint/cov_matrix_constrain_lkj.hpp index 115a396cf6c..8ceddd2e7e5 100644 --- a/stan/math/rev/constraint/cov_matrix_constrain_lkj.hpp +++ b/stan/math/rev/constraint/cov_matrix_constrain_lkj.hpp @@ -1,27 +1,29 @@ #ifndef STAN_MATH_REV_CONSTRAINT_COV_MATRIX_CONSTRAIN_LKJ_HPP #define STAN_MATH_REV_CONSTRAINT_COV_MATRIX_CONSTRAIN_LKJ_HPP -#include #include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include -#include -#include +#include +#include namespace stan { namespace math { /** - * Return the covariance matrix of the specified dimensionality - * derived from constraining the specified vector of unconstrained - * values. The input vector must be of length \f$k \choose 2 + - * k\f$. The first \f$k \choose 2\f$ values in the input - * represent unconstrained (partial) correlations and the last - * \f$k\f$ are unconstrained standard deviations of the dimensions. - * - *

The transform scales the correlation matrix transform defined - * in corr_matrix_constrain(Matrix, size_t) - * with the constrained deviations. + * Return the `k x k` covariance matrix `(diag(sds) L) (diag(sds) L)^T`, with + * `L` the Cholesky factor of the correlation matrix of the first + * `k choose 2` unconstrained values and `sds = exp(x.tail(k))`. See + * `corr_matrix_constrain` for the correlation transform. * * @tparam T type of input vector (must be a `var_value` where `S` * inherits from EigenBase) @@ -32,28 +34,21 @@ namespace math { * correlations and deviations. */ template * = nullptr> -inline var_value cov_matrix_constrain_lkj(const T& x, - size_t k) { +inline var_value cov_matrix_constrain_lkj(T&& x, size_t k) { size_t k_choose_2 = (k * (k - 1)) / 2; - return read_cov_matrix(corr_constrain(x.head(k_choose_2)), - positive_constrain(x.tail(k))); + check_size_match("cov_matrix_constrain_lkj", "x.size()", x.size(), + "k_choose_2 + k", k_choose_2 + k); + auto arena_x = to_arena(std::forward(x)); + return multiply_lower_tri_self_transpose(diag_pre_multiply( + positive_constrain(arena_x.tail(k)), + internal::cholesky_corr_factor( + arena_x.head(k_choose_2), k))); } /** - * Return the covariance matrix of the specified dimensionality - * derived from constraining the specified vector of unconstrained - * values and increment the specified log probability reference - * with the log absolute Jacobian determinant. - * - *

The transform is defined as for - * cov_matrix_constrain(Matrix, size_t). - * - *

The log absolute Jacobian determinant is derived by - * composing the log absolute Jacobian determinant for the - * underlying correlation matrix as defined in - * cov_matrix_constrain(Matrix, size_t, T&) with - * the Jacobian of the transform of the correlation matrix - * into a covariance matrix by scaling by standard deviations. + * Return the covariance matrix as `cov_matrix_constrain_lkj(x, k)` and add + * the log absolute Jacobian determinant to `lp`. The Jacobian is the one of + * `corr_matrix_constrain` plus `k log 2 + (k + 1) sum(x.tail(k))`. * * @tparam T type of input vector (must be a `var_value` where `S` * inherits from EigenBase) @@ -66,10 +61,19 @@ inline var_value cov_matrix_constrain_lkj(const T& x, */ template * = nullptr> inline var_value cov_matrix_constrain_lkj( - const T& x, size_t k, scalar_type_t& lp) { + T&& x, size_t k, scalar_type_t& lp) { size_t k_choose_2 = (k * (k - 1)) / 2; - return read_cov_matrix(corr_constrain(x.head(k_choose_2)), - positive_constrain(x.tail(k)), lp); + check_size_match("cov_matrix_constrain_lkj", "x.size()", x.size(), + "k_choose_2 + k", k_choose_2 + k); + auto arena_x = to_arena(std::forward(x)); + const auto& cpcs = arena_x.head(k_choose_2); + const auto& log_sds = arena_x.tail(k); + lp -= dot_product(internal::corr_matrix_log_jacobian_coefficients(k), + log_cosh(cpcs)); + lp += (k + 1.0) * sum(log_sds) + k * LOG_TWO; + return multiply_lower_tri_self_transpose(diag_pre_multiply( + positive_constrain(log_sds), + internal::cholesky_corr_factor(cpcs, k))); } } // namespace math diff --git a/stan/math/rev/fun/log_cosh.hpp b/stan/math/rev/fun/log_cosh.hpp index d3b820656c1..d1f43f0347b 100644 --- a/stan/math/rev/fun/log_cosh.hpp +++ b/stan/math/rev/fun/log_cosh.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -18,10 +19,9 @@ namespace math { */ template * = nullptr> inline var log_cosh(const var_value& x) { - const std::pair value_and_tanh - = internal::log_cosh_and_tanh(x.val()); - const double tanh_x = value_and_tanh.second; - return make_callback_var(value_and_tanh.first, [x, tanh_x](auto& vi) mutable { + const internal::log_cosh_parts parts = internal::log_cosh_tanh_sech(x.val()); + const double tanh_x = parts.tanh; + return make_callback_var(parts.log_cosh, [x, tanh_x](auto& vi) mutable { x.adj() += vi.adj() * tanh_x; }); } @@ -30,24 +30,26 @@ inline var log_cosh(const var_value& x) { * Return the elementwise natural logarithm of the hyperbolic cosine of the * argument. * - * @tparam T a type derived from `Eigen::EigenBase` + * @tparam T `var_value` with inner Eigen type * @param x `var_value` with inner Eigen type * @return elementwise `log(cosh(x))`, with derivative `tanh(x)` */ -template * = nullptr> -inline auto log_cosh(const var_value& x) { - using val_t = plain_type_t; - val_t value(x.rows(), x.cols()); - arena_t tanh_x(x.rows(), x.cols()); - for (Eigen::Index i = 0; i < x.size(); ++i) { - const std::pair value_and_tanh - = internal::log_cosh_and_tanh(x.val().coeff(i)); - value.coeffRef(i) = value_and_tanh.first; - tanh_x.coeffRef(i) = value_and_tanh.second; +template * = nullptr> +inline auto log_cosh(T&& x) { + using val_t = plain_type_t>; + auto arena_x = to_arena(std::forward(x)); + val_t value(arena_x.rows(), arena_x.cols()); + arena_t tanh_x(arena_x.rows(), arena_x.cols()); + for (Eigen::Index i = 0; i < arena_x.size(); ++i) { + const internal::log_cosh_parts parts + = internal::log_cosh_tanh_sech(arena_x.val().coeff(i)); + value.coeffRef(i) = parts.log_cosh; + tanh_x.coeffRef(i) = parts.tanh; } - return make_callback_var(std::move(value), [x, tanh_x](auto& vi) mutable { - x.adj().array() += vi.adj().array() * tanh_x.array(); - }); + return make_callback_var( + std::move(value), [arena_x, tanh_x](auto& vi) mutable { + arena_x.adj().array() += vi.adj().array() * tanh_x.array(); + }); } } // namespace math diff --git a/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp b/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp index 7618098e847..f804d900b60 100644 --- a/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp +++ b/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include namespace cholesky_corr_constrain_test { @@ -116,138 +116,72 @@ TEST(mathMixMatFun, cholesky_corr_constrain_lp) { stan::test::expect_ad_matvar(f2(3), x2); } -namespace cholesky_corr_constrain_test { -// the strictly lower triangular elements of the Cholesky factor -struct strict_lower_of_constrained { - int K; - template - Eigen::Matrix operator()( - const Eigen::Matrix& y) const { - auto L = stan::math::cholesky_corr_constrain(y, K); - Eigen::Matrix v((K * (K - 1)) / 2); - int k = 0; - for (int j = 0; j < K; ++j) { - for (int i = j + 1; i < K; ++i) { - v(k++) = L(i, j); +// The lazy coefficient expressions recover the row or column of each +// position in closed form; check them against the formulas r + 1 - c (row +// order) and K - c (column order) for every element of several sizes. +TEST(mathMixMatFun, corr_log_jacobian_coefficients) { + using stan::math::internal::cholesky_corr_log_jacobian_coefficients; + using stan::math::internal::cholesky_corr_position; + using stan::math::internal::corr_matrix_log_jacobian_coefficients; + using stan::math::internal::CorrOrder; + for (int K : {0, 1, 2, 3, 4, 7, 40, 200}) { + Eigen::VectorXd row_order = cholesky_corr_log_jacobian_coefficients(K); + Eigen::VectorXd column_order = corr_matrix_log_jacobian_coefficients(K); + ASSERT_EQ((K * (K - 1)) / 2, row_order.size()) << "K = " << K; + ASSERT_EQ((K * (K - 1)) / 2, column_order.size()) << "K = " << K; + for (int r = 1; r < K; ++r) { + for (int c = 0; c < r; ++c) { + EXPECT_EQ(r + 1 - c, + row_order(cholesky_corr_position(r, c, K))) + << "K = " << K << ", r = " << r << ", c = " << c; + EXPECT_EQ(K - c, column_order(cholesky_corr_position( + r, c, K))) + << "K = " << K << ", r = " << r << ", c = " << c; } } - return v; } -}; -} // namespace cholesky_corr_constrain_test +} // lp is log |det J| of the map from y to the strictly lower triangular // elements of the Cholesky factor. TEST(mathMixMatFun, cholesky_corr_constrain_lp_is_log_det_jacobian) { Eigen::VectorXd y(6); y << 0.3, -1.2, 0.8, 2.1, -0.4, 1.5; - Eigen::VectorXd fx; - Eigen::MatrixXd J; - stan::math::jacobian( - cholesky_corr_constrain_test::strict_lower_of_constrained{4}, y, fx, J); - double lp = 0; - stan::math::cholesky_corr_constrain(y, 4, lp); - EXPECT_NEAR(std::log(std::fabs(J.determinant())), lp, 1e-12); + corr_transform_test::expect_lp_is_log_det_jacobian( + [](const auto& y) { return stan::math::cholesky_corr_constrain(y, 4); }, + [](const auto& y, auto& lp) { + stan::math::cholesky_corr_constrain(y, 4, lp); + }, + y, true, 1e-12); } // lp = sum_n w_n log(1 - tanh(y_n)^2) with weights (1, 1.5, 1) for K = 3, // and its gradient, at large y against mpmath. Forming 1 - tanh(y)^2 and // 1 - sum of squares loses digits from |y| = 8. TEST(mathMixMatFun, cholesky_corr_constrain_lp_large_argument) { - using stan::math::fvar; - using stan::math::var; Eigen::VectorXd y(3); y << 15.0, 12.0, -0.5; - const double lp_ref = -62.77449311123027; Eigen::VectorXd grad_ref(3); grad_ref << -1.9999999999996257, -2.9999999997734919, 0.92423431452001952; - - double lp_d = 0; - stan::math::cholesky_corr_constrain(y, 3, lp_d); - EXPECT_NEAR(lp_ref, lp_d, 1e-13); - - Eigen::Matrix y_v = stan::math::to_var(y); - var lp_v = 0; - stan::math::cholesky_corr_constrain(y_v, 3, lp_v); - lp_v.grad(); - EXPECT_NEAR(lp_ref, lp_v.val(), 1e-13); - for (int i = 0; i < 3; ++i) { - EXPECT_NEAR(grad_ref(i), y_v(i).adj(), 1e-14); - } - stan::math::recover_memory(); - - stan::math::var_value y_m(y); - var lp_m = 0; - stan::math::cholesky_corr_constrain(y_m, 3, lp_m); - lp_m.grad(); - EXPECT_NEAR(lp_ref, lp_m.val(), 1e-13); - for (int i = 0; i < 3; ++i) { - EXPECT_NEAR(grad_ref(i), y_m.adj()(i), 1e-14); - } - stan::math::recover_memory(); - - for (int n = 0; n < 3; ++n) { - Eigen::Matrix, Eigen::Dynamic, 1> y_f(3); - for (int i = 0; i < 3; ++i) { - y_f(i) = fvar(y(i), i == n ? 1.0 : 0.0); - } - fvar lp_f = 0; - stan::math::cholesky_corr_constrain(y_f, 3, lp_f); - EXPECT_NEAR(lp_ref, lp_f.val_, 1e-13); - EXPECT_NEAR(grad_ref(n), lp_f.d_, 1e-14); - } -} - -namespace cholesky_corr_constrain_test { -// lower triangle (diagonal included), row by row -template -Eigen::VectorXd lower_triangle(const M& L) { - const int K = L.rows(); - Eigen::VectorXd v((K * (K + 1)) / 2); - int k = 0; - for (int i = 0; i < K; ++i) { - for (int j = 0; j <= i; ++j) { - v(k++) = stan::math::value_of_rec(L(i, j)); - } - } - return v; + corr_transform_test::expect_lp_all_modes( + [](const auto& y, auto& lp) { + stan::math::cholesky_corr_constrain(y, 3, lp); + }, + y, -62.77449311123027, grad_ref, 1e-13, 1e-14); } -} // namespace cholesky_corr_constrain_test // Values of L at large |y| against mpmath. sqrt(1 - sum of squares) had a // relative error of 3.8e-4 in L(1, 1) at y = 15. TEST(mathMixMatFun, cholesky_corr_constrain_values_large_argument) { - using cholesky_corr_constrain_test::lower_triangle; - using stan::math::fvar; - using stan::math::var; Eigen::VectorXd y(6); y << 15.0, 12.0, -0.5, 9.0, 2.0, -14.0; Eigen::VectorXd ref(10); ref << 1.0, 0.99999999999981285, 6.1180464100359433e-7, 0.99999999992449731, -5.6786918924293556e-6, 1.0897607083695929e-5, 0.99999996954004097, 0.00023794090595878951, -6.5605200973176475e-5, 1.091052174635906e-10; - auto expect_ref = [&ref](const Eigen::VectorXd& v, const char* mode) { - for (int i = 0; i < ref.size(); ++i) { - EXPECT_NEAR(ref(i), v(i), 4e-15 * std::fabs(ref(i))) - << mode << ", element " << i; - } - }; - expect_ref(lower_triangle(stan::math::cholesky_corr_constrain(y, 4)), - "double"); - Eigen::Matrix y_v = stan::math::to_var(y); - expect_ref(lower_triangle(stan::math::cholesky_corr_constrain(y_v, 4)), - "var"); - stan::math::var_value y_m(y); - expect_ref( - lower_triangle(stan::math::cholesky_corr_constrain(y_m, 4).val().eval()), - "var_value"); - Eigen::Matrix, Eigen::Dynamic, 1> y_f(6); - for (int i = 0; i < 6; ++i) { - y_f(i) = fvar(y(i), 1.0); - } - expect_ref(lower_triangle(stan::math::cholesky_corr_constrain(y_f, 4)), - "fvar"); - stan::math::recover_memory(); + corr_transform_test::expect_values_all_modes( + [](const auto& y) { return stan::math::cholesky_corr_constrain(y, 4); }, + y, false, ref, 0.0, 4e-15); } // K = 40 with unconstrained values in [-1.9, 1.9], as default inits: a row diff --git a/test/unit/math/mix/constraint/corr_constrain_test.cpp b/test/unit/math/mix/constraint/corr_constrain_test.cpp index 73f19c0bbd1..88c719bf6d3 100644 --- a/test/unit/math/mix/constraint/corr_constrain_test.cpp +++ b/test/unit/math/mix/constraint/corr_constrain_test.cpp @@ -1,50 +1,15 @@ -#include +#include // log(1 - tanh(x)^2) and its derivative -2 tanh(x) at large |x|, against // mpmath. Forming 1 - tanh(x)^2 loses all digits from |x| = 19.06. TEST(mathMixMatFun, corr_constrain_lp_large_argument) { - using stan::math::fvar; - using stan::math::var; Eigen::VectorXd x(3); x << 15.0, -19.5, 30.0; - const double lp_ref = -124.84111691664052; Eigen::VectorXd grad_ref(3); grad_ref << -1.9999999999996257, 2.0, -2.0; - - double lp_d = 0; - stan::math::corr_constrain(x, lp_d); - EXPECT_NEAR(lp_ref, lp_d, 1e-13); - - Eigen::Matrix x_v = stan::math::to_var(x); - var lp_v = 0; - stan::math::corr_constrain(x_v, lp_v); - lp_v.grad(); - EXPECT_NEAR(lp_ref, lp_v.val(), 1e-13); - for (int i = 0; i < 3; ++i) { - EXPECT_NEAR(grad_ref(i), x_v(i).adj(), 1e-14); - } - stan::math::recover_memory(); - - stan::math::var_value x_m(x); - var lp_m = 0; - stan::math::corr_constrain(x_m, lp_m); - lp_m.grad(); - EXPECT_NEAR(lp_ref, lp_m.val(), 1e-13); - for (int i = 0; i < 3; ++i) { - EXPECT_NEAR(grad_ref(i), x_m.adj()(i), 1e-14); - } - stan::math::recover_memory(); - - for (int n = 0; n < 3; ++n) { - Eigen::Matrix, Eigen::Dynamic, 1> x_f(3); - for (int i = 0; i < 3; ++i) { - x_f(i) = fvar(x(i), i == n ? 1.0 : 0.0); - } - fvar lp_f = 0; - stan::math::corr_constrain(x_f, lp_f); - EXPECT_NEAR(lp_ref, lp_f.val_, 1e-13); - EXPECT_NEAR(grad_ref(n), lp_f.d_, 1e-14); - } + corr_transform_test::expect_lp_all_modes( + [](const auto& x, auto& lp) { stan::math::corr_constrain(x, lp); }, x, + -124.84111691664052, grad_ref, 1e-13, 1e-14); } TEST(mathMixMatFun, corr_constrain) { diff --git a/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp b/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp index b0539788946..e1b80c27c0b 100644 --- a/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp +++ b/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include namespace corr_matrix_constrain_test { @@ -119,134 +119,46 @@ TEST(mathMixMatFun, corr_matrix_constrain_lp) { stan::test::expect_ad_matvar(f2(3), x2); } -namespace corr_matrix_constrain_test { -// the strictly lower triangular elements of the correlation matrix -struct strict_lower_of_constrained { - int K; - template - Eigen::Matrix operator()( - const Eigen::Matrix& y) const { - auto Omega = stan::math::corr_matrix_constrain(y, K); - Eigen::Matrix v((K * (K - 1)) / 2); - int k = 0; - for (int j = 0; j < K; ++j) { - for (int i = j + 1; i < K; ++i) { - v(k++) = Omega(i, j); - } - } - return v; - } -}; -} // namespace corr_matrix_constrain_test - // lp is log |det J| of the map from y to the strictly lower triangular // elements of the correlation matrix. TEST(mathMixMatFun, corr_matrix_constrain_lp_is_log_det_jacobian) { Eigen::VectorXd y(6); y << 0.3, -1.2, 0.8, 2.1, -0.4, 1.5; - Eigen::VectorXd fx; - Eigen::MatrixXd J; - stan::math::jacobian( - corr_matrix_constrain_test::strict_lower_of_constrained{4}, y, fx, J); - double lp = 0; - stan::math::corr_matrix_constrain(y, 4, lp); - EXPECT_NEAR(std::log(std::fabs(J.determinant())), lp, 1e-12); + corr_transform_test::expect_lp_is_log_det_jacobian( + [](const auto& y) { return stan::math::corr_matrix_constrain(y, 4); }, + [](const auto& y, auto& lp) { + stan::math::corr_matrix_constrain(y, 4, lp); + }, + y, true, 1e-12); } // lp = sum_n w_n log(1 - tanh(y_n)^2) with weights (1.5, 1.5, 1) for K = 3, // and its gradient, at large y against mpmath. Forming 1 - tanh(y)^2 loses // digits from |y| = 8. TEST(mathMixMatFun, corr_matrix_constrain_lp_large_argument) { - using stan::math::fvar; - using stan::math::var; Eigen::VectorXd y(3); y << 15.0, 12.0, -0.5; - const double lp_ref = -77.081345930670418; Eigen::VectorXd grad_ref(3); grad_ref << -2.9999999999994385, -2.9999999997734919, 0.92423431452001952; - - double lp_d = 0; - stan::math::corr_matrix_constrain(y, 3, lp_d); - EXPECT_NEAR(lp_ref, lp_d, 1e-13); - - Eigen::Matrix y_v = stan::math::to_var(y); - var lp_v = 0; - stan::math::corr_matrix_constrain(y_v, 3, lp_v); - lp_v.grad(); - EXPECT_NEAR(lp_ref, lp_v.val(), 1e-13); - for (int i = 0; i < 3; ++i) { - EXPECT_NEAR(grad_ref(i), y_v(i).adj(), 1e-14); - } - stan::math::recover_memory(); - - stan::math::var_value y_m(y); - var lp_m = 0; - stan::math::corr_matrix_constrain(y_m, 3, lp_m); - lp_m.grad(); - EXPECT_NEAR(lp_ref, lp_m.val(), 1e-13); - for (int i = 0; i < 3; ++i) { - EXPECT_NEAR(grad_ref(i), y_m.adj()(i), 1e-14); - } - stan::math::recover_memory(); - - for (int n = 0; n < 3; ++n) { - Eigen::Matrix, Eigen::Dynamic, 1> y_f(3); - for (int i = 0; i < 3; ++i) { - y_f(i) = fvar(y(i), i == n ? 1.0 : 0.0); - } - fvar lp_f = 0; - stan::math::corr_matrix_constrain(y_f, 3, lp_f); - EXPECT_NEAR(lp_ref, lp_f.val_, 1e-13); - EXPECT_NEAR(grad_ref(n), lp_f.d_, 1e-14); - } + corr_transform_test::expect_lp_all_modes( + [](const auto& y, auto& lp) { + stan::math::corr_matrix_constrain(y, 3, lp); + }, + y, -77.081345930670418, grad_ref, 1e-13, 1e-14); } -namespace corr_matrix_constrain_test { -// strictly lower triangle, row by row -template -Eigen::VectorXd strict_lower(const M& Omega) { - const int K = Omega.rows(); - Eigen::VectorXd v((K * (K - 1)) / 2); - int k = 0; - for (int i = 0; i < K; ++i) { - for (int j = 0; j < i; ++j) { - v(k++) = stan::math::value_of_rec(Omega(i, j)); - } - } - return v; -} -} // namespace corr_matrix_constrain_test - // Correlations at large |y| against mpmath. With sqrt(1 - z^2) from a // rounded z, the small elements of the Cholesky factor had relative errors // up to 3.8e-4, which reached the correlations close to 1. TEST(mathMixMatFun, corr_matrix_constrain_values_large_argument) { - using corr_matrix_constrain_test::strict_lower; - using stan::math::fvar; - using stan::math::var; Eigen::VectorXd y(6); y << 15.0, 12.0, -0.5, 9.0, 2.0, -14.0; Eigen::VectorXd ref(6); ref << 0.99999999999981285, 0.99999999992449731, 0.99999999993182827, -0.46211715726000976, -0.46211663421720721, -0.46210665234659286; - auto expect_ref = [&ref](const Eigen::VectorXd& v, const char* mode) { - for (int i = 0; i < ref.size(); ++i) { - EXPECT_NEAR(ref(i), v(i), 4e-16) << mode << ", element " << i; - } - }; - expect_ref(strict_lower(stan::math::corr_matrix_constrain(y, 4)), "double"); - Eigen::Matrix y_v = stan::math::to_var(y); - expect_ref(strict_lower(stan::math::corr_matrix_constrain(y_v, 4)), "var"); - stan::math::var_value y_m(y); - expect_ref( - strict_lower(stan::math::corr_matrix_constrain(y_m, 4).val().eval()), - "var_value"); - Eigen::Matrix, Eigen::Dynamic, 1> y_f(6); - for (int i = 0; i < 6; ++i) { - y_f(i) = fvar(y(i), 1.0); - } - expect_ref(strict_lower(stan::math::corr_matrix_constrain(y_f, 4)), "fvar"); - stan::math::recover_memory(); + corr_transform_test::expect_values_all_modes( + [](const auto& y) { return stan::math::corr_matrix_constrain(y, 4); }, y, + true, ref, 4e-16, 0.0); } // K = 40 with unconstrained values in [-1.9, 1.9], as default inits: the diff --git a/test/unit/math/mix/constraint/corr_transform_helpers.hpp b/test/unit/math/mix/constraint/corr_transform_helpers.hpp new file mode 100644 index 00000000000..71b63e458c5 --- /dev/null +++ b/test/unit/math/mix/constraint/corr_transform_helpers.hpp @@ -0,0 +1,146 @@ +#ifndef STAN_TEST_UNIT_MATH_MIX_CONSTRAINT_CORR_TRANSFORM_HELPERS_HPP +#define STAN_TEST_UNIT_MATH_MIX_CONSTRAINT_CORR_TRANSFORM_HELPERS_HPP + +#include +#include + +namespace corr_transform_test { + +/** + * Return the lower triangle of a matrix, row by row, without (`strict`) or + * with the diagonal. + */ +template +Eigen::Matrix, Eigen::Dynamic, 1> lower_triangle( + const M& m, bool strict) { + const Eigen::Index K = m.rows(); + Eigen::Matrix, Eigen::Dynamic, 1> v( + strict ? (K * (K - 1)) / 2 : (K * (K + 1)) / 2); + Eigen::Index k = 0; + for (Eigen::Index i = 0; i < K; ++i) { + for (Eigen::Index j = 0; j < (strict ? i : i + 1); ++j) { + v(k++) = m(i, j); + } + } + return v; +} + +/** + * Functor for `stan::math::jacobian`: the lower triangle of `constrain(y)`. + */ +template +struct lower_triangle_of { + F constrain; + bool strict; + template + Eigen::Matrix operator()( + const Eigen::Matrix& y) const { + return lower_triangle(constrain(y), strict); + } +}; + +/** + * Expect `lp` from `constrain_lp(y, lp)` to be the log absolute determinant + * of the Jacobian of the map from `y` to the lower triangle of + * `constrain(y)`. + */ +template +void expect_lp_is_log_det_jacobian(F constrain, G constrain_lp, + const Eigen::VectorXd& y, bool strict, + double tol) { + Eigen::VectorXd fx; + Eigen::MatrixXd J; + stan::math::jacobian(lower_triangle_of{constrain, strict}, y, fx, J); + double lp = 0; + constrain_lp(y, lp); + EXPECT_NEAR(std::log(std::fabs(J.determinant())), lp, tol); +} + +/** + * Expect `lp` from `constrain_lp(y, lp)` and its gradient to match the + * references in double, `Eigen::Matrix`, + * `var_value` and `fvar` (one direction at a time). + */ +template +void expect_lp_all_modes(G constrain_lp, const Eigen::VectorXd& y, + double lp_ref, const Eigen::VectorXd& grad_ref, + double tol_lp, double tol_grad) { + using stan::math::fvar; + using stan::math::var; + const Eigen::Index n = y.size(); + + double lp_d = 0; + constrain_lp(y, lp_d); + EXPECT_NEAR(lp_ref, lp_d, tol_lp) << "double"; + + Eigen::Matrix y_v = stan::math::to_var(y); + var lp_v = 0; + constrain_lp(y_v, lp_v); + lp_v.grad(); + EXPECT_NEAR(lp_ref, lp_v.val(), tol_lp) << "var"; + for (Eigen::Index i = 0; i < n; ++i) { + EXPECT_NEAR(grad_ref(i), y_v(i).adj(), tol_grad) << "var, element " << i; + } + stan::math::recover_memory(); + + stan::math::var_value y_m(y); + var lp_m = 0; + constrain_lp(y_m, lp_m); + lp_m.grad(); + EXPECT_NEAR(lp_ref, lp_m.val(), tol_lp) << "var_value"; + for (Eigen::Index i = 0; i < n; ++i) { + EXPECT_NEAR(grad_ref(i), y_m.adj()(i), tol_grad) + << "var_value, element " << i; + } + stan::math::recover_memory(); + + for (Eigen::Index d = 0; d < n; ++d) { + Eigen::Matrix, Eigen::Dynamic, 1> y_f(n); + for (Eigen::Index i = 0; i < n; ++i) { + y_f(i) = fvar(y(i), i == d ? 1.0 : 0.0); + } + fvar lp_f = 0; + constrain_lp(y_f, lp_f); + EXPECT_NEAR(lp_ref, lp_f.val_, tol_lp) << "fvar, direction " << d; + EXPECT_NEAR(grad_ref(d), lp_f.d_, tol_grad) << "fvar, direction " << d; + } +} + +/** + * Expect the lower triangle of `constrain(y)` to match `ref` within + * `abs_tol + rel_tol * |ref|` in double, `Eigen::Matrix`, + * `var_value` and `fvar`. + */ +template +void expect_values_all_modes(F constrain, const Eigen::VectorXd& y, bool strict, + const Eigen::VectorXd& ref, double abs_tol, + double rel_tol) { + using stan::math::fvar; + using stan::math::var; + auto expect_ref = [&](const Eigen::VectorXd& v, const char* mode) { + ASSERT_EQ(ref.size(), v.size()) << mode; + for (Eigen::Index i = 0; i < ref.size(); ++i) { + EXPECT_NEAR(ref(i), v(i), abs_tol + rel_tol * std::fabs(ref(i))) + << mode << ", element " << i; + } + }; + expect_ref(lower_triangle(stan::math::value_of(constrain(y)), strict), + "double"); + Eigen::Matrix y_v = stan::math::to_var(y); + expect_ref(lower_triangle(stan::math::value_of(constrain(y_v)), strict), + "var"); + stan::math::var_value y_m(y); + expect_ref(lower_triangle(stan::math::value_of(constrain(y_m)), strict), + "var_value"); + Eigen::Matrix, Eigen::Dynamic, 1> y_f(y.size()); + for (Eigen::Index i = 0; i < y.size(); ++i) { + y_f(i) = fvar(y(i), 1.0); + } + expect_ref(lower_triangle(stan::math::value_of(constrain(y_f)), strict), + "fvar"); + stan::math::recover_memory(); +} + +} // namespace corr_transform_test + +#endif diff --git a/test/unit/math/mix/constraint/cov_matrix_constrain_lkj_test.cpp b/test/unit/math/mix/constraint/cov_matrix_constrain_lkj_test.cpp index 1ae38872908..60b0b630054 100644 --- a/test/unit/math/mix/constraint/cov_matrix_constrain_lkj_test.cpp +++ b/test/unit/math/mix/constraint/cov_matrix_constrain_lkj_test.cpp @@ -1,4 +1,5 @@ -#include +#include +#include TEST(mathMixMatFun, cov_matrix_constrain_lkj) { auto f = [](int K) { @@ -52,3 +53,74 @@ TEST(mathMixMatFun, cov_matrix_constrain_lkj_lp) { stan::test::expect_ad_matvar(f2(4), x1); stan::test::expect_ad_matvar(f2(3), x2); } + +// lp is log |det J| of the map from x (6 partial correlations, then 4 log +// standard deviations) to the lower triangle of the covariance matrix, +// diagonal included. +TEST(mathMixMatFun, cov_matrix_constrain_lkj_lp_is_log_det_jacobian) { + Eigen::VectorXd x(10); + x << 0.3, -1.2, 0.8, 2.1, -0.4, 1.5, 0.2, -0.7, 1.1, -0.3; + corr_transform_test::expect_lp_is_log_det_jacobian( + [](const auto& x) { return stan::math::cov_matrix_constrain_lkj(x, 4); }, + [](const auto& x, auto& lp) { + stan::math::cov_matrix_constrain_lkj(x, 4, lp); + }, + x, false, 1e-12); +} + +// K = 40 with unconstrained values in [-1.9, 1.9], as default inits: the +// covariance matrix must be the correlation matrix scaled by exp(x) with +// the diagonal exp(2 x), lp finite, and the var_value gradient must equal +// the Eigen gradient and be finite. (The matrix has a condition number +// near 1e26, so a floating-point Cholesky factorization of it is not a +// meaningful check.) +TEST(mathMixMatFun, cov_matrix_constrain_lkj_long_rows) { + using stan::math::var; + const int K = 40; + const int k_choose_2 = (K * (K - 1)) / 2; + Eigen::VectorXd x(k_choose_2 + K); + for (int i = 0; i < x.size(); ++i) { + x(i) = 1.9 * std::sin(1.3 * i + 0.7); + } + Eigen::MatrixXd W(K, K); + for (int i = 0; i < W.size(); ++i) { + W(i) = std::cos(0.7 * i); + } + double lp = 0; + Eigen::MatrixXd Sigma = stan::math::cov_matrix_constrain_lkj(x, K, lp); + EXPECT_TRUE(std::isfinite(lp)); + EXPECT_LT((Sigma.diagonal().array() / (2 * x.tail(K).array()).exp() - 1) + .abs() + .maxCoeff(), + 1e-14); + Eigen::VectorXd sds = x.tail(K).array().exp(); + Eigen::MatrixXd expected + = sds.asDiagonal() + * stan::math::corr_matrix_constrain(x.head(k_choose_2).eval(), K) + * sds.asDiagonal(); + EXPECT_LT((Sigma - expected).cwiseAbs().maxCoeff(), + 1e-14 * expected.cwiseAbs().maxCoeff()); + + Eigen::Matrix x_v = stan::math::to_var(x); + var lp_v = 0; + Eigen::Matrix Sigma_v + = stan::math::cov_matrix_constrain_lkj(x_v, K, lp_v); + var f_v = (W.array() * Sigma_v.array()).sum() + lp_v; + f_v.grad(); + Eigen::VectorXd g_v = stan::math::value_of(x_v.adj()); + stan::math::recover_memory(); + + stan::math::var_value x_m(x); + var lp_m = 0; + stan::math::var_value Sigma_m + = stan::math::cov_matrix_constrain_lkj(x_m, K, lp_m); + var f_m = stan::math::sum(stan::math::elt_multiply(W, Sigma_m)) + lp_m; + f_m.grad(); + Eigen::VectorXd g_m = x_m.adj(); + stan::math::recover_memory(); + + EXPECT_TRUE(g_v.allFinite()); + EXPECT_TRUE(g_m.allFinite()); + EXPECT_LT((g_v - g_m).cwiseAbs().maxCoeff(), + 1e-12 * g_v.cwiseAbs().maxCoeff()); +} From 60c75c2ea2f4aad35117ee81d560e2a7b85302e2 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 11 Nov 2026 16:45:46 -0500 Subject: [PATCH 4/7] Prune and document the correlation transform tests Delete the coefficient-builder test and the corr_matrix and LKJ long-rows tests, which checked no behavior develop gets wrong. Split the Cholesky long-rows test into the #3218 reproduction and a var_value gradient check against Eigen, sharing one input helper. Add a reference test for log_cosh_tanh_sech and more sizes to the log-det-Jacobian tests. Give each new test a short comment on why it exists. Replace const auto& with auto&& or decltype(auto) on branch-added lines, and note in the coefficient builder docs why the floating-point inverse of the position is exact. --- .../constraint/cholesky_corr_constrain.hpp | 20 ++-- .../prim/constraint/corr_matrix_constrain.hpp | 2 +- .../constraint/cov_matrix_constrain_lkj.hpp | 8 +- .../constraint/cov_matrix_constrain_lkj.hpp | 4 +- .../cholesky_corr_constrain_test.cpp | 103 ++++++++---------- .../mix/constraint/corr_constrain_test.cpp | 6 +- .../constraint/corr_matrix_constrain_test.cpp | 89 +++++---------- .../cov_matrix_constrain_lkj_test.cpp | 82 +++----------- test/unit/math/mix/fun/log_cosh_test.cpp | 41 ++++++- test/unit/math/mix/fun/tanh_test.cpp | 14 +-- test/unit/math/rev/prob/lkj_corr_test.cpp | 5 +- 11 files changed, 156 insertions(+), 218 deletions(-) diff --git a/stan/math/prim/constraint/cholesky_corr_constrain.hpp b/stan/math/prim/constraint/cholesky_corr_constrain.hpp index 71a65dba01e..9e49bdcff52 100644 --- a/stan/math/prim/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/prim/constraint/cholesky_corr_constrain.hpp @@ -47,9 +47,12 @@ inline Eigen::Index cholesky_corr_position(Eigen::Index r, Eigen::Index c, /** * 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 + * `-sum_n c_n log(cosh(y_n))`, in row order: the coefficient of element * `(r, c)` is `r + 1 - c`, with `r` recovered from the position by - * inverting `cholesky_corr_position`. + * inverting `cholesky_corr_position`. The inverse is exact, + * since at the first position of row `r` the radicand `1 + 8 i` is the + * perfect square `(2 r - 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 @@ -57,7 +60,7 @@ inline Eigen::Index cholesky_corr_position(Eigen::Index r, Eigen::Index c, inline auto cholesky_corr_log_jacobian_coefficients(Eigen::Index K) { const Eigen::Index n = (K * (K - 1)) / 2; return make_holder( - [](const auto& i) { + [](auto&& i) { const auto r = (0.5 * (1.0 + (1.0 + 8.0 * i).sqrt())).floor(); return (r + 1.0 - (i - 0.5 * r * (r - 1.0))).matrix(); }, @@ -67,10 +70,13 @@ inline auto cholesky_corr_log_jacobian_coefficients(Eigen::Index K) { /** * 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 + * `-sum_n c_n log(cosh(y_n))`, in column order: the coefficient of every * element of column `c` is `K - c` (equation 11 of Lewandowski, Kurowicka * and Joe, 2009), with `c` recovered from the position by inverting - * `cholesky_corr_position`. + * `cholesky_corr_position`. The inverse is exact, since + * at the first position of column `c` the radicand `b^2 - 8 i` is the + * perfect square `(2 (K - c) - 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 @@ -79,7 +85,7 @@ inline auto corr_matrix_log_jacobian_coefficients(Eigen::Index K) { const Eigen::Index n = (K * (K - 1)) / 2; const double b = 2.0 * K - 1.0; return make_holder( - [K = static_cast(K), b](const auto& i) { + [K = static_cast(K), b](auto&& i) { return (K - (0.5 * (b - (b * b - 8.0 * i).sqrt())).floor()).matrix(); }, Eigen::ArrayXd::LinSpaced(n, 0.0, n - 1.0)); @@ -167,7 +173,7 @@ cholesky_corr_constrain(EigVec&& y, int K, Lp& lp) { check_size_match("cholesky_corr_constrain", "y.size()", y.size(), "k_choose_2", k_choose_2); decltype(auto) y_ref = to_ref(std::forward(y)); - const auto& log_cosh_y = to_ref(log_cosh(y_ref)); + 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( diff --git a/stan/math/prim/constraint/corr_matrix_constrain.hpp b/stan/math/prim/constraint/corr_matrix_constrain.hpp index 4cfb4ab3d6c..0085ea2250c 100644 --- a/stan/math/prim/constraint/corr_matrix_constrain.hpp +++ b/stan/math/prim/constraint/corr_matrix_constrain.hpp @@ -81,7 +81,7 @@ corr_matrix_constrain(T&& x, Eigen::Index k, Lp& lp) { decltype(auto) x_ref = to_ref(std::forward(x)); check_size_match("cov_matrix_constrain", "x.size()", x_ref.size(), "k_choose_2", k_choose_2); - const auto& log_cosh_x = to_ref(log_cosh(x_ref)); + decltype(auto) log_cosh_x = to_ref(log_cosh(x_ref)); lp -= dot_product(internal::corr_matrix_log_jacobian_coefficients(k), log_cosh_x); return multiply_lower_tri_self_transpose( diff --git a/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp b/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp index d26a94b8b0f..6aed42f585f 100644 --- a/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp +++ b/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp @@ -39,7 +39,7 @@ cov_matrix_constrain_lkj(T&& x, size_t k) { decltype(auto) x_ref = to_ref(std::forward(x)); check_size_match("cov_matrix_constrain_lkj", "x.size()", x_ref.size(), "k_choose_2 + k", k_choose_2 + k); - const auto& cpcs = x_ref.head(k_choose_2); + auto&& cpcs = x_ref.head(k_choose_2); return multiply_lower_tri_self_transpose(diag_pre_multiply( to_ref(positive_constrain(x_ref.tail(k))), internal::cholesky_corr_factor( @@ -67,9 +67,9 @@ cov_matrix_constrain_lkj(T&& x, size_t k, return_type_t& lp) { decltype(auto) x_ref = to_ref(std::forward(x)); check_size_match("cov_matrix_constrain_lkj", "x.size()", x_ref.size(), "k_choose_2 + k", k_choose_2 + k); - const auto& cpcs = x_ref.head(k_choose_2); - const auto& log_sds = x_ref.tail(k); - const auto& log_cosh_cpcs = to_ref(log_cosh(cpcs)); + auto&& cpcs = x_ref.head(k_choose_2); + auto&& log_sds = x_ref.tail(k); + decltype(auto) log_cosh_cpcs = to_ref(log_cosh(cpcs)); lp -= dot_product(internal::corr_matrix_log_jacobian_coefficients(k), log_cosh_cpcs); lp += (k + 1.0) * sum(log_sds) + k * LOG_TWO; diff --git a/stan/math/rev/constraint/cov_matrix_constrain_lkj.hpp b/stan/math/rev/constraint/cov_matrix_constrain_lkj.hpp index 8ceddd2e7e5..7d9191d44d4 100644 --- a/stan/math/rev/constraint/cov_matrix_constrain_lkj.hpp +++ b/stan/math/rev/constraint/cov_matrix_constrain_lkj.hpp @@ -66,8 +66,8 @@ inline var_value cov_matrix_constrain_lkj( check_size_match("cov_matrix_constrain_lkj", "x.size()", x.size(), "k_choose_2 + k", k_choose_2 + k); auto arena_x = to_arena(std::forward(x)); - const auto& cpcs = arena_x.head(k_choose_2); - const auto& log_sds = arena_x.tail(k); + auto&& cpcs = arena_x.head(k_choose_2); + auto&& log_sds = arena_x.tail(k); lp -= dot_product(internal::corr_matrix_log_jacobian_coefficients(k), log_cosh(cpcs)); lp += (k + 1.0) * sum(log_sds) + k * LOG_TWO; diff --git a/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp b/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp index f804d900b60..e6eb734c1e5 100644 --- a/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp +++ b/test/unit/math/mix/constraint/cholesky_corr_constrain_test.cpp @@ -14,6 +14,15 @@ int inv_size(const T& x) { return -1; } +// K (K - 1) / 2 deterministic values in [-1.9, 1.9], like default inits +inline Eigen::VectorXd long_rows_input(int K) { + Eigen::VectorXd y((K * (K - 1)) / 2); + for (int i = 0; i < y.size(); ++i) { + y(i) = 1.9 * std::sin(1.3 * i + 0.7); + } + return y; +} + template auto g1(const T& x) { stan::scalar_type_t lp = 0; @@ -116,62 +125,43 @@ TEST(mathMixMatFun, cholesky_corr_constrain_lp) { stan::test::expect_ad_matvar(f2(3), x2); } -// The lazy coefficient expressions recover the row or column of each -// position in closed form; check them against the formulas r + 1 - c (row -// order) and K - c (column order) for every element of several sizes. -TEST(mathMixMatFun, corr_log_jacobian_coefficients) { - using stan::math::internal::cholesky_corr_log_jacobian_coefficients; - using stan::math::internal::cholesky_corr_position; - using stan::math::internal::corr_matrix_log_jacobian_coefficients; - using stan::math::internal::CorrOrder; - for (int K : {0, 1, 2, 3, 4, 7, 40, 200}) { - Eigen::VectorXd row_order = cholesky_corr_log_jacobian_coefficients(K); - Eigen::VectorXd column_order = corr_matrix_log_jacobian_coefficients(K); - ASSERT_EQ((K * (K - 1)) / 2, row_order.size()) << "K = " << K; - ASSERT_EQ((K * (K - 1)) / 2, column_order.size()) << "K = " << K; - for (int r = 1; r < K; ++r) { - for (int c = 0; c < r; ++c) { - EXPECT_EQ(r + 1 - c, - row_order(cholesky_corr_position(r, c, K))) - << "K = " << K << ", r = " << r << ", c = " << c; - EXPECT_EQ(K - c, column_order(cholesky_corr_position( - r, c, K))) - << "K = " << K << ", r = " << r << ", c = " << c; - } - } - } -} - -// lp is log |det J| of the map from y to the strictly lower triangular -// elements of the Cholesky factor. +// The Jacobian is a closed-form weighted sum of log_cosh(y); the weights +// are only right if lp equals log |det J| of the autodiff Jacobian. TEST(mathMixMatFun, cholesky_corr_constrain_lp_is_log_det_jacobian) { - Eigen::VectorXd y(6); - y << 0.3, -1.2, 0.8, 2.1, -0.4, 1.5; + Eigen::VectorXd y4(6); + y4 << 0.3, -1.2, 0.8, 2.1, -0.4, 1.5; corr_transform_test::expect_lp_is_log_det_jacobian( - [](const auto& y) { return stan::math::cholesky_corr_constrain(y, 4); }, - [](const auto& y, auto& lp) { - stan::math::cholesky_corr_constrain(y, 4, lp); - }, - y, true, 1e-12); + [](auto&& y) { return stan::math::cholesky_corr_constrain(y, 4); }, + [](auto&& y, auto& lp) { stan::math::cholesky_corr_constrain(y, 4, lp); }, + y4, true, 1e-12); + for (int K : {2, 3, 5, 7}) { + Eigen::VectorXd y((K * (K - 1)) / 2); + for (int i = 0; i < y.size(); ++i) { + y(i) = 1.5 * std::sin(0.9 * i + 0.3); + } + corr_transform_test::expect_lp_is_log_det_jacobian( + [K](auto&& y) { return stan::math::cholesky_corr_constrain(y, K); }, + [K](auto&& y, auto& lp) { + stan::math::cholesky_corr_constrain(y, K, lp); + }, + y, true, 1e-12); + } } -// lp = sum_n w_n log(1 - tanh(y_n)^2) with weights (1, 1.5, 1) for K = 3, -// and its gradient, at large y against mpmath. Forming 1 - tanh(y)^2 and -// 1 - sum of squares loses digits from |y| = 8. +// The old Jacobian terms log1m(square(z)) and log1m(sum_sqs) cancel at +// large |y|; the log_cosh form must stay accurate there in every mode. TEST(mathMixMatFun, cholesky_corr_constrain_lp_large_argument) { Eigen::VectorXd y(3); y << 15.0, 12.0, -0.5; Eigen::VectorXd grad_ref(3); grad_ref << -1.9999999999996257, -2.9999999997734919, 0.92423431452001952; corr_transform_test::expect_lp_all_modes( - [](const auto& y, auto& lp) { - stan::math::cholesky_corr_constrain(y, 3, lp); - }, + [](auto&& y, auto& lp) { stan::math::cholesky_corr_constrain(y, 3, lp); }, y, -62.77449311123027, grad_ref, 1e-13, 1e-14); } -// Values of L at large |y| against mpmath. sqrt(1 - sum of squares) had a -// relative error of 3.8e-4 in L(1, 1) at y = 15. +// The old sqrt(1 - sum_sqs) recursion loses the small elements of L when +// partial correlations are near 1; the tanh / log_cosh factor must not. TEST(mathMixMatFun, cholesky_corr_constrain_values_large_argument) { Eigen::VectorXd y(6); y << 15.0, 12.0, -0.5, 9.0, 2.0, -14.0; @@ -180,31 +170,30 @@ TEST(mathMixMatFun, cholesky_corr_constrain_values_large_argument) { -5.6786918924293556e-6, 1.0897607083695929e-5, 0.99999996954004097, 0.00023794090595878951, -6.5605200973176475e-5, 1.091052174635906e-10; corr_transform_test::expect_values_all_modes( - [](const auto& y) { return stan::math::cholesky_corr_constrain(y, 4); }, - y, false, ref, 0.0, 4e-15); + [](auto&& y) { return stan::math::cholesky_corr_constrain(y, 4); }, y, + false, ref, 0.0, 4e-15); } -// K = 40 with unconstrained values in [-1.9, 1.9], as default inits: a row -// of 39 moderate values makes 1 - sum of squares about 1e-20, which -// sqrt(1 - sum) rounded to 0. The diagonal must stay positive, the rows -// must have unit norm, lp and the LKJ density must be finite, and the -// var_value gradient must equal the Eigen gradient. +// #3218: with default inits at K = 40 a diagonal element of L rounded to 0, +// lkj_corr_cholesky_lpdf rejected it, and initialization failed. TEST(mathMixMatFun, cholesky_corr_constrain_long_rows) { - using stan::math::var; const int K = 40; - Eigen::VectorXd y((K * (K - 1)) / 2); - for (int i = 0; i < y.size(); ++i) { - y(i) = 1.9 * std::sin(1.3 * i + 0.7); - } + Eigen::VectorXd y = cholesky_corr_constrain_test::long_rows_input(K); double lp = 0; Eigen::MatrixXd L = stan::math::cholesky_corr_constrain(y, K, lp); EXPECT_GT(L.diagonal().minCoeff(), 0.0); EXPECT_LT((L.rowwise().squaredNorm().array() - 1).abs().maxCoeff(), 1e-14); EXPECT_TRUE(std::isfinite(lp)); EXPECT_TRUE(std::isfinite(stan::math::lkj_corr_cholesky_lpdf(L, 1.0))); +} + +// The var_value overload has a hand-written reverse pass; Eigen +// differentiates the forward formula and is the oracle for it at K = 40. +TEST(mathMixMatFun, cholesky_corr_constrain_long_rows_var_value_gradient) { + using stan::math::var; + const int K = 40; + Eigen::VectorXd y = cholesky_corr_constrain_test::long_rows_input(K); - // gradient of 2 sum(log(diag(L))) + lp: Eigen through the prim code, - // var_value through the hand-written reverse pass Eigen::Matrix y_v = stan::math::to_var(y); var lp_v = 0; Eigen::Matrix L_v diff --git a/test/unit/math/mix/constraint/corr_constrain_test.cpp b/test/unit/math/mix/constraint/corr_constrain_test.cpp index 88c719bf6d3..c991c091ff2 100644 --- a/test/unit/math/mix/constraint/corr_constrain_test.cpp +++ b/test/unit/math/mix/constraint/corr_constrain_test.cpp @@ -1,14 +1,14 @@ #include -// log(1 - tanh(x)^2) and its derivative -2 tanh(x) at large |x|, against -// mpmath. Forming 1 - tanh(x)^2 loses all digits from |x| = 19.06. +// The old Jacobian log1m(square(tanh(x))) loses accuracy past |x| = 10 and +// is -inf past 19; the log_cosh form must stay exact and finite there. TEST(mathMixMatFun, corr_constrain_lp_large_argument) { Eigen::VectorXd x(3); x << 15.0, -19.5, 30.0; Eigen::VectorXd grad_ref(3); grad_ref << -1.9999999999996257, 2.0, -2.0; corr_transform_test::expect_lp_all_modes( - [](const auto& x, auto& lp) { stan::math::corr_constrain(x, lp); }, x, + [](auto&& x, auto& lp) { stan::math::corr_constrain(x, lp); }, x, -124.84111691664052, grad_ref, 1e-13, 1e-14); } diff --git a/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp b/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp index e1b80c27c0b..d5f0a322cab 100644 --- a/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp +++ b/test/unit/math/mix/constraint/corr_matrix_constrain_test.cpp @@ -119,37 +119,43 @@ TEST(mathMixMatFun, corr_matrix_constrain_lp) { stan::test::expect_ad_matvar(f2(3), x2); } -// lp is log |det J| of the map from y to the strictly lower triangular -// elements of the correlation matrix. +// The Jacobian is a closed-form weighted sum of log_cosh(y) in column order; +// the weights are only right if lp equals log |det J| of the autodiff Jacobian. TEST(mathMixMatFun, corr_matrix_constrain_lp_is_log_det_jacobian) { - Eigen::VectorXd y(6); - y << 0.3, -1.2, 0.8, 2.1, -0.4, 1.5; + Eigen::VectorXd y4(6); + y4 << 0.3, -1.2, 0.8, 2.1, -0.4, 1.5; corr_transform_test::expect_lp_is_log_det_jacobian( - [](const auto& y) { return stan::math::corr_matrix_constrain(y, 4); }, - [](const auto& y, auto& lp) { - stan::math::corr_matrix_constrain(y, 4, lp); - }, - y, true, 1e-12); + [](auto&& y) { return stan::math::corr_matrix_constrain(y, 4); }, + [](auto&& y, auto& lp) { stan::math::corr_matrix_constrain(y, 4, lp); }, + y4, true, 1e-12); + for (int K : {2, 3, 5, 7}) { + Eigen::VectorXd y((K * (K - 1)) / 2); + for (int i = 0; i < y.size(); ++i) { + y(i) = 1.5 * std::sin(0.9 * i + 0.3); + } + corr_transform_test::expect_lp_is_log_det_jacobian( + [K](auto&& y) { return stan::math::corr_matrix_constrain(y, K); }, + [K](auto&& y, auto& lp) { + stan::math::corr_matrix_constrain(y, K, lp); + }, + y, true, 1e-12); + } } -// lp = sum_n w_n log(1 - tanh(y_n)^2) with weights (1.5, 1.5, 1) for K = 3, -// and its gradient, at large y against mpmath. Forming 1 - tanh(y)^2 loses -// digits from |y| = 8. +// The old read_corr_L Jacobian terms cancel at large |y|; the log_cosh form +// must stay accurate there in every mode. TEST(mathMixMatFun, corr_matrix_constrain_lp_large_argument) { Eigen::VectorXd y(3); y << 15.0, 12.0, -0.5; Eigen::VectorXd grad_ref(3); grad_ref << -2.9999999999994385, -2.9999999997734919, 0.92423431452001952; corr_transform_test::expect_lp_all_modes( - [](const auto& y, auto& lp) { - stan::math::corr_matrix_constrain(y, 3, lp); - }, + [](auto&& y, auto& lp) { stan::math::corr_matrix_constrain(y, 3, lp); }, y, -77.081345930670418, grad_ref, 1e-13, 1e-14); } -// Correlations at large |y| against mpmath. With sqrt(1 - z^2) from a -// rounded z, the small elements of the Cholesky factor had relative errors -// up to 3.8e-4, which reached the correlations close to 1. +// The old sqrt(1 - sum_sqs) recursion put ~1e-10 errors in the correlations +// when the partial correlations are near 1; the log_cosh factor must not. TEST(mathMixMatFun, corr_matrix_constrain_values_large_argument) { Eigen::VectorXd y(6); y << 15.0, 12.0, -0.5, 9.0, 2.0, -14.0; @@ -157,49 +163,6 @@ TEST(mathMixMatFun, corr_matrix_constrain_values_large_argument) { ref << 0.99999999999981285, 0.99999999992449731, 0.99999999993182827, -0.46211715726000976, -0.46211663421720721, -0.46210665234659286; corr_transform_test::expect_values_all_modes( - [](const auto& y) { return stan::math::corr_matrix_constrain(y, 4); }, y, - true, ref, 4e-16, 0.0); -} - -// K = 40 with unconstrained values in [-1.9, 1.9], as default inits: the -// diagonal must be 1, lp finite, and the var_value gradient must equal the -// Eigen gradient and be finite. -TEST(mathMixMatFun, corr_matrix_constrain_long_rows) { - using stan::math::var; - const int K = 40; - Eigen::VectorXd y((K * (K - 1)) / 2); - for (int i = 0; i < y.size(); ++i) { - y(i) = 1.9 * std::sin(1.3 * i + 0.7); - } - Eigen::MatrixXd W(K, K); - for (int i = 0; i < W.size(); ++i) { - W(i) = std::cos(0.7 * i); - } - double lp = 0; - Eigen::MatrixXd Omega = stan::math::corr_matrix_constrain(y, K, lp); - EXPECT_LT((Omega.diagonal().array() - 1).abs().maxCoeff(), 1e-14); - EXPECT_TRUE(std::isfinite(lp)); - - Eigen::Matrix y_v = stan::math::to_var(y); - var lp_v = 0; - Eigen::Matrix Omega_v - = stan::math::corr_matrix_constrain(y_v, K, lp_v); - var f_v = (W.array() * Omega_v.array()).sum() + lp_v; - f_v.grad(); - Eigen::VectorXd g_v = stan::math::value_of(y_v.adj()); - stan::math::recover_memory(); - - stan::math::var_value y_m(y); - var lp_m = 0; - stan::math::var_value Omega_m - = stan::math::corr_matrix_constrain(y_m, K, lp_m); - var f_m = stan::math::sum(stan::math::elt_multiply(W, Omega_m)) + lp_m; - f_m.grad(); - Eigen::VectorXd g_m = y_m.adj(); - stan::math::recover_memory(); - - EXPECT_TRUE(g_v.allFinite()); - EXPECT_TRUE(g_m.allFinite()); - EXPECT_LT((g_v - g_m).cwiseAbs().maxCoeff(), - 1e-12 * g_v.cwiseAbs().maxCoeff()); + [](auto&& y) { return stan::math::corr_matrix_constrain(y, 4); }, y, true, + ref, 4e-16, 0.0); } diff --git a/test/unit/math/mix/constraint/cov_matrix_constrain_lkj_test.cpp b/test/unit/math/mix/constraint/cov_matrix_constrain_lkj_test.cpp index 60b0b630054..76d7e1cffe1 100644 --- a/test/unit/math/mix/constraint/cov_matrix_constrain_lkj_test.cpp +++ b/test/unit/math/mix/constraint/cov_matrix_constrain_lkj_test.cpp @@ -54,73 +54,27 @@ TEST(mathMixMatFun, cov_matrix_constrain_lkj_lp) { stan::test::expect_ad_matvar(f2(3), x2); } -// lp is log |det J| of the map from x (6 partial correlations, then 4 log -// standard deviations) to the lower triangle of the covariance matrix, -// diagonal included. +// The lp overload used to omit the tanh and exp Jacobian terms of the partial +// correlations and standard deviations; lp must equal log |det J|. TEST(mathMixMatFun, cov_matrix_constrain_lkj_lp_is_log_det_jacobian) { - Eigen::VectorXd x(10); - x << 0.3, -1.2, 0.8, 2.1, -0.4, 1.5, 0.2, -0.7, 1.1, -0.3; + Eigen::VectorXd x4(10); + x4 << 0.3, -1.2, 0.8, 2.1, -0.4, 1.5, 0.2, -0.7, 1.1, -0.3; corr_transform_test::expect_lp_is_log_det_jacobian( - [](const auto& x) { return stan::math::cov_matrix_constrain_lkj(x, 4); }, - [](const auto& x, auto& lp) { + [](auto&& x) { return stan::math::cov_matrix_constrain_lkj(x, 4); }, + [](auto&& x, auto& lp) { stan::math::cov_matrix_constrain_lkj(x, 4, lp); }, - x, false, 1e-12); -} - -// K = 40 with unconstrained values in [-1.9, 1.9], as default inits: the -// covariance matrix must be the correlation matrix scaled by exp(x) with -// the diagonal exp(2 x), lp finite, and the var_value gradient must equal -// the Eigen gradient and be finite. (The matrix has a condition number -// near 1e26, so a floating-point Cholesky factorization of it is not a -// meaningful check.) -TEST(mathMixMatFun, cov_matrix_constrain_lkj_long_rows) { - using stan::math::var; - const int K = 40; - const int k_choose_2 = (K * (K - 1)) / 2; - Eigen::VectorXd x(k_choose_2 + K); - for (int i = 0; i < x.size(); ++i) { - x(i) = 1.9 * std::sin(1.3 * i + 0.7); - } - Eigen::MatrixXd W(K, K); - for (int i = 0; i < W.size(); ++i) { - W(i) = std::cos(0.7 * i); + x4, false, 1e-12); + for (int K : {2, 3, 5, 7}) { + Eigen::VectorXd x((K * (K - 1)) / 2 + K); + for (int i = 0; i < x.size(); ++i) { + x(i) = 1.5 * std::sin(0.9 * i + 0.3); + } + corr_transform_test::expect_lp_is_log_det_jacobian( + [K](auto&& x) { return stan::math::cov_matrix_constrain_lkj(x, K); }, + [K](auto&& x, auto& lp) { + stan::math::cov_matrix_constrain_lkj(x, K, lp); + }, + x, false, 1e-12); } - double lp = 0; - Eigen::MatrixXd Sigma = stan::math::cov_matrix_constrain_lkj(x, K, lp); - EXPECT_TRUE(std::isfinite(lp)); - EXPECT_LT((Sigma.diagonal().array() / (2 * x.tail(K).array()).exp() - 1) - .abs() - .maxCoeff(), - 1e-14); - Eigen::VectorXd sds = x.tail(K).array().exp(); - Eigen::MatrixXd expected - = sds.asDiagonal() - * stan::math::corr_matrix_constrain(x.head(k_choose_2).eval(), K) - * sds.asDiagonal(); - EXPECT_LT((Sigma - expected).cwiseAbs().maxCoeff(), - 1e-14 * expected.cwiseAbs().maxCoeff()); - - Eigen::Matrix x_v = stan::math::to_var(x); - var lp_v = 0; - Eigen::Matrix Sigma_v - = stan::math::cov_matrix_constrain_lkj(x_v, K, lp_v); - var f_v = (W.array() * Sigma_v.array()).sum() + lp_v; - f_v.grad(); - Eigen::VectorXd g_v = stan::math::value_of(x_v.adj()); - stan::math::recover_memory(); - - stan::math::var_value x_m(x); - var lp_m = 0; - stan::math::var_value Sigma_m - = stan::math::cov_matrix_constrain_lkj(x_m, K, lp_m); - var f_m = stan::math::sum(stan::math::elt_multiply(W, Sigma_m)) + lp_m; - f_m.grad(); - Eigen::VectorXd g_m = x_m.adj(); - stan::math::recover_memory(); - - EXPECT_TRUE(g_v.allFinite()); - EXPECT_TRUE(g_m.allFinite()); - EXPECT_LT((g_v - g_m).cwiseAbs().maxCoeff(), - 1e-12 * g_v.cwiseAbs().maxCoeff()); } diff --git a/test/unit/math/mix/fun/log_cosh_test.cpp b/test/unit/math/mix/fun/log_cosh_test.cpp index 0eb052845fe..1e9bd043363 100644 --- a/test/unit/math/mix/fun/log_cosh_test.cpp +++ b/test/unit/math/mix/fun/log_cosh_test.cpp @@ -4,19 +4,23 @@ #include #include +// Every autodiff mode of log_cosh must agree with finite differences on +// both sides of the |x| = 1 branch. TEST(mathMixMatFun, logCosh) { - auto f = [](const auto& x1) { return stan::math::log_cosh(x1); }; + auto f = [](auto&& x1) { return stan::math::log_cosh(x1); }; stan::test::expect_common_unary_vectorized( f); stan::test::expect_unary_vectorized( f, -30.0, -2.6, -1.0, -0.5, 0.0, 0.5, 0.999, 1.0, 1.001, 2.6, 30.0); } +// The var_value overload has its own reverse pass; it must match the +// Eigen path. TEST(mathMixMatFun, logCoshVarMat) { using stan::math::vec_concat; using stan::test::expect_ad_vector_matvar; using stan::test::internal::common_nonzero_args; - auto f = [](const auto& x1) { return stan::math::log_cosh(x1); }; + auto f = [](auto&& x1) { return stan::math::log_cosh(x1); }; std::vector com_args = common_nonzero_args(); std::vector args{-30.0, -2.6, -0.5, 0.999, 1.001, 2.6, 30.0}; auto all_args = vec_concat(com_args, args); @@ -27,9 +31,8 @@ TEST(mathMixMatFun, logCoshVarMat) { expect_ad_vector_matvar(f, A); } -// Values and derivatives against mpmath, on both sides of the branch at -// |x| = 1, at a tiny argument (where log(cosh(x)) = x^2 / 2), and where -// cosh(x) overflows. +// log(cosh(x)) rounds to 0 near x = 0 and overflows at 800; log_cosh must +// match mpmath to a few eps there and across the |x| = 1 branch. TEST(mathMixMatFun, logCoshReference) { using stan::math::fvar; using stan::math::var; @@ -42,7 +45,7 @@ TEST(mathMixMatFun, logCoshReference) { {-3.0, 2.3093285045777851, -0.99505475368673045}, {30.0, 29.306852819440055, 1.0}, {800.0, 799.30685281944005, 1.0}}; - for (const auto& c : cases) { + for (auto&& c : cases) { const double x = c[0]; const double tol_v = 4 * std::numeric_limits::epsilon() * c[1]; const double tol_d @@ -61,3 +64,29 @@ TEST(mathMixMatFun, logCoshReference) { EXPECT_NEAR(c[2], y_f.d_, tol_d) << "x = " << x; } } + +// The var_value Cholesky factor takes tanh and sech from this helper; all +// three outputs must stay accurate through the underflow of sech at 800. +TEST(mathMixMatFun, logCoshTanhSech) { + const double eps = std::numeric_limits::epsilon(); + // x, log(cosh(x)), tanh(x), sech(x) + std::vector> cases + = {{0.0, 0.0, 0.0, 1.0}, + {1e-10, 5.0e-21, 1.0e-10, 1.0}, + {0.5, 0.12011450695827752, 0.46211715726000976, 0.88681888397007391}, + {0.999, 0.43301944642088479, 0.76117386166056995, 0.64854787974731328}, + {1.0, 0.43378083048302719, 0.76159415595576489, 0.6480542736638854}, + {1.001, 0.43454263451956291, 0.76201381055100676, 0.64756077130222642}, + {-3.0, 2.3093285045777851, -0.99505475368673045, 0.099327927419433208}, + {30.0, 29.306852819440055, 1.0, 1.8715245937680349e-13}, + {700.0, 699.30685281944005, 1.0, 1.9719353087519542e-304}, + {800.0, 799.30685281944005, 1.0, 0.0}}; + for (auto&& c : cases) { + const double x = c[0]; + const stan::math::internal::log_cosh_parts parts + = stan::math::internal::log_cosh_tanh_sech(x); + EXPECT_NEAR(c[1], parts.log_cosh, 4 * eps * std::fabs(c[1])) << "x = " << x; + EXPECT_NEAR(c[2], parts.tanh, 4 * eps * std::fabs(c[2])) << "x = " << x; + EXPECT_NEAR(c[3], parts.sech, 4 * eps * std::fabs(c[3])) << "x = " << x; + } +} diff --git a/test/unit/math/mix/fun/tanh_test.cpp b/test/unit/math/mix/fun/tanh_test.cpp index 9a894406fb9..b3c76d0e629 100644 --- a/test/unit/math/mix/fun/tanh_test.cpp +++ b/test/unit/math/mix/fun/tanh_test.cpp @@ -16,9 +16,8 @@ TEST(mathMixMatFun, tanh) { stan::test::expect_complex_common(f); } -// First and second derivatives at large |x| against mpmath. 1 - tanh(x)^2 -// is 0 in double from |x| = 19.06, and 1 / cosh(x)^2 makes the second -// derivative NaN once cosh(x)^2 overflows (|x| > 355). +// The old forward-mode derivative 1 - u * u rounds to exactly 0 once +// |x| > 19; the derivatives must stay accurate down to their underflow. TEST(mathMixMatFun, tanh_derivatives_large_argument) { using stan::math::fvar; using stan::math::var; @@ -29,7 +28,7 @@ TEST(mathMixMatFun, tanh_derivatives_large_argument) { {20.0, 1.6993417021166356e-17, -3.3986834042332711e-17}, {300.0, 1.0601586212017243e-260, -2.1203172424034487e-260}, {1000.0, 0.0, 0.0}}; - for (const auto& c : cases) { + for (auto&& c : cases) { const double x = c[0]; const double d1 = c[1]; const double d2 = c[2]; @@ -51,9 +50,8 @@ TEST(mathMixMatFun, tanh_derivatives_large_argument) { } } -// First, second and third derivatives in fvar> against mpmath, -// at 0 (where an fabs(x) in the derivative would make the third derivative -// 0), on both sides of 0.5, and in the tail. +// The 4 e / (1 + e)^2 branch starts at |x| = 0.5 so that the zero derivative +// of fabs at 0 cannot zero the third derivative; guard x = 0 and both edges. TEST(mathMixMatFun, tanh_third_derivative) { using stan::math::fvar; using stan::math::var; @@ -67,7 +65,7 @@ TEST(mathMixMatFun, tanh_third_derivative) { {3.0, 0.0098660371654401913, -0.019634494363042436, 0.038880116525661682}, {-20.0, 1.6993417021166356e-17, 3.3986834042332711e-17, 6.7973668084665422e-17}}; - for (const auto& c : cases) { + for (auto&& c : cases) { const double x = c[0]; var x_v = x; fvar> x_ffv(fvar(x_v, 1.0), fvar(1.0, 0.0)); diff --git a/test/unit/math/rev/prob/lkj_corr_test.cpp b/test/unit/math/rev/prob/lkj_corr_test.cpp index c56c7972bf0..1b00498ec23 100644 --- a/test/unit/math/rev/prob/lkj_corr_test.cpp +++ b/test/unit/math/rev/prob/lkj_corr_test.cpp @@ -65,9 +65,8 @@ TEST_F(AgradRev, ProbDistributionsLkjCorrCholesky_gradients1) { double fx_ad_1; finite_diff_gradient(test_func_1, x1, fx_1, grad_1); gradient(test_func_1, x1, fx_ad_1, grad_ad_1); - // x1(0) = 0, where the exact derivative is 0; the finite difference has - // rounding noise of about 1e-13 there, so compare with an absolute - // tolerance instead of float equality + // autodiff gives exactly 0 at x1(0) = 0 while the finite difference has + // ~1e-13 of rounding noise, so float equality would fail there test_grad_near(grad_1, grad_ad_1, 1e-10); EXPECT_FLOAT_EQ(fx_1, fx_ad_1); } From 8062b5da73a1d8e21e470e00ec14ed03ac7ddb15 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Wed, 11 Nov 2026 17:11:06 -0500 Subject: [PATCH 5/7] Split the log_cosh helper and hold head/tail blocks by value Replace the struct-returning log_cosh_tanh_sech with log_cosh holding its formula inline, the existing tanh for derivatives, and an internal sech for the var_value Cholesky factor, so each caller computes only what it uses. Bind the head and tail blocks of the LKJ input with auto rather than auto&&, since they are expressions, not references. --- .../constraint/cholesky_corr_constrain.hpp | 2 +- .../constraint/cov_matrix_constrain_lkj.hpp | 6 +-- stan/math/prim/fun/log_cosh.hpp | 44 +++++++------------ .../constraint/cholesky_corr_constrain.hpp | 12 ++--- .../constraint/cov_matrix_constrain_lkj.hpp | 4 +- stan/math/rev/fun/log_cosh.hpp | 18 +++----- test/unit/math/mix/fun/log_cosh_test.cpp | 37 +++++++--------- 7 files changed, 50 insertions(+), 73 deletions(-) diff --git a/stan/math/prim/constraint/cholesky_corr_constrain.hpp b/stan/math/prim/constraint/cholesky_corr_constrain.hpp index 9e49bdcff52..971568d5acc 100644 --- a/stan/math/prim/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/prim/constraint/cholesky_corr_constrain.hpp @@ -177,7 +177,7 @@ cholesky_corr_constrain(EigVec&& y, int K, Lp& lp) { lp -= dot_product(internal::cholesky_corr_log_jacobian_coefficients(K), log_cosh_y); return internal::cholesky_corr_factor( - y_ref, log_cosh_y, K); + y_ref, std::move(log_cosh_y), K); } /** diff --git a/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp b/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp index 6aed42f585f..fbdfe01100d 100644 --- a/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp +++ b/stan/math/prim/constraint/cov_matrix_constrain_lkj.hpp @@ -39,7 +39,7 @@ cov_matrix_constrain_lkj(T&& x, size_t k) { decltype(auto) x_ref = to_ref(std::forward(x)); check_size_match("cov_matrix_constrain_lkj", "x.size()", x_ref.size(), "k_choose_2 + k", k_choose_2 + k); - auto&& cpcs = x_ref.head(k_choose_2); + auto cpcs = x_ref.head(k_choose_2); return multiply_lower_tri_self_transpose(diag_pre_multiply( to_ref(positive_constrain(x_ref.tail(k))), internal::cholesky_corr_factor( @@ -67,8 +67,8 @@ cov_matrix_constrain_lkj(T&& x, size_t k, return_type_t& lp) { decltype(auto) x_ref = to_ref(std::forward(x)); check_size_match("cov_matrix_constrain_lkj", "x.size()", x_ref.size(), "k_choose_2 + k", k_choose_2 + k); - auto&& cpcs = x_ref.head(k_choose_2); - auto&& log_sds = x_ref.tail(k); + auto cpcs = x_ref.head(k_choose_2); + auto log_sds = x_ref.tail(k); decltype(auto) log_cosh_cpcs = to_ref(log_cosh(cpcs)); lp -= dot_product(internal::corr_matrix_log_jacobian_coefficients(k), log_cosh_cpcs); diff --git a/stan/math/prim/fun/log_cosh.hpp b/stan/math/prim/fun/log_cosh.hpp index 08c6d676dd0..417c935988c 100644 --- a/stan/math/prim/fun/log_cosh.hpp +++ b/stan/math/prim/fun/log_cosh.hpp @@ -2,7 +2,6 @@ #define STAN_MATH_PRIM_FUN_LOG_COSH_HPP #include -#include #include #include #include @@ -15,40 +14,23 @@ namespace math { namespace internal { /** - * `log(cosh(x))`, `tanh(x)` and `sech(x) = 1 / cosh(x)` of one argument. - */ -struct log_cosh_parts { - double log_cosh; - double tanh; - double sech; -}; - -/** - * Return `log(cosh(x))`, its derivative `tanh(x)`, and `sech(x)` from one - * call of `expm1` (`|x| < 1`) or `exp` (`|x| >= 1`). Neither branch - * subtracts two nearly equal numbers or overflows. + * Return `sech(x) = 1 / cosh(x)` as `2 e / (1 + e^2)` with `e = exp(-|x|)`, + * which underflows to 0 instead of overflowing for large `|x|`. * * @param x argument - * @return `log(cosh(x))`, `tanh(x)` and `sech(x)` + * @return `sech(x)` */ -inline log_cosh_parts log_cosh_tanh_sech(double x) { - const double a = fabs(x); - if (a < 1.0) { - const double u = expm1(a); - const double d = u * u / (2.0 * (1.0 + u)); - const double w = u * (u + 2.0); - return {log1p(d), copysign(w / (w + 2.0), x), 1.0 / (1.0 + d)}; - } - const double e = exp(-a); - const double e2 = e * e; - return {a + log1p(0.5 * (e2 - 1.0)), copysign((1.0 - e2) / (1.0 + e2), x), - 2.0 * e / (1.0 + e2)}; +inline double sech(double x) { + const double e = exp(-fabs(x)); + return 2.0 * e / (1.0 + e * e); } } // namespace internal /** * Return the natural logarithm of the hyperbolic cosine of the argument, - * without overflow for large `|x|`. The derivative is `tanh(x)`. + * from `expm1` for `|x| < 1` and from `exp(-|x|)` above, so that neither + * branch subtracts two nearly equal numbers or overflows. The derivative is + * `tanh(x)`. * * @tparam T arithmetic type * @param x argument @@ -56,7 +38,13 @@ inline log_cosh_parts log_cosh_tanh_sech(double x) { */ template * = nullptr> inline double log_cosh(T x) { - return internal::log_cosh_tanh_sech(static_cast(x)).log_cosh; + const double a = fabs(static_cast(x)); + if (a < 1.0) { + const double u = expm1(a); + return log1p(u * u / (2.0 * (1.0 + u))); + } + const double e = exp(-a); + return a + log1p(0.5 * (e * e - 1.0)); } /** diff --git a/stan/math/rev/constraint/cholesky_corr_constrain.hpp b/stan/math/rev/constraint/cholesky_corr_constrain.hpp index d6b5d4f955c..9168f977662 100644 --- a/stan/math/rev/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/rev/constraint/cholesky_corr_constrain.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -45,11 +46,12 @@ inline var_value cholesky_corr_factor(T&& y, Eigen::Index K) { double exp_minus_a = 1.0; for (Index c = 0; c < r; ++c) { const Index k = cholesky_corr_position(r, c, K); - const log_cosh_parts parts = log_cosh_tanh_sech(arena_y.val().coeff(k)); - tanh_y.coeffRef(k) = parts.tanh; - d_own.coeffRef(k) = parts.sech * parts.sech * exp_minus_a; - L_val.coeffRef(r, c) = parts.tanh * exp_minus_a; - exp_minus_a *= parts.sech; + const double y_k = arena_y.val().coeff(k); + const double sech_k = sech(y_k); + tanh_y.coeffRef(k) = tanh(y_k); + d_own.coeffRef(k) = sech_k * sech_k * exp_minus_a; + L_val.coeffRef(r, c) = tanh_y.coeff(k) * exp_minus_a; + exp_minus_a *= sech_k; } L_val.coeffRef(r, r) = exp_minus_a; } diff --git a/stan/math/rev/constraint/cov_matrix_constrain_lkj.hpp b/stan/math/rev/constraint/cov_matrix_constrain_lkj.hpp index 7d9191d44d4..0d153049d64 100644 --- a/stan/math/rev/constraint/cov_matrix_constrain_lkj.hpp +++ b/stan/math/rev/constraint/cov_matrix_constrain_lkj.hpp @@ -66,8 +66,8 @@ inline var_value cov_matrix_constrain_lkj( check_size_match("cov_matrix_constrain_lkj", "x.size()", x.size(), "k_choose_2 + k", k_choose_2 + k); auto arena_x = to_arena(std::forward(x)); - auto&& cpcs = arena_x.head(k_choose_2); - auto&& log_sds = arena_x.tail(k); + auto cpcs = arena_x.head(k_choose_2); + auto log_sds = arena_x.tail(k); lp -= dot_product(internal::corr_matrix_log_jacobian_coefficients(k), log_cosh(cpcs)); lp += (k + 1.0) * sum(log_sds) + k * LOG_TWO; diff --git a/stan/math/rev/fun/log_cosh.hpp b/stan/math/rev/fun/log_cosh.hpp index d1f43f0347b..177575d0efb 100644 --- a/stan/math/rev/fun/log_cosh.hpp +++ b/stan/math/rev/fun/log_cosh.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include namespace stan { @@ -19,9 +20,8 @@ namespace math { */ template * = nullptr> inline var log_cosh(const var_value& x) { - const internal::log_cosh_parts parts = internal::log_cosh_tanh_sech(x.val()); - const double tanh_x = parts.tanh; - return make_callback_var(parts.log_cosh, [x, tanh_x](auto& vi) mutable { + const double tanh_x = tanh(x.val()); + return make_callback_var(log_cosh(x.val()), [x, tanh_x](auto& vi) mutable { x.adj() += vi.adj() * tanh_x; }); } @@ -36,18 +36,10 @@ inline var log_cosh(const var_value& x) { */ template * = nullptr> inline auto log_cosh(T&& x) { - using val_t = plain_type_t>; auto arena_x = to_arena(std::forward(x)); - val_t value(arena_x.rows(), arena_x.cols()); - arena_t tanh_x(arena_x.rows(), arena_x.cols()); - for (Eigen::Index i = 0; i < arena_x.size(); ++i) { - const internal::log_cosh_parts parts - = internal::log_cosh_tanh_sech(arena_x.val().coeff(i)); - value.coeffRef(i) = parts.log_cosh; - tanh_x.coeffRef(i) = parts.tanh; - } + arena_t>> tanh_x = tanh(arena_x.val()); return make_callback_var( - std::move(value), [arena_x, tanh_x](auto& vi) mutable { + log_cosh(arena_x.val()), [arena_x, tanh_x](auto& vi) mutable { arena_x.adj().array() += vi.adj().array() * tanh_x.array(); }); } diff --git a/test/unit/math/mix/fun/log_cosh_test.cpp b/test/unit/math/mix/fun/log_cosh_test.cpp index 1e9bd043363..ed08c9912c3 100644 --- a/test/unit/math/mix/fun/log_cosh_test.cpp +++ b/test/unit/math/mix/fun/log_cosh_test.cpp @@ -65,28 +65,23 @@ TEST(mathMixMatFun, logCoshReference) { } } -// The var_value Cholesky factor takes tanh and sech from this helper; all -// three outputs must stay accurate through the underflow of sech at 800. -TEST(mathMixMatFun, logCoshTanhSech) { +// The var_value Cholesky factor builds exp(-a) as a product of sech; it must +// stay accurate through its underflow at 800. +TEST(mathMixMatFun, sechReference) { const double eps = std::numeric_limits::epsilon(); - // x, log(cosh(x)), tanh(x), sech(x) - std::vector> cases - = {{0.0, 0.0, 0.0, 1.0}, - {1e-10, 5.0e-21, 1.0e-10, 1.0}, - {0.5, 0.12011450695827752, 0.46211715726000976, 0.88681888397007391}, - {0.999, 0.43301944642088479, 0.76117386166056995, 0.64854787974731328}, - {1.0, 0.43378083048302719, 0.76159415595576489, 0.6480542736638854}, - {1.001, 0.43454263451956291, 0.76201381055100676, 0.64756077130222642}, - {-3.0, 2.3093285045777851, -0.99505475368673045, 0.099327927419433208}, - {30.0, 29.306852819440055, 1.0, 1.8715245937680349e-13}, - {700.0, 699.30685281944005, 1.0, 1.9719353087519542e-304}, - {800.0, 799.30685281944005, 1.0, 0.0}}; + // x, sech(x) + std::vector> cases = {{0.0, 1.0}, + {1e-10, 1.0}, + {0.5, 0.88681888397007391}, + {0.999, 0.64854787974731328}, + {1.0, 0.6480542736638854}, + {1.001, 0.64756077130222642}, + {-3.0, 0.099327927419433208}, + {30.0, 1.8715245937680349e-13}, + {700.0, 1.9719353087519542e-304}, + {800.0, 0.0}}; for (auto&& c : cases) { - const double x = c[0]; - const stan::math::internal::log_cosh_parts parts - = stan::math::internal::log_cosh_tanh_sech(x); - EXPECT_NEAR(c[1], parts.log_cosh, 4 * eps * std::fabs(c[1])) << "x = " << x; - EXPECT_NEAR(c[2], parts.tanh, 4 * eps * std::fabs(c[2])) << "x = " << x; - EXPECT_NEAR(c[3], parts.sech, 4 * eps * std::fabs(c[3])) << "x = " << x; + EXPECT_NEAR(c[1], stan::math::internal::sech(c[0]), 4 * eps * c[1]) + << "x = " << c[0]; } } From 60bf474fd1627ce0e46553b65a4c1b8eaadcb431 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 16 Nov 2026 14:41:40 -0500 Subject: [PATCH 6/7] cleanup for cholesky_corr_constrain --- stan/math/rev/constraint/cholesky_corr_constrain.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/stan/math/rev/constraint/cholesky_corr_constrain.hpp b/stan/math/rev/constraint/cholesky_corr_constrain.hpp index 9168f977662..6e230644fef 100644 --- a/stan/math/rev/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/rev/constraint/cholesky_corr_constrain.hpp @@ -58,13 +58,15 @@ inline var_value cholesky_corr_factor(T&& y, Eigen::Index K) { var_value L = L_val; reverse_pass_callback([arena_y, L, tanh_y, d_own, K]() mutable { for (Index r = 1; r < K; ++r) { - double later = L.adj().coeff(r, r) * L.val().coeff(r, r); + // sum of adj * val over the elements of the row after column c, which + // all depend on y_c through the shared exp(-a) + double running_sum = L.adj().coeff(r, r) * L.val().coeff(r, r); for (Index c = r - 1; c >= 0; --c) { const Index k = cholesky_corr_position(r, c, K); const double adj_rc = L.adj().coeff(r, c); arena_y.adj().coeffRef(k) - += adj_rc * d_own.coeff(k) - tanh_y.coeff(k) * later; - later += adj_rc * L.val().coeff(r, c); + += adj_rc * d_own.coeff(k) - tanh_y.coeff(k) * running_sum; + running_sum += adj_rc * L.val().coeff(r, c); } } }); From fac338f4e7b81c6b886b7aa4f8a8567550c286f5 Mon Sep 17 00:00:00 2001 From: Steve Bronder Date: Mon, 16 Nov 2026 15:08:02 -0500 Subject: [PATCH 7/7] Add sech and give the correlation transform locals full names Add sech as inv(cosh(x)) in prim, which covers every scalar and container type through the existing cosh and inv overloads, and use it in the var_value Cholesky factor in place of the 2e / (1 + e^2) form, which had no accuracy advantage. Replace the one-letter locals in the correlation transforms, log_cosh and forward-mode tanh with descriptive names, and rename the reverse-pass accumulator to running_sum with a comment on what it sums. --- stan/math/fwd/fun/tanh.hpp | 13 ++-- .../constraint/cholesky_corr_constrain.hpp | 76 ++++++++++--------- stan/math/prim/fun.hpp | 1 + stan/math/prim/fun/log_cosh.hpp | 26 ++----- stan/math/prim/fun/sech.hpp | 29 +++++++ .../constraint/cholesky_corr_constrain.hpp | 51 +++++++------ test/unit/math/mix/fun/log_cosh_test.cpp | 21 ----- test/unit/math/mix/fun/sech_test.cpp | 47 ++++++++++++ 8 files changed, 157 insertions(+), 107 deletions(-) create mode 100644 stan/math/prim/fun/sech.hpp create mode 100644 test/unit/math/mix/fun/sech_test.cpp diff --git a/stan/math/fwd/fun/tanh.hpp b/stan/math/fwd/fun/tanh.hpp index a887879a383..1dc564cf8af 100644 --- a/stan/math/fwd/fun/tanh.hpp +++ b/stan/math/fwd/fun/tanh.hpp @@ -25,15 +25,16 @@ namespace math { */ template inline fvar tanh(const fvar& x) { - const T u = tanh(x.val_); + const T tanh_x = tanh(x.val_); const double x_val = value_of_rec(x.val_); if (fabs(x_val) < 0.5) { - return fvar(u, x.d_ * (1 - u * u)); + return fvar(tanh_x, x.d_ * (1 - tanh_x * tanh_x)); } - // -2 |x|, with the sign of x fixed in this branch - const T e = exp(x_val > 0 ? -2 * x.val_ : 2 * x.val_); - const T one_p_e = 1 + e; - return fvar(u, x.d_ * (4 * e / (one_p_e * one_p_e))); + // 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(tanh_x, + x.d_ * (4 * exp_neg_2abs_x / (one_plus_exp * one_plus_exp))); } /** diff --git a/stan/math/prim/constraint/cholesky_corr_constrain.hpp b/stan/math/prim/constraint/cholesky_corr_constrain.hpp index 971568d5acc..e483c1b33f8 100644 --- a/stan/math/prim/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/prim/constraint/cholesky_corr_constrain.hpp @@ -26,21 +26,21 @@ enum class CorrOrder : uint8_t { row = 0, column = 1 }; /** * Return the position in the unconstrained vector of the partial - * correlation for element `(r, c)`, `c < r`, of the Cholesky factor. + * correlation for element `(row, col)`, `col < row`, of the Cholesky factor. * * @tparam Order order of the partial correlations in the vector - * @param r row - * @param c column + * @param row row + * @param col column * @param K number of rows * @return position in the unconstrained vector */ template -inline Eigen::Index cholesky_corr_position(Eigen::Index r, Eigen::Index c, +inline Eigen::Index cholesky_corr_position(Eigen::Index row, Eigen::Index col, Eigen::Index K) { if constexpr (Order == CorrOrder::column) { - return c * (K - 1) - (c * (c - 1)) / 2 + (r - c - 1); + return col * (K - 1) - (col * (col - 1)) / 2 + (row - col - 1); } else { - return (r * (r - 1)) / 2 + c; + return (row * (row - 1)) / 2 + col; } } @@ -48,54 +48,60 @@ inline Eigen::Index cholesky_corr_position(Eigen::Index r, Eigen::Index c, * 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 - * `(r, c)` is `r + 1 - c`, with `r` recovered from the position by + * `(row, col)` is `row + 1 - col`, with `row` recovered from the position by * inverting `cholesky_corr_position`. The inverse is exact, - * since at the first position of row `r` the radicand `1 + 8 i` is the - * perfect square `(2 r - 1)^2`, for which `sqrt` is exact, so `floor` never - * lands one row too low. + * 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 n = (K * (K - 1)) / 2; + const Eigen::Index k_choose_2 = (K * (K - 1)) / 2; return make_holder( - [](auto&& i) { - const auto r = (0.5 * (1.0 + (1.0 + 8.0 * i).sqrt())).floor(); - return (r + 1.0 - (i - 0.5 * r * (r - 1.0))).matrix(); + [](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(n, 0.0, n - 1.0)); + 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 `c` is `K - c` (equation 11 of Lewandowski, Kurowicka - * and Joe, 2009), with `c` recovered from the position by inverting - * `cholesky_corr_position`. The inverse is exact, since - * at the first position of column `c` the radicand `b^2 - 8 i` is the - * perfect square `(2 (K - c) - 1)^2`, for which `sqrt` is exact, so `floor` - * never lands one column too low. + * 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`. 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 n = (K * (K - 1)) / 2; - const double b = 2.0 * K - 1.0; + 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(K), b](auto&& i) { - return (K - (0.5 * (b - (b * b - 8.0 * i).sqrt())).floor()).matrix(); + [K = static_cast(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(n, 0.0, n - 1.0)); + 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(r, c) = tanh(y_c) exp(-a)` with `a` the sum of `log(cosh(y))` over the - * earlier columns of the row, which has no cancellation. + * `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 @@ -118,14 +124,14 @@ cholesky_corr_factor(EigVec&& y, LogCoshVec&& log_cosh_y, Eigen::Index K) { decltype(auto) y_ref = to_ref(std::forward(y)); decltype(auto) log_cosh_y_ref = to_ref(std::forward(log_cosh_y)); L.coeffRef(0, 0) = 1; - for (Eigen::Index r = 1; r < K; ++r) { - T_scalar a = 0; - for (Eigen::Index c = 0; c < r; ++c) { - const Eigen::Index k = cholesky_corr_position(r, c, K); - L.coeffRef(r, c) = tanh(y_ref.coeff(k)) * exp(-a); - a += log_cosh_y_ref.coeff(k); + 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(row, col, K); + L.coeffRef(row, col) = tanh(y_ref.coeff(pos)) * exp(-acc); + acc += log_cosh_y_ref.coeff(pos); } - L.coeffRef(r, r) = exp(-a); + L.coeffRef(row, row) = exp(-acc); } return L; } diff --git a/stan/math/prim/fun.hpp b/stan/math/prim/fun.hpp index 550d71a53de..469b3b37519 100644 --- a/stan/math/prim/fun.hpp +++ b/stan/math/prim/fun.hpp @@ -285,6 +285,7 @@ #include #include #include +#include #include #include #include diff --git a/stan/math/prim/fun/log_cosh.hpp b/stan/math/prim/fun/log_cosh.hpp index 417c935988c..db9f9183c66 100644 --- a/stan/math/prim/fun/log_cosh.hpp +++ b/stan/math/prim/fun/log_cosh.hpp @@ -12,20 +12,6 @@ namespace stan { namespace math { -namespace internal { -/** - * Return `sech(x) = 1 / cosh(x)` as `2 e / (1 + e^2)` with `e = exp(-|x|)`, - * which underflows to 0 instead of overflowing for large `|x|`. - * - * @param x argument - * @return `sech(x)` - */ -inline double sech(double x) { - const double e = exp(-fabs(x)); - return 2.0 * e / (1.0 + e * e); -} -} // namespace internal - /** * Return the natural logarithm of the hyperbolic cosine of the argument, * from `expm1` for `|x| < 1` and from `exp(-|x|)` above, so that neither @@ -38,13 +24,13 @@ inline double sech(double x) { */ template * = nullptr> inline double log_cosh(T x) { - const double a = fabs(static_cast(x)); - if (a < 1.0) { - const double u = expm1(a); - return log1p(u * u / (2.0 * (1.0 + u))); + const double abs_x = fabs(static_cast(x)); + if (abs_x < 1.0) { + const double expm1_abs_x = expm1(abs_x); + return log1p(expm1_abs_x * expm1_abs_x / (2.0 * (1.0 + expm1_abs_x))); } - const double e = exp(-a); - return a + log1p(0.5 * (e * e - 1.0)); + const double exp_neg_abs_x = exp(-abs_x); + return abs_x + log1p(0.5 * (exp_neg_abs_x * exp_neg_abs_x - 1.0)); } /** diff --git a/stan/math/prim/fun/sech.hpp b/stan/math/prim/fun/sech.hpp new file mode 100644 index 00000000000..c380d480464 --- /dev/null +++ b/stan/math/prim/fun/sech.hpp @@ -0,0 +1,29 @@ +#ifndef STAN_MATH_PRIM_FUN_SECH_HPP +#define STAN_MATH_PRIM_FUN_SECH_HPP + +#include +#include +#include +#include + +namespace stan { +namespace math { + +/** + * Return the hyperbolic secant `1 / cosh(x)` of the argument, elementwise + * for containers. For large `|x|`, `cosh(x)` overflows to infinity and the + * result is 0. + * + * @tparam T scalar, container, or `var_value` with inner Eigen type + * @param x argument + * @return hyperbolic secant of the argument + */ +template +inline auto sech(T&& x) { + return inv(cosh(std::forward(x))); +} + +} // namespace math +} // namespace stan + +#endif diff --git a/stan/math/rev/constraint/cholesky_corr_constrain.hpp b/stan/math/rev/constraint/cholesky_corr_constrain.hpp index 6e230644fef..84432a57ca3 100644 --- a/stan/math/rev/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/rev/constraint/cholesky_corr_constrain.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -35,38 +36,38 @@ inline var_value cholesky_corr_factor(T&& y, Eigen::Index K) { return Eigen::MatrixXd(); } auto arena_y = to_arena(std::forward(y)); - const Index n = arena_y.size(); - arena_t tanh_y(n); + arena_t tanh_y(arena_y.size()); // derivative of each element with respect to its own unconstrained value - arena_t d_own(n); + arena_t d_own(arena_y.size()); arena_t L_val = Eigen::MatrixXd::Zero(K, K); L_val.coeffRef(0, 0) = 1; - for (Index r = 1; r < K; ++r) { - // exp(-a), as the product of sech(y) over the earlier elements of the row - double exp_minus_a = 1.0; - for (Index c = 0; c < r; ++c) { - const Index k = cholesky_corr_position(r, c, K); - const double y_k = arena_y.val().coeff(k); - const double sech_k = sech(y_k); - tanh_y.coeffRef(k) = tanh(y_k); - d_own.coeffRef(k) = sech_k * sech_k * exp_minus_a; - L_val.coeffRef(r, c) = tanh_y.coeff(k) * exp_minus_a; - exp_minus_a *= sech_k; + for (Index row = 1; row < K; ++row) { + // exp(-acc), as the product of sech(y) over the earlier elements of the + // row + double exp_neg_acc = 1.0; + for (Index col = 0; col < row; ++col) { + const Index pos = cholesky_corr_position(row, col, K); + const double y_val = arena_y.val().coeff(pos); + const double sech_val = sech(y_val); + tanh_y.coeffRef(pos) = tanh(y_val); + d_own.coeffRef(pos) = sech_val * sech_val * exp_neg_acc; + L_val.coeffRef(row, col) = tanh_y.coeff(pos) * exp_neg_acc; + exp_neg_acc *= sech_val; } - L_val.coeffRef(r, r) = exp_minus_a; + L_val.coeffRef(row, row) = exp_neg_acc; } var_value L = L_val; reverse_pass_callback([arena_y, L, tanh_y, d_own, K]() mutable { - for (Index r = 1; r < K; ++r) { - // sum of adj * val over the elements of the row after column c, which - // all depend on y_c through the shared exp(-a) - double running_sum = L.adj().coeff(r, r) * L.val().coeff(r, r); - for (Index c = r - 1; c >= 0; --c) { - const Index k = cholesky_corr_position(r, c, K); - const double adj_rc = L.adj().coeff(r, c); - arena_y.adj().coeffRef(k) - += adj_rc * d_own.coeff(k) - tanh_y.coeff(k) * running_sum; - running_sum += adj_rc * L.val().coeff(r, c); + for (Index row = 1; row < K; ++row) { + // sum of adj * val over the elements of the row after column col, which + // all depend on y_col through the shared exp(-acc) + double running_sum = L.adj().coeff(row, row) * L.val().coeff(row, row); + for (Index col = row - 1; col >= 0; --col) { + const Index pos = cholesky_corr_position(row, col, K); + const double adj_rc = L.adj().coeff(row, col); + arena_y.adj().coeffRef(pos) + += adj_rc * d_own.coeff(pos) - tanh_y.coeff(pos) * running_sum; + running_sum += adj_rc * L.val().coeff(row, col); } } }); diff --git a/test/unit/math/mix/fun/log_cosh_test.cpp b/test/unit/math/mix/fun/log_cosh_test.cpp index ed08c9912c3..7de15d22298 100644 --- a/test/unit/math/mix/fun/log_cosh_test.cpp +++ b/test/unit/math/mix/fun/log_cosh_test.cpp @@ -64,24 +64,3 @@ TEST(mathMixMatFun, logCoshReference) { EXPECT_NEAR(c[2], y_f.d_, tol_d) << "x = " << x; } } - -// The var_value Cholesky factor builds exp(-a) as a product of sech; it must -// stay accurate through its underflow at 800. -TEST(mathMixMatFun, sechReference) { - const double eps = std::numeric_limits::epsilon(); - // x, sech(x) - std::vector> cases = {{0.0, 1.0}, - {1e-10, 1.0}, - {0.5, 0.88681888397007391}, - {0.999, 0.64854787974731328}, - {1.0, 0.6480542736638854}, - {1.001, 0.64756077130222642}, - {-3.0, 0.099327927419433208}, - {30.0, 1.8715245937680349e-13}, - {700.0, 1.9719353087519542e-304}, - {800.0, 0.0}}; - for (auto&& c : cases) { - EXPECT_NEAR(c[1], stan::math::internal::sech(c[0]), 4 * eps * c[1]) - << "x = " << c[0]; - } -} diff --git a/test/unit/math/mix/fun/sech_test.cpp b/test/unit/math/mix/fun/sech_test.cpp new file mode 100644 index 00000000000..7969ffb20a2 --- /dev/null +++ b/test/unit/math/mix/fun/sech_test.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +TEST(mathMixMatFun, sech) { + auto f = [](auto&& x1) { return stan::math::sech(x1); }; + stan::test::expect_common_unary_vectorized( + f); + stan::test::expect_unary_vectorized( + f, -30.0, -2.6, -1.2, -0.2, 0.0, 0.5, 1.3, 30.0); +} + +TEST(mathMixMatFun, sech_varmat) { + using stan::math::vec_concat; + using stan::test::expect_ad_vector_matvar; + using stan::test::internal::common_args; + auto f = [](auto&& x1) { return stan::math::sech(x1); }; + std::vector com_args = common_args(); + std::vector args{-30.0, -2.6, -1.2, -0.2, 0.5, 1.3, 30.0}; + auto all_args = vec_concat(com_args, args); + Eigen::VectorXd A(all_args.size()); + for (int i = 0; i < all_args.size(); ++i) { + A(i) = all_args[i]; + } + expect_ad_vector_matvar(f, A); +} + +// The var_value Cholesky factor builds exp(-acc) as a product of sech; it +// must stay accurate through its underflow at 800. +TEST(mathMixMatFun, sech_reference) { + const double eps = std::numeric_limits::epsilon(); + // x, sech(x) + std::vector> cases = {{0.0, 1.0}, + {1e-10, 1.0}, + {0.5, 0.88681888397007391}, + {0.999, 0.64854787974731328}, + {1.0, 0.6480542736638854}, + {1.001, 0.64756077130222642}, + {-3.0, 0.099327927419433208}, + {30.0, 1.8715245937680349e-13}, + {700.0, 1.9719353087519542e-304}, + {800.0, 0.0}}; + for (auto&& c : cases) { + EXPECT_NEAR(c[1], stan::math::sech(c[0]), 4 * eps * c[1]) << "x = " << c[0]; + } +}