1 #ifndef __STAN__PROB__DISTRIBUTIONS__DOUBLE_EXPONENTIAL_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__DOUBLE_EXPONENTIAL_HPP__
10 #include <boost/math/special_functions/sign.hpp>
18 template <
bool propto,
19 typename T_y,
typename T_loc,
typename T_scale,
24 static const char*
function
25 =
"stan::prob::double_exponential_log(%1%)";
35 using boost::math::sign;
46 if(!
check_finite(
function, y,
"Random variable", &logp, Policy()))
59 "Random variable",
"Location parameter",
"Shape parameter",
77 inv_sigma_squared(
length(sigma));
80 for (
size_t i = 0; i <
length(sigma); i++) {
81 const double sigma_dbl =
value_of(sigma_vec[i]);
83 inv_sigma[i] = 1.0 / sigma_dbl;
87 inv_sigma_squared[i] = inv_sigma[i] * inv_sigma[i];
91 for (
size_t n = 0; n < N; n++) {
92 const double y_dbl =
value_of(y_vec[n]);
93 const double mu_dbl =
value_of(mu_vec[n]);
96 const double y_m_mu = y_dbl - mu_dbl;
97 const double fabs_y_m_mu =
fabs(y_m_mu);
103 logp -= log_sigma[n];
105 logp -= fabs_y_m_mu * inv_sigma[n];
108 double sign_y_m_mu_times_inv_sigma(0);
110 sign_y_m_mu_times_inv_sigma = sign(y_m_mu) * inv_sigma[n];
112 operands_and_partials.
d_x1[n] -= sign_y_m_mu_times_inv_sigma;
115 operands_and_partials.
d_x2[n] += sign_y_m_mu_times_inv_sigma;
118 operands_and_partials.
d_x3[n] += -inv_sigma[n] + fabs_y_m_mu * inv_sigma_squared[n];
120 return operands_and_partials.
to_var(logp);
124 template <
bool propto,
125 typename T_y,
typename T_loc,
typename T_scale>
128 const T_scale& sigma) {
129 return double_exponential_log<propto>(y,mu,sigma,
134 template <
typename T_y,
typename T_loc,
typename T_scale,
139 return double_exponential_log<false>(y,mu,sigma,Policy());
142 template <
typename T_y,
typename T_loc,
typename T_scale>
145 const T_scale& sigma) {
146 return double_exponential_log<false>(y,mu,sigma,
164 template <
typename T_y,
typename T_loc,
typename T_scale,
169 static const char*
function
170 =
"stan::prob::double_exponential_cdf(%1%)";
174 using boost::math::tools::promote_args;
176 typename promote_args<T_y,T_loc,T_scale>::type lp(0.0);
177 if(!
check_finite(
function, y,
"Random variable", &lp, Policy()))
190 return exp((y-mu)/sigma)/2;
192 return 1 -
exp((mu-y)/sigma)/2;
195 template <
typename T_y,
typename T_loc,
typename T_scale>
196 typename boost::math::tools::promote_args<T_y,T_loc,T_scale>::type
var fabs(const var &a)
Return the absolute value of the variable (cmath).
double value_of(const agrad::var &v)
Return the value of the specified variable.
var log(const var &a)
Return the natural log of the specified variable (cmath).
var exp(const var &a)
Return the exponentiation of the specified variable (cmath).
double value_of(T x)
Return the value of the specified scalar argument converted to a double value.
bool check_consistent_sizes(const char *function, const T1 &x1, const T2 &x2, const char *name1, const char *name2, T_result *result, const Policy &)
bool check_positive(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
bool check_finite(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is finite.
boost::math::policies::policy default_policy
Default error-handling policy from Boost.
return_type< T_y, T_loc, T_scale >::type double_exponential_log(const T_y &y, const T_loc &mu, const T_scale &sigma, const Policy &)
return_type< T_y, T_loc, T_scale >::type double_exponential_cdf(const T_y &y, const T_loc &mu, const T_scale &sigma, const Policy &)
Calculates the double exponential cumulative density function.
Probability, optimization and sampling library.
size_t length(const T &x)
size_t max_size(const T1 &x1, const T2 &x2)
A variable implementation that stores operands and derivatives with respect to the variable.
VectorView< double *, is_vector< T1 >::value > d_x1
VectorView< double *, is_vector< T2 >::value > d_x2
T_return_type to_var(double logp)
VectorView< double *, is_vector< T3 >::value > d_x3
Metaprogram to determine if a type has a base scalar type that can be assigned to type double.
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
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