Automatic Differentiation
 
Loading...
Searching...
No Matches
skew_double_exponential_rng.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_PROB_SKEW_DOUBLE_EXPONENTIAL_RNG_HPP
2#define STAN_MATH_PRIM_PROB_SKEW_DOUBLE_EXPONENTIAL_RNG_HPP
3
8#include <boost/random/uniform_real_distribution.hpp>
9#include <boost/random/variate_generator.hpp>
10#include <cmath>
11
12namespace stan {
13namespace math {
14
36template <typename T_loc, typename T_scale, typename T_skewness, class RNG>
38skew_double_exponential_rng(const T_loc& mu, const T_scale& sigma,
39 const T_skewness& tau, RNG& rng) {
40 using boost::variate_generator;
41 using boost::random::uniform_real_distribution;
42 using T_mu_ref = ref_type_t<T_loc>;
43 using T_sigma_ref = ref_type_t<T_scale>;
44 using T_tau_ref = ref_type_t<T_skewness>;
45 static constexpr const char* function = "skew_double_exponential_rng";
46 check_consistent_sizes(function, "Location parameter", mu, "Scale Parameter",
47 sigma, "Skewness Parameter", tau);
48 T_mu_ref mu_ref = mu;
49 T_sigma_ref sigma_ref = sigma;
50 T_tau_ref tau_ref = tau;
51 check_finite(function, "Location parameter", mu_ref);
52 check_positive_finite(function, "Scale parameter", sigma_ref);
53 check_bounded(function, "Skewness parameter", tau_ref, 0.0, 1.0);
54
55 scalar_seq_view<T_mu_ref> mu_vec(mu_ref);
56 scalar_seq_view<T_sigma_ref> sigma_vec(sigma_ref);
57 scalar_seq_view<T_tau_ref> tau_vec(tau_ref);
58 size_t N = max_size(mu, sigma, tau);
60
61 variate_generator<RNG&, uniform_real_distribution<> > z_rng(
62 rng, uniform_real_distribution<>(0.0, 1.0));
63 for (size_t n = 0; n < N; ++n) {
64 double z = z_rng();
65 if (z < tau_vec[n]) {
66 output[n]
67 = log(z / tau_vec[n]) * sigma_vec[n] / (2.0 * (1.0 - tau_vec[n]))
68 + mu_vec[n];
69 } else {
70 output[n] = log((1.0 - z) / (1.0 - tau_vec[n])) * (-sigma_vec[n])
71 / (2.0 * tau_vec[n])
72 + mu_vec[n];
73 }
74 }
75
76 return output.data();
77}
78
79} // namespace math
80} // namespace stan
81#endif
typename helper::type type
VectorBuilder allocates type T1 values to be used as intermediate values.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
VectorBuilder< true, double, T_loc, T_scale, T_skewness >::type skew_double_exponential_rng(const T_loc &mu, const T_scale &sigma, const T_skewness &tau, RNG &rng)
Return a skew double exponential random variate with the given location scale and skewness using the ...
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.
fvar< T > log(const fvar< T > &x)
Definition log.hpp:15
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
void check_finite(const char *function, const char *name, const T_y &y)
Return true if all values in y are finite.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
Definition max_size.hpp:20
void check_positive_finite(const char *function, const char *name, const T_y &y)
Check if y is positive and finite.
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...