|
| 1 | +#ifndef STAN_MATH_PRIM_CONSTRAINT_SUM_TO_ZERO_CONSTRAIN_HPP |
| 2 | +#define STAN_MATH_PRIM_CONSTRAINT_SUM_TO_ZERO_CONSTRAIN_HPP |
| 3 | + |
| 4 | +#include <stan/math/prim/meta.hpp> |
| 5 | +#include <stan/math/prim/fun/Eigen.hpp> |
| 6 | +#include <stan/math/prim/fun/to_ref.hpp> |
| 7 | +#include <stan/math/prim/fun/inv_sqrt.hpp> |
| 8 | +#include <stan/math/prim/functor/apply_vector_unary.hpp> |
| 9 | +#include <cmath> |
| 10 | + |
| 11 | +namespace stan { |
| 12 | +namespace math { |
| 13 | + |
| 14 | +/** |
| 15 | + * Return a vector with sum zero corresponding to the specified |
| 16 | + * free vector. |
| 17 | + * |
| 18 | + * The sum-to-zero transform is defined using a modified version of the |
| 19 | + * the inverse of the isometric log ratio transform (ILR). |
| 20 | + * See: |
| 21 | + * Egozcue, Juan Jose; Pawlowsky-Glahn, Vera; Mateu-Figueras, Gloria; |
| 22 | + * Barcelo-Vidal, Carles (2003), "Isometric logratio transformations for |
| 23 | + * compositional data analysis", Mathematical Geology, 35 (3): 279–300, |
| 24 | + * doi:10.1023/A:1023818214614, S2CID 122844634 |
| 25 | + * |
| 26 | + * This implementation is closer to the description of the same using "pivot |
| 27 | + * coordinates" in |
| 28 | + * Filzmoser, P., Hron, K., Templ, M. (2018). Geometrical Properties of |
| 29 | + * Compositional Data. In: Applied Compositional Data Analysis. Springer Series |
| 30 | + * in Statistics. Springer, Cham. https://doi.org/10.1007/978-3-319-96422-5_3 |
| 31 | + * |
| 32 | + * This is a linear transform, with no Jacobian. |
| 33 | + * |
| 34 | + * @tparam Vec type of the vector |
| 35 | + * @param y Free vector input of dimensionality K - 1. |
| 36 | + * @return Zero-sum vector of dimensionality K. |
| 37 | + */ |
| 38 | +template <typename Vec, require_eigen_col_vector_t<Vec>* = nullptr, |
| 39 | + require_not_st_var<Vec>* = nullptr> |
| 40 | +inline plain_type_t<Vec> sum_to_zero_constrain(const Vec& y) { |
| 41 | + const auto N = y.size(); |
| 42 | + |
| 43 | + plain_type_t<Vec> z = Eigen::VectorXd::Zero(N + 1); |
| 44 | + if (unlikely(N == 0)) { |
| 45 | + return z; |
| 46 | + } |
| 47 | + |
| 48 | + auto&& y_ref = to_ref(y); |
| 49 | + |
| 50 | + value_type_t<Vec> sum_w(0); |
| 51 | + for (int i = N; i > 0; --i) { |
| 52 | + double n = static_cast<double>(i); |
| 53 | + auto w = y_ref(i - 1) * inv_sqrt(n * (n + 1)); |
| 54 | + sum_w += w; |
| 55 | + |
| 56 | + z.coeffRef(i - 1) += sum_w; |
| 57 | + z.coeffRef(i) -= w * n; |
| 58 | + } |
| 59 | + |
| 60 | + return z; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Return a vector with sum zero corresponding to the specified |
| 65 | + * free vector. |
| 66 | + * |
| 67 | + * The sum-to-zero transform is defined using a modified version of the |
| 68 | + * the inverse of the isometric log ratio transform (ILR). |
| 69 | + * See: |
| 70 | + * Egozcue, Juan Jose; Pawlowsky-Glahn, Vera; Mateu-Figueras, Gloria; |
| 71 | + * Barcelo-Vidal, Carles (2003), "Isometric logratio transformations for |
| 72 | + * compositional data analysis", Mathematical Geology, 35 (3): 279–300, |
| 73 | + * doi:10.1023/A:1023818214614, S2CID 122844634 |
| 74 | + * |
| 75 | + * This implementation is closer to the description of the same using "pivot |
| 76 | + * coordinates" in |
| 77 | + * Filzmoser, P., Hron, K., Templ, M. (2018). Geometrical Properties of |
| 78 | + * Compositional Data. In: Applied Compositional Data Analysis. Springer Series |
| 79 | + * in Statistics. Springer, Cham. https://doi.org/10.1007/978-3-319-96422-5_3 |
| 80 | + * |
| 81 | + * This is a linear transform, with no Jacobian. |
| 82 | + * |
| 83 | + * @tparam Vec type of the vector |
| 84 | + * @param y Free vector input of dimensionality K - 1. |
| 85 | + * @param lp unused |
| 86 | + * @return Zero-sum vector of dimensionality K. |
| 87 | + */ |
| 88 | +template <typename Vec, require_eigen_col_vector_t<Vec>* = nullptr, |
| 89 | + require_not_st_var<Vec>* = nullptr> |
| 90 | +inline plain_type_t<Vec> sum_to_zero_constrain(const Vec& y, |
| 91 | + value_type_t<Vec>& lp) { |
| 92 | + return sum_to_zero_constrain(y); |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Return a vector with sum zero corresponding to the specified |
| 97 | + * free vector. |
| 98 | + * |
| 99 | + * The sum-to-zero transform is defined using a modified version of |
| 100 | + * the inverse of the isometric log ratio transform (ILR). |
| 101 | + * See: |
| 102 | + * Egozcue, Juan Jose; Pawlowsky-Glahn, Vera; Mateu-Figueras, Gloria; |
| 103 | + * Barcelo-Vidal, Carles (2003), "Isometric logratio transformations for |
| 104 | + * compositional data analysis", Mathematical Geology, 35 (3): 279–300, |
| 105 | + * doi:10.1023/A:1023818214614, S2CID 122844634 |
| 106 | + * |
| 107 | + * This implementation is closer to the description of the same using "pivot |
| 108 | + * coordinates" in |
| 109 | + * Filzmoser, P., Hron, K., Templ, M. (2018). Geometrical Properties of |
| 110 | + * Compositional Data. In: Applied Compositional Data Analysis. Springer Series |
| 111 | + * in Statistics. Springer, Cham. https://doi.org/10.1007/978-3-319-96422-5_3 |
| 112 | + * |
| 113 | + * This is a linear transform, with no Jacobian. |
| 114 | + * |
| 115 | + * @tparam Jacobian unused |
| 116 | + * @tparam Vec A type inheriting from `Eigen::DenseBase` or a `var_value` with |
| 117 | + * inner type inheriting from `Eigen::DenseBase` with compile time dynamic rows |
| 118 | + * and 1 column |
| 119 | + * @param[in] y free vector |
| 120 | + * @param[in, out] lp unused |
| 121 | + * @return Zero-sum vector of dimensionality one greater than `y` |
| 122 | + */ |
| 123 | +template <bool Jacobian, typename Vec, require_not_std_vector_t<Vec>* = nullptr> |
| 124 | +inline plain_type_t<Vec> sum_to_zero_constrain(const Vec& y, |
| 125 | + return_type_t<Vec>& lp) { |
| 126 | + return sum_to_zero_constrain(y); |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Return a vector with sum zero corresponding to the specified |
| 131 | + * free vector. |
| 132 | + * |
| 133 | + * The sum-to-zero transform is defined using a modified version of |
| 134 | + * the inverse of the isometric log ratio transform (ILR). |
| 135 | + * See: |
| 136 | + * Egozcue, Juan Jose; Pawlowsky-Glahn, Vera; Mateu-Figueras, Gloria; |
| 137 | + * Barcelo-Vidal, Carles (2003), "Isometric logratio transformations for |
| 138 | + * compositional data analysis", Mathematical Geology, 35 (3): 279–300, |
| 139 | + * doi:10.1023/A:1023818214614, S2CID 122844634 |
| 140 | + * |
| 141 | + * This implementation is closer to the description of the same using "pivot |
| 142 | + * coordinates" in |
| 143 | + * Filzmoser, P., Hron, K., Templ, M. (2018). Geometrical Properties of |
| 144 | + * Compositional Data. In: Applied Compositional Data Analysis. Springer Series |
| 145 | + * in Statistics. Springer, Cham. https://doi.org/10.1007/978-3-319-96422-5_3 |
| 146 | + * |
| 147 | + * This is a linear transform, with no Jacobian. |
| 148 | + * |
| 149 | + * @tparam Jacobian unused |
| 150 | + * @tparam Vec A standard vector with inner type inheriting from |
| 151 | + * `Eigen::DenseBase` or a `var_value` with inner type inheriting from |
| 152 | + * `Eigen::DenseBase` with compile time dynamic rows and 1 column |
| 153 | + * @param[in] y free vector |
| 154 | + * @param[in, out] lp unused |
| 155 | + * @return Zero-sum vectors of dimensionality one greater than `y` |
| 156 | + */ |
| 157 | +template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr> |
| 158 | +inline auto sum_to_zero_constrain(const T& y, return_type_t<T>& lp) { |
| 159 | + return apply_vector_unary<T>::apply( |
| 160 | + y, [](auto&& v) { return sum_to_zero_constrain(v); }); |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace math |
| 164 | +} // namespace stan |
| 165 | + |
| 166 | +#endif |
0 commit comments