From c9f049e0d742629bbc776aafe4345a1075d93290 Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Thu, 5 Mar 2020 16:45:08 +0000 Subject: [PATCH 1/3] Move size_zero() calls after other consistency checks --- .../math/prim/prob/lkj_corr_cholesky_lpdf.hpp | 6 +-- stan/math/prim/prob/lkj_corr_cholesky_rng.hpp | 1 - stan/math/prim/prob/lkj_cov_lpdf.hpp | 8 ++-- stan/math/prim/prob/logistic_cdf.hpp | 20 ++++---- stan/math/prim/prob/logistic_lccdf.hpp | 20 ++++---- stan/math/prim/prob/logistic_lcdf.hpp | 20 ++++---- stan/math/prim/prob/logistic_lpdf.hpp | 14 ++---- stan/math/prim/prob/logistic_rng.hpp | 5 +- stan/math/prim/prob/lognormal_cdf.hpp | 16 +++---- stan/math/prim/prob/lognormal_lccdf.hpp | 15 +++--- stan/math/prim/prob/lognormal_lcdf.hpp | 15 +++--- stan/math/prim/prob/lognormal_lpdf.hpp | 10 ++-- stan/math/prim/prob/lognormal_rng.hpp | 6 +-- .../prim/prob/matrix_normal_prec_lpdf.hpp | 14 +++--- .../math/prim/prob/matrix_normal_prec_rng.hpp | 5 -- .../math/prim/prob/multi_gp_cholesky_lpdf.hpp | 3 +- stan/math/prim/prob/multi_gp_lpdf.hpp | 13 +++--- stan/math/prim/prob/multi_normal_lpdf.hpp | 4 +- .../math/prim/prob/multi_normal_prec_lpdf.hpp | 8 ++-- stan/math/prim/prob/multi_normal_prec_rng.hpp | 2 - stan/math/prim/prob/multi_normal_rng.hpp | 2 - stan/math/prim/prob/multi_student_t_lpdf.hpp | 10 ++-- stan/math/prim/prob/multi_student_t_rng.hpp | 1 - stan/math/prim/prob/multinomial_lpmf.hpp | 4 +- stan/math/prim/prob/multinomial_rng.hpp | 1 - stan/math/prim/prob/neg_binomial_2_cdf.hpp | 17 ++++--- stan/math/prim/prob/neg_binomial_2_lccdf.hpp | 8 ++-- stan/math/prim/prob/neg_binomial_2_lcdf.hpp | 9 ++-- .../prim/prob/neg_binomial_2_log_glm_lpmf.hpp | 6 +-- .../prim/prob/neg_binomial_2_log_lpmf.hpp | 18 +++----- .../math/prim/prob/neg_binomial_2_log_rng.hpp | 2 - stan/math/prim/prob/neg_binomial_2_lpmf.hpp | 16 +++---- stan/math/prim/prob/neg_binomial_2_rng.hpp | 6 +-- stan/math/prim/prob/neg_binomial_cdf.hpp | 14 +++--- stan/math/prim/prob/neg_binomial_lccdf.hpp | 19 ++++---- stan/math/prim/prob/neg_binomial_lcdf.hpp | 19 ++++---- stan/math/prim/prob/neg_binomial_lpmf.hpp | 16 +++---- stan/math/prim/prob/neg_binomial_rng.hpp | 7 +-- stan/math/prim/prob/normal_cdf.hpp | 21 ++++----- stan/math/prim/prob/normal_id_glm_lpdf.hpp | 7 ++- stan/math/prim/prob/normal_lccdf.hpp | 14 +++--- stan/math/prim/prob/normal_lcdf.hpp | 13 +++--- stan/math/prim/prob/normal_lpdf.hpp | 25 +++++----- stan/math/prim/prob/normal_rng.hpp | 5 +- .../math/prim/prob/normal_sufficient_lpdf.hpp | 32 +++++-------- .../prim/prob/ordered_logistic_glm_lpmf.hpp | 5 +- stan/math/prim/prob/ordered_logistic_rng.hpp | 2 - stan/math/prim/prob/ordered_probit_lpmf.hpp | 23 +++------- stan/math/prim/prob/ordered_probit_rng.hpp | 1 - stan/math/prim/prob/pareto_cdf.hpp | 20 ++++---- stan/math/prim/prob/pareto_lccdf.hpp | 20 ++++---- stan/math/prim/prob/pareto_lcdf.hpp | 20 ++++---- stan/math/prim/prob/pareto_lpdf.hpp | 7 ++- stan/math/prim/prob/pareto_rng.hpp | 6 +-- stan/math/prim/prob/pareto_type_2_cdf.hpp | 21 ++++----- stan/math/prim/prob/pareto_type_2_lccdf.hpp | 21 ++++----- stan/math/prim/prob/pareto_type_2_lcdf.hpp | 21 ++++----- stan/math/prim/prob/pareto_type_2_lpdf.hpp | 20 ++++---- stan/math/prim/prob/pareto_type_2_rng.hpp | 8 ++-- stan/math/prim/prob/poisson_cdf.hpp | 17 +++---- stan/math/prim/prob/poisson_lccdf.hpp | 17 +++---- stan/math/prim/prob/poisson_lcdf.hpp | 17 +++---- stan/math/prim/prob/poisson_log_glm_lpmf.hpp | 4 +- stan/math/prim/prob/poisson_log_lpmf.hpp | 19 +++----- stan/math/prim/prob/poisson_log_rng.hpp | 2 - stan/math/prim/prob/poisson_lpmf.hpp | 16 +++---- stan/math/prim/prob/poisson_rng.hpp | 2 - stan/math/prim/prob/rayleigh_cdf.hpp | 15 +++--- stan/math/prim/prob/rayleigh_lccdf.hpp | 14 +++--- stan/math/prim/prob/rayleigh_lcdf.hpp | 15 +++--- stan/math/prim/prob/rayleigh_lpdf.hpp | 14 ++---- stan/math/prim/prob/rayleigh_rng.hpp | 2 - .../prim/prob/scaled_inv_chi_square_cdf.hpp | 26 +++++------ .../prim/prob/scaled_inv_chi_square_lccdf.hpp | 24 ++++------ .../prim/prob/scaled_inv_chi_square_lcdf.hpp | 24 ++++------ .../prim/prob/scaled_inv_chi_square_lpdf.hpp | 16 ++++--- .../prim/prob/scaled_inv_chi_square_rng.hpp | 6 +-- stan/math/prim/prob/skew_normal_cdf.hpp | 18 +++----- stan/math/prim/prob/skew_normal_lccdf.hpp | 20 ++++---- stan/math/prim/prob/skew_normal_lcdf.hpp | 20 ++++---- stan/math/prim/prob/skew_normal_lpdf.hpp | 15 ++---- stan/math/prim/prob/skew_normal_rng.hpp | 8 ++-- stan/math/prim/prob/std_normal_cdf.hpp | 9 ++-- stan/math/prim/prob/std_normal_lccdf.hpp | 9 ++-- stan/math/prim/prob/std_normal_lcdf.hpp | 8 ++-- stan/math/prim/prob/std_normal_lpdf.hpp | 9 ++-- stan/math/prim/prob/student_t_cdf.hpp | 23 ++++------ stan/math/prim/prob/student_t_lccdf.hpp | 25 ++++------ stan/math/prim/prob/student_t_lcdf.hpp | 25 ++++------ stan/math/prim/prob/student_t_lpdf.hpp | 29 ++++++------ stan/math/prim/prob/student_t_rng.hpp | 8 ++-- stan/math/prim/prob/uniform_cdf.hpp | 16 +++---- stan/math/prim/prob/uniform_lccdf.hpp | 17 ++++--- stan/math/prim/prob/uniform_lcdf.hpp | 18 ++++---- stan/math/prim/prob/uniform_lpdf.hpp | 22 ++++----- stan/math/prim/prob/uniform_rng.hpp | 6 +-- stan/math/prim/prob/von_mises_lpdf.hpp | 33 ++++++------- stan/math/prim/prob/von_mises_rng.hpp | 1 - stan/math/prim/prob/weibull_cdf.hpp | 11 ++--- stan/math/prim/prob/weibull_lccdf.hpp | 11 ++--- stan/math/prim/prob/weibull_lcdf.hpp | 11 ++--- stan/math/prim/prob/weibull_lpdf.hpp | 8 ++-- stan/math/prim/prob/weibull_rng.hpp | 6 +-- stan/math/prim/prob/wiener_lpdf.hpp | 46 ++++++++++--------- stan/math/prim/prob/wishart_lpdf.hpp | 15 +++--- stan/math/prim/prob/wishart_rng.hpp | 4 +- 106 files changed, 571 insertions(+), 822 deletions(-) diff --git a/stan/math/prim/prob/lkj_corr_cholesky_lpdf.hpp b/stan/math/prim/prob/lkj_corr_cholesky_lpdf.hpp index 14cd643813f..82f68ea5184 100644 --- a/stan/math/prim/prob/lkj_corr_cholesky_lpdf.hpp +++ b/stan/math/prim/prob/lkj_corr_cholesky_lpdf.hpp @@ -17,10 +17,8 @@ template return_type_t lkj_corr_cholesky_lpdf( const Eigen::Matrix& L, const T_shape& eta) { - static const char* function = "lkj_corr_cholesky_lpdf"; - using lp_ret = return_type_t; - lp_ret lp(0.0); + static const char* function = "lkj_corr_cholesky_lpdf"; check_positive(function, "Shape parameter", eta); check_lower_triangular(function, "Random variable", L); @@ -29,6 +27,8 @@ return_type_t lkj_corr_cholesky_lpdf( return 0.0; } + lp_ret lp(0.0); + if (include_summand::value) { lp += do_lkj_constant(eta, K); } diff --git a/stan/math/prim/prob/lkj_corr_cholesky_rng.hpp b/stan/math/prim/prob/lkj_corr_cholesky_rng.hpp index 6caeb8247bc..9cfa57c5981 100644 --- a/stan/math/prim/prob/lkj_corr_cholesky_rng.hpp +++ b/stan/math/prim/prob/lkj_corr_cholesky_rng.hpp @@ -12,7 +12,6 @@ namespace math { template inline Eigen::MatrixXd lkj_corr_cholesky_rng(size_t K, double eta, RNG& rng) { static const char* function = "lkj_corr_cholesky_rng"; - check_positive(function, "Shape parameter", eta); Eigen::ArrayXd CPCs((K * (K - 1)) / 2); diff --git a/stan/math/prim/prob/lkj_cov_lpdf.hpp b/stan/math/prim/prob/lkj_cov_lpdf.hpp index ee2546814f1..3f7702f8d2f 100644 --- a/stan/math/prim/prob/lkj_cov_lpdf.hpp +++ b/stan/math/prim/prob/lkj_cov_lpdf.hpp @@ -19,8 +19,6 @@ return_type_t lkj_cov_lpdf( const Eigen::Matrix& sigma, const T_shape& eta) { static const char* function = "lkj_cov_lpdf"; - - return_type_t lp(0.0); check_size_match(function, "Rows of location parameter", mu.rows(), "columns of scale parameter", sigma.rows()); check_square(function, "random variable", y); @@ -31,6 +29,8 @@ return_type_t lkj_cov_lpdf( check_finite(function, "Scale parameter", sigma); check_finite(function, "Covariance matrix", y); + return_type_t lp(0.0); + const unsigned int K = y.rows(); const Eigen::Array sds = y.diagonal().array().sqrt(); for (unsigned int k = 0; k < K; k++) { @@ -65,13 +65,13 @@ return_type_t lkj_cov_lpdf( const Eigen::Matrix& y, const T_loc& mu, const T_scale& sigma, const T_shape& eta) { static const char* function = "lkj_cov_lpdf"; - - return_type_t lp(0.0); check_positive(function, "Shape parameter", eta); check_finite(function, "Location parameter", mu); check_finite(function, "Scale parameter", sigma); check_finite(function, "Covariance matrix", y); + return_type_t lp(0.0); + const unsigned int K = y.rows(); const Eigen::Array sds = y.diagonal().array().sqrt(); for (unsigned int k = 0; k < K; k++) { diff --git a/stan/math/prim/prob/logistic_cdf.hpp b/stan/math/prim/prob/logistic_cdf.hpp index 0083e4490d1..4ea79f35620 100644 --- a/stan/math/prim/prob/logistic_cdf.hpp +++ b/stan/math/prim/prob/logistic_cdf.hpp @@ -20,30 +20,26 @@ template return_type_t logistic_cdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { using T_partials_return = partials_return_t; - - if (size_zero(y, mu, sigma)) { - return 1.0; - } - - static const char* function = "logistic_cdf"; - using std::exp; - - T_partials_return P(1.0); - + static const char* function = "logistic_cdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma); + if (size_zero(y, mu, sigma)) { + return 1.0; + } + + T_partials_return P(1.0); + operands_and_partials ops_partials(y, mu, sigma); + scalar_seq_view y_vec(y); scalar_seq_view mu_vec(mu); scalar_seq_view sigma_vec(sigma); size_t N = max_size(y, mu, sigma); - operands_and_partials ops_partials(y, mu, sigma); - // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::math::size(y); i++) { diff --git a/stan/math/prim/prob/logistic_lccdf.hpp b/stan/math/prim/prob/logistic_lccdf.hpp index b58b0add66c..c0dc6862502 100644 --- a/stan/math/prim/prob/logistic_lccdf.hpp +++ b/stan/math/prim/prob/logistic_lccdf.hpp @@ -20,31 +20,27 @@ template return_type_t logistic_lccdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { using T_partials_return = partials_return_t; - - if (size_zero(y, mu, sigma)) { - return 0.0; - } - - static const char* function = "logistic_lccdf"; - using std::exp; using std::log; - - T_partials_return P(0.0); - + static const char* function = "logistic_lccdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma); + if (size_zero(y, mu, sigma)) { + return 0; + } + + T_partials_return P(0.0); + operands_and_partials ops_partials(y, mu, sigma); + scalar_seq_view y_vec(y); scalar_seq_view mu_vec(mu); scalar_seq_view sigma_vec(sigma); size_t N = max_size(y, mu, sigma); - operands_and_partials ops_partials(y, mu, sigma); - // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::math::size(y); i++) { diff --git a/stan/math/prim/prob/logistic_lcdf.hpp b/stan/math/prim/prob/logistic_lcdf.hpp index a7b055a0250..1cd38af8d74 100644 --- a/stan/math/prim/prob/logistic_lcdf.hpp +++ b/stan/math/prim/prob/logistic_lcdf.hpp @@ -20,31 +20,27 @@ template return_type_t logistic_lcdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { using T_partials_return = partials_return_t; - - if (size_zero(y, mu, sigma)) { - return 0.0; - } - - static const char* function = "logistic_lcdf"; - using std::exp; using std::log; - - T_partials_return P(0.0); - + static const char* function = "logistic_lcdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma); + if (size_zero(y, mu, sigma)) { + return 0; + } + + T_partials_return P(0.0); + operands_and_partials ops_partials(y, mu, sigma); + scalar_seq_view y_vec(y); scalar_seq_view mu_vec(mu); scalar_seq_view sigma_vec(sigma); size_t N = max_size(y, mu, sigma); - operands_and_partials ops_partials(y, mu, sigma); - // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::math::size(y); i++) { diff --git a/stan/math/prim/prob/logistic_lpdf.hpp b/stan/math/prim/prob/logistic_lpdf.hpp index 6a6e0211495..81d5c4b89c0 100644 --- a/stan/math/prim/prob/logistic_lpdf.hpp +++ b/stan/math/prim/prob/logistic_lpdf.hpp @@ -19,28 +19,24 @@ namespace math { template return_type_t logistic_lpdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { - static const char* function = "logistic_lpdf"; using T_partials_return = partials_return_t; - using std::exp; using std::log; - - if (size_zero(y, mu, sigma)) { - return 0.0; - } - - T_partials_return logp(0.0); - + static const char* function = "logistic_lpdf"; check_finite(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma); + if (size_zero(y, mu, sigma)) { + return 0.0; + } if (!include_summand::value) { return 0.0; } + T_partials_return logp(0.0); operands_and_partials ops_partials(y, mu, sigma); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/logistic_rng.hpp b/stan/math/prim/prob/logistic_rng.hpp index d21fa100da4..c87fec1ccf7 100644 --- a/stan/math/prim/prob/logistic_rng.hpp +++ b/stan/math/prim/prob/logistic_rng.hpp @@ -18,8 +18,8 @@ namespace math { * mu and sigma can each be a scalar or a one-dimensional container. Any * non-scalar inputs must be the same size. * - * @tparam T_loc Type of location parameter - * @tparam T_scale Type of scale parameter + * @tparam T_loc type of location parameter + * @tparam T_scale type of scale parameter * @tparam RNG type of random number generator * @param mu (Sequence of) location parameter(s) * @param sigma (Sequence of) scale parameter(s) @@ -35,7 +35,6 @@ inline typename VectorBuilder::type logistic_rng( using boost::random::exponential_distribution; using boost::variate_generator; static const char* function = "logistic_rng"; - check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); check_consistent_sizes(function, "Location parameter", mu, "Scale Parameter", diff --git a/stan/math/prim/prob/lognormal_cdf.hpp b/stan/math/prim/prob/lognormal_cdf.hpp index 5ae26d2f684..5515b980eea 100644 --- a/stan/math/prim/prob/lognormal_cdf.hpp +++ b/stan/math/prim/prob/lognormal_cdf.hpp @@ -18,24 +18,20 @@ namespace math { template return_type_t lognormal_cdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { - static const char* function = "lognormal_cdf"; - using T_partials_return = partials_return_t; - - T_partials_return cdf = 1.0; - using std::exp; using std::log; - - if (size_zero(y, mu, sigma)) { - return cdf; - } - + static const char* function = "lognormal_cdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); + if (size_zero(y, mu, sigma)) { + return 1.0; + } + + T_partials_return cdf = 1.0; operands_and_partials ops_partials(y, mu, sigma); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/lognormal_lccdf.hpp b/stan/math/prim/prob/lognormal_lccdf.hpp index 81cd8a4ccbe..675a16b3279 100644 --- a/stan/math/prim/prob/lognormal_lccdf.hpp +++ b/stan/math/prim/prob/lognormal_lccdf.hpp @@ -20,23 +20,20 @@ template return_type_t lognormal_lccdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { - static const char* function = "lognormal_lccdf"; using T_partials_return = partials_return_t; - - T_partials_return ccdf_log = 0.0; - using std::exp; using std::log; - - if (size_zero(y, mu, sigma)) { - return ccdf_log; - } - + static const char* function = "lognormal_lccdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); + if (size_zero(y, mu, sigma)) { + return 0; + } + + T_partials_return ccdf_log = 0.0; operands_and_partials ops_partials(y, mu, sigma); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/lognormal_lcdf.hpp b/stan/math/prim/prob/lognormal_lcdf.hpp index 852ebe87a71..4a663f01aa0 100644 --- a/stan/math/prim/prob/lognormal_lcdf.hpp +++ b/stan/math/prim/prob/lognormal_lcdf.hpp @@ -19,23 +19,20 @@ namespace math { template return_type_t lognormal_lcdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { - static const char* function = "lognormal_lcdf"; using T_partials_return = partials_return_t; - - T_partials_return cdf_log = 0.0; - using std::exp; using std::log; - - if (size_zero(y, mu, sigma)) { - return cdf_log; - } - + static const char* function = "lognormal_lcdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); + if (size_zero(y, mu, sigma)) { + return 0; + } + + T_partials_return cdf_log = 0.0; operands_and_partials ops_partials(y, mu, sigma); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/lognormal_lpdf.hpp b/stan/math/prim/prob/lognormal_lpdf.hpp index 155849614ae..6fb1d400dc7 100644 --- a/stan/math/prim/prob/lognormal_lpdf.hpp +++ b/stan/math/prim/prob/lognormal_lpdf.hpp @@ -18,20 +18,22 @@ namespace math { template return_type_t lognormal_lpdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { - static const char* function = "lognormal_lpdf"; using T_partials_return = partials_return_t; - + using std::log; + static const char* function = "lognormal_lpdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma); + if (size_zero(y, mu, sigma)) { return 0; } T_partials_return logp(0); + operands_and_partials ops_partials(y, mu, sigma); scalar_seq_view y_vec(y); scalar_seq_view mu_vec(mu); @@ -44,10 +46,6 @@ return_type_t lognormal_lpdf(const T_y& y, const T_loc& mu, } } - operands_and_partials ops_partials(y, mu, sigma); - - using std::log; - VectorBuilder::value, T_partials_return, T_scale> log_sigma(size(sigma)); diff --git a/stan/math/prim/prob/lognormal_rng.hpp b/stan/math/prim/prob/lognormal_rng.hpp index 5c80b616902..8985de27936 100644 --- a/stan/math/prim/prob/lognormal_rng.hpp +++ b/stan/math/prim/prob/lognormal_rng.hpp @@ -17,8 +17,8 @@ namespace math { * mu and sigma can each be a scalar or a one-dimensional container. Any * non-scalar inputs must be the same size. * - * @tparam T_loc Type of location parameter - * @tparam T_scale Type of scale parameter + * @tparam T_loc type of location parameter + * @tparam T_scale type of scale parameter * @tparam RNG type of random number generator * @param mu (Sequence of) location parameter(s) * @param sigma (Sequence of) positive scale parameter(s) @@ -33,9 +33,7 @@ inline typename VectorBuilder::type lognormal_rng( const T_loc& mu, const T_scale& sigma, RNG& rng) { using boost::random::lognormal_distribution; using boost::variate_generator; - static const char* function = "lognormal_rng"; - check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); check_consistent_sizes(function, "Location parameter", mu, "Scale Parameter", diff --git a/stan/math/prim/prob/matrix_normal_prec_lpdf.hpp b/stan/math/prim/prob/matrix_normal_prec_lpdf.hpp index ef5f2c05a0d..7321212a049 100644 --- a/stan/math/prim/prob/matrix_normal_prec_lpdf.hpp +++ b/stan/math/prim/prob/matrix_normal_prec_lpdf.hpp @@ -10,10 +10,16 @@ namespace stan { namespace math { + /** \ingroup multivar_dists * The log of the matrix normal density for the given y, mu, Sigma and D * where Sigma and D are given as precision matrices, not covariance matrices. * + * @tparam T_y type of scalar + * @tparam T_Mu type of location + * @tparam T_Sigma type of Sigma + * @tparam T_D type of D + * * @param y An mxn matrix. * @param Mu The mean matrix. * @param Sigma The mxm inverse covariance matrix (i.e., the precision matrix) @@ -23,10 +29,6 @@ namespace math { * @return The log of the matrix normal density. * @throw std::domain_error if Sigma or D are not square, not symmetric, * or not semi-positive definite. - * @tparam T_y Type of scalar. - * @tparam T_Mu Type of location. - * @tparam T_Sigma Type of Sigma. - * @tparam T_D Type of D. */ template @@ -36,8 +38,6 @@ return_type_t matrix_normal_prec_lpdf( const Eigen::Matrix& Sigma, const Eigen::Matrix& D) { static const char* function = "matrix_normal_prec_lpdf"; - return_type_t lp(0.0); - check_positive(function, "Sigma rows", Sigma.rows()); check_finite(function, "Sigma", Sigma); check_symmetric(function, "Sigma", Sigma); @@ -61,6 +61,8 @@ return_type_t matrix_normal_prec_lpdf( check_finite(function, "Location parameter", Mu); check_finite(function, "Random variable", y); + return_type_t lp(0.0); + if (include_summand::value) { lp += NEG_LOG_SQRT_TWO_PI * y.cols() * y.rows(); } diff --git a/stan/math/prim/prob/matrix_normal_prec_rng.hpp b/stan/math/prim/prob/matrix_normal_prec_rng.hpp index 305dbd1fb07..8b814a33a68 100644 --- a/stan/math/prim/prob/matrix_normal_prec_rng.hpp +++ b/stan/math/prim/prob/matrix_normal_prec_rng.hpp @@ -35,22 +35,17 @@ inline Eigen::MatrixXd matrix_normal_prec_rng(const Eigen::MatrixXd &Mu, RNG &rng) { using boost::normal_distribution; using boost::variate_generator; - static const char *function = "matrix_normal_prec_rng"; - check_positive(function, "Sigma rows", Sigma.rows()); check_finite(function, "Sigma", Sigma); check_symmetric(function, "Sigma", Sigma); - check_positive(function, "D rows", D.rows()); check_finite(function, "D", D); check_symmetric(function, "D", D); - check_size_match(function, "Rows of location parameter", Mu.rows(), "Rows of Sigma", Sigma.rows()); check_size_match(function, "Columns of location parameter", Mu.cols(), "Rows of D", D.rows()); - check_finite(function, "Location parameter", Mu); Eigen::LDLT Sigma_ldlt(Sigma); diff --git a/stan/math/prim/prob/multi_gp_cholesky_lpdf.hpp b/stan/math/prim/prob/multi_gp_cholesky_lpdf.hpp index 64bb586123c..71c87664d63 100644 --- a/stan/math/prim/prob/multi_gp_cholesky_lpdf.hpp +++ b/stan/math/prim/prob/multi_gp_cholesky_lpdf.hpp @@ -39,9 +39,8 @@ return_type_t multi_gp_cholesky_lpdf( const Eigen::Matrix& y, const Eigen::Matrix& L, const Eigen::Matrix& w) { - static const char* function = "multi_gp_cholesky_lpdf"; using T_lp = return_type_t; - + static const char* function = "multi_gp_cholesky_lpdf"; check_size_match(function, "Size of random variable (rows y)", y.rows(), "Size of kernel scales (w)", w.size()); check_size_match(function, "Size of random variable", y.cols(), diff --git a/stan/math/prim/prob/multi_gp_lpdf.hpp b/stan/math/prim/prob/multi_gp_lpdf.hpp index 3f3012a3ef3..786554f874e 100644 --- a/stan/math/prim/prob/multi_gp_lpdf.hpp +++ b/stan/math/prim/prob/multi_gp_lpdf.hpp @@ -10,6 +10,7 @@ namespace stan { namespace math { + /** \ingroup multivar_dists * The log of a multivariate Gaussian Process for the given y, Sigma, and * w. y is a dxN matrix, where each column is a different observation and each @@ -18,6 +19,9 @@ namespace math { * This distribution is equivalent to: * for (i in 1:d) row(y, i) ~ multi_normal(0, (1/w[i])*Sigma). * + * @tparam T_y type of scalar + * @tparam T_covar type of kernel + * @tparam T_w type of weight * @param y A dxN matrix * @param Sigma The NxN kernel matrix * @param w A d-dimensional vector of positive inverse scale parameters for each @@ -25,19 +29,14 @@ namespace math { * @return The log of the multivariate GP density. * @throw std::domain_error if Sigma is not square, not symmetric, * or not semi-positive definite. - * @tparam T_y Type of scalar. - * @tparam T_covar Type of kernel. - * @tparam T_w Type of weight. */ template return_type_t multi_gp_lpdf( const Eigen::Matrix& y, const Eigen::Matrix& Sigma, const Eigen::Matrix& w) { - static const char* function = "multi_gp_lpdf"; using T_lp = return_type_t; - T_lp lp(0.0); - + static const char* function = "multi_gp_lpdf"; check_positive(function, "Kernel rows", Sigma.rows()); check_finite(function, "Kernel", Sigma); check_symmetric(function, "Kernel", Sigma); @@ -52,6 +51,8 @@ return_type_t multi_gp_lpdf( check_positive_finite(function, "Kernel scales", w); check_finite(function, "Random variable", y); + T_lp lp(0.0); + if (y.rows() == 0) { return lp; } diff --git a/stan/math/prim/prob/multi_normal_lpdf.hpp b/stan/math/prim/prob/multi_normal_lpdf.hpp index c2075246d04..19e9a28fd9f 100644 --- a/stan/math/prim/prob/multi_normal_lpdf.hpp +++ b/stan/math/prim/prob/multi_normal_lpdf.hpp @@ -16,12 +16,10 @@ template return_type_t multi_normal_lpdf(const T_y& y, const T_loc& mu, const T_covar& Sigma) { - static const char* function = "multi_normal_lpdf"; using T_covar_elem = typename scalar_type::type; using lp_type = return_type_t; - using Eigen::Dynamic; - + static const char* function = "multi_normal_lpdf"; check_positive(function, "Covariance matrix rows", Sigma.rows()); check_symmetric(function, "Covariance matrix", Sigma); diff --git a/stan/math/prim/prob/multi_normal_prec_lpdf.hpp b/stan/math/prim/prob/multi_normal_prec_lpdf.hpp index 09fd4af0b53..44b42074be0 100644 --- a/stan/math/prim/prob/multi_normal_prec_lpdf.hpp +++ b/stan/math/prim/prob/multi_normal_prec_lpdf.hpp @@ -16,19 +16,17 @@ namespace math { template return_type_t multi_normal_prec_lpdf( const T_y& y, const T_loc& mu, const T_covar& Sigma) { - static const char* function = "multi_normal_prec_lpdf"; using T_covar_elem = typename scalar_type::type; using lp_type = return_type_t; - + using Eigen::Matrix; + using std::vector; + static const char* function = "multi_normal_prec_lpdf"; check_positive(function, "Precision matrix rows", Sigma.rows()); check_symmetric(function, "Precision matrix", Sigma); LDLT_factor ldlt_Sigma(Sigma); check_ldlt_factor(function, "LDLT_Factor of precision parameter", ldlt_Sigma); - using Eigen::Matrix; - using std::vector; - size_t number_of_y = size_mvt(y); size_t number_of_mu = size_mvt(mu); if (number_of_y == 0 || number_of_mu == 0) { diff --git a/stan/math/prim/prob/multi_normal_prec_rng.hpp b/stan/math/prim/prob/multi_normal_prec_rng.hpp index 79b18a98a88..954ae3d0545 100644 --- a/stan/math/prim/prob/multi_normal_prec_rng.hpp +++ b/stan/math/prim/prob/multi_normal_prec_rng.hpp @@ -31,9 +31,7 @@ inline typename StdVectorBuilder::type multi_normal_prec_rng(const T_loc &mu, const Eigen::MatrixXd &S, RNG &rng) { using boost::normal_distribution; using boost::variate_generator; - static const char *function = "multi_normal_prec_rng"; - check_positive(function, "Precision matrix rows", S.rows()); check_finite(function, "Precision matrix", S); check_symmetric(function, "Precision matrix", S); diff --git a/stan/math/prim/prob/multi_normal_rng.hpp b/stan/math/prim/prob/multi_normal_rng.hpp index db77f56bc38..fae90133b15 100644 --- a/stan/math/prim/prob/multi_normal_rng.hpp +++ b/stan/math/prim/prob/multi_normal_rng.hpp @@ -33,9 +33,7 @@ multi_normal_rng(const T_loc& mu, RNG& rng) { using boost::normal_distribution; using boost::variate_generator; - static const char* function = "multi_normal_rng"; - check_positive(function, "Covariance matrix rows", S.rows()); check_not_nan(function, "Covariance matrix", S); check_symmetric(function, "Covariance matrix", S); diff --git a/stan/math/prim/prob/multi_student_t_lpdf.hpp b/stan/math/prim/prob/multi_student_t_lpdf.hpp index a670fdb9375..226fd70c53e 100644 --- a/stan/math/prim/prob/multi_student_t_lpdf.hpp +++ b/stan/math/prim/prob/multi_student_t_lpdf.hpp @@ -27,11 +27,12 @@ template return_type_t multi_student_t_lpdf( const T_y& y, const T_dof& nu, const T_loc& mu, const T_scale& Sigma) { - static const char* function = "multi_student_t"; - using std::log; using T_scale_elem = typename scalar_type::type; using lp_type = return_type_t; - + using Eigen::Matrix; + using std::vector; + using std::log; + static const char* function = "multi_student_t"; check_not_nan(function, "Degrees of freedom parameter", nu); check_positive(function, "Degrees of freedom parameter", nu); @@ -39,9 +40,6 @@ return_type_t multi_student_t_lpdf( return multi_normal_log(y, mu, Sigma); } - using Eigen::Matrix; - using std::vector; - size_t number_of_y = size_mvt(y); size_t number_of_mu = size_mvt(mu); if (number_of_y == 0 || number_of_mu == 0) { diff --git a/stan/math/prim/prob/multi_student_t_rng.hpp b/stan/math/prim/prob/multi_student_t_rng.hpp index 1790d321724..e27e89c4cdb 100644 --- a/stan/math/prim/prob/multi_student_t_rng.hpp +++ b/stan/math/prim/prob/multi_student_t_rng.hpp @@ -40,7 +40,6 @@ multi_student_t_rng( using boost::variate_generator; static const char* function = "multi_student_t_rng"; - check_not_nan(function, "Degrees of freedom parameter", nu); check_positive(function, "Degrees of freedom parameter", nu); check_positive(function, "Covariance matrix rows", S.rows()); diff --git a/stan/math/prim/prob/multinomial_lpmf.hpp b/stan/math/prim/prob/multinomial_lpmf.hpp index 337312f167f..b6fd34f19fe 100644 --- a/stan/math/prim/prob/multinomial_lpmf.hpp +++ b/stan/math/prim/prob/multinomial_lpmf.hpp @@ -16,13 +16,13 @@ return_type_t multinomial_lpmf( const std::vector& ns, const Eigen::Matrix& theta) { static const char* function = "multinomial_lpmf"; - - return_type_t lp(0.0); check_nonnegative(function, "Number of trials variable", ns); check_simplex(function, "Probabilities parameter", theta); check_size_match(function, "Size of number of trials variable", ns.size(), "rows of probabilities parameter", theta.rows()); + return_type_t lp(0.0); + if (include_summand::value) { double sum = 1.0; for (int n : ns) { diff --git a/stan/math/prim/prob/multinomial_rng.hpp b/stan/math/prim/prob/multinomial_rng.hpp index 6f82c4ebf5b..5cb3b829990 100644 --- a/stan/math/prim/prob/multinomial_rng.hpp +++ b/stan/math/prim/prob/multinomial_rng.hpp @@ -13,7 +13,6 @@ template inline std::vector multinomial_rng( const Eigen::Matrix& theta, int N, RNG& rng) { static const char* function = "multinomial_rng"; - check_simplex(function, "Probabilities parameter", theta); check_positive(function, "number of trials variables", N); diff --git a/stan/math/prim/prob/neg_binomial_2_cdf.hpp b/stan/math/prim/prob/neg_binomial_2_cdf.hpp index bd355578380..1838cc631db 100644 --- a/stan/math/prim/prob/neg_binomial_2_cdf.hpp +++ b/stan/math/prim/prob/neg_binomial_2_cdf.hpp @@ -21,20 +21,21 @@ namespace math { template return_type_t neg_binomial_2_cdf( const T_n& n, const T_location& mu, const T_precision& phi) { - static const char* function = "neg_binomial_2_cdf"; using T_partials_return = partials_return_t; - - T_partials_return P(1.0); - if (size_zero(n, mu, phi)) { - return P; - } - + static const char* function = "neg_binomial_2_cdf"; check_positive_finite(function, "Location parameter", mu); check_positive_finite(function, "Precision parameter", phi); check_not_nan(function, "Random variable", n); check_consistent_sizes(function, "Random variable", n, "Location parameter", mu, "Precision Parameter", phi); + if (size_zero(n, mu, phi)) { + return 1.0; + } + + T_partials_return P(1.0); + operands_and_partials ops_partials(mu, phi); + scalar_seq_view n_vec(n); scalar_seq_view mu_vec(mu); scalar_seq_view phi_vec(phi); @@ -42,8 +43,6 @@ return_type_t neg_binomial_2_cdf( size_t size_n_phi = max_size(n, phi); size_t max_size_seq_view = max_size(n, mu, phi); - operands_and_partials ops_partials(mu, phi); - // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::math::size(n); i++) { diff --git a/stan/math/prim/prob/neg_binomial_2_lccdf.hpp b/stan/math/prim/prob/neg_binomial_2_lccdf.hpp index 7406c848369..cb4ba5b5938 100644 --- a/stan/math/prim/prob/neg_binomial_2_lccdf.hpp +++ b/stan/math/prim/prob/neg_binomial_2_lccdf.hpp @@ -15,10 +15,6 @@ namespace math { template return_type_t neg_binomial_2_lccdf( const T_n& n, const T_location& mu, const T_precision& phi) { - if (size_zero(n, mu, phi)) { - return 0.0; - } - static const char* function = "neg_binomial_2_lccdf"; check_positive_finite(function, "Location parameter", mu); check_positive_finite(function, "Precision parameter", phi); @@ -26,6 +22,10 @@ return_type_t neg_binomial_2_lccdf( check_consistent_sizes(function, "Random variable", n, "Location parameter", mu, "Precision Parameter", phi); + if (size_zero(n, mu, phi)) { + return 0; + } + scalar_seq_view mu_vec(mu); scalar_seq_view phi_vec(phi); size_t size_beta = max_size(mu, phi); diff --git a/stan/math/prim/prob/neg_binomial_2_lcdf.hpp b/stan/math/prim/prob/neg_binomial_2_lcdf.hpp index 4aad9ea0d62..5af27d75d88 100644 --- a/stan/math/prim/prob/neg_binomial_2_lcdf.hpp +++ b/stan/math/prim/prob/neg_binomial_2_lcdf.hpp @@ -17,11 +17,6 @@ template return_type_t neg_binomial_2_lcdf( const T_n& n, const T_location& mu, const T_precision& phi) { using std::log; - - if (size_zero(n, mu, phi)) { - return 0.0; - } - static const char* function = "neg_binomial_2_lcdf"; check_positive_finite(function, "Location parameter", mu); check_positive_finite(function, "Precision parameter", phi); @@ -29,6 +24,10 @@ return_type_t neg_binomial_2_lcdf( check_consistent_sizes(function, "Random variable", n, "Location parameter", mu, "Precision Parameter", phi); + if (size_zero(n, mu, phi)) { + return 0; + } + scalar_seq_view n_vec(n); scalar_seq_view mu_vec(mu); scalar_seq_view phi_vec(phi); diff --git a/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp b/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp index 2834d1bcfed..e155b8935bf 100644 --- a/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp +++ b/stan/math/prim/prob/neg_binomial_2_log_glm_lpmf.hpp @@ -26,6 +26,7 @@ namespace math { * neg_binomial_2_log_lpmf(y, alpha + x * beta, phi) by using analytically * simplified gradients. * If containers are supplied, returns the log sum of the probabilities. + * * @tparam T_y type of positive int vector of variates (labels); * this can also be a single positive integer value; * @tparam T_x_scalar type of a scalar in the matrix of independent variables @@ -40,6 +41,7 @@ namespace math { * @tparam T_precision type of the (positive) precision(s); * this can be a vector (of the same length as y, for heteroskedasticity) * or a scalar. + * * @param y failures count scalar or vector parameter. If it is a scalar it will * be broadcast - used for all instances. * @param x design matrix or row vector. If it is a row vector it will be @@ -59,14 +61,11 @@ return_type_t neg_binomial_2_log_glm_lpmf( const T_y& y, const Eigen::Matrix& x, const T_alpha& alpha, const T_beta& beta, const T_precision& phi) { - static const char* function = "neg_binomial_2_log_glm_lpmf"; - using Eigen::Array; using Eigen::Dynamic; using Eigen::Matrix; using Eigen::exp; using Eigen::log1p; - using T_partials_return = partials_return_t; using T_precision_val = typename std::conditional_t< @@ -84,6 +83,7 @@ neg_binomial_2_log_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 = "neg_binomial_2_log_glm_lpmf"; check_consistent_size(function, "Vector of dependent variables", y, N_instances); check_consistent_size(function, "Weight vector", beta, N_attributes); diff --git a/stan/math/prim/prob/neg_binomial_2_log_lpmf.hpp b/stan/math/prim/prob/neg_binomial_2_log_lpmf.hpp index cce691d8be0..3cd52455664 100644 --- a/stan/math/prim/prob/neg_binomial_2_log_lpmf.hpp +++ b/stan/math/prim/prob/neg_binomial_2_log_lpmf.hpp @@ -25,14 +25,9 @@ template neg_binomial_2_log_lpmf( const T_n& n, const T_log_location& eta, const T_precision& phi) { using T_partials_return = partials_return_t; - + using std::exp; + using std::log; static const char* function = "neg_binomial_2_log_lpmf"; - - if (size_zero(n, eta, phi)) { - return 0.0; - } - - T_partials_return logp(0.0); check_nonnegative(function, "Failures variable", n); check_finite(function, "Log location parameter", eta); check_positive_finite(function, "Precision parameter", phi); @@ -40,12 +35,15 @@ return_type_t neg_binomial_2_log_lpmf( "Log location parameter", eta, "Precision parameter", phi); + if (size_zero(n, eta, phi)) { + return 0.0; + } if (!include_summand::value) { return 0.0; } - using std::exp; - using std::log; + T_partials_return logp(0.0); + operands_and_partials ops_partials(eta, phi); scalar_seq_view n_vec(n); scalar_seq_view eta_vec(eta); @@ -56,8 +54,6 @@ return_type_t neg_binomial_2_log_lpmf( size_t size_n_phi = max_size(n, phi); size_t max_size_seq_view = max_size(n, eta, phi); - operands_and_partials ops_partials(eta, phi); - VectorBuilder eta_val(size_eta); for (size_t i = 0; i < size_eta; ++i) { eta_val[i] = value_of(eta_vec[i]); diff --git a/stan/math/prim/prob/neg_binomial_2_log_rng.hpp b/stan/math/prim/prob/neg_binomial_2_log_rng.hpp index 51278b8c290..1a7d85e95bb 100644 --- a/stan/math/prim/prob/neg_binomial_2_log_rng.hpp +++ b/stan/math/prim/prob/neg_binomial_2_log_rng.hpp @@ -37,9 +37,7 @@ neg_binomial_2_log_rng(const T_loc& eta, const T_inv& phi, RNG& rng) { using boost::gamma_distribution; using boost::random::poisson_distribution; using boost::variate_generator; - static const char* function = "neg_binomial_2_log_rng"; - check_finite(function, "Log-location parameter", eta); check_positive_finite(function, "Inverse dispersion parameter", phi); check_consistent_sizes(function, "Log-location parameter", eta, diff --git a/stan/math/prim/prob/neg_binomial_2_lpmf.hpp b/stan/math/prim/prob/neg_binomial_2_lpmf.hpp index b832abc1bfb..da99e76594c 100644 --- a/stan/math/prim/prob/neg_binomial_2_lpmf.hpp +++ b/stan/math/prim/prob/neg_binomial_2_lpmf.hpp @@ -22,25 +22,23 @@ template return_type_t neg_binomial_2_lpmf( const T_n& n, const T_location& mu, const T_precision& phi) { using T_partials_return = partials_return_t; - + using std::log; static const char* function = "neg_binomial_2_lpmf"; - - if (size_zero(n, mu, phi)) { - return 0.0; - } - - T_partials_return logp(0.0); check_nonnegative(function, "Failures variable", n); check_positive_finite(function, "Location parameter", mu); check_positive_finite(function, "Precision parameter", phi); check_consistent_sizes(function, "Failures variable", n, "Location parameter", mu, "Precision parameter", phi); + if (size_zero(n, mu, phi)) { + return 0.0; + } if (!include_summand::value) { return 0.0; } - using std::log; + T_partials_return logp(0.0); + operands_and_partials ops_partials(mu, phi); scalar_seq_view n_vec(n); scalar_seq_view mu_vec(mu); @@ -51,8 +49,6 @@ return_type_t neg_binomial_2_lpmf( size_t size_n_phi = max_size(n, phi); size_t max_size_seq_view = max_size(n, mu, phi); - operands_and_partials ops_partials(mu, phi); - VectorBuilder mu_val(size_mu); for (size_t i = 0; i < size_mu; ++i) { mu_val[i] = value_of(mu_vec[i]); diff --git a/stan/math/prim/prob/neg_binomial_2_rng.hpp b/stan/math/prim/prob/neg_binomial_2_rng.hpp index cd2a290bfa4..9a9cdef03cb 100644 --- a/stan/math/prim/prob/neg_binomial_2_rng.hpp +++ b/stan/math/prim/prob/neg_binomial_2_rng.hpp @@ -19,8 +19,8 @@ namespace math { * mu and phi can each be a scalar or a one-dimensional container. Any * non-scalar inputs must be the same size. * - * @tparam T_loc Type of location parameter - * @tparam T_prec Type of precision parameter + * @tparam T_loc type of location parameter + * @tparam T_prec type of precision parameter * @tparam RNG type of random number generator * @param mu (Sequence of) positive location parameter(s) * @param phi (Sequence of) positive precision parameter(s) @@ -36,9 +36,7 @@ neg_binomial_2_rng(const T_loc& mu, const T_prec& phi, RNG& rng) { using boost::gamma_distribution; using boost::random::poisson_distribution; using boost::variate_generator; - static const char* function = "neg_binomial_2_rng"; - check_positive_finite(function, "Location parameter", mu); check_positive_finite(function, "Precision parameter", phi); check_consistent_sizes(function, "Location parameter", mu, diff --git a/stan/math/prim/prob/neg_binomial_cdf.hpp b/stan/math/prim/prob/neg_binomial_cdf.hpp index 2a07310c1a2..d5f26ab2344 100644 --- a/stan/math/prim/prob/neg_binomial_cdf.hpp +++ b/stan/math/prim/prob/neg_binomial_cdf.hpp @@ -23,19 +23,19 @@ template return_type_t neg_binomial_cdf(const T_n& n, const T_shape& alpha, const T_inv_scale& beta) { - static const char* function = "neg_binomial_cdf"; using T_partials_return = partials_return_t; + static const char* function = "neg_binomial_cdf"; + check_positive_finite(function, "Shape parameter", alpha); + check_positive_finite(function, "Inverse scale parameter", beta); + check_consistent_sizes(function, "Failures variable", n, "Shape parameter", + alpha, "Inverse scale parameter", beta); if (size_zero(n, alpha, beta)) { return 1.0; } T_partials_return P(1.0); - - check_positive_finite(function, "Shape parameter", alpha); - check_positive_finite(function, "Inverse scale parameter", beta); - check_consistent_sizes(function, "Failures variable", n, "Shape parameter", - alpha, "Inverse scale parameter", beta); + operands_and_partials ops_partials(alpha, beta); scalar_seq_view n_vec(n); scalar_seq_view alpha_vec(alpha); @@ -44,8 +44,6 @@ return_type_t neg_binomial_cdf(const T_n& n, size_t size_n_alpha = max_size(n, alpha); size_t max_size_seq_view = max_size(n, alpha, beta); - operands_and_partials ops_partials(alpha, beta); - // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::math::size(n); i++) { diff --git a/stan/math/prim/prob/neg_binomial_lccdf.hpp b/stan/math/prim/prob/neg_binomial_lccdf.hpp index 43a9431d99f..a9eb87c4a85 100644 --- a/stan/math/prim/prob/neg_binomial_lccdf.hpp +++ b/stan/math/prim/prob/neg_binomial_lccdf.hpp @@ -24,24 +24,21 @@ namespace math { template return_type_t neg_binomial_lccdf( const T_n& n, const T_shape& alpha, const T_inv_scale& beta) { - static const char* function = "neg_binomial_lccdf"; using T_partials_return = partials_return_t; - - if (size_zero(n, alpha, beta)) { - return 0.0; - } - - T_partials_return P(0.0); - + using std::exp; + using std::log; + using std::pow; + static const char* function = "neg_binomial_lccdf"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Inverse scale parameter", beta); check_consistent_sizes(function, "Failures variable", n, "Shape parameter", alpha, "Inverse scale parameter", beta); - using std::exp; - using std::log; - using std::pow; + if (size_zero(n, alpha, beta)) { + return 0; + } + T_partials_return P(0.0); operands_and_partials ops_partials(alpha, beta); scalar_seq_view n_vec(n); diff --git a/stan/math/prim/prob/neg_binomial_lcdf.hpp b/stan/math/prim/prob/neg_binomial_lcdf.hpp index 1b500f7edee..5e9d8676bc9 100644 --- a/stan/math/prim/prob/neg_binomial_lcdf.hpp +++ b/stan/math/prim/prob/neg_binomial_lcdf.hpp @@ -25,24 +25,21 @@ template return_type_t neg_binomial_lcdf(const T_n& n, const T_shape& alpha, const T_inv_scale& beta) { - static const char* function = "neg_binomial_lcdf"; using T_partials_return = partials_return_t; - - if (size_zero(n, alpha, beta)) { - return 0.0; - } - - T_partials_return P(0.0); - + using std::exp; + using std::log; + using std::pow; + static const char* function = "neg_binomial_lcdf"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Inverse scale parameter", beta); check_consistent_sizes(function, "Failures variable", n, "Shape parameter", alpha, "Inverse scale parameter", beta); - using std::exp; - using std::log; - using std::pow; + if (size_zero(n, alpha, beta)) { + return 0; + } + T_partials_return P(0.0); operands_and_partials ops_partials(alpha, beta); scalar_seq_view n_vec(n); diff --git a/stan/math/prim/prob/neg_binomial_lpmf.hpp b/stan/math/prim/prob/neg_binomial_lpmf.hpp index fed80a2da2e..bef6448e730 100644 --- a/stan/math/prim/prob/neg_binomial_lpmf.hpp +++ b/stan/math/prim/prob/neg_binomial_lpmf.hpp @@ -30,25 +30,23 @@ return_type_t neg_binomial_lpmf(const T_n& n, const T_shape& alpha, const T_inv_scale& beta) { using T_partials_return = partials_return_t; - + using std::log; static const char* function = "neg_binomial_lpmf"; - - if (size_zero(n, alpha, beta)) { - return 0.0; - } - - T_partials_return logp(0.0); check_nonnegative(function, "Failures variable", n); check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Inverse scale parameter", beta); check_consistent_sizes(function, "Failures variable", n, "Shape parameter", alpha, "Inverse scale parameter", beta); + if (size_zero(n, alpha, beta)) { + return 0.0; + } if (!include_summand::value) { return 0.0; } - using std::log; + T_partials_return logp(0.0); + operands_and_partials ops_partials(alpha, beta); scalar_seq_view n_vec(n); scalar_seq_view alpha_vec(alpha); @@ -58,8 +56,6 @@ return_type_t neg_binomial_lpmf(const T_n& n, size_t size_alpha_beta = max_size(alpha, beta); size_t max_size_seq_view = max_size(n, alpha, beta); - operands_and_partials ops_partials(alpha, beta); - VectorBuilder::value, T_partials_return, T_shape> digamma_alpha(size_alpha); if (!is_constant_all::value) { diff --git a/stan/math/prim/prob/neg_binomial_rng.hpp b/stan/math/prim/prob/neg_binomial_rng.hpp index e703d68f491..9557772f750 100644 --- a/stan/math/prim/prob/neg_binomial_rng.hpp +++ b/stan/math/prim/prob/neg_binomial_rng.hpp @@ -19,8 +19,8 @@ namespace math { * alpha and beta can each be a scalar or a one-dimensional container. Any * non-scalar inputs must be the same size. * - * @tparam T_shape Type of shape parameter - * @tparam T_inv Type of inverse scale parameter + * @tparam T_shape type of shape parameter + * @tparam T_inv type of inverse scale parameter * @tparam RNG type of random number generator * @param alpha (Sequence of) positive shape parameter(s) * @param beta (Sequence of) positive inverse scale parameter(s) @@ -36,10 +36,7 @@ inline typename VectorBuilder::type neg_binomial_rng( using boost::gamma_distribution; using boost::random::poisson_distribution; using boost::variate_generator; - static const char* function = "neg_binomial_rng"; - - // gamma_rng params must be positive and finite check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Inverse scale parameter", beta); check_consistent_sizes(function, "Shape parameter", alpha, diff --git a/stan/math/prim/prob/normal_cdf.hpp b/stan/math/prim/prob/normal_cdf.hpp index 1812fb93dce..3dd91feba34 100644 --- a/stan/math/prim/prob/normal_cdf.hpp +++ b/stan/math/prim/prob/normal_cdf.hpp @@ -22,29 +22,21 @@ namespace math { * * \f$\Phi(x) = \frac{1}{\sqrt{2 \pi}} \int_{-\inf}^x e^{-t^2/2} dt\f$. * + * @tparam T_y type of y + * @tparam T_loc type of mean parameter + * @tparam T_scale type of standard deviation parameter * @param y A scalar variate. * @param mu The location of the normal distribution. * @param sigma The scale of the normal distribution * @return The unit normal cdf evaluated at the specified arguments. - * @tparam T_y Type of y. - * @tparam T_loc Type of mean parameter. - * @tparam T_scale Type of standard deviation parameter. */ template inline return_type_t normal_cdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { - static const char* function = "normal_cdf"; using T_partials_return = partials_return_t; - using std::exp; - - T_partials_return cdf(1.0); - - if (size_zero(y, mu, sigma)) { - return cdf; - } - + static const char* function = "normal_cdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_not_nan(function, "Scale parameter", sigma); @@ -52,6 +44,11 @@ inline return_type_t normal_cdf(const T_y& y, check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma); + if (size_zero(y, mu, sigma)) { + return 1.0; + } + + T_partials_return cdf(1.0); operands_and_partials ops_partials(y, mu, sigma); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/normal_id_glm_lpdf.hpp b/stan/math/prim/prob/normal_id_glm_lpdf.hpp index d12d9f66d66..9e698f92bd1 100644 --- a/stan/math/prim/prob/normal_id_glm_lpdf.hpp +++ b/stan/math/prim/prob/normal_id_glm_lpdf.hpp @@ -21,6 +21,7 @@ namespace math { * The idea is that normal_id_glm_lpdf(y, x, alpha, beta, sigma) should * compute a more efficient version of normal_lpdf(y, alpha + x * beta, sigma) * by using analytically simplified gradients. + * * @tparam T_y type of vector of dependent variables (labels); * @tparam T_x_scalar type of a scalar in the matrix of independent variables * (features) @@ -34,6 +35,7 @@ namespace math { * @tparam T_scale type of the (positive) scale(s); * this can be a vector (of the same length as y, for heteroskedasticity) * or a scalar. + * * @param y scalar or vector of dependent variables. If it is a scalar it will * be broadcast - used for all instances. * @param x design matrix or row vector. If it is a row vector it will be @@ -56,7 +58,6 @@ return_type_t normal_id_glm_lpdf( using Eigen::Dynamic; using Eigen::Matrix; using Eigen::VectorXd; - using T_partials_return = partials_return_t; using T_scale_val = typename std::conditional_t< @@ -67,11 +68,10 @@ return_type_t normal_id_glm_lpdf( typename std::conditional_t>; - static const char *function = "normal_id_glm_lpdf"; - 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 = "normal_id_glm_lpdf"; check_consistent_size(function, "Vector of dependent variables", y, N_instances); check_consistent_size(function, "Weight vector", beta, N_attributes); @@ -83,7 +83,6 @@ return_type_t normal_id_glm_lpdf( if (size_zero(y, sigma)) { return 0; } - if (!include_summand::value) { return 0; diff --git a/stan/math/prim/prob/normal_lccdf.hpp b/stan/math/prim/prob/normal_lccdf.hpp index 2f17fd34fbd..60c153c734c 100644 --- a/stan/math/prim/prob/normal_lccdf.hpp +++ b/stan/math/prim/prob/normal_lccdf.hpp @@ -20,17 +20,10 @@ template inline return_type_t normal_lccdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { - static const char* function = "normal_lccdf"; using T_partials_return = partials_return_t; - using std::exp; using std::log; - - T_partials_return ccdf_log(0.0); - if (size_zero(y, mu, sigma)) { - return ccdf_log; - } - + static const char* function = "normal_lccdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_not_nan(function, "Scale parameter", sigma); @@ -38,6 +31,11 @@ inline return_type_t normal_lccdf(const T_y& y, check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma); + if (size_zero(y, mu, sigma)) { + return 0; + } + + T_partials_return ccdf_log(0.0); operands_and_partials ops_partials(y, mu, sigma); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/normal_lcdf.hpp b/stan/math/prim/prob/normal_lcdf.hpp index e0d3af48291..5660244fe30 100644 --- a/stan/math/prim/prob/normal_lcdf.hpp +++ b/stan/math/prim/prob/normal_lcdf.hpp @@ -23,19 +23,13 @@ template inline return_type_t normal_lcdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { - static const char* function = "normal_lcdf"; using T_partials_return = partials_return_t; using std::exp; using std::fabs; using std::log; using std::pow; using std::sqrt; - - T_partials_return cdf_log(0.0); - if (size_zero(y, mu, sigma)) { - return cdf_log; - } - + static const char* function = "normal_lcdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_not_nan(function, "Scale parameter", sigma); @@ -43,6 +37,11 @@ inline return_type_t normal_lcdf(const T_y& y, check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma); + if (size_zero(y, mu, sigma)) { + return 0; + } + + T_partials_return cdf_log(0.0); operands_and_partials ops_partials(y, mu, sigma); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/normal_lpdf.hpp b/stan/math/prim/prob/normal_lpdf.hpp index 2866926bef3..6c697b81ea9 100644 --- a/stan/math/prim/prob/normal_lpdf.hpp +++ b/stan/math/prim/prob/normal_lpdf.hpp @@ -22,14 +22,14 @@ namespace math { * *

The result log probability is defined to be the sum of the * log probabilities for each observation/mean/deviation triple. - * @tparam T_y Underlying type of scalar in sequence. - * @tparam T_loc Type of location parameter. - * @tparam T_scale Type of scale parameter. + * + * @tparam T_y type of scalar + * @tparam T_loc type of location parameter + * @tparam T_scale type of scale parameter * @param y (Sequence of) scalar(s). * @param mu (Sequence of) location parameter(s) * for the normal distribution. - * @param sigma (Sequence of) scale parameters for the normal - * distribution. + * @param sigma (Sequence of) scale parameters for the normal distribution. * @return The log of the product of the densities. * @throw std::domain_error if the scale is not positive. */ @@ -37,26 +37,23 @@ template inline return_type_t normal_lpdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { - static const char* function = "normal_lpdf"; using T_partials_return = partials_return_t; - using std::log; - - if (size_zero(y, mu, sigma)) { - return 0.0; - } - - T_partials_return logp(0.0); - + static const char* function = "normal_lpdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_positive(function, "Scale parameter", sigma); check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma); + + if (size_zero(y, mu, sigma)) { + return 0.0; + } if (!include_summand::value) { return 0.0; } + T_partials_return logp(0.0); operands_and_partials ops_partials(y, mu, sigma); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/normal_rng.hpp b/stan/math/prim/prob/normal_rng.hpp index 5356de1137b..37b7c9af42c 100644 --- a/stan/math/prim/prob/normal_rng.hpp +++ b/stan/math/prim/prob/normal_rng.hpp @@ -17,8 +17,8 @@ namespace math { * mu and sigma can each be a scalar or a vector. Any non-scalar * inputs must be the same length. * - * @tparam T_loc Type of location parameter - * @tparam T_scale Type of scale parameter + * @tparam T_loc type of location parameter + * @tparam T_scale type of scale parameter * @tparam RNG type of random number generator * @param mu (Sequence of) location parameter(s) * @param sigma (Sequence of) positive scale parameter(s) @@ -34,7 +34,6 @@ inline typename VectorBuilder::type normal_rng( using boost::normal_distribution; using boost::variate_generator; static const char* function = "normal_rng"; - check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); check_consistent_sizes(function, "Location parameter", mu, "Scale Parameter", diff --git a/stan/math/prim/prob/normal_sufficient_lpdf.hpp b/stan/math/prim/prob/normal_sufficient_lpdf.hpp index 1230c73b92e..d5b3c3952c2 100644 --- a/stan/math/prim/prob/normal_sufficient_lpdf.hpp +++ b/stan/math/prim/prob/normal_sufficient_lpdf.hpp @@ -26,11 +26,12 @@ namespace math { *

The result log probability is defined to be the sum of the * log probabilities for each observation/mean/deviation triple. * - * @tparam T_y Type of sample average parameter. - * @tparam T_s Type of sample squared errors parameter. - * @tparam T_n Type of sample size parameter. - * @tparam T_loc Type of location parameter. - * @tparam T_scale Type of scale parameter. + * @tparam T_y type of sample average parameter + * @tparam T_s type of sample squared errors parameter + * @tparam T_n type of sample size parameter + * @tparam T_loc type of location parameter + * @tparam T_scale type of scale parameter + * * @param y_bar (Sequence of) scalar(s) (sample average(s)). * @param s_squared (Sequence of) sum(s) of sample squared errors * @param n_obs (Sequence of) sample size(s) @@ -47,21 +48,10 @@ template normal_sufficient_lpdf( const T_y& y_bar, const T_s& s_squared, const T_n& n_obs, const T_loc& mu, const T_scale& sigma) { - static const char* function = "normal_sufficient_lpdf"; using T_partials_return = partials_return_t; - using std::log; using std::pow; - - // check if any vectors are zero length - if (size_zero(y_bar, s_squared, n_obs, mu, sigma)) { - return 0.0; - } - - // set up return value accumulator - T_partials_return logp(0.0); - - // validate args (here done over var, which should be OK) + static const char* function = "normal_sufficient_lpdf"; check_finite(function, "Location parameter sufficient statistic", y_bar); check_finite(function, "Scale parameter sufficient statistic", s_squared); check_nonnegative(function, "Scale parameter sufficient statistic", @@ -75,15 +65,17 @@ return_type_t normal_sufficient_lpdf( y_bar, "Scale parameter sufficient statistic", s_squared, "Number of observations", n_obs, "Location parameter", mu, "Scale parameter", sigma); - // check if no variables are involved and prop-to + + if (size_zero(y_bar, s_squared, n_obs, mu, sigma)) { + return 0.0; + } if (!include_summand::value) { return 0.0; } - // set up template expressions wrapping scalars into vector views + T_partials_return logp(0.0); operands_and_partials ops_partials(y_bar, s_squared, mu, sigma); - scalar_seq_view y_bar_vec(y_bar); scalar_seq_view s_squared_vec(s_squared); scalar_seq_view n_obs_vec(n_obs); diff --git a/stan/math/prim/prob/ordered_logistic_glm_lpmf.hpp b/stan/math/prim/prob/ordered_logistic_glm_lpmf.hpp index 4f0c06c4ab7..0f1656ad849 100644 --- a/stan/math/prim/prob/ordered_logistic_glm_lpmf.hpp +++ b/stan/math/prim/prob/ordered_logistic_glm_lpmf.hpp @@ -26,6 +26,7 @@ namespace math { * `Eigen::Dynamic` or 1. * @tparam T_beta_scalar type of a scalar in the vector of weights * @tparam T_cuts_scalar type of a scalar in the vector of cutpoints + * * @param y a scalar or vector of classes. If it is a scalar it will be * broadcast - used for all instances. Values should be between 1 and number of * classes, including endpoints. @@ -52,18 +53,16 @@ ordered_logistic_glm_lpmf( using Eigen::VectorXd; using std::exp; using std::isfinite; - using T_partials_return = partials_return_t; typedef typename std::conditional_t T_location; - static const char* function = "ordered_logistic_glm_lpmf"; - const size_t N_instances = T_x_rows == 1 ? stan::math::size(y) : x.rows(); const size_t N_attributes = x.cols(); const size_t N_classes = stan::math::size(cuts) + 1; + static const char* function = "ordered_logistic_glm_lpmf"; check_consistent_size(function, "Vector of dependent variables", y, N_instances); check_consistent_size(function, "Weight vector", beta, N_attributes); diff --git a/stan/math/prim/prob/ordered_logistic_rng.hpp b/stan/math/prim/prob/ordered_logistic_rng.hpp index f0c2f228e81..b8b054670fe 100644 --- a/stan/math/prim/prob/ordered_logistic_rng.hpp +++ b/stan/math/prim/prob/ordered_logistic_rng.hpp @@ -14,9 +14,7 @@ template inline int ordered_logistic_rng( double eta, const Eigen::Matrix& c, RNG& rng) { using boost::variate_generator; - static const char* function = "ordered_logistic"; - check_finite(function, "Location parameter", eta); check_greater(function, "Size of cut points parameter", c.size(), 0); check_ordered(function, "Cut points parameter", c); diff --git a/stan/math/prim/prob/ordered_probit_lpmf.hpp b/stan/math/prim/prob/ordered_probit_lpmf.hpp index 681139c1602..cf040a3d96f 100644 --- a/stan/math/prim/prob/ordered_probit_lpmf.hpp +++ b/stan/math/prim/prob/ordered_probit_lpmf.hpp @@ -21,14 +21,12 @@ namespace math { * will be the dot product of a vector of regression coefficients * and a vector of predictors for the outcome. * - * @tparam propto True if calculating up to a proportion. - * @tparam T_loc Location type. - * @tparam T_cut Cut-point type. + * @tparam T_loc location type + * @tparam T_cut cut-point type * @param y Outcome. * @param lambda Location. * @param c Positive increasing vector of cutpoints. - * @return Log probability of outcome given location and - * cutpoints. + * @return Log probability of outcome given location and cutpoints. * @throw std::domain_error If the outcome is not between 1 and * the number of cutpoints plus 2; if the cutpoint vector is * empty; if the cutpoint vector contains a non-positive, @@ -41,11 +39,8 @@ return_type_t ordered_probit_lpmf( const Eigen::Matrix& c) { using std::exp; using std::log; - static const char* function = "ordered_probit"; - int K = c.size() + 1; - check_bounded(function, "Random variable", y, 1, K); check_finite(function, "Location parameter", lambda); check_greater(function, "Size of cut points parameter", c.size(), 0); @@ -98,9 +93,6 @@ return_type_t ordered_probit_lpmf( const std::vector& y, const Eigen::Matrix& lambda, const Eigen::Matrix& c) { - using std::exp; - using std::log; - static const char* function = "ordered_probit"; int N = lambda.size(); @@ -113,6 +105,8 @@ return_type_t ordered_probit_lpmf( check_greater(function, "Size of cut points parameter", c.size(), 0); check_finite(function, "Cut-points", c); + using std::exp; + using std::log; return_type_t logp_n(0.0); for (int i = 0; i < N; ++i) { @@ -168,13 +162,8 @@ return_type_t ordered_probit_lpmf( const std::vector& y, const Eigen::Matrix& lambda, const std::vector >& c) { - using std::exp; - using std::log; - static const char* function = "ordered_probit"; - int N = lambda.size(); - check_consistent_sizes(function, "Integers", y, "Locations", lambda); check_consistent_sizes(function, "Integers", y, "Cut-points", c); @@ -188,6 +177,8 @@ return_type_t ordered_probit_lpmf( check_finite(function, "Location parameter", lambda); check_finite(function, "Cut-points", c); + using std::exp; + using std::log; return_type_t logp_n(0.0); for (int i = 0; i < N; ++i) { diff --git a/stan/math/prim/prob/ordered_probit_rng.hpp b/stan/math/prim/prob/ordered_probit_rng.hpp index 724b1a3e77c..c46790177db 100644 --- a/stan/math/prim/prob/ordered_probit_rng.hpp +++ b/stan/math/prim/prob/ordered_probit_rng.hpp @@ -12,7 +12,6 @@ namespace math { template inline int ordered_probit_rng(double eta, const Eigen::VectorXd& c, RNG& rng) { static const char* function = "ordered_probit"; - check_finite(function, "Location parameter", eta); check_greater(function, "Size of cut points parameter", c.size(), 0); check_ordered(function, "Cut points vector", c); diff --git a/stan/math/prim/prob/pareto_cdf.hpp b/stan/math/prim/prob/pareto_cdf.hpp index 1a4c1cea5e8..8dc25936dfd 100644 --- a/stan/math/prim/prob/pareto_cdf.hpp +++ b/stan/math/prim/prob/pareto_cdf.hpp @@ -20,18 +20,9 @@ return_type_t pareto_cdf(const T_y& y, const T_scale& y_min, const T_shape& alpha) { using T_partials_return = partials_return_t; - - if (size_zero(y, y_min, alpha)) { - return 1.0; - } - - static const char* function = "pareto_cdf"; - using std::exp; using std::log; - - T_partials_return P(1.0); - + static const char* function = "pareto_cdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_positive_finite(function, "Scale parameter", y_min); @@ -39,13 +30,18 @@ return_type_t pareto_cdf(const T_y& y, check_consistent_sizes(function, "Random variable", y, "Scale parameter", y_min, "Shape parameter", alpha); + if (size_zero(y, y_min, alpha)) { + return 1.0; + } + + T_partials_return P(1.0); + operands_and_partials ops_partials(y, y_min, alpha); + scalar_seq_view y_vec(y); scalar_seq_view y_min_vec(y_min); scalar_seq_view alpha_vec(alpha); size_t N = max_size(y, y_min, alpha); - operands_and_partials ops_partials(y, y_min, alpha); - // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::math::size(y); i++) { diff --git a/stan/math/prim/prob/pareto_lccdf.hpp b/stan/math/prim/prob/pareto_lccdf.hpp index b4f37872bd2..5a2e999be43 100644 --- a/stan/math/prim/prob/pareto_lccdf.hpp +++ b/stan/math/prim/prob/pareto_lccdf.hpp @@ -20,18 +20,9 @@ return_type_t pareto_lccdf(const T_y& y, const T_scale& y_min, const T_shape& alpha) { using T_partials_return = partials_return_t; - - if (size_zero(y, y_min, alpha)) { - return 0.0; - } - - static const char* function = "pareto_lccdf"; - using std::exp; using std::log; - - T_partials_return P(0.0); - + static const char* function = "pareto_lccdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_positive_finite(function, "Scale parameter", y_min); @@ -39,13 +30,18 @@ return_type_t pareto_lccdf(const T_y& y, check_consistent_sizes(function, "Random variable", y, "Scale parameter", y_min, "Shape parameter", alpha); + if (size_zero(y, y_min, alpha)) { + return 0; + } + + T_partials_return P(0.0); + operands_and_partials ops_partials(y, y_min, alpha); + scalar_seq_view y_vec(y); scalar_seq_view y_min_vec(y_min); scalar_seq_view alpha_vec(alpha); size_t N = max_size(y, y_min, alpha); - operands_and_partials ops_partials(y, y_min, alpha); - // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::math::size(y); i++) { diff --git a/stan/math/prim/prob/pareto_lcdf.hpp b/stan/math/prim/prob/pareto_lcdf.hpp index a3a1f29881c..9351e9e0db8 100644 --- a/stan/math/prim/prob/pareto_lcdf.hpp +++ b/stan/math/prim/prob/pareto_lcdf.hpp @@ -20,18 +20,9 @@ return_type_t pareto_lcdf(const T_y& y, const T_scale& y_min, const T_shape& alpha) { using T_partials_return = partials_return_t; - - if (size_zero(y, y_min, alpha)) { - return 0.0; - } - - static const char* function = "pareto_lcdf"; - using std::exp; using std::log; - - T_partials_return P(0.0); - + static const char* function = "pareto_lcdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_positive_finite(function, "Scale parameter", y_min); @@ -39,13 +30,18 @@ return_type_t pareto_lcdf(const T_y& y, check_consistent_sizes(function, "Random variable", y, "Scale parameter", y_min, "Shape parameter", alpha); + if (size_zero(y, y_min, alpha)) { + return 0; + } + + T_partials_return P(0.0); + operands_and_partials ops_partials(y, y_min, alpha); + scalar_seq_view y_vec(y); scalar_seq_view y_min_vec(y_min); scalar_seq_view alpha_vec(alpha); size_t N = max_size(y, y_min, alpha); - operands_and_partials ops_partials(y, y_min, alpha); - // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::math::size(y); i++) { diff --git a/stan/math/prim/prob/pareto_lpdf.hpp b/stan/math/prim/prob/pareto_lpdf.hpp index c3e4c5cbe08..eb4d3dbfa21 100644 --- a/stan/math/prim/prob/pareto_lpdf.hpp +++ b/stan/math/prim/prob/pareto_lpdf.hpp @@ -19,23 +19,24 @@ template return_type_t pareto_lpdf(const T_y& y, const T_scale& y_min, const T_shape& alpha) { - static const char* function = "pareto_lpdf"; using T_partials_return = partials_return_t; using std::log; + static const char* function = "pareto_lpdf"; check_not_nan(function, "Random variable", y); check_positive_finite(function, "Scale parameter", y_min); check_positive_finite(function, "Shape parameter", alpha); check_consistent_sizes(function, "Random variable", y, "Scale parameter", y_min, "Shape parameter", alpha); + if (size_zero(y, y_min, alpha)) { return 0; } - if (!include_summand::value) { return 0; } T_partials_return logp(0); + operands_and_partials ops_partials(y, y_min, alpha); scalar_seq_view y_vec(y); scalar_seq_view y_min_vec(y_min); @@ -48,8 +49,6 @@ return_type_t pareto_lpdf(const T_y& y, } } - operands_and_partials ops_partials(y, y_min, alpha); - VectorBuilder::value, T_partials_return, T_y> log_y(size(y)); diff --git a/stan/math/prim/prob/pareto_rng.hpp b/stan/math/prim/prob/pareto_rng.hpp index 887188fdccc..8db960c9171 100644 --- a/stan/math/prim/prob/pareto_rng.hpp +++ b/stan/math/prim/prob/pareto_rng.hpp @@ -18,8 +18,8 @@ namespace math { * y_min and alpha can each be a scalar or a one-dimensional container. Any * non-scalar inputs must be the same size. * - * @tparam T_scale Type of scale parameter - * @tparam T_shape Type of shape parameter + * @tparam T_scale type of scale parameter + * @tparam T_shape type of shape parameter * @tparam RNG type of random number generator * @param y_min (Sequence of) positive scale parameter(s) * @param alpha (Sequence of) positive shape parameter(s) @@ -34,9 +34,7 @@ inline typename VectorBuilder::type pareto_rng( const T_scale& y_min, const T_shape& alpha, RNG& rng) { using boost::exponential_distribution; using boost::variate_generator; - static const char* function = "pareto_rng"; - check_positive_finite(function, "Scale parameter", y_min); check_positive_finite(function, "Shape parameter", alpha); check_consistent_sizes(function, "Scale Parameter", y_min, "Shape parameter", diff --git a/stan/math/prim/prob/pareto_type_2_cdf.hpp b/stan/math/prim/prob/pareto_type_2_cdf.hpp index 85995bcd3e4..3642ced7bf7 100644 --- a/stan/math/prim/prob/pareto_type_2_cdf.hpp +++ b/stan/math/prim/prob/pareto_type_2_cdf.hpp @@ -18,17 +18,9 @@ return_type_t pareto_type_2_cdf( const T_y& y, const T_loc& mu, const T_scale& lambda, const T_shape& alpha) { using T_partials_return = partials_return_t; - static const char* function = "pareto_type_2_cdf"; - - if (size_zero(y, mu, lambda, alpha)) { - return 1.0; - } - using std::log; using std::pow; - - T_partials_return P(1.0); - + static const char* function = "pareto_type_2_cdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_positive_finite(function, "Scale parameter", lambda); @@ -38,15 +30,20 @@ return_type_t pareto_type_2_cdf( alpha); check_greater_or_equal(function, "Random variable", y, mu); + if (size_zero(y, mu, lambda, alpha)) { + return 1.0; + } + + T_partials_return P(1.0); + operands_and_partials ops_partials( + y, mu, lambda, alpha); + scalar_seq_view y_vec(y); scalar_seq_view mu_vec(mu); scalar_seq_view lambda_vec(lambda); scalar_seq_view alpha_vec(alpha); size_t N = max_size(y, mu, lambda, alpha); - operands_and_partials ops_partials( - y, mu, lambda, alpha); - for (size_t n = 0; n < N; n++) { const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return mu_dbl = value_of(mu_vec[n]); diff --git a/stan/math/prim/prob/pareto_type_2_lccdf.hpp b/stan/math/prim/prob/pareto_type_2_lccdf.hpp index ae7e51b8680..d7633cf26ea 100644 --- a/stan/math/prim/prob/pareto_type_2_lccdf.hpp +++ b/stan/math/prim/prob/pareto_type_2_lccdf.hpp @@ -17,16 +17,8 @@ return_type_t pareto_type_2_lccdf( const T_y& y, const T_loc& mu, const T_scale& lambda, const T_shape& alpha) { using T_partials_return = partials_return_t; - static const char* function = "pareto_type_2_lccdf"; - - if (size_zero(y, mu, lambda, alpha)) { - return 0.0; - } - using std::log; - - T_partials_return P(0.0); - + static const char* function = "pareto_type_2_lccdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_positive_finite(function, "Scale parameter", lambda); @@ -36,15 +28,20 @@ return_type_t pareto_type_2_lccdf( alpha); check_greater_or_equal(function, "Random variable", y, mu); + if (size_zero(y, mu, lambda, alpha)) { + return 0; + } + + T_partials_return P(0.0); + operands_and_partials ops_partials( + y, mu, lambda, alpha); + scalar_seq_view y_vec(y); scalar_seq_view mu_vec(mu); scalar_seq_view lambda_vec(lambda); scalar_seq_view alpha_vec(alpha); size_t N = max_size(y, mu, lambda, alpha); - operands_and_partials ops_partials( - y, mu, lambda, alpha); - for (size_t n = 0; n < N; n++) { const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return mu_dbl = value_of(mu_vec[n]); diff --git a/stan/math/prim/prob/pareto_type_2_lcdf.hpp b/stan/math/prim/prob/pareto_type_2_lcdf.hpp index 0580b6b5dde..9e83568f985 100644 --- a/stan/math/prim/prob/pareto_type_2_lcdf.hpp +++ b/stan/math/prim/prob/pareto_type_2_lcdf.hpp @@ -18,17 +18,9 @@ return_type_t pareto_type_2_lcdf( const T_y& y, const T_loc& mu, const T_scale& lambda, const T_shape& alpha) { using T_partials_return = partials_return_t; - static const char* function = "pareto_type_2_lcdf"; - - if (size_zero(y, mu, lambda, alpha)) { - return 0.0; - } - using std::log; using std::pow; - - T_partials_return P(0.0); - + static const char* function = "pareto_type_2_lcdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_positive_finite(function, "Scale parameter", lambda); @@ -38,15 +30,20 @@ return_type_t pareto_type_2_lcdf( alpha); check_greater_or_equal(function, "Random variable", y, mu); + if (size_zero(y, mu, lambda, alpha)) { + return 0; + } + + T_partials_return P(0.0); + operands_and_partials ops_partials( + y, mu, lambda, alpha); + scalar_seq_view y_vec(y); scalar_seq_view mu_vec(mu); scalar_seq_view lambda_vec(lambda); scalar_seq_view alpha_vec(alpha); size_t N = max_size(y, mu, lambda, alpha); - operands_and_partials ops_partials( - y, mu, lambda, alpha); - for (size_t n = 0; n < N; n++) { const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return mu_dbl = value_of(mu_vec[n]); diff --git a/stan/math/prim/prob/pareto_type_2_lpdf.hpp b/stan/math/prim/prob/pareto_type_2_lpdf.hpp index f6a7e3bea4d..7c5f4b0da85 100644 --- a/stan/math/prim/prob/pareto_type_2_lpdf.hpp +++ b/stan/math/prim/prob/pareto_type_2_lpdf.hpp @@ -20,17 +20,9 @@ template pareto_type_2_lpdf( const T_y& y, const T_loc& mu, const T_scale& lambda, const T_shape& alpha) { - static const char* function = "pareto_type_2_lpdf"; using T_partials_return = partials_return_t; - using std::log; - - if (size_zero(y, mu, lambda, alpha)) { - return 0.0; - } - - T_partials_return logp(0.0); - + static const char* function = "pareto_type_2_lpdf"; check_not_nan(function, "Random variable", y); check_positive_finite(function, "Scale parameter", lambda); check_positive_finite(function, "Shape parameter", alpha); @@ -39,19 +31,23 @@ return_type_t pareto_type_2_lpdf( alpha); check_greater_or_equal(function, "Random variable", y, mu); + if (size_zero(y, mu, lambda, alpha)) { + return 0.0; + } if (!include_summand::value) { return 0.0; } + T_partials_return logp(0.0); + operands_and_partials ops_partials( + y, mu, lambda, alpha); + scalar_seq_view y_vec(y); scalar_seq_view mu_vec(mu); scalar_seq_view lambda_vec(lambda); scalar_seq_view alpha_vec(alpha); size_t N = max_size(y, mu, lambda, alpha); - operands_and_partials ops_partials( - y, mu, lambda, alpha); - VectorBuilder::value, T_partials_return, T_scale> log_lambda(size(lambda)); diff --git a/stan/math/prim/prob/pareto_type_2_rng.hpp b/stan/math/prim/prob/pareto_type_2_rng.hpp index da59ed47e22..f6108671821 100644 --- a/stan/math/prim/prob/pareto_type_2_rng.hpp +++ b/stan/math/prim/prob/pareto_type_2_rng.hpp @@ -20,10 +20,11 @@ namespace math { * mu, lambda, and alpha can each be a scalar or a one-dimensional container. * Any non-scalar inputs must be the same size. * - * @tparam T_loc Type of location parameter - * @tparam T_scale Type of scale parameter - * @tparam T_shape Type of shape parameter + * @tparam T_loc type of location parameter + * @tparam T_scale type of scale parameter + * @tparam T_shape type of shape parameter * @tparam RNG type of random number generator + * * @param mu (Sequence of) location parameter(s) * @param lambda (Sequence of) scale parameter(s) * @param alpha (Sequence of) shape parameter(s) @@ -41,7 +42,6 @@ pareto_type_2_rng(const T_loc& mu, const T_scale& lambda, const T_shape& alpha, using boost::random::uniform_real_distribution; using boost::variate_generator; static const char* function = "pareto_type_2_rng"; - check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", lambda); check_positive_finite(function, "Shape parameter", alpha); diff --git a/stan/math/prim/prob/poisson_cdf.hpp b/stan/math/prim/prob/poisson_cdf.hpp index 9934b33b0a6..67e6f93577c 100644 --- a/stan/math/prim/prob/poisson_cdf.hpp +++ b/stan/math/prim/prob/poisson_cdf.hpp @@ -19,23 +19,20 @@ namespace math { // Poisson CDF template return_type_t poisson_cdf(const T_n& n, const T_rate& lambda) { - static const char* function = "poisson_cdf"; using T_partials_return = partials_return_t; - - if (size_zero(n, lambda)) { - return 1.0; - } - - T_partials_return P(1.0); - + using std::exp; + using std::pow; + static const char* function = "poisson_cdf"; check_not_nan(function, "Rate parameter", lambda); check_nonnegative(function, "Rate parameter", lambda); check_consistent_sizes(function, "Random variable", n, "Rate parameter", lambda); - using std::exp; - using std::pow; + if (size_zero(n, lambda)) { + return 1.0; + } + T_partials_return P(1.0); operands_and_partials ops_partials(lambda); scalar_seq_view n_vec(n); diff --git a/stan/math/prim/prob/poisson_lccdf.hpp b/stan/math/prim/prob/poisson_lccdf.hpp index 5b1b7fbd7a4..b86b94a5964 100644 --- a/stan/math/prim/prob/poisson_lccdf.hpp +++ b/stan/math/prim/prob/poisson_lccdf.hpp @@ -20,23 +20,20 @@ namespace math { template return_type_t poisson_lccdf(const T_n& n, const T_rate& lambda) { - static const char* function = "poisson_lccdf"; using T_partials_return = partials_return_t; - - if (size_zero(n, lambda)) { - return 0.0; - } - - T_partials_return P(0.0); - + using std::exp; + using std::log; + static const char* function = "poisson_lccdf"; check_not_nan(function, "Rate parameter", lambda); check_nonnegative(function, "Rate parameter", lambda); check_consistent_sizes(function, "Random variable", n, "Rate parameter", lambda); - using std::exp; - using std::log; + if (size_zero(n, lambda)) { + return 0; + } + T_partials_return P(0.0); operands_and_partials ops_partials(lambda); scalar_seq_view n_vec(n); diff --git a/stan/math/prim/prob/poisson_lcdf.hpp b/stan/math/prim/prob/poisson_lcdf.hpp index 06e587f5932..49e507bd961 100644 --- a/stan/math/prim/prob/poisson_lcdf.hpp +++ b/stan/math/prim/prob/poisson_lcdf.hpp @@ -20,23 +20,20 @@ namespace math { template return_type_t poisson_lcdf(const T_n& n, const T_rate& lambda) { - static const char* function = "poisson_lcdf"; using T_partials_return = partials_return_t; - - if (size_zero(n, lambda)) { - return 0.0; - } - - T_partials_return P(0.0); - + using std::exp; + using std::log; + static const char* function = "poisson_lcdf"; check_not_nan(function, "Rate parameter", lambda); check_nonnegative(function, "Rate parameter", lambda); check_consistent_sizes(function, "Random variable", n, "Rate parameter", lambda); - using std::exp; - using std::log; + if (size_zero(n, lambda)) { + return 0; + } + T_partials_return P(0.0); operands_and_partials ops_partials(lambda); scalar_seq_view n_vec(n); diff --git a/stan/math/prim/prob/poisson_log_glm_lpmf.hpp b/stan/math/prim/prob/poisson_log_glm_lpmf.hpp index 29b3e709b1c..8571c5c3526 100644 --- a/stan/math/prim/prob/poisson_log_glm_lpmf.hpp +++ b/stan/math/prim/prob/poisson_log_glm_lpmf.hpp @@ -21,6 +21,7 @@ namespace math { * compute a more efficient version of poisson_log_lpmf(y, alpha + x * beta) * by using analytically simplified gradients. * If containers are supplied, returns the log sum of the probabilities. + * * @tparam T_y type of vector of variates (labels), integers >=0; * this can also be a single positive integer; * @tparam T_x_scalar type of a scalar in the matrix of independent variables @@ -48,8 +49,6 @@ template poisson_log_glm_lpmf( const T_y& y, const Eigen::Matrix& x, const T_alpha& alpha, const T_beta& beta) { - static const char* function = "poisson_log_glm_lpmf"; - using Eigen::Array; using Eigen::Dynamic; using Eigen::Matrix; @@ -66,6 +65,7 @@ return_type_t poisson_log_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 = "poisson_log_glm_lpmf"; check_consistent_size(function, "Vector of dependent variables", y, N_instances); check_consistent_size(function, "Weight vector", beta, N_attributes); diff --git a/stan/math/prim/prob/poisson_log_lpmf.hpp b/stan/math/prim/prob/poisson_log_lpmf.hpp index 50049bf83d2..35bdc7dbc2a 100644 --- a/stan/math/prim/prob/poisson_log_lpmf.hpp +++ b/stan/math/prim/prob/poisson_log_lpmf.hpp @@ -20,26 +20,23 @@ template return_type_t poisson_log_lpmf(const T_n& n, const T_log_rate& alpha) { using T_partials_return = partials_return_t; - - static const char* function = "poisson_log_lpmf"; - using std::exp; - - if (size_zero(n, alpha)) { - return 0.0; - } - - T_partials_return logp(0.0); - + static const char* function = "poisson_log_lpmf"; check_nonnegative(function, "Random variable", n); check_not_nan(function, "Log rate parameter", alpha); check_consistent_sizes(function, "Random variable", n, "Log rate parameter", alpha); + if (size_zero(n, alpha)) { + return 0.0; + } if (!include_summand::value) { return 0.0; } + T_partials_return logp(0.0); + operands_and_partials ops_partials(alpha); + scalar_seq_view n_vec(n); scalar_seq_view alpha_vec(alpha); size_t max_size_seq_view = max_size(n, alpha); @@ -56,8 +53,6 @@ return_type_t poisson_log_lpmf(const T_n& n, } } - operands_and_partials ops_partials(alpha); - VectorBuilder::value, T_partials_return, T_n> lgamma_n_plus_one(size(n)); if (include_summand::value) { diff --git a/stan/math/prim/prob/poisson_log_rng.hpp b/stan/math/prim/prob/poisson_log_rng.hpp index 8fc6260a174..9d1364791f8 100644 --- a/stan/math/prim/prob/poisson_log_rng.hpp +++ b/stan/math/prim/prob/poisson_log_rng.hpp @@ -30,10 +30,8 @@ inline typename VectorBuilder::type poisson_log_rng( const T_rate& alpha, RNG& rng) { using boost::random::poisson_distribution; using boost::variate_generator; - static const char* function = "poisson_log_rng"; static const double POISSON_MAX_LOG_RATE = 30 * LOG_TWO; - check_finite(function, "Log rate parameter", alpha); check_less(function, "Log rate parameter", alpha, POISSON_MAX_LOG_RATE); diff --git a/stan/math/prim/prob/poisson_lpmf.hpp b/stan/math/prim/prob/poisson_lpmf.hpp index 2f8e7397893..9a63f9d6c84 100644 --- a/stan/math/prim/prob/poisson_lpmf.hpp +++ b/stan/math/prim/prob/poisson_lpmf.hpp @@ -19,25 +19,23 @@ namespace math { template return_type_t poisson_lpmf(const T_n& n, const T_rate& lambda) { using T_partials_return = partials_return_t; - static const char* function = "poisson_lpmf"; - - if (size_zero(n, lambda)) { - return 0.0; - } - - T_partials_return logp(0.0); - check_nonnegative(function, "Random variable", n); check_not_nan(function, "Rate parameter", lambda); check_nonnegative(function, "Rate parameter", lambda); check_consistent_sizes(function, "Random variable", n, "Rate parameter", lambda); + if (size_zero(n, lambda)) { + return 0.0; + } if (!include_summand::value) { return 0.0; } + T_partials_return logp(0.0); + operands_and_partials ops_partials(lambda); + scalar_seq_view n_vec(n); scalar_seq_view lambda_vec(lambda); size_t max_size_seq_view = max_size(n, lambda); @@ -54,8 +52,6 @@ return_type_t poisson_lpmf(const T_n& n, const T_rate& lambda) { } } - operands_and_partials ops_partials(lambda); - VectorBuilder::value, T_partials_return, T_n> lgamma_n_plus_one(size(n)); if (include_summand::value) { diff --git a/stan/math/prim/prob/poisson_rng.hpp b/stan/math/prim/prob/poisson_rng.hpp index 34ccdadb9a2..c1a0532ff5e 100644 --- a/stan/math/prim/prob/poisson_rng.hpp +++ b/stan/math/prim/prob/poisson_rng.hpp @@ -29,9 +29,7 @@ inline typename VectorBuilder::type poisson_rng( const T_rate& lambda, RNG& rng) { using boost::random::poisson_distribution; using boost::variate_generator; - static const char* function = "poisson_rng"; - check_not_nan(function, "Rate parameter", lambda); check_positive(function, "Rate parameter", lambda); check_less(function, "Rate parameter", lambda, POISSON_MAX_RATE); diff --git a/stan/math/prim/prob/rayleigh_cdf.hpp b/stan/math/prim/prob/rayleigh_cdf.hpp index 5ac3e22a992..8ba8129f2d1 100644 --- a/stan/math/prim/prob/rayleigh_cdf.hpp +++ b/stan/math/prim/prob/rayleigh_cdf.hpp @@ -16,17 +16,9 @@ namespace math { template return_type_t rayleigh_cdf(const T_y& y, const T_scale& sigma) { - static const char* function = "rayleigh_cdf"; using T_partials_return = partials_return_t; - using std::exp; - - T_partials_return cdf(1.0); - - if (size_zero(y, sigma)) { - return cdf; - } - + static const char* function = "rayleigh_cdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_not_nan(function, "Scale parameter", sigma); @@ -34,6 +26,11 @@ return_type_t rayleigh_cdf(const T_y& y, const T_scale& sigma) { check_consistent_sizes(function, "Random variable", y, "Scale parameter", sigma); + if (size_zero(y, sigma)) { + return 1.0; + } + + T_partials_return cdf(1.0); operands_and_partials ops_partials(y, sigma); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/rayleigh_lccdf.hpp b/stan/math/prim/prob/rayleigh_lccdf.hpp index e6e52766b0b..7fd9bb2eb93 100644 --- a/stan/math/prim/prob/rayleigh_lccdf.hpp +++ b/stan/math/prim/prob/rayleigh_lccdf.hpp @@ -14,15 +14,8 @@ namespace math { template return_type_t rayleigh_lccdf(const T_y& y, const T_scale& sigma) { - static const char* function = "rayleigh_lccdf"; using T_partials_return = partials_return_t; - - T_partials_return ccdf_log(0.0); - - if (size_zero(y, sigma)) { - return ccdf_log; - } - + static const char* function = "rayleigh_lccdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_not_nan(function, "Scale parameter", sigma); @@ -30,6 +23,11 @@ return_type_t rayleigh_lccdf(const T_y& y, const T_scale& sigma) { check_consistent_sizes(function, "Random variable", y, "Scale parameter", sigma); + if (size_zero(y, sigma)) { + return 0; + } + + T_partials_return ccdf_log(0.0); operands_and_partials ops_partials(y, sigma); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/rayleigh_lcdf.hpp b/stan/math/prim/prob/rayleigh_lcdf.hpp index d0e5e161f8c..29ffb97eecc 100644 --- a/stan/math/prim/prob/rayleigh_lcdf.hpp +++ b/stan/math/prim/prob/rayleigh_lcdf.hpp @@ -16,17 +16,9 @@ namespace math { template return_type_t rayleigh_lcdf(const T_y& y, const T_scale& sigma) { - static const char* function = "rayleigh_lcdf"; using T_partials_return = partials_return_t; - using std::exp; - - T_partials_return cdf_log(0.0); - - if (size_zero(y, sigma)) { - return cdf_log; - } - + static const char* function = "rayleigh_lcdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_not_nan(function, "Scale parameter", sigma); @@ -34,6 +26,11 @@ return_type_t rayleigh_lcdf(const T_y& y, const T_scale& sigma) { check_consistent_sizes(function, "Random variable", y, "Scale parameter", sigma); + if (size_zero(y, sigma)) { + return 0; + } + + T_partials_return cdf_log(0.0); operands_and_partials ops_partials(y, sigma); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/rayleigh_lpdf.hpp b/stan/math/prim/prob/rayleigh_lpdf.hpp index 91f20c12bb4..6d17ab8d8a5 100644 --- a/stan/math/prim/prob/rayleigh_lpdf.hpp +++ b/stan/math/prim/prob/rayleigh_lpdf.hpp @@ -16,27 +16,23 @@ namespace math { template return_type_t rayleigh_lpdf(const T_y& y, const T_scale& sigma) { - static const char* function = "rayleigh_lpdf"; using T_partials_return = partials_return_t; - using std::log; - - if (size_zero(y, sigma)) { - return 0.0; - } - - T_partials_return logp(0.0); - + static const char* function = "rayleigh_lpdf"; check_not_nan(function, "Random variable", y); check_positive(function, "Scale parameter", sigma); check_positive(function, "Random variable", y); check_consistent_sizes(function, "Random variable", y, "Scale parameter", sigma); + if (size_zero(y, sigma)) { + return 0.0; + } if (!include_summand::value) { return 0.0; } + T_partials_return logp(0.0); operands_and_partials ops_partials(y, sigma); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/rayleigh_rng.hpp b/stan/math/prim/prob/rayleigh_rng.hpp index 1465b70a34c..a767d0b705d 100644 --- a/stan/math/prim/prob/rayleigh_rng.hpp +++ b/stan/math/prim/prob/rayleigh_rng.hpp @@ -29,9 +29,7 @@ inline typename VectorBuilder::type rayleigh_rng( const T_scale& sigma, RNG& rng) { using boost::random::uniform_real_distribution; using boost::variate_generator; - static const char* function = "rayleigh_rng"; - check_positive_finite(function, "Scale parameter", sigma); scalar_seq_view sigma_vec(sigma); diff --git a/stan/math/prim/prob/scaled_inv_chi_square_cdf.hpp b/stan/math/prim/prob/scaled_inv_chi_square_cdf.hpp index 42fc36e894d..cdf5d450848 100644 --- a/stan/math/prim/prob/scaled_inv_chi_square_cdf.hpp +++ b/stan/math/prim/prob/scaled_inv_chi_square_cdf.hpp @@ -22,29 +22,23 @@ namespace math { * The CDF of a scaled inverse chi-squared density for y with the * specified degrees of freedom parameter and scale parameter. * + * @tparam T_y type of scalar + * @tparam T_dof type of degrees of freedom * @param y A scalar variable. * @param nu Degrees of freedom. * @param s Scale parameter. * @throw std::domain_error if nu is not greater than 0 * @throw std::domain_error if s is not greater than 0. * @throw std::domain_error if y is not greater than 0. - * @tparam T_y Type of scalar. - * @tparam T_dof Type of degrees of freedom. */ template return_type_t scaled_inv_chi_square_cdf(const T_y& y, const T_dof& nu, const T_scale& s) { using T_partials_return = partials_return_t; - - if (size_zero(y, nu, s)) { - return 1.0; - } - + using std::exp; + using std::pow; static const char* function = "scaled_inv_chi_square_cdf"; - - T_partials_return P(1.0); - check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_positive_finite(function, "Degrees of freedom parameter", nu); @@ -53,13 +47,18 @@ return_type_t scaled_inv_chi_square_cdf(const T_y& y, "Degrees of freedom parameter", nu, "Scale parameter", s); + if (size_zero(y, nu, s)) { + return 1.0; + } + + T_partials_return P(1.0); + operands_and_partials ops_partials(y, nu, s); + scalar_seq_view y_vec(y); scalar_seq_view nu_vec(nu); scalar_seq_view s_vec(s); size_t N = max_size(y, nu, s); - operands_and_partials ops_partials(y, nu, s); - // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::math::size(y); i++) { @@ -68,9 +67,6 @@ return_type_t scaled_inv_chi_square_cdf(const T_y& y, } } - using std::exp; - using std::pow; - VectorBuilder::value, T_partials_return, T_dof> gamma_vec(size(nu)); VectorBuilder::value, T_partials_return, T_dof> diff --git a/stan/math/prim/prob/scaled_inv_chi_square_lccdf.hpp b/stan/math/prim/prob/scaled_inv_chi_square_lccdf.hpp index 62e2ea4716b..749e70044d0 100644 --- a/stan/math/prim/prob/scaled_inv_chi_square_lccdf.hpp +++ b/stan/math/prim/prob/scaled_inv_chi_square_lccdf.hpp @@ -23,15 +23,10 @@ template return_type_t scaled_inv_chi_square_lccdf( const T_y& y, const T_dof& nu, const T_scale& s) { using T_partials_return = partials_return_t; - - if (size_zero(y, nu, s)) { - return 0.0; - } - + using std::exp; + using std::log; + using std::pow; static const char* function = "scaled_inv_chi_square_lccdf"; - - T_partials_return P(0.0); - check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_positive_finite(function, "Degrees of freedom parameter", nu); @@ -40,13 +35,18 @@ return_type_t scaled_inv_chi_square_lccdf( "Degrees of freedom parameter", nu, "Scale parameter", s); + if (size_zero(y, nu, s)) { + return 0; + } + + T_partials_return P(0.0); + operands_and_partials ops_partials(y, nu, s); + scalar_seq_view y_vec(y); scalar_seq_view nu_vec(nu); scalar_seq_view s_vec(s); size_t N = max_size(y, nu, s); - operands_and_partials ops_partials(y, nu, s); - // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::math::size(y); i++) { @@ -55,10 +55,6 @@ return_type_t scaled_inv_chi_square_lccdf( } } - using std::exp; - using std::log; - using std::pow; - VectorBuilder::value, T_partials_return, T_dof> gamma_vec(size(nu)); VectorBuilder::value, T_partials_return, T_dof> diff --git a/stan/math/prim/prob/scaled_inv_chi_square_lcdf.hpp b/stan/math/prim/prob/scaled_inv_chi_square_lcdf.hpp index 100595416e1..735c7f94ce3 100644 --- a/stan/math/prim/prob/scaled_inv_chi_square_lcdf.hpp +++ b/stan/math/prim/prob/scaled_inv_chi_square_lcdf.hpp @@ -23,15 +23,10 @@ template return_type_t scaled_inv_chi_square_lcdf( const T_y& y, const T_dof& nu, const T_scale& s) { using T_partials_return = partials_return_t; - - if (size_zero(y, nu, s)) { - return 0.0; - } - + using std::exp; + using std::log; + using std::pow; static const char* function = "scaled_inv_chi_square_lcdf"; - - T_partials_return P(0.0); - check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); check_positive_finite(function, "Degrees of freedom parameter", nu); @@ -40,13 +35,18 @@ return_type_t scaled_inv_chi_square_lcdf( "Degrees of freedom parameter", nu, "Scale parameter", s); + if (size_zero(y, nu, s)) { + return 0; + } + + T_partials_return P(0.0); + operands_and_partials ops_partials(y, nu, s); + scalar_seq_view y_vec(y); scalar_seq_view nu_vec(nu); scalar_seq_view s_vec(s); size_t N = max_size(y, nu, s); - operands_and_partials ops_partials(y, nu, s); - // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::math::size(y); i++) { @@ -55,10 +55,6 @@ return_type_t scaled_inv_chi_square_lcdf( } } - using std::exp; - using std::log; - using std::pow; - VectorBuilder::value, T_partials_return, T_dof> gamma_vec(size(nu)); VectorBuilder::value, T_partials_return, T_dof> diff --git a/stan/math/prim/prob/scaled_inv_chi_square_lpdf.hpp b/stan/math/prim/prob/scaled_inv_chi_square_lpdf.hpp index 4829b8f8f4f..a32f11e5da8 100644 --- a/stan/math/prim/prob/scaled_inv_chi_square_lpdf.hpp +++ b/stan/math/prim/prob/scaled_inv_chi_square_lpdf.hpp @@ -28,34 +28,39 @@ namespace math { &=& \frac{\nu}{2} \log(\frac{\nu}{2}) - \log (\Gamma (\nu / 2)) + \nu \log(s) - (\frac{\nu}{2} + 1) \log(y) - \frac{\nu s^2}{2y} \\ & & \mathrm{ where } \; y > 0 \f} + * + * @tparam T_y type of scalar + * @tparam T_dof type of degrees of freedom * @param y A scalar variable. * @param nu Degrees of freedom. * @param s Scale parameter. * @throw std::domain_error if nu is not greater than 0 * @throw std::domain_error if s is not greater than 0. * @throw std::domain_error if y is not greater than 0. - * @tparam T_y Type of scalar. - * @tparam T_dof Type of degrees of freedom. */ template return_type_t scaled_inv_chi_square_lpdf( const T_y& y, const T_dof& nu, const T_scale& s) { - static const char* function = "scaled_inv_chi_square_lpdf"; using T_partials_return = partials_return_t; - + using std::log; + static const char* function = "scaled_inv_chi_square_lpdf"; check_not_nan(function, "Random variable", y); check_positive_finite(function, "Degrees of freedom parameter", nu); check_positive_finite(function, "Scale parameter", s); check_consistent_sizes(function, "Random variable", y, "Degrees of freedom parameter", nu, "Scale parameter", s); + if (size_zero(y, nu, s)) { return 0; } if (!include_summand::value) { return 0; } + T_partials_return logp(0); + operands_and_partials ops_partials(y, nu, s); + scalar_seq_view y_vec(y); scalar_seq_view nu_vec(nu); scalar_seq_view s_vec(s); @@ -67,8 +72,6 @@ return_type_t scaled_inv_chi_square_lpdf( } } - using std::log; - VectorBuilder::value, T_partials_return, T_dof> half_nu(size(nu)); @@ -123,7 +126,6 @@ return_type_t scaled_inv_chi_square_lpdf( } } - operands_and_partials ops_partials(y, nu, s); for (size_t n = 0; n < N; n++) { const T_partials_return s_dbl = value_of(s_vec[n]); const T_partials_return nu_dbl = value_of(nu_vec[n]); diff --git a/stan/math/prim/prob/scaled_inv_chi_square_rng.hpp b/stan/math/prim/prob/scaled_inv_chi_square_rng.hpp index 569fb6b012f..f9648899f94 100644 --- a/stan/math/prim/prob/scaled_inv_chi_square_rng.hpp +++ b/stan/math/prim/prob/scaled_inv_chi_square_rng.hpp @@ -18,8 +18,8 @@ namespace math { * nu and sigma can each be a scalar or a one-dimensional container. Any * non-scalar inputs must be the same size. * - * @tparam T_deg Type of degrees of freedom parameter - * @tparam T_scale Type of scale parameter + * @tparam T_deg type of degrees of freedom parameter + * @tparam T_scale type of scale parameter * @tparam RNG type of random number generator * @param nu (Sequence of) positive degrees of freedom parameter(s) * @param s (Sequence of) positive scale parameter(s) @@ -34,9 +34,7 @@ inline typename VectorBuilder::type scaled_inv_chi_square_rng(const T_deg& nu, const T_scale& s, RNG& rng) { using boost::random::chi_squared_distribution; using boost::variate_generator; - static const char* function = "scaled_inv_chi_square_rng"; - check_positive_finite(function, "Degrees of freedom parameter", nu); check_positive_finite(function, "Scale parameter", s); check_consistent_sizes(function, "Location parameter", nu, "Scale Parameter", diff --git a/stan/math/prim/prob/skew_normal_cdf.hpp b/stan/math/prim/prob/skew_normal_cdf.hpp index f4e451ef10c..eb323553e0d 100644 --- a/stan/math/prim/prob/skew_normal_cdf.hpp +++ b/stan/math/prim/prob/skew_normal_cdf.hpp @@ -20,15 +20,9 @@ namespace math { template return_type_t skew_normal_cdf( const T_y& y, const T_loc& mu, const T_scale& sigma, const T_shape& alpha) { - static const char* function = "skew_normal_cdf"; using T_partials_return = partials_return_t; - - T_partials_return cdf(1.0); - - if (size_zero(y, mu, sigma, alpha)) { - return cdf; - } - + using std::exp; + static const char* function = "skew_normal_cdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_not_nan(function, "Scale parameter", sigma); @@ -38,11 +32,13 @@ return_type_t skew_normal_cdf( check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma, "Shape paramter", alpha); + if (size_zero(y, mu, sigma, alpha)) { + return 1.0; + } + + T_partials_return cdf(1.0); operands_and_partials ops_partials(y, mu, sigma, alpha); - - using std::exp; - scalar_seq_view y_vec(y); scalar_seq_view mu_vec(mu); scalar_seq_view sigma_vec(sigma); diff --git a/stan/math/prim/prob/skew_normal_lccdf.hpp b/stan/math/prim/prob/skew_normal_lccdf.hpp index 68895a84622..1a922003a57 100644 --- a/stan/math/prim/prob/skew_normal_lccdf.hpp +++ b/stan/math/prim/prob/skew_normal_lccdf.hpp @@ -20,15 +20,10 @@ namespace math { template return_type_t skew_normal_lccdf( const T_y& y, const T_loc& mu, const T_scale& sigma, const T_shape& alpha) { - static const char* function = "skew_normal_lccdf"; using T_partials_return = partials_return_t; - - T_partials_return ccdf_log(0.0); - - if (size_zero(y, mu, sigma, alpha)) { - return ccdf_log; - } - + using std::exp; + using std::log; + static const char* function = "skew_normal_lccdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_not_nan(function, "Scale parameter", sigma); @@ -38,12 +33,13 @@ return_type_t skew_normal_lccdf( check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma, "Shape paramter", alpha); + if (size_zero(y, mu, sigma, alpha)) { + return 0; + } + + T_partials_return ccdf_log(0.0); operands_and_partials ops_partials(y, mu, sigma, alpha); - - using std::exp; - using std::log; - scalar_seq_view y_vec(y); scalar_seq_view mu_vec(mu); scalar_seq_view sigma_vec(sigma); diff --git a/stan/math/prim/prob/skew_normal_lcdf.hpp b/stan/math/prim/prob/skew_normal_lcdf.hpp index 67860183331..2c1d5983995 100644 --- a/stan/math/prim/prob/skew_normal_lcdf.hpp +++ b/stan/math/prim/prob/skew_normal_lcdf.hpp @@ -20,15 +20,10 @@ namespace math { template return_type_t skew_normal_lcdf( const T_y& y, const T_loc& mu, const T_scale& sigma, const T_shape& alpha) { - static const char* function = "skew_normal_lcdf"; using T_partials_return = partials_return_t; - - T_partials_return cdf_log(0.0); - - if (size_zero(y, mu, sigma, alpha)) { - return cdf_log; - } - + using std::exp; + using std::log; + static const char* function = "skew_normal_lcdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_not_nan(function, "Scale parameter", sigma); @@ -38,12 +33,13 @@ return_type_t skew_normal_lcdf( check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma, "Shape paramter", alpha); + if (size_zero(y, mu, sigma, alpha)) { + return 0; + } + + T_partials_return cdf_log(0.0); operands_and_partials ops_partials(y, mu, sigma, alpha); - - using std::exp; - using std::log; - scalar_seq_view y_vec(y); scalar_seq_view mu_vec(mu); scalar_seq_view sigma_vec(sigma); diff --git a/stan/math/prim/prob/skew_normal_lpdf.hpp b/stan/math/prim/prob/skew_normal_lpdf.hpp index 4816fee47c9..f2547d14c17 100644 --- a/stan/math/prim/prob/skew_normal_lpdf.hpp +++ b/stan/math/prim/prob/skew_normal_lpdf.hpp @@ -21,18 +21,10 @@ template return_type_t skew_normal_lpdf( const T_y& y, const T_loc& mu, const T_scale& sigma, const T_shape& alpha) { - static const char* function = "skew_normal_lpdf"; using T_partials_return = partials_return_t; - using std::exp; using std::log; - - if (size_zero(y, mu, sigma, alpha)) { - return 0.0; - } - - T_partials_return logp(0.0); - + static const char* function = "skew_normal_lpdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_finite(function, "Shape parameter", alpha); @@ -40,13 +32,16 @@ return_type_t skew_normal_lpdf( check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", sigma, "Shape paramter", alpha); + if (size_zero(y, mu, sigma, alpha)) { + return 0.0; + } if (!include_summand::value) { return 0.0; } + T_partials_return logp(0.0); operands_and_partials ops_partials(y, mu, sigma, alpha); - scalar_seq_view y_vec(y); scalar_seq_view mu_vec(mu); scalar_seq_view sigma_vec(sigma); diff --git a/stan/math/prim/prob/skew_normal_rng.hpp b/stan/math/prim/prob/skew_normal_rng.hpp index 34808b171af..926b5db2466 100644 --- a/stan/math/prim/prob/skew_normal_rng.hpp +++ b/stan/math/prim/prob/skew_normal_rng.hpp @@ -18,10 +18,11 @@ namespace math { * mu, sigma, and alpha can each be a scalar or a one-dimensional container. Any * non-scalar inputs must be the same size. * - * @tparam T_loc Type of location parameter - * @tparam T_scale Type of scale parameter - * @tparam T_shape Type of shape parameter + * @tparam T_loc type of location parameter + * @tparam T_scale type of scale parameter + * @tparam T_shape type of shape parameter * @tparam RNG type of random number generator + * * @param mu (Sequence of) location parameter(s) * @param sigma (Sequence of) scale parameter(s) * @param alpha (Sequence of) shape parameter(s) @@ -39,7 +40,6 @@ skew_normal_rng(const T_loc& mu, const T_scale& sigma, const T_shape& alpha, using boost::random::normal_distribution; using boost::variate_generator; static const char* function = "skew_normal_rng"; - check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); check_finite(function, "Shape parameter", alpha); diff --git a/stan/math/prim/prob/std_normal_cdf.hpp b/stan/math/prim/prob/std_normal_cdf.hpp index d68351d378c..84fd8d51867 100644 --- a/stan/math/prim/prob/std_normal_cdf.hpp +++ b/stan/math/prim/prob/std_normal_cdf.hpp @@ -29,17 +29,14 @@ template inline return_type_t std_normal_cdf(const T_y& y) { using T_partials_return = partials_return_t; using std::exp; - static const char* function = "std_normal_cdf"; - - T_partials_return cdf(1.0); + check_not_nan(function, "Random variable", y); if (size_zero(y)) { - return cdf; + return 1.0; } - check_not_nan(function, "Random variable", y); - + T_partials_return cdf(1.0); operands_and_partials ops_partials(y); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/std_normal_lccdf.hpp b/stan/math/prim/prob/std_normal_lccdf.hpp index d83bb5939ac..05abc951081 100644 --- a/stan/math/prim/prob/std_normal_lccdf.hpp +++ b/stan/math/prim/prob/std_normal_lccdf.hpp @@ -21,17 +21,16 @@ inline return_type_t std_normal_lccdf(const T_y& y) { using T_partials_return = partials_return_t; using std::exp; using std::log; - static const char* function = "std_normal_lccdf"; + check_not_nan(function, "Random variable", y); - T_partials_return lccdf(0.0); if (size_zero(y)) { - return lccdf; + return 0; } - check_not_nan(function, "Random variable", y); - + T_partials_return lccdf(0.0); operands_and_partials ops_partials(y); + scalar_seq_view y_vec(y); size_t N = stan::math::size(y); diff --git a/stan/math/prim/prob/std_normal_lcdf.hpp b/stan/math/prim/prob/std_normal_lcdf.hpp index 2ce2e1705f6..17487f73862 100644 --- a/stan/math/prim/prob/std_normal_lcdf.hpp +++ b/stan/math/prim/prob/std_normal_lcdf.hpp @@ -26,16 +26,14 @@ inline return_type_t std_normal_lcdf(const T_y& y) { using std::fabs; using std::log; using std::pow; - static const char* function = "std_normal_lcdf"; + check_not_nan(function, "Random variable", y); - T_partials_return lcdf(0.0); if (size_zero(y)) { - return lcdf; + return 0; } - check_not_nan(function, "Random variable", y); - + T_partials_return lcdf(0.0); operands_and_partials ops_partials(y); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/std_normal_lpdf.hpp b/stan/math/prim/prob/std_normal_lpdf.hpp index 5c1c9f20f7a..d57687dc073 100644 --- a/stan/math/prim/prob/std_normal_lpdf.hpp +++ b/stan/math/prim/prob/std_normal_lpdf.hpp @@ -18,7 +18,8 @@ namespace math { * *

The result log probability is defined to be the sum of the * log probabilities for each observation. - * @tparam T_y Underlying type of scalar in sequence. + * + * @tparam T_y type of scalar * @param y (Sequence of) scalar(s). * @return The log of the product of the densities. * @throw std::domain_error if any scalar is nan. @@ -27,20 +28,18 @@ template return_type_t std_normal_lpdf(const T_y& y) { static const char* function = "std_normal_lpdf"; using T_partials_return = partials_return_t; + check_not_nan(function, "Random variable", y); if (size_zero(y)) { return 0.0; } - - check_not_nan(function, "Random variable", y); - if (!include_summand::value) { return 0.0; } + T_partials_return logp(0.0); operands_and_partials ops_partials(y); - T_partials_return logp(0.0); scalar_seq_view y_vec(y); size_t N = stan::math::size(y); diff --git a/stan/math/prim/prob/student_t_cdf.hpp b/stan/math/prim/prob/student_t_cdf.hpp index 66f5ae8d962..e0641d1fe04 100644 --- a/stan/math/prim/prob/student_t_cdf.hpp +++ b/stan/math/prim/prob/student_t_cdf.hpp @@ -23,29 +23,27 @@ return_type_t student_t_cdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { using T_partials_return = partials_return_t; - - if (size_zero(y, nu, mu, sigma)) { - return 1.0; - } - + using std::exp; + using std::pow; static const char* function = "student_t_cdf"; - - T_partials_return P(1.0); - check_not_nan(function, "Random variable", y); check_positive_finite(function, "Degrees of freedom parameter", nu); check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); + if (size_zero(y, nu, mu, sigma)) { + return 1.0; + } + + T_partials_return P(1.0); + operands_and_partials ops_partials(y, nu, mu, + sigma); scalar_seq_view y_vec(y); scalar_seq_view nu_vec(nu); scalar_seq_view mu_vec(mu); scalar_seq_view sigma_vec(sigma); size_t N = max_size(y, nu, mu, sigma); - operands_and_partials ops_partials(y, nu, mu, - sigma); - // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::math::size(y); i++) { @@ -54,9 +52,6 @@ return_type_t student_t_cdf(const T_y& y, } } - using std::exp; - using std::pow; - T_partials_return digammaHalf = 0; VectorBuilder::value, T_partials_return, T_dof> diff --git a/stan/math/prim/prob/student_t_lccdf.hpp b/stan/math/prim/prob/student_t_lccdf.hpp index 083e2a8b364..5a52d793100 100644 --- a/stan/math/prim/prob/student_t_lccdf.hpp +++ b/stan/math/prim/prob/student_t_lccdf.hpp @@ -22,29 +22,28 @@ template return_type_t student_t_lccdf( const T_y& y, const T_dof& nu, const T_loc& mu, const T_scale& sigma) { using T_partials_return = partials_return_t; - - if (size_zero(y, nu, mu, sigma)) { - return 0.0; - } - + using std::exp; + using std::log; + using std::pow; static const char* function = "student_t_lccdf"; - - T_partials_return P(0.0); - check_not_nan(function, "Random variable", y); check_positive_finite(function, "Degrees of freedom parameter", nu); check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); + if (size_zero(y, nu, mu, sigma)) { + return 0; + } + + T_partials_return P(0.0); + operands_and_partials ops_partials(y, nu, mu, + sigma); scalar_seq_view y_vec(y); scalar_seq_view nu_vec(nu); scalar_seq_view mu_vec(mu); scalar_seq_view sigma_vec(sigma); size_t N = max_size(y, nu, mu, sigma); - operands_and_partials ops_partials(y, nu, mu, - sigma); - // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::math::size(y); i++) { @@ -53,10 +52,6 @@ return_type_t student_t_lccdf( } } - using std::exp; - using std::log; - using std::pow; - T_partials_return digammaHalf = 0; VectorBuilder::value, T_partials_return, T_dof> diff --git a/stan/math/prim/prob/student_t_lcdf.hpp b/stan/math/prim/prob/student_t_lcdf.hpp index 74d383791c3..68eeaeedd4b 100644 --- a/stan/math/prim/prob/student_t_lcdf.hpp +++ b/stan/math/prim/prob/student_t_lcdf.hpp @@ -24,29 +24,28 @@ return_type_t student_t_lcdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { using T_partials_return = partials_return_t; - - if (size_zero(y, nu, mu, sigma)) { - return 0.0; - } - + using std::exp; + using std::log; + using std::pow; static const char* function = "student_t_lcdf"; - - T_partials_return P(0.0); - check_not_nan(function, "Random variable", y); check_positive_finite(function, "Degrees of freedom parameter", nu); check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); + if (size_zero(y, nu, mu, sigma)) { + return 0; + } + + T_partials_return P(0.0); + operands_and_partials ops_partials(y, nu, mu, + sigma); scalar_seq_view y_vec(y); scalar_seq_view nu_vec(nu); scalar_seq_view mu_vec(mu); scalar_seq_view sigma_vec(sigma); size_t N = max_size(y, nu, mu, sigma); - operands_and_partials ops_partials(y, nu, mu, - sigma); - // Explicit return for extreme values // The gradients are technically ill-defined, but treated as zero for (size_t i = 0; i < stan::math::size(y); i++) { @@ -55,10 +54,6 @@ return_type_t student_t_lcdf(const T_y& y, } } - using std::exp; - using std::log; - using std::pow; - T_partials_return digammaHalf = 0; VectorBuilder::value, T_partials_return, T_dof> diff --git a/stan/math/prim/prob/student_t_lpdf.hpp b/stan/math/prim/prob/student_t_lpdf.hpp index af68c436c3d..4424a744cbd 100644 --- a/stan/math/prim/prob/student_t_lpdf.hpp +++ b/stan/math/prim/prob/student_t_lpdf.hpp @@ -34,6 +34,11 @@ namespace math { -\frac{\nu + 1}{2} \log (1 + \frac{1}{\nu} (\frac{y - \mu}{\sigma})^2) \f} * + * @tparam T_y type of scalar + * @tparam T_dof type of degrees of freedom + * @tparam T_loc type of location + * @tparam T_scale type of scale + * * @param y A scalar variable. * @param nu Degrees of freedom. * @param mu The mean of the Student-t distribution. @@ -41,10 +46,6 @@ namespace math { * @return The log of the Student-t density at y. * @throw std::domain_error if sigma is not greater than 0. * @throw std::domain_error if nu is not greater than 0. - * @tparam T_y Type of scalar. - * @tparam T_dof Type of degrees of freedom. - * @tparam T_loc Type of location. - * @tparam T_scale Type of scale. */ template @@ -52,15 +53,9 @@ return_type_t student_t_lpdf(const T_y& y, const T_dof& nu, const T_loc& mu, const T_scale& sigma) { - static const char* function = "student_t_lpdf"; using T_partials_return = partials_return_t; - - if (size_zero(y, nu, mu, sigma)) { - return 0.0; - } - - T_partials_return logp(0.0); - + using std::log; + static const char* function = "student_t_lpdf"; check_not_nan(function, "Random variable", y); check_positive_finite(function, "Degrees of freedom parameter", nu); check_finite(function, "Location parameter", mu); @@ -69,18 +64,22 @@ return_type_t student_t_lpdf(const T_y& y, "Degrees of freedom parameter", nu, "Location parameter", mu, "Scale parameter", sigma); + if (size_zero(y, nu, mu, sigma)) { + return 0.0; + } if (!include_summand::value) { return 0.0; } + T_partials_return logp(0.0); + operands_and_partials ops_partials(y, nu, mu, + sigma); scalar_seq_view y_vec(y); scalar_seq_view nu_vec(nu); scalar_seq_view mu_vec(mu); scalar_seq_view sigma_vec(sigma); size_t N = max_size(y, nu, mu, sigma); - using std::log; - VectorBuilder::value, T_partials_return, T_dof> half_nu(size(nu)); @@ -145,8 +144,6 @@ return_type_t student_t_lpdf(const T_y& y, log1p_exp[i] = log1p(square_y_minus_mu_over_sigma__over_nu[i]); } - operands_and_partials ops_partials(y, nu, mu, - sigma); for (size_t n = 0; n < N; n++) { const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return mu_dbl = value_of(mu_vec[n]); diff --git a/stan/math/prim/prob/student_t_rng.hpp b/stan/math/prim/prob/student_t_rng.hpp index 12616c9db51..5edfa17f1d7 100644 --- a/stan/math/prim/prob/student_t_rng.hpp +++ b/stan/math/prim/prob/student_t_rng.hpp @@ -17,10 +17,11 @@ namespace math { * nu, mu, and sigma can each be a scalar or a one-dimensional container. Any * non-scalar inputs must be the same size. * - * @tparam T_deg Type of degrees of freedom parameter - * @tparam T_loc Type of location parameter - * @tparam T_scale Type of scale parameter + * @tparam T_deg type of degrees of freedom parameter + * @tparam T_loc type of location parameter + * @tparam T_scale type of scale parameter * @tparam RNG type of random number generator + * * @param nu (Sequence of) degrees of freedom parameter(s) * @param mu (Sequence of) location parameter(s) * @param sigma (Sequence of) scale parameter(s) @@ -38,7 +39,6 @@ student_t_rng(const T_deg& nu, const T_loc& mu, const T_scale& sigma, using boost::random::student_t_distribution; using boost::variate_generator; static const char* function = "student_t_rng"; - check_positive_finite(function, "Degrees of freedom parameter", nu); check_finite(function, "Location parameter", mu); check_positive_finite(function, "Scale parameter", sigma); diff --git a/stan/math/prim/prob/uniform_cdf.hpp b/stan/math/prim/prob/uniform_cdf.hpp index 8f551a75d9d..3e4fd75509d 100644 --- a/stan/math/prim/prob/uniform_cdf.hpp +++ b/stan/math/prim/prob/uniform_cdf.hpp @@ -14,14 +14,8 @@ namespace math { template return_type_t uniform_cdf(const T_y& y, const T_low& alpha, const T_high& beta) { - static const char* function = "uniform_cdf"; using T_partials_return = partials_return_t; - - if (size_zero(y, alpha, beta)) { - return 1.0; - } - - T_partials_return cdf(1.0); + static const char* function = "uniform_cdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Lower bound parameter", alpha); check_finite(function, "Upper bound parameter", beta); @@ -30,6 +24,13 @@ return_type_t uniform_cdf(const T_y& y, const T_low& alpha, "Lower bound parameter", alpha, "Upper bound parameter", beta); + if (size_zero(y, alpha, beta)) { + return 1.0; + } + + T_partials_return cdf(1.0); + operands_and_partials ops_partials(y, alpha, beta); + scalar_seq_view y_vec(y); scalar_seq_view alpha_vec(alpha); scalar_seq_view beta_vec(beta); @@ -42,7 +43,6 @@ return_type_t uniform_cdf(const T_y& y, const T_low& alpha, } } - operands_and_partials ops_partials(y, alpha, beta); for (size_t n = 0; n < N; n++) { const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return alpha_dbl = value_of(alpha_vec[n]); diff --git a/stan/math/prim/prob/uniform_lccdf.hpp b/stan/math/prim/prob/uniform_lccdf.hpp index 28cfe72887e..9446466e4f7 100644 --- a/stan/math/prim/prob/uniform_lccdf.hpp +++ b/stan/math/prim/prob/uniform_lccdf.hpp @@ -17,16 +17,9 @@ template return_type_t uniform_lccdf(const T_y& y, const T_low& alpha, const T_high& beta) { - static const char* function = "uniform_lccdf"; using T_partials_return = partials_return_t; - using std::log; - - if (size_zero(y, alpha, beta)) { - return 0.0; - } - - T_partials_return ccdf_log(0.0); + static const char* function = "uniform_lccdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Lower bound parameter", alpha); check_finite(function, "Upper bound parameter", beta); @@ -35,6 +28,13 @@ return_type_t uniform_lccdf(const T_y& y, "Lower bound parameter", alpha, "Upper bound parameter", beta); + if (size_zero(y, alpha, beta)) { + return 0; + } + + T_partials_return ccdf_log(0.0); + operands_and_partials ops_partials(y, alpha, beta); + scalar_seq_view y_vec(y); scalar_seq_view alpha_vec(alpha); scalar_seq_view beta_vec(beta); @@ -50,7 +50,6 @@ return_type_t uniform_lccdf(const T_y& y, } } - operands_and_partials ops_partials(y, alpha, beta); for (size_t n = 0; n < N; n++) { const T_partials_return y_dbl = value_of(y_vec[n]); const T_partials_return alpha_dbl = value_of(alpha_vec[n]); diff --git a/stan/math/prim/prob/uniform_lcdf.hpp b/stan/math/prim/prob/uniform_lcdf.hpp index 772a52bd78d..c8bbfeeeaa8 100644 --- a/stan/math/prim/prob/uniform_lcdf.hpp +++ b/stan/math/prim/prob/uniform_lcdf.hpp @@ -16,16 +16,9 @@ namespace math { template return_type_t uniform_lcdf(const T_y& y, const T_low& alpha, const T_high& beta) { - static const char* function = "uniform_lcdf"; using T_partials_return = partials_return_t; - using std::log; - - if (size_zero(y, alpha, beta)) { - return 0.0; - } - - T_partials_return cdf_log(0.0); + static const char* function = "uniform_lcdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Lower bound parameter", alpha); check_finite(function, "Upper bound parameter", beta); @@ -34,13 +27,18 @@ return_type_t uniform_lcdf(const T_y& y, const T_low& alpha, "Lower bound parameter", alpha, "Upper bound parameter", beta); + if (size_zero(y, alpha, beta)) { + return 0; + } + + T_partials_return cdf_log(0.0); + operands_and_partials ops_partials(y, alpha, beta); + scalar_seq_view y_vec(y); scalar_seq_view alpha_vec(alpha); scalar_seq_view beta_vec(beta); size_t N = max_size(y, alpha, beta); - operands_and_partials ops_partials(y, alpha, beta); - for (size_t n = 0; n < N; n++) { const T_partials_return y_dbl = value_of(y_vec[n]); if (y_dbl < value_of(alpha_vec[n]) || y_dbl > value_of(beta_vec[n])) { diff --git a/stan/math/prim/prob/uniform_lpdf.hpp b/stan/math/prim/prob/uniform_lpdf.hpp index c84914747c8..9e690667fe3 100644 --- a/stan/math/prim/prob/uniform_lpdf.hpp +++ b/stan/math/prim/prob/uniform_lpdf.hpp @@ -26,28 +26,21 @@ namespace math { & & \mathrm{ where } \; y \in [\alpha, \beta], \log(0) \; \mathrm{otherwise} \f} * + * @tparam T_y type of scalar + * @tparam T_low type of lower bound + * @tparam T_high type of upper bound * @param y A scalar variable. * @param alpha Lower bound. * @param beta Upper bound. * @throw std::invalid_argument if the lower bound is greater than * or equal to the lower bound - * @tparam T_y Type of scalar. - * @tparam T_low Type of lower bound. - * @tparam T_high Type of upper bound. */ template return_type_t uniform_lpdf(const T_y& y, const T_low& alpha, const T_high& beta) { - static const char* function = "uniform_lpdf"; using T_partials_return = partials_return_t; - using std::log; - - if (size_zero(y, alpha, beta)) { - return 0.0; - } - - T_partials_return logp(0.0); + static const char* function = "uniform_lpdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Lower bound parameter", alpha); check_finite(function, "Upper bound parameter", beta); @@ -56,10 +49,16 @@ return_type_t uniform_lpdf(const T_y& y, const T_low& alpha, "Upper bound parameter", beta); check_greater(function, "Upper bound parameter", beta, alpha); + if (size_zero(y, alpha, beta)) { + return 0.0; + } if (!include_summand::value) { return 0.0; } + T_partials_return logp(0.0); + operands_and_partials ops_partials(y, alpha, beta); + scalar_seq_view y_vec(y); scalar_seq_view alpha_vec(alpha); scalar_seq_view beta_vec(beta); @@ -92,7 +91,6 @@ return_type_t uniform_lpdf(const T_y& y, const T_low& alpha, } } - operands_and_partials ops_partials(y, alpha, beta); for (size_t n = 0; n < N; n++) { if (include_summand::value) { logp -= log_beta_minus_alpha[n]; diff --git a/stan/math/prim/prob/uniform_rng.hpp b/stan/math/prim/prob/uniform_rng.hpp index 83302709c57..84906bc9625 100644 --- a/stan/math/prim/prob/uniform_rng.hpp +++ b/stan/math/prim/prob/uniform_rng.hpp @@ -19,8 +19,8 @@ namespace math { * alpha and beta can each be a scalar or a one-dimensional container. Any * non-scalar inputs must be the same size. * - * @tparam T_alpha Type of shape parameter - * @tparam T_beta Type of inverse scale parameter + * @tparam T_alpha type of shape parameter + * @tparam T_beta type of inverse scale parameter * @tparam RNG type of random number generator * @param alpha (Sequence of) lower bound parameter(s) * @param beta (Sequence of) upper bound parameter(s) @@ -35,9 +35,7 @@ inline typename VectorBuilder::type uniform_rng( const T_alpha& alpha, const T_beta& beta, RNG& rng) { using boost::random::uniform_real_distribution; using boost::variate_generator; - static const char* function = "uniform_rng"; - check_finite(function, "Lower bound parameter", alpha); check_finite(function, "Upper bound parameter", beta); check_consistent_sizes(function, "Lower bound parameter", alpha, diff --git a/stan/math/prim/prob/von_mises_lpdf.hpp b/stan/math/prim/prob/von_mises_lpdf.hpp index 96b1363b5d8..4538a2198d5 100644 --- a/stan/math/prim/prob/von_mises_lpdf.hpp +++ b/stan/math/prim/prob/von_mises_lpdf.hpp @@ -20,20 +20,11 @@ namespace math { template return_type_t von_mises_lpdf(T_y const& y, T_loc const& mu, T_scale const& kappa) { - static char const* const function = "von_mises_lpdf"; using T_partials_return = partials_return_t; - - if (size_zero(y, mu, kappa)) { - return 0.0; - } - using std::cos; using std::floor; - using std::log; using std::sin; - - T_partials_return logp = 0.0; - + static char const* const function = "von_mises_lpdf"; check_finite(function, "Random variable", y); check_finite(function, "Location parameter", mu); check_nonnegative(function, "Scale parameter", kappa); @@ -41,21 +32,27 @@ return_type_t von_mises_lpdf(T_y const& y, T_loc const& mu, check_consistent_sizes(function, "Random variable", y, "Location parameter", mu, "Scale parameter", kappa); + if (size_zero(y, mu, kappa)) { + return 0; + } if (!include_summand::value) { - return logp; + return 0; } + T_partials_return logp = 0.0; + operands_and_partials ops_partials(y, mu, kappa); + + scalar_seq_view y_vec(y); + scalar_seq_view mu_vec(mu); + scalar_seq_view kappa_vec(kappa); + size_t N = max_size(y, mu, kappa); + const bool y_const = is_constant_all::value; const bool mu_const = is_constant_all::value; const bool kappa_const = is_constant_all::value; - const bool compute_bessel0 = include_summand::value; const bool compute_bessel1 = !kappa_const; - scalar_seq_view y_vec(y); - scalar_seq_view mu_vec(mu); - scalar_seq_view kappa_vec(kappa); - VectorBuilder kappa_dbl(size(kappa)); VectorBuilder::value, T_partials_return, T_scale> @@ -68,10 +65,6 @@ return_type_t von_mises_lpdf(T_y const& y, T_loc const& mu, } } - operands_and_partials ops_partials(y, mu, kappa); - - size_t N = max_size(y, mu, kappa); - for (size_t n = 0; n < N; n++) { const T_partials_return y_val = value_of(y_vec[n]); const T_partials_return y_dbl = y_val - floor(y_val / TWO_PI) * TWO_PI; diff --git a/stan/math/prim/prob/von_mises_rng.hpp b/stan/math/prim/prob/von_mises_rng.hpp index 337acd8cc86..d36ed93bbdb 100644 --- a/stan/math/prim/prob/von_mises_rng.hpp +++ b/stan/math/prim/prob/von_mises_rng.hpp @@ -47,7 +47,6 @@ inline typename VectorBuilder::type von_mises_rng( using boost::random::uniform_real_distribution; using boost::variate_generator; static const char* function = "von_mises_rng"; - check_finite(function, "Location parameter", mu); check_nonnegative(function, "Scale parameter", kappa); check_finite(function, "Scale parameter", kappa); diff --git a/stan/math/prim/prob/weibull_cdf.hpp b/stan/math/prim/prob/weibull_cdf.hpp index 49001695ffa..59568c12a7a 100644 --- a/stan/math/prim/prob/weibull_cdf.hpp +++ b/stan/math/prim/prob/weibull_cdf.hpp @@ -33,22 +33,19 @@ return_type_t weibull_cdf(const T_y& y, const T_shape& alpha, const T_scale& sigma) { using T_partials_return = partials_return_t; - - static const char* function = "weibull_cdf"; - using std::exp; using std::log; using std::pow; + static const char* function = "weibull_cdf"; + check_nonnegative(function, "Random variable", y); + check_positive_finite(function, "Shape parameter", alpha); + check_positive_finite(function, "Scale parameter", sigma); if (size_zero(y, alpha, sigma)) { return 1.0; } T_partials_return cdf(1.0); - check_nonnegative(function, "Random variable", y); - check_positive_finite(function, "Shape parameter", alpha); - check_positive_finite(function, "Scale parameter", sigma); - operands_and_partials ops_partials(y, alpha, sigma); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/weibull_lccdf.hpp b/stan/math/prim/prob/weibull_lccdf.hpp index 2b870777e54..2ad07cbffbf 100644 --- a/stan/math/prim/prob/weibull_lccdf.hpp +++ b/stan/math/prim/prob/weibull_lccdf.hpp @@ -31,21 +31,18 @@ return_type_t weibull_lccdf(const T_y& y, const T_shape& alpha, const T_scale& sigma) { using T_partials_return = partials_return_t; - - static const char* function = "weibull_lccdf"; - using std::log; using std::pow; + static const char* function = "weibull_lccdf"; + check_nonnegative(function, "Random variable", y); + check_positive_finite(function, "Shape parameter", alpha); + check_positive_finite(function, "Scale parameter", sigma); if (size_zero(y, alpha, sigma)) { return 0.0; } T_partials_return ccdf_log(0.0); - check_nonnegative(function, "Random variable", y); - check_positive_finite(function, "Shape parameter", alpha); - check_positive_finite(function, "Scale parameter", sigma); - operands_and_partials ops_partials(y, alpha, sigma); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/weibull_lcdf.hpp b/stan/math/prim/prob/weibull_lcdf.hpp index 98c2030905a..757229b03a0 100644 --- a/stan/math/prim/prob/weibull_lcdf.hpp +++ b/stan/math/prim/prob/weibull_lcdf.hpp @@ -32,22 +32,19 @@ return_type_t weibull_lcdf(const T_y& y, const T_shape& alpha, const T_scale& sigma) { using T_partials_return = partials_return_t; - - static const char* function = "weibull_lcdf"; - using std::exp; using std::log; using std::pow; + static const char* function = "weibull_lcdf"; + check_nonnegative(function, "Random variable", y); + check_positive_finite(function, "Shape parameter", alpha); + check_positive_finite(function, "Scale parameter", sigma); if (size_zero(y, alpha, sigma)) { return 0.0; } T_partials_return cdf_log(0.0); - check_nonnegative(function, "Random variable", y); - check_positive_finite(function, "Shape parameter", alpha); - check_positive_finite(function, "Scale parameter", sigma); - operands_and_partials ops_partials(y, alpha, sigma); scalar_seq_view y_vec(y); diff --git a/stan/math/prim/prob/weibull_lpdf.hpp b/stan/math/prim/prob/weibull_lpdf.hpp index 76f8e7b9224..cc5e42dc8f0 100644 --- a/stan/math/prim/prob/weibull_lpdf.hpp +++ b/stan/math/prim/prob/weibull_lpdf.hpp @@ -32,17 +32,16 @@ template return_type_t weibull_lpdf(const T_y& y, const T_shape& alpha, const T_scale& sigma) { - static const char* function = "weibull_lpdf"; using T_partials_return = partials_return_t; - using std::log; using std::pow; - + static const char* function = "weibull_lpdf"; check_finite(function, "Random variable", y); check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Scale parameter", sigma); check_consistent_sizes(function, "Random variable", y, "Shape parameter", alpha, "Scale parameter", sigma); + if (size_zero(y, alpha, sigma)) { return 0; } @@ -51,6 +50,8 @@ return_type_t weibull_lpdf(const T_y& y, } T_partials_return logp(0); + operands_and_partials ops_partials(y, alpha, sigma); + scalar_seq_view y_vec(y); scalar_seq_view alpha_vec(alpha); scalar_seq_view sigma_vec(sigma); @@ -106,7 +107,6 @@ return_type_t weibull_lpdf(const T_y& y, y_div_sigma_pow_alpha[i] = pow(y_dbl * inv_sigma[i], alpha_dbl); } - operands_and_partials ops_partials(y, alpha, sigma); for (size_t n = 0; n < N; n++) { const T_partials_return alpha_dbl = value_of(alpha_vec[n]); if (include_summand::value) { diff --git a/stan/math/prim/prob/weibull_rng.hpp b/stan/math/prim/prob/weibull_rng.hpp index abb10bb2a5b..bf91178711f 100644 --- a/stan/math/prim/prob/weibull_rng.hpp +++ b/stan/math/prim/prob/weibull_rng.hpp @@ -17,8 +17,8 @@ namespace math { * alpha and sigma can each be a scalar or a one-dimensional container. Any * non-scalar inputs must be the same size. * - * @tparam T_shape Type of shape parameter - * @tparam T_scale Type of scale parameter + * @tparam T_shape type of shape parameter + * @tparam T_scale type of scale parameter * @tparam RNG type of random number generator * @param alpha (Sequence of) positive shape parameter(s) * @param sigma (Sequence of) positive scale parameter(s) @@ -33,9 +33,7 @@ inline typename VectorBuilder::type weibull_rng( const T_shape& alpha, const T_scale& sigma, RNG& rng) { using boost::random::weibull_distribution; using boost::variate_generator; - static const char* function = "weibull_rng"; - check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Scale parameter", sigma); check_consistent_sizes(function, "Shape parameter", alpha, "Scale Parameter", diff --git a/stan/math/prim/prob/wiener_lpdf.hpp b/stan/math/prim/prob/wiener_lpdf.hpp index b906563f850..3d2b476b70c 100644 --- a/stan/math/prim/prob/wiener_lpdf.hpp +++ b/stan/math/prim/prob/wiener_lpdf.hpp @@ -57,6 +57,12 @@ namespace math { * reaction times in seconds (strictly positive) with * upper-boundary responses. * + * @tparam T_y type of scalar + * @tparam T_alpha type of alpha parameter + * @tparam T_tau type of tau parameter + * @tparam T_beta type of beta parameter + * @tparam T_delta type of delta parameter + * * @param y A scalar variate. * @param alpha The boundary separation. * @param tau The nondecision time. @@ -70,32 +76,14 @@ template wiener_lpdf( const T_y& y, const T_alpha& alpha, const T_tau& tau, const T_beta& beta, const T_delta& delta) { - static const char* function = "wiener_lpdf"; - + using T_return_type = return_type_t; using std::ceil; using std::exp; using std::floor; using std::log; using std::sin; using std::sqrt; - - static const double WIENER_ERR = 0.000001; - static const double PI_TIMES_WIENER_ERR = pi() * WIENER_ERR; - static const double LOG_PI_LOG_WIENER_ERR = LOG_PI + log(WIENER_ERR); - static const double TWO_TIMES_SQRT_TWO_PI_TIMES_WIENER_ERR - = 2.0 * SQRT_TWO_PI * WIENER_ERR; - static const double LOG_TWO_OVER_TWO_PLUS_LOG_SQRT_PI - = LOG_TWO / 2 + LOG_SQRT_PI; - static const double SQUARE_PI_OVER_TWO = square(pi()) * 0.5; - static const double TWO_TIMES_LOG_SQRT_PI = 2.0 * LOG_SQRT_PI; - - if (size_zero(y, alpha, beta, tau, delta)) { - return 0.0; - } - - using T_return_type = return_type_t; - T_return_type lp(0.0); - + static const char* function = "wiener_lpdf"; check_not_nan(function, "Random variable", y); check_not_nan(function, "Boundary separation", alpha); check_not_nan(function, "A-priori bias", beta); @@ -113,6 +101,12 @@ return_type_t wiener_lpdf( alpha, "A-priori bias", beta, "Nondecision time", tau, "Drift rate", delta); + if (size_zero(y, alpha, beta, tau, delta)) { + return 0; + } + + T_return_type lp(0.0); + size_t N = std::max(max_size(y, alpha, beta), max_size(tau, delta)); if (!N) { return 0.0; @@ -123,8 +117,8 @@ return_type_t wiener_lpdf( scalar_seq_view beta_vec(beta); scalar_seq_view tau_vec(tau); scalar_seq_view delta_vec(delta); - size_t N_y_tau = max_size(y, tau); + for (size_t i = 0; i < N_y_tau; ++i) { if (y_vec[i] <= tau_vec[i]) { std::stringstream msg; @@ -139,6 +133,16 @@ return_type_t wiener_lpdf( return 0; } + static const double WIENER_ERR = 0.000001; + static const double PI_TIMES_WIENER_ERR = pi() * WIENER_ERR; + static const double LOG_PI_LOG_WIENER_ERR = LOG_PI + log(WIENER_ERR); + static const double TWO_TIMES_SQRT_TWO_PI_TIMES_WIENER_ERR + = 2.0 * SQRT_TWO_PI * WIENER_ERR; + static const double LOG_TWO_OVER_TWO_PLUS_LOG_SQRT_PI + = LOG_TWO / 2 + LOG_SQRT_PI; + static const double SQUARE_PI_OVER_TWO = square(pi()) * 0.5; + static const double TWO_TIMES_LOG_SQRT_PI = 2.0 * LOG_SQRT_PI; + for (size_t i = 0; i < N; i++) { typename scalar_type::type one_minus_beta = 1.0 - beta_vec[i]; typename scalar_type::type alpha2 = square(alpha_vec[i]); diff --git a/stan/math/prim/prob/wishart_lpdf.hpp b/stan/math/prim/prob/wishart_lpdf.hpp index a586cea2366..cc7853a69af 100644 --- a/stan/math/prim/prob/wishart_lpdf.hpp +++ b/stan/math/prim/prob/wishart_lpdf.hpp @@ -31,30 +31,27 @@ namespace math { -\frac{\nu}{2} \log(\det(S)) + \frac{\nu-k-1}{2}\log (\det(W)) - \frac{1}{2} \mbox{tr} (S^{-1}W) \f} * + * @tparam T_y type of scalar + * @tparam T_dof type of degrees of freedom + * @tparam T_scale type of scale * @param W A scalar matrix * @param nu Degrees of freedom * @param S The scale matrix * @return The log of the Wishart density at W given nu and S. * @throw std::domain_error if nu is not greater than k-1 * @throw std::domain_error if S is not square, not symmetric, or not - semi-positive definite. - * @tparam T_y Type of scalar. - * @tparam T_dof Type of degrees of freedom. - * @tparam T_scale Type of scale. + * semi-positive definite. */ template return_type_t wishart_lpdf( const Eigen::Matrix& W, const T_dof& nu, const Eigen::Matrix& S) { - static const char* function = "wishart_lpdf"; - using Eigen::Dynamic; using Eigen::Lower; using Eigen::Matrix; - + static const char* function = "wishart_lpdf"; index_type_t> k = W.rows(); - return_type_t lp(0.0); check_greater(function, "Degrees of freedom parameter", nu, k - 1); check_square(function, "random variable", W); check_square(function, "scale parameter", S); @@ -67,6 +64,8 @@ return_type_t wishart_lpdf( LDLT_factor ldlt_S(S); check_ldlt_factor(function, "LDLT_Factor of scale parameter", ldlt_S); + return_type_t lp(0.0); + if (include_summand::value) { lp -= nu * k * HALF_LOG_TWO; } diff --git a/stan/math/prim/prob/wishart_rng.hpp b/stan/math/prim/prob/wishart_rng.hpp index 4e8846a74d1..11332f3309d 100644 --- a/stan/math/prim/prob/wishart_rng.hpp +++ b/stan/math/prim/prob/wishart_rng.hpp @@ -14,11 +14,9 @@ namespace math { template inline Eigen::MatrixXd wishart_rng(double nu, const Eigen::MatrixXd& S, RNG& rng) { - static const char* function = "wishart_rng"; - using Eigen::MatrixXd; + static const char* function = "wishart_rng"; index_type_t k = S.rows(); - check_square(function, "scale parameter", S); check_greater(function, "degrees of freedom > dims - 1", nu, k - 1); From 5861564efdf2829c33f65409c9ab915ba7f55ea8 Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Thu, 5 Mar 2020 17:21:36 +0000 Subject: [PATCH 2/3] Move using statements to the beginning of the function --- stan/math/prim/prob/bernoulli_lccdf.hpp | 2 +- stan/math/prim/prob/bernoulli_lcdf.hpp | 2 +- stan/math/prim/prob/bernoulli_logit_glm_lpmf.hpp | 4 +--- stan/math/prim/prob/bernoulli_logit_glm_rng.hpp | 3 +-- stan/math/prim/prob/bernoulli_logit_lpmf.hpp | 2 +- stan/math/prim/prob/bernoulli_logit_rng.hpp | 1 - stan/math/prim/prob/bernoulli_lpmf.hpp | 2 +- stan/math/prim/prob/beta_binomial_cdf.hpp | 2 +- stan/math/prim/prob/beta_binomial_lccdf.hpp | 4 ++-- stan/math/prim/prob/beta_binomial_lcdf.hpp | 4 ++-- stan/math/prim/prob/beta_lccdf.hpp | 6 +++--- stan/math/prim/prob/beta_lcdf.hpp | 6 +++--- stan/math/prim/prob/beta_lpdf.hpp | 2 +- stan/math/prim/prob/beta_proportion_lccdf.hpp | 6 +++--- stan/math/prim/prob/beta_proportion_lcdf.hpp | 6 +++--- stan/math/prim/prob/beta_proportion_lpdf.hpp | 8 ++++---- stan/math/prim/prob/binomial_cdf.hpp | 4 ++-- stan/math/prim/prob/binomial_lccdf.hpp | 6 +++--- stan/math/prim/prob/binomial_lcdf.hpp | 6 +++--- stan/math/prim/prob/binomial_logit_lpmf.hpp | 2 +- stan/math/prim/prob/categorical_logit_glm_lpmf.hpp | 3 +-- stan/math/prim/prob/cauchy_cdf.hpp | 2 +- stan/math/prim/prob/cauchy_lccdf.hpp | 4 ++-- stan/math/prim/prob/cauchy_lcdf.hpp | 4 ++-- stan/math/prim/prob/cauchy_lpdf.hpp | 2 +- stan/math/prim/prob/chi_square_cdf.hpp | 4 ++-- stan/math/prim/prob/chi_square_lccdf.hpp | 6 +++--- stan/math/prim/prob/chi_square_lcdf.hpp | 6 +++--- stan/math/prim/prob/chi_square_lpdf.hpp | 2 +- stan/math/prim/prob/discrete_range_lpmf.hpp | 3 +-- stan/math/prim/prob/double_exponential_cdf.hpp | 2 +- stan/math/prim/prob/double_exponential_lccdf.hpp | 4 ++-- stan/math/prim/prob/double_exponential_lcdf.hpp | 4 ++-- stan/math/prim/prob/double_exponential_lpdf.hpp | 4 ++-- stan/math/prim/prob/exp_mod_normal_cdf.hpp | 2 +- stan/math/prim/prob/exp_mod_normal_lccdf.hpp | 4 ++-- stan/math/prim/prob/exp_mod_normal_lcdf.hpp | 4 ++-- stan/math/prim/prob/exp_mod_normal_lpdf.hpp | 6 +++--- stan/math/prim/prob/exponential_cdf.hpp | 2 +- stan/math/prim/prob/exponential_lcdf.hpp | 4 ++-- stan/math/prim/prob/exponential_lpdf.hpp | 2 +- stan/math/prim/prob/frechet_cdf.hpp | 6 +++--- stan/math/prim/prob/frechet_lccdf.hpp | 6 +++--- stan/math/prim/prob/frechet_lcdf.hpp | 4 ++-- stan/math/prim/prob/frechet_lpdf.hpp | 4 ++-- stan/math/prim/prob/gamma_cdf.hpp | 2 +- stan/math/prim/prob/gamma_lccdf.hpp | 6 +++--- stan/math/prim/prob/gamma_lcdf.hpp | 6 +++--- stan/math/prim/prob/gamma_lpdf.hpp | 2 +- stan/math/prim/prob/gaussian_dlm_obs_lpdf.hpp | 4 ++-- stan/math/prim/prob/gumbel_cdf.hpp | 2 +- stan/math/prim/prob/gumbel_lccdf.hpp | 4 ++-- stan/math/prim/prob/gumbel_lcdf.hpp | 2 +- stan/math/prim/prob/gumbel_lpdf.hpp | 4 ++-- stan/math/prim/prob/inv_chi_square_cdf.hpp | 4 ++-- stan/math/prim/prob/inv_chi_square_lccdf.hpp | 6 +++--- stan/math/prim/prob/inv_chi_square_lcdf.hpp | 6 +++--- stan/math/prim/prob/inv_chi_square_lpdf.hpp | 4 ++-- stan/math/prim/prob/inv_gamma_cdf.hpp | 4 ++-- stan/math/prim/prob/inv_gamma_lccdf.hpp | 6 +++--- stan/math/prim/prob/inv_gamma_lcdf.hpp | 6 +++--- stan/math/prim/prob/inv_gamma_lpdf.hpp | 2 +- stan/math/prim/prob/multi_student_t_lpdf.hpp | 2 +- 63 files changed, 119 insertions(+), 125 deletions(-) diff --git a/stan/math/prim/prob/bernoulli_lccdf.hpp b/stan/math/prim/prob/bernoulli_lccdf.hpp index 91722d75f9f..4c05d70a90b 100644 --- a/stan/math/prim/prob/bernoulli_lccdf.hpp +++ b/stan/math/prim/prob/bernoulli_lccdf.hpp @@ -30,6 +30,7 @@ namespace math { template return_type_t bernoulli_lccdf(const T_n& n, const T_prob& theta) { using T_partials_return = partials_return_t; + 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); @@ -40,7 +41,6 @@ return_type_t 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 ops_partials(theta); diff --git a/stan/math/prim/prob/bernoulli_lcdf.hpp b/stan/math/prim/prob/bernoulli_lcdf.hpp index 77f074f5c21..ac6327aeed4 100644 --- a/stan/math/prim/prob/bernoulli_lcdf.hpp +++ b/stan/math/prim/prob/bernoulli_lcdf.hpp @@ -30,6 +30,7 @@ namespace math { template return_type_t bernoulli_lcdf(const T_n& n, const T_prob& theta) { using T_partials_return = partials_return_t; + 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); @@ -40,7 +41,6 @@ return_type_t 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 ops_partials(theta); diff --git a/stan/math/prim/prob/bernoulli_logit_glm_lpmf.hpp b/stan/math/prim/prob/bernoulli_logit_glm_lpmf.hpp index 8755656c292..d8ed7024ac1 100644 --- a/stan/math/prim/prob/bernoulli_logit_glm_lpmf.hpp +++ b/stan/math/prim/prob/bernoulli_logit_glm_lpmf.hpp @@ -49,14 +49,11 @@ template bernoulli_logit_glm_lpmf( const T_y &y, const Eigen::Matrix &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; using T_y_val = typename std::conditional_t::value, @@ -69,6 +66,7 @@ return_type_t 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); diff --git a/stan/math/prim/prob/bernoulli_logit_glm_rng.hpp b/stan/math/prim/prob/bernoulli_logit_glm_rng.hpp index 35cbae43234..ed48b749be6 100644 --- a/stan/math/prim/prob/bernoulli_logit_glm_rng.hpp +++ b/stan/math/prim/prob/bernoulli_logit_glm_rng.hpp @@ -42,11 +42,10 @@ inline typename VectorBuilder::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"; check_finite(function, "Matrix of independent variables", x); check_finite(function, "Weight vector", beta); check_finite(function, "Intercept", alpha); diff --git a/stan/math/prim/prob/bernoulli_logit_lpmf.hpp b/stan/math/prim/prob/bernoulli_logit_lpmf.hpp index 40fe82902c7..418aacb23ef 100644 --- a/stan/math/prim/prob/bernoulli_logit_lpmf.hpp +++ b/stan/math/prim/prob/bernoulli_logit_lpmf.hpp @@ -28,6 +28,7 @@ namespace math { template return_type_t bernoulli_logit_lpmf(const T_n& n, const T_prob& theta) { using T_partials_return = partials_return_t; + 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); @@ -41,7 +42,6 @@ return_type_t 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 ops_partials(theta); diff --git a/stan/math/prim/prob/bernoulli_logit_rng.hpp b/stan/math/prim/prob/bernoulli_logit_rng.hpp index 3c7f863096b..5f3ae9903df 100644 --- a/stan/math/prim/prob/bernoulli_logit_rng.hpp +++ b/stan/math/prim/prob/bernoulli_logit_rng.hpp @@ -30,7 +30,6 @@ inline typename VectorBuilder::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); diff --git a/stan/math/prim/prob/bernoulli_lpmf.hpp b/stan/math/prim/prob/bernoulli_lpmf.hpp index 3048732f3f0..515c100b0a7 100644 --- a/stan/math/prim/prob/bernoulli_lpmf.hpp +++ b/stan/math/prim/prob/bernoulli_lpmf.hpp @@ -29,6 +29,7 @@ namespace math { template return_type_t bernoulli_lpmf(const T_n& n, const T_prob& theta) { using T_partials_return = partials_return_t; + using std::log; static const char* function = "bernoulli_lpmf"; check_bounded(function, "n", n, 0, 1); check_finite(function, "Probability parameter", theta); @@ -43,7 +44,6 @@ return_type_t 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 ops_partials(theta); diff --git a/stan/math/prim/prob/beta_binomial_cdf.hpp b/stan/math/prim/prob/beta_binomial_cdf.hpp index 784c124f0c3..86a6d169cfb 100644 --- a/stan/math/prim/prob/beta_binomial_cdf.hpp +++ b/stan/math/prim/prob/beta_binomial_cdf.hpp @@ -42,6 +42,7 @@ return_type_t 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; + 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); @@ -55,7 +56,6 @@ return_type_t 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 ops_partials(alpha, beta); diff --git a/stan/math/prim/prob/beta_binomial_lccdf.hpp b/stan/math/prim/prob/beta_binomial_lccdf.hpp index 2cf33343edd..07fb04df4a4 100644 --- a/stan/math/prim/prob/beta_binomial_lccdf.hpp +++ b/stan/math/prim/prob/beta_binomial_lccdf.hpp @@ -43,6 +43,8 @@ return_type_t 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; + 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); @@ -56,8 +58,6 @@ return_type_t 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 ops_partials(alpha, beta); diff --git a/stan/math/prim/prob/beta_binomial_lcdf.hpp b/stan/math/prim/prob/beta_binomial_lcdf.hpp index 1f7b6def5e7..b1b97c7cd90 100644 --- a/stan/math/prim/prob/beta_binomial_lcdf.hpp +++ b/stan/math/prim/prob/beta_binomial_lcdf.hpp @@ -43,6 +43,8 @@ return_type_t 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; + 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); @@ -56,8 +58,6 @@ return_type_t 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 ops_partials(alpha, beta); diff --git a/stan/math/prim/prob/beta_lccdf.hpp b/stan/math/prim/prob/beta_lccdf.hpp index 4205650da61..e549a70341b 100644 --- a/stan/math/prim/prob/beta_lccdf.hpp +++ b/stan/math/prim/prob/beta_lccdf.hpp @@ -39,6 +39,9 @@ template return_type_t beta_lccdf( const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) { using T_partials_return = partials_return_t; + 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); @@ -53,9 +56,6 @@ return_type_t beta_lccdf( return 0; } - using std::exp; - using std::log; - using std::pow; T_partials_return ccdf_log(0.0); operands_and_partials ops_partials(y, alpha, beta); diff --git a/stan/math/prim/prob/beta_lcdf.hpp b/stan/math/prim/prob/beta_lcdf.hpp index ad12b79cd31..e3d0a28b849 100644 --- a/stan/math/prim/prob/beta_lcdf.hpp +++ b/stan/math/prim/prob/beta_lcdf.hpp @@ -39,6 +39,9 @@ template return_type_t beta_lcdf( const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) { using T_partials_return = partials_return_t; + 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); @@ -53,9 +56,6 @@ return_type_t beta_lcdf( return 0; } - using std::exp; - using std::log; - using std::pow; T_partials_return cdf_log(0.0); operands_and_partials ops_partials(y, alpha, beta); diff --git a/stan/math/prim/prob/beta_lpdf.hpp b/stan/math/prim/prob/beta_lpdf.hpp index a698abe0dab..949ca6bc27f 100644 --- a/stan/math/prim/prob/beta_lpdf.hpp +++ b/stan/math/prim/prob/beta_lpdf.hpp @@ -41,6 +41,7 @@ template beta_lpdf( const T_y& y, const T_scale_succ& alpha, const T_scale_fail& beta) { using T_partials_return = partials_return_t; + 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); @@ -58,7 +59,6 @@ return_type_t beta_lpdf( return 0; } - using std::log; T_partials_return logp(0); operands_and_partials ops_partials(y, alpha, beta); diff --git a/stan/math/prim/prob/beta_proportion_lccdf.hpp b/stan/math/prim/prob/beta_proportion_lccdf.hpp index eceab76a876..742b67d51b4 100644 --- a/stan/math/prim/prob/beta_proportion_lccdf.hpp +++ b/stan/math/prim/prob/beta_proportion_lccdf.hpp @@ -43,6 +43,9 @@ return_type_t beta_proportion_lccdf(const T_y& y, const T_loc& mu, const T_prec& kappa) { using T_partials_return = partials_return_t; + 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); @@ -57,9 +60,6 @@ return_type_t 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 ops_partials(y, mu, kappa); diff --git a/stan/math/prim/prob/beta_proportion_lcdf.hpp b/stan/math/prim/prob/beta_proportion_lcdf.hpp index 67b435e68be..02ee0513ec9 100644 --- a/stan/math/prim/prob/beta_proportion_lcdf.hpp +++ b/stan/math/prim/prob/beta_proportion_lcdf.hpp @@ -44,6 +44,9 @@ return_type_t beta_proportion_lcdf(const T_y& y, const T_loc& mu, const T_prec& kappa) { using T_partials_return = partials_return_t; + 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); @@ -58,9 +61,6 @@ return_type_t 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 ops_partials(y, mu, kappa); diff --git a/stan/math/prim/prob/beta_proportion_lpdf.hpp b/stan/math/prim/prob/beta_proportion_lpdf.hpp index 67bb3756c88..39db3e2efd0 100644 --- a/stan/math/prim/prob/beta_proportion_lpdf.hpp +++ b/stan/math/prim/prob/beta_proportion_lpdf.hpp @@ -43,10 +43,9 @@ template return_type_t 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; 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); @@ -55,13 +54,16 @@ return_type_t 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::value) { return 0; } + T_partials_return logp(0); + operands_and_partials ops_partials(y, mu, kappa); scalar_seq_view y_vec(y); scalar_seq_view mu_vec(mu); @@ -78,8 +80,6 @@ return_type_t beta_proportion_lpdf(const T_y& y, } } - operands_and_partials ops_partials(y, mu, kappa); - VectorBuilder::value, T_partials_return, T_y> log_y(size_y); diff --git a/stan/math/prim/prob/binomial_cdf.hpp b/stan/math/prim/prob/binomial_cdf.hpp index d85e7560e39..c969d356eb1 100644 --- a/stan/math/prim/prob/binomial_cdf.hpp +++ b/stan/math/prim/prob/binomial_cdf.hpp @@ -34,6 +34,8 @@ template return_type_t binomial_cdf(const T_n& n, const T_N& N, const T_prob& theta) { using T_partials_return = partials_return_t; + 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); @@ -46,8 +48,6 @@ return_type_t 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 ops_partials(theta); diff --git a/stan/math/prim/prob/binomial_lccdf.hpp b/stan/math/prim/prob/binomial_lccdf.hpp index b59ba8e2ef2..68cec944ba8 100644 --- a/stan/math/prim/prob/binomial_lccdf.hpp +++ b/stan/math/prim/prob/binomial_lccdf.hpp @@ -36,6 +36,9 @@ template return_type_t binomial_lccdf(const T_n& n, const T_N& N, const T_prob& theta) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; + using std::pow; static const char* function = "binomial_lccdf"; check_nonnegative(function, "Population size parameter", N); check_finite(function, "Probability parameter", theta); @@ -48,9 +51,6 @@ return_type_t binomial_lccdf(const T_n& n, const T_N& N, return 0; } - using std::exp; - using std::log; - using std::pow; T_partials_return P(0.0); operands_and_partials ops_partials(theta); diff --git a/stan/math/prim/prob/binomial_lcdf.hpp b/stan/math/prim/prob/binomial_lcdf.hpp index 44d3e523e2e..800574fe5d4 100644 --- a/stan/math/prim/prob/binomial_lcdf.hpp +++ b/stan/math/prim/prob/binomial_lcdf.hpp @@ -36,6 +36,9 @@ template return_type_t binomial_lcdf(const T_n& n, const T_N& N, const T_prob& theta) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; + using std::pow; static const char* function = "binomial_lcdf"; check_nonnegative(function, "Population size parameter", N); check_finite(function, "Probability parameter", theta); @@ -48,9 +51,6 @@ return_type_t binomial_lcdf(const T_n& n, const T_N& N, return 0; } - using std::exp; - using std::log; - using std::pow; T_partials_return P(0.0); operands_and_partials ops_partials(theta); diff --git a/stan/math/prim/prob/binomial_logit_lpmf.hpp b/stan/math/prim/prob/binomial_logit_lpmf.hpp index 24a7c192844..63ad522fdd1 100644 --- a/stan/math/prim/prob/binomial_logit_lpmf.hpp +++ b/stan/math/prim/prob/binomial_logit_lpmf.hpp @@ -33,6 +33,7 @@ template return_type_t binomial_logit_lpmf(const T_n& n, const T_N& N, const T_prob& alpha) { using T_partials_return = partials_return_t; + using std::log; static const char* function = "binomial_logit_lpmf"; check_bounded(function, "Successes variable", n, 0, N); check_nonnegative(function, "Population size parameter", N); @@ -48,7 +49,6 @@ return_type_t binomial_logit_lpmf(const T_n& n, const T_N& N, return 0.0; } - using std::log; T_partials_return logp = 0; operands_and_partials ops_partials(alpha); diff --git a/stan/math/prim/prob/categorical_logit_glm_lpmf.hpp b/stan/math/prim/prob/categorical_logit_glm_lpmf.hpp index af98042ffd2..f0c87178742 100644 --- a/stan/math/prim/prob/categorical_logit_glm_lpmf.hpp +++ b/stan/math/prim/prob/categorical_logit_glm_lpmf.hpp @@ -45,8 +45,6 @@ categorical_logit_glm_lpmf( const Eigen::Matrix& beta) { using T_partials_return = partials_return_t; - static const char* function = "categorical_logit_glm_lpmf"; - using Eigen::Array; using Eigen::Dynamic; using Eigen::Matrix; @@ -57,6 +55,7 @@ categorical_logit_glm_lpmf( const size_t N_attributes = x.cols(); const size_t N_classes = beta.cols(); + static const char* function = "categorical_logit_glm_lpmf"; check_consistent_size(function, "Vector of dependent variables", y, N_instances); check_consistent_size(function, "Intercept vector", alpha, N_classes); diff --git a/stan/math/prim/prob/cauchy_cdf.hpp b/stan/math/prim/prob/cauchy_cdf.hpp index f5f63abc723..c7c016891a0 100644 --- a/stan/math/prim/prob/cauchy_cdf.hpp +++ b/stan/math/prim/prob/cauchy_cdf.hpp @@ -31,6 +31,7 @@ template return_type_t cauchy_cdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { using T_partials_return = partials_return_t; + using std::atan; static const char* function = "cauchy_cdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -42,7 +43,6 @@ return_type_t cauchy_cdf(const T_y& y, const T_loc& mu, return 1.0; } - using std::atan; T_partials_return P(1.0); operands_and_partials ops_partials(y, mu, sigma); diff --git a/stan/math/prim/prob/cauchy_lccdf.hpp b/stan/math/prim/prob/cauchy_lccdf.hpp index 1de887b563a..841402e6df3 100644 --- a/stan/math/prim/prob/cauchy_lccdf.hpp +++ b/stan/math/prim/prob/cauchy_lccdf.hpp @@ -32,6 +32,8 @@ template return_type_t cauchy_lccdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { using T_partials_return = partials_return_t; + using std::atan; + using std::log; static const char* function = "cauchy_lccdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -43,8 +45,6 @@ return_type_t cauchy_lccdf(const T_y& y, const T_loc& mu, return 0; } - using std::atan; - using std::log; T_partials_return ccdf_log(0.0); operands_and_partials ops_partials(y, mu, sigma); diff --git a/stan/math/prim/prob/cauchy_lcdf.hpp b/stan/math/prim/prob/cauchy_lcdf.hpp index 706576a1fba..aeb410bc008 100644 --- a/stan/math/prim/prob/cauchy_lcdf.hpp +++ b/stan/math/prim/prob/cauchy_lcdf.hpp @@ -32,6 +32,8 @@ template return_type_t cauchy_lcdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { using T_partials_return = partials_return_t; + using std::atan; + using std::log; static const char* function = "cauchy_lcdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -43,8 +45,6 @@ return_type_t cauchy_lcdf(const T_y& y, const T_loc& mu, return 0; } - using std::atan; - using std::log; T_partials_return cdf_log(0.0); operands_and_partials ops_partials(y, mu, sigma); diff --git a/stan/math/prim/prob/cauchy_lpdf.hpp b/stan/math/prim/prob/cauchy_lpdf.hpp index d9e8151e1e5..3935e6f330f 100644 --- a/stan/math/prim/prob/cauchy_lpdf.hpp +++ b/stan/math/prim/prob/cauchy_lpdf.hpp @@ -36,6 +36,7 @@ template return_type_t cauchy_lpdf(const T_y& y, const T_loc& mu, const T_scale& sigma) { using T_partials_return = partials_return_t; + using std::log; static const char* function = "cauchy_lpdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -50,7 +51,6 @@ return_type_t cauchy_lpdf(const T_y& y, const T_loc& mu, return 0.0; } - using std::log; T_partials_return logp(0.0); operands_and_partials ops_partials(y, mu, sigma); diff --git a/stan/math/prim/prob/chi_square_cdf.hpp b/stan/math/prim/prob/chi_square_cdf.hpp index 9eb65b03a7c..b9b96533248 100644 --- a/stan/math/prim/prob/chi_square_cdf.hpp +++ b/stan/math/prim/prob/chi_square_cdf.hpp @@ -34,6 +34,8 @@ namespace math { template return_type_t chi_square_cdf(const T_y& y, const T_dof& nu) { using T_partials_return = partials_return_t; + using std::exp; + using std::pow; static const char* function = "chi_square_cdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -45,8 +47,6 @@ return_type_t chi_square_cdf(const T_y& y, const T_dof& nu) { return 1.0; } - using std::exp; - using std::pow; T_partials_return cdf(1.0); operands_and_partials ops_partials(y, nu); diff --git a/stan/math/prim/prob/chi_square_lccdf.hpp b/stan/math/prim/prob/chi_square_lccdf.hpp index 3a3f21c6813..e98591ea8ad 100644 --- a/stan/math/prim/prob/chi_square_lccdf.hpp +++ b/stan/math/prim/prob/chi_square_lccdf.hpp @@ -35,6 +35,9 @@ namespace math { template return_type_t chi_square_lccdf(const T_y& y, const T_dof& nu) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; + using std::pow; static const char* function = "chi_square_lccdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -46,9 +49,6 @@ return_type_t chi_square_lccdf(const T_y& y, const T_dof& nu) { return 0; } - using std::exp; - using std::log; - using std::pow; T_partials_return ccdf_log(0.0); operands_and_partials ops_partials(y, nu); diff --git a/stan/math/prim/prob/chi_square_lcdf.hpp b/stan/math/prim/prob/chi_square_lcdf.hpp index 8f3b3641b82..900511945b8 100644 --- a/stan/math/prim/prob/chi_square_lcdf.hpp +++ b/stan/math/prim/prob/chi_square_lcdf.hpp @@ -35,6 +35,9 @@ namespace math { template return_type_t chi_square_lcdf(const T_y& y, const T_dof& nu) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; + using std::pow; static const char* function = "chi_square_lcdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -46,9 +49,6 @@ return_type_t chi_square_lcdf(const T_y& y, const T_dof& nu) { return 0; } - using std::exp; - using std::log; - using std::pow; T_partials_return cdf_log(0.0); operands_and_partials ops_partials(y, nu); diff --git a/stan/math/prim/prob/chi_square_lpdf.hpp b/stan/math/prim/prob/chi_square_lpdf.hpp index 30a4a8579b7..d55df1a068f 100644 --- a/stan/math/prim/prob/chi_square_lpdf.hpp +++ b/stan/math/prim/prob/chi_square_lpdf.hpp @@ -39,6 +39,7 @@ namespace math { template return_type_t chi_square_lpdf(const T_y& y, const T_dof& nu) { using T_partials_return = partials_return_t; + using std::log; static const char* function = "chi_square_lpdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -53,7 +54,6 @@ return_type_t chi_square_lpdf(const T_y& y, const T_dof& nu) { return 0; } - using std::log; T_partials_return logp(0); operands_and_partials ops_partials(y, nu); diff --git a/stan/math/prim/prob/discrete_range_lpmf.hpp b/stan/math/prim/prob/discrete_range_lpmf.hpp index a3d1632ec15..a8014715369 100644 --- a/stan/math/prim/prob/discrete_range_lpmf.hpp +++ b/stan/math/prim/prob/discrete_range_lpmf.hpp @@ -44,6 +44,7 @@ namespace math { template double discrete_range_lpmf(const T_y& y, const T_lower& lower, const T_upper& upper) { + using std::log; static const char* function = "discrete_range_lpmf"; check_not_nan(function, "Random variable", y); check_consistent_sizes(function, "Lower bound parameter", lower, @@ -57,8 +58,6 @@ double discrete_range_lpmf(const T_y& y, const T_lower& lower, return 0.0; } - using std::log; - scalar_seq_view y_vec(y); scalar_seq_view lower_vec(lower); scalar_seq_view upper_vec(upper); diff --git a/stan/math/prim/prob/double_exponential_cdf.hpp b/stan/math/prim/prob/double_exponential_cdf.hpp index 35b831254c3..376191ac079 100644 --- a/stan/math/prim/prob/double_exponential_cdf.hpp +++ b/stan/math/prim/prob/double_exponential_cdf.hpp @@ -31,6 +31,7 @@ template return_type_t double_exponential_cdf( const T_y& y, const T_loc& mu, const T_scale& sigma) { using T_partials_return = partials_return_t; + using std::exp; static const char* function = "double_exponential_cdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -40,7 +41,6 @@ return_type_t double_exponential_cdf( return 1.0; } - using std::exp; T_partials_return cdf(1.0); operands_and_partials ops_partials(y, mu, sigma); diff --git a/stan/math/prim/prob/double_exponential_lccdf.hpp b/stan/math/prim/prob/double_exponential_lccdf.hpp index ae7fb946c51..6869cfffb1e 100644 --- a/stan/math/prim/prob/double_exponential_lccdf.hpp +++ b/stan/math/prim/prob/double_exponential_lccdf.hpp @@ -34,6 +34,8 @@ template return_type_t double_exponential_lccdf( const T_y& y, const T_loc& mu, const T_scale& sigma) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; static const char* function = "double_exponential_lccdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -45,8 +47,6 @@ return_type_t double_exponential_lccdf( return 0; } - using std::exp; - using std::log; T_partials_return ccdf_log(0.0); operands_and_partials ops_partials(y, mu, sigma); diff --git a/stan/math/prim/prob/double_exponential_lcdf.hpp b/stan/math/prim/prob/double_exponential_lcdf.hpp index ac82ec10fb4..24e906cf824 100644 --- a/stan/math/prim/prob/double_exponential_lcdf.hpp +++ b/stan/math/prim/prob/double_exponential_lcdf.hpp @@ -33,6 +33,8 @@ template return_type_t double_exponential_lcdf( const T_y& y, const T_loc& mu, const T_scale& sigma) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; static const char* function = "double_exponential_lcdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -44,8 +46,6 @@ return_type_t double_exponential_lcdf( return 0; } - using std::exp; - using std::log; T_partials_return cdf_log(0.0); operands_and_partials ops_partials(y, mu, sigma); diff --git a/stan/math/prim/prob/double_exponential_lpdf.hpp b/stan/math/prim/prob/double_exponential_lpdf.hpp index e74d4436cbd..2c91a9fee49 100644 --- a/stan/math/prim/prob/double_exponential_lpdf.hpp +++ b/stan/math/prim/prob/double_exponential_lpdf.hpp @@ -35,6 +35,8 @@ template return_type_t double_exponential_lpdf( const T_y& y, const T_loc& mu, const T_scale& sigma) { using T_partials_return = partials_return_t; + using std::fabs; + using std::log; static const char* function = "double_exponential_lpdf"; check_finite(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -49,8 +51,6 @@ return_type_t double_exponential_lpdf( return 0.0; } - using std::fabs; - using std::log; T_partials_return logp(0.0); operands_and_partials ops_partials(y, mu, sigma); diff --git a/stan/math/prim/prob/exp_mod_normal_cdf.hpp b/stan/math/prim/prob/exp_mod_normal_cdf.hpp index 8e14871e41f..75d9ca4dcdc 100644 --- a/stan/math/prim/prob/exp_mod_normal_cdf.hpp +++ b/stan/math/prim/prob/exp_mod_normal_cdf.hpp @@ -23,6 +23,7 @@ return_type_t exp_mod_normal_cdf( const T_y& y, const T_loc& mu, const T_scale& sigma, const T_inv_scale& lambda) { using T_partials_return = partials_return_t; + using std::exp; static const char* function = "exp_mod_normal_cdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -38,7 +39,6 @@ return_type_t exp_mod_normal_cdf( return 1.0; } - using std::exp; T_partials_return cdf(1.0); operands_and_partials ops_partials( y, mu, sigma, lambda); diff --git a/stan/math/prim/prob/exp_mod_normal_lccdf.hpp b/stan/math/prim/prob/exp_mod_normal_lccdf.hpp index 67b464f7a5a..5039baeef57 100644 --- a/stan/math/prim/prob/exp_mod_normal_lccdf.hpp +++ b/stan/math/prim/prob/exp_mod_normal_lccdf.hpp @@ -24,6 +24,8 @@ return_type_t exp_mod_normal_lccdf( const T_y& y, const T_loc& mu, const T_scale& sigma, const T_inv_scale& lambda) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; static const char* function = "exp_mod_normal_lccdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -39,8 +41,6 @@ return_type_t exp_mod_normal_lccdf( return 0; } - using std::exp; - using std::log; T_partials_return ccdf_log(0.0); operands_and_partials ops_partials( y, mu, sigma, lambda); diff --git a/stan/math/prim/prob/exp_mod_normal_lcdf.hpp b/stan/math/prim/prob/exp_mod_normal_lcdf.hpp index b34980660de..61a59c06ece 100644 --- a/stan/math/prim/prob/exp_mod_normal_lcdf.hpp +++ b/stan/math/prim/prob/exp_mod_normal_lcdf.hpp @@ -24,6 +24,8 @@ return_type_t exp_mod_normal_lcdf( const T_y& y, const T_loc& mu, const T_scale& sigma, const T_inv_scale& lambda) { static const char* function = "exp_mod_normal_lcdf"; + using std::exp; + using std::log; using T_partials_return = partials_return_t; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -39,8 +41,6 @@ return_type_t exp_mod_normal_lcdf( return 0; } - using std::exp; - using std::log; T_partials_return cdf_log(0.0); operands_and_partials ops_partials( y, mu, sigma, lambda); diff --git a/stan/math/prim/prob/exp_mod_normal_lpdf.hpp b/stan/math/prim/prob/exp_mod_normal_lpdf.hpp index ad2346d4a8b..248fcc1c54d 100644 --- a/stan/math/prim/prob/exp_mod_normal_lpdf.hpp +++ b/stan/math/prim/prob/exp_mod_normal_lpdf.hpp @@ -23,6 +23,9 @@ return_type_t exp_mod_normal_lpdf( const T_y& y, const T_loc& mu, const T_scale& sigma, const T_inv_scale& lambda) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; + using std::sqrt; static const char* function = "exp_mod_normal_lpdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -39,9 +42,6 @@ return_type_t exp_mod_normal_lpdf( return 0.0; } - using std::exp; - using std::log; - using std::sqrt; T_partials_return logp(0.0); operands_and_partials ops_partials( y, mu, sigma, lambda); diff --git a/stan/math/prim/prob/exponential_cdf.hpp b/stan/math/prim/prob/exponential_cdf.hpp index e7834eed850..a316dfcd455 100644 --- a/stan/math/prim/prob/exponential_cdf.hpp +++ b/stan/math/prim/prob/exponential_cdf.hpp @@ -28,6 +28,7 @@ template return_type_t exponential_cdf(const T_y& y, const T_inv_scale& beta) { using T_partials_return = partials_return_t; + using std::exp; static const char* function = "exponential_cdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -37,7 +38,6 @@ return_type_t exponential_cdf(const T_y& y, return 1.0; } - using std::exp; T_partials_return cdf(1.0); operands_and_partials ops_partials(y, beta); diff --git a/stan/math/prim/prob/exponential_lcdf.hpp b/stan/math/prim/prob/exponential_lcdf.hpp index 0d15a85228e..75024d5a4f6 100644 --- a/stan/math/prim/prob/exponential_lcdf.hpp +++ b/stan/math/prim/prob/exponential_lcdf.hpp @@ -17,6 +17,8 @@ template return_type_t exponential_lcdf(const T_y& y, const T_inv_scale& beta) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; static const char* function = "exponential_lcdf"; check_not_nan(function, "Random variable", y); check_nonnegative(function, "Random variable", y); @@ -26,8 +28,6 @@ return_type_t exponential_lcdf(const T_y& y, return 0; } - using std::exp; - using std::log; T_partials_return cdf_log(0.0); operands_and_partials ops_partials(y, beta); diff --git a/stan/math/prim/prob/exponential_lpdf.hpp b/stan/math/prim/prob/exponential_lpdf.hpp index 0e8306cf737..51dc9511366 100644 --- a/stan/math/prim/prob/exponential_lpdf.hpp +++ b/stan/math/prim/prob/exponential_lpdf.hpp @@ -45,6 +45,7 @@ template return_type_t exponential_lpdf(const T_y& y, const T_inv_scale& beta) { using T_partials_return = partials_return_t; + using std::log; static const char* function = "exponential_lpdf"; check_nonnegative(function, "Random variable", y); check_positive_finite(function, "Inverse scale parameter", beta); @@ -55,7 +56,6 @@ return_type_t exponential_lpdf(const T_y& y, return 0.0; } - using std::log; T_partials_return logp(0.0); operands_and_partials ops_partials(y, beta); diff --git a/stan/math/prim/prob/frechet_cdf.hpp b/stan/math/prim/prob/frechet_cdf.hpp index 39a6affb14e..0b78a268cdd 100644 --- a/stan/math/prim/prob/frechet_cdf.hpp +++ b/stan/math/prim/prob/frechet_cdf.hpp @@ -24,6 +24,9 @@ return_type_t frechet_cdf(const T_y& y, const T_shape& alpha, const T_scale& sigma) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; + using std::pow; static const char* function = "frechet_cdf"; check_positive(function, "Random variable", y); check_positive_finite(function, "Shape parameter", alpha); @@ -33,9 +36,6 @@ return_type_t frechet_cdf(const T_y& y, return 1.0; } - using std::exp; - using std::log; - using std::pow; T_partials_return cdf(1.0); operands_and_partials ops_partials(y, alpha, sigma); diff --git a/stan/math/prim/prob/frechet_lccdf.hpp b/stan/math/prim/prob/frechet_lccdf.hpp index 9f4afa94ff9..878916a6cb5 100644 --- a/stan/math/prim/prob/frechet_lccdf.hpp +++ b/stan/math/prim/prob/frechet_lccdf.hpp @@ -23,6 +23,9 @@ return_type_t frechet_lccdf(const T_y& y, const T_shape& alpha, const T_scale& sigma) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; + using std::pow; static const char* function = "frechet_lccdf"; check_positive(function, "Random variable", y); check_positive_finite(function, "Shape parameter", alpha); @@ -32,9 +35,6 @@ return_type_t frechet_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 ops_partials(y, alpha, sigma); diff --git a/stan/math/prim/prob/frechet_lcdf.hpp b/stan/math/prim/prob/frechet_lcdf.hpp index d8fe9b0452a..a2f04c2ff90 100644 --- a/stan/math/prim/prob/frechet_lcdf.hpp +++ b/stan/math/prim/prob/frechet_lcdf.hpp @@ -22,6 +22,8 @@ return_type_t frechet_lcdf(const T_y& y, const T_shape& alpha, const T_scale& sigma) { using T_partials_return = partials_return_t; + using std::log; + using std::pow; static const char* function = "frechet_lcdf"; check_positive(function, "Random variable", y); check_positive_finite(function, "Shape parameter", alpha); @@ -31,8 +33,6 @@ return_type_t frechet_lcdf(const T_y& y, return 0.0; } - using std::log; - using std::pow; T_partials_return cdf_log(0.0); operands_and_partials ops_partials(y, alpha, sigma); diff --git a/stan/math/prim/prob/frechet_lpdf.hpp b/stan/math/prim/prob/frechet_lpdf.hpp index 21b5a8d8be1..978a7b89a0c 100644 --- a/stan/math/prim/prob/frechet_lpdf.hpp +++ b/stan/math/prim/prob/frechet_lpdf.hpp @@ -25,6 +25,8 @@ return_type_t frechet_lpdf(const T_y& y, const T_shape& alpha, const T_scale& sigma) { using T_partials_return = partials_return_t; + using std::log; + using std::pow; static const char* function = "frechet_lpdf"; check_positive(function, "Random variable", y); check_positive_finite(function, "Shape parameter", alpha); @@ -39,8 +41,6 @@ return_type_t frechet_lpdf(const T_y& y, return 0; } - using std::log; - using std::pow; T_partials_return logp(0); operands_and_partials ops_partials(y, alpha, sigma); diff --git a/stan/math/prim/prob/gamma_cdf.hpp b/stan/math/prim/prob/gamma_cdf.hpp index 089fc85dd6b..2bd7f966138 100644 --- a/stan/math/prim/prob/gamma_cdf.hpp +++ b/stan/math/prim/prob/gamma_cdf.hpp @@ -40,6 +40,7 @@ return_type_t gamma_cdf(const T_y& y, const T_shape& alpha, const T_inv_scale& beta) { using T_partials_return = partials_return_t; + using std::exp; static const char* function = "gamma_cdf"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Inverse scale parameter", beta); @@ -52,7 +53,6 @@ return_type_t gamma_cdf(const T_y& y, return 1.0; } - using std::exp; T_partials_return P(1.0); operands_and_partials ops_partials(y, alpha, beta); diff --git a/stan/math/prim/prob/gamma_lccdf.hpp b/stan/math/prim/prob/gamma_lccdf.hpp index eacfbd7434a..251bd09f528 100644 --- a/stan/math/prim/prob/gamma_lccdf.hpp +++ b/stan/math/prim/prob/gamma_lccdf.hpp @@ -24,6 +24,9 @@ return_type_t gamma_lccdf(const T_y& y, const T_shape& alpha, const T_inv_scale& beta) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; + using std::pow; static const char* function = "gamma_lccdf"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Inverse scale parameter", beta); @@ -36,9 +39,6 @@ return_type_t gamma_lccdf(const T_y& y, return 0; } - using std::exp; - using std::log; - using std::pow; T_partials_return P(0.0); operands_and_partials ops_partials(y, alpha, beta); diff --git a/stan/math/prim/prob/gamma_lcdf.hpp b/stan/math/prim/prob/gamma_lcdf.hpp index 275a1efd35c..ffac87193c2 100644 --- a/stan/math/prim/prob/gamma_lcdf.hpp +++ b/stan/math/prim/prob/gamma_lcdf.hpp @@ -24,6 +24,9 @@ return_type_t gamma_lcdf(const T_y& y, const T_shape& alpha, const T_inv_scale& beta) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; + using std::pow; static const char* function = "gamma_lcdf"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Inverse scale parameter", beta); @@ -36,9 +39,6 @@ return_type_t gamma_lcdf(const T_y& y, return 0; } - using std::exp; - using std::log; - using std::pow; T_partials_return P(0.0); operands_and_partials ops_partials(y, alpha, beta); diff --git a/stan/math/prim/prob/gamma_lpdf.hpp b/stan/math/prim/prob/gamma_lpdf.hpp index 19024db3d5a..c5333cf63ff 100644 --- a/stan/math/prim/prob/gamma_lpdf.hpp +++ b/stan/math/prim/prob/gamma_lpdf.hpp @@ -45,6 +45,7 @@ return_type_t gamma_lpdf(const T_y& y, const T_shape& alpha, const T_inv_scale& beta) { using T_partials_return = partials_return_t; + using std::log; static const char* function = "gamma_lpdf"; check_not_nan(function, "Random variable", y); check_positive_finite(function, "Shape parameter", alpha); @@ -59,7 +60,6 @@ return_type_t gamma_lpdf(const T_y& y, return 0.0; } - using std::log; T_partials_return logp(0.0); operands_and_partials ops_partials(y, alpha, beta); diff --git a/stan/math/prim/prob/gaussian_dlm_obs_lpdf.hpp b/stan/math/prim/prob/gaussian_dlm_obs_lpdf.hpp index 25110d3fefe..20ab1d10741 100644 --- a/stan/math/prim/prob/gaussian_dlm_obs_lpdf.hpp +++ b/stan/math/prim/prob/gaussian_dlm_obs_lpdf.hpp @@ -71,14 +71,14 @@ gaussian_dlm_obs_lpdf( const Eigen::Matrix& W, const Eigen::Matrix& m0, const Eigen::Matrix& C0) { - static const char* function = "gaussian_dlm_obs_lpdf"; using T_lp = return_type_t>; + using std::pow; int r = y.rows(); // number of variables int T = y.cols(); // number of observations int n = G.rows(); // number of states - using std::pow; + static const char* function = "gaussian_dlm_obs_lpdf"; check_finite(function, "y", y); check_not_nan(function, "y", y); check_size_match(function, "columns of F", F.cols(), "rows of y", y.rows()); diff --git a/stan/math/prim/prob/gumbel_cdf.hpp b/stan/math/prim/prob/gumbel_cdf.hpp index 622e9442170..29fb6b77b4a 100644 --- a/stan/math/prim/prob/gumbel_cdf.hpp +++ b/stan/math/prim/prob/gumbel_cdf.hpp @@ -32,6 +32,7 @@ template return_type_t gumbel_cdf(const T_y& y, const T_loc& mu, const T_scale& beta) { using T_partials_return = partials_return_t; + using std::exp; static const char* function = "gumbel_cdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -44,7 +45,6 @@ return_type_t gumbel_cdf(const T_y& y, const T_loc& mu, return 1.0; } - using std::exp; T_partials_return cdf(1.0); operands_and_partials ops_partials(y, mu, beta); diff --git a/stan/math/prim/prob/gumbel_lccdf.hpp b/stan/math/prim/prob/gumbel_lccdf.hpp index 5f393aad6a6..8d8c35e0ad9 100644 --- a/stan/math/prim/prob/gumbel_lccdf.hpp +++ b/stan/math/prim/prob/gumbel_lccdf.hpp @@ -32,6 +32,8 @@ template return_type_t gumbel_lccdf(const T_y& y, const T_loc& mu, const T_scale& beta) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; static const char* function = "gumbel_lccdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -44,8 +46,6 @@ return_type_t gumbel_lccdf(const T_y& y, const T_loc& mu, return 0; } - using std::exp; - using std::log; T_partials_return ccdf_log(0.0); operands_and_partials ops_partials(y, mu, beta); diff --git a/stan/math/prim/prob/gumbel_lcdf.hpp b/stan/math/prim/prob/gumbel_lcdf.hpp index a9430a7fb3a..b2d37a38e53 100644 --- a/stan/math/prim/prob/gumbel_lcdf.hpp +++ b/stan/math/prim/prob/gumbel_lcdf.hpp @@ -31,6 +31,7 @@ template return_type_t gumbel_lcdf(const T_y& y, const T_loc& mu, const T_scale& beta) { using T_partials_return = partials_return_t; + using std::exp; static const char* function = "gumbel_lcdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -43,7 +44,6 @@ return_type_t gumbel_lcdf(const T_y& y, const T_loc& mu, return 0; } - using std::exp; T_partials_return cdf_log(0.0); operands_and_partials ops_partials(y, mu, beta); diff --git a/stan/math/prim/prob/gumbel_lpdf.hpp b/stan/math/prim/prob/gumbel_lpdf.hpp index edc1332f8da..792dbdb0a49 100644 --- a/stan/math/prim/prob/gumbel_lpdf.hpp +++ b/stan/math/prim/prob/gumbel_lpdf.hpp @@ -33,6 +33,8 @@ template return_type_t gumbel_lpdf(const T_y& y, const T_loc& mu, const T_scale& beta) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; static const char* function = "gumbel_lpdf"; check_not_nan(function, "Random variable", y); check_finite(function, "Location parameter", mu); @@ -47,8 +49,6 @@ return_type_t gumbel_lpdf(const T_y& y, const T_loc& mu, return 0.0; } - using std::exp; - using std::log; T_partials_return logp(0.0); operands_and_partials ops_partials(y, mu, beta); diff --git a/stan/math/prim/prob/inv_chi_square_cdf.hpp b/stan/math/prim/prob/inv_chi_square_cdf.hpp index fdb9882a994..427c242aa39 100644 --- a/stan/math/prim/prob/inv_chi_square_cdf.hpp +++ b/stan/math/prim/prob/inv_chi_square_cdf.hpp @@ -34,6 +34,8 @@ namespace math { template return_type_t inv_chi_square_cdf(const T_y& y, const T_dof& nu) { using T_partials_return = partials_return_t; + using std::exp; + using std::pow; static const char* function = "inv_chi_square_cdf"; check_positive_finite(function, "Degrees of freedom parameter", nu); check_not_nan(function, "Random variable", y); @@ -45,8 +47,6 @@ return_type_t inv_chi_square_cdf(const T_y& y, const T_dof& nu) { return 1.0; } - using std::exp; - using std::pow; T_partials_return P(1.0); operands_and_partials ops_partials(y, nu); diff --git a/stan/math/prim/prob/inv_chi_square_lccdf.hpp b/stan/math/prim/prob/inv_chi_square_lccdf.hpp index d6dca7717d1..77eee9419bd 100644 --- a/stan/math/prim/prob/inv_chi_square_lccdf.hpp +++ b/stan/math/prim/prob/inv_chi_square_lccdf.hpp @@ -35,6 +35,9 @@ namespace math { template return_type_t inv_chi_square_lccdf(const T_y& y, const T_dof& nu) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; + using std::pow; static const char* function = "inv_chi_square_lccdf"; check_positive_finite(function, "Degrees of freedom parameter", nu); check_not_nan(function, "Random variable", y); @@ -46,9 +49,6 @@ return_type_t inv_chi_square_lccdf(const T_y& y, const T_dof& nu) { return 0; } - using std::exp; - using std::log; - using std::pow; T_partials_return P(0.0); operands_and_partials ops_partials(y, nu); diff --git a/stan/math/prim/prob/inv_chi_square_lcdf.hpp b/stan/math/prim/prob/inv_chi_square_lcdf.hpp index 72e7d3c0773..c297273b474 100644 --- a/stan/math/prim/prob/inv_chi_square_lcdf.hpp +++ b/stan/math/prim/prob/inv_chi_square_lcdf.hpp @@ -35,6 +35,9 @@ namespace math { template return_type_t inv_chi_square_lcdf(const T_y& y, const T_dof& nu) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; + using std::pow; static const char* function = "inv_chi_square_lcdf"; check_positive_finite(function, "Degrees of freedom parameter", nu); check_not_nan(function, "Random variable", y); @@ -46,9 +49,6 @@ return_type_t inv_chi_square_lcdf(const T_y& y, const T_dof& nu) { return 0; } - using std::exp; - using std::log; - using std::pow; T_partials_return P(0.0); operands_and_partials ops_partials(y, nu); diff --git a/stan/math/prim/prob/inv_chi_square_lpdf.hpp b/stan/math/prim/prob/inv_chi_square_lpdf.hpp index 8083b897023..08294d3b279 100644 --- a/stan/math/prim/prob/inv_chi_square_lpdf.hpp +++ b/stan/math/prim/prob/inv_chi_square_lpdf.hpp @@ -39,8 +39,9 @@ namespace math { */ template return_type_t inv_chi_square_lpdf(const T_y& y, const T_dof& nu) { - static const char* function = "inv_chi_square_lpdf"; + using std::log; using T_partials_return = partials_return_t; + static const char* function = "inv_chi_square_lpdf"; check_positive_finite(function, "Degrees of freedom parameter", nu); check_not_nan(function, "Random variable", y); check_consistent_sizes(function, "Random variable", y, @@ -50,7 +51,6 @@ return_type_t inv_chi_square_lpdf(const T_y& y, const T_dof& nu) { return 0; } - using std::log; T_partials_return logp(0); operands_and_partials ops_partials(y, nu); diff --git a/stan/math/prim/prob/inv_gamma_cdf.hpp b/stan/math/prim/prob/inv_gamma_cdf.hpp index f45cddbcba7..af8854f74e9 100644 --- a/stan/math/prim/prob/inv_gamma_cdf.hpp +++ b/stan/math/prim/prob/inv_gamma_cdf.hpp @@ -39,6 +39,8 @@ return_type_t inv_gamma_cdf(const T_y& y, const T_shape& alpha, const T_scale& beta) { using T_partials_return = partials_return_t; + using std::exp; + using std::pow; static const char* function = "inv_gamma_cdf"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Scale parameter", beta); @@ -51,8 +53,6 @@ return_type_t inv_gamma_cdf(const T_y& y, return 1.0; } - using std::exp; - using std::pow; T_partials_return P(1.0); operands_and_partials ops_partials(y, alpha, beta); diff --git a/stan/math/prim/prob/inv_gamma_lccdf.hpp b/stan/math/prim/prob/inv_gamma_lccdf.hpp index 7f550e3a60b..b3f35ba7412 100644 --- a/stan/math/prim/prob/inv_gamma_lccdf.hpp +++ b/stan/math/prim/prob/inv_gamma_lccdf.hpp @@ -24,6 +24,9 @@ return_type_t inv_gamma_lccdf(const T_y& y, const T_shape& alpha, const T_scale& beta) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; + using std::pow; static const char* function = "inv_gamma_lccdf"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Scale parameter", beta); @@ -36,9 +39,6 @@ return_type_t inv_gamma_lccdf(const T_y& y, return 0; } - using std::exp; - using std::log; - using std::pow; T_partials_return P(0.0); operands_and_partials ops_partials(y, alpha, beta); diff --git a/stan/math/prim/prob/inv_gamma_lcdf.hpp b/stan/math/prim/prob/inv_gamma_lcdf.hpp index b83b0ab6c83..98568cd6ed8 100644 --- a/stan/math/prim/prob/inv_gamma_lcdf.hpp +++ b/stan/math/prim/prob/inv_gamma_lcdf.hpp @@ -24,6 +24,9 @@ return_type_t inv_gamma_lcdf(const T_y& y, const T_shape& alpha, const T_scale& beta) { using T_partials_return = partials_return_t; + using std::exp; + using std::log; + using std::pow; static const char* function = "inv_gamma_lcdf"; check_positive_finite(function, "Shape parameter", alpha); check_positive_finite(function, "Scale parameter", beta); @@ -36,9 +39,6 @@ return_type_t inv_gamma_lcdf(const T_y& y, return 0; } - using std::exp; - using std::log; - using std::pow; T_partials_return P(0.0); operands_and_partials ops_partials(y, alpha, beta); diff --git a/stan/math/prim/prob/inv_gamma_lpdf.hpp b/stan/math/prim/prob/inv_gamma_lpdf.hpp index 0951a68a232..bc1ae56166b 100644 --- a/stan/math/prim/prob/inv_gamma_lpdf.hpp +++ b/stan/math/prim/prob/inv_gamma_lpdf.hpp @@ -37,6 +37,7 @@ return_type_t inv_gamma_lpdf(const T_y& y, const T_shape& alpha, const T_scale& beta) { using T_partials_return = partials_return_t; + using std::log; static const char* function = "inv_gamma_lpdf"; check_not_nan(function, "Random variable", y); check_positive_finite(function, "Shape parameter", alpha); @@ -51,7 +52,6 @@ return_type_t inv_gamma_lpdf(const T_y& y, return 0; } - using std::log; T_partials_return logp(0); operands_and_partials ops_partials(y, alpha, beta); diff --git a/stan/math/prim/prob/multi_student_t_lpdf.hpp b/stan/math/prim/prob/multi_student_t_lpdf.hpp index 226fd70c53e..09ea93a8e5a 100644 --- a/stan/math/prim/prob/multi_student_t_lpdf.hpp +++ b/stan/math/prim/prob/multi_student_t_lpdf.hpp @@ -30,8 +30,8 @@ return_type_t multi_student_t_lpdf( using T_scale_elem = typename scalar_type::type; using lp_type = return_type_t; using Eigen::Matrix; - using std::vector; using std::log; + using std::vector; static const char* function = "multi_student_t"; check_not_nan(function, "Degrees of freedom parameter", nu); check_positive(function, "Degrees of freedom parameter", nu); From dacc6f5f93d1b21655ad8236d56c6f9111626196 Mon Sep 17 00:00:00 2001 From: Marco Colombo Date: Thu, 5 Mar 2020 21:20:29 +0000 Subject: [PATCH 3/3] Fix definition of check_consistent_sizes with 5 arguments and add docs --- stan/math/prim/err/check_consistent_sizes.hpp | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/stan/math/prim/err/check_consistent_sizes.hpp b/stan/math/prim/err/check_consistent_sizes.hpp index 85db0409f31..263683653b7 100644 --- a/stan/math/prim/err/check_consistent_sizes.hpp +++ b/stan/math/prim/err/check_consistent_sizes.hpp @@ -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) @@ -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 @@ -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 @@ -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 invalid_argument if sizes are inconsistent + */ template inline void check_consistent_sizes(const char* function, const char* name1, const T1& x1, const char* name2, @@ -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::value * stan::math::size(x1), + std::max( + is_vector::value * stan::math::size(x2), + std::max(is_vector::value * stan::math::size(x3), + std::max(is_vector::value * stan::math::size(x4), + is_vector::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);