Stan  1.0
probability, sampling & optimization
lognormal.hpp
Go to the documentation of this file.
1 #ifndef __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__LOGNORMAL_HPP__
2 #define __STAN__PROB__DISTRIBUTIONS__UNIVARIATE__CONTINUOUS__LOGNORMAL_HPP__
3 
4 #include <stan/agrad.hpp>
7 #include <stan/meta/traits.hpp>
9 #include <stan/prob/traits.hpp>
10 
11 namespace stan {
12  namespace prob {
13 
14  // LogNormal(y|mu,sigma) [y >= 0; sigma > 0]
15  // FIXME: document
16  template <bool propto,
17  typename T_y, typename T_loc, typename T_scale,
18  class Policy>
20  lognormal_log(const T_y& y, const T_loc& mu, const T_scale& sigma,
21  const Policy&) {
22  static const char* function = "stan::prob::lognormal_log(%1%)";
23 
31 
32 
33  // check if any vectors are zero length
34  if (!(stan::length(y)
35  && stan::length(mu)
36  && stan::length(sigma)))
37  return 0.0;
38 
39  // set up return value accumulator
40  double logp(0.0);
41 
42  // validate args (here done over var, which should be OK)
43  if (!check_not_nan(function, y, "Random variable", &logp, Policy()))
44  return logp;
45  if (!check_finite(function, mu, "Location parameter",
46  &logp, Policy()))
47  return logp;
48  if (!check_finite(function, sigma, "Scale parameter",
49  &logp, Policy()))
50  return logp;
51  if (!check_positive(function, sigma, "Scale parameter",
52  &logp, Policy()))
53  return logp;
54  if (!(check_consistent_sizes(function,
55  y,mu,sigma,
56  "Random variable","Location parameter","Scale parameter",
57  &logp, Policy())))
58  return logp;
59 
60 
61  VectorView<const T_y> y_vec(y);
62  VectorView<const T_loc> mu_vec(mu);
63  VectorView<const T_scale> sigma_vec(sigma);
64  size_t N = max_size(y, mu, sigma);
65 
66  for (size_t n = 0; n < length(y); n++)
67  if (value_of(y_vec[n]) <= 0)
68  return LOG_ZERO;
69 
70  agrad::OperandsAndPartials<T_y, T_loc, T_scale> operands_and_partials(y, mu, sigma);
71 
72  using stan::math::square;
73  using std::log;
74  using stan::prob::NEG_LOG_SQRT_TWO_PI;
75 
76 
79  for (size_t n = 0; n < length(sigma); n++)
80  log_sigma[n] = log(value_of(sigma_vec[n]));
84  for (size_t n = 0; n < length(sigma); n++)
85  inv_sigma[n] = 1 / value_of(sigma_vec[n]);
87  for (size_t n = 0; n < length(sigma); n++)
88  inv_sigma_sq[n] = inv_sigma[n] * inv_sigma[n];
89 
92  for (size_t n = 0; n < length(y); n++)
93  log_y[n] = log(value_of(y_vec[n]));
96  for (size_t n = 0; n < length(y); n++)
97  inv_y[n] = 1 / value_of(y_vec[n]);
98 
100  logp += N * NEG_LOG_SQRT_TWO_PI;
101 
102  for (size_t n = 0; n < N; n++) {
103  const double mu_dbl = value_of(mu_vec[n]);
104 
105  double logy_m_mu(0);
108  logy_m_mu = log_y[n] - mu_dbl;
109 
110  double logy_m_mu_sq = logy_m_mu * logy_m_mu;
111  double logy_m_mu_div_sigma(0);
115  logy_m_mu_div_sigma = logy_m_mu * inv_sigma_sq[n];
116 
117 
118  // log probability
120  logp -= log_sigma[n];
122  logp -= log_y[n];
124  logp -= 0.5 * logy_m_mu_sq * inv_sigma_sq[n];
125 
126  // gradients
128  operands_and_partials.d_x1[n] -= (1 + logy_m_mu_div_sigma) * inv_y[n];
130  operands_and_partials.d_x2[n] += logy_m_mu_div_sigma;
132  operands_and_partials.d_x3[n] += (logy_m_mu_div_sigma * logy_m_mu - 1) * inv_sigma[n];
133  }
134  return operands_and_partials.to_var(logp);
135  }
136 
137  template <bool propto,
138  typename T_y, typename T_loc, typename T_scale>
139  inline
141  lognormal_log(const T_y& y, const T_loc& mu, const T_scale& sigma) {
142  return lognormal_log<propto>(y,mu,sigma,stan::math::default_policy());
143  }
144 
145  template <typename T_y, typename T_loc, typename T_scale,
146  class Policy>
147  inline
149  lognormal_log(const T_y& y, const T_loc& mu, const T_scale& sigma,
150  const Policy&) {
151  return lognormal_log<false>(y,mu,sigma,Policy());
152  }
153 
154  template <typename T_y, typename T_loc, typename T_scale>
155  inline
157  lognormal_log(const T_y& y, const T_loc& mu, const T_scale& sigma) {
158  return lognormal_log<false>(y,mu,sigma,stan::math::default_policy());
159  }
160 
161 
162 
163 
164  template <typename T_y, typename T_loc, typename T_scale,
165  class Policy>
166  typename boost::math::tools::promote_args<T_y,T_loc,T_scale>::type
167  lognormal_cdf(const T_y& y, const T_loc& mu, const T_scale& sigma,
168  const Policy&) {
169  static const char* function = "stan::prob::lognormal_cdf(%1%)";
170 
174  using boost::math::tools::promote_args;
175 
176  typename promote_args<T_y,T_loc,T_scale>::type lp;
177  if (!check_not_nan(function, y, "Random variable", &lp, Policy()))
178  return lp;
179  if (!check_finite(function, mu, "Location parameter", &lp, Policy()))
180  return lp;
181  if (!check_finite(function, sigma, "Scale parameter",
182  &lp, Policy()))
183  return lp;
184  if (!check_positive(function, sigma, "Scale parameter",
185  &lp, Policy()))
186  return lp;
187 
188  return 0.5 * erfc(-(log(y) - mu)/(sigma * SQRT_2));
189  }
190 
191  template <typename T_y, typename T_loc, typename T_scale>
192  inline
193  typename boost::math::tools::promote_args<T_y,T_loc,T_scale>::type
194  lognormal_cdf(const T_y& y, const T_loc& mu, const T_scale& sigma) {
195  return lognormal_cdf(y,mu,sigma,stan::math::default_policy());
196  }
197 
198  }
199 }
200 #endif
var erfc(const stan::agrad::var &a)
The complementary error 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
const double SQRT_2
The value of the square root of 2, .
Definition: constants.hpp:20
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.
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.
boost::math::tools::promote_args< T_y, T_loc, T_scale >::type lognormal_cdf(const T_y &y, const T_loc &mu, const T_scale &sigma, const Policy &)
Definition: lognormal.hpp:167
return_type< T_y, T_loc, T_scale >::type lognormal_log(const T_y &y, const T_loc &mu, const T_scale &sigma, const Policy &)
Definition: lognormal.hpp:20
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.