1 #ifndef STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__EXPONENTIAL_HPP
2 #define STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__EXPONENTIAL_HPP
4 #include <boost/random/exponential_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
44 template <
bool propto,
typename T_y,
typename T_inv_scale>
45 typename return_type<T_y,T_inv_scale>::type
47 static const char*
function =
"stan::prob::exponential_log(%1%)";
64 "Random variable",
"Inverse scale parameter",
76 for (
size_t i = 0; i <
length(beta); i++)
77 if (include_summand<propto,T_inv_scale>::value)
82 for (
size_t n = 0; n < N; n++) {
83 const double beta_dbl =
value_of(beta_vec[n]);
84 const double y_dbl =
value_of(y_vec[n]);
85 if (include_summand<propto,T_inv_scale>::value)
88 logp -= beta_dbl * y_dbl;
91 operands_and_partials.
d_x1[n] -= beta_dbl;
93 operands_and_partials.
d_x2[n] += 1 / beta_dbl - y_dbl;
95 return operands_and_partials.
to_var(logp);
98 template <
typename T_y,
typename T_inv_scale>
102 return exponential_log<false>(y,beta);
120 template <
typename T_y,
typename T_inv_scale>
124 static const char*
function =
"stan::prob::exponential_cdf(%1%)";
129 using boost::math::tools::promote_args;
143 operands_and_partials(y, beta);
148 for (
size_t n = 0; n < N; n++) {
149 const double beta_dbl =
value_of(beta_vec[n]);
150 const double y_dbl =
value_of(y_vec[n]);
151 const double one_m_exp = 1.0 -
exp(-beta_dbl * y_dbl);
157 for(
size_t n = 0; n < N; n++) {
158 const double beta_dbl =
value_of(beta_vec[n]);
159 const double y_dbl =
value_of(y_vec[n]);
160 const double one_m_exp = 1.0 -
exp(-beta_dbl * y_dbl);
163 double rep_deriv =
exp(-beta_dbl * y_dbl) / one_m_exp;
165 operands_and_partials.
d_x1[n] += rep_deriv * beta_dbl * cdf;
167 operands_and_partials.
d_x2[n] += rep_deriv * y_dbl * cdf;
170 return operands_and_partials.
to_var(cdf);
173 template <
typename T_y,
typename T_inv_scale>
177 static const char*
function =
"stan::prob::exponential_cdf_log(%1%)";
182 using boost::math::tools::promote_args;
197 operands_and_partials(y, beta);
202 for (
size_t n = 0; n < N; n++) {
203 const double beta_dbl =
value_of(beta_vec[n]);
204 const double y_dbl =
value_of(y_vec[n]);
205 double one_m_exp = 1.0 -
exp(-beta_dbl * y_dbl);
207 cdf_log +=
log(one_m_exp);
210 double rep_deriv = -
exp(-beta_dbl * y_dbl) / one_m_exp;
212 operands_and_partials.
d_x1[n] -= rep_deriv * beta_dbl;
214 operands_and_partials.
d_x2[n] -= rep_deriv * y_dbl;
216 return operands_and_partials.
to_var(cdf_log);
219 template <
typename T_y,
typename T_inv_scale>
223 static const char*
function =
"stan::prob::exponential_ccdf_log(%1%)";
228 using boost::math::tools::promote_args;
231 double ccdf_log(0.0);
243 operands_and_partials(y, beta);
248 for (
size_t n = 0; n < N; n++) {
249 const double beta_dbl =
value_of(beta_vec[n]);
250 const double y_dbl =
value_of(y_vec[n]);
252 ccdf_log += -beta_dbl * y_dbl;
256 operands_and_partials.
d_x1[n] -= beta_dbl;
258 operands_and_partials.
d_x2[n] -= y_dbl;
260 return operands_and_partials.
to_var(ccdf_log);
267 using boost::variate_generator;
268 using boost::exponential_distribution;
270 static const char*
function =
"stan::prob::exponential_rng(%1%)";
277 variate_generator<RNG&, exponential_distribution<> >
278 exp_rng(rng, exponential_distribution<>(beta));
return_type< T_y, T_inv_scale >::type exponential_ccdf_log(const T_y &y, const T_inv_scale &beta)
T_return_type to_var(double logp)
bool check_positive_finite(const char *function, const T_y &y, const char *name, T_result *result)
return_type< T_y, T_inv_scale >::type exponential_cdf_log(const T_y &y, const T_inv_scale &beta)
DoubleVectorView allocates double values to be used as intermediate values.
double exponential_rng(const double beta, RNG &rng)
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
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
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
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)
return_type< T_y, T_inv_scale >::type exponential_cdf(const T_y &y, const T_inv_scale &beta)
Calculates the exponential cumulative distribution function for the given y and beta.
return_type< T_y, T_inv_scale >::type exponential_log(const T_y &y, const T_inv_scale &beta)
The log of an exponential density for y with the specified inverse scale parameter.