test_nd.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2017, the mcl_3dl authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <cstddef>
31 #include <cmath>
32 #include <Eigen/Geometry>
33 
34 #include <gtest/gtest.h>
35 
36 #include <mcl_3dl/nd.h>
37 
38 TEST(NormalLiklihood, Normality)
39 {
40  for (double sigma = 1.0; sigma <= 3.0; sigma += 1.0)
41  {
42  // Check distribution
44  const double likelihood0 = 1.0 / sqrtf(M_PI * 2.0 * sigma * sigma);
45  ASSERT_NEAR(nl(0.0), likelihood0, 1e-6);
46  ASSERT_NEAR(nl(sigma), likelihood0 * 0.60653066, 1e-6);
47  ASSERT_NEAR(nl(-sigma), likelihood0 * 0.60653066, 1e-6);
48  ASSERT_NEAR(nl(3.0 * sigma), likelihood0 * 0.011108997, 1e-6);
49  ASSERT_NEAR(nl(-3.0 * sigma), likelihood0 * 0.011108997, 1e-6);
50 
51  // Check integrated value
52  double sum(0.0);
53  const double step = 0.1;
54  for (double i = -100.0; i < 100.0; i += step)
55  {
56  sum += nl(i) * step;
57  }
58  ASSERT_NEAR(sum, 1.0, 1e-6);
59  }
60 }
61 
62 TEST(NormalLiklihood, NormalityNd)
63 {
64  for (double sigma = 1.0; sigma <= 3.0; sigma += 1.0)
65  {
66  // Check distribution
67  using NormalLikelihood2d = mcl_3dl::NormalLikelihoodNd<double, 2>;
68  NormalLikelihood2d::Matrix cov;
69  cov << sigma, 0,
70  0, sigma * 2;
71  NormalLikelihood2d nl(cov);
72 
73  const double likelihood0 = 1.0 / (M_PI * 2.0 * sqrt(cov.determinant()));
74  ASSERT_NEAR(
75  nl(NormalLikelihood2d::Vector(0.0, 0.0)), likelihood0, 1e-6);
76 
77  const double e1a =
78  exp(-0.5 *
79  NormalLikelihood2d::Vector(sigma, 0.0).transpose() *
80  cov.inverse() *
81  NormalLikelihood2d::Vector(sigma, 0.0));
82  const double e1b =
83  exp(-0.5 *
84  NormalLikelihood2d::Vector(0.0, sigma).transpose() *
85  cov.inverse() *
86  NormalLikelihood2d::Vector(0.0, sigma));
87  ASSERT_NEAR(
88  nl(NormalLikelihood2d::Vector(sigma, 0.0)), likelihood0 * e1a, 1e-6);
89  ASSERT_NEAR(
90  nl(NormalLikelihood2d::Vector(-sigma, 0.0)), likelihood0 * e1a, 1e-6);
91  ASSERT_NEAR(
92  nl(NormalLikelihood2d::Vector(0.0, sigma)), likelihood0 * e1b, 1e-6);
93  ASSERT_NEAR(
94  nl(NormalLikelihood2d::Vector(0.0, -sigma)), likelihood0 * e1b, 1e-6);
95 
96  const double e2a =
97  exp(-0.5 *
98  NormalLikelihood2d::Vector(3.0 * sigma, 0.0).transpose() *
99  cov.inverse() *
100  NormalLikelihood2d::Vector(3.0 * sigma, 0.0));
101  const double e2b =
102  exp(-0.5 *
103  NormalLikelihood2d::Vector(0.0, 3.0 * sigma).transpose() *
104  cov.inverse() *
105  NormalLikelihood2d::Vector(0.0, 3.0 * sigma));
106  ASSERT_NEAR(
107  nl(NormalLikelihood2d::Vector(3.0 * sigma, 0.0)), likelihood0 * e2a, 1e-6);
108  ASSERT_NEAR(
109  nl(NormalLikelihood2d::Vector(-3.0 * sigma, 0.0)), likelihood0 * e2a, 1e-6);
110  ASSERT_NEAR(
111  nl(NormalLikelihood2d::Vector(0.0, 3.0 * sigma)), likelihood0 * e2b, 1e-6);
112  ASSERT_NEAR(
113  nl(NormalLikelihood2d::Vector(0.0, -3.0 * sigma)), likelihood0 * e2b, 1e-6);
114 
115  // Check integrated value
116  double sum(0.0);
117  const double step = 0.1;
118  const double step_sq = step * step;
119  for (double i = -100.0; i < 100.0; i += step)
120  {
121  for (double j = -100.0; j < 100.0; j += step)
122  {
123  sum += nl(NormalLikelihood2d::Vector(i, j)) * step_sq;
124  }
125  }
126  ASSERT_NEAR(sum, 1.0, 1e-6);
127  }
128  for (double sigma = 1.0; sigma <= 3.0; sigma += 1.0)
129  {
130  // Check distribution
131  using NormalLikelihood2d = mcl_3dl::NormalLikelihoodNd<double, 2>;
132  NormalLikelihood2d::Matrix cov;
133  cov << sigma, 0,
134  0, sigma;
135  NormalLikelihood2d nl(cov);
136 
137  const double likelihood0 = 1.0 / (M_PI * 2.0 * sqrt(cov.determinant()));
138  const double e1 =
139  exp(-0.5 *
140  NormalLikelihood2d::Vector(sigma, 0.0).transpose() *
141  cov.inverse() *
142  NormalLikelihood2d::Vector(sigma, 0.0));
143  const double e2 =
144  exp(-0.5 *
145  NormalLikelihood2d::Vector(3.0 * sigma, 0.0).transpose() *
146  cov.inverse() *
147  NormalLikelihood2d::Vector(3.0 * sigma, 0.0));
148  for (double r = 0; r < M_PI * 2; r += M_PI / 6)
149  {
150  const auto v1 =
151  Eigen::Rotation2D<double>(r) *
152  NormalLikelihood2d::Vector(sigma, 0.0);
153  ASSERT_NEAR(nl(v1), likelihood0 * e1, 1e-6);
154  const auto v2 =
155  Eigen::Rotation2D<double>(r) *
156  NormalLikelihood2d::Vector(3.0 * sigma, 0.0);
157  ASSERT_NEAR(nl(v2), likelihood0 * e2, 1e-6);
158  }
159  }
160 }
161 
162 int main(int argc, char** argv)
163 {
164  testing::InitGoogleTest(&argc, argv);
165 
166  return RUN_ALL_TESTS();
167 }
int main(int argc, char **argv)
Definition: test_nd.cpp:162
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
INLINE Rall1d< T, V, S > exp(const Rall1d< T, V, S > &arg)
unsigned int step
TEST(NormalLiklihood, Normality)
Definition: test_nd.cpp:38


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Mon Jul 8 2019 03:32:36