Stan Math Library  2.20.0
reverse mode automatic differentiation
bernoulli_logit_lpmf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BERNOULLI_LOGIT_LPMF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BERNOULLI_LOGIT_LPMF_HPP
3 
11 #include <cmath>
12 
13 namespace stan {
14 namespace math {
15 
28 template <bool propto, typename T_n, typename T_prob>
30  const T_prob& theta) {
31  static const char* function = "bernoulli_logit_lpmf";
32  typedef
33  typename stan::partials_return_type<T_n, T_prob>::type T_partials_return;
34 
35  using std::exp;
36 
37  if (size_zero(n, theta))
38  return 0.0;
39 
40  T_partials_return logp(0.0);
41 
42  check_bounded(function, "n", n, 0, 1);
43  check_not_nan(function, "Logit transformed probability parameter", theta);
44  check_consistent_sizes(function, "Random variable", n,
45  "Probability parameter", theta);
46 
48  return 0.0;
49 
50  scalar_seq_view<T_n> n_vec(n);
51  scalar_seq_view<T_prob> theta_vec(theta);
52  size_t N = max_size(n, theta);
53  operands_and_partials<T_prob> ops_partials(theta);
54 
55  for (size_t n = 0; n < N; n++) {
56  const T_partials_return theta_dbl = value_of(theta_vec[n]);
57 
58  const int sign = 2 * n_vec[n] - 1;
59  const T_partials_return ntheta = sign * theta_dbl;
60  const T_partials_return exp_m_ntheta = exp(-ntheta);
61 
62  // Handle extreme values gracefully using Taylor approximations.
63  static const double cutoff = 20.0;
64  if (ntheta > cutoff)
65  logp -= exp_m_ntheta;
66  else if (ntheta < -cutoff)
67  logp += ntheta;
68  else
69  logp -= log1p(exp_m_ntheta);
70 
72  if (ntheta > cutoff)
73  ops_partials.edge1_.partials_[n] -= exp_m_ntheta;
74  else if (ntheta < -cutoff)
75  ops_partials.edge1_.partials_[n] += sign;
76  else
77  ops_partials.edge1_.partials_[n]
78  += sign * exp_m_ntheta / (exp_m_ntheta + 1);
79  }
80  }
81  return ops_partials.build(logp);
82 }
83 
84 template <typename T_n, typename T_prob>
86  const T_n& n, const T_prob& theta) {
87  return bernoulli_logit_lpmf<false>(n, theta);
88 }
89 
90 } // namespace math
91 } // namespace stan
92 #endif
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
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:17
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...
Definition: conjunction.hpp:14
int sign(const T &z)
Definition: sign.hpp:10
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
This template builds partial derivatives with respect to a set of operands.
bool size_zero(T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition: size_zero.hpp:18
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:11
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
size_t max_size(const T1 &x1, const T2 &x2)
Definition: max_size.hpp:9
T_return_type build(double value)
Build the node to be stored on the autodiff graph.
fvar< T > log1p(const fvar< T > &x)
Definition: log1p.hpp:12
return_type< T_prob >::type bernoulli_logit_lpmf(const T_n &n, const T_prob &theta)
Returns the log PMF of the logit-parametrized Bernoulli distribution.
void check_consistent_sizes(const char *function, const char *name1, const T1 &x1, const char *name2, const T2 &x2)
Check if the dimension of x1 is consistent with x2.
internal::ops_partials_edge< double, Op1 > edge1_

     [ Stan Home Page ] © 2011–2018, Stan Development Team.