1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CAUCHY_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CAUCHY_HPP__
4 #include <boost/random/cauchy_distribution.hpp>
5 #include <boost/random/variate_generator.hpp>
36 template <
bool propto,
37 typename T_y,
typename T_loc,
typename T_scale,
39 typename return_type<T_y,T_loc,T_scale>::type
40 cauchy_log(
const T_y& y,
const T_loc& mu,
const T_scale& sigma,
42 static const char*
function =
"stan::prob::cauchy_log(%1%)";
61 if (!
check_not_nan(
function, y,
"Random variable", &logp, Policy()))
74 "Random variable",
"Location parameter",
"Scale parameter",
94 for (
size_t i = 0; i <
length(sigma); i++) {
95 const double sigma_dbl =
value_of(sigma_vec[i]);
96 inv_sigma[i] = 1.0 / sigma_dbl;
97 sigma_squared[i] = sigma_dbl * sigma_dbl;
99 log_sigma[i] =
log(sigma_dbl);
105 for (
size_t n = 0; n < N; n++) {
107 const double y_dbl =
value_of(y_vec[n]);
108 const double mu_dbl =
value_of(mu_vec[n]);
111 const double y_minus_mu
113 const double y_minus_mu_squared
114 = y_minus_mu * y_minus_mu;
115 const double y_minus_mu_over_sigma
116 = y_minus_mu * inv_sigma[n];
117 const double y_minus_mu_over_sigma_squared
118 = y_minus_mu_over_sigma * y_minus_mu_over_sigma;
124 logp -= log_sigma[n];
126 logp -=
log1p(y_minus_mu_over_sigma_squared);
130 operands_and_partials.
d_x1[n] -= 2 * y_minus_mu / (sigma_squared[n] + y_minus_mu_squared);
132 operands_and_partials.
d_x2[n] += 2 * y_minus_mu / (sigma_squared[n] + y_minus_mu_squared);
134 operands_and_partials.
d_x3[n] += (y_minus_mu_squared - sigma_squared[n]) * inv_sigma[n] / (sigma_squared[n] + y_minus_mu_squared);
136 return operands_and_partials.
to_var(logp);
140 template <
bool propto,
141 typename T_y,
typename T_loc,
typename T_scale>
144 cauchy_log(
const T_y& y,
const T_loc& mu,
const T_scale& sigma) {
148 template <
typename T_y,
typename T_loc,
typename T_scale,
152 cauchy_log(
const T_y& y,
const T_loc& mu,
const T_scale& sigma,
154 return cauchy_log<false>(y,mu,sigma,Policy());
157 template <
typename T_y,
typename T_loc,
typename T_scale>
160 cauchy_log(
const T_y& y,
const T_loc& mu,
const T_scale& sigma) {
180 template <
typename T_y,
typename T_loc,
typename T_scale,
class Policy>
182 cauchy_cdf(
const T_y& y,
const T_loc& mu,
const T_scale& sigma,
const Policy&) {
187 static const char*
function =
"stan::prob::cauchy_cdf(%1%)";
193 using boost::math::tools::promote_args;
198 if(!
check_not_nan(
function, y,
"Random variable", &P, Policy()))
201 if(!
check_finite(
function, mu,
"Location parameter", &P, Policy()))
204 if(!
check_finite(
function, sigma,
"Scale parameter", &P, Policy()))
207 if(!
check_positive(
function, sigma,
"Scale parameter", &P, Policy()))
211 "Random variable",
"Location parameter",
"Scale Parameter",
229 if (
value_of(y_vec[i]) == -std::numeric_limits<double>::infinity())
230 return operands_and_partials.
to_var(0.0);
238 for (
size_t n = 0; n < N; n++) {
242 if (
value_of(y_vec[n]) == std::numeric_limits<double>::infinity()) {
247 const double y_dbl =
value_of(y_vec[n]);
248 const double mu_dbl =
value_of(mu_vec[n]);
249 const double sigma_inv_dbl = 1.0 /
value_of(sigma_vec[n]);
251 const double z = (y_dbl - mu_dbl) * sigma_inv_dbl;
254 const double Pn =
atan(z) /
pi() + 0.5;
259 operands_and_partials.
d_x1[n]
260 += sigma_inv_dbl / (
pi() * (1.0 + z * z) * Pn);
263 operands_and_partials.
d_x2[n]
264 += - sigma_inv_dbl / (
pi() * (1.0 + z * z) * Pn);
267 operands_and_partials.
d_x3[n]
268 += - z * sigma_inv_dbl / (
pi() * (1.0 + z * z) * Pn);
273 for(
size_t n = 0; n <
stan::length(y); ++n) operands_and_partials.
d_x1[n] *= P;
277 for(
size_t n = 0; n <
stan::length(mu); ++n) operands_and_partials.
d_x2[n] *= P;
281 for(
size_t n = 0; n <
stan::length(sigma); ++n) operands_and_partials.
d_x3[n] *= P;
284 return operands_and_partials.
to_var(P);
287 template <
typename T_y,
typename T_loc,
typename T_scale>
289 cauchy_cdf(
const T_y& y,
const T_loc& mu,
const T_scale& sigma) {
298 using boost::variate_generator;
299 using boost::random::cauchy_distribution;
300 variate_generator<RNG&, cauchy_distribution<> >
301 cauchy_rng(rng, cauchy_distribution<>(mu, sigma));