Main MRPT website > C++ reference for MRPT 1.9.9
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 
11 #include <mrpt/random.h>
12 #include <gtest/gtest.h>
13 
14 using namespace mrpt;
15 using namespace mrpt::utils;
16 using namespace mrpt::math;
17 using namespace mrpt::random;
18 using namespace std;
19 
20 const double eps = 1e-12;
21 
22 TEST(distributions, normalPDF_1d)
23 {
24  EXPECT_NEAR(normalPDF(0, 0, 1), 0.398942280401433, eps);
25  EXPECT_NEAR(normalPDF(5, 5, 1), 0.398942280401433, eps);
26  EXPECT_NEAR(normalPDF(0, 0, 2), 0.199471140200716, eps);
27  EXPECT_NEAR(normalPDF(1, 0, 1), 0.241970724519143, eps);
28 }
29 
30 TEST(distributions, normalPDF_vector)
31 {
32  const double cov_vals[3 * 3] = {4.0, 2.0, 1.0, 2.0, 3.0,
33  0.5, 1.0, 0.5, 1.0};
34  const double x1_vals[3] = {1.0, 0.0, 0.0};
35  const double x2_vals[3] = {1.0, 2.0, 3.0};
36 
37  const CMatrixDouble33 COV(cov_vals);
39  const CMatrixFixedNumeric<double, 3, 1> x1(x1_vals);
40  const CMatrixFixedNumeric<double, 3, 1> x2(x2_vals);
41 
42  EXPECT_NEAR(
43  normalPDF(x0, x0, COV), 0.02592116832548877620,
44  eps); // sprintf('%.20f',mvnpdf([0;0;0],[0;0;0],S))
45  EXPECT_NEAR(
46  normalPDF(x2, x2, COV), 0.02592116832548877620,
47  eps); // sprintf('%.20f',mvnpdf([0;0;0],[0;0;0],S))
48 
49  EXPECT_NEAR(
50  normalPDF(x1, x0, COV), 0.02061240910323311470,
51  eps); // sprintf('%.20f',mvnpdf([1;0;0],[0;0;0],S))
52  EXPECT_NEAR(
53  normalPDF(x2, x0, COV), 0.00008423820480102986,
54  eps); // sprintf('%.20f',mvnpdf([1;2;3],[0;0;0],S))
55 
56  EXPECT_NEAR(
57  normalPDF(x1, COV), 0.02061240910323311470,
58  eps); // sprintf('%.20f',mvnpdf([1;0;0],[0;0;0],S))
59  EXPECT_NEAR(
60  normalPDF(x2, COV), 0.00008423820480102986,
61  eps); // sprintf('%.20f',mvnpdf([1;0;0],[0;0;0],S))
62 }
63 
64 TEST(distributions, erfc)
65 {
66  const double eps2 = 1e-7;
67 
68  EXPECT_NEAR(std::erfc(0), 1, eps);
69  EXPECT_NEAR(std::erfc(1), 0.157299207050285, eps2);
70  EXPECT_NEAR(std::erfc(2), 0.004677734981047, eps2);
71 }
72 
73 TEST(distributions, erf)
74 {
75  const double eps2 = 1e-7;
76 
77  EXPECT_NEAR(std::erf(0), 0, eps);
78  EXPECT_NEAR(std::erf(1), 0.842700792949715, eps2);
79  EXPECT_NEAR(std::erf(2), 0.995322265018953, eps2);
80 }
81 
82 TEST(distributions, normalCDF)
83 {
84  EXPECT_NEAR(mrpt::math::normalCDF(0), 0.5, eps);
85  EXPECT_NEAR(mrpt::math::normalCDF(1), 0.841344746068543, eps);
86  EXPECT_NEAR(mrpt::math::normalCDF(2), 0.977249868051821, eps);
87  EXPECT_NEAR(mrpt::math::normalCDF(3), 0.998650101968370, eps);
88 }
89 
90 TEST(distributions, chi2inv)
91 {
92  EXPECT_NEAR(mrpt::math::chi2inv(0.0, 1), 0, eps);
93  EXPECT_NEAR(mrpt::math::chi2inv(0.5, 3), 2.365973884375338, 0.1);
94  EXPECT_NEAR(mrpt::math::chi2inv(0.95, 3), 7.814727903251178, 0.1);
95 }
96 
97 TEST(distributions, chi2PDF)
98 {
99  EXPECT_NEAR(mrpt::math::chi2PDF(1, 1.0), 0.241970724519143, eps);
100  EXPECT_NEAR(mrpt::math::chi2PDF(1, 2.0), 0.103776874355149, eps);
101  EXPECT_NEAR(mrpt::math::chi2PDF(1, 3.0), 0.051393443267923, eps);
102  EXPECT_NEAR(mrpt::math::chi2PDF(1, 4.0), 0.026995483256594, eps);
103 
104  EXPECT_NEAR(mrpt::math::chi2PDF(4, 1.0), 0.151632664928158, eps);
105 }
106 
108 {
109  const double eps2 = 1e-7;
110 
111  // ncx2cdf(arg,degreesOfFreedom,noncentrality)
112  // noncentralChi2PDF_CDF(degreesOfFreedom,noncentrality,arg)
113  EXPECT_NEAR(mrpt::math::noncentralChi2PDF_CDF(1, 1.0, 0).first, 0, eps2);
114  EXPECT_NEAR(mrpt::math::noncentralChi2PDF_CDF(1, 2.0, 0).first, 0, eps2);
115 
116  // MATLAB: ncx2cdf(1:3,1,1)
117  EXPECT_NEAR(
118  mrpt::math::noncentralChi2PDF_CDF(1, 1, 1.0).second, 0.477249868051821,
119  eps2);
120  EXPECT_NEAR(
121  mrpt::math::noncentralChi2PDF_CDF(1, 1, 2.0).second, 0.652756536682270,
122  eps2);
123  EXPECT_NEAR(
124  mrpt::math::noncentralChi2PDF_CDF(1, 1, 3.0).second, 0.764784149631031,
125  eps2);
126  // MATLAB: ncx2pdf(1:3,1,1)
127  EXPECT_NEAR(
128  mrpt::math::noncentralChi2PDF_CDF(1, 1, 1.0).first, 0.226466623457311,
129  eps2);
130  EXPECT_NEAR(
131  mrpt::math::noncentralChi2PDF_CDF(1, 1, 2.0).first, 0.137103272271503,
132  eps2);
133  EXPECT_NEAR(
134  mrpt::math::noncentralChi2PDF_CDF(1, 1, 3.0).first, 0.090852330823658,
135  eps2);
136 
137  // MATLAB: ncx2cdf(1:3,2,3)
138  EXPECT_NEAR(
139  mrpt::math::noncentralChi2PDF_CDF(2, 3, 1.0).second, 0.121825497229364,
140  eps2);
141  EXPECT_NEAR(
142  mrpt::math::noncentralChi2PDF_CDF(2, 3, 2.0).second, 0.252206942426039,
143  eps2);
144  EXPECT_NEAR(
145  mrpt::math::noncentralChi2PDF_CDF(2, 3, 3.0).second, 0.378499822919087,
146  eps2);
147  // MATLAB: ncx2pdf(1:3,2,3)
148  EXPECT_NEAR(
149  mrpt::math::noncentralChi2PDF_CDF(2, 3, 1.0).first, 0.128765424775546,
150  eps2);
151  EXPECT_NEAR(
152  mrpt::math::noncentralChi2PDF_CDF(2, 3, 2.0).first, 0.129923687128879,
153  eps2);
154  EXPECT_NEAR(
155  mrpt::math::noncentralChi2PDF_CDF(2, 3, 3.0).first, 0.121500177080913,
156  eps2);
157 }
A namespace of pseudo-random numbers genrators of diferent distributions.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
TEST(distributions, normalPDF_1d)
GLint * first
Definition: glext.h:3827
double normalCDF(double p)
Evaluates the Gaussian cumulative density function.
Definition: math.cpp:1132
STL namespace.
This base provides a set of functions for maths stuff.
Definition: CArrayNumeric.h:19
const double eps
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
double 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:998
std::pair< double, double > noncentralChi2PDF_CDF(unsigned int degreesOfFreedom, double noncentrality, double arg, double eps=1e-7)
Returns the &#39;exact&#39; PDF (first) and CDF (second) of a Non-central chi-squared probability distributio...
Definition: math.cpp:2066
double normalPDF(double x, double mu, double std)
Evaluates the univariate normal (Gaussian) distribution at a given point "x".
Definition: math.cpp:989
double chi2PDF(unsigned int degreesOfFreedom, double arg, double accuracy=1e-7)
Definition: math.cpp:2133



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019