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