lognormal.hpp 12 KB

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