Automatic Differentiation
 
Loading...
Searching...
No Matches
bernoulli_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_BERNOULLI_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_BERNOULLI_LPMF_HPP
3
14#include <cmath>
15
16namespace stan {
17namespace math {
18
31template <bool propto, typename T_n, typename T_prob,
33 T_n, T_prob>* = nullptr>
34return_type_t<T_prob> bernoulli_lpmf(const T_n& n, const T_prob& theta) {
35 using T_partials_return = partials_return_t<T_n, T_prob>;
36 using T_theta_ref = ref_type_t<T_prob>;
37 using T_n_ref = ref_type_t<T_n>;
38 using std::log;
39 static constexpr const char* function = "bernoulli_lpmf";
40 check_consistent_sizes(function, "Random variable", n,
41 "Probability parameter", theta);
42 const T_n_ref n_ref = to_ref(n);
43 const T_theta_ref theta_ref = to_ref(theta);
44 check_bounded(function, "n", n_ref, 0, 1);
45 check_bounded(function, "Probability parameter", value_of(theta_ref), 0.0,
46 1.0);
47
48 if (size_zero(n, theta)) {
49 return 0.0;
50 }
52 return 0.0;
53 }
54
55 T_partials_return logp(0.0);
56 auto ops_partials = make_partials_propagator(theta_ref);
57
58 scalar_seq_view<T_n_ref> n_vec(n_ref);
59 scalar_seq_view<T_theta_ref> theta_vec(theta_ref);
60 size_t N = max_size(n, theta);
61
62 if (math::size(theta) == 1) {
63 size_t sum = 0;
64 for (size_t n = 0; n < N; n++) {
65 sum += n_vec.val(n);
66 }
67 const T_partials_return theta_dbl = theta_vec.val(0);
68 // avoid nans when sum == N or sum == 0
69 if (sum == N) {
70 logp += N * log(theta_dbl);
72 partials<0>(ops_partials)[0] += N / theta_dbl;
73 }
74 } else if (sum == 0) {
75 logp += N * log1m(theta_dbl);
77 partials<0>(ops_partials)[0] += N / (theta_dbl - 1);
78 }
79 } else {
80 const T_partials_return log_theta = log(theta_dbl);
81 const T_partials_return log1m_theta = log1m(theta_dbl);
82
83 logp += sum * log_theta;
84 logp += (N - sum) * log1m_theta;
85
87 partials<0>(ops_partials)[0] += sum * inv(theta_dbl);
88 partials<0>(ops_partials)[0] += (N - sum) * inv(theta_dbl - 1);
89 }
90 }
91 } else {
92 for (size_t n = 0; n < N; n++) {
93 const int n_int = n_vec.val(n);
94 const T_partials_return theta_dbl = theta_vec.val(n);
95
96 if (n_int == 1) {
97 logp += log(theta_dbl);
98 } else {
99 logp += log1m(theta_dbl);
100 }
101
103 if (n_int == 1) {
104 partials<0>(ops_partials)[n] += inv(theta_dbl);
105 } else {
106 partials<0>(ops_partials)[n] += inv(theta_dbl - 1);
107 }
108 }
109 }
110 }
111 return ops_partials.build(logp);
112}
113
114template <typename T_y, typename T_prob>
115inline return_type_t<T_prob> bernoulli_lpmf(const T_y& n, const T_prob& theta) {
116 return bernoulli_lpmf<false>(n, theta);
117}
118
119} // namespace math
120} // namespace stan
121#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
require_all_not_t< is_nonscalar_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_not_nonscalar_prim_or_rev_kernel_expression_t
Require none of the types satisfy is_nonscalar_prim_or_rev_kernel_expression.
return_type_t< T_prob_cl > bernoulli_lpmf(const T_n_cl &n, const T_prob_cl &theta)
Returns the log PMF of the Bernoulli distribution.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
int64_t size(const T &m)
Returns the size (number of the elements) of a matrix_cl or var_value<matrix_cl<T>>.
Definition 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
void check_bounded(const char *function, const char *name, const T_y &y, const T_low &low, const T_high &high)
Check if the value is between the low and high values, inclusively.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:17
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
Definition sum.hpp:23
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
fvar< T > log1m(const fvar< T > &x)
Definition log1m.hpp:12
fvar< T > inv(const fvar< T > &x)
Definition inv.hpp:13
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
typename partials_return_type< Args... >::type partials_return_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...