File: git_issue_1120.cpp

package info (click to toggle)
scipy 1.16.0-1exp7
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 234,820 kB
  • sloc: cpp: 503,145; python: 344,611; ansic: 195,638; javascript: 89,566; fortran: 56,210; cs: 3,081; f90: 1,150; sh: 848; makefile: 785; pascal: 284; csh: 135; lisp: 134; xml: 56; perl: 51
file content (60 lines) | stat: -rw-r--r-- 2,432 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//  Copyright John Maddock 2024.
//  Use, modification and distribution are subject to the
//  Boost Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

//
// The purpose of this test case is to probe the skew normal quantiles
// for extreme values of skewness and ensure that our root finders don't
// blow up, see https://github.com/boostorg/math/issues/1120 for original
// test case.  We test both the maximum number of iterations taken, and the
// overall total (ie average).  Any changes to the skew normal should
// ideally NOT cause this test to fail, as this indicates that our root
// finding has been made worse by the change!!
//
// Note that defining BOOST_MATH_INSTRUMENT_SKEW_NORMAL_ITERATIONS
// causes the skew normal quantile to save the number of iterations
// to a global variable "global_iter_count".
//

#define BOOST_MATH_INSTRUMENT_SKEW_NORMAL_ITERATIONS

#include <random>
#include <boost/math/distributions/skew_normal.hpp>
#include "math_unit_test.hpp"

std::uintmax_t global_iter_count;
std::uintmax_t total_iter_count = 0;

int main()
{
   using scipy_policy = boost::math::policies::policy<boost::math::policies::promote_double<false>>;

   std::mt19937 gen;
   std::uniform_real_distribution<double> location(-3, 3);
   std::uniform_real_distribution<double> scale(0.001, 3);

   for (unsigned skew = 50; skew < 2000; skew += 43)
   {
      constexpr double pn[] = { 0.0001, 0.001, 0.01, 0.1, 0.2, 0.3, 0.4 };
      boost::math::skew_normal_distribution<double, scipy_policy> dist(location(gen), scale(gen), skew);
      for (unsigned i = 0; i < 7; ++i)
      {
         global_iter_count = 0;
         double x = quantile(dist, pn[i]);
         total_iter_count += global_iter_count;
         CHECK_LE(global_iter_count, static_cast<std::uintmax_t>(60));
         double p = cdf(dist, x);
         CHECK_ABSOLUTE_ERROR(p, pn[i], 45 * std::numeric_limits<double>::epsilon());

         global_iter_count = 0;
         x = quantile(complement(dist, pn[i]));
         total_iter_count += global_iter_count;
         CHECK_LE(global_iter_count, static_cast<std::uintmax_t>(60));
         p = cdf(complement(dist, x));
         CHECK_ABSOLUTE_ERROR(p, pn[i], 45 * std::numeric_limits<double>::epsilon());
      }
   }
   CHECK_LE(total_iter_count, static_cast<std::uintmax_t>(10000));
   return boost::math::test::report_errors();
}