diff --git a/stan/math/fwd/fun.hpp b/stan/math/fwd/fun.hpp index c028d2ebb86..cce2f4a5110 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..1dc564cf8af 100644 --- a/stan/math/fwd/fun/tanh.hpp +++ b/stan/math/fwd/fun/tanh.hpp @@ -4,17 +4,37 @@ #include #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|)`, which neither cancels nor overflows. + * + * @tparam T inner type of the forward-mode autodiff variable + * @param[in] x argument + * @return hyperbolic tangent of the argument + */ template inline fvar tanh(const fvar& x) { - T u = tanh(x.val_); - return fvar(u, x.d_ * (1 - u * u)); + const T tanh_x = tanh(x.val_); + const double x_val = value_of_rec(x.val_); + if (fabs(x_val) < 0.5) { + return fvar(tanh_x, x.d_ * (1 - tanh_x * tanh_x)); + } + // exp(-2 |x|), with the sign of x fixed in this branch + const T exp_neg_2abs_x = exp(x_val > 0 ? -2 * x.val_ : 2 * x.val_); + const T one_plus_exp = 1 + exp_neg_2abs_x; + return fvar(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 8ed2b1aa454..e483c1b33f8 100644 --- a/stan/math/prim/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/prim/constraint/cholesky_corr_constrain.hpp @@ -1,79 +1,189 @@ #ifndef STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP #define STAN_MATH_PRIM_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP +#include #include #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include namespace stan { namespace math { -template * = nullptr> +namespace internal { +/** + * Order of the partial correlations in the unconstrained vector: `row` for + * `cholesky_corr_constrain` (row 1, then row 2, ...), `column` for + * `corr_matrix_constrain` and `cov_matrix_constrain_lkj` (column 0, then + * column 1, ...). + */ +enum class CorrOrder : uint8_t { row = 0, column = 1 }; + +/** + * Return the position in the unconstrained vector of the partial + * correlation for element `(row, col)`, `col < row`, of the Cholesky factor. + * + * @tparam Order order of the partial correlations in the vector + * @param row row + * @param col column + * @param K number of rows + * @return position in the unconstrained vector + */ +template +inline Eigen::Index cholesky_corr_position(Eigen::Index row, Eigen::Index col, + Eigen::Index K) { + if constexpr (Order == CorrOrder::column) { + return col * (K - 1) - (col * (col - 1)) / 2 + (row - col - 1); + } else { + return (row * (row - 1)) / 2 + col; + } +} + +/** + * Return a lazy expression of the coefficients `c` for which the log + * absolute Jacobian determinant of `cholesky_corr_constrain` is + * `-sum_n c_n log(cosh(y_n))`, in row order: the coefficient of element + * `(row, col)` is `row + 1 - col`, with `row` recovered from the position by + * inverting `cholesky_corr_position`. The inverse is exact, + * since at the first position of a row the radicand `1 + 8 pos` is the + * perfect square `(2 row - 1)^2`, for which `sqrt` is exact, so `floor` + * never lands one row too low. + * + * @param K number of rows + * @return coefficients in the order of the unconstrained vector + */ +inline auto cholesky_corr_log_jacobian_coefficients(Eigen::Index K) { + const Eigen::Index k_choose_2 = (K * (K - 1)) / 2; + return make_holder( + [](auto&& pos) { + const auto row = (0.5 * (1.0 + (1.0 + 8.0 * pos).sqrt())).floor(); + return (row + 1.0 - (pos - 0.5 * row * (row - 1.0))).matrix(); + }, + Eigen::ArrayXd::LinSpaced(k_choose_2, 0.0, k_choose_2 - 1.0)); +} + +/** + * Return a lazy expression of the coefficients `c` for which the log + * absolute Jacobian determinant of `corr_matrix_constrain` is + * `-sum_n c_n log(cosh(y_n))`, in column order: the coefficient of every + * element of column `col` is `K - col` (equation 11 of Lewandowski, + * Kurowicka and Joe, 2009), with `col` recovered from the position by + * inverting `cholesky_corr_position`. The inverse is + * exact, since at the first position of a column the radicand + * `(2 K - 1)^2 - 8 pos` is the perfect square `(2 (K - col) - 1)^2`, for + * which `sqrt` is exact, so `floor` never lands one column too low. + * + * @param K number of rows + * @return coefficients in the order of the unconstrained vector + */ +inline auto corr_matrix_log_jacobian_coefficients(Eigen::Index K) { + const Eigen::Index k_choose_2 = (K * (K - 1)) / 2; + const double two_K_minus_one = 2.0 * K - 1.0; + return make_holder( + [K = static_cast(K), two_K_minus_one](auto&& pos) { + return (K + - (0.5 + * (two_K_minus_one + - (two_K_minus_one * two_K_minus_one - 8.0 * pos).sqrt())) + .floor()) + .matrix(); + }, + Eigen::ArrayXd::LinSpaced(k_choose_2, 0.0, k_choose_2 - 1.0)); +} + +/** + * Return the Cholesky factor of a correlation matrix from unconstrained + * values `y`, whose partial correlations are `tanh(y)`. Each element is + * `L(row, col) = tanh(y_col) exp(-acc)` with `acc` the sum of + * `log(cosh(y))` over the earlier columns of the row, which has no + * cancellation. + * + * @tparam Order order of the partial correlations in `y` + * @tparam EigVec type of the vector + * @tparam LogCoshVec type of the vector of `log(cosh(y))` + * @param y unconstrained values, `K (K - 1) / 2` of them + * @param log_cosh_y `log_cosh(y)`, shared with the log Jacobian + * @param K number of rows + * @return Cholesky factor of a correlation matrix + */ +template * = 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; +cholesky_corr_factor(EigVec&& y, LogCoshVec&& log_cosh_y, Eigen::Index K) { 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); + Eigen::Matrix L + = Eigen::Matrix::Zero(K, K); if (K == 0) { - return x; + return L; } - x.setZero(); - x.coeffRef(0, 0) = 1; - int k = 0; - for (int i = 1; i < K; ++i) { - x.coeffRef(i, 0) = z.coeff(k++); - T_scalar sum_sqs = square(x.coeff(i, 0)); - for (int j = 1; j < i; ++j) { - x.coeffRef(i, j) = z.coeff(k++) * sqrt(1.0 - sum_sqs); - sum_sqs += square(x.coeff(i, j)); + decltype(auto) y_ref = to_ref(std::forward(y)); + decltype(auto) log_cosh_y_ref = to_ref(std::forward(log_cosh_y)); + L.coeffRef(0, 0) = 1; + for (Eigen::Index row = 1; row < K; ++row) { + T_scalar acc = 0; + for (Eigen::Index col = 0; col < row; ++col) { + const Eigen::Index pos = cholesky_corr_position(row, col, K); + L.coeffRef(row, col) = tanh(y_ref.coeff(pos)) * exp(-acc); + acc += log_cosh_y_ref.coeff(pos); } - x.coeffRef(i, i) = sqrt(1.0 - sum_sqs); + L.coeffRef(row, row) = exp(-acc); } - return x; + return L; +} +} // namespace internal + +/** + * Return the Cholesky factor of a `K x K` correlation matrix from the + * `K (K - 1) / 2` unconstrained values `y`, whose partial correlations are + * `tanh(y)` in row order. + * + * @tparam EigVec type of the vector + * @param y unconstrained values + * @param K number of rows + * @return Cholesky factor of a correlation matrix + */ +template * = nullptr> +inline Eigen::Matrix, Eigen::Dynamic, Eigen::Dynamic> +cholesky_corr_constrain(EigVec&& y, int K) { + int k_choose_2 = (K * (K - 1)) / 2; + check_size_match("cholesky_corr_constrain", "constrain size", y.size(), + "k_choose_2", k_choose_2); + decltype(auto) y_ref = to_ref(std::forward(y)); + return internal::cholesky_corr_factor( + y_ref, log_cosh(y_ref), K); } -// FIXME to match above after debugged +/** + * Return the Cholesky factor of a `K x K` correlation matrix from the + * `K (K - 1) / 2` unconstrained values `y`, and add the log absolute + * Jacobian determinant of the transform to `lp`. + * + * @tparam EigVec type of the vector + * @tparam Lp scalar type of the log density accumulator + * @param y unconstrained values + * @param K number of rows + * @param[in,out] lp log density accumulator + * @return Cholesky factor of a correlation matrix + */ template * = 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) { - using Eigen::Dynamic; - using Eigen::Matrix; - using std::sqrt; - using T_scalar = value_type_t; +cholesky_corr_constrain(EigVec&& y, int K, Lp& lp) { int k_choose_2 = (K * (K - 1)) / 2; check_size_match("cholesky_corr_constrain", "y.size()", y.size(), "k_choose_2", k_choose_2); - Matrix z = corr_constrain(y, lp); - 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) { - lp += 0.5 * log1m(sum_sqs); - x.coeffRef(i, j) = z.coeff(k++) * sqrt(1.0 - sum_sqs); - sum_sqs += square(x.coeff(i, j)); - } - x.coeffRef(i, i) = sqrt(1.0 - sum_sqs); - } - return x; + decltype(auto) y_ref = to_ref(std::forward(y)); + decltype(auto) log_cosh_y = to_ref(log_cosh(y_ref)); + lp -= dot_product(internal::cholesky_corr_log_jacobian_coefficients(K), + log_cosh_y); + return internal::cholesky_corr_factor( + y_ref, std::move(log_cosh_y), K); } /** diff --git a/stan/math/prim/constraint/corr_constrain.hpp b/stan/math/prim/constraint/corr_constrain.hpp index c5cd1862139..e79be4de43d 100644 --- a/stan/math/prim/constraint/corr_constrain.hpp +++ b/stan/math/prim/constraint/corr_constrain.hpp @@ -2,11 +2,11 @@ #define STAN_MATH_PRIM_CONSTRAINT_CORR_CONSTRAIN_HPP #include -#include -#include +#include #include #include -#include +#include +#include namespace stan { namespace math { @@ -36,7 +36,8 @@ 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$. * * @tparam T_x Type of scalar or container * @param[in] x value or container @@ -44,9 +45,9 @@ 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; + 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 a2d54131f6f..0085ea2250c 100644 --- a/stan/math/prim/constraint/corr_matrix_constrain.hpp +++ b/stan/math/prim/constraint/corr_matrix_constrain.hpp @@ -4,10 +4,12 @@ #include #include #include -#include -#include +#include +#include +#include +#include #include -#include +#include namespace stan { namespace math { @@ -44,9 +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); - 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, log_cosh(x_ref), k)); } /** @@ -79,9 +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); - decltype(auto) cpc_ref - = to_ref(corr_constrain(std::forward(x_ref), lp)); - return read_corr_matrix(cpc_ref, k, lp); + 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( + 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..fbdfe01100d 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); + 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); + 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; + 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.hpp b/stan/math/prim/fun.hpp index 5fa3a64cd25..469b3b37519 100644 --- a/stan/math/prim/fun.hpp +++ b/stan/math/prim/fun.hpp @@ -176,6 +176,7 @@ #include #include #include +#include #include #include #include @@ -284,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 new file mode 100644 index 00000000000..db9f9183c66 --- /dev/null +++ b/stan/math/prim/fun/log_cosh.hpp @@ -0,0 +1,70 @@ +#ifndef STAN_MATH_PRIM_FUN_LOG_COSH_HPP +#define STAN_MATH_PRIM_FUN_LOG_COSH_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace stan { +namespace math { + +/** + * Return the natural logarithm of the hyperbolic cosine of the argument, + * 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 + * @return `log(cosh(x))` + */ +template * = nullptr> +inline double log_cosh(T x) { + 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 exp_neg_abs_x = exp(-abs_x); + return abs_x + log1p(0.5 * (exp_neg_abs_x * exp_neg_abs_x - 1.0)); +} + +/** + * 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(T&& x) { + return apply_scalar_unary::apply(std::forward(x)); +} + +} // namespace math +} // namespace stan + +#endif 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 576a1bb962e..84432a57ca3 100644 --- a/stan/math/rev/constraint/cholesky_corr_constrain.hpp +++ b/stan/math/rev/constraint/cholesky_corr_constrain.hpp @@ -2,17 +2,79 @@ #define STAN_MATH_REV_CONSTRAINT_CHOLESKY_CORR_CONSTRAIN_HPP #include +#include +#include +#include #include #include -#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)`, 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. + * + * @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(T&& y, Eigen::Index K) { + using Eigen::Index; + if (K == 0) { + return Eigen::MatrixXd(); + } + auto arena_y = to_arena(std::forward(y)); + arena_t tanh_y(arena_y.size()); + // derivative of each element with respect to its own unconstrained value + arena_t d_own(arena_y.size()); + arena_t L_val = Eigen::MatrixXd::Zero(K, K); + L_val.coeffRef(0, 0) = 1; + 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(row, row) = exp_neg_acc; + } + var_value L = L_val; + reverse_pass_callback([arena_y, L, tanh_y, d_own, K]() mutable { + 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); + } + } + }); + 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 @@ -25,55 +87,12 @@ namespace math { * @return Cholesky factor of correlation matrix */ template * = nullptr> -inline var_value cholesky_corr_constrain(const T& y, int K) { - using std::sqrt; +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); - - 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( + std::forward(y), K); } /** @@ -90,62 +109,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) { - using Eigen::Dynamic; - using Eigen::Matrix; - using std::sqrt; + 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(); - } - - var_value z = corr_constrain(y, lp); - 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)); - } - 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 { - 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); - 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--; - z.adj().coeffRef(k) += x.adj().coeffRef(i, 0); - } - }); - - return x; + lp -= dot_product(internal::cholesky_corr_log_jacobian_coefficients(K), + log_cosh(y)); + 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 8b8a028c9a5..b4f63a038c6 100644 --- a/stan/math/rev/constraint/corr_matrix_constrain.hpp +++ b/stan/math/rev/constraint/corr_matrix_constrain.hpp @@ -2,12 +2,15 @@ #define STAN_MATH_REV_CONSTRAINT_CORR_MATRIX_CONSTRAIN_HPP #include +#include +#include +#include +#include #include #include #include -#include -#include -#include +#include +#include namespace stan { namespace math { @@ -38,12 +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); - return read_corr_matrix(corr_constrain(x), k); + return multiply_lower_tri_self_transpose( + internal::cholesky_corr_factor( + std::forward(x), k)); } /** @@ -67,13 +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); - return read_corr_matrix(corr_constrain(x, lp), k, lp); + lp -= dot_product(internal::corr_matrix_log_jacobian_coefficients(k), + log_cosh(x)); + return multiply_lower_tri_self_transpose( + 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..0d153049d64 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)); + 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; + 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.hpp b/stan/math/rev/fun.hpp index 15b9dda45da..259bcafa3c2 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..177575d0efb --- /dev/null +++ b/stan/math/rev/fun/log_cosh.hpp @@ -0,0 +1,49 @@ +#ifndef STAN_MATH_REV_FUN_LOG_COSH_HPP +#define STAN_MATH_REV_FUN_LOG_COSH_HPP + +#include +#include +#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 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; + }); +} + +/** + * Return the elementwise natural logarithm of the hyperbolic cosine of the + * argument. + * + * @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(T&& x) { + auto arena_x = to_arena(std::forward(x)); + arena_t>> tanh_x = tanh(arena_x.val()); + return make_callback_var( + log_cosh(arena_x.val()), [arena_x, tanh_x](auto& vi) mutable { + arena_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..e6eb734c1e5 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 +#include namespace cholesky_corr_constrain_test { // easier than fiddling the quadratic equation @@ -13,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; @@ -114,3 +124,96 @@ TEST(mathMixMatFun, cholesky_corr_constrain_lp) { stan::test::expect_ad_matvar(f2(4), x1); stan::test::expect_ad_matvar(f2(3), x2); } + +// 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 y4(6); + y4 << 0.3, -1.2, 0.8, 2.1, -0.4, 1.5; + corr_transform_test::expect_lp_is_log_det_jacobian( + [](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); + } +} + +// 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( + [](auto&& y, auto& lp) { stan::math::cholesky_corr_constrain(y, 3, lp); }, + y, -62.77449311123027, grad_ref, 1e-13, 1e-14); +} + +// 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; + 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; + corr_transform_test::expect_values_all_modes( + [](auto&& y) { return stan::math::cholesky_corr_constrain(y, 4); }, y, + false, ref, 0.0, 4e-15); +} + +// #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) { + const int K = 40; + 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); + + 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_constrain_test.cpp b/test/unit/math/mix/constraint/corr_constrain_test.cpp index 609062f9f73..c991c091ff2 100644 --- a/test/unit/math/mix/constraint/corr_constrain_test.cpp +++ b/test/unit/math/mix/constraint/corr_constrain_test.cpp @@ -1,4 +1,16 @@ -#include +#include + +// 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( + [](auto&& x, auto& lp) { stan::math::corr_constrain(x, lp); }, x, + -124.84111691664052, grad_ref, 1e-13, 1e-14); +} TEST(mathMixMatFun, corr_constrain) { auto f = [](const auto& x1) { 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..d5f0a322cab 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 +#include namespace corr_matrix_constrain_test { // easier than fiddling the quadratic equation @@ -117,3 +118,51 @@ TEST(mathMixMatFun, corr_matrix_constrain_lp) { stan::test::expect_ad_matvar(f2(4), x1); stan::test::expect_ad_matvar(f2(3), x2); } + +// 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 y4(6); + y4 << 0.3, -1.2, 0.8, 2.1, -0.4, 1.5; + corr_transform_test::expect_lp_is_log_det_jacobian( + [](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); + } +} + +// 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( + [](auto&& y, auto& lp) { stan::math::corr_matrix_constrain(y, 3, lp); }, + y, -77.081345930670418, grad_ref, 1e-13, 1e-14); +} + +// 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; + Eigen::VectorXd ref(6); + ref << 0.99999999999981285, 0.99999999992449731, 0.99999999993182827, + -0.46211715726000976, -0.46211663421720721, -0.46210665234659286; + corr_transform_test::expect_values_all_modes( + [](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/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..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 @@ -1,4 +1,5 @@ -#include +#include +#include TEST(mathMixMatFun, cov_matrix_constrain_lkj) { auto f = [](int K) { @@ -52,3 +53,28 @@ TEST(mathMixMatFun, cov_matrix_constrain_lkj_lp) { stan::test::expect_ad_matvar(f2(4), x1); stan::test::expect_ad_matvar(f2(3), x2); } + +// 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 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( + [](auto&& x) { return stan::math::cov_matrix_constrain_lkj(x, 4); }, + [](auto&& x, auto& lp) { + stan::math::cov_matrix_constrain_lkj(x, 4, lp); + }, + 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); + } +} 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..7de15d22298 --- /dev/null +++ b/test/unit/math/mix/fun/log_cosh_test.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#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 = [](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 = [](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); +} + +// 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; + // 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 (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/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]; + } +} diff --git a/test/unit/math/mix/fun/tanh_test.cpp b/test/unit/math/mix/fun/tanh_test.cpp index d9e4ad5d567..b3c76d0e629 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,69 @@ TEST(mathMixMatFun, tanh) { stan::test::expect_complex_common(f); } +// 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; + // 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 (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(); + } +} + +// 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; + // 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 (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; diff --git a/test/unit/math/rev/prob/lkj_corr_test.cpp b/test/unit/math/rev/prob/lkj_corr_test.cpp index 6c912bd949a..1b00498ec23 100644 --- a/test/unit/math/rev/prob/lkj_corr_test.cpp +++ b/test/unit/math/rev/prob/lkj_corr_test.cpp @@ -65,7 +65,9 @@ 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); + // 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); }