logistic.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. template <class RealType, class Policy>
  46. inline const std::pair<RealType, RealType> range(const logistic_distribution<RealType, Policy>& /* dist */)
  47. { // Range of permissible values for random variable x.
  48. using boost::math::tools::max_value;
  49. return std::pair<RealType, RealType>(
  50. std::numeric_limits<RealType>::has_infinity ? -std::numeric_limits<RealType>::infinity() : -max_value<RealType>(),
  51. std::numeric_limits<RealType>::has_infinity ? std::numeric_limits<RealType>::infinity() : max_value<RealType>());
  52. }
  53. template <class RealType, class Policy>
  54. inline const std::pair<RealType, RealType> support(const logistic_distribution<RealType, Policy>& /* dist */)
  55. { // Range of supported values for random variable x.
  56. // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
  57. using boost::math::tools::max_value;
  58. return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + infinity
  59. }
  60. template <class RealType, class Policy>
  61. inline RealType pdf(const logistic_distribution<RealType, Policy>& dist, const RealType& x)
  62. {
  63. static const char* function = "boost::math::pdf(const logistic_distribution<%1%>&, %1%)";
  64. RealType scale = dist.scale();
  65. RealType location = dist.location();
  66. RealType result = 0;
  67. if(false == detail::check_scale(function, scale , &result, Policy()))
  68. {
  69. return result;
  70. }
  71. if(false == detail::check_location(function, location, &result, Policy()))
  72. {
  73. return result;
  74. }
  75. if((boost::math::isinf)(x))
  76. {
  77. return 0; // pdf + and - infinity is zero.
  78. }
  79. if(false == detail::check_x(function, x, &result, Policy()))
  80. {
  81. return result;
  82. }
  83. BOOST_MATH_STD_USING
  84. RealType exp_term = (location - x) / scale;
  85. if(fabs(exp_term) > tools::log_max_value<RealType>())
  86. return 0;
  87. exp_term = exp(exp_term);
  88. if((exp_term * scale > 1) && (exp_term > tools::max_value<RealType>() / (scale * exp_term)))
  89. return 1 / (scale * exp_term);
  90. return (exp_term) / (scale * (1 + exp_term) * (1 + exp_term));
  91. }
  92. template <class RealType, class Policy>
  93. inline RealType cdf(const logistic_distribution<RealType, Policy>& dist, const RealType& x)
  94. {
  95. RealType scale = dist.scale();
  96. RealType location = dist.location();
  97. RealType result = 0; // of checks.
  98. static const char* function = "boost::math::cdf(const logistic_distribution<%1%>&, %1%)";
  99. if(false == detail::check_scale(function, scale, &result, Policy()))
  100. {
  101. return result;
  102. }
  103. if(false == detail::check_location(function, location, &result, Policy()))
  104. {
  105. return result;
  106. }
  107. if((boost::math::isinf)(x))
  108. {
  109. if(x < 0) return 0; // -infinity
  110. return 1; // + infinity
  111. }
  112. if(false == detail::check_x(function, x, &result, Policy()))
  113. {
  114. return result;
  115. }
  116. BOOST_MATH_STD_USING
  117. RealType power = (location - x) / scale;
  118. if(power > tools::log_max_value<RealType>())
  119. return 0;
  120. if(power < -tools::log_max_value<RealType>())
  121. return 1;
  122. return 1 / (1 + exp(power));
  123. }
  124. template <class RealType, class Policy>
  125. inline RealType quantile(const logistic_distribution<RealType, Policy>& dist, const RealType& p)
  126. {
  127. BOOST_MATH_STD_USING
  128. RealType location = dist.location();
  129. RealType scale = dist.scale();
  130. static const char* function = "boost::math::quantile(const logistic_distribution<%1%>&, %1%)";
  131. RealType result = 0;
  132. if(false == detail::check_scale(function, scale, &result, Policy()))
  133. return result;
  134. if(false == detail::check_location(function, location, &result, Policy()))
  135. return result;
  136. if(false == detail::check_probability(function, p, &result, Policy()))
  137. return result;
  138. if(p == 0)
  139. {
  140. return -policies::raise_overflow_error<RealType>(function,"probability argument is 0, must be >0 and <1",Policy());
  141. }
  142. if(p == 1)
  143. {
  144. return policies::raise_overflow_error<RealType>(function,"probability argument is 1, must be >0 and <1",Policy());
  145. }
  146. //Expressions to try
  147. //return location+scale*log(p/(1-p));
  148. //return location+scale*log1p((2*p-1)/(1-p));
  149. //return location - scale*log( (1-p)/p);
  150. //return location - scale*log1p((1-2*p)/p);
  151. //return -scale*log(1/p-1) + location;
  152. return location - scale * log((1 - p) / p);
  153. } // RealType quantile(const logistic_distribution<RealType, Policy>& dist, const RealType& p)
  154. template <class RealType, class Policy>
  155. inline RealType cdf(const complemented2_type<logistic_distribution<RealType, Policy>, RealType>& c)
  156. {
  157. BOOST_MATH_STD_USING
  158. RealType location = c.dist.location();
  159. RealType scale = c.dist.scale();
  160. RealType x = c.param;
  161. static const char* function = "boost::math::cdf(const complement(logistic_distribution<%1%>&), %1%)";
  162. RealType result = 0;
  163. if(false == detail::check_scale(function, scale, &result, Policy()))
  164. {
  165. return result;
  166. }
  167. if(false == detail::check_location(function, location, &result, Policy()))
  168. {
  169. return result;
  170. }
  171. if((boost::math::isinf)(x))
  172. {
  173. if(x < 0) return 1; // cdf complement -infinity is unity.
  174. return 0; // cdf complement +infinity is zero.
  175. }
  176. if(false == detail::check_x(function, x, &result, Policy()))
  177. {
  178. return result;
  179. }
  180. RealType power = (x - location) / scale;
  181. if(power > tools::log_max_value<RealType>())
  182. return 0;
  183. if(power < -tools::log_max_value<RealType>())
  184. return 1;
  185. return 1 / (1 + exp(power));
  186. }
  187. template <class RealType, class Policy>
  188. inline RealType quantile(const complemented2_type<logistic_distribution<RealType, Policy>, RealType>& c)
  189. {
  190. BOOST_MATH_STD_USING
  191. RealType scale = c.dist.scale();
  192. RealType location = c.dist.location();
  193. static const char* function = "boost::math::quantile(const complement(logistic_distribution<%1%>&), %1%)";
  194. RealType result = 0;
  195. if(false == detail::check_scale(function, scale, &result, Policy()))
  196. return result;
  197. if(false == detail::check_location(function, location, &result, Policy()))
  198. return result;
  199. RealType q = c.param;
  200. if(false == detail::check_probability(function, q, &result, Policy()))
  201. return result;
  202. using boost::math::tools::max_value;
  203. if(q == 1)
  204. {
  205. return -policies::raise_overflow_error<RealType>(function,"probability argument is 1, but must be >0 and <1",Policy());
  206. }
  207. if(q == 0)
  208. {
  209. return policies::raise_overflow_error<RealType>(function,"probability argument is 0, but must be >0 and <1",Policy());
  210. }
  211. //Expressions to try
  212. //return location+scale*log((1-q)/q);
  213. return location + scale * log((1 - q) / q);
  214. //return location-scale*log(q/(1-q));
  215. //return location-scale*log1p((2*q-1)/(1-q));
  216. //return location+scale*log(1/q-1);
  217. //return location+scale*log1p(1/q-2);
  218. }
  219. template <class RealType, class Policy>
  220. inline RealType mean(const logistic_distribution<RealType, Policy>& dist)
  221. {
  222. return dist.location();
  223. } // RealType mean(const logistic_distribution<RealType, Policy>& dist)
  224. template <class RealType, class Policy>
  225. inline RealType variance(const logistic_distribution<RealType, Policy>& dist)
  226. {
  227. BOOST_MATH_STD_USING
  228. RealType scale = dist.scale();
  229. return boost::math::constants::pi<RealType>()*boost::math::constants::pi<RealType>()*scale*scale/3;
  230. } // RealType variance(const logistic_distribution<RealType, Policy>& dist)
  231. template <class RealType, class Policy>
  232. inline RealType mode(const logistic_distribution<RealType, Policy>& dist)
  233. {
  234. return dist.location();
  235. }
  236. template <class RealType, class Policy>
  237. inline RealType median(const logistic_distribution<RealType, Policy>& dist)
  238. {
  239. return dist.location();
  240. }
  241. template <class RealType, class Policy>
  242. inline RealType skewness(const logistic_distribution<RealType, Policy>& /*dist*/)
  243. {
  244. return 0;
  245. } // RealType skewness(const logistic_distribution<RealType, Policy>& dist)
  246. template <class RealType, class Policy>
  247. inline RealType kurtosis_excess(const logistic_distribution<RealType, Policy>& /*dist*/)
  248. {
  249. return static_cast<RealType>(6)/5;
  250. } // RealType kurtosis_excess(const logistic_distribution<RealType, Policy>& dist)
  251. template <class RealType, class Policy>
  252. inline RealType kurtosis(const logistic_distribution<RealType, Policy>& dist)
  253. {
  254. return kurtosis_excess(dist) + 3;
  255. } // RealType kurtosis_excess(const logistic_distribution<RealType, Policy>& dist)
  256. template <class RealType, class Policy>
  257. inline RealType entropy(const logistic_distribution<RealType, Policy>& dist)
  258. {
  259. using std::log;
  260. return 2 + log(dist.scale());
  261. }
  262. }}
  263. // Must come at the end:
  264. #include <boost/math/distributions/detail/derived_accessors.hpp>
  265. #endif // BOOST_MATH_DISTRIBUTIONS_LOGISTIC