Automatic Differentiation
 
Loading...
Searching...
No Matches
discrete_range_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_DISCRETE_RANGE_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_DISCRETE_RANGE_LPMF_HPP
3
13#include <cmath>
14
15namespace stan {
16namespace math {
17
45template <bool propto, typename T_y, typename T_lower, typename T_upper>
46double discrete_range_lpmf(const T_y& y, const T_lower& lower,
47 const T_upper& upper) {
48 using std::log;
49 static constexpr const char* function = "discrete_range_lpmf";
50 check_not_nan(function, "Random variable", y);
51 check_consistent_sizes(function, "Lower bound parameter", lower,
52 "Upper bound parameter", upper);
53 check_greater_or_equal(function, "Upper bound parameter", upper, lower);
54
55 if (size_zero(y, lower, upper)) {
56 return 0.0;
57 }
59 return 0.0;
60 }
61
62 scalar_seq_view<T_y> y_vec(y);
63 scalar_seq_view<T_lower> lower_vec(lower);
64 scalar_seq_view<T_upper> upper_vec(upper);
65 size_t size_lower_upper = max_size(lower, upper);
66 size_t N = max_size(y, lower, upper);
67
68 for (size_t n = 0; n < N; ++n) {
69 const double y_dbl = y_vec.val(n);
70 if (y_dbl < lower_vec.val(n) || y_dbl > upper_vec.val(n)) {
71 return LOG_ZERO;
72 }
73 }
74
76 size_lower_upper);
77
78 for (size_t i = 0; i < size_lower_upper; i++) {
79 const double lower_dbl = lower_vec.val(i);
80 const double upper_dbl = upper_vec.val(i);
81 log_upper_minus_lower[i] = log(upper_dbl - lower_dbl + 1);
82 }
83
84 double logp(0.0);
85 for (size_t n = 0; n < N; n++) {
86 logp -= log_upper_minus_lower[n];
87 }
88
89 return logp;
90}
91
92template <typename T_y, typename T_lower, typename T_upper>
93inline double discrete_range_lpmf(const T_y& y, const T_lower& lower,
94 const T_upper& upper) {
95 return discrete_range_lpmf<false>(y, lower, upper);
96}
97
98} // namespace math
99} // namespace stan
100#endif
VectorBuilder allocates type T1 values to be used as intermediate values.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
double discrete_range_lpmf(const T_y &y, const T_lower &lower, const T_upper &upper)
Return the log PMF of a discrete range for the given y, lower and upper bound (all integers).
static constexpr double LOG_ZERO
The natural logarithm of 0, .
Definition constants.hpp:68
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
void check_greater_or_equal(const char *function, const char *name, const T_y &y, const T_low &low, Idxs... idxs)
Throw an exception if y is not greater or equal than low.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...