1 #ifndef __STAN__PROB__DISTRIBUTIONS__WEIBULL_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__WEIBULL_HPP__
4 #include <boost/random/weibull_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
20 template <
bool propto,
21 typename T_y,
typename T_shape,
typename T_scale,
23 typename return_type<T_y,T_shape,T_scale>::type
24 weibull_log(
const T_y& y,
const T_shape& alpha,
const T_scale& sigma,
26 static const char*
function =
"stan::prob::weibull_log(%1%)";
43 if(!
check_finite(
function, y,
"Random variable", &logp, Policy()))
59 "Random variable",
"Shape parameter",
"Scale parameter",
70 size_t N =
max_size(y, alpha, sigma);
72 for (
size_t n = 0; n < N; n++) {
73 const double y_dbl =
value_of(y_vec[n]);
80 for (
size_t i = 0; i <
length(alpha); i++)
86 for (
size_t i = 0; i <
length(y); i++)
92 for (
size_t i = 0; i <
length(sigma); i++)
98 for (
size_t i = 0; i <
length(sigma); i++)
100 inv_sigma[i] = 1.0 /
value_of(sigma_vec[i]);
104 y_div_sigma_pow_alpha(N);
105 for (
size_t i = 0; i < N; i++)
107 const double y_dbl =
value_of(y_vec[i]);
108 const double alpha_dbl =
value_of(alpha_vec[i]);
109 y_div_sigma_pow_alpha[i] =
pow(y_dbl * inv_sigma[i], alpha_dbl);
113 for (
size_t n = 0; n < N; n++) {
114 const double alpha_dbl =
value_of(alpha_vec[n]);
116 logp += log_alpha[n];
118 logp += (alpha_dbl-1.0)*log_y[n];
120 logp -= alpha_dbl*log_sigma[n];
122 logp -= y_div_sigma_pow_alpha[n];
125 const double inv_y = 1.0 /
value_of(y_vec[n]);
126 operands_and_partials.
d_x1[n]
127 += (alpha_dbl-1.0) * inv_y
128 - alpha_dbl * y_div_sigma_pow_alpha[n] * inv_y;
131 operands_and_partials.
d_x2[n]
133 + (1.0 - y_div_sigma_pow_alpha[n]) * (log_y[n] - log_sigma[n]);
135 operands_and_partials.
d_x3[n]
136 += -alpha_dbl * inv_sigma[n]
137 + alpha_dbl * inv_sigma[n] * y_div_sigma_pow_alpha[n];
139 return operands_and_partials.
to_var(logp);
143 template <
bool propto,
144 typename T_y,
typename T_shape,
typename T_scale>
147 weibull_log(
const T_y& y,
const T_shape& alpha,
const T_scale& sigma) {
152 template <
typename T_y,
typename T_shape,
typename T_scale,
156 weibull_log(
const T_y& y,
const T_shape& alpha,
const T_scale& sigma,
158 return weibull_log<false>(y,alpha,sigma,Policy());
162 template <
typename T_y,
typename T_shape,
typename T_scale>
165 weibull_log(
const T_y& y,
const T_shape& alpha,
const T_scale& sigma) {
172 template <
typename T_y,
typename T_shape,
typename T_scale,
174 typename boost::math::tools::promote_args<T_y,T_shape,T_scale>::type
175 weibull_cdf(
const T_y& y,
const T_shape& alpha,
const T_scale& sigma,
178 static const char*
function =
"stan::prob::weibull_cdf(%1%)";
182 using boost::math::tools::promote_args;
184 typename promote_args<T_y,T_shape,T_scale>::type lp;
200 return 1.0 -
exp(-
pow(y / sigma, alpha));
203 template <
typename T_y,
typename T_shape,
typename T_scale>
205 typename boost::math::tools::promote_args<T_y,T_shape,T_scale>::type
206 weibull_cdf(
const T_y& y,
const T_shape& alpha,
const T_scale& sigma) {
216 using boost::variate_generator;
217 using boost::random::weibull_distribution;
218 variate_generator<RNG&, weibull_distribution<> >
219 weibull_rng(rng, weibull_distribution<>(alpha, sigma));