📄 test_cauchy.cpp
字号:
// Copyright John Maddock 2006, 2007.// Copyright Paul A. Bristow 2007// 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)// test_cauchy.cpp Test Cauchy distribution#ifdef _MSC_VER# pragma warning(disable: 4100) // unreferenced formal parameter.// Seems an entirely spurious warning - formal parameter T IS used - get error if /* T *///# pragma warning(disable: 4535) // calling _set_se_translator() requires /EHa (in Boost.test)// Enable C++ Exceptions Yes With SEH Exceptions (/EHa) prevents warning 4535.# pragma warning(disable: 4127) // conditional expression is constant#endif// #define BOOST_MATH_ASSERT_UNDEFINED_POLICY false // To compile even if Cauchy mean is used.#include <boost/math/concepts/real_concept.hpp> // for real_concept#include <boost/math/distributions/cauchy.hpp> using boost::math::cauchy_distribution;#include <boost/test/included/test_exec_monitor.hpp> // Boost.Test#include <boost/test/floating_point_comparison.hpp>#include <iostream> using std::cout; using std::endl;template <class RealType>void test_spots(RealType T){ // Check some bad parameters to the distribution, BOOST_CHECK_THROW(boost::math::cauchy_distribution<RealType> nbad1(0, 0), std::domain_error); // zero sd BOOST_CHECK_THROW(boost::math::cauchy_distribution<RealType> nbad1(0, -1), std::domain_error); // negative scale (shape) cauchy_distribution<RealType> C01; BOOST_CHECK_EQUAL(C01.location(), 0); // Check standard values. BOOST_CHECK_EQUAL(C01.scale(), 1); // Tests on extreme values of random variate x, if has numeric_limit infinity etc. if(std::numeric_limits<RealType>::has_infinity) { BOOST_CHECK_EQUAL(pdf(C01, +std::numeric_limits<RealType>::infinity()), 0); // x = + infinity, pdf = 0 BOOST_CHECK_EQUAL(pdf(C01, -std::numeric_limits<RealType>::infinity()), 0); // x = - infinity, pdf = 0 BOOST_CHECK_EQUAL(cdf(C01, +std::numeric_limits<RealType>::infinity()), 1); // x = + infinity, cdf = 1 BOOST_CHECK_EQUAL(cdf(C01, -std::numeric_limits<RealType>::infinity()), 0); // x = - infinity, cdf = 0 BOOST_CHECK_EQUAL(cdf(complement(C01, +std::numeric_limits<RealType>::infinity())), 0); // x = + infinity, cdf = 0 BOOST_CHECK_EQUAL(cdf(complement(C01, -std::numeric_limits<RealType>::infinity())), 1); // x = - infinity, cdf = 1 BOOST_CHECK_THROW(boost::math::cauchy_distribution<RealType> nbad1(std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // +infinite mean BOOST_CHECK_THROW(boost::math::cauchy_distribution<RealType> nbad1(-std::numeric_limits<RealType>::infinity(), static_cast<RealType>(1)), std::domain_error); // -infinite mean BOOST_CHECK_THROW(boost::math::cauchy_distribution<RealType> nbad1(static_cast<RealType>(0), std::numeric_limits<RealType>::infinity()), std::domain_error); // infinite sd } if (std::numeric_limits<RealType>::has_quiet_NaN) { // No longer allow x to be NaN, so these tests should throw. BOOST_CHECK_THROW(pdf(C01, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN BOOST_CHECK_THROW(cdf(C01, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // x = NaN BOOST_CHECK_THROW(cdf(complement(C01, +std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // x = + infinity BOOST_CHECK_THROW(quantile(C01, +std::numeric_limits<RealType>::quiet_NaN()), std::domain_error); // p = + infinity BOOST_CHECK_THROW(quantile(complement(C01, +std::numeric_limits<RealType>::quiet_NaN())), std::domain_error); // p = + infinity } // Basic sanity checks. // 50eps as a percentage, up to a maximum of double precision // (that's the limit of our test data). RealType tolerance = (std::max)( static_cast<RealType>(boost::math::tools::epsilon<double>()), boost::math::tools::epsilon<RealType>()); tolerance *= 50 * 100; cout << "Tolerance for type " << typeid(T).name() << " is " << tolerance << " %" << endl; // These first sets of test values were calculated by punching numbers // into a calculator, and using the formulas on the Mathworld website: // http://mathworld.wolfram.com/CauchyDistribution.html // and values from MathCAD 200 Professional, // CDF: // BOOST_CHECK_CLOSE( ::boost::math::cdf( cauchy_distribution<RealType>(), static_cast<RealType>(0.125)), // x static_cast<RealType>(0.53958342416056554201085167134004L), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( cauchy_distribution<RealType>(), static_cast<RealType>(-0.125)), // x static_cast<RealType>(0.46041657583943445798914832865996L), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( cauchy_distribution<RealType>(), static_cast<RealType>(0.5)), // x static_cast<RealType>(0.64758361765043327417540107622474L), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( cauchy_distribution<RealType>(), static_cast<RealType>(-0.5)), // x static_cast<RealType>(0.35241638234956672582459892377526L), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( cauchy_distribution<RealType>(), static_cast<RealType>(1.0)), // x static_cast<RealType>(0.75), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( cauchy_distribution<RealType>(), static_cast<RealType>(-1.0)), // x static_cast<RealType>(0.25), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( cauchy_distribution<RealType>(), static_cast<RealType>(2.0)), // x static_cast<RealType>(0.85241638234956672582459892377526L), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( cauchy_distribution<RealType>(), static_cast<RealType>(-2.0)), // x static_cast<RealType>(0.14758361765043327417540107622474L), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( cauchy_distribution<RealType>(), static_cast<RealType>(10.0)), // x static_cast<RealType>(0.9682744825694464304850228813987L), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( cauchy_distribution<RealType>(), static_cast<RealType>(-10.0)), // x static_cast<RealType>(0.031725517430553569514977118601302L), // probability. tolerance); // % // // Complements: // BOOST_CHECK_CLOSE( ::boost::math::cdf( complement(cauchy_distribution<RealType>(), static_cast<RealType>(0.125))), // x static_cast<RealType>(0.46041657583943445798914832865996L), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( complement(cauchy_distribution<RealType>(), static_cast<RealType>(-0.125))), // x static_cast<RealType>(0.53958342416056554201085167134004L), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( complement(cauchy_distribution<RealType>(), static_cast<RealType>(0.5))), // x static_cast<RealType>(0.35241638234956672582459892377526L), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( complement(cauchy_distribution<RealType>(), static_cast<RealType>(-0.5))), // x static_cast<RealType>(0.64758361765043327417540107622474L), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( complement(cauchy_distribution<RealType>(), static_cast<RealType>(1.0))), // x static_cast<RealType>(0.25), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( complement(cauchy_distribution<RealType>(), static_cast<RealType>(-1.0))), // x static_cast<RealType>(0.75), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( complement(cauchy_distribution<RealType>(), static_cast<RealType>(2.0))), // x static_cast<RealType>(0.14758361765043327417540107622474L), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( complement(cauchy_distribution<RealType>(), static_cast<RealType>(-2.0))), // x static_cast<RealType>(0.85241638234956672582459892377526L), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( complement(cauchy_distribution<RealType>(), static_cast<RealType>(10.0))), // x static_cast<RealType>(0.031725517430553569514977118601302L), // probability. tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::cdf( complement(cauchy_distribution<RealType>(), static_cast<RealType>(-10.0))), // x static_cast<RealType>(0.9682744825694464304850228813987L), // probability. tolerance); // % // // Quantiles: // BOOST_CHECK_CLOSE( ::boost::math::quantile( cauchy_distribution<RealType>(), static_cast<RealType>(0.53958342416056554201085167134004L)), static_cast<RealType>(0.125), tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::quantile( cauchy_distribution<RealType>(), static_cast<RealType>(0.46041657583943445798914832865996L)), static_cast<RealType>(-0.125), tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::quantile( cauchy_distribution<RealType>(), static_cast<RealType>(0.64758361765043327417540107622474L)), static_cast<RealType>(0.5), tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::quantile( cauchy_distribution<RealType>(), static_cast<RealType>(0.35241638234956672582459892377526)), static_cast<RealType>(-0.5), tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::quantile( cauchy_distribution<RealType>(), static_cast<RealType>(0.75)), static_cast<RealType>(1.0), tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::quantile( cauchy_distribution<RealType>(), static_cast<RealType>(0.25)), static_cast<RealType>(-1.0), tolerance); // % BOOST_CHECK_CLOSE( ::boost::math::quantile( cauchy_distribution<RealType>(), static_cast<RealType>(0.85241638234956672582459892377526L)), static_cast<RealType>(2.0), tolerance); // % BOOST_CHECK_CLOSE(
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -