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>
21 template <
bool propto,
22 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) {
25 static const char*
function =
"stan::prob::weibull_log(%1%)";
47 "Random variable",
"Shape parameter",
58 size_t N =
max_size(y, alpha, sigma);
60 for (
size_t n = 0; n < N; n++) {
61 const double y_dbl =
value_of(y_vec[n]);
68 for (
size_t i = 0; i <
length(alpha); i++)
74 for (
size_t i = 0; i <
length(y); i++)
80 for (
size_t i = 0; i <
length(sigma); i++)
86 for (
size_t i = 0; i <
length(sigma); i++)
88 inv_sigma[i] = 1.0 /
value_of(sigma_vec[i]);
92 y_div_sigma_pow_alpha(N);
93 for (
size_t i = 0; i < N; i++)
95 const double y_dbl =
value_of(y_vec[i]);
96 const double alpha_dbl =
value_of(alpha_vec[i]);
97 y_div_sigma_pow_alpha[i] =
pow(y_dbl * inv_sigma[i], alpha_dbl);
101 for (
size_t n = 0; n < N; n++) {
102 const double alpha_dbl =
value_of(alpha_vec[n]);
104 logp += log_alpha[n];
106 logp += (alpha_dbl-1.0)*log_y[n];
108 logp -= alpha_dbl*log_sigma[n];
110 logp -= y_div_sigma_pow_alpha[n];
113 const double inv_y = 1.0 /
value_of(y_vec[n]);
114 operands_and_partials.
d_x1[n]
115 += (alpha_dbl-1.0) * inv_y
116 - alpha_dbl * y_div_sigma_pow_alpha[n] * inv_y;
119 operands_and_partials.
d_x2[n]
121 + (1.0 - y_div_sigma_pow_alpha[n]) * (log_y[n] - log_sigma[n]);
123 operands_and_partials.
d_x3[n]
124 += alpha_dbl * inv_sigma[n] * ( y_div_sigma_pow_alpha[n] - 1.0 );
126 return operands_and_partials.
to_var(logp);
129 template <
typename T_y,
typename T_shape,
typename T_scale>
132 weibull_log(
const T_y& y,
const T_shape& alpha,
const T_scale& sigma) {
133 return weibull_log<false>(y,alpha,sigma);
136 template <
typename T_y,
typename T_shape,
typename T_scale>
138 weibull_cdf(
const T_y& y,
const T_shape& alpha,
const T_scale& sigma) {
140 static const char*
function =
"stan::prob::weibull_cdf(%1%)";
144 using boost::math::tools::promote_args;
159 operands_and_partials(y, alpha, sigma);
164 size_t N =
max_size(y, sigma, alpha);
165 for (
size_t n = 0; n < N; n++) {
166 const double y_dbl =
value_of(y_vec[n]);
167 const double sigma_dbl =
value_of(sigma_vec[n]);
168 const double alpha_dbl =
value_of(alpha_vec[n]);
169 const double pow_ =
pow(y_dbl / sigma_dbl, alpha_dbl);
170 const double exp_ =
exp(-pow_);
171 const double cdf_ = 1.0 - exp_;
177 const double rep_deriv = exp_ * pow_ / cdf_;
179 operands_and_partials.
d_x1[n] += rep_deriv * alpha_dbl / y_dbl;
181 operands_and_partials.
d_x2[n] += rep_deriv *
log(y_dbl / sigma_dbl);
183 operands_and_partials.
d_x3[n] -= rep_deriv * alpha_dbl / sigma_dbl;
188 operands_and_partials.
d_x1[n] *= cdf;
191 operands_and_partials.
d_x2[n] *= cdf;
194 operands_and_partials.
d_x3[n] *= cdf;
196 return operands_and_partials.
to_var(cdf);
199 template <
typename T_y,
typename T_shape,
typename T_scale>
203 static const char*
function =
"stan::prob::weibull_cdf_log(%1%)";
207 using boost::math::tools::promote_args;
222 operands_and_partials(y, alpha, sigma);
227 size_t N =
max_size(y, sigma, alpha);
228 for (
size_t n = 0; n < N; n++) {
229 const double y_dbl =
value_of(y_vec[n]);
230 const double sigma_dbl =
value_of(sigma_vec[n]);
231 const double alpha_dbl =
value_of(alpha_vec[n]);
232 const double pow_ =
pow(y_dbl / sigma_dbl, alpha_dbl);
233 const double exp_ =
exp(-pow_);
234 const double cdf_ = 1.0 - exp_;
237 cdf_log +=
log(cdf_);
240 const double rep_deriv = pow_ / (1.0 / exp_ - 1.0);
242 operands_and_partials.
d_x1[n] += rep_deriv * alpha_dbl / y_dbl;
244 operands_and_partials.
d_x2[n] += rep_deriv *
log(y_dbl / sigma_dbl);
246 operands_and_partials.
d_x3[n] -= rep_deriv * alpha_dbl / sigma_dbl;
249 return operands_and_partials.
to_var(cdf_log);
252 template <
typename T_y,
typename T_shape,
typename T_scale>
256 static const char*
function =
"stan::prob::weibull_ccdf_log(%1%)";
260 using boost::math::tools::promote_args;
269 double ccdf_log(0.0);
275 operands_and_partials(y, alpha, sigma);
280 size_t N =
max_size(y, sigma, alpha);
281 for (
size_t n = 0; n < N; n++) {
282 const double y_dbl =
value_of(y_vec[n]);
283 const double sigma_dbl =
value_of(sigma_vec[n]);
284 const double alpha_dbl =
value_of(alpha_vec[n]);
285 const double pow_ =
pow(y_dbl / sigma_dbl, alpha_dbl);
292 operands_and_partials.
d_x1[n] -= alpha_dbl / y_dbl * pow_;
294 operands_and_partials.
d_x2[n] -=
log(y_dbl / sigma_dbl) * pow_;
296 operands_and_partials.
d_x3[n] += alpha_dbl / sigma_dbl * pow_;
299 return operands_and_partials.
to_var(ccdf_log);
307 using boost::variate_generator;
308 using boost::random::weibull_distribution;
310 static const char*
function =
"stan::prob::weibull_rng(%1%)";
317 variate_generator<RNG&, weibull_distribution<> >
318 weibull_rng(rng, weibull_distribution<>(alpha, sigma));
double weibull_rng(const double alpha, const double sigma, RNG &rng)
return_type< T_y, T_shape, T_scale >::type weibull_cdf_log(const T_y &y, const T_shape &alpha, const T_scale &sigma)
T_return_type to_var(double logp)
bool check_positive_finite(const char *function, const T_y &y, const char *name, T_result *result)
fvar< T > pow(const fvar< T > &x1, const fvar< T > &x2)
boost::math::tools::promote_args< T_a, T_b >::type multiply_log(const T_a a, const T_b b)
Calculated the value of the first argument times log of the second argument while behaving properly w...
return_type< T_y, T_shape, T_scale >::type weibull_cdf(const T_y &y, const T_shape &alpha, const T_scale &sigma)
DoubleVectorView allocates double values to be used as intermediate values.
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...
return_type< T_y, T_shape, T_scale >::type weibull_log(const T_y &y, const T_shape &alpha, const T_scale &sigma)
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
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...
VectorView< double *, is_vector< T2 >::value, is_constant_struct< T2 >::value > d_x2
return_type< T_y, T_shape, T_scale >::type weibull_ccdf_log(const T_y &y, const T_shape &alpha, const T_scale &sigma)
bool check_nonnegative(const char *function, const T_y &y, const char *name, T_result *result)
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_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< T1 >::value, is_constant_struct< T1 >::value > d_x1
VectorView< double *, is_vector< T3 >::value, is_constant_struct< T3 >::value > d_x3
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)