Automatic Differentiation
 
Loading...
Searching...
No Matches
student_t_qf.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_STUDENT_T_QF_HPP
2#define STAN_MATH_PRIM_PROB_STUDENT_T_QF_HPP
3
9
10namespace stan {
11namespace math {
12
28template <typename T_p, typename T_nu, typename T_mu, typename T_sigma,
29 require_all_stan_scalar_t<T_p, T_nu, T_mu, T_sigma>* = nullptr,
30 require_all_arithmetic_t<T_p, T_nu, T_mu, T_sigma>* = nullptr>
31inline double student_t_qf(const T_p& p, const T_nu& nu, const T_mu& mu,
32 const T_sigma& sigma) {
33 static constexpr const char* function = "student_t_qf";
34 check_nonnegative(function, "Degrees of freedom parameter", nu);
35 check_positive(function, "Scale parameter", sigma);
36 check_bounded(function, "Probability parameter", p, 0.0, 1.0);
37
38 if (p == 0.0) {
39 return NEGATIVE_INFTY;
40 } else if (p == 1.0) {
41 return INFTY;
42 } else if (p == 0.5) {
43 return mu;
44 }
45
46 const double p_val_flip = p < 0.5 ? p : 1.0 - p;
47 const double p_sign = p < 0.5 ? -1.0 : 1.0;
48 const auto ibeta_arg = inv_inc_beta(0.5 * nu, 0.5, 2 * p_val_flip);
49
50 return mu + p_sign * sigma * sqrt(nu) * sqrt(-1.0 + 1.0 / ibeta_arg);
51}
52
69template <typename T_p, typename T_nu, typename T_mu, typename T_sigma,
71inline auto student_t_qf(const T_p& p, const T_nu& nu, const T_mu& mu,
72 const T_sigma& sigma) {
74 static constexpr const char* function = "student_t_qf";
75 const size_t max_size_all = max_size(p, nu, mu, sigma);
76 T_container result(max_size_all);
77
78 ref_type_t<T_p> p_ref = p;
79 ref_type_t<T_nu> nu_ref = nu;
80 ref_type_t<T_mu> mu_ref = mu;
81 ref_type_t<T_sigma> sigma_ref = sigma;
82
86 scalar_seq_view<ref_type_t<T_sigma>> sigma_vec(sigma_ref);
87
88 for (size_t i = 0; i < max_size_all; ++i) {
89 result[i] = student_t_qf(p_vec[i], nu_vec[i], mu_vec[i], sigma_vec[i]);
90 }
91
92 return result;
93}
94
95} // namespace math
96} // namespace stan
97
98#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
require_any_t< is_vector< std::decay_t< Types > >... > require_any_vector_t
Require any of the types satisfy is_vector.
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
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.
static constexpr double NEGATIVE_INFTY
Negative infinity.
Definition constants.hpp:51
fvar< T > sqrt(const fvar< T > &x)
Definition sqrt.hpp:18
auto student_t_qf(const T_p &p, const T_nu &nu, const T_mu &mu, const T_sigma &sigma)
fvar< partials_return_t< T1, T2, T3 > > inv_inc_beta(const T1 &a, const T2 &b, const T3 &p)
The inverse of the normalized incomplete beta function of a, b, with probability p.
void check_positive(const char *function, const char *name, const T_y &y)
Check if y is positive.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
static constexpr double INFTY
Positive infinity.
Definition constants.hpp:46
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:56
typename common_container_type< Ts... >::type common_container_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...