Main MRPT website > C++ reference for MRPT 1.5.6
distributions_unittest.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 
12 #include <mrpt/random.h>
13 #include <gtest/gtest.h>
14 
15 using namespace mrpt;
16 using namespace mrpt::utils;
17 using namespace mrpt::math;
18 using namespace mrpt::random;
19 using namespace std;
20 
21 const double eps = 1e-12;
22 
23 TEST(distributions,normalPDF_1d)
24 {
25  EXPECT_NEAR( normalPDF(0,0,1),0.398942280401433, eps);
26  EXPECT_NEAR( normalPDF(5,5,1),0.398942280401433, eps);
27  EXPECT_NEAR( normalPDF(0,0,2),0.199471140200716, eps);
28  EXPECT_NEAR( normalPDF(1,0,1),0.241970724519143, eps);
29 }
30 
31 TEST(distributions,normalPDF_vector)
32 {
33  const double cov_vals [3*3] = {
34  4.0, 2.0, 1.0,
35  2.0, 3.0, 0.5,
36  1.0, 0.5, 1.0
37  };
38  const double x1_vals[3] = {1.0, 0.0, 0.0};
39  const double x2_vals[3] = {1.0, 2.0, 3.0};
40 
41  const CMatrixDouble33 COV(cov_vals);
43  const CMatrixFixedNumeric<double,3,1> x1(x1_vals);
44  const CMatrixFixedNumeric<double,3,1> x2(x2_vals);
45 
46  EXPECT_NEAR( normalPDF(x0,x0,COV), 0.02592116832548877620, eps); // sprintf('%.20f',mvnpdf([0;0;0],[0;0;0],S))
47  EXPECT_NEAR( normalPDF(x2,x2,COV), 0.02592116832548877620, eps); // sprintf('%.20f',mvnpdf([0;0;0],[0;0;0],S))
48 
49  EXPECT_NEAR( normalPDF(x1,x0,COV), 0.02061240910323311470, eps); // sprintf('%.20f',mvnpdf([1;0;0],[0;0;0],S))
50  EXPECT_NEAR( normalPDF(x2,x0,COV), 0.00008423820480102986, eps); // sprintf('%.20f',mvnpdf([1;2;3],[0;0;0],S))
51 
52  EXPECT_NEAR( normalPDF(x1,COV), 0.02061240910323311470, eps); // sprintf('%.20f',mvnpdf([1;0;0],[0;0;0],S))
53  EXPECT_NEAR( normalPDF(x2,COV), 0.00008423820480102986, eps); // sprintf('%.20f',mvnpdf([1;0;0],[0;0;0],S))
54 }
55 
56 TEST(distributions,erfc)
57 {
58  const double eps2 = 1e-7;
59 
60  EXPECT_NEAR( mrpt::math::erfc(0), 1, eps );
61  EXPECT_NEAR( mrpt::math::erfc(1), 0.157299207050285, eps2 );
62  EXPECT_NEAR( mrpt::math::erfc(2), 0.004677734981047, eps2 );
63 }
64 
65 TEST(distributions,erf)
66 {
67  const double eps2 = 1e-7;
68 
69  EXPECT_NEAR( mrpt::math::erf(0), 0, eps );
70  EXPECT_NEAR( mrpt::math::erf(1), 0.842700792949715, eps2 );
71  EXPECT_NEAR( mrpt::math::erf(2), 0.995322265018953, eps2 );
72 }
73 
74 TEST(distributions,normalCDF)
75 {
76  EXPECT_NEAR( mrpt::math::normalCDF(0), 0.5 , eps );
77  EXPECT_NEAR( mrpt::math::normalCDF(1), 0.841344746068543 , eps );
78  EXPECT_NEAR( mrpt::math::normalCDF(2), 0.977249868051821 , eps );
79  EXPECT_NEAR( mrpt::math::normalCDF(3), 0.998650101968370 , eps );
80 }
81 
82 TEST(distributions,chi2inv)
83 {
84  EXPECT_NEAR( mrpt::math::chi2inv(0.0, 1), 0 , eps );
85  EXPECT_NEAR( mrpt::math::chi2inv(0.5, 3), 2.365973884375338 , 0.1 );
86  EXPECT_NEAR( mrpt::math::chi2inv(0.95, 3), 7.814727903251178 , 0.1 );
87 }
88 
89 TEST(distributions,chi2PDF)
90 {
91  EXPECT_NEAR( mrpt::math::chi2PDF(1, 1.0), 0.241970724519143 , eps );
92  EXPECT_NEAR( mrpt::math::chi2PDF(1, 2.0), 0.103776874355149 , eps );
93  EXPECT_NEAR( mrpt::math::chi2PDF(1, 3.0), 0.051393443267923 , eps );
94  EXPECT_NEAR( mrpt::math::chi2PDF(1, 4.0), 0.026995483256594 , eps );
95 
96  EXPECT_NEAR( mrpt::math::chi2PDF(4, 1.0), 0.151632664928158 , eps );
97 }
98 
100 {
101  const double eps2 = 1e-7;
102 
103  // ncx2cdf(arg,degreesOfFreedom,noncentrality)
104  // noncentralChi2PDF_CDF(degreesOfFreedom,noncentrality,arg)
105  EXPECT_NEAR( mrpt::math::noncentralChi2PDF_CDF(1, 1.0, 0).first, 0 , eps2 );
106  EXPECT_NEAR( mrpt::math::noncentralChi2PDF_CDF(1, 2.0, 0).first, 0 , eps2 );
107 
108  // MATLAB: ncx2cdf(1:3,1,1)
109  EXPECT_NEAR( mrpt::math::noncentralChi2PDF_CDF(1, 1, 1.0).second, 0.477249868051821, eps2 );
110  EXPECT_NEAR( mrpt::math::noncentralChi2PDF_CDF(1, 1, 2.0).second, 0.652756536682270, eps2 );
111  EXPECT_NEAR( mrpt::math::noncentralChi2PDF_CDF(1, 1, 3.0).second, 0.764784149631031, eps2 );
112  // MATLAB: ncx2pdf(1:3,1,1)
113  EXPECT_NEAR( mrpt::math::noncentralChi2PDF_CDF(1, 1, 1.0).first, 0.226466623457311, eps2 );
114  EXPECT_NEAR( mrpt::math::noncentralChi2PDF_CDF(1, 1, 2.0).first, 0.137103272271503, eps2 );
115  EXPECT_NEAR( mrpt::math::noncentralChi2PDF_CDF(1, 1, 3.0).first, 0.090852330823658, eps2 );
116 
117  // MATLAB: ncx2cdf(1:3,2,3)
118  EXPECT_NEAR( mrpt::math::noncentralChi2PDF_CDF(2, 3, 1.0).second, 0.121825497229364, eps2 );
119  EXPECT_NEAR( mrpt::math::noncentralChi2PDF_CDF(2, 3, 2.0).second, 0.252206942426039, eps2 );
120  EXPECT_NEAR( mrpt::math::noncentralChi2PDF_CDF(2, 3, 3.0).second, 0.378499822919087, eps2 );
121  // MATLAB: ncx2pdf(1:3,2,3)
122  EXPECT_NEAR( mrpt::math::noncentralChi2PDF_CDF(2, 3, 1.0).first, 0.128765424775546, eps2 );
123  EXPECT_NEAR( mrpt::math::noncentralChi2PDF_CDF(2, 3, 2.0).first, 0.129923687128879, eps2 );
124  EXPECT_NEAR( mrpt::math::noncentralChi2PDF_CDF(2, 3, 3.0).first, 0.121500177080913, eps2 );
125 }
TEST(distributions, normalPDF_1d)
double BASE_IMPEXP normalCDF(double p)
Evaluates the Gaussian cumulative density function.
Definition: math.cpp:1108
double BASE_IMPEXP erfc(const double x)
The complementary error function of a Normal distribution.
Definition: math.cpp:917
double BASE_IMPEXP erf(const double x)
The error function of a Normal distribution.
Definition: math.cpp:955
const double eps
GLint * first
Definition: glew.h:1433
double BASE_IMPEXP chi2inv(double P, unsigned int dim=1)
The "quantile" of the Chi-Square distribution, for dimension "dim" and probability 0<P<1 (the inverse...
Definition: math.cpp:967
std::pair< double, double > BASE_IMPEXP noncentralChi2PDF_CDF(unsigned int degreesOfFreedom, double noncentrality, double arg, double eps=1e-7)
Returns the 'exact' PDF (first) and CDF (second) of a Non-central chi-squared probability distributio...
Definition: math.cpp:2120
double BASE_IMPEXP normalPDF(double x, double mu, double std)
Evaluates the univariate normal (Gaussian) distribution at a given point "x".
Definition: math.cpp:908
double BASE_IMPEXP chi2PDF(unsigned int degreesOfFreedom, double arg, double accuracy=1e-7)
Definition: math.cpp:2189



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018