1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CAUCHY_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CAUCHY_HPP__
32 template <
bool propto,
33 typename T_y,
typename T_loc,
typename T_scale,
36 cauchy_log(
const T_y& y,
const T_loc& mu,
const T_scale& sigma,
38 static const char*
function =
"stan::prob::cauchy_log(%1%)";
57 if (!
check_not_nan(
function, y,
"Random variable", &logp, Policy()))
70 "Random variable",
"Location parameter",
"Scale parameter",
90 for (
size_t i = 0; i <
length(sigma); i++) {
91 const double sigma_dbl =
value_of(sigma_vec[i]);
92 inv_sigma[i] = 1.0 / sigma_dbl;
93 sigma_squared[i] = sigma_dbl * sigma_dbl;
95 log_sigma[i] =
log(sigma_dbl);
101 for (
size_t n = 0; n < N; n++) {
103 const double y_dbl =
value_of(y_vec[n]);
104 const double mu_dbl =
value_of(mu_vec[n]);
107 const double y_minus_mu
109 const double y_minus_mu_squared
110 = y_minus_mu * y_minus_mu;
111 const double y_minus_mu_over_sigma
112 = y_minus_mu * inv_sigma[n];
113 const double y_minus_mu_over_sigma_squared
114 = y_minus_mu_over_sigma * y_minus_mu_over_sigma;
120 logp -= log_sigma[n];
122 logp -=
log1p(y_minus_mu_over_sigma_squared);
126 operands_and_partials.
d_x1[n] -= 2 * y_minus_mu / (sigma_squared[n] + y_minus_mu_squared);
128 operands_and_partials.
d_x2[n] += 2 * y_minus_mu / (sigma_squared[n] + y_minus_mu_squared);
130 operands_and_partials.
d_x3[n] += (y_minus_mu_squared - sigma_squared[n]) * inv_sigma[n] / (sigma_squared[n] + y_minus_mu_squared);
132 return operands_and_partials.
to_var(logp);
136 template <
bool propto,
137 typename T_y,
typename T_loc,
typename T_scale>
140 cauchy_log(
const T_y& y,
const T_loc& mu,
const T_scale& sigma) {
144 template <
typename T_y,
typename T_loc,
typename T_scale,
148 cauchy_log(
const T_y& y,
const T_loc& mu,
const T_scale& sigma,
150 return cauchy_log<false>(y,mu,sigma,Policy());
153 template <
typename T_y,
typename T_loc,
typename T_scale>
156 cauchy_log(
const T_y& y,
const T_loc& mu,
const T_scale& sigma) {
176 template <
typename T_y,
typename T_loc,
typename T_scale,
179 cauchy_cdf(
const T_y& y,
const T_loc& mu,
const T_scale& sigma,
181 static const char*
function =
"stan::prob::cauchy_cdf(%1%)";
186 using boost::math::tools::promote_args;
188 typename promote_args<T_y,T_loc,T_scale>::type lp(0.0);
189 if(!
check_not_nan(
function, y,
"Random variable", &lp, Policy()))
204 return atan2(y-mu, sigma) /
pi() + 0.5;
207 template <
typename T_y,
typename T_loc,
typename T_scale>
209 cauchy_cdf(
const T_y& y,
const T_loc& mu,
const T_scale& sigma) {
var log1p(const stan::agrad::var &a)
The log (1 + x) function for variables (C99).
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 atan2(const var &a, const var &b)
Return the principal value of the arc tangent, in radians, of the first variable divided by the secon...
boost::math::tools::promote_args< T >::type log1p(T x)
Return the natural logarithm of one plus the specified value.
double value_of(T x)
Return the value of the specified scalar argument converted to a double value.
T square(T x)
Return the square of the specified argument.
bool check_not_nan(const char *function, const T_y &y, const char *name, T_result *result, const Policy &)
Checks if the variable y is nan.
double pi()
Return the value of pi.
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 cauchy_log(const T_y &y, const T_loc &mu, const T_scale &sigma, const Policy &)
The log of the Cauchy density for the specified scalar(s) given the specified location parameter(s) a...
return_type< T_y, T_loc, T_scale >::type cauchy_cdf(const T_y &y, const T_loc &mu, const T_scale &sigma, const Policy &)
Calculates the cauchy cumulative distribution function for the given variate, location,...
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