Automatic Differentiation
 
Loading...
Searching...
No Matches
discrete_range_lcdf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_DISCRETE_RANGE_LCDF_HPP
2#define STAN_MATH_PRIM_PROB_DISCRETE_RANGE_LCDF_HPP
3
12#include <cmath>
13
14namespace stan {
15namespace math {
16
37template <typename T_y, typename T_lower, typename T_upper>
38double discrete_range_lcdf(const T_y& y, const T_lower& lower,
39 const T_upper& upper) {
40 static constexpr const char* function = "discrete_range_lcdf";
41 check_consistent_sizes(function, "Lower bound parameter", lower,
42 "Upper bound parameter", upper);
43 check_greater_or_equal(function, "Upper bound parameter", upper, lower);
44
45 if (size_zero(y, lower, upper)) {
46 return 0;
47 }
48
49 scalar_seq_view<T_y> y_vec(y);
50 scalar_seq_view<T_lower> lower_vec(lower);
51 scalar_seq_view<T_upper> upper_vec(upper);
52 size_t N = max_size(y, lower, upper);
53
54 for (size_t n = 0; n < N; ++n) {
55 const int y_dbl = y_vec[n];
56 if (y_dbl < lower_vec[n]) {
57 return LOG_ZERO;
58 }
59 }
60
61 double cdf(0.0);
62 for (size_t n = 0; n < N; n++) {
63 const int y_dbl = y_vec[n];
64 if (y_dbl < upper_vec[n]) {
65 const int lower_dbl = lower_vec[n];
66 const int upper_dbl = upper_vec[n];
67 cdf += log(y_dbl - lower_dbl + 1) - log(upper_dbl - lower_dbl + 1);
68 }
69 }
70 return cdf;
71}
72
73} // namespace math
74} // namespace stan
75#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
double discrete_range_lcdf(const T_y &y, const T_lower &lower, const T_upper &upper)
Return the log CDF of a discrete range distribution for the given y, lower and upper bounds (all inte...
static constexpr double LOG_ZERO
The natural logarithm of 0, .
Definition constants.hpp:68
size_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:19
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.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9