logistic.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // Copyright 2008 Gautam Sewani
  2. //
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_MATH_DISTRIBUTIONS_LOGISTIC
  8. #define BOOST_MATH_DISTRIBUTIONS_LOGISTIC
  9. #include <boost/math/distributions/fwd.hpp>
  10. #include <boost/math/distributions/detail/common_error_handling.hpp>
  11. #include <boost/math/distributions/complement.hpp>
  12. #include <boost/math/special_functions/log1p.hpp>
  13. #include <boost/math/constants/constants.hpp>
  14. #include <utility>
  15. namespace boost { namespace math {
  16. template <class RealType = double, class Policy = policies::policy<> >
  17. class logistic_distribution
  18. {
  19. public:
  20. typedef RealType value_type;
  21. typedef Policy policy_type;
  22. logistic_distribution(RealType l_location=0, RealType l_scale=1) // Constructor.
  23. : m_location(l_location), m_scale(l_scale)
  24. {
  25. static const char* function = "boost::math::logistic_distribution<%1%>::logistic_distribution";
  26. RealType result;
  27. detail::check_scale(function, l_scale, &result, Policy());
  28. detail::check_location(function, l_location, &result, Policy());
  29. }
  30. // Accessor functions.
  31. RealType scale()const
  32. {
  33. return m_scale;
  34. }
  35. RealType location()const
  36. {
  37. return m_location;
  38. }
  39. private:
  40. // Data members:
  41. RealType m_location; // distribution location aka mu.
  42. RealType m_scale; // distribution scale aka s.
  43. }; // class logistic_distribution
  44. typedef logistic_distribution<double> logistic;
  45. #ifdef __cpp_deduction_guides
  46. template <class RealType>
  47. logistic_distribution(RealType)->logistic_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  48. template <class RealType>
  49. logistic_distribution(RealType,RealType)->logistic_distribution<typename boost::math::tools::promote_args<RealType>::type>;
  50. #endif
  51. template <class RealType, class Policy>
  52. inline const std::pair<RealType, RealType> range(const logistic_distribution<RealType, Policy>& /* dist */)
  53. { // Range of permissible values for random variable x.
  54. using boost::math::tools::max_value;
  55. return std::pair<RealType, RealType>(
  56. std::numeric_limits<RealType>::has_infinity ? -std::numeric_limits<RealType>::infinity() : -max_value<RealType>(),
  57. std::numeric_limits<RealType>::has_infinity ? std::numeric_limits<RealType>::infinity() : max_value<RealType>());
  58. }
  59. template <class RealType, class Policy>
  60. inline const std::pair<RealType, RealType> support(const logistic_distribution<RealType, Policy>& /* dist */)
  61. { // Range of supported values for random variable x.
  62. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  63. using boost::math::tools::max_value;
  64. return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + infinity
  65. }
  66. template <class RealType, class Policy>
  67. inline RealType pdf(const logistic_distribution<RealType, Policy>& dist, const RealType& x)
  68. {
  69. static const char* function = "boost::math::pdf(const logistic_distribution<%1%>&, %1%)";
  70. RealType scale = dist.scale();
  71. RealType location = dist.location();
  72. RealType result = 0;
  73. if(false == detail::check_scale(function, scale , &result, Policy()))
  74. {
  75. return result;
  76. }
  77. if(false == detail::check_location(function, location, &result, Policy()))
  78. {
  79. return result;
  80. }
  81. if((boost::math::isinf)(x))
  82. {
  83. return 0; // pdf + and - infinity is zero.
  84. }
  85. if(false == detail::check_x(function, x, &result, Policy()))
  86. {
  87. return result;
  88. }
  89. BOOST_MATH_STD_USING
  90. RealType exp_term = (location - x) / scale;
  91. if(fabs(exp_term) > tools::log_max_value<RealType>())
  92. return 0;
  93. exp_term = exp(exp_term);
  94. if((exp_term * scale > 1) && (exp_term > tools::max_value<RealType>() / (scale * exp_term)))
  95. return 1 / (scale * exp_term);
  96. return (exp_term) / (scale * (1 + exp_term) * (1 + exp_term));
  97. }
  98. template <class RealType, class Policy>
  99. inline RealType cdf(const logistic_distribution<RealType, Policy>& dist, const RealType& x)
  100. {
  101. RealType scale = dist.scale();
  102. RealType location = dist.location();
  103. RealType result = 0; // of checks.
  104. static const char* function = "boost::math::cdf(const logistic_distribution<%1%>&, %1%)";
  105. if(false == detail::check_scale(function, scale, &result, Policy()))
  106. {
  107. return result;
  108. }
  109. if(false == detail::check_location(function, location, &result, Policy()))
  110. {
  111. return result;
  112. }
  113. if((boost::math::isinf)(x))
  114. {
  115. if(x < 0) return 0; // -infinity
  116. return 1; // + infinity
  117. }
  118. if(false == detail::check_x(function, x, &result, Policy()))
  119. {
  120. return result;
  121. }
  122. BOOST_MATH_STD_USING
  123. RealType power = (location - x) / scale;
  124. if(power > tools::log_max_value<RealType>())
  125. return 0;
  126. if(power < -tools::log_max_value<RealType>())
  127. return 1;
  128. return 1 / (1 + exp(power));
  129. }
  130. template <class RealType, class Policy>
  131. inline RealType logcdf(const logistic_distribution<RealType, Policy>& dist, const RealType& x)
  132. {
  133. RealType scale = dist.scale();
  134. RealType location = dist.location();
  135. RealType result = 0; // of checks.
  136. static const char* function = "boost::math::cdf(const logistic_distribution<%1%>&, %1%)";
  137. if(false == detail::check_scale(function, scale, &result, Policy()))
  138. {
  139. return result;
  140. }
  141. if(false == detail::check_location(function, location, &result, Policy()))
  142. {
  143. return result;
  144. }
  145. if((boost::math::isinf)(x))
  146. {
  147. if(x < 0)
  148. {
  149. return 0; // -infinity
  150. }
  151. return 1; // + infinity
  152. }
  153. if(false == detail::check_x(function, x, &result, Policy()))
  154. {
  155. return result;
  156. }
  157. BOOST_MATH_STD_USING
  158. RealType power = (location - x) / scale;
  159. if(power > tools::log_max_value<RealType>())
  160. {
  161. return 0;
  162. }
  163. if(power < -tools::log_max_value<RealType>())
  164. {
  165. return 1;
  166. }
  167. return -log1p(exp(power));
  168. }
  169. template <class RealType, class Policy>
  170. inline RealType quantile(const logistic_distribution<RealType, Policy>& dist, const RealType& p)
  171. {
  172. BOOST_MATH_STD_USING
  173. RealType location = dist.location();
  174. RealType scale = dist.scale();
  175. static const char* function = "boost::math::quantile(const logistic_distribution<%1%>&, %1%)";
  176. RealType result = 0;
  177. if(false == detail::check_scale(function, scale, &result, Policy()))
  178. return result;
  179. if(false == detail::check_location(function, location, &result, Policy()))
  180. return result;
  181. if(false == detail::check_probability(function, p, &result, Policy()))
  182. return result;
  183. if(p == 0)
  184. {
  185. return -policies::raise_overflow_error<RealType>(function,"probability argument is 0, must be >0 and <1",Policy());
  186. }
  187. if(p == 1)
  188. {
  189. return policies::raise_overflow_error<RealType>(function,"probability argument is 1, must be >0 and <1",Policy());
  190. }
  191. //Expressions to try
  192. //return location+scale*log(p/(1-p));
  193. //return location+scale*log1p((2*p-1)/(1-p));
  194. //return location - scale*log( (1-p)/p);
  195. //return location - scale*log1p((1-2*p)/p);
  196. //return -scale*log(1/p-1) + location;
  197. return location - scale * log((1 - p) / p);
  198. } // RealType quantile(const logistic_distribution<RealType, Policy>& dist, const RealType& p)
  199. template <class RealType, class Policy>
  200. inline RealType cdf(const complemented2_type<logistic_distribution<RealType, Policy>, RealType>& c)
  201. {
  202. BOOST_MATH_STD_USING
  203. RealType location = c.dist.location();
  204. RealType scale = c.dist.scale();
  205. RealType x = c.param;
  206. static const char* function = "boost::math::cdf(const complement(logistic_distribution<%1%>&), %1%)";
  207. RealType result = 0;
  208. if(false == detail::check_scale(function, scale, &result, Policy()))
  209. {
  210. return result;
  211. }
  212. if(false == detail::check_location(function, location, &result, Policy()))
  213. {
  214. return result;
  215. }
  216. if((boost::math::isinf)(x))
  217. {
  218. if(x < 0) return 1; // cdf complement -infinity is unity.
  219. return 0; // cdf complement +infinity is zero.
  220. }
  221. if(false == detail::check_x(function, x, &result, Policy()))
  222. {
  223. return result;
  224. }
  225. RealType power = (x - location) / scale;
  226. if(power > tools::log_max_value<RealType>())
  227. return 0;
  228. if(power < -tools::log_max_value<RealType>())
  229. return 1;
  230. return 1 / (1 + exp(power));
  231. }
  232. template <class RealType, class Policy>
  233. inline RealType logcdf(const complemented2_type<logistic_distribution<RealType, Policy>, RealType>& c)
  234. {
  235. BOOST_MATH_STD_USING
  236. RealType location = c.dist.location();
  237. RealType scale = c.dist.scale();
  238. RealType x = c.param;
  239. static const char* function = "boost::math::cdf(const complement(logistic_distribution<%1%>&), %1%)";
  240. RealType result = 0;
  241. if(false == detail::check_scale(function, scale, &result, Policy()))
  242. {
  243. return result;
  244. }
  245. if(false == detail::check_location(function, location, &result, Policy()))
  246. {
  247. return result;
  248. }
  249. if((boost::math::isinf)(x))
  250. {
  251. if(x < 0) return 1; // cdf complement -infinity is unity.
  252. return 0; // cdf complement +infinity is zero.
  253. }
  254. if(false == detail::check_x(function, x, &result, Policy()))
  255. {
  256. return result;
  257. }
  258. RealType power = (x - location) / scale;
  259. if(power > tools::log_max_value<RealType>())
  260. return 0;
  261. if(power < -tools::log_max_value<RealType>())
  262. return 1;
  263. return -log1p(exp(power));
  264. }
  265. template <class RealType, class Policy>
  266. inline RealType quantile(const complemented2_type<logistic_distribution<RealType, Policy>, RealType>& c)
  267. {
  268. BOOST_MATH_STD_USING
  269. RealType scale = c.dist.scale();
  270. RealType location = c.dist.location();
  271. static const char* function = "boost::math::quantile(const complement(logistic_distribution<%1%>&), %1%)";
  272. RealType result = 0;
  273. if(false == detail::check_scale(function, scale, &result, Policy()))
  274. return result;
  275. if(false == detail::check_location(function, location, &result, Policy()))
  276. return result;
  277. RealType q = c.param;
  278. if(false == detail::check_probability(function, q, &result, Policy()))
  279. return result;
  280. using boost::math::tools::max_value;
  281. if(q == 1)
  282. {
  283. return -policies::raise_overflow_error<RealType>(function,"probability argument is 1, but must be >0 and <1",Policy());
  284. }
  285. if(q == 0)
  286. {
  287. return policies::raise_overflow_error<RealType>(function,"probability argument is 0, but must be >0 and <1",Policy());
  288. }
  289. //Expressions to try
  290. //return location+scale*log((1-q)/q);
  291. return location + scale * log((1 - q) / q);
  292. //return location-scale*log(q/(1-q));
  293. //return location-scale*log1p((2*q-1)/(1-q));
  294. //return location+scale*log(1/q-1);
  295. //return location+scale*log1p(1/q-2);
  296. }
  297. template <class RealType, class Policy>
  298. inline RealType mean(const logistic_distribution<RealType, Policy>& dist)
  299. {
  300. return dist.location();
  301. } // RealType mean(const logistic_distribution<RealType, Policy>& dist)
  302. template <class RealType, class Policy>
  303. inline RealType variance(const logistic_distribution<RealType, Policy>& dist)
  304. {
  305. BOOST_MATH_STD_USING
  306. RealType scale = dist.scale();
  307. return boost::math::constants::pi<RealType>()*boost::math::constants::pi<RealType>()*scale*scale/3;
  308. } // RealType variance(const logistic_distribution<RealType, Policy>& dist)
  309. template <class RealType, class Policy>
  310. inline RealType mode(const logistic_distribution<RealType, Policy>& dist)
  311. {
  312. return dist.location();
  313. }
  314. template <class RealType, class Policy>
  315. inline RealType median(const logistic_distribution<RealType, Policy>& dist)
  316. {
  317. return dist.location();
  318. }
  319. template <class RealType, class Policy>
  320. inline RealType skewness(const logistic_distribution<RealType, Policy>& /*dist*/)
  321. {
  322. return 0;
  323. } // RealType skewness(const logistic_distribution<RealType, Policy>& dist)
  324. template <class RealType, class Policy>
  325. inline RealType kurtosis_excess(const logistic_distribution<RealType, Policy>& /*dist*/)
  326. {
  327. return static_cast<RealType>(6)/5;
  328. } // RealType kurtosis_excess(const logistic_distribution<RealType, Policy>& dist)
  329. template <class RealType, class Policy>
  330. inline RealType kurtosis(const logistic_distribution<RealType, Policy>& dist)
  331. {
  332. return kurtosis_excess(dist) + 3;
  333. } // RealType kurtosis_excess(const logistic_distribution<RealType, Policy>& dist)
  334. template <class RealType, class Policy>
  335. inline RealType entropy(const logistic_distribution<RealType, Policy>& dist)
  336. {
  337. using std::log;
  338. return 2 + log(dist.scale());
  339. }
  340. }}
  341. // Must come at the end:
  342. #include <boost/math/distributions/detail/derived_accessors.hpp>
  343. #endif // BOOST_MATH_DISTRIBUTIONS_LOGISTIC