Stan Math Library  2.20.0
reverse mode automatic differentiation
beta_proportion_lcdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BETA_PROPORTION_LCDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BETA_PROPORTION_LCDF_HPP
3 
17 #include <cmath>
18 
19 namespace stan {
20 namespace math {
21 
42 template <typename T_y, typename T_loc, typename T_prec>
44  const T_y& y, const T_loc& mu, const T_prec& kappa) {
46  T_partials_return;
47 
48  if (size_zero(y, mu, kappa))
49  return 0.0;
50 
51  static const char* function = "beta_proportion_lcdf";
52 
53  T_partials_return cdf_log(0.0);
54 
55  check_positive(function, "Location parameter", mu);
56  check_less_or_equal(function, "Location parameter", mu, 1.0);
57  check_positive_finite(function, "Precision parameter", kappa);
58  check_not_nan(function, "Random variable", y);
59  check_nonnegative(function, "Random variable", y);
60  check_less_or_equal(function, "Random variable", y, 1.0);
61  check_consistent_sizes(function, "Random variable", y, "Location parameter",
62  mu, "Precision parameter", kappa);
63 
64  scalar_seq_view<T_y> y_vec(y);
65  scalar_seq_view<T_loc> mu_vec(mu);
66  scalar_seq_view<T_prec> kappa_vec(kappa);
67  size_t N = max_size(y, mu, kappa);
68 
69  operands_and_partials<T_y, T_loc, T_prec> ops_partials(y, mu, kappa);
70 
71  using std::exp;
72  using std::log;
73  using std::pow;
74 
76  T_loc, T_prec>
77  digamma_mukappa(max_size(mu, kappa));
79  T_loc, T_prec>
80  digamma_kappa_mukappa(max_size(mu, kappa));
82  T_prec>
83  digamma_kappa(length(kappa));
84 
86  for (size_t i = 0; i < max_size(mu, kappa); i++) {
87  const T_partials_return mukappa_dbl
88  = value_of(mu_vec[i]) * value_of(kappa_vec[i]);
89  const T_partials_return kappa_mukappa_dbl
90  = value_of(kappa_vec[i]) - mukappa_dbl;
91 
92  digamma_mukappa[i] = digamma(mukappa_dbl);
93  digamma_kappa_mukappa[i] = digamma(kappa_mukappa_dbl);
94  }
95 
96  for (size_t i = 0; i < length(kappa); i++) {
97  digamma_kappa[i] = digamma(value_of(kappa_vec[i]));
98  }
99  }
100 
101  for (size_t n = 0; n < N; n++) {
102  const T_partials_return y_dbl = value_of(y_vec[n]);
103  const T_partials_return mu_dbl = value_of(mu_vec[n]);
104  const T_partials_return kappa_dbl = value_of(kappa_vec[n]);
105  const T_partials_return mukappa_dbl = mu_dbl * kappa_dbl;
106  const T_partials_return kappa_mukappa_dbl = kappa_dbl - mukappa_dbl;
107  const T_partials_return betafunc_dbl = beta(mukappa_dbl, kappa_mukappa_dbl);
108  const T_partials_return Pn
109  = inc_beta(mukappa_dbl, kappa_mukappa_dbl, y_dbl);
110 
111  cdf_log += log(Pn);
112 
114  ops_partials.edge1_.partials_[n] += pow(1 - y_dbl, kappa_mukappa_dbl - 1)
115  * pow(y_dbl, mukappa_dbl - 1)
116  / betafunc_dbl / Pn;
117 
118  T_partials_return g1 = 0;
119  T_partials_return g2 = 0;
120 
122  grad_reg_inc_beta(g1, g2, mukappa_dbl, kappa_mukappa_dbl, y_dbl,
123  digamma_mukappa[n], digamma_kappa_mukappa[n],
124  digamma_kappa[n], betafunc_dbl);
125  }
127  ops_partials.edge2_.partials_[n] += kappa_dbl * (g1 - g2) / Pn;
129  ops_partials.edge3_.partials_[n]
130  += (g1 * mu_dbl + g2 * (1 - mu_dbl)) / Pn;
131  }
132 
133  return ops_partials.build(cdf_log);
134 }
135 
136 } // namespace math
137 } // namespace stan
138 #endif
void check_less_or_equal(const char *function, const char *name, const T_y &y, const T_high &high)
Check if y is less or equal to high.
boost::math::tools::promote_args< double, typename partials_type< typename scalar_type< T >::type >::type, typename partials_return_type< T_pack... >::type >::type type
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
fvar< T > log(const fvar< T > &x)
Definition: log.hpp:12
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.
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
return_type< T_y, T_loc, T_prec >::type beta_proportion_lcdf(const T_y &y, const T_loc &mu, const T_prec &kappa)
Returns the beta log cumulative distribution function for specified probability, location, and precision parameters: beta_proportion_lcdf(y | mu, kappa) = beta_lcdf(y | mu * kappa, (1 - mu) * kappa).
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
fvar< T > inc_beta(const fvar< T > &a, const fvar< T > &b, const fvar< T > &x)
Definition: inc_beta.hpp:18
fvar< T > beta(const fvar< T > &x1, const fvar< T > &x2)
Return fvar with the beta function applied to the specified arguments and its gradient.
Definition: beta.hpp:51
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
void grad_reg_inc_beta(T &g1, T &g2, const T &a, const T &b, const T &z, const T &digammaA, const T &digammaB, const T &digammaSum, const T &betaAB)
Computes the gradients of the regularized incomplete beta function.
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.
VectorBuilder allocates type T1 values to be used as intermediate values.
internal::ops_partials_edge< double, Op2 > edge2_
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
Definition: pow.hpp:16
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, Op3 > edge3_
internal::ops_partials_edge< double, Op1 > edge1_
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition: digamma.hpp:23

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