1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__SKEW__NORMAL__HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__SKEW__NORMAL__HPP
4 #include <boost/random/variate_generator.hpp>
5 #include <boost/math/distributions.hpp>
20 template <
bool propto,
21 typename T_y,
typename T_loc,
typename T_scale,
typename T_shape>
22 typename return_type<T_y,T_loc,T_scale,T_shape>::type
24 const T_shape& alpha) {
25 static const char*
function =
"stan::prob::skew_normal_log(%1%)";
53 "Random variable",
"Location parameter",
54 "Scale parameter",
"Shape paramter",
63 operands_and_partials(y, mu, sigma, alpha);
69 size_t N =
max_size(y, mu, sigma, alpha);
74 for (
size_t i = 0; i <
length(sigma); i++) {
75 inv_sigma[i] = 1.0 /
value_of(sigma_vec[i]);
80 for (
size_t n = 0; n < N; n++) {
82 const double y_dbl =
value_of(y_vec[n]);
83 const double mu_dbl =
value_of(mu_vec[n]);
84 const double sigma_dbl =
value_of(sigma_vec[n]);
85 const double alpha_dbl =
value_of(alpha_vec[n]);
88 const double y_minus_mu_over_sigma
89 = (y_dbl - mu_dbl) * inv_sigma[n];
90 const double pi_dbl = boost::math::constants::pi<double>();
94 logp -= 0.5 *
log(2.0 * pi_dbl);
96 logp -=
log(sigma_dbl);
98 logp -= y_minus_mu_over_sigma * y_minus_mu_over_sigma / 2.0;
106 *
exp(-alpha_dbl * y_minus_mu_over_sigma /
std::sqrt(2.0)
107 * alpha_dbl * y_minus_mu_over_sigma /
std::sqrt(2.0))
111 operands_and_partials.
d_x1[n]
112 += -y_minus_mu_over_sigma / sigma_dbl
113 + deriv_logerf * alpha_dbl / (sigma_dbl *
std::sqrt(2.0)) ;
115 operands_and_partials.
d_x2[n]
116 += y_minus_mu_over_sigma / sigma_dbl
117 + deriv_logerf * -alpha_dbl / (sigma_dbl *
std::sqrt(2.0));
119 operands_and_partials.
d_x3[n]
121 + y_minus_mu_over_sigma * y_minus_mu_over_sigma / sigma_dbl
122 - deriv_logerf * y_minus_mu_over_sigma * alpha_dbl
125 operands_and_partials.
d_x4[n]
126 += deriv_logerf * y_minus_mu_over_sigma /
std::sqrt(2.0);
128 return operands_and_partials.
to_var(logp);
131 template <
typename T_y,
typename T_loc,
typename T_scale,
typename T_shape>
135 const T_shape& alpha) {
136 return skew_normal_log<false>(y,mu,sigma,alpha);
139 template <
typename T_y,
typename T_loc,
typename T_scale,
typename T_shape>
142 const T_shape& alpha) {
143 static const char*
function =
"stan::prob::skew_normal_cdf(%1%)";
169 "Random variable",
"Location parameter",
170 "Scale parameter",
"Shape paramter",
174 operands_and_partials(y, mu, sigma, alpha);
183 size_t N =
max_size(y, mu, sigma, alpha);
186 for (
size_t n = 0; n < N; n++) {
187 const double y_dbl =
value_of(y_vec[n]);
188 const double mu_dbl =
value_of(mu_vec[n]);
189 const double sigma_dbl =
value_of(sigma_vec[n]);
190 const double alpha_dbl =
value_of(alpha_vec[n]);
191 const double alpha_dbl_sq = alpha_dbl * alpha_dbl;
192 const double diff = (y_dbl - mu_dbl) / sigma_dbl;
193 const double diff_sq = diff * diff;
194 const double scaled_diff = diff /
SQRT_2;
195 const double scaled_diff_sq = diff_sq * 0.5;
196 const double cdf_ = 0.5 *
erfc(-scaled_diff) - 2 *
owens_t(diff,
202 const double deriv_erfc = SQRT_TWO_OVER_PI * 0.5 *
exp(-scaled_diff_sq)
204 const double deriv_owens =
erf(alpha_dbl * scaled_diff)
205 *
exp(-scaled_diff_sq) / SQRT_TWO_OVER_PI / (-2.0 *
pi()) / sigma_dbl;
206 const double rep_deriv = (-2.0 * deriv_owens + deriv_erfc) / cdf_;
209 operands_and_partials.
d_x1[n] += rep_deriv;
211 operands_and_partials.
d_x2[n] -= rep_deriv;
213 operands_and_partials.
d_x3[n] -= rep_deriv * diff;
215 operands_and_partials.
d_x4[n] += -2.0 *
exp(-0.5 * diff_sq * (1.0
216 + alpha_dbl_sq)) / ((1 + alpha_dbl_sq) * 2.0 *
pi()) / cdf_;
221 operands_and_partials.
d_x1[n] *= cdf;
224 operands_and_partials.
d_x2[n] *= cdf;
227 operands_and_partials.
d_x3[n] *= cdf;
230 operands_and_partials.
d_x4[n] *= cdf;
232 return operands_and_partials.
to_var(cdf);
235 template <
typename T_y,
typename T_loc,
typename T_scale,
typename T_shape>
238 const T_shape& alpha) {
239 static const char*
function =
"stan::prob::skew_normal_cdf_log(%1%)";
258 check_finite(
function, mu,
"Location parameter", &cdf_log);
261 check_finite(
function, alpha,
"Shape parameter", &cdf_log);
265 "Random variable",
"Location parameter",
266 "Scale parameter",
"Shape paramter",
270 operands_and_partials(y, mu, sigma, alpha);
279 size_t N =
max_size(y, mu, sigma, alpha);
282 for (
size_t n = 0; n < N; n++) {
283 const double y_dbl =
value_of(y_vec[n]);
284 const double mu_dbl =
value_of(mu_vec[n]);
285 const double sigma_dbl =
value_of(sigma_vec[n]);
286 const double alpha_dbl =
value_of(alpha_vec[n]);
287 const double alpha_dbl_sq = alpha_dbl * alpha_dbl;
288 const double diff = (y_dbl - mu_dbl) / sigma_dbl;
289 const double diff_sq = diff * diff;
290 const double scaled_diff = diff /
SQRT_2;
291 const double scaled_diff_sq = diff_sq * 0.5;
292 const double cdf_log_ = 0.5 *
erfc(-scaled_diff) - 2 *
owens_t(diff,
295 cdf_log +=
log(cdf_log_);
298 const double deriv_erfc = SQRT_TWO_OVER_PI * 0.5 *
exp(-scaled_diff_sq)
300 const double deriv_owens =
erf(alpha_dbl * scaled_diff)
301 *
exp(-scaled_diff_sq) / SQRT_TWO_OVER_PI / (-2.0 *
pi()) / sigma_dbl;
302 const double rep_deriv = (-2.0 * deriv_owens + deriv_erfc) / cdf_log_;
305 operands_and_partials.
d_x1[n] += rep_deriv;
307 operands_and_partials.
d_x2[n] -= rep_deriv;
309 operands_and_partials.
d_x3[n] -= rep_deriv * diff;
311 operands_and_partials.
d_x4[n] += -2.0 *
exp(-0.5 * diff_sq * (1.0
312 + alpha_dbl_sq)) / ((1 + alpha_dbl_sq) * 2.0 *
pi()) / cdf_log_;
315 return operands_and_partials.
to_var(cdf_log);
318 template <
typename T_y,
typename T_loc,
typename T_scale,
typename T_shape>
321 const T_shape& alpha) {
322 static const char*
function =
"stan::prob::skew_normal_ccdf_log(%1%)";
331 double ccdf_log(0.0);
341 check_finite(
function, mu,
"Location parameter", &ccdf_log);
342 check_not_nan(
function, sigma,
"Scale parameter", &ccdf_log);
344 check_finite(
function, alpha,
"Shape parameter", &ccdf_log);
345 check_not_nan(
function, alpha,
"Shape parameter", &ccdf_log);
348 "Random variable",
"Location parameter",
349 "Scale parameter",
"Shape paramter",
353 operands_and_partials(y, mu, sigma, alpha);
362 size_t N =
max_size(y, mu, sigma, alpha);
365 for (
size_t n = 0; n < N; n++) {
366 const double y_dbl =
value_of(y_vec[n]);
367 const double mu_dbl =
value_of(mu_vec[n]);
368 const double sigma_dbl =
value_of(sigma_vec[n]);
369 const double alpha_dbl =
value_of(alpha_vec[n]);
370 const double alpha_dbl_sq = alpha_dbl * alpha_dbl;
371 const double diff = (y_dbl - mu_dbl) / sigma_dbl;
372 const double diff_sq = diff * diff;
373 const double scaled_diff = diff /
SQRT_2;
374 const double scaled_diff_sq = diff_sq * 0.5;
375 const double ccdf_log_ = 1.0 - 0.5 *
erfc(-scaled_diff) + 2 *
owens_t(diff,
378 ccdf_log +=
log(ccdf_log_);
381 const double deriv_erfc = SQRT_TWO_OVER_PI * 0.5 *
exp(-scaled_diff_sq)
383 const double deriv_owens =
erf(alpha_dbl * scaled_diff)
384 *
exp(-scaled_diff_sq) / SQRT_TWO_OVER_PI / (-2.0 *
pi()) / sigma_dbl;
385 const double rep_deriv = (-2.0 * deriv_owens + deriv_erfc) / ccdf_log_;
388 operands_and_partials.
d_x1[n] -= rep_deriv;
390 operands_and_partials.
d_x2[n] += rep_deriv;
392 operands_and_partials.
d_x3[n] += rep_deriv * diff;
394 operands_and_partials.
d_x4[n] -= -2.0 *
exp(-0.5 * diff_sq * (1.0
395 + alpha_dbl_sq)) / ((1 + alpha_dbl_sq) * 2.0 *
pi()) / ccdf_log_;
398 return operands_and_partials.
to_var(ccdf_log);
407 boost::math::skew_normal_distribution<>
dist (mu, sigma, alpha);
409 static const char*
function =
"stan::prob::skew_normal_rng(%1%)";
414 check_finite(
function, mu,
"Location parameter", (
double*)0);
415 check_finite(
function, alpha,
"Shape parameter", (
double*)0);
fvar< T > erf(const fvar< T > &x)
T_return_type to_var(double logp)
fvar< T > erfc(const fvar< T > &x)
DoubleVectorView allocates double values to be used as intermediate values.
return_type< T_y, T_loc, T_scale, T_shape >::type skew_normal_cdf_log(const T_y &y, const T_loc &mu, const T_scale &sigma, const T_shape &alpha)
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is finite.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
A variable implementation that stores operands and derivatives with respect to the variable...
boost::math::tools::promote_args< typename scalar_type< T1 >::type, typename scalar_type< T2 >::type, typename scalar_type< T3 >::type, typename scalar_type< T4 >::type, typename scalar_type< T5 >::type, typename scalar_type< T6 >::type >::type type
return_type< T_y, T_loc, T_scale, T_shape >::type skew_normal_cdf(const T_y &y, const T_loc &mu, const T_scale &sigma, const T_shape &alpha)
boost::math::tools::promote_args< T1, T2 >::type owens_t(const T1 &h, const T2 &a)
The Owen's T function of h and a.
Metaprogram to determine if a type has a base scalar type that can be assigned to type double...
double value_of(const T x)
Return the value of the specified scalar argument converted to a double value.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
double skew_normal_rng(const double mu, const double sigma, const double alpha, RNG &rng)
const double SQRT_2
The value of the square root of 2, .
fvar< T > owens_t(const fvar< T > &x1, const fvar< T > &x2)
fvar< T > sqrt(const fvar< T > &x)
VectorView< double *, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
return_type< T_y, T_loc, T_scale, T_shape >::type skew_normal_log(const T_y &y, const T_loc &mu, const T_scale &sigma, const T_shape &alpha)
double uniform_rng(const double alpha, const double beta, RNG &rng)
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result)
size_t max_size(const T1 &x1, const T2 &x2)
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result)
return_type< T_y, T_loc, T_scale, T_shape >::type skew_normal_ccdf_log(const T_y &y, const T_loc &mu, const T_scale &sigma, const T_shape &alpha)
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result)
Checks if the variable y is nan.
VectorView< double *, is_vector< T4 >::value, is_constant_struct< T4 >::value > d_x4
VectorView< double *, is_vector< T1 >::value, is_constant_struct< T1 >::value > d_x1
VectorView< double *, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
double pi()
Return the value of pi.
fvar< T > log(const fvar< T > &x)
VectorView is a template metaprogram that takes its argument and allows it to be used like a vector...
fvar< T > exp(const fvar< T > &x)
double dist(const std::vector< double > &x, const std::vector< double > &y)