1 // Copyright 2008 Gautam Sewani
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)
8 #ifndef BOOST_MATH_DISTRIBUTIONS_LOGISTIC
9 #define BOOST_MATH_DISTRIBUTIONS_LOGISTIC
11 #include <boost/math/distributions/fwd.hpp>
12 #include <boost/math/distributions/detail/common_error_handling.hpp>
13 #include <boost/math/distributions/complement.hpp>
14 #include <boost/math/special_functions/log1p.hpp>
15 #include <boost/math/constants/constants.hpp>
18 namespace boost { namespace math {
20 template <class RealType = double, class Policy = policies::policy<> >
21 class logistic_distribution
24 typedef RealType value_type;
25 typedef Policy policy_type;
27 logistic_distribution(RealType l_location=0, RealType l_scale=1) // Constructor.
28 : m_location(l_location), m_scale(l_scale)
30 static const char* function = "boost::math::logistic_distribution<%1%>::logistic_distribution";
33 detail::check_scale(function, l_scale, &result, Policy());
34 detail::check_location(function, l_location, &result, Policy());
36 // Accessor functions.
42 RealType location()const
48 RealType m_location; // distribution location aka mu.
49 RealType m_scale; // distribution scale aka s.
50 }; // class logistic_distribution
53 typedef logistic_distribution<double> logistic;
55 template <class RealType, class Policy>
56 inline const std::pair<RealType, RealType> range(const logistic_distribution<RealType, Policy>& /* dist */)
57 { // Range of permissible values for random variable x.
58 using boost::math::tools::max_value;
59 return std::pair<RealType, RealType>(
60 std::numeric_limits<RealType>::has_infinity ? -std::numeric_limits<RealType>::infinity() : -max_value<RealType>(),
61 std::numeric_limits<RealType>::has_infinity ? std::numeric_limits<RealType>::infinity() : max_value<RealType>());
64 template <class RealType, class Policy>
65 inline const std::pair<RealType, RealType> support(const logistic_distribution<RealType, Policy>& /* dist */)
66 { // Range of supported values for random variable x.
67 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
68 using boost::math::tools::max_value;
69 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + infinity
72 template <class RealType, class Policy>
73 inline RealType pdf(const logistic_distribution<RealType, Policy>& dist, const RealType& x)
75 static const char* function = "boost::math::pdf(const logistic_distribution<%1%>&, %1%)";
76 RealType scale = dist.scale();
77 RealType location = dist.location();
80 if(false == detail::check_scale(function, scale , &result, Policy()))
84 if(false == detail::check_location(function, location, &result, Policy()))
89 if((boost::math::isinf)(x))
91 return 0; // pdf + and - infinity is zero.
94 if(false == detail::check_x(function, x, &result, Policy()))
100 RealType exp_term = (location - x) / scale;
101 if(fabs(exp_term) > tools::log_max_value<RealType>())
103 exp_term = exp(exp_term);
104 if((exp_term * scale > 1) && (exp_term > tools::max_value<RealType>() / (scale * exp_term)))
105 return 1 / (scale * exp_term);
106 return (exp_term) / (scale * (1 + exp_term) * (1 + exp_term));
109 template <class RealType, class Policy>
110 inline RealType cdf(const logistic_distribution<RealType, Policy>& dist, const RealType& x)
112 RealType scale = dist.scale();
113 RealType location = dist.location();
114 RealType result = 0; // of checks.
115 static const char* function = "boost::math::cdf(const logistic_distribution<%1%>&, %1%)";
116 if(false == detail::check_scale(function, scale, &result, Policy()))
120 if(false == detail::check_location(function, location, &result, Policy()))
125 if((boost::math::isinf)(x))
127 if(x < 0) return 0; // -infinity
128 return 1; // + infinity
131 if(false == detail::check_x(function, x, &result, Policy()))
136 RealType power = (location - x) / scale;
137 if(power > tools::log_max_value<RealType>())
139 if(power < -tools::log_max_value<RealType>())
141 return 1 / (1 + exp(power));
144 template <class RealType, class Policy>
145 inline RealType quantile(const logistic_distribution<RealType, Policy>& dist, const RealType& p)
148 RealType location = dist.location();
149 RealType scale = dist.scale();
151 static const char* function = "boost::math::quantile(const logistic_distribution<%1%>&, %1%)";
154 if(false == detail::check_scale(function, scale, &result, Policy()))
156 if(false == detail::check_location(function, location, &result, Policy()))
158 if(false == detail::check_probability(function, p, &result, Policy()))
163 return -policies::raise_overflow_error<RealType>(function,"probability argument is 0, must be >0 and <1",Policy());
167 return policies::raise_overflow_error<RealType>(function,"probability argument is 1, must be >0 and <1",Policy());
170 //return location+scale*log(p/(1-p));
171 //return location+scale*log1p((2*p-1)/(1-p));
173 //return location - scale*log( (1-p)/p);
174 //return location - scale*log1p((1-2*p)/p);
176 //return -scale*log(1/p-1) + location;
177 return location - scale * log((1 - p) / p);
178 } // RealType quantile(const logistic_distribution<RealType, Policy>& dist, const RealType& p)
180 template <class RealType, class Policy>
181 inline RealType cdf(const complemented2_type<logistic_distribution<RealType, Policy>, RealType>& c)
184 RealType location = c.dist.location();
185 RealType scale = c.dist.scale();
186 RealType x = c.param;
187 static const char* function = "boost::math::cdf(const complement(logistic_distribution<%1%>&), %1%)";
190 if(false == detail::check_scale(function, scale, &result, Policy()))
194 if(false == detail::check_location(function, location, &result, Policy()))
198 if((boost::math::isinf)(x))
200 if(x < 0) return 1; // cdf complement -infinity is unity.
201 return 0; // cdf complement +infinity is zero.
203 if(false == detail::check_x(function, x, &result, Policy()))
207 RealType power = (x - location) / scale;
208 if(power > tools::log_max_value<RealType>())
210 if(power < -tools::log_max_value<RealType>())
212 return 1 / (1 + exp(power));
215 template <class RealType, class Policy>
216 inline RealType quantile(const complemented2_type<logistic_distribution<RealType, Policy>, RealType>& c)
219 RealType scale = c.dist.scale();
220 RealType location = c.dist.location();
221 static const char* function = "boost::math::quantile(const complement(logistic_distribution<%1%>&), %1%)";
223 if(false == detail::check_scale(function, scale, &result, Policy()))
225 if(false == detail::check_location(function, location, &result, Policy()))
227 RealType q = c.param;
228 if(false == detail::check_probability(function, q, &result, Policy()))
230 using boost::math::tools::max_value;
234 return -policies::raise_overflow_error<RealType>(function,"probability argument is 1, but must be >0 and <1",Policy());
238 return policies::raise_overflow_error<RealType>(function,"probability argument is 0, but must be >0 and <1",Policy());
241 //return location+scale*log((1-q)/q);
242 return location + scale * log((1 - q) / q);
244 //return location-scale*log(q/(1-q));
245 //return location-scale*log1p((2*q-1)/(1-q));
247 //return location+scale*log(1/q-1);
248 //return location+scale*log1p(1/q-2);
251 template <class RealType, class Policy>
252 inline RealType mean(const logistic_distribution<RealType, Policy>& dist)
254 return dist.location();
255 } // RealType mean(const logistic_distribution<RealType, Policy>& dist)
257 template <class RealType, class Policy>
258 inline RealType variance(const logistic_distribution<RealType, Policy>& dist)
261 RealType scale = dist.scale();
262 return boost::math::constants::pi<RealType>()*boost::math::constants::pi<RealType>()*scale*scale/3;
263 } // RealType variance(const logistic_distribution<RealType, Policy>& dist)
265 template <class RealType, class Policy>
266 inline RealType mode(const logistic_distribution<RealType, Policy>& dist)
268 return dist.location();
271 template <class RealType, class Policy>
272 inline RealType median(const logistic_distribution<RealType, Policy>& dist)
274 return dist.location();
276 template <class RealType, class Policy>
277 inline RealType skewness(const logistic_distribution<RealType, Policy>& /*dist*/)
280 } // RealType skewness(const logistic_distribution<RealType, Policy>& dist)
282 template <class RealType, class Policy>
283 inline RealType kurtosis_excess(const logistic_distribution<RealType, Policy>& /*dist*/)
285 return static_cast<RealType>(6)/5;
286 } // RealType kurtosis_excess(const logistic_distribution<RealType, Policy>& dist)
288 template <class RealType, class Policy>
289 inline RealType kurtosis(const logistic_distribution<RealType, Policy>& dist)
291 return kurtosis_excess(dist) + 3;
292 } // RealType kurtosis_excess(const logistic_distribution<RealType, Policy>& dist)
296 // Must come at the end:
297 #include <boost/math/distributions/detail/derived_accessors.hpp>
299 #endif // BOOST_MATH_DISTRIBUTIONS_LOGISTIC