1
// Copyright John Maddock 2006.
2
// Use, modification and distribution are subject to the
3
// Boost Software License, Version 1.0. (See accompanying file
4
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
#ifndef BOOST_STATS_LOGNORMAL_HPP
7
#define BOOST_STATS_LOGNORMAL_HPP
9
// http://www.itl.nist.gov/div898/handbook/eda/section3/eda3669.htm
10
// http://mathworld.wolfram.com/LogNormalDistribution.html
11
// http://en.wikipedia.org/wiki/Lognormal_distribution
13
#include <boost/math/distributions/fwd.hpp>
14
#include <boost/math/distributions/normal.hpp>
15
#include <boost/math/special_functions/expm1.hpp>
16
#include <boost/math/distributions/detail/common_error_handling.hpp>
20
namespace boost{ namespace math
25
template <class RealType, class Policy>
26
inline bool check_lognormal_x(
29
RealType* result, const Policy& pol)
31
if((x < 0) || !(boost::math::isfinite)(x))
33
*result = policies::raise_domain_error<RealType>(
35
"Random variate is %1% but must be >= 0 !", x, pol);
44
template <class RealType = double, class Policy = policies::policy<> >
45
class lognormal_distribution
48
typedef RealType value_type;
49
typedef Policy policy_type;
51
lognormal_distribution(RealType location = 0, RealType scale = 1)
52
: m_location(location), m_scale(scale)
55
detail::check_scale("boost::math::lognormal_distribution<%1%>::lognormal_distribution", scale, &result, Policy());
58
RealType location()const
71
RealType m_location; // distribution location.
72
RealType m_scale; // distribution scale.
75
typedef lognormal_distribution<double> lognormal;
77
template <class RealType, class Policy>
78
inline const std::pair<RealType, RealType> range(const lognormal_distribution<RealType, Policy>& /*dist*/)
79
{ // Range of permissible values for random variable x is >0 to +infinity.
80
using boost::math::tools::max_value;
81
return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
84
template <class RealType, class Policy>
85
inline const std::pair<RealType, RealType> support(const lognormal_distribution<RealType, Policy>& /*dist*/)
86
{ // Range of supported values for random variable x.
87
// This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
88
using boost::math::tools::max_value;
89
return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
92
template <class RealType, class Policy>
93
RealType pdf(const lognormal_distribution<RealType, Policy>& dist, const RealType& x)
95
BOOST_MATH_STD_USING // for ADL of std functions
97
RealType mu = dist.location();
98
RealType sigma = dist.scale();
100
static const char* function = "boost::math::pdf(const lognormal_distribution<%1%>&, %1%)";
103
if(0 == detail::check_scale(function, sigma, &result, Policy()))
105
if(0 == detail::check_lognormal_x(function, x, &result, Policy()))
111
RealType exponent = log(x) - mu;
112
exponent *= -exponent;
113
exponent /= 2 * sigma * sigma;
115
result = exp(exponent);
116
result /= sigma * sqrt(2 * constants::pi<RealType>()) * x;
121
template <class RealType, class Policy>
122
inline RealType cdf(const lognormal_distribution<RealType, Policy>& dist, const RealType& x)
124
BOOST_MATH_STD_USING // for ADL of std functions
126
static const char* function = "boost::math::cdf(const lognormal_distribution<%1%>&, %1%)";
129
if(0 == detail::check_lognormal_x(function, x, &result, Policy()))
135
normal_distribution<RealType, Policy> norm(dist.location(), dist.scale());
136
return cdf(norm, log(x));
139
template <class RealType, class Policy>
140
inline RealType quantile(const lognormal_distribution<RealType, Policy>& dist, const RealType& p)
142
BOOST_MATH_STD_USING // for ADL of std functions
144
static const char* function = "boost::math::quantile(const lognormal_distribution<%1%>&, %1%)";
147
if(0 == detail::check_probability(function, p, &result, Policy()))
153
return policies::raise_overflow_error<RealType>(function, 0, Policy());
155
normal_distribution<RealType, Policy> norm(dist.location(), dist.scale());
156
return exp(quantile(norm, p));
159
template <class RealType, class Policy>
160
inline RealType cdf(const complemented2_type<lognormal_distribution<RealType, Policy>, RealType>& c)
162
BOOST_MATH_STD_USING // for ADL of std functions
164
static const char* function = "boost::math::cdf(const lognormal_distribution<%1%>&, %1%)";
167
if(0 == detail::check_lognormal_x(function, c.param, &result, Policy()))
173
normal_distribution<RealType, Policy> norm(c.dist.location(), c.dist.scale());
174
return cdf(complement(norm, log(c.param)));
177
template <class RealType, class Policy>
178
inline RealType quantile(const complemented2_type<lognormal_distribution<RealType, Policy>, RealType>& c)
180
BOOST_MATH_STD_USING // for ADL of std functions
182
static const char* function = "boost::math::quantile(const lognormal_distribution<%1%>&, %1%)";
185
if(0 == detail::check_probability(function, c.param, &result, Policy()))
191
return policies::raise_overflow_error<RealType>(function, 0, Policy());
193
normal_distribution<RealType, Policy> norm(c.dist.location(), c.dist.scale());
194
return exp(quantile(complement(norm, c.param)));
197
template <class RealType, class Policy>
198
inline RealType mean(const lognormal_distribution<RealType, Policy>& dist)
200
BOOST_MATH_STD_USING // for ADL of std functions
202
RealType mu = dist.location();
203
RealType sigma = dist.scale();
206
if(0 == detail::check_scale("boost::math::mean(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
209
return exp(mu + sigma * sigma / 2);
212
template <class RealType, class Policy>
213
inline RealType variance(const lognormal_distribution<RealType, Policy>& dist)
215
BOOST_MATH_STD_USING // for ADL of std functions
217
RealType mu = dist.location();
218
RealType sigma = dist.scale();
221
if(0 == detail::check_scale("boost::math::variance(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
224
return boost::math::expm1(sigma * sigma, Policy()) * exp(2 * mu + sigma * sigma);
227
template <class RealType, class Policy>
228
inline RealType mode(const lognormal_distribution<RealType, Policy>& dist)
230
BOOST_MATH_STD_USING // for ADL of std functions
232
RealType mu = dist.location();
233
RealType sigma = dist.scale();
236
if(0 == detail::check_scale("boost::math::mode(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
239
return exp(mu - sigma * sigma);
242
template <class RealType, class Policy>
243
inline RealType median(const lognormal_distribution<RealType, Policy>& dist)
245
BOOST_MATH_STD_USING // for ADL of std functions
246
RealType mu = dist.location();
247
return exp(mu); // e^mu
250
template <class RealType, class Policy>
251
inline RealType skewness(const lognormal_distribution<RealType, Policy>& dist)
253
BOOST_MATH_STD_USING // for ADL of std functions
255
//RealType mu = dist.location();
256
RealType sigma = dist.scale();
258
RealType ss = sigma * sigma;
259
RealType ess = exp(ss);
262
if(0 == detail::check_scale("boost::math::skewness(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
265
return (ess + 2) * sqrt(boost::math::expm1(ss, Policy()));
268
template <class RealType, class Policy>
269
inline RealType kurtosis(const lognormal_distribution<RealType, Policy>& dist)
271
BOOST_MATH_STD_USING // for ADL of std functions
273
//RealType mu = dist.location();
274
RealType sigma = dist.scale();
275
RealType ss = sigma * sigma;
278
if(0 == detail::check_scale("boost::math::kurtosis(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
281
return exp(4 * ss) + 2 * exp(3 * ss) + 3 * exp(2 * ss) - 3;
284
template <class RealType, class Policy>
285
inline RealType kurtosis_excess(const lognormal_distribution<RealType, Policy>& dist)
287
BOOST_MATH_STD_USING // for ADL of std functions
289
// RealType mu = dist.location();
290
RealType sigma = dist.scale();
291
RealType ss = sigma * sigma;
294
if(0 == detail::check_scale("boost::math::kurtosis_excess(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
297
return exp(4 * ss) + 2 * exp(3 * ss) + 3 * exp(2 * ss) - 6;
303
// This include must be at the end, *after* the accessors
304
// for this distribution have been defined, in order to
305
// keep compilers that support two-phase lookup happy.
306
#include <boost/math/distributions/detail/derived_accessors.hpp>
308
#endif // BOOST_STATS_STUDENTS_T_HPP