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 lb/ub/lub_constrain specializations #2373

Merged
merged 91 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
1f822bc
update docs
SteveBronder Feb 17, 2021
9457aca
update headers
SteveBronder Feb 17, 2021
e9fa351
fix docs
SteveBronder Feb 17, 2021
72c484b
Merge commit 'cb21e28c79d5184f1687b349ef24aabbc0d7ae80' into HEAD
yashikno Feb 17, 2021
d0251d2
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 17, 2021
7946a9d
fix identity docs
SteveBronder Feb 17, 2021
b3d58ce
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 17, 2021
4f45643
fix identity_constrain docs
SteveBronder Feb 17, 2021
9cafb48
fix identity_constrain docs
SteveBronder Feb 17, 2021
a7e8ee5
remove error throwing for lb_constrain tests
SteveBronder Feb 17, 2021
54efb4b
Merge commit '9895be8ee0a27a9c7935cf19345d76fcbc85a69d' into HEAD
yashikno Feb 17, 2021
97650e7
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 17, 2021
b839292
update tests for vectors and get vectors of matrices working
SteveBronder Feb 17, 2021
f4e15ef
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 17, 2021
f61322e
fix lb_free tests
SteveBronder Feb 17, 2021
13dc7f4
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 17, 2021
f68b4f2
fixup var lp_constrain specialization
SteveBronder Feb 18, 2021
6e30071
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 18, 2021
4e3c98b
update to review comments
SteveBronder Feb 18, 2021
33b1979
Merge remote-tracking branch 'origin/develop' into feature/lb_constra…
SteveBronder Feb 18, 2021
4e13e87
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 18, 2021
53f1101
adds ub_constrain for matvar
SteveBronder Feb 19, 2021
131893c
update transform test
SteveBronder Feb 19, 2021
7246985
remove using std::exp
SteveBronder Feb 19, 2021
ef7cbdc
Merge commit '4e13e8761abead5b6366d43462add2a957da73af' into HEAD
yashikno Feb 19, 2021
3121317
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 19, 2021
0bac89a
typo in lb_constrain test
SteveBronder Feb 19, 2021
1a794b6
Added
bbbales2 Feb 19, 2021
87fd105
Updated matvar tests for lb_constrain
bbbales2 Feb 19, 2021
ae36dad
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 19, 2021
05216c8
Updated lower bound constrain tests and matvar test framework to work…
bbbales2 Feb 20, 2021
de67809
get most of lub working
SteveBronder Feb 20, 2021
ef93e6d
Add size checks to lb_constrain (Issue #1805)
bbbales2 Feb 20, 2021
4efe605
Added dimension checks to lb_constrain (Issue #1805)
bbbales2 Feb 20, 2021
2f2fd21
Removed debug prints (Issue #1805)
bbbales2 Feb 20, 2021
3b4f666
Removed lb_free tests temporarily (Issue #1805)
bbbales2 Feb 21, 2021
b3b4ea3
Updated initializations in lb_constrain tests (Issue #1805)
bbbales2 Feb 21, 2021
082e9f2
Updated matvar test tests (Issue #1805)
bbbales2 Feb 21, 2021
ddf5b64
cleanup all to use the .select() style for lub
SteveBronder Feb 21, 2021
a4abbe2
Merge remote-tracking branch 'origin/develop' into feature/lb_constra…
SteveBronder Feb 22, 2021
e820c6c
merge to feature/lb_constrain
SteveBronder Feb 22, 2021
e2e8035
Merge branch 'feature/ub_constrain-matvar' into feature/lub_constrain…
SteveBronder Feb 22, 2021
fa889aa
adds stdvec tests
SteveBronder Feb 22, 2021
92270f2
Merge remote-tracking branch 'origin/feature/lb_constrain-matvar' int…
bbbales2 Feb 22, 2021
ab230e6
Re-enabled free tests (Issue #1805)
bbbales2 Feb 22, 2021
558530e
Merge pull request #2382 from stan-dev/feature/lb_constrain-matvar_va…
SteveBronder Feb 22, 2021
842ac7d
Merge commit '0617772635c9f7c9fee0aa48de68b873052c68e2' into HEAD
yashikno Feb 22, 2021
2e18715
Renamed matvar convert test functions (Issue #1805)
bbbales2 Feb 22, 2021
532fd1b
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 22, 2021
137c04f
Merge branch 'feature/lb_constrain-matvar' of github.com:stan-dev/mat…
bbbales2 Feb 22, 2021
ec228d9
Merge branch 'feature/lb_constrain-matvar' into feature/ub_constrain-…
bbbales2 Feb 22, 2021
6aa6dab
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 22, 2021
57feb09
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 22, 2021
a221a40
update lb_free tests
SteveBronder Feb 22, 2021
b174101
Merge branch 'feature/lb_constrain-matvar' of github.com:stan-dev/mat…
SteveBronder Feb 22, 2021
a5d91c6
Merge branch 'feature/lb_constrain-matvar' into feature/ub_constrain-…
SteveBronder Feb 22, 2021
339dbe3
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 22, 2021
d6f653f
update ub_transform tests
SteveBronder Feb 22, 2021
18b745e
Merge branch 'feature/lb_constrain-matvar' into feature/ub_constrain-…
SteveBronder Feb 22, 2021
e9e327a
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 22, 2021
cf71ccb
Update upper bound constrain to match lower bound (Issue #1805)
bbbales2 Feb 22, 2021
2153cc0
update lub_constrain tests
SteveBronder Feb 22, 2021
fda25b1
Merge branch 'feature/ub_constrain-matvar' of github.com:stan-dev/mat…
bbbales2 Feb 22, 2021
8998906
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 22, 2021
fb7b549
Added ub_free comments (Issue #1805)
bbbales2 Feb 22, 2021
78b602f
Added lb_free comments (Issue #1805)
bbbales2 Feb 22, 2021
7aba52e
Merge branch 'feature/ub_constrain-matvar' of github.com:stan-dev/mat…
bbbales2 Feb 22, 2021
81b5332
Updated ub_free comments (Issue #1805)
bbbales2 Feb 22, 2021
f0fe5b2
update lub_transform tests
SteveBronder Feb 22, 2021
e931f7a
change prim ub_constrain's ub template parameter from L to U
SteveBronder Feb 22, 2021
aa91181
update ub_constrain template names
SteveBronder Feb 22, 2021
30fc7d2
Merge branch 'feature/ub_constrain-matvar' into feature/lub_constrain…
SteveBronder Feb 22, 2021
d77c968
Merge pull request #2379 from stan-dev/feature/ub_constrain-matvar
SteveBronder Feb 22, 2021
9e7dd41
Merge commit 'd77c96838668743760aff36321f6cf8a18eb348b' into HEAD
yashikno Feb 22, 2021
9508c41
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 22, 2021
7d4196a
update docs
SteveBronder Feb 22, 2021
dad89bb
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 22, 2021
d1a2a27
Simplified derivatives a bit, adding a couple more free tests (Issue …
bbbales2 Feb 23, 2021
0161eb5
Merge branch 'feature/lb_constrain-matvar' of github.com:stan-dev/mat…
bbbales2 Feb 23, 2021
43acdc9
Merge branch 'feature/lb_constrain-matvar' into feature/lub_constrain…
bbbales2 Feb 23, 2021
24cd21e
Added some comments to lub_free (Issue #1805)
bbbales2 Feb 23, 2021
88ec8b7
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 23, 2021
ab35b80
update ub_free exception test name
SteveBronder Feb 23, 2021
4d3e347
Merge pull request #2387 from stan-dev/feature/lub_constrain-matvar
SteveBronder Feb 23, 2021
b647d5e
breakout lub constrain tests more
SteveBronder Feb 24, 2021
790ab70
break lub tests more
SteveBronder Feb 24, 2021
82dcb8a
breakout multiply_log tests
SteveBronder Feb 24, 2021
abf6393
Merge commit '94e16d87d9cb464b4e05b8914f6cbbc8ea20ac0b' into HEAD
yashikno Feb 24, 2021
3fcebf6
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Feb 24, 2021
c397c8e
fix typo
SteveBronder Feb 24, 2021
5a6079a
Fixed initialization in constrain mix tests (Issue #1805)
bbbales2 Feb 24, 2021
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
35 changes: 8 additions & 27 deletions stan/math/prim/fun/identity_constrain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,18 @@ namespace math {

/**
* Returns the result of applying the identity constraint
* transform to the input.
* transform to the input. This promotes the input to the least upper
* bound of the input types.
*
* <p>This method is effectively a no-op and is mainly useful as a
* placeholder in auto-generated code.
*
* @tparam T Any type.
* @param[in] x object
* @return transformed input
*/
template <typename T>
inline auto identity_constrain(T&& x) {
return std::forward<T>(x);
}

/**
* Returns the result of applying the identity constraint
* transform to the input and increments the log probability
* reference with the log absolute Jacobian determinant.
*
* <p>This method is effectively a no-op and mainly useful as a
* placeholder in auto-generated code.
*
* @tparam T Any type
* @tparam S type of log probability
* @tparam T type of value to promote
* @tparam Types Other types.
* @param[in] x object
* @param[in] lp log density reference
* @return transformed input
*/
template <typename T, typename S>
inline auto identity_constrain(T&& x, S& lp) {
return std::forward<T>(x);
template <typename T, typename... Types,
require_all_not_var_matrix_t<T, Types...>* = nullptr>
inline auto identity_constrain(T&& x, Types&&... /* args */) {
return promote_scalar_t<return_type_t<T, Types...>, T>(x);
}

} // namespace math
Expand Down
17 changes: 9 additions & 8 deletions stan/math/prim/fun/identity_free.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ namespace math {

/**
* Returns the result of applying the inverse of the identity
* constraint transform to the input.
* constraint transform to the input. This promotes the input to the least upper
* bound of the input types.
*
* <p>This function is a no-op and mainly useful as a placeholder
* in auto-generated code.
*
* @tparam T type of value
* @param[in] y value
* @tparam T type of value to promote
* @tparam Types Other types.
* @param[in] x value to promote
* @return value
*/
template <typename T>
inline auto identity_free(T&& y) {
return std::forward<T>(y);
template <typename T, typename... Types,
require_all_not_var_matrix_t<T, Types...>* = nullptr>
inline auto identity_free(T&& x, Types&&... /* args */) {
return promote_scalar_t<return_type_t<T, Types...>, T>(x);
}

} // namespace math
Expand Down
197 changes: 180 additions & 17 deletions stan/math/prim/fun/lb_constrain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/err.hpp>
#include <stan/math/prim/fun/constants.hpp>
#include <stan/math/prim/fun/identity_constrain.hpp>
#include <stan/math/prim/fun/identity_free.hpp>
#include <stan/math/prim/fun/add.hpp>
#include <stan/math/prim/fun/exp.hpp>
#include <stan/math/prim/fun/eval.hpp>
#include <stan/math/prim/fun/sum.hpp>
#include <stan/math/prim/fun/value_of.hpp>
#include <cmath>
Expand All @@ -22,17 +26,20 @@ namespace math {
*
* <p>where \f$L\f$ is the constant lower bound.
*
* @tparam T type of Matrix
* @tparam L type of lower bound
* @param[in] x Unconstrained Matrix input
* @tparam T Scalar.
* @tparam L Scalar.
* @param[in] x Unconstrained input
* @param[in] lb Lower bound
* @return Constrained matrix
*/
template <typename T, typename L>
template <typename T, typename L, require_all_stan_scalar_t<T, L>* = nullptr,
require_all_not_st_var<T, L>* = nullptr>
inline auto lb_constrain(const T& x, const L& lb) {
auto& lb_ref = to_ref(lb);
check_finite("lb_constrain", "lb", value_of(lb_ref));
return eval(add(exp(x), lb_ref));
if (unlikely(value_of_rec(lb) == NEGATIVE_INFTY)) {
return identity_constrain(x, lb);
} else {
return add(exp(x), lb);
}
}

/**
Expand All @@ -41,21 +48,177 @@ inline auto lb_constrain(const T& x, const L& lb) {
* reference with the log absolute Jacobian determinant of the
* transform.
*
* @tparam T Type of Matrix
* @tparam L type of lower bound
* @tparam S type of log probability
* @param[in] x unconstrained Matrix input
* @tparam T Scalar.
* @tparam L Scalar.
* @param[in] x unconstrained input
* @param[in] lb lower bound on output
* @param[in,out] lp reference to log probability to increment
* @return lower-bound constrained value corresponding to inputs
*/
template <typename T, typename L>
template <typename T, typename L, require_all_stan_scalar_t<T, L>* = nullptr,
require_all_not_st_var<T, L>* = nullptr>
inline auto lb_constrain(const T& x, const L& lb, return_type_t<T, L>& lp) {
if (value_of_rec(lb) == NEGATIVE_INFTY) {
return identity_constrain(x, lb);
} else {
lp += x;
return add(exp(x), lb);
}
}

/**
* Specialization of `lb_constrain` to apply a scalar lower bound elementwise
* to each input.
*
* @tparam T A type inheriting from `EigenBase`.
* @tparam L Scalar.
* @param[in] x unconstrained input
* @param[in] lb lower bound on output
* @return lower-bound constrained value corresponding to inputs
*/
template <typename T, typename L, require_eigen_t<T>* = nullptr,
require_stan_scalar_t<L>* = nullptr,
require_all_not_st_var<T, L>* = nullptr>
inline auto lb_constrain(T&& x, L&& lb) {
return eval(x.unaryExpr([lb](auto&& x) { return lb_constrain(x, lb); }));
}

/**
* Specialization of `lb_constrain` to apply a scalar lower bound elementwise
* to each input.
*
* @tparam T A type inheriting from `EigenBase`.
* @tparam L Scalar.
* @param[in] x unconstrained input
* @param[in] lb lower bound on output
* @param[in,out] lp reference to log probability to increment
* @return lower-bound constrained value corresponding to inputs
*/
template <typename T, typename L, require_eigen_t<T>* = nullptr,
require_stan_scalar_t<L>* = nullptr,
require_all_not_st_var<T, L>* = nullptr>
inline auto lb_constrain(const T& x, const L& lb, return_type_t<T, L>& lp) {
return eval(
x.unaryExpr([lb, &lp](auto&& xx) { return lb_constrain(xx, lb, lp); }));
}

/**
* Specialization of `lb_constrain` to apply a matrix of lower bounds
* elementwise to each input element.
*
* @tparam T A type inheriting from `EigenBase`.
* @tparam L A type inheriting from `EigenBase`.
* @param[in] x unconstrained input
* @param[in] lb lower bound on output
* @return lower-bound constrained value corresponding to inputs
*/
template <typename T, typename L, require_all_eigen_t<T, L>* = nullptr,
require_all_not_st_var<T, L>* = nullptr>
inline auto lb_constrain(T&& x, L&& lb) {
check_matching_dims("lb_constrain", "x", x, "lb", lb);
return eval(x.binaryExpr(
lb, [](auto&& x, auto&& lb) { return lb_constrain(x, lb); }));
}

/**
* Specialization of `lb_constrain` to apply a matrix of lower bounds
* elementwise to each input element.
*
* @tparam T A type inheriting from `EigenBase`.
* @tparam L A type inheriting from `EigenBase`.
* @param[in] x unconstrained input
* @param[in] lb lower bound on output
* @param[in,out] lp reference to log probability to increment
* @return lower-bound constrained value corresponding to inputs
*/
template <typename T, typename L, require_all_eigen_t<T, L>* = nullptr,
require_all_not_st_var<T, L>* = nullptr>
inline auto lb_constrain(const T& x, const L& lb, return_type_t<T, L>& lp) {
auto& x_ref = to_ref(x);
auto& lb_ref = to_ref(lb);
check_finite("lb_constrain", "lb", value_of(lb_ref));
lp += sum(x_ref);
return eval(add(exp(x_ref), lb_ref));
check_matching_dims("lb_constrain", "x", x, "lb", lb);
return eval(x.binaryExpr(
lb, [&lp](auto&& xx, auto&& lbb) { return lb_constrain(xx, lbb, lp); }));
}

/**
* Specialization of `lb_constrain` to apply a container of lower bounds
* elementwise to each input element.
*
* @tparam T A Any type with a Scalar `scalar_type`.
* @tparam L A type inheriting from `EigenBase` or a scalar.
* @param[in] x unconstrained input
* @param[in] lb lower bound on output
* @return lower-bound constrained value corresponding to inputs
*/
template <typename T, typename L, require_not_std_vector_t<L>* = nullptr>
inline auto lb_constrain(const std::vector<T>& x, const L& lb) {
std::vector<plain_type_t<decltype(lb_constrain(x[0], lb))>> ret(x.size());
for (size_t i = 0; i < x.size(); ++i) {
ret[i] = lb_constrain(x[i], lb);
}
return ret;
}

/**
* Specialization of `lb_constrain` to apply a container of lower bounds
* elementwise to each input element.
*
* @tparam T A Any type with a Scalar `scalar_type`.
* @tparam L A type inheriting from `EigenBase` or a standard vector.
* @param[in] x unconstrained input
* @param[in] lb lower bound on output
* @param[in,out] lp reference to log probability to increment
* @return lower-bound constrained value corresponding to inputs
*/
template <typename T, typename L, require_not_std_vector_t<L>* = nullptr>
inline auto lb_constrain(const std::vector<T>& x, const L& lb,
return_type_t<T, L>& lp) {
std::vector<plain_type_t<decltype(lb_constrain(x[0], lb))>> ret(x.size());
for (size_t i = 0; i < x.size(); ++i) {
ret[i] = lb_constrain(x[i], lb, lp);
}
return ret;
}

/**
* Specialization of `lb_constrain` to apply a container of lower bounds
* elementwise to each input element.
*
* @tparam T A Any type with a Scalar `scalar_type`.
* @tparam L A type inheriting from `EigenBase` or a standard vector.
* @param[in] x unconstrained input
* @param[in] lb lower bound on output
* @return lower-bound constrained value corresponding to inputs
*/
template <typename T, typename L>
inline auto lb_constrain(const std::vector<T>& x, const std::vector<L>& lb) {
check_matching_dims("lb_constrain", "x", x, "lb", lb);
std::vector<plain_type_t<decltype(lb_constrain(x[0], lb[0]))>> ret(x.size());
for (size_t i = 0; i < x.size(); ++i) {
ret[i] = lb_constrain(x[i], lb[i]);
}
return ret;
}

/**
* Specialization of `lb_constrain` to apply a container of lower bounds
* elementwise to each input element.
*
* @tparam T A Any type with a Scalar `scalar_type`.
* @tparam L A type inheriting from `EigenBase` or a standard vector.
* @param[in] x unconstrained input
* @param[in] lb lower bound on output
* @param[in,out] lp reference to log probability to increment
* @return lower-bound constrained value corresponding to inputs
*/
template <typename T, typename L>
inline auto lb_constrain(const std::vector<T>& x, const std::vector<L>& lb,
return_type_t<T, L>& lp) {
check_matching_dims("lb_constrain", "x", x, "lb", lb);
std::vector<plain_type_t<decltype(lb_constrain(x[0], lb[0]))>> ret(x.size());
for (size_t i = 0; i < x.size(); ++i) {
ret[i] = lb_constrain(x[i], lb[i], lp);
}
return ret;
}

} // namespace math
Expand Down
Loading