Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
cfcff04
Changed logic for select_var_param select the nth parameter instead o…
bbbales2 Mar 5, 2020
4ffbd3c
Check value_of_rec in results so that propto check works for all auto…
bbbales2 May 2, 2020
fa14478
Merge branch 'develop' into bugfix/issue-1763-propto-tests
bbbales2 May 2, 2020
5698804
Removed poisson approximation from negative binomial and updated the …
bbbales2 May 2, 2020
fc3a47b
[Jenkins] auto-formatting by clang-format version 6.0.0
stan-buildbot May 2, 2020
d100bc7
Merge remote-tracking branch 'origin' into bugfix/issue-1763-propto-t…
bbbales2 Jul 17, 2020
ff6046d
Removed vector check from select_var_param and added gradient checks …
bbbales2 Jul 17, 2020
2ff7275
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 17, 2020
49de095
Use long double in test to avoid precision loss (Issue #1763)
bbbales2 Jul 17, 2020
e8e373f
Merge branch 'bugfix/issue-1763-propto-tests' of https://github.com/s…
bbbales2 Jul 17, 2020
483a3c0
Replaced EXPECT_EQ with EXPECT_NEAR for float comparison of normalizi…
bbbales2 Jul 19, 2020
ed2b7f2
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 19, 2020
8213edf
Added missing T_loc arguments to include_summand terms in pareto_type…
bbbales2 Jul 20, 2020
edf1b22
Merge branch 'bugfix/issue-1763-propto-tests' of https://github.com/s…
bbbales2 Jul 20, 2020
ea096c0
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 20, 2020
ee2cf8d
Added n = 0, n = 1 tests to negative binomial pdf (Issue #1763)
bbbales2 Jul 20, 2020
fa21fe0
Merge branch 'bugfix/issue-1763-propto-tests' of https://github.com/s…
bbbales2 Jul 20, 2020
283a2b0
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 20, 2020
4e4ad06
Pulled alpha/beta values into their own containers (Issue #1763)
bbbales2 Jul 21, 2020
a6c5759
Merge branch 'bugfix/issue-1763-propto-tests' of https://github.com/s…
bbbales2 Jul 21, 2020
c1822fc
Switching back to logs of value_ofs in neg_binomial. Adding some chan…
bbbales2 Jul 21, 2020
55cf763
[Jenkins] auto-formatting by clang-format version 6.0.0-1ubuntu2~16.0…
stan-buildbot Jul 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 24 additions & 47 deletions stan/math/prim/prob/neg_binomial_lpmf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,69 +65,46 @@ return_type_t<T_shape, T_inv_scale> neg_binomial_lpmf(const T_n& n,
}
}

VectorBuilder<true, T_partials_return, T_inv_scale> log_beta(size_beta);
VectorBuilder<true, T_partials_return, T_inv_scale> log1p_inv_beta(size_beta);
VectorBuilder<true, T_partials_return, T_inv_scale> log1p_beta(size_beta);
VectorBuilder<true, T_partials_return, T_inv_scale> log_beta_m_log1p_beta(
size_beta);
for (size_t i = 0; i < size_beta; ++i) {
const T_partials_return beta_dbl = value_of(beta_vec[i]);
log_beta[i] = log(beta_dbl);
log1p_inv_beta[i] = log1p(inv(beta_dbl));
log1p_beta[i] = log1p(beta_dbl);
log_beta_m_log1p_beta[i] = log_beta[i] - log1p_beta[i];
}

VectorBuilder<true, T_partials_return, T_shape, T_inv_scale> lambda(
size_alpha_beta);
VectorBuilder<true, T_partials_return, T_shape, T_inv_scale>
alpha_log_beta_over_1p_beta(size_alpha_beta);
VectorBuilder<!is_constant_all<T_inv_scale>::value, T_partials_return,
T_shape, T_inv_scale>
lambda_m_alpha_over_1p_beta(size_alpha_beta);
for (size_t i = 0; i < size_alpha_beta; ++i) {
const T_partials_return alpha_dbl = value_of(alpha_vec[i]);
const T_partials_return beta_dbl = value_of(beta_vec[i]);
lambda[i] = alpha_dbl / beta_dbl;
alpha_log_beta_over_1p_beta[i] = alpha_dbl * log_beta_m_log1p_beta[i];
if (!is_constant_all<T_inv_scale>::value) {
lambda_m_alpha_over_1p_beta[i] = lambda[i] - alpha_dbl / (1 + beta_dbl);
if (!is_constant_all<T_inv_scale>::value) {
for (size_t i = 0; i < size_alpha_beta; ++i) {
const T_partials_return alpha_dbl = value_of(alpha_vec[i]);
const T_partials_return beta_dbl = value_of(beta_vec[i]);
lambda_m_alpha_over_1p_beta[i]
= alpha_dbl / beta_dbl - alpha_dbl / (1 + beta_dbl);
}
}

for (size_t i = 0; i < max_size_seq_view; i++) {
if (alpha_vec[i] > internal::neg_binomial_alpha_cutoff) {
// reduces numerically to Poisson
if (include_summand<propto>::value) {
logp -= lgamma(n_vec[i] + 1.0);
}
logp += multiply_log(n_vec[i], lambda[i]) - lambda[i];
const T_partials_return alpha_dbl = value_of(alpha_vec[i]);
const T_partials_return beta_dbl = value_of(beta_vec[i]);

if (!is_constant_all<T_shape>::value) {
ops_partials.edge1_.partials_[i]
+= n_vec[i] / value_of(alpha_vec[i]) - 1.0 / value_of(beta_vec[i]);
}
if (!is_constant_all<T_inv_scale>::value) {
ops_partials.edge2_.partials_[i]
+= (lambda[i] - n_vec[i]) / value_of(beta_vec[i]);
if (include_summand<propto, T_shape>::value) {
if (n_vec[i] != 0) {
logp += binomial_coefficient_log(n_vec[i] + alpha_dbl - 1.0,
alpha_dbl - 1.0);
}
} else { // standard density definition
if (include_summand<propto, T_shape>::value) {
if (n_vec[i] != 0) {
logp += binomial_coefficient_log(
n_vec[i] + value_of(alpha_vec[i]) - 1.0, n_vec[i]);
}
}
logp += alpha_log_beta_over_1p_beta[i] - n_vec[i] * log1p_beta[i];
}
logp -= alpha_dbl * log1p_inv_beta[i] + n_vec[i] * log1p_beta[i];

if (!is_constant_all<T_shape>::value) {
ops_partials.edge1_.partials_[i]
+= digamma(value_of(alpha_vec[i]) + n_vec[i]) - digamma_alpha[i]
+ log_beta_m_log1p_beta[i];
}
if (!is_constant_all<T_inv_scale>::value) {
ops_partials.edge2_.partials_[i]
+= lambda_m_alpha_over_1p_beta[i]
- n_vec[i] / (value_of(beta_vec[i]) + 1.0);
}
if (!is_constant_all<T_shape>::value) {
ops_partials.edge1_.partials_[i] += digamma(alpha_dbl + n_vec[i])
- digamma_alpha[i]
- log1p_inv_beta[i];
}
if (!is_constant_all<T_inv_scale>::value) {
ops_partials.edge2_.partials_[i]
+= lambda_m_alpha_over_1p_beta[i] - n_vec[i] / (beta_dbl + 1.0);
}
}

Expand Down
7 changes: 4 additions & 3 deletions stan/math/prim/prob/pareto_type_2_lpdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ return_type_t<T_y, T_loc, T_scale, T_shape> pareto_type_2_lpdf(
inv_alpha(size(alpha));
if (!is_constant_all<T_shape>::value) {
for (size_t n = 0; n < stan::math::size(alpha); n++) {
inv_alpha[n] = 1 / value_of(alpha_vec[n]);
inv_alpha[n] = inv(value_of(alpha_vec[n]));
}
}

Expand All @@ -85,7 +85,7 @@ return_type_t<T_y, T_loc, T_scale, T_shape> pareto_type_2_lpdf(
const T_partials_return deriv_1_2 = inv_sum + alpha_div_sum;

const T_partials_return log1p_scaled_diff
= include_summand<propto, T_y, T_scale, T_shape>::value
= include_summand<propto, T_y, T_loc, T_scale, T_shape>::value
? log1p((y_dbl - mu_dbl) / lambda_dbl)
: 0;

Expand All @@ -95,7 +95,8 @@ return_type_t<T_y, T_loc, T_scale, T_shape> pareto_type_2_lpdf(
if (include_summand<propto, T_scale>::value) {
logp -= log_lambda[n];
}
if (include_summand<propto, T_y, T_scale, T_shape>::value) {

if (include_summand<propto, T_y, T_loc, T_scale, T_shape>::value) {
logp -= (alpha_dbl + 1.0) * log1p_scaled_diff;
}

Expand Down
6 changes: 3 additions & 3 deletions test/prob/neg_binomial/neg_binomial_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class AgradDistributionsNegBinomial : public AgradDistributionTest {
log_prob.push_back(-142.6147368129045105434); // expected log_prob

param[0] = 13;
param[1] = 1e11; // alpha > 1e10, causes redux to Poisson
param[2] = 1e10; // equiv to Poisson(1e11/1e10) = Poisson(10)
param[1] = 1e11;
param[2] = 1e10;
parameters.push_back(param);
log_prob.push_back(-2.6185576442008289933); // log poisson(13|10)
log_prob.push_back(-2.6185576442208003); // expected log_prob
}

void invalid_values(vector<size_t>& index, vector<double>& value) {
Expand Down
5 changes: 3 additions & 2 deletions test/prob/test_fixture_distr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using stan::is_vector;
using stan::scalar_type;
using stan::math::fvar;
using stan::math::value_of;
using stan::math::value_of_rec;
using stan::math::var;
using std::vector;

Expand Down Expand Up @@ -251,8 +252,8 @@ class AgradDistributionTestFixture : public ::testing::Test {
Scalar3, Scalar4, Scalar5>(p0, p1, p2,
p3, p4, p5);

EXPECT_TRUE(reference_logprob_false - logprob_false
== reference_logprob_true - logprob_true)
EXPECT_NEAR(value_of_rec(reference_logprob_false - logprob_false),
value_of_rec(reference_logprob_true - logprob_true), 1e-12)
<< "Proportional test failed at index: " << n << std::endl
<< " reference params: " << parameters[0] << std::endl
<< " current params: " << parameters[n] << std::endl
Expand Down
2 changes: 1 addition & 1 deletion test/prob/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ typename scalar_type<T>::type select_var_param(
const vector<vector<double>>& parameters, const size_t n, const size_t p) {
typename scalar_type<T>::type param(0);
if (p < parameters[0].size()) {
if (is_vector<T>::value && !is_constant_all<T>::value)
if (!is_constant_all<T>::value)
param = parameters[n][p];
else
param = parameters[0][p];
Expand Down
19 changes: 19 additions & 0 deletions test/unit/math/mix/prob/neg_binomial_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stan/math/rev.hpp>
#include <test/unit/math/test_ad.hpp>
#include <gtest/gtest.h>
#include <boost/math/differentiation/finite_difference.hpp>

TEST(mathMixScalFun, neg_binomial_lpmf_derivatives) {
auto f = [](const int y) {
return [=](const auto& alpha, const auto& beta) {
return stan::math::neg_binomial_lpmf(y, alpha, beta);
};
};

stan::test::expect_ad(f(0), 1.5, 4.1);
stan::test::expect_ad(f(0), std::vector<double>({1.2, 2.0}),
std::vector<double>({1.1, 1.2}));
stan::test::expect_ad(f(6), 1.5, 4.1);
stan::test::expect_ad(f(6), std::vector<double>({1.7, 2.0}),
std::vector<double>({1.1, 2.3}));
}
11 changes: 11 additions & 0 deletions test/unit/math/prim/prob/neg_binomial_log_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ TEST(ProbNegBinomial, log_matches_lpmf) {
double alpha = 1.1;
double beta = 2.3;

long double alpha2 = 1e11;
long double beta2 = 1e10;

EXPECT_FLOAT_EQ(stan::math::neg_binomial_lpmf(0, 1e11, 1e10),
-alpha2 * std::log1p(1.0 / beta2));
EXPECT_FLOAT_EQ(stan::math::neg_binomial_lpmf(1, 1e11, 1e10),
std::log(alpha2 - 1.0) - alpha2 * std::log1p(1.0 / beta2)
- std::log1p(beta2));
EXPECT_FLOAT_EQ((stan::math::neg_binomial_lpmf(13, 1e11, 1e10)),
-2.6185576442208003);

EXPECT_FLOAT_EQ((stan::math::neg_binomial_lpmf(y, alpha, beta)),
(stan::math::neg_binomial_log(y, alpha, beta)));
EXPECT_FLOAT_EQ((stan::math::neg_binomial_lpmf<true>(y, alpha, beta)),
Expand Down
4 changes: 2 additions & 2 deletions test/unit/math/prim/prob/neg_binomial_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ TEST(ProbDistributionsNegBinomial, error_check) {

void expected_bin_sizes(double* expect, const int K, const int N,
const double alpha, const double beta) {
double p = 0;
long double p = 0;
for (int i = 0; i < K; i++) {
expect[i] = N * std::exp(stan::math::neg_binomial_log(i, alpha, beta));
p += std::exp(stan::math::neg_binomial_log(i, alpha, beta));
}
expect[K - 1] = N * (1.0 - p);
expect[K - 1] = N * static_cast<double>(1.0 - p);
}

TEST(ProbDistributionsNegBinomial, chiSquareGoodnessFitTest) {
Expand Down