1#ifndef STAN_MATH_PRIM_PROB_YULE_SIMON_LPMF_HPP
2#define STAN_MATH_PRIM_PROB_YULE_SIMON_LPMF_HPP
33template <
bool propto,
typename T_n,
typename T_alpha,
35 T_n, T_alpha> * =
nullptr>
37 const T_alpha &alpha) {
42 static constexpr const char *function =
"yule_simon_lpmf";
50 T_alpha_ref alpha_ref = alpha;
62 const size_t max_size_seq_view =
max_size(n_ref, alpha_ref);
63 T_partials_return logp(0.0);
65 if constexpr (is_stan_scalar_v<T_n>) {
66 logp +=
lgamma(n_ref) * max_size_seq_view;
69 for (
size_t i = 0; i < max_size_seq_view; i++) {
71 if constexpr (!is_stan_scalar_v<T_n>) {
72 logp +=
lgamma(n_vec.val(i));
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);
79 partials<0>(ops_partials)[i] += 1.0 / alpha_vec.val(i)
81 -
digamma(n_vec.val(i) + alpha_plus_one);
84 return ops_partials.build(logp);
87template <
typename T_n,
typename T_alpha>
89 const T_alpha &alpha) {
90 return yule_simon_lpmf<false>(n, alpha);
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.
fvar< T > log(const fvar< T > &x)
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.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
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.
typename ref_type_if< true, T >::type ref_type_t
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...