MRPT  2.0.4
distributions_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <gtest/gtest.h>
11 #include <mrpt/math/data_utils.h>
13 #include <mrpt/random.h>
14 #include <Eigen/Dense>
15 
16 using namespace mrpt;
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] = {4.0, 2.0, 1.0, 2.0, 3.0,
34  0.5, 1.0, 0.5, 1.0};
35  const double x1_vals[3] = {1.0, 0.0, 0.0};
36  const double x2_vals[3] = {1.0, 2.0, 3.0};
37 
38  const CMatrixDouble33 COV(cov_vals);
40  const CMatrixFixed<double, 3, 1> x1(x1_vals);
41  const CMatrixFixed<double, 3, 1> x2(x2_vals);
42 
44  normalPDF(x0, x0, COV), 0.02592116832548877620,
45  eps); // sprintf('%.20f',mvnpdf([0;0;0],[0;0;0],S))
47  normalPDF(x2, x2, COV), 0.02592116832548877620,
48  eps); // sprintf('%.20f',mvnpdf([0;0;0],[0;0;0],S))
49 
51  normalPDF(x1, x0, COV), 0.02061240910323311470,
52  eps); // sprintf('%.20f',mvnpdf([1;0;0],[0;0;0],S))
54  normalPDF(x2, x0, COV), 0.00008423820480102986,
55  eps); // sprintf('%.20f',mvnpdf([1;2;3],[0;0;0],S))
56 
58  normalPDF(x1, COV), 0.02061240910323311470,
59  eps); // sprintf('%.20f',mvnpdf([1;0;0],[0;0;0],S))
61  normalPDF(x2, COV), 0.00008423820480102986,
62  eps); // sprintf('%.20f',mvnpdf([1;0;0],[0;0;0],S))
63 }
64 
65 TEST(distributions, erfc)
66 {
67  const double eps2 = 1e-7;
68 
69  EXPECT_NEAR(std::erfc(0), 1, eps);
70  EXPECT_NEAR(std::erfc(1), 0.157299207050285, eps2);
71  EXPECT_NEAR(std::erfc(2), 0.004677734981047, eps2);
72 }
73 
74 TEST(distributions, erf)
75 {
76  const double eps2 = 1e-7;
77 
78  EXPECT_NEAR(std::erf(0), 0, eps);
79  EXPECT_NEAR(std::erf(1), 0.842700792949715, eps2);
80  EXPECT_NEAR(std::erf(2), 0.995322265018953, eps2);
81 }
82 
83 TEST(distributions, normalCDF)
84 {
86  EXPECT_NEAR(mrpt::math::normalCDF(1), 0.841344746068543, eps);
87  EXPECT_NEAR(mrpt::math::normalCDF(2), 0.977249868051821, eps);
88  EXPECT_NEAR(mrpt::math::normalCDF(3), 0.998650101968370, eps);
89 }
90 
91 TEST(distributions, chi2inv)
92 {
93  EXPECT_NEAR(mrpt::math::chi2inv(0.0, 1), 0, eps);
94  EXPECT_NEAR(mrpt::math::chi2inv(0.5, 3), 2.365973884375338, 0.1);
95  EXPECT_NEAR(mrpt::math::chi2inv(0.95, 3), 7.814727903251178, 0.1);
96 }
97 
98 TEST(distributions, chi2PDF)
99 {
100  EXPECT_NEAR(mrpt::math::chi2PDF(1, 1.0), 0.241970724519143, eps);
101  EXPECT_NEAR(mrpt::math::chi2PDF(1, 2.0), 0.103776874355149, eps);
102  EXPECT_NEAR(mrpt::math::chi2PDF(1, 3.0), 0.051393443267923, eps);
103  EXPECT_NEAR(mrpt::math::chi2PDF(1, 4.0), 0.026995483256594, eps);
104 
105  EXPECT_NEAR(mrpt::math::chi2PDF(4, 1.0), 0.151632664928158, eps);
106 }
107 
109 {
110  const double eps2 = 1e-7;
111 
112  // ncx2cdf(arg,degreesOfFreedom,noncentrality)
113  // noncentralChi2PDF_CDF(degreesOfFreedom,noncentrality,arg)
114  EXPECT_NEAR(mrpt::math::noncentralChi2PDF_CDF(1, 1.0, 0).first, 0, eps2);
115  EXPECT_NEAR(mrpt::math::noncentralChi2PDF_CDF(1, 2.0, 0).first, 0, eps2);
116 
117  // MATLAB: ncx2cdf(1:3,1,1)
118  EXPECT_NEAR(
119  mrpt::math::noncentralChi2PDF_CDF(1, 1, 1.0).second, 0.477249868051821,
120  eps2);
121  EXPECT_NEAR(
122  mrpt::math::noncentralChi2PDF_CDF(1, 1, 2.0).second, 0.652756536682270,
123  eps2);
124  EXPECT_NEAR(
125  mrpt::math::noncentralChi2PDF_CDF(1, 1, 3.0).second, 0.764784149631031,
126  eps2);
127  // MATLAB: ncx2pdf(1:3,1,1)
128  EXPECT_NEAR(
129  mrpt::math::noncentralChi2PDF_CDF(1, 1, 1.0).first, 0.226466623457311,
130  eps2);
131  EXPECT_NEAR(
132  mrpt::math::noncentralChi2PDF_CDF(1, 1, 2.0).first, 0.137103272271503,
133  eps2);
134  EXPECT_NEAR(
135  mrpt::math::noncentralChi2PDF_CDF(1, 1, 3.0).first, 0.090852330823658,
136  eps2);
137 
138  // MATLAB: ncx2cdf(1:3,2,3)
139  EXPECT_NEAR(
140  mrpt::math::noncentralChi2PDF_CDF(2, 3, 1.0).second, 0.121825497229364,
141  eps2);
142  EXPECT_NEAR(
143  mrpt::math::noncentralChi2PDF_CDF(2, 3, 2.0).second, 0.252206942426039,
144  eps2);
145  EXPECT_NEAR(
146  mrpt::math::noncentralChi2PDF_CDF(2, 3, 3.0).second, 0.378499822919087,
147  eps2);
148  // MATLAB: ncx2pdf(1:3,2,3)
149  EXPECT_NEAR(
150  mrpt::math::noncentralChi2PDF_CDF(2, 3, 1.0).first, 0.128765424775546,
151  eps2);
152  EXPECT_NEAR(
153  mrpt::math::noncentralChi2PDF_CDF(2, 3, 2.0).first, 0.129923687128879,
154  eps2);
155  EXPECT_NEAR(
156  mrpt::math::noncentralChi2PDF_CDF(2, 3, 3.0).first, 0.121500177080913,
157  eps2);
158 }
159 
161 {
162  const double cov_vals[3 * 3] = {0.00393682, -6.11165e-07, -8.62169e-05,
163  -6.11165e-07, 7.44917e-05, -1.17274e-07,
164  -8.62169e-05, -1.17274e-07, 0.000108955};
165  const CMatrixDouble33 COV(cov_vals);
166 
167  const double x_vals[3] = {0.0135442, 0.00504134, -0.000452334};
168  const CMatrixDouble31 x(x_vals);
169 
170  double out_maha2, out_ml;
171  mrpt::math::mahalanobisDistance2AndLogPDF(x, COV, out_maha2, out_ml);
172 
173  EXPECT_NEAR(out_maha2, 0.388264, 1e-4);
174  EXPECT_NEAR(out_ml, 9.14118, 1e-4);
175 }
A namespace of pseudo-random numbers generators of diferent distributions.
TEST(distributions, normalPDF_1d)
double normalCDF(double p)
Evaluates the Gaussian cumulative density function.
Definition: math.cpp:134
STL namespace.
This base provides a set of functions for maths stuff.
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:528
void mahalanobisDistance2AndLogPDF(const VECLIKE &diff_mean, const MATRIXLIKE &cov, T &maha2_out, T &log_pdf_out)
Computes both, the logarithm of the PDF and the square Mahalanobis distance between a point (given by...
Definition: data_utils.h:209
double chi2PDF(unsigned int degreesOfFreedom, double arg, double accuracy=1e-7)
Definition: math.cpp:595
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:42
EXPECT_NEAR(out.cam_params.rightCameraPose.x, 0.1194, 0.005)
double normalPDF(double x, double mu, double std)
Evaluates the univariate normal (Gaussian) distribution at a given point "x".
Definition: math.cpp:33



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020