Stan  1.0
probability, sampling & optimization
cauchy.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CAUCHY_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CAUCHY_HPP__
3 
4 #include <stan/agrad.hpp>
5 #include <stan/prob/traits.hpp>
9 
10 namespace stan {
11 
12  namespace prob {
13 
32  template <bool propto,
33  typename T_y, typename T_loc, typename T_scale,
34  class Policy>
36  cauchy_log(const T_y& y, const T_loc& mu, const T_scale& sigma,
37  const Policy&) {
38  static const char* function = "stan::prob::cauchy_log(%1%)";
39 
46 
47  // check if any vectors are zero length
48  if (!(stan::length(y)
49  && stan::length(mu)
50  && stan::length(sigma)))
51  return 0.0;
52 
53  // set up return value accumulator
54  double logp(0.0);
55 
56  // validate args (here done over var, which should be OK)
57  if (!check_not_nan(function, y, "Random variable", &logp, Policy()))
58  return logp;
59  if (!check_finite(function, mu, "Location parameter",
60  &logp, Policy()))
61  return logp;
62  if (!check_positive(function, sigma, "Scale parameter",
63  &logp, Policy()))
64  return logp;
65  if (!check_finite(function, sigma, "Scale parameter",
66  &logp, Policy()))
67  return logp;
68  if (!(check_consistent_sizes(function,
69  y,mu,sigma,
70  "Random variable","Location parameter","Scale parameter",
71  &logp, Policy())))
72  return logp;
73 
74  // check if no variables are involved and prop-to
76  return 0.0;
77 
78  using stan::math::log1p;
79  using stan::math::square;
80 
81  // set up template expressions wrapping scalars into vector views
82  VectorView<const T_y> y_vec(y);
83  VectorView<const T_loc> mu_vec(mu);
84  VectorView<const T_scale> sigma_vec(sigma);
85  size_t N = max_size(y, mu, sigma);
86 
87  DoubleVectorView<true, T_scale> inv_sigma(length(sigma));
88  DoubleVectorView<true, T_scale> sigma_squared(length(sigma));
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);
96  }
97  }
98 
99  agrad::OperandsAndPartials<T_y, T_loc, T_scale> operands_and_partials(y, mu, sigma);
100 
101  for (size_t n = 0; n < N; n++) {
102  // pull out values of arguments
103  const double y_dbl = value_of(y_vec[n]);
104  const double mu_dbl = value_of(mu_vec[n]);
105 
106  // reusable subexpression values
107  const double y_minus_mu
108  = y_dbl - mu_dbl;
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;
115 
116  // log probability
118  logp += NEG_LOG_PI;
120  logp -= log_sigma[n];
122  logp -= log1p(y_minus_mu_over_sigma_squared);
123 
124  // gradients
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);
131  }
132  return operands_and_partials.to_var(logp);
133  }
134 
135 
136  template <bool propto,
137  typename T_y, typename T_loc, typename T_scale>
138  inline
140  cauchy_log(const T_y& y, const T_loc& mu, const T_scale& sigma) {
141  return cauchy_log<propto>(y,mu,sigma,stan::math::default_policy());
142  }
143 
144  template <typename T_y, typename T_loc, typename T_scale,
145  class Policy>
146  inline
148  cauchy_log(const T_y& y, const T_loc& mu, const T_scale& sigma,
149  const Policy&) {
150  return cauchy_log<false>(y,mu,sigma,Policy());
151  }
152 
153  template <typename T_y, typename T_loc, typename T_scale>
154  inline
156  cauchy_log(const T_y& y, const T_loc& mu, const T_scale& sigma) {
157  return cauchy_log<false>(y,mu,sigma,stan::math::default_policy());
158  }
159 
160 
176  template <typename T_y, typename T_loc, typename T_scale,
177  class Policy>
179  cauchy_cdf(const T_y& y, const T_loc& mu, const T_scale& sigma,
180  const Policy&) {
181  static const char* function = "stan::prob::cauchy_cdf(%1%)";
182 
186  using boost::math::tools::promote_args;
187 
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()))
190  return lp;
191  if(!check_finite(function, mu, "Location parameter",
192  &lp, Policy()))
193  return lp;
194  if(!check_finite(function, sigma, "Scale parameter",
195  &lp, Policy()))
196  return lp;
197  if(!check_positive(function, sigma, "Scale parameter",
198  &lp, Policy()))
199  return lp;
200 
201  using std::atan2;
202  using stan::math::pi;
203 
204  return atan2(y-mu, sigma) / pi() + 0.5;
205  }
206 
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) {
210  return cauchy_cdf(y, mu, sigma, stan::math::default_policy());
211  }
212  }
213 }
214 #endif
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).
Definition: agrad.hpp:1730
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...
Definition: agrad.hpp:1924
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...
Definition: cauchy.hpp:36
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,...
Definition: cauchy.hpp:179
Probability, optimization and sampling library.
Definition: agrad.cpp:6
size_t length(const T &x)
Definition: traits.hpp:111
size_t max_size(const T1 &x1, const T2 &x2)
Definition: traits.hpp:148
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.
Definition: traits.hpp:39
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...
Definition: traits.hpp:33
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
Definition: traits.hpp:368

     [ Stan Home Page ] © 2011–2012, Stan Development Team.