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



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST