00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <cstddef>
00031 #include <cmath>
00032 #include <Eigen/Geometry>
00033
00034 #include <gtest/gtest.h>
00035
00036 #include <mcl_3dl/nd.h>
00037
00038 TEST(NormalLiklihood, Normality)
00039 {
00040 for (double sigma = 1.0; sigma <= 3.0; sigma += 1.0)
00041 {
00042
00043 mcl_3dl::NormalLikelihood<double> nl(sigma);
00044 const double likelihood0 = 1.0 / sqrtf(M_PI * 2.0 * sigma * sigma);
00045 ASSERT_NEAR(nl(0.0), likelihood0, 1e-6);
00046 ASSERT_NEAR(nl(sigma), likelihood0 * 0.60653066, 1e-6);
00047 ASSERT_NEAR(nl(-sigma), likelihood0 * 0.60653066, 1e-6);
00048 ASSERT_NEAR(nl(3.0 * sigma), likelihood0 * 0.011108997, 1e-6);
00049 ASSERT_NEAR(nl(-3.0 * sigma), likelihood0 * 0.011108997, 1e-6);
00050
00051
00052 double sum(0.0);
00053 const double step = 0.1;
00054 for (double i = -100.0; i < 100.0; i += step)
00055 {
00056 sum += nl(i) * step;
00057 }
00058 ASSERT_NEAR(sum, 1.0, 1e-6);
00059 }
00060 }
00061
00062 TEST(NormalLiklihood, NormalityNd)
00063 {
00064 for (double sigma = 1.0; sigma <= 3.0; sigma += 1.0)
00065 {
00066
00067 using NormalLikelihood2d = mcl_3dl::NormalLikelihoodNd<double, 2>;
00068 NormalLikelihood2d::Matrix cov;
00069 cov << sigma, 0,
00070 0, sigma * 2;
00071 NormalLikelihood2d nl(cov);
00072
00073 const double likelihood0 = 1.0 / (M_PI * 2.0 * sqrt(cov.determinant()));
00074 ASSERT_NEAR(
00075 nl(NormalLikelihood2d::Vector(0.0, 0.0)), likelihood0, 1e-6);
00076
00077 const double e1a =
00078 exp(-0.5 *
00079 NormalLikelihood2d::Vector(sigma, 0.0).transpose() *
00080 cov.inverse() *
00081 NormalLikelihood2d::Vector(sigma, 0.0));
00082 const double e1b =
00083 exp(-0.5 *
00084 NormalLikelihood2d::Vector(0.0, sigma).transpose() *
00085 cov.inverse() *
00086 NormalLikelihood2d::Vector(0.0, sigma));
00087 ASSERT_NEAR(
00088 nl(NormalLikelihood2d::Vector(sigma, 0.0)), likelihood0 * e1a, 1e-6);
00089 ASSERT_NEAR(
00090 nl(NormalLikelihood2d::Vector(-sigma, 0.0)), likelihood0 * e1a, 1e-6);
00091 ASSERT_NEAR(
00092 nl(NormalLikelihood2d::Vector(0.0, sigma)), likelihood0 * e1b, 1e-6);
00093 ASSERT_NEAR(
00094 nl(NormalLikelihood2d::Vector(0.0, -sigma)), likelihood0 * e1b, 1e-6);
00095
00096 const double e2a =
00097 exp(-0.5 *
00098 NormalLikelihood2d::Vector(3.0 * sigma, 0.0).transpose() *
00099 cov.inverse() *
00100 NormalLikelihood2d::Vector(3.0 * sigma, 0.0));
00101 const double e2b =
00102 exp(-0.5 *
00103 NormalLikelihood2d::Vector(0.0, 3.0 * sigma).transpose() *
00104 cov.inverse() *
00105 NormalLikelihood2d::Vector(0.0, 3.0 * sigma));
00106 ASSERT_NEAR(
00107 nl(NormalLikelihood2d::Vector(3.0 * sigma, 0.0)), likelihood0 * e2a, 1e-6);
00108 ASSERT_NEAR(
00109 nl(NormalLikelihood2d::Vector(-3.0 * sigma, 0.0)), likelihood0 * e2a, 1e-6);
00110 ASSERT_NEAR(
00111 nl(NormalLikelihood2d::Vector(0.0, 3.0 * sigma)), likelihood0 * e2b, 1e-6);
00112 ASSERT_NEAR(
00113 nl(NormalLikelihood2d::Vector(0.0, -3.0 * sigma)), likelihood0 * e2b, 1e-6);
00114
00115
00116 double sum(0.0);
00117 const double step = 0.1;
00118 const double step_sq = step * step;
00119 for (double i = -100.0; i < 100.0; i += step)
00120 {
00121 for (double j = -100.0; j < 100.0; j += step)
00122 {
00123 sum += nl(NormalLikelihood2d::Vector(i, j)) * step_sq;
00124 }
00125 }
00126 ASSERT_NEAR(sum, 1.0, 1e-6);
00127 }
00128 for (double sigma = 1.0; sigma <= 3.0; sigma += 1.0)
00129 {
00130
00131 using NormalLikelihood2d = mcl_3dl::NormalLikelihoodNd<double, 2>;
00132 NormalLikelihood2d::Matrix cov;
00133 cov << sigma, 0,
00134 0, sigma;
00135 NormalLikelihood2d nl(cov);
00136
00137 const double likelihood0 = 1.0 / (M_PI * 2.0 * sqrt(cov.determinant()));
00138 const double e1 =
00139 exp(-0.5 *
00140 NormalLikelihood2d::Vector(sigma, 0.0).transpose() *
00141 cov.inverse() *
00142 NormalLikelihood2d::Vector(sigma, 0.0));
00143 const double e2 =
00144 exp(-0.5 *
00145 NormalLikelihood2d::Vector(3.0 * sigma, 0.0).transpose() *
00146 cov.inverse() *
00147 NormalLikelihood2d::Vector(3.0 * sigma, 0.0));
00148 for (double r = 0; r < M_PI * 2; r += M_PI / 6)
00149 {
00150 const auto v1 =
00151 Eigen::Rotation2D<double>(r) *
00152 NormalLikelihood2d::Vector(sigma, 0.0);
00153 ASSERT_NEAR(nl(v1), likelihood0 * e1, 1e-6);
00154 const auto v2 =
00155 Eigen::Rotation2D<double>(r) *
00156 NormalLikelihood2d::Vector(3.0 * sigma, 0.0);
00157 ASSERT_NEAR(nl(v2), likelihood0 * e2, 1e-6);
00158 }
00159 }
00160 }
00161
00162 int main(int argc, char** argv)
00163 {
00164 testing::InitGoogleTest(&argc, argv);
00165
00166 return RUN_ALL_TESTS();
00167 }