Automatic Differentiation
 
Loading...
Searching...
No Matches
yule_simon_lpmf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_YULE_SIMON_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_YULE_SIMON_LPMF_HPP
3
16
17namespace stan {
18namespace math {
19
33template <bool propto, typename T_n, typename T_alpha,
35 T_n, T_alpha> * = nullptr>
37 const T_alpha &alpha) {
38 using std::log;
39 using T_partials_return = partials_return_t<T_n, T_alpha>;
40 using T_n_ref = ref_type_t<T_n>;
41 using T_alpha_ref = ref_type_t<T_alpha>;
42 static constexpr const char *function = "yule_simon_lpmf";
43 check_consistent_sizes(function, "Failures variable", n, "Shape parameter",
44 alpha);
45 if (size_zero(n, alpha)) {
46 return 0.0;
47 }
48
49 T_n_ref n_ref = n;
50 T_alpha_ref alpha_ref = alpha;
51 check_greater_or_equal(function, "Outcome variable", n_ref, 1);
52 check_positive_finite(function, "Shape parameter", alpha_ref);
53
55 return 0.0;
56 }
57
58 auto ops_partials = make_partials_propagator(alpha_ref);
59
60 scalar_seq_view<T_n_ref> n_vec(n_ref);
61 scalar_seq_view<T_alpha_ref> alpha_vec(alpha_ref);
62 const size_t max_size_seq_view = max_size(n_ref, alpha_ref);
63 T_partials_return logp(0.0);
64 if constexpr (include_summand<propto>::value) {
65 if constexpr (is_stan_scalar_v<T_n>) {
66 logp += lgamma(n_ref) * max_size_seq_view;
67 }
68 }
69 for (size_t i = 0; i < max_size_seq_view; i++) {
70 if constexpr (include_summand<propto>::value) {
71 if constexpr (!is_stan_scalar_v<T_n>) {
72 logp += lgamma(n_vec.val(i));
73 }
74 }
75 T_partials_return alpha_plus_one = alpha_vec.val(i) + 1.0;
76 logp += log(alpha_vec.val(i)) + lgamma(alpha_plus_one)
77 - lgamma(n_vec.val(i) + alpha_plus_one);
78 if constexpr (!is_constant<T_alpha>::value) {
79 partials<0>(ops_partials)[i] += 1.0 / alpha_vec.val(i)
80 + digamma(alpha_plus_one)
81 - digamma(n_vec.val(i) + alpha_plus_one);
82 }
83 }
84 return ops_partials.build(logp);
85}
86
87template <typename T_n, typename T_alpha>
89 const T_alpha &alpha) {
90 return yule_simon_lpmf<false>(n, alpha);
91}
92
93} // namespace math
94} // namespace stan
95#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_alpha > yule_simon_lpmf(const T_n &n, const T_alpha &alpha)
Returns the log PMF of the Yule-Simon distribution with shape parameter.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
bool size_zero(const T &x)
Returns 1 if input is of length 0, returns 0 otherwise.
Definition size_zero.hpp:19
fvar< T > log(const fvar< T > &x)
Definition log.hpp:18
void check_greater_or_equal(const char *function, const char *name, const T_y &y, const T_low &low, Idxs... idxs)
Throw an exception if y is not greater or equal than low.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
Definition lgamma.hpp:21
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
Definition digamma.hpp:23
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 ...
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...