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 l_location = 0, RealType l_scale = 1)
52 : m_location(l_location), m_scale(l_scale)
55 detail::check_scale("boost::math::lognormal_distribution<%1%>::lognormal_distribution", l_scale, &result, Policy());
56 detail::check_location("boost::math::lognormal_distribution<%1%>::lognormal_distribution", l_location, &result, Policy());
59 RealType location()const
72 RealType m_location; // distribution location.
73 RealType m_scale; // distribution scale.
76 typedef lognormal_distribution<double> lognormal;
78 template <class RealType, class Policy>
79 inline const std::pair<RealType, RealType> range(const lognormal_distribution<RealType, Policy>& /*dist*/)
80 { // Range of permissible values for random variable x is >0 to +infinity.
81 using boost::math::tools::max_value;
82 return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
85 template <class RealType, class Policy>
86 inline const std::pair<RealType, RealType> support(const lognormal_distribution<RealType, Policy>& /*dist*/)
87 { // Range of supported values for random variable x.
88 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
89 using boost::math::tools::max_value;
90 return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
93 template <class RealType, class Policy>
94 RealType pdf(const lognormal_distribution<RealType, Policy>& dist, const RealType& x)
96 BOOST_MATH_STD_USING // for ADL of std functions
98 RealType mu = dist.location();
99 RealType sigma = dist.scale();
101 static const char* function = "boost::math::pdf(const lognormal_distribution<%1%>&, %1%)";
104 if(0 == detail::check_scale(function, sigma, &result, Policy()))
106 if(0 == detail::check_location(function, mu, &result, Policy()))
108 if(0 == detail::check_lognormal_x(function, x, &result, Policy()))
114 RealType exponent = log(x) - mu;
115 exponent *= -exponent;
116 exponent /= 2 * sigma * sigma;
118 result = exp(exponent);
119 result /= sigma * sqrt(2 * constants::pi<RealType>()) * x;
124 template <class RealType, class Policy>
125 inline RealType cdf(const lognormal_distribution<RealType, Policy>& dist, const RealType& x)
127 BOOST_MATH_STD_USING // for ADL of std functions
129 static const char* function = "boost::math::cdf(const lognormal_distribution<%1%>&, %1%)";
132 if(0 == detail::check_scale(function, dist.scale(), &result, Policy()))
134 if(0 == detail::check_location(function, dist.location(), &result, Policy()))
136 if(0 == detail::check_lognormal_x(function, x, &result, Policy()))
142 normal_distribution<RealType, Policy> norm(dist.location(), dist.scale());
143 return cdf(norm, log(x));
146 template <class RealType, class Policy>
147 inline RealType quantile(const lognormal_distribution<RealType, Policy>& dist, const RealType& p)
149 BOOST_MATH_STD_USING // for ADL of std functions
151 static const char* function = "boost::math::quantile(const lognormal_distribution<%1%>&, %1%)";
154 if(0 == detail::check_scale(function, dist.scale(), &result, Policy()))
156 if(0 == detail::check_location(function, dist.location(), &result, Policy()))
158 if(0 == detail::check_probability(function, p, &result, Policy()))
164 return policies::raise_overflow_error<RealType>(function, 0, Policy());
166 normal_distribution<RealType, Policy> norm(dist.location(), dist.scale());
167 return exp(quantile(norm, p));
170 template <class RealType, class Policy>
171 inline RealType cdf(const complemented2_type<lognormal_distribution<RealType, Policy>, RealType>& c)
173 BOOST_MATH_STD_USING // for ADL of std functions
175 static const char* function = "boost::math::cdf(const lognormal_distribution<%1%>&, %1%)";
178 if(0 == detail::check_scale(function, c.dist.scale(), &result, Policy()))
180 if(0 == detail::check_location(function, c.dist.location(), &result, Policy()))
182 if(0 == detail::check_lognormal_x(function, c.param, &result, Policy()))
188 normal_distribution<RealType, Policy> norm(c.dist.location(), c.dist.scale());
189 return cdf(complement(norm, log(c.param)));
192 template <class RealType, class Policy>
193 inline RealType quantile(const complemented2_type<lognormal_distribution<RealType, Policy>, RealType>& c)
195 BOOST_MATH_STD_USING // for ADL of std functions
197 static const char* function = "boost::math::quantile(const lognormal_distribution<%1%>&, %1%)";
200 if(0 == detail::check_scale(function, c.dist.scale(), &result, Policy()))
202 if(0 == detail::check_location(function, c.dist.location(), &result, Policy()))
204 if(0 == detail::check_probability(function, c.param, &result, Policy()))
210 return policies::raise_overflow_error<RealType>(function, 0, Policy());
212 normal_distribution<RealType, Policy> norm(c.dist.location(), c.dist.scale());
213 return exp(quantile(complement(norm, c.param)));
216 template <class RealType, class Policy>
217 inline RealType mean(const lognormal_distribution<RealType, Policy>& dist)
219 BOOST_MATH_STD_USING // for ADL of std functions
221 RealType mu = dist.location();
222 RealType sigma = dist.scale();
225 if(0 == detail::check_scale("boost::math::mean(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
227 if(0 == detail::check_location("boost::math::mean(const lognormal_distribution<%1%>&)", mu, &result, Policy()))
230 return exp(mu + sigma * sigma / 2);
233 template <class RealType, class Policy>
234 inline RealType variance(const lognormal_distribution<RealType, Policy>& dist)
236 BOOST_MATH_STD_USING // for ADL of std functions
238 RealType mu = dist.location();
239 RealType sigma = dist.scale();
242 if(0 == detail::check_scale("boost::math::variance(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
244 if(0 == detail::check_location("boost::math::variance(const lognormal_distribution<%1%>&)", mu, &result, Policy()))
247 return boost::math::expm1(sigma * sigma, Policy()) * exp(2 * mu + sigma * sigma);
250 template <class RealType, class Policy>
251 inline RealType mode(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();
259 if(0 == detail::check_scale("boost::math::mode(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
261 if(0 == detail::check_location("boost::math::mode(const lognormal_distribution<%1%>&)", mu, &result, Policy()))
264 return exp(mu - sigma * sigma);
267 template <class RealType, class Policy>
268 inline RealType median(const lognormal_distribution<RealType, Policy>& dist)
270 BOOST_MATH_STD_USING // for ADL of std functions
271 RealType mu = dist.location();
272 return exp(mu); // e^mu
275 template <class RealType, class Policy>
276 inline RealType skewness(const lognormal_distribution<RealType, Policy>& dist)
278 BOOST_MATH_STD_USING // for ADL of std functions
280 //RealType mu = dist.location();
281 RealType sigma = dist.scale();
283 RealType ss = sigma * sigma;
284 RealType ess = exp(ss);
287 if(0 == detail::check_scale("boost::math::skewness(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
289 if(0 == detail::check_location("boost::math::skewness(const lognormal_distribution<%1%>&)", dist.location(), &result, Policy()))
292 return (ess + 2) * sqrt(boost::math::expm1(ss, Policy()));
295 template <class RealType, class Policy>
296 inline RealType kurtosis(const lognormal_distribution<RealType, Policy>& dist)
298 BOOST_MATH_STD_USING // for ADL of std functions
300 //RealType mu = dist.location();
301 RealType sigma = dist.scale();
302 RealType ss = sigma * sigma;
305 if(0 == detail::check_scale("boost::math::kurtosis(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
307 if(0 == detail::check_location("boost::math::kurtosis(const lognormal_distribution<%1%>&)", dist.location(), &result, Policy()))
310 return exp(4 * ss) + 2 * exp(3 * ss) + 3 * exp(2 * ss) - 3;
313 template <class RealType, class Policy>
314 inline RealType kurtosis_excess(const lognormal_distribution<RealType, Policy>& dist)
316 BOOST_MATH_STD_USING // for ADL of std functions
318 // RealType mu = dist.location();
319 RealType sigma = dist.scale();
320 RealType ss = sigma * sigma;
323 if(0 == detail::check_scale("boost::math::kurtosis_excess(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
325 if(0 == detail::check_location("boost::math::kurtosis_excess(const lognormal_distribution<%1%>&)", dist.location(), &result, Policy()))
328 return exp(4 * ss) + 2 * exp(3 * ss) + 3 * exp(2 * ss) - 6;
334 // This include must be at the end, *after* the accessors
335 // for this distribution have been defined, in order to
336 // keep compilers that support two-phase lookup happy.
337 #include <boost/math/distributions/detail/derived_accessors.hpp>
339 #endif // BOOST_STATS_STUDENTS_T_HPP