lognormal.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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)
  5. #ifndef BOOST_STATS_LOGNORMAL_HPP
  6. #define BOOST_STATS_LOGNORMAL_HPP
  7. // http://www.itl.nist.gov/div898/handbook/eda/section3/eda3669.htm
  8. // http://mathworld.wolfram.com/LogNormalDistribution.html
  9. // http://en.wikipedia.org/wiki/Lognormal_distribution
  10. #include <boost/math/distributions/fwd.hpp>
  11. #include <boost/math/distributions/normal.hpp>
  12. #include <boost/math/special_functions/expm1.hpp>
  13. #include <boost/math/distributions/detail/common_error_handling.hpp>
  14. #include <utility>
  15. namespace boost{ namespace math
  16. {
  17. namespace detail
  18. {
  19. template <class RealType, class Policy>
  20. inline bool check_lognormal_x(
  21. const char* function,
  22. RealType const& x,
  23. RealType* result, const Policy& pol)
  24. {
  25. if((x < 0) || !(boost::math::isfinite)(x))
  26. {
  27. *result = policies::raise_domain_error<RealType>(
  28. function,
  29. "Random variate is %1% but must be >= 0 !", x, pol);
  30. return false;
  31. }
  32. return true;
  33. }
  34. } // namespace detail
  35. template <class RealType = double, class Policy = policies::policy<> >
  36. class lognormal_distribution
  37. {
  38. public:
  39. typedef RealType value_type;
  40. typedef Policy policy_type;
  41. lognormal_distribution(RealType l_location = 0, RealType l_scale = 1)
  42. : m_location(l_location), m_scale(l_scale)
  43. {
  44. RealType result;
  45. detail::check_scale("boost::math::lognormal_distribution<%1%>::lognormal_distribution", l_scale, &result, Policy());
  46. detail::check_location("boost::math::lognormal_distribution<%1%>::lognormal_distribution", l_location, &result, Policy());
  47. }
  48. RealType location()const
  49. {
  50. return m_location;
  51. }
  52. RealType scale()const
  53. {
  54. return m_scale;
  55. }
  56. private:
  57. //
  58. // Data members:
  59. //
  60. RealType m_location; // distribution location.
  61. RealType m_scale; // distribution scale.
  62. };
  63. typedef lognormal_distribution<double> lognormal;
  64. #ifdef __cpp_deduction_guides
  65. template <class RealType>
  66. lognormal_distribution(RealType)->lognormal_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  67. template <class RealType>
  68. lognormal_distribution(RealType,RealType)->lognormal_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  69. #endif
  70. template <class RealType, class Policy>
  71. inline const std::pair<RealType, RealType> range(const lognormal_distribution<RealType, Policy>& /*dist*/)
  72. { // Range of permissible values for random variable x is >0 to +infinity.
  73. using boost::math::tools::max_value;
  74. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
  75. }
  76. template <class RealType, class Policy>
  77. inline const std::pair<RealType, RealType> support(const lognormal_distribution<RealType, Policy>& /*dist*/)
  78. { // Range of supported values for random variable x.
  79. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  80. using boost::math::tools::max_value;
  81. return std::pair<RealType, RealType>(static_cast<RealType>(0), max_value<RealType>());
  82. }
  83. template <class RealType, class Policy>
  84. RealType pdf(const lognormal_distribution<RealType, Policy>& dist, const RealType& x)
  85. {
  86. BOOST_MATH_STD_USING // for ADL of std functions
  87. RealType mu = dist.location();
  88. RealType sigma = dist.scale();
  89. static const char* function = "boost::math::pdf(const lognormal_distribution<%1%>&, %1%)";
  90. RealType result = 0;
  91. if(0 == detail::check_scale(function, sigma, &result, Policy()))
  92. return result;
  93. if(0 == detail::check_location(function, mu, &result, Policy()))
  94. return result;
  95. if(0 == detail::check_lognormal_x(function, x, &result, Policy()))
  96. return result;
  97. if(x == 0)
  98. return 0;
  99. RealType exponent = log(x) - mu;
  100. exponent *= -exponent;
  101. exponent /= 2 * sigma * sigma;
  102. result = exp(exponent);
  103. result /= sigma * sqrt(2 * constants::pi<RealType>()) * x;
  104. return result;
  105. }
  106. template <class RealType, class Policy>
  107. inline RealType cdf(const lognormal_distribution<RealType, Policy>& dist, const RealType& x)
  108. {
  109. BOOST_MATH_STD_USING // for ADL of std functions
  110. static const char* function = "boost::math::cdf(const lognormal_distribution<%1%>&, %1%)";
  111. RealType result = 0;
  112. if(0 == detail::check_scale(function, dist.scale(), &result, Policy()))
  113. return result;
  114. if(0 == detail::check_location(function, dist.location(), &result, Policy()))
  115. return result;
  116. if(0 == detail::check_lognormal_x(function, x, &result, Policy()))
  117. return result;
  118. if(x == 0)
  119. return 0;
  120. normal_distribution<RealType, Policy> norm(dist.location(), dist.scale());
  121. return cdf(norm, log(x));
  122. }
  123. template <class RealType, class Policy>
  124. inline RealType quantile(const lognormal_distribution<RealType, Policy>& dist, const RealType& p)
  125. {
  126. BOOST_MATH_STD_USING // for ADL of std functions
  127. static const char* function = "boost::math::quantile(const lognormal_distribution<%1%>&, %1%)";
  128. RealType result = 0;
  129. if(0 == detail::check_scale(function, dist.scale(), &result, Policy()))
  130. return result;
  131. if(0 == detail::check_location(function, dist.location(), &result, Policy()))
  132. return result;
  133. if(0 == detail::check_probability(function, p, &result, Policy()))
  134. return result;
  135. if(p == 0)
  136. return 0;
  137. if(p == 1)
  138. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  139. normal_distribution<RealType, Policy> norm(dist.location(), dist.scale());
  140. return exp(quantile(norm, p));
  141. }
  142. template <class RealType, class Policy>
  143. inline RealType cdf(const complemented2_type<lognormal_distribution<RealType, Policy>, RealType>& c)
  144. {
  145. BOOST_MATH_STD_USING // for ADL of std functions
  146. static const char* function = "boost::math::cdf(const lognormal_distribution<%1%>&, %1%)";
  147. RealType result = 0;
  148. if(0 == detail::check_scale(function, c.dist.scale(), &result, Policy()))
  149. return result;
  150. if(0 == detail::check_location(function, c.dist.location(), &result, Policy()))
  151. return result;
  152. if(0 == detail::check_lognormal_x(function, c.param, &result, Policy()))
  153. return result;
  154. if(c.param == 0)
  155. return 1;
  156. normal_distribution<RealType, Policy> norm(c.dist.location(), c.dist.scale());
  157. return cdf(complement(norm, log(c.param)));
  158. }
  159. template <class RealType, class Policy>
  160. inline RealType quantile(const complemented2_type<lognormal_distribution<RealType, Policy>, RealType>& c)
  161. {
  162. BOOST_MATH_STD_USING // for ADL of std functions
  163. static const char* function = "boost::math::quantile(const lognormal_distribution<%1%>&, %1%)";
  164. RealType result = 0;
  165. if(0 == detail::check_scale(function, c.dist.scale(), &result, Policy()))
  166. return result;
  167. if(0 == detail::check_location(function, c.dist.location(), &result, Policy()))
  168. return result;
  169. if(0 == detail::check_probability(function, c.param, &result, Policy()))
  170. return result;
  171. if(c.param == 1)
  172. return 0;
  173. if(c.param == 0)
  174. return policies::raise_overflow_error<RealType>(function, 0, Policy());
  175. normal_distribution<RealType, Policy> norm(c.dist.location(), c.dist.scale());
  176. return exp(quantile(complement(norm, c.param)));
  177. }
  178. template <class RealType, class Policy>
  179. inline RealType mean(const lognormal_distribution<RealType, Policy>& dist)
  180. {
  181. BOOST_MATH_STD_USING // for ADL of std functions
  182. RealType mu = dist.location();
  183. RealType sigma = dist.scale();
  184. RealType result = 0;
  185. if(0 == detail::check_scale("boost::math::mean(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
  186. return result;
  187. if(0 == detail::check_location("boost::math::mean(const lognormal_distribution<%1%>&)", mu, &result, Policy()))
  188. return result;
  189. return exp(mu + sigma * sigma / 2);
  190. }
  191. template <class RealType, class Policy>
  192. inline RealType variance(const lognormal_distribution<RealType, Policy>& dist)
  193. {
  194. BOOST_MATH_STD_USING // for ADL of std functions
  195. RealType mu = dist.location();
  196. RealType sigma = dist.scale();
  197. RealType result = 0;
  198. if(0 == detail::check_scale("boost::math::variance(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
  199. return result;
  200. if(0 == detail::check_location("boost::math::variance(const lognormal_distribution<%1%>&)", mu, &result, Policy()))
  201. return result;
  202. return boost::math::expm1(sigma * sigma, Policy()) * exp(2 * mu + sigma * sigma);
  203. }
  204. template <class RealType, class Policy>
  205. inline RealType mode(const lognormal_distribution<RealType, Policy>& dist)
  206. {
  207. BOOST_MATH_STD_USING // for ADL of std functions
  208. RealType mu = dist.location();
  209. RealType sigma = dist.scale();
  210. RealType result = 0;
  211. if(0 == detail::check_scale("boost::math::mode(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
  212. return result;
  213. if(0 == detail::check_location("boost::math::mode(const lognormal_distribution<%1%>&)", mu, &result, Policy()))
  214. return result;
  215. return exp(mu - sigma * sigma);
  216. }
  217. template <class RealType, class Policy>
  218. inline RealType median(const lognormal_distribution<RealType, Policy>& dist)
  219. {
  220. BOOST_MATH_STD_USING // for ADL of std functions
  221. RealType mu = dist.location();
  222. return exp(mu); // e^mu
  223. }
  224. template <class RealType, class Policy>
  225. inline RealType skewness(const lognormal_distribution<RealType, Policy>& dist)
  226. {
  227. BOOST_MATH_STD_USING // for ADL of std functions
  228. //RealType mu = dist.location();
  229. RealType sigma = dist.scale();
  230. RealType ss = sigma * sigma;
  231. RealType ess = exp(ss);
  232. RealType result = 0;
  233. if(0 == detail::check_scale("boost::math::skewness(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
  234. return result;
  235. if(0 == detail::check_location("boost::math::skewness(const lognormal_distribution<%1%>&)", dist.location(), &result, Policy()))
  236. return result;
  237. return (ess + 2) * sqrt(boost::math::expm1(ss, Policy()));
  238. }
  239. template <class RealType, class Policy>
  240. inline RealType kurtosis(const lognormal_distribution<RealType, Policy>& dist)
  241. {
  242. BOOST_MATH_STD_USING // for ADL of std functions
  243. //RealType mu = dist.location();
  244. RealType sigma = dist.scale();
  245. RealType ss = sigma * sigma;
  246. RealType result = 0;
  247. if(0 == detail::check_scale("boost::math::kurtosis(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
  248. return result;
  249. if(0 == detail::check_location("boost::math::kurtosis(const lognormal_distribution<%1%>&)", dist.location(), &result, Policy()))
  250. return result;
  251. return exp(4 * ss) + 2 * exp(3 * ss) + 3 * exp(2 * ss) - 3;
  252. }
  253. template <class RealType, class Policy>
  254. inline RealType kurtosis_excess(const lognormal_distribution<RealType, Policy>& dist)
  255. {
  256. BOOST_MATH_STD_USING // for ADL of std functions
  257. // RealType mu = dist.location();
  258. RealType sigma = dist.scale();
  259. RealType ss = sigma * sigma;
  260. RealType result = 0;
  261. if(0 == detail::check_scale("boost::math::kurtosis_excess(const lognormal_distribution<%1%>&)", sigma, &result, Policy()))
  262. return result;
  263. if(0 == detail::check_location("boost::math::kurtosis_excess(const lognormal_distribution<%1%>&)", dist.location(), &result, Policy()))
  264. return result;
  265. return exp(4 * ss) + 2 * exp(3 * ss) + 3 * exp(2 * ss) - 6;
  266. }
  267. template <class RealType, class Policy>
  268. inline RealType entropy(const lognormal_distribution<RealType, Policy>& dist)
  269. {
  270. using std::log;
  271. RealType mu = dist.location();
  272. RealType sigma = dist.scale();
  273. return mu + log(constants::two_pi<RealType>()*constants::e<RealType>()*sigma*sigma)/2;
  274. }
  275. } // namespace math
  276. } // namespace boost
  277. // This include must be at the end, *after* the accessors
  278. // for this distribution have been defined, in order to
  279. // keep compilers that support two-phase lookup happy.
  280. #include <boost/math/distributions/detail/derived_accessors.hpp>
  281. #endif // BOOST_STATS_STUDENTS_T_HPP