Stan Math Library  2.20.0
reverse mode automatic differentiation
beta_proportion_lccdf.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_SCAL_PROB_BETA_PROPORTION_LCCDF_HPP
2 #define STAN_MATH_PRIM_SCAL_PROB_BETA_PROPORTION_LCCDF_HPP
3 
16 #include <cmath>
17 
18 namespace stan {
19 namespace math {
20 
41 template <typename T_y, typename T_loc, typename T_prec>
43  const T_y& y, const T_loc& mu, const T_prec& kappa) {
45  T_partials_return;
46 
47  static const char* function = "beta_proportion_lccdf";
48 
49  if (size_zero(y, mu, kappa))
50  return 0.0;
51 
52  T_partials_return ccdf_log(0.0);
53 
54  check_positive(function, "Location parameter", mu);
55  check_less_or_equal(function, "Location parameter", mu, 1.0);
56  check_positive_finite(function, "Precision parameter", kappa);
57  check_not_nan(function, "Random variable", y);
58  check_nonnegative(function, "Random variable", y);
59  check_less_or_equal(function, "Random variable", y, 1.0);
60  check_consistent_sizes(function, "Random variable", y, "Location parameter",
61  mu, "Precision parameter", kappa);
62 
63  scalar_seq_view<T_y> y_vec(y);
64  scalar_seq_view<T_loc> mu_vec(mu);
65  scalar_seq_view<T_prec> kappa_vec(kappa);
66  size_t N = max_size(y, mu, kappa);
67 
68  operands_and_partials<T_y, T_loc, T_prec> ops_partials(y, mu, kappa);
69 
70  using std::exp;
71  using std::log;
72  using std::pow;
73 
75  T_loc, T_prec>
76  digamma_mukappa(max_size(mu, kappa));
78  T_loc, T_prec>
79  digamma_kappa_mukappa(max_size(mu, kappa));
81  T_prec>
82  digamma_kappa(length(kappa));
83 
85  for (size_t i = 0; i < max_size(mu, kappa); i++) {
86  const T_partials_return mukappa_dbl
87  = value_of(mu_vec[i]) * value_of(kappa_vec[i]);
88  const T_partials_return kappa_mukappa_dbl
89  = value_of(kappa_vec[i]) - mukappa_dbl;
90 
91  digamma_mukappa[i] = digamma(mukappa_dbl);
92  digamma_kappa_mukappa[i] = digamma(kappa_mukappa_dbl);
93  }
94 
95  for (size_t i = 0; i < length(kappa); i++) {
96  digamma_kappa[i] = digamma(value_of(kappa_vec[i]));
97  }
98  }
99 
100  for (size_t n = 0; n < N; n++) {
101  const T_partials_return y_dbl = value_of(y_vec[n]);
102  const T_partials_return mu_dbl = value_of(mu_vec[n]);
103  const T_partials_return kappa_dbl = value_of(kappa_vec[n]);
104  const T_partials_return mukappa_dbl = mu_dbl * kappa_dbl;
105  const T_partials_return kappa_mukappa_dbl = kappa_dbl - mukappa_dbl;
106  const T_partials_return betafunc_dbl = beta(mukappa_dbl, kappa_mukappa_dbl);
107  const T_partials_return Pn
108  = 1 - inc_beta(mukappa_dbl, kappa_mukappa_dbl, y_dbl);
109 
110  ccdf_log += log(Pn);
111 
113  ops_partials.edge1_.partials_[n] -= pow(1 - y_dbl, kappa_mukappa_dbl - 1)
114  * pow(y_dbl, mukappa_dbl - 1)
115  / betafunc_dbl / Pn;
116 
117  T_partials_return g1 = 0;
118  T_partials_return g2 = 0;
119 
121  grad_reg_inc_beta(g1, g2, mukappa_dbl, kappa_mukappa_dbl, y_dbl,
122  digamma_mukappa[n], digamma_kappa_mukappa[n],
123  digamma_kappa[n], betafunc_dbl);
124  }
126  ops_partials.edge2_.partials_[n] -= kappa_dbl * (g1 - g2) / Pn;
128  ops_partials.edge3_.partials_[n]
129  -= (g1 * mu_dbl + g2 * (1 - mu_dbl)) / Pn;
130  }
131  return ops_partials.build(ccdf_log);
132 }
133 
134 } // namespace math
135 } // namespace stan
136 #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_lccdf(const T_y &y, const T_loc &mu, const T_prec &kappa)
Returns the beta log complementary cumulative distribution function for specified probability...
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.