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,
21 typename return_type<T_y,T_loc,T_scale>::type
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