1 // Copyright Benjamin Sobotta 2012
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)
7 #ifndef BOOST_STATS_SKEW_NORMAL_HPP
8 #define BOOST_STATS_SKEW_NORMAL_HPP
10 // http://en.wikipedia.org/wiki/Skew_normal_distribution
11 // http://azzalini.stat.unipd.it/SN/
13 // Azzalini, A. (1985). "A class of distributions which includes the normal ones".
14 // Scand. J. Statist. 12: 171-178.
16 #include <boost/math/distributions/fwd.hpp> // TODO add skew_normal distribution to fwd.hpp!
17 #include <boost/math/special_functions/owens_t.hpp> // Owen's T function
18 #include <boost/math/distributions/complement.hpp>
19 #include <boost/math/distributions/normal.hpp>
20 #include <boost/math/distributions/detail/common_error_handling.hpp>
21 #include <boost/math/constants/constants.hpp>
22 #include <boost/math/tools/tuple.hpp>
23 #include <boost/math/tools/roots.hpp> // Newton-Raphson
24 #include <boost/assert.hpp>
25 #include <boost/math/distributions/detail/generic_mode.hpp> // pdf max finder.
28 #include <algorithm> // std::lower_bound, std::distance
30 namespace boost{ namespace math{
34 template <class RealType, class Policy>
35 inline bool check_skew_normal_shape(
41 if(!(boost::math::isfinite)(shape))
44 policies::raise_domain_error<RealType>(function,
45 "Shape parameter is %1%, but must be finite!",
54 template <class RealType = double, class Policy = policies::policy<> >
55 class skew_normal_distribution
58 typedef RealType value_type;
59 typedef Policy policy_type;
61 skew_normal_distribution(RealType l_location = 0, RealType l_scale = 1, RealType l_shape = 0)
62 : location_(l_location), scale_(l_scale), shape_(l_shape)
63 { // Default is a 'standard' normal distribution N01. (shape=0 results in the normal distribution with no skew)
64 static const char* function = "boost::math::skew_normal_distribution<%1%>::skew_normal_distribution";
67 detail::check_scale(function, l_scale, &result, Policy());
68 detail::check_location(function, l_location, &result, Policy());
69 detail::check_skew_normal_shape(function, l_shape, &result, Policy());
72 RealType location()const
92 RealType location_; // distribution location.
93 RealType scale_; // distribution scale.
94 RealType shape_; // distribution shape.
95 }; // class skew_normal_distribution
97 typedef skew_normal_distribution<double> skew_normal;
99 template <class RealType, class Policy>
100 inline const std::pair<RealType, RealType> range(const skew_normal_distribution<RealType, Policy>& /*dist*/)
101 { // Range of permissible values for random variable x.
102 using boost::math::tools::max_value;
103 return std::pair<RealType, RealType>(
104 std::numeric_limits<RealType>::has_infinity ? -std::numeric_limits<RealType>::infinity() : -max_value<RealType>(),
105 std::numeric_limits<RealType>::has_infinity ? std::numeric_limits<RealType>::infinity() : max_value<RealType>()); // - to + max value.
108 template <class RealType, class Policy>
109 inline const std::pair<RealType, RealType> support(const skew_normal_distribution<RealType, Policy>& /*dist*/)
110 { // Range of supported values for random variable x.
111 // This is range where cdf rises from 0 to 1, and outside it, the pdf is zero.
113 using boost::math::tools::max_value;
114 return std::pair<RealType, RealType>(-max_value<RealType>(), max_value<RealType>()); // - to + max value.
117 template <class RealType, class Policy>
118 inline RealType pdf(const skew_normal_distribution<RealType, Policy>& dist, const RealType& x)
120 const RealType scale = dist.scale();
121 const RealType location = dist.location();
122 const RealType shape = dist.shape();
124 static const char* function = "boost::math::pdf(const skew_normal_distribution<%1%>&, %1%)";
127 if(false == detail::check_scale(function, scale, &result, Policy()))
131 if(false == detail::check_location(function, location, &result, Policy()))
135 if(false == detail::check_skew_normal_shape(function, shape, &result, Policy()))
139 if((boost::math::isinf)(x))
141 return 0; // pdf + and - infinity is zero.
143 // Below produces MSVC 4127 warnings, so the above used instead.
144 //if(std::numeric_limits<RealType>::has_infinity && abs(x) == std::numeric_limits<RealType>::infinity())
145 //{ // pdf + and - infinity is zero.
148 if(false == detail::check_x(function, x, &result, Policy()))
153 const RealType transformed_x = (x-location)/scale;
155 normal_distribution<RealType, Policy> std_normal;
157 result = pdf(std_normal, transformed_x) * cdf(std_normal, shape*transformed_x) * 2 / scale;
162 template <class RealType, class Policy>
163 inline RealType cdf(const skew_normal_distribution<RealType, Policy>& dist, const RealType& x)
165 const RealType scale = dist.scale();
166 const RealType location = dist.location();
167 const RealType shape = dist.shape();
169 static const char* function = "boost::math::cdf(const skew_normal_distribution<%1%>&, %1%)";
171 if(false == detail::check_scale(function, scale, &result, Policy()))
175 if(false == detail::check_location(function, location, &result, Policy()))
179 if(false == detail::check_skew_normal_shape(function, shape, &result, Policy()))
183 if((boost::math::isinf)(x))
185 if(x < 0) return 0; // -infinity
186 return 1; // + infinity
188 // These produce MSVC 4127 warnings, so the above used instead.
189 //if(std::numeric_limits<RealType>::has_infinity && x == std::numeric_limits<RealType>::infinity())
190 //{ // cdf +infinity is unity.
193 //if(std::numeric_limits<RealType>::has_infinity && x == -std::numeric_limits<RealType>::infinity())
194 //{ // cdf -infinity is zero.
197 if(false == detail::check_x(function, x, &result, Policy()))
202 const RealType transformed_x = (x-location)/scale;
204 normal_distribution<RealType, Policy> std_normal;
206 result = cdf(std_normal, transformed_x) - owens_t(transformed_x, shape)*static_cast<RealType>(2);
211 template <class RealType, class Policy>
212 inline RealType cdf(const complemented2_type<skew_normal_distribution<RealType, Policy>, RealType>& c)
214 const RealType scale = c.dist.scale();
215 const RealType location = c.dist.location();
216 const RealType shape = c.dist.shape();
217 const RealType x = c.param;
219 static const char* function = "boost::math::cdf(const complement(skew_normal_distribution<%1%>&), %1%)";
221 if((boost::math::isinf)(x))
223 if(x < 0) return 1; // cdf complement -infinity is unity.
224 return 0; // cdf complement +infinity is zero
226 // These produce MSVC 4127 warnings, so the above used instead.
227 //if(std::numeric_limits<RealType>::has_infinity && x == std::numeric_limits<RealType>::infinity())
228 //{ // cdf complement +infinity is zero.
231 //if(std::numeric_limits<RealType>::has_infinity && x == -std::numeric_limits<RealType>::infinity())
232 //{ // cdf complement -infinity is unity.
236 if(false == detail::check_scale(function, scale, &result, Policy()))
238 if(false == detail::check_location(function, location, &result, Policy()))
240 if(false == detail::check_skew_normal_shape(function, shape, &result, Policy()))
242 if(false == detail::check_x(function, x, &result, Policy()))
245 const RealType transformed_x = (x-location)/scale;
247 normal_distribution<RealType, Policy> std_normal;
249 result = cdf(complement(std_normal, transformed_x)) + owens_t(transformed_x, shape)*static_cast<RealType>(2);
253 template <class RealType, class Policy>
254 inline RealType location(const skew_normal_distribution<RealType, Policy>& dist)
256 return dist.location();
259 template <class RealType, class Policy>
260 inline RealType scale(const skew_normal_distribution<RealType, Policy>& dist)
265 template <class RealType, class Policy>
266 inline RealType shape(const skew_normal_distribution<RealType, Policy>& dist)
271 template <class RealType, class Policy>
272 inline RealType mean(const skew_normal_distribution<RealType, Policy>& dist)
274 BOOST_MATH_STD_USING // for ADL of std functions
276 using namespace boost::math::constants;
278 //const RealType delta = dist.shape() / sqrt(static_cast<RealType>(1)+dist.shape()*dist.shape());
280 //return dist.location() + dist.scale() * delta * root_two_div_pi<RealType>();
282 return dist.location() + dist.scale() * dist.shape() / sqrt(pi<RealType>()+pi<RealType>()*dist.shape()*dist.shape()) * root_two<RealType>();
285 template <class RealType, class Policy>
286 inline RealType variance(const skew_normal_distribution<RealType, Policy>& dist)
288 using namespace boost::math::constants;
290 const RealType delta2 = static_cast<RealType>(1) / (static_cast<RealType>(1)+static_cast<RealType>(1)/(dist.shape()*dist.shape()));
291 //const RealType inv_delta2 = static_cast<RealType>(1)+static_cast<RealType>(1)/(dist.shape()*dist.shape());
293 RealType variance = dist.scale()*dist.scale()*(static_cast<RealType>(1)-two_div_pi<RealType>()*delta2);
294 //RealType variance = dist.scale()*dist.scale()*(static_cast<RealType>(1)-two_div_pi<RealType>()/inv_delta2);
302 TODO No closed expression for mode, so use max of pdf.
305 template <class RealType, class Policy>
306 inline RealType mode_fallback(const skew_normal_distribution<RealType, Policy>& dist)
308 static const char* function = "mode(skew_normal_distribution<%1%> const&)";
309 const RealType scale = dist.scale();
310 const RealType location = dist.location();
311 const RealType shape = dist.shape();
314 if(!detail::check_scale(
316 scale, &result, Policy())
318 !detail::check_skew_normal_shape(
332 skew_normal_distribution<RealType, Policy> D(0, 1, -shape);
333 result = mode_fallback(D);
334 result = location-scale*result;
341 static const RealType shapes[] = {
343 1.000000000000000e-004,
344 2.069138081114790e-004,
345 4.281332398719396e-004,
346 8.858667904100824e-004,
347 1.832980710832436e-003,
348 3.792690190732250e-003,
349 7.847599703514606e-003,
350 1.623776739188722e-002,
351 3.359818286283781e-002,
352 6.951927961775606e-002,
353 1.438449888287663e-001,
354 2.976351441631319e-001,
355 6.158482110660261e-001,
356 1.274274985703135e+000,
357 2.636650898730361e+000,
358 5.455594781168514e+000,
359 1.128837891684688e+001,
360 2.335721469090121e+001,
361 4.832930238571753e+001,
362 1.000000000000000e+002};
365 static const RealType guess[] = {
367 5.000050000525391e-005,
368 1.500015000148736e-004,
369 3.500035000350010e-004,
370 7.500075000752560e-004,
371 1.450014500145258e-003,
372 3.050030500305390e-003,
373 6.250062500624765e-003,
374 1.295012950129504e-002,
375 2.675026750267495e-002,
376 5.525055250552491e-002,
377 1.132511325113255e-001,
378 2.249522495224952e-001,
379 3.992539925399257e-001,
380 5.353553535535358e-001,
381 4.954549545495457e-001,
382 3.524535245352451e-001,
383 2.182521825218249e-001,
384 1.256512565125654e-001,
385 6.945069450694508e-002,
386 3.735037350373460e-002
389 const RealType* result_ptr = std::lower_bound(shapes, shapes+21, shape);
391 typedef typename std::iterator_traits<RealType*>::difference_type diff_type;
393 const diff_type d = std::distance(shapes, result_ptr);
395 BOOST_ASSERT(d > static_cast<diff_type>(0));
398 if(d < static_cast<diff_type>(21)) // shape smaller 100
400 result = guess[d-static_cast<diff_type>(1)]
401 + (guess[d]-guess[d-static_cast<diff_type>(1)])/(shapes[d]-shapes[d-static_cast<diff_type>(1)])
402 * (shape-shapes[d-static_cast<diff_type>(1)]);
404 else // shape greater 100
409 skew_normal_distribution<RealType, Policy> helper(0, 1, shape);
411 result = detail::generic_find_mode_01(helper, result, function);
413 result = result*scale + location;
420 * TODO No closed expression for mode, so use f'(x) = 0
422 template <class RealType, class Policy>
423 struct skew_normal_mode_functor
425 skew_normal_mode_functor(const boost::math::skew_normal_distribution<RealType, Policy> dist)
430 boost::math::tuple<RealType, RealType> operator()(RealType const& x)
432 normal_distribution<RealType, Policy> std_normal;
433 const RealType shape = distribution.shape();
434 const RealType pdf_x = pdf(distribution, x);
435 const RealType normpdf_x = pdf(std_normal, x);
436 const RealType normpdf_ax = pdf(std_normal, x*shape);
437 RealType fx = static_cast<RealType>(2)*shape*normpdf_ax*normpdf_x - x*pdf_x;
438 RealType dx = static_cast<RealType>(2)*shape*x*normpdf_x*normpdf_ax*(static_cast<RealType>(1) + shape*shape) + pdf_x + x*fx;
439 // return both function evaluation difference f(x) and 1st derivative f'(x).
440 return boost::math::make_tuple(fx, -dx);
443 const boost::math::skew_normal_distribution<RealType, Policy> distribution;
446 } // namespace detail
448 template <class RealType, class Policy>
449 inline RealType mode(const skew_normal_distribution<RealType, Policy>& dist)
451 const RealType scale = dist.scale();
452 const RealType location = dist.location();
453 const RealType shape = dist.shape();
455 static const char* function = "boost::math::mode(const skew_normal_distribution<%1%>&, %1%)";
458 if(false == detail::check_scale(function, scale, &result, Policy()))
460 if(false == detail::check_location(function, location, &result, Policy()))
462 if(false == detail::check_skew_normal_shape(function, shape, &result, Policy()))
472 skew_normal_distribution<RealType, Policy> D(0, 1, -shape);
474 result = location-scale*result;
479 static const RealType shapes[] = {
481 static_cast<RealType>(1.000000000000000e-004),
482 static_cast<RealType>(2.069138081114790e-004),
483 static_cast<RealType>(4.281332398719396e-004),
484 static_cast<RealType>(8.858667904100824e-004),
485 static_cast<RealType>(1.832980710832436e-003),
486 static_cast<RealType>(3.792690190732250e-003),
487 static_cast<RealType>(7.847599703514606e-003),
488 static_cast<RealType>(1.623776739188722e-002),
489 static_cast<RealType>(3.359818286283781e-002),
490 static_cast<RealType>(6.951927961775606e-002),
491 static_cast<RealType>(1.438449888287663e-001),
492 static_cast<RealType>(2.976351441631319e-001),
493 static_cast<RealType>(6.158482110660261e-001),
494 static_cast<RealType>(1.274274985703135e+000),
495 static_cast<RealType>(2.636650898730361e+000),
496 static_cast<RealType>(5.455594781168514e+000),
497 static_cast<RealType>(1.128837891684688e+001),
498 static_cast<RealType>(2.335721469090121e+001),
499 static_cast<RealType>(4.832930238571753e+001),
500 static_cast<RealType>(1.000000000000000e+002)
504 static const RealType guess[] = {
506 static_cast<RealType>(5.000050000525391e-005),
507 static_cast<RealType>(1.500015000148736e-004),
508 static_cast<RealType>(3.500035000350010e-004),
509 static_cast<RealType>(7.500075000752560e-004),
510 static_cast<RealType>(1.450014500145258e-003),
511 static_cast<RealType>(3.050030500305390e-003),
512 static_cast<RealType>(6.250062500624765e-003),
513 static_cast<RealType>(1.295012950129504e-002),
514 static_cast<RealType>(2.675026750267495e-002),
515 static_cast<RealType>(5.525055250552491e-002),
516 static_cast<RealType>(1.132511325113255e-001),
517 static_cast<RealType>(2.249522495224952e-001),
518 static_cast<RealType>(3.992539925399257e-001),
519 static_cast<RealType>(5.353553535535358e-001),
520 static_cast<RealType>(4.954549545495457e-001),
521 static_cast<RealType>(3.524535245352451e-001),
522 static_cast<RealType>(2.182521825218249e-001),
523 static_cast<RealType>(1.256512565125654e-001),
524 static_cast<RealType>(6.945069450694508e-002),
525 static_cast<RealType>(3.735037350373460e-002)
528 const RealType* result_ptr = std::lower_bound(shapes, shapes+21, shape);
530 typedef typename std::iterator_traits<RealType*>::difference_type diff_type;
532 const diff_type d = std::distance(shapes, result_ptr);
534 BOOST_ASSERT(d > static_cast<diff_type>(0));
536 // TODO: make the search bounds smarter, depending on the shape parameter
537 RealType search_min = 0; // below zero was caught above
538 RealType search_max = 0.55f; // will never go above 0.55
541 if(d < static_cast<diff_type>(21)) // shape smaller 100
543 // it is safe to assume that d > 0, because shape==0.0 is caught earlier
544 result = guess[d-static_cast<diff_type>(1)]
545 + (guess[d]-guess[d-static_cast<diff_type>(1)])/(shapes[d]-shapes[d-static_cast<diff_type>(1)])
546 * (shape-shapes[d-static_cast<diff_type>(1)]);
548 else // shape greater 100
551 search_max = guess[19]; // set 19 instead of 20 to have a safety margin because the table may not be exact @ shape=100
554 const int get_digits = policies::digits<RealType, Policy>();// get digits from policy,
555 boost::uintmax_t m = policies::get_max_root_iterations<Policy>(); // and max iterations.
557 skew_normal_distribution<RealType, Policy> helper(0, 1, shape);
559 result = tools::newton_raphson_iterate(detail::skew_normal_mode_functor<RealType, Policy>(helper), result,
560 search_min, search_max, get_digits, m);
562 result = result*scale + location;
569 template <class RealType, class Policy>
570 inline RealType skewness(const skew_normal_distribution<RealType, Policy>& dist)
572 BOOST_MATH_STD_USING // for ADL of std functions
573 using namespace boost::math::constants;
575 static const RealType factor = four_minus_pi<RealType>()/static_cast<RealType>(2);
576 const RealType delta = dist.shape() / sqrt(static_cast<RealType>(1)+dist.shape()*dist.shape());
578 return factor * pow(root_two_div_pi<RealType>() * delta, 3) /
579 pow(static_cast<RealType>(1)-two_div_pi<RealType>()*delta*delta, static_cast<RealType>(1.5));
582 template <class RealType, class Policy>
583 inline RealType kurtosis(const skew_normal_distribution<RealType, Policy>& dist)
585 return kurtosis_excess(dist)+static_cast<RealType>(3);
588 template <class RealType, class Policy>
589 inline RealType kurtosis_excess(const skew_normal_distribution<RealType, Policy>& dist)
591 using namespace boost::math::constants;
593 static const RealType factor = pi_minus_three<RealType>()*static_cast<RealType>(2);
595 const RealType delta2 = static_cast<RealType>(1) / (static_cast<RealType>(1)+static_cast<RealType>(1)/(dist.shape()*dist.shape()));
597 const RealType x = static_cast<RealType>(1)-two_div_pi<RealType>()*delta2;
598 const RealType y = two_div_pi<RealType>() * delta2;
600 return factor * y*y / (x*x);
606 template <class RealType, class Policy>
607 struct skew_normal_quantile_functor
609 skew_normal_quantile_functor(const boost::math::skew_normal_distribution<RealType, Policy> dist, RealType const& p)
610 : distribution(dist), prob(p)
614 boost::math::tuple<RealType, RealType> operator()(RealType const& x)
616 RealType c = cdf(distribution, x);
617 RealType fx = c - prob; // Difference cdf - value - to minimize.
618 RealType dx = pdf(distribution, x); // pdf is 1st derivative.
619 // return both function evaluation difference f(x) and 1st derivative f'(x).
620 return boost::math::make_tuple(fx, dx);
623 const boost::math::skew_normal_distribution<RealType, Policy> distribution;
627 } // namespace detail
629 template <class RealType, class Policy>
630 inline RealType quantile(const skew_normal_distribution<RealType, Policy>& dist, const RealType& p)
632 const RealType scale = dist.scale();
633 const RealType location = dist.location();
634 const RealType shape = dist.shape();
636 static const char* function = "boost::math::quantile(const skew_normal_distribution<%1%>&, %1%)";
639 if(false == detail::check_scale(function, scale, &result, Policy()))
641 if(false == detail::check_location(function, location, &result, Policy()))
643 if(false == detail::check_skew_normal_shape(function, shape, &result, Policy()))
645 if(false == detail::check_probability(function, p, &result, Policy()))
648 // Compute initial guess via Cornish-Fisher expansion.
649 RealType x = -boost::math::erfc_inv(2 * p, Policy()) * constants::root_two<RealType>();
651 // Avoid unnecessary computations if there is no skew.
654 const RealType skew = skewness(dist);
655 const RealType exk = kurtosis_excess(dist);
657 x = x + (x*x-static_cast<RealType>(1))*skew/static_cast<RealType>(6)
658 + x*(x*x-static_cast<RealType>(3))*exk/static_cast<RealType>(24)
659 - x*(static_cast<RealType>(2)*x*x-static_cast<RealType>(5))*skew*skew/static_cast<RealType>(36);
662 result = standard_deviation(dist)*x+mean(dist);
664 // handle special case of non-skew normal distribution.
668 // refine the result by numerically searching the root of (p-cdf)
670 const RealType search_min = range(dist).first;
671 const RealType search_max = range(dist).second;
673 const int get_digits = policies::digits<RealType, Policy>();// get digits from policy,
674 boost::uintmax_t m = policies::get_max_root_iterations<Policy>(); // and max iterations.
676 result = tools::newton_raphson_iterate(detail::skew_normal_quantile_functor<RealType, Policy>(dist, p), result,
677 search_min, search_max, get_digits, m);
682 template <class RealType, class Policy>
683 inline RealType quantile(const complemented2_type<skew_normal_distribution<RealType, Policy>, RealType>& c)
685 const RealType scale = c.dist.scale();
686 const RealType location = c.dist.location();
687 const RealType shape = c.dist.shape();
689 static const char* function = "boost::math::quantile(const complement(skew_normal_distribution<%1%>&), %1%)";
691 if(false == detail::check_scale(function, scale, &result, Policy()))
693 if(false == detail::check_location(function, location, &result, Policy()))
695 if(false == detail::check_skew_normal_shape(function, shape, &result, Policy()))
697 RealType q = c.param;
698 if(false == detail::check_probability(function, q, &result, Policy()))
701 skew_normal_distribution<RealType, Policy> D(-location, scale, -shape);
703 result = -quantile(D, q);
712 // This include must be at the end, *after* the accessors
713 // for this distribution have been defined, in order to
714 // keep compilers that support two-phase lookup happy.
715 #include <boost/math/distributions/detail/derived_accessors.hpp>
717 #endif // BOOST_STATS_SKEW_NORMAL_HPP