Skip to content

Commit aa576fd

Browse files
yasumorishimaclaude
andcommitted
Fix CodeRabbit review findings
- Guard gradient computations against theta=0 division by zero in geometric_lpmf, geometric_cdf, and geometric_lcdf - Fix include ordering in prob.hpp (gaussian before geometric) - Fix signed/unsigned comparison in geometric_rng loop index Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b61d325 commit aa576fd

5 files changed

Lines changed: 16 additions & 7 deletions

File tree

stan/math/prim/prob.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@
111111
#include <stan/math/prim/prob/gamma_lcdf.hpp>
112112
#include <stan/math/prim/prob/gamma_lpdf.hpp>
113113
#include <stan/math/prim/prob/gamma_rng.hpp>
114+
#include <stan/math/prim/prob/gaussian_dlm_obs_lpdf.hpp>
115+
#include <stan/math/prim/prob/gaussian_dlm_obs_rng.hpp>
114116
#include <stan/math/prim/prob/geometric_cdf.hpp>
115117
#include <stan/math/prim/prob/geometric_lccdf.hpp>
116118
#include <stan/math/prim/prob/geometric_lcdf.hpp>
117119
#include <stan/math/prim/prob/geometric_lpmf.hpp>
118120
#include <stan/math/prim/prob/geometric_rng.hpp>
119-
#include <stan/math/prim/prob/gaussian_dlm_obs_lpdf.hpp>
120-
#include <stan/math/prim/prob/gaussian_dlm_obs_rng.hpp>
121121
#include <stan/math/prim/prob/gumbel_ccdf_log.hpp>
122122
#include <stan/math/prim/prob/gumbel_cdf.hpp>
123123
#include <stan/math/prim/prob/gumbel_cdf_log.hpp>

stan/math/prim/prob/geometric_cdf.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ inline return_type_t<T_prob> geometric_cdf(const T_n& n,
7575
cdf *= cdf_i;
7676

7777
if constexpr (is_autodiff_v<T_prob>) {
78-
partials<0>(ops_partials)[i]
79-
+= np1 * ccdf_i / ((1.0 - theta_val) * cdf_i);
78+
if (cdf_i > 0.0 && theta_val > 0.0) {
79+
partials<0>(ops_partials)[i]
80+
+= np1 * ccdf_i / ((1.0 - theta_val) * cdf_i);
81+
}
8082
}
8183
}
8284

stan/math/prim/prob/geometric_lcdf.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ inline return_type_t<T_prob> geometric_lcdf(const T_n& n,
7575

7676
if constexpr (is_autodiff_v<T_prob>) {
7777
const auto ccdf = stan::math::exp(log_ccdf);
78-
partials<0>(ops_partials)[i]
79-
+= np1 * ccdf / ((1.0 - theta_val) * (1.0 - ccdf));
78+
if (theta_val > 0.0 && ccdf < 1.0) {
79+
partials<0>(ops_partials)[i]
80+
+= np1 * ccdf / ((1.0 - theta_val) * (1.0 - ccdf));
81+
}
8082
}
8183
}
8284

stan/math/prim/prob/geometric_lpmf.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ inline return_type_t<T_prob> geometric_lpmf(const T_n& n,
7878
const auto theta_val = theta_vec.val(i);
7979
const auto n_val = n_vec.val(i);
8080

81+
// When theta == 0.0, P(n) = 0 for all n
82+
if (theta_val == 0.0) {
83+
return negative_infinity();
84+
}
85+
8186
// When theta == 1.0, P(n=0) = 1, P(n>0) = 0
8287
if (theta_val == 1.0) {
8388
if (n_val > 0) {

stan/math/prim/prob/geometric_rng.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ inline auto geometric_rng(T_prob&& theta, RNG& rng) {
5858
} else {
5959
auto theta_arr = as_array_or_scalar(theta_ref);
6060
std::vector<int> result(theta_arr.size());
61-
for (int i = 0; i < theta_arr.size(); i++) {
61+
for (size_t i = 0; i < theta_arr.size(); i++) {
6262
if (theta_arr[i] == 1.0) {
6363
result[i] = 0;
6464
} else {

0 commit comments

Comments
 (0)