⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_uniform.cpp

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Copyright Paul Bristow 2007.// Copyright John Maddock 2006.// 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_uniform.cpp#ifdef _MSC_VER#  pragma warning(disable: 4127) // conditional expression is constant.#  pragma warning(disable: 4100) // unreferenced formal parameter.#endif#include <boost/math/concepts/real_concept.hpp> // for real_concept#include <boost/test/included/test_exec_monitor.hpp> // Boost.Test#include <boost/test/floating_point_comparison.hpp>#include <boost/math/distributions/uniform.hpp>    using boost::math::uniform_distribution;#include <boost/math/tools/test.hpp> #include <iostream>   using std::cout;   using std::endl;   using std::setprecision;#include <limits>  using std::numeric_limits;template <class RealType>void check_uniform(RealType lower, RealType upper, RealType x, RealType p, RealType q, RealType tol){   BOOST_CHECK_CLOSE_FRACTION(      ::boost::math::cdf(         uniform_distribution<RealType>(lower, upper),   // distribution.         x),  // random variable.         p,    // probability.         tol);   // tolerance.   BOOST_CHECK_CLOSE_FRACTION(      ::boost::math::cdf(         complement(            uniform_distribution<RealType>(lower, upper), // distribution.            x)),    // random variable.         q,    // probability complement.         tol);  // tolerance.   BOOST_CHECK_CLOSE_FRACTION(      ::boost::math::quantile(         uniform_distribution<RealType>(lower, upper),  // distribution.         p),   // probability.         x,  // random variable.         tol);  // tolerance.   BOOST_CHECK_CLOSE_FRACTION(      ::boost::math::quantile(         complement(            uniform_distribution<RealType>(lower, upper),  // distribution.            q)),     // probability complement.         x,                                             // random variable.         tol);  // tolerance.} // void check_uniformtemplate <class RealType>void test_spots(RealType){   // Basic sanity checks   //   // These test values were generated for the normal distribution   // using the online calculator at    // http://espse.ed.psu.edu/edpsych/faculty/rhale/hale/507Mat/statlets/free/pdist.htm   //   // Tolerance is just over 5 decimal digits expressed as a fraction:   // that's the limit of the test data.   RealType tolerance = 2e-5f;     cout << "Tolerance for type " << typeid(RealType).name()  << " is " << tolerance << "." << endl;   using std::exp;   // Tests for PDF   //   BOOST_CHECK_CLOSE_FRACTION( // x == upper      pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0)),       static_cast<RealType>(1),       tolerance);   BOOST_CHECK_CLOSE_FRACTION( // x == lower      pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(1)),       static_cast<RealType>(1),       tolerance);   BOOST_CHECK_CLOSE_FRACTION( // x > upper      pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(-1)),       static_cast<RealType>(0),       tolerance);    BOOST_CHECK_CLOSE_FRACTION( // x < lower      pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(2)),       static_cast<RealType>(0),       tolerance);   if(std::numeric_limits<RealType>::has_infinity)    { // BOOST_CHECK tests for infinity using std::numeric_limits<>::infinity()      // Note that infinity is not implemented for real_concept, so these tests      // are only done for types, like built-in float, double.. that have infinity.    // Note that these assume that  BOOST_MATH_OVERFLOW_ERROR_POLICY is NOT throw_on_error.    // #define BOOST_MATH_OVERFLOW_ERROR_POLICY == throw_on_error would give a throw here.    // #define BOOST_MATH_DOMAIN_ERROR_POLICY == throw_on_error IS defined, so the throw path    // of error handling is tested below with BOOST_CHECK_THROW tests.     BOOST_CHECK_THROW( // x == infinity should NOT be OK.       pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(std::numeric_limits<RealType>::infinity())),        std::domain_error);     BOOST_CHECK_THROW( // x == minus infinity should be OK too.       pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(-std::numeric_limits<RealType>::infinity())),        std::domain_error);   }   if(std::numeric_limits<RealType>::has_quiet_NaN)   { // BOOST_CHECK tests for NaN using std::numeric_limits<>::has_quiet_NaN() - should throw.     BOOST_CHECK_THROW(       pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(std::numeric_limits<RealType>::quiet_NaN())),        std::domain_error);     BOOST_CHECK_THROW(       pdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(-std::numeric_limits<RealType>::quiet_NaN())),        std::domain_error);   } // test for x = NaN using std::numeric_limits<>::quiet_NaN()   // cdf   BOOST_CHECK_EQUAL( // x < lower      cdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(-1)),       static_cast<RealType>(0) );   BOOST_CHECK_CLOSE_FRACTION(      cdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0)),       static_cast<RealType>(0),       tolerance);   BOOST_CHECK_CLOSE_FRACTION(      cdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.5)),       static_cast<RealType>(0.5),       tolerance);   BOOST_CHECK_CLOSE_FRACTION(      cdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.1)),       static_cast<RealType>(0.1),       tolerance);   BOOST_CHECK_CLOSE_FRACTION(      cdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.9)),       static_cast<RealType>(0.9),       tolerance);   BOOST_CHECK_EQUAL( // x > upper      cdf(uniform_distribution<RealType>(0, 1), static_cast<RealType>(2)),       static_cast<RealType>(1));  // cdf complement   BOOST_CHECK_EQUAL( // x < lower      cdf(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0))),       static_cast<RealType>(1));   BOOST_CHECK_EQUAL( // x == 0      cdf(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0))),       static_cast<RealType>(1));   BOOST_CHECK_CLOSE_FRACTION( // x = 0.1      cdf(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.1))),       static_cast<RealType>(0.9),       tolerance);   BOOST_CHECK_CLOSE_FRACTION( // x = 0.5      cdf(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.5))),       static_cast<RealType>(0.5),       tolerance);   BOOST_CHECK_EQUAL( // x == 1      cdf(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(1))),       static_cast<RealType>(0));   BOOST_CHECK_EQUAL( // x > upper      cdf(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(2))),       static_cast<RealType>(1));   // quantile   BOOST_CHECK_CLOSE_FRACTION(      quantile(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.9)),       static_cast<RealType>(0.9),       tolerance);   BOOST_CHECK_CLOSE_FRACTION(      quantile(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.1)),       static_cast<RealType>(0.1),       tolerance);   BOOST_CHECK_CLOSE_FRACTION(      quantile(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.5)),       static_cast<RealType>(0.5),       tolerance);   BOOST_CHECK_CLOSE_FRACTION(      quantile(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0)),       static_cast<RealType>(0),       tolerance);   BOOST_CHECK_CLOSE_FRACTION(      quantile(uniform_distribution<RealType>(0, 1), static_cast<RealType>(1)),       static_cast<RealType>(1),       tolerance);   // quantile complement   BOOST_CHECK_CLOSE_FRACTION(      quantile(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.1))),       static_cast<RealType>(0.9),       tolerance);   BOOST_CHECK_CLOSE_FRACTION(      quantile(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.9))),       static_cast<RealType>(0.1),       tolerance);   BOOST_CHECK_CLOSE_FRACTION(      quantile(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0.5))),       static_cast<RealType>(0.5),       tolerance);   BOOST_CHECK_CLOSE_FRACTION(      quantile(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(0))),       static_cast<RealType>(1),       tolerance);   BOOST_CHECK_CLOSE_FRACTION(      quantile(complement(uniform_distribution<RealType>(0, 1), static_cast<RealType>(1))),       static_cast<RealType>(1),       tolerance);   // Some tests using a different location & scale, neight zero or unity.   BOOST_CHECK_CLOSE_FRACTION( // x == mid      pdf(uniform_distribution<RealType>(-1, 2), static_cast<RealType>(1)),       static_cast<RealType>(0.3333333333333333333333333333333333333333333333333333),       tolerance);   BOOST_CHECK_CLOSE_FRACTION( // x == upper      pdf(uniform_distribution<RealType>(-1, 2), static_cast<RealType>(+2)),       static_cast<RealType>(0.3333333333333333333333333333333333333333333333333333),  // 1 / (2 - -1) = 1/3      tolerance);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -