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

Move size_zero() calls after other consistency checks (L-Z) #1761

Merged
merged 3 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 35 additions & 4 deletions stan/math/prim/err/check_consistent_sizes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace math {
* Check if the dimension of x1 is consistent with x2.
* Consistent size is defined as having the same size if vector-like or
* being a scalar.
*
* @tparam T1 Type of x1
* @tparam T2 Type of x2
* @param function Function name (for error messages)
Expand All @@ -36,6 +37,7 @@ inline void check_consistent_sizes(const char* function, const char* name1,
* Check if the dimension of x1, x2, and x3 are consistent.
* Consistent size is defined as having the same size if vector-like or
* being a scalar.
*
* @tparam T1 Type of x1
* @tparam T2 Type of x2
* @tparam T3 Type of x3
Expand Down Expand Up @@ -66,10 +68,12 @@ inline void check_consistent_sizes(const char* function, const char* name1,
* Check if the dimension of x1, x2, x3, and x4 are consistent.
* Consistent size is defined as having the same size if
* vector-like or being a scalar.
*
* @tparam T1 Type of x1
* @tparam T2 Type of x2
* @tparam T3 Type of x3
* @tparam T4 Type of x4
*
* @param function Function name (for error messages)
* @param name1 Variable name (for error messages)
* @param x1 Variable to check for consistent size
Expand Down Expand Up @@ -97,6 +101,31 @@ inline void check_consistent_sizes(const char* function, const char* name1,
check_consistent_size(function, name3, x3, max_size);
check_consistent_size(function, name4, x4, max_size);
}

/**
* Check if the dimension of x1, x2, x3, x4 and x5 are consistent.
* Consistent size is defined as having the same size if
* vector-like or being a scalar.
*
* @tparam T1 Type of x1
* @tparam T2 Type of x2
* @tparam T3 Type of x3
* @tparam T4 Type of x4
* @tparam T5 Type of x5
*
* @param function Function name (for error messages)
* @param name1 Variable name (for error messages)
* @param x1 Variable to check for consistent size
* @param name2 Variable name (for error messages)
* @param x2 Variable to check for consistent size
* @param name3 Variable name (for error messages)
* @param x3 Variable to check for consistent size
* @param name4 Variable name (for error messages)
* @param x4 Variable to check for consistent size
* @param name5 Variable name (for error messages)
* @param x5 Variable to check for consistent size
* @throw <code>invalid_argument</code> if sizes are inconsistent
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[comment]
Not a request for this PR, but this should all be rewritten for arbitrary sizes using parameter packs. It'd also greatly simplify the docs.

template <typename T1, typename T2, typename T3, typename T4, typename T5>
inline void check_consistent_sizes(const char* function, const char* name1,
const T1& x1, const char* name2,
Expand All @@ -105,10 +134,12 @@ inline void check_consistent_sizes(const char* function, const char* name1,
const T4& x4, const char* name5,
const T5& x5) {
size_t max_size = std::max(
stan::math::size(x1),
std::max(stan::math::size(x2),
std::max(stan::math::size(x3),
std::max(stan::math::size(x4), stan::math::size(x5)))));
is_vector<T1>::value * stan::math::size(x1),
std::max(
is_vector<T2>::value * stan::math::size(x2),
std::max(is_vector<T3>::value * stan::math::size(x3),
std::max(is_vector<T4>::value * stan::math::size(x4),
is_vector<T5>::value * stan::math::size(x5)))));
check_consistent_size(function, name1, x1, max_size);
check_consistent_size(function, name2, x2, max_size);
check_consistent_size(function, name3, x3, max_size);
Expand Down
2 changes: 1 addition & 1 deletion stan/math/prim/prob/bernoulli_lccdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace math {
template <typename T_n, typename T_prob>
return_type_t<T_prob> bernoulli_lccdf(const T_n& n, const T_prob& theta) {
using T_partials_return = partials_return_t<T_n, T_prob>;
using std::log;
static const char* function = "bernoulli_lccdf";
check_finite(function, "Probability parameter", theta);
check_bounded(function, "Probability parameter", theta, 0.0, 1.0);
Expand All @@ -40,7 +41,6 @@ return_type_t<T_prob> bernoulli_lccdf(const T_n& n, const T_prob& theta) {
return 0.0;
}

using std::log;
T_partials_return P(0.0);
operands_and_partials<T_prob> ops_partials(theta);

Expand Down
2 changes: 1 addition & 1 deletion stan/math/prim/prob/bernoulli_lcdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace math {
template <typename T_n, typename T_prob>
return_type_t<T_prob> bernoulli_lcdf(const T_n& n, const T_prob& theta) {
using T_partials_return = partials_return_t<T_n, T_prob>;
using std::log;
static const char* function = "bernoulli_lcdf";
check_finite(function, "Probability parameter", theta);
check_bounded(function, "Probability parameter", theta, 0.0, 1.0);
Expand All @@ -40,7 +41,6 @@ return_type_t<T_prob> bernoulli_lcdf(const T_n& n, const T_prob& theta) {
return 0.0;
}

using std::log;
T_partials_return P(0.0);
operands_and_partials<T_prob> ops_partials(theta);

Expand Down
4 changes: 1 addition & 3 deletions stan/math/prim/prob/bernoulli_logit_glm_lpmf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,11 @@ template <bool propto, typename T_y, typename T_x_scalar, int T_x_rows,
return_type_t<T_x_scalar, T_alpha, T_beta> bernoulli_logit_glm_lpmf(
const T_y &y, const Eigen::Matrix<T_x_scalar, T_x_rows, Eigen::Dynamic> &x,
const T_alpha &alpha, const T_beta &beta) {
static const char *function = "bernoulli_logit_glm_lpmf";

using Eigen::Array;
using Eigen::Dynamic;
using Eigen::Matrix;
using Eigen::log1p;
using std::exp;

using T_partials_return = partials_return_t<T_y, T_x_scalar, T_alpha, T_beta>;
using T_y_val =
typename std::conditional_t<is_vector<T_y>::value,
Expand All @@ -69,6 +66,7 @@ return_type_t<T_x_scalar, T_alpha, T_beta> bernoulli_logit_glm_lpmf(
const size_t N_instances = T_x_rows == 1 ? stan::math::size(y) : x.rows();
const size_t N_attributes = x.cols();

static const char *function = "bernoulli_logit_glm_lpmf";
check_consistent_size(function, "Vector of dependent variables", y,
N_instances);
check_consistent_size(function, "Weight vector", beta, N_attributes);
Expand Down
3 changes: 1 addition & 2 deletions stan/math/prim/prob/bernoulli_logit_glm_rng.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ inline typename VectorBuilder<true, int, T_alpha>::type bernoulli_logit_glm_rng(
using boost::bernoulli_distribution;
using boost::variate_generator;

static const char *function = "bernoulli_logit_glm_rng";

const size_t N = x.row(0).size();
const size_t M = x.col(0).size();

static const char *function = "bernoulli_logit_glm_rng";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[optional]
Maybe we should sit down and scope out design for this. I'm happier with all the static constants being defined up top with the using statements. The reason is that they're both static and so don't execute anything at runtime. Putting them up front makes it easier to read the rest of the code that does get executed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I moved them closer to the consistency checks, which is the only place where they are used. The thing is that in this and the other glm functions, there are some constants to be computed before they can be used in the consistency checks...

check_finite(function, "Matrix of independent variables", x);
check_finite(function, "Weight vector", beta);
check_finite(function, "Intercept", alpha);
Expand Down
2 changes: 1 addition & 1 deletion stan/math/prim/prob/bernoulli_logit_lpmf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace math {
template <bool propto, typename T_n, typename T_prob>
return_type_t<T_prob> bernoulli_logit_lpmf(const T_n& n, const T_prob& theta) {
using T_partials_return = partials_return_t<T_n, T_prob>;
using std::exp;
static const char* function = "bernoulli_logit_lpmf";
check_bounded(function, "n", n, 0, 1);
check_not_nan(function, "Logit transformed probability parameter", theta);
Expand All @@ -41,7 +42,6 @@ return_type_t<T_prob> bernoulli_logit_lpmf(const T_n& n, const T_prob& theta) {
return 0.0;
}

using std::exp;
T_partials_return logp(0.0);
operands_and_partials<T_prob> ops_partials(theta);

Expand Down
1 change: 0 additions & 1 deletion stan/math/prim/prob/bernoulli_logit_rng.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ inline typename VectorBuilder<true, int, T_t>::type bernoulli_logit_rng(
const T_t& t, RNG& rng) {
using boost::bernoulli_distribution;
using boost::variate_generator;

check_finite("bernoulli_logit_rng", "Logit transformed probability parameter",
t);

Expand Down
2 changes: 1 addition & 1 deletion stan/math/prim/prob/bernoulli_lpmf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace math {
template <bool propto, typename T_n, typename T_prob>
return_type_t<T_prob> bernoulli_lpmf(const T_n& n, const T_prob& theta) {
using T_partials_return = partials_return_t<T_n, T_prob>;
using std::log;
static const char* function = "bernoulli_lpmf";
check_bounded(function, "n", n, 0, 1);
check_finite(function, "Probability parameter", theta);
Expand All @@ -43,7 +44,6 @@ return_type_t<T_prob> bernoulli_lpmf(const T_n& n, const T_prob& theta) {
return 0.0;
}

using std::log;
T_partials_return logp(0.0);
operands_and_partials<T_prob> ops_partials(theta);

Expand Down
2 changes: 1 addition & 1 deletion stan/math/prim/prob/beta_binomial_cdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ return_type_t<T_size1, T_size2> beta_binomial_cdf(const T_n& n, const T_N& N,
const T_size1& alpha,
const T_size2& beta) {
using T_partials_return = partials_return_t<T_n, T_N, T_size1, T_size2>;
using std::exp;
static const char* function = "beta_binomial_cdf";
check_nonnegative(function, "Population size parameter", N);
check_positive_finite(function, "First prior sample size parameter", alpha);
Expand All @@ -55,7 +56,6 @@ return_type_t<T_size1, T_size2> beta_binomial_cdf(const T_n& n, const T_N& N,
return 1.0;
}

using std::exp;
T_partials_return P(1.0);
operands_and_partials<T_size1, T_size2> ops_partials(alpha, beta);

Expand Down
4 changes: 2 additions & 2 deletions stan/math/prim/prob/beta_binomial_lccdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ return_type_t<T_size1, T_size2> beta_binomial_lccdf(const T_n& n, const T_N& N,
const T_size1& alpha,
const T_size2& beta) {
using T_partials_return = partials_return_t<T_n, T_N, T_size1, T_size2>;
using std::exp;
using std::log;
static const char* function = "beta_binomial_lccdf";
check_nonnegative(function, "Population size parameter", N);
check_positive_finite(function, "First prior sample size parameter", alpha);
Expand All @@ -56,8 +58,6 @@ return_type_t<T_size1, T_size2> beta_binomial_lccdf(const T_n& n, const T_N& N,
return 0;
}

using std::exp;
using std::log;
T_partials_return P(0.0);
operands_and_partials<T_size1, T_size2> ops_partials(alpha, beta);

Expand Down
4 changes: 2 additions & 2 deletions stan/math/prim/prob/beta_binomial_lcdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ return_type_t<T_size1, T_size2> beta_binomial_lcdf(const T_n& n, const T_N& N,
const T_size1& alpha,
const T_size2& beta) {
using T_partials_return = partials_return_t<T_n, T_N, T_size1, T_size2>;
using std::exp;
using std::log;
static const char* function = "beta_binomial_lcdf";
check_nonnegative(function, "Population size parameter", N);
check_positive_finite(function, "First prior sample size parameter", alpha);
Expand All @@ -56,8 +58,6 @@ return_type_t<T_size1, T_size2> beta_binomial_lcdf(const T_n& n, const T_N& N,
return 0;
}

using std::exp;
using std::log;
T_partials_return P(0.0);
operands_and_partials<T_size1, T_size2> ops_partials(alpha, beta);

Expand Down
6 changes: 3 additions & 3 deletions stan/math/prim/prob/beta_lccdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ template <typename T_y, typename T_scale_succ, typename T_scale_fail>
return_type_t<T_y, T_scale_succ, T_scale_fail> beta_lccdf(
const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) {
using T_partials_return = partials_return_t<T_y, T_scale_succ, T_scale_fail>;
using std::exp;
using std::log;
using std::pow;
static const char* function = "beta_lccdf";
check_positive_finite(function, "First shape parameter", alpha);
check_positive_finite(function, "Second shape parameter", beta);
Expand All @@ -53,9 +56,6 @@ return_type_t<T_y, T_scale_succ, T_scale_fail> beta_lccdf(
return 0;
}

using std::exp;
using std::log;
using std::pow;
T_partials_return ccdf_log(0.0);
operands_and_partials<T_y, T_scale_succ, T_scale_fail> ops_partials(y, alpha,
beta);
Expand Down
6 changes: 3 additions & 3 deletions stan/math/prim/prob/beta_lcdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ template <typename T_y, typename T_scale_succ, typename T_scale_fail>
return_type_t<T_y, T_scale_succ, T_scale_fail> beta_lcdf(
const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) {
using T_partials_return = partials_return_t<T_y, T_scale_succ, T_scale_fail>;
using std::exp;
using std::log;
using std::pow;
static const char* function = "beta_lcdf";
check_positive_finite(function, "First shape parameter", alpha);
check_positive_finite(function, "Second shape parameter", beta);
Expand All @@ -53,9 +56,6 @@ return_type_t<T_y, T_scale_succ, T_scale_fail> beta_lcdf(
return 0;
}

using std::exp;
using std::log;
using std::pow;
T_partials_return cdf_log(0.0);
operands_and_partials<T_y, T_scale_succ, T_scale_fail> ops_partials(y, alpha,
beta);
Expand Down
2 changes: 1 addition & 1 deletion stan/math/prim/prob/beta_lpdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ template <bool propto, typename T_y, typename T_scale_succ,
return_type_t<T_y, T_scale_succ, T_scale_fail> beta_lpdf(
const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) {
using T_partials_return = partials_return_t<T_y, T_scale_succ, T_scale_fail>;
using std::log;
static const char* function = "beta_lpdf";
check_positive_finite(function, "First shape parameter", alpha);
check_positive_finite(function, "Second shape parameter", beta);
Expand All @@ -58,7 +59,6 @@ return_type_t<T_y, T_scale_succ, T_scale_fail> beta_lpdf(
return 0;
}

using std::log;
T_partials_return logp(0);
operands_and_partials<T_y, T_scale_succ, T_scale_fail> ops_partials(y, alpha,
beta);
Expand Down
6 changes: 3 additions & 3 deletions stan/math/prim/prob/beta_proportion_lccdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ return_type_t<T_y, T_loc, T_prec> beta_proportion_lccdf(const T_y& y,
const T_loc& mu,
const T_prec& kappa) {
using T_partials_return = partials_return_t<T_y, T_loc, T_prec>;
using std::exp;
using std::log;
using std::pow;
static const char* function = "beta_proportion_lccdf";
check_positive(function, "Location parameter", mu);
check_less(function, "Location parameter", mu, 1.0);
Expand All @@ -57,9 +60,6 @@ return_type_t<T_y, T_loc, T_prec> beta_proportion_lccdf(const T_y& y,
return 0;
}

using std::exp;
using std::log;
using std::pow;
T_partials_return ccdf_log(0.0);
operands_and_partials<T_y, T_loc, T_prec> ops_partials(y, mu, kappa);

Expand Down
6 changes: 3 additions & 3 deletions stan/math/prim/prob/beta_proportion_lcdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ return_type_t<T_y, T_loc, T_prec> beta_proportion_lcdf(const T_y& y,
const T_loc& mu,
const T_prec& kappa) {
using T_partials_return = partials_return_t<T_y, T_loc, T_prec>;
using std::exp;
using std::log;
using std::pow;
static const char* function = "beta_proportion_lcdf";
check_positive(function, "Location parameter", mu);
check_less(function, "Location parameter", mu, 1.0);
Expand All @@ -58,9 +61,6 @@ return_type_t<T_y, T_loc, T_prec> beta_proportion_lcdf(const T_y& y,
return 0;
}

using std::exp;
using std::log;
using std::pow;
T_partials_return cdf_log(0.0);
operands_and_partials<T_y, T_loc, T_prec> ops_partials(y, mu, kappa);

Expand Down
8 changes: 4 additions & 4 deletions stan/math/prim/prob/beta_proportion_lpdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ template <bool propto, typename T_y, typename T_loc, typename T_prec>
return_type_t<T_y, T_loc, T_prec> beta_proportion_lpdf(const T_y& y,
const T_loc& mu,
const T_prec& kappa) {
static const char* function = "beta_proportion_lpdf";

using T_partials_return = partials_return_t<T_y, T_loc, T_prec>;
using std::log;
static const char* function = "beta_proportion_lpdf";
check_positive(function, "Location parameter", mu);
check_less(function, "Location parameter", mu, 1.0);
check_positive_finite(function, "Precision parameter", kappa);
Expand All @@ -55,13 +54,16 @@ return_type_t<T_y, T_loc, T_prec> beta_proportion_lpdf(const T_y& y,
check_less_or_equal(function, "Random variable", y, 1.0);
check_consistent_sizes(function, "Random variable", y, "Location parameter",
mu, "Precision parameter", kappa);

if (size_zero(y, mu, kappa)) {
return 0;
}
if (!include_summand<propto, T_y, T_loc, T_prec>::value) {
return 0;
}

T_partials_return logp(0);
operands_and_partials<T_y, T_loc, T_prec> ops_partials(y, mu, kappa);

scalar_seq_view<T_y> y_vec(y);
scalar_seq_view<T_loc> mu_vec(mu);
Expand All @@ -78,8 +80,6 @@ return_type_t<T_y, T_loc, T_prec> beta_proportion_lpdf(const T_y& y,
}
}

operands_and_partials<T_y, T_loc, T_prec> ops_partials(y, mu, kappa);

VectorBuilder<include_summand<propto, T_y, T_loc, T_prec>::value,
T_partials_return, T_y>
log_y(size_y);
Expand Down
4 changes: 2 additions & 2 deletions stan/math/prim/prob/binomial_cdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ template <typename T_n, typename T_N, typename T_prob>
return_type_t<T_prob> binomial_cdf(const T_n& n, const T_N& N,
const T_prob& theta) {
using T_partials_return = partials_return_t<T_n, T_N, T_prob>;
using std::exp;
using std::pow;
static const char* function = "binomial_cdf";
check_nonnegative(function, "Population size parameter", N);
check_finite(function, "Probability parameter", theta);
Expand All @@ -46,8 +48,6 @@ return_type_t<T_prob> binomial_cdf(const T_n& n, const T_N& N,
return 1.0;
}

using std::exp;
using std::pow;
T_partials_return P(1.0);
operands_and_partials<T_prob> ops_partials(theta);

Expand Down
Loading