Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds vectorized versions of the constrain functions #2580

Merged
merged 3 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
adds docs for each vectorized constrain function
  • Loading branch information
SteveBronder committed Sep 14, 2021
commit b6536cc9b5203090e2a38865eb4aa9dae987c6bc
2 changes: 1 addition & 1 deletion stan/math/prim/fun/corr_matrix_constrain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ inline auto corr_matrix_constrain(const T& x, Eigen::Index k,
*
* @tparam Jacobian if `true`, increment log density accumulator with log
* absolute Jacobian determinant of constraining transform
* @tparam T A type inheriting from `Eigen::DenseBase` or a `var_value` with
* @tparam T A standard vector with inner type inheriting from `Eigen::DenseBase` or a `var_value` with
* inner type inheriting from `Eigen::DenseBase` with compile time dynamic rows
* and 1 column
* @param x Vector of unconstrained partial correlations
Expand Down
18 changes: 18 additions & 0 deletions stan/math/prim/fun/cov_matrix_constrain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ inline auto cov_matrix_constrain(const T& x, Eigen::Index K, return_type_t<T>& l
}
}

/**
* Return the symmetric, positive-definite matrix of dimensions K by K resulting
* from transforming the specified finite vector of size K plus (K choose 2). If
* the `Jacobian` parameter is `true`, the log density accumulator is
* incremented with the log absolute Jacobian determinant of the transform. All
* of the transforms are specified with their Jacobians in the *Stan Reference
* Manual* chapter Constraint Transforms.
*
* @tparam Jacobian if `true`, increment log density accumulator with log
* absolute Jacobian determinant of constraining transform
* @tparam T A standard vector with inner type inheriting from `Eigen::DenseBase` or a `var_value` with
* inner type inheriting from `Eigen::DenseBase` with compile time dynamic rows
* and 1 column
* @param x The vector to convert to a covariance matrix
* @param K The dimensions of the resulting covariance matrix
* @param[in, out] lp log density accumulator
* @throws std::domain_error if (x.size() != K + (K choose 2)).
*/
template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
inline auto cov_matrix_constrain(const T& x, Eigen::Index K, return_type_t<T>& lp) {
return apply_vector_unary<T>::apply(x, [&lp, K](auto&& v) {
Expand Down
22 changes: 21 additions & 1 deletion stan/math/prim/fun/cov_matrix_constrain_lkj.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,30 @@ inline auto cov_matrix_constrain_lkj(const T& x, size_t k, return_type_t<T>& lp)
}
}

/**
* Return the covariance matrix of the specified dimensionality derived from
* constraining the specified vector of unconstrained values. If the `Jacobian`
* parameter is `true`, the log density accumulator is incremented with the log
* absolute Jacobian determinant of the transform. All of the transforms are
* specified with their Jacobians in the *Stan Reference Manual* chapter
* Constraint Transforms.
*
* @tparam Jacobian if `true`, increment log density accumulator with log
* absolute Jacobian determinant of constraining transform
* @tparam T A standard vector with inner type inheriting from `Eigen::DenseBase` or a `var_value` with
* inner type inheriting from `Eigen::DenseBase` with compile time rows or
* columns equal to 1
* @param x Input vector of unconstrained partial correlations and
* standard deviations
* @param k Dimensionality of returned covariance matrix
* @param[in, out] lp log density accumulator
* @return Covariance matrix derived from the unconstrained partial
* correlations and deviations.
*/
template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
inline auto cov_matrix_constrain_lkj(const T& x, size_t k, return_type_t<T>& lp) {
return apply_vector_unary<T>::apply(x, [&lp, k](auto&& v) {
return cov_matrix_constrain_lkj<Jacobian>(v, k, lp);
return cov_matrix_constrain_lkj<Jacobian>(v, k, lp);
});
}

Expand Down
18 changes: 18 additions & 0 deletions stan/math/prim/fun/ordered_constrain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ inline auto ordered_constrain(const T& x, return_type_t<T>& lp) {
}
}

