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
10
11namespace stan {
12namespace math {
13
29template <typename T_p, typename T_nu, typename T_mu, typename T_sigma,
30 require_all_stan_scalar_t<T_p, T_nu, T_mu, T_sigma>* = nullptr,
31 require_all_arithmetic_t<T_p, T_nu, T_mu, T_sigma>* = nullptr>
32inline double student_t_qf(const T_p& p, const T_nu& nu, const T_mu& mu,
33 const T_sigma& sigma) {
34 static constexpr const char* function = "student_t_qf";
35 check_nonnegative(function, "Degrees of freedom parameter", nu);
36 check_positive(function, "Scale parameter", sigma);
37 check_bounded(function, "Probability parameter", p, 0.0, 1.0);
38
39 if (p == 0.0) {
40 return NEGATIVE_INFTY;
41 } else if (p == 1.0) {
42 return INFTY;
43 } else if (p == 0.5) {
44 return mu;
45 }
46
47 const double p_val_flip = p < 0.5 ? p : 1.0 - p;
48 const double p_sign = p < 0.5 ? -1.0 : 1.0;
49 const auto ibeta_arg = inv_inc_beta(0.5 * nu, 0.5, 2 * p_val_flip);
50
51 return mu + p_sign * sigma * sqrt(nu) * sqrt(-1.0 + 1.0 / ibeta_arg);
52}
53
70template <typename T_p, typename T_nu, typename T_mu, typename T_sigma,
72inline auto student_t_qf(const T_p& p, const T_nu& nu, const T_mu& mu,
73 const T_sigma& sigma) {
75 static constexpr const char* function = "student_t_qf";
76 const size_t max_size_all = max_size(p, nu, mu, sigma);
77 Eigen::Matrix<ret_scalar, -1, 1> result(max_size_all);
78
79 decltype(auto) p_ref = to_ref(p);
80 decltype(auto) nu_ref = to_ref(nu);
81 decltype(auto) mu_ref = to_ref(mu);
82 decltype(auto) sigma_ref = to_ref(sigma);
83
84 scalar_seq_view<decltype(p_ref)> p_vec(p_ref);
85 scalar_seq_view<decltype(nu_ref)> nu_vec(nu_ref);
86 scalar_seq_view<decltype(mu_ref)> mu_vec(mu_ref);
87 scalar_seq_view<decltype(sigma_ref)> sigma_vec(sigma_ref);
88
89 for (size_t i = 0; i < max_size_all; ++i) {
90 result[i] = student_t_qf(p_vec[i], nu_vec[i], mu_vec[i], sigma_vec[i]);
91 }
92
93 return result;
94}
95
96} // namespace math
97} // namespace stan
98
99#endif
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
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.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
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
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...