Stan Math Library  2.20.0
reverse mode automatic differentiation
binomial_logit_lpmf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BINOMIAL_LOGIT_LPMF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BINOMIAL_LOGIT_LPMF_HPP
3 
16 
17 namespace stan {
18 namespace math {
19 
35 template <bool propto, typename T_n, typename T_N, typename T_prob>
37  const T_N& N,
38  const T_prob& alpha) {
40  T_partials_return;
41 
42  static const char* function = "binomial_logit_lpmf";
43 
44  if (size_zero(n, N, alpha))
45  return 0.0;
46 
47  T_partials_return logp = 0;
48  check_bounded(function, "Successes variable", n, 0, N);
49  check_nonnegative(function, "Population size parameter", N);
50  check_finite(function, "Probability parameter", alpha);
51  check_consistent_sizes(function, "Successes variable", n,
52  "Population size parameter", N,
53  "Probability parameter", alpha);
54 
56  return 0.0;
57 
58  scalar_seq_view<T_n> n_vec(n);
59  scalar_seq_view<T_N> N_vec(N);
60  scalar_seq_view<T_prob> alpha_vec(alpha);
61  size_t size = max_size(n, N, alpha);
62 
63  operands_and_partials<T_prob> ops_partials(alpha);
64 
66  for (size_t i = 0; i < size; ++i)
67  logp += binomial_coefficient_log(N_vec[i], n_vec[i]);
68  }
69 
71  length(alpha));
72  for (size_t i = 0; i < length(alpha); ++i)
73  log_inv_logit_alpha[i] = log_inv_logit(value_of(alpha_vec[i]));
74 
75  VectorBuilder<true, T_partials_return, T_prob> log_inv_logit_neg_alpha(
76  length(alpha));
77  for (size_t i = 0; i < length(alpha); ++i)
78  log_inv_logit_neg_alpha[i] = log_inv_logit(-value_of(alpha_vec[i]));
79 
80  for (size_t i = 0; i < size; ++i)
81  logp += n_vec[i] * log_inv_logit_alpha[i]
82  + (N_vec[i] - n_vec[i]) * log_inv_logit_neg_alpha[i];
83 
84  if (length(alpha) == 1) {
85  T_partials_return temp1 = 0;
86  T_partials_return temp2 = 0;
87  for (size_t i = 0; i < size; ++i) {
88  temp1 += n_vec[i];
89  temp2 += N_vec[i] - n_vec[i];
90  }
92  ops_partials.edge1_.partials_[0]
93  += temp1 * inv_logit(-value_of(alpha_vec[0]))
94  - temp2 * inv_logit(value_of(alpha_vec[0]));
95  }
96  } else {
98  for (size_t i = 0; i < size; ++i)
99  ops_partials.edge1_.partials_[i]
100  += n_vec[i] * inv_logit(-value_of(alpha_vec[i]))
101  - (N_vec[i] - n_vec[i]) * inv_logit(value_of(alpha_vec[i]));
102  }
103  }
104 
105  return ops_partials.build(logp);
106 }
107 
108 template <typename T_n, typename T_N, typename T_prob>
110  const T_n& n, const T_N& N, const T_prob& alpha) {
111  return binomial_logit_lpmf<false>(n, N, alpha);
112 }
113 
114 } // namespace math
115 } // namespace stan
116 #endif
void check_finite(const char *function, const char *name, const T_y &y)
Check if y is finite.
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
fvar< T > binomial_coefficient_log(const fvar< T > &x1, const fvar< T > &x2)
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
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.
fvar< T > inv_logit(const fvar< T > &x)
Returns the inverse logit function applied to the argument.
Definition: inv_logit.hpp:20
size_t length(const std::vector< T > &x)
Returns the length of the provided std::vector.
Definition: length.hpp:16
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...
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
boost::math::tools::promote_args< double, typename scalar_type< T >::type, typename return_type< Types_pack... >::type >::type type
Definition: return_type.hpp:36
return_type< T_prob >::type binomial_logit_lpmf(const T_n &n, const T_N &N, const T_prob &alpha)
Binomial log PMF in logit parametrization.
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.
VectorBuilder allocates type T1 values to be used as intermediate values.
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
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.
fvar< T > log_inv_logit(const fvar< T > &x)
internal::ops_partials_edge< double, Op1 > edge1_

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