/**
* Return a positive valued, increasing ordered vector derived from the
* specified free vector. The returned constrained vector will have the same
* dimensionality as the specified free vector. If the `Jacobian` parameter is
* `true`, the log density accumulator is incremented with the log absolute
* Jacobian determinant of the transform. All of the transforms are specified
* with their Jacobians in the *Stan Reference Manual* chapter Constraint
* Transforms.
*
* @tparam Jacobian if `true`, increment log density accumulator with log
* absolute Jacobian determinant of constraining transform
* @tparam T A standard vector with inner type inheriting from `Eigen::DenseBase` or a `var_value` with
* inner type inheriting from `Eigen::DenseBase` with compile time dynamic rows
* and 1 column
* @param x Free vector of scalars
* @param[in, out] lp log density accumulator
* @return Positive, increasing ordered vector.
*/
template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
inline auto ordered_constrain(const T& x, return_type_t<T>& lp) {
return apply_vector_unary<T>::apply(x, [&lp](auto&& v) {
Expand Down
15 changes: 15 additions & 0 deletions stan/math/prim/fun/positive_constrain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ inline auto positive_constrain(const T& x, return_type_t<T>& lp) {
}
}

/**
* Return the positive value for the specified unconstrained input. If the
* `Jacobian` parameter is `true`, the log density accumulator is incremented
* with the log absolute Jacobian determinant of the transform. All of the
* transforms are specified with their Jacobians in the *Stan Reference Manual*
* chapter Constraint Transforms.
*
* @tparam Jacobian if `true`, increment log density accumulator with log
* absolute Jacobian determinant of constraining transform
* @tparam T A standard vector with inner type inheriting from `Eigen::EigenBase`, a `var_value` with inner
* type inheriting from `Eigen::EigenBase`, a standard vector, or a scalar
* @param x unconstrained value or container
* @param[in, out] lp log density accumulator
* @return positive constrained version of unconstrained value(s)
*/
template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
inline auto positive_constrain(const T& x, return_type_t<T>& lp) {
return apply_vector_unary<T>::apply(x, [&lp](auto&& v) {
Expand Down
17 changes: 17 additions & 0 deletions stan/math/prim/fun/positive_ordered_constrain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ inline auto positive_ordered_constrain(const Vec& x, return_type_t<Vec>& lp) {
}
}

/**
* Return a positive valued, increasing positive ordered vector derived from the
* specified free vector. The returned constrained vector will have the same
* dimensionality as the specified free vector. If the `Jacobian` parameter is
* `true`, the log density accumulator is incremented with the log absolute
* Jacobian determinant of the transform. All of the transforms are specified
* with their Jacobians in the *Stan Reference Manual* chapter Constraint
* Transforms.
*
* @tparam Jacobian if `true`, increment log density accumulator with log
* absolute Jacobian determinant of constraining transform
* @tparam Vec A standard vector with inner type inheriting from `Eigen::EigenBase`, a `var_value` with
* inner type inheriting from `Eigen::EigenBase`
* @param x Free vector of scalars
* @param[in, out] lp log density accumulato
* @return Positive, increasing ordered vector
*/
template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
inline auto positive_ordered_constrain(const T& x, return_type_t<T>& lp) {
return apply_vector_unary<T>::apply(x, [&lp](auto&& v) {
Expand Down
16 changes: 16 additions & 0 deletions stan/math/prim/fun/simplex_constrain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ auto simplex_constrain(const Vec& y, return_type_t<Vec>& lp) {
}
}

/**
* Return the simplex corresponding to the specified free vector. If the
* `Jacobian` parameter is `true`, the log density accumulator is incremented
* with the log absolute Jacobian determinant of the transform. All of the
* transforms are specified with their Jacobians in the *Stan Reference Manual*
* chapter Constraint Transforms.
*
* @tparam Jacobian if `true`, increment log density accumulator with log
* absolute Jacobian determinant of constraining transform
* @tparam Vec A standard vector with inner type inheriting from `Eigen::DenseBase` or a `var_value` with
* inner type inheriting from `Eigen::DenseBase` with compile time dynamic rows
* and 1 column
* @param[in] y free vector
* @param[in, out] lp log density accumulator
* @return simplex of dimensionality one greater than `y`
*/
template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
inline auto simplex_constrain(const T& y, return_type_t<T>& lp) {
return apply_vector_unary<T>::apply(y, [&lp](auto&& v) {
Expand Down
16 changes: 16 additions & 0 deletions stan/math/prim/fun/unit_vector_constrain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ inline auto unit_vector_constrain(const T& y, return_type_t<T>& lp) {
}
}

/**
* Return the unit length vector corresponding to the free vector y. If the
* `Jacobian` parameter is `true`, the log density accumulator is incremented
* with the log absolute Jacobian determinant of the transform. All of the
* transforms are specified with their Jacobians in the *Stan Reference Manual*
* chapter Constraint Transforms.
*
* @tparam Jacobian if `true`, increment log density accumulator with log
* absolute Jacobian determinant of constraining transform
* @tparam T A standard vector with inner type inheriting from `Eigen::DenseBase` or a `var_value` with
* inner type inheriting from `Eigen::DenseBase` with compile time dynamic rows
* and 1 column
* @param y vector of K unrestricted variables
* @param[in, out] lp log density accumulator
* @return Unit length vector of dimension K
*/
template <bool Jacobian, typename T, require_std_vector_t<T>* = nullptr>
inline auto unit_vector_constrain(const T& y, return_type_t<T>& lp) {
return apply_vector_unary<T>::apply(y, [&lp](auto&& v) {
Expand Down