test_nd.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2016-2017, the mcl_3dl authors
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the copyright holder nor the names of its 
00014  *       contributors may be used to endorse or promote products derived from 
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
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     // Check distribution
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     // Check integrated value
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     // Check distribution
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     // Check integrated value
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     // Check distribution
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 }


mcl_3dl
Author(s): Atsushi Watanabe
autogenerated on Thu Jun 20 2019 20:04:43