<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>distributions.h Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> </head><body> <div align="left"><a href="http://www.mrpt.org/">Main MRPT website</a> > <b>C++ reference</b> </div> <div align="right"> <a href="index.html"><img border="0" src="mrpt_logo.png" alt="MRPT logo"></a> </div> <!-- Generated by Doxygen 1.7.5 --> <script type="text/javascript"> var searchBox = new SearchBox("searchBox", "search",false,'Search'); </script> <div id="navrow1" class="tabs"> <ul class="tablist"> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li> <div id="MSearchBox" class="MSearchBoxInactive"> <div class="left"> <form id="FSearchBox" action="search.php" method="get"> <img id="MSearchSelect" src="search/mag.png" alt=""/> <input type="text" id="MSearchField" name="query" value="Search" size="20" accesskey="S" onfocus="searchBox.OnSearchFieldFocus(true)" onblur="searchBox.OnSearchFieldFocus(false)"/> </form> </div><div class="right"></div> </div> </li> </ul> </div> <div id="navrow2" class="tabs2"> <ul class="tablist"> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <div class="header"> <div class="headertitle"> <div class="title">distributions.h</div> </div> </div> <div class="contents"> <a href="distributions_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* +---------------------------------------------------------------------------+</span> <a name="l00002"></a>00002 <span class="comment"> | The Mobile Robot Programming Toolkit (MRPT) C++ library |</span> <a name="l00003"></a>00003 <span class="comment"> | |</span> <a name="l00004"></a>00004 <span class="comment"> | http://www.mrpt.org/ |</span> <a name="l00005"></a>00005 <span class="comment"> | |</span> <a name="l00006"></a>00006 <span class="comment"> | Copyright (C) 2005-2011 University of Malaga |</span> <a name="l00007"></a>00007 <span class="comment"> | |</span> <a name="l00008"></a>00008 <span class="comment"> | This software was written by the Machine Perception and Intelligent |</span> <a name="l00009"></a>00009 <span class="comment"> | Robotics Lab, University of Malaga (Spain). |</span> <a name="l00010"></a>00010 <span class="comment"> | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |</span> <a name="l00011"></a>00011 <span class="comment"> | |</span> <a name="l00012"></a>00012 <span class="comment"> | This file is part of the MRPT project. |</span> <a name="l00013"></a>00013 <span class="comment"> | |</span> <a name="l00014"></a>00014 <span class="comment"> | MRPT is free software: you can redistribute it and/or modify |</span> <a name="l00015"></a>00015 <span class="comment"> | it under the terms of the GNU General Public License as published by |</span> <a name="l00016"></a>00016 <span class="comment"> | the Free Software Foundation, either version 3 of the License, or |</span> <a name="l00017"></a>00017 <span class="comment"> | (at your option) any later version. |</span> <a name="l00018"></a>00018 <span class="comment"> | |</span> <a name="l00019"></a>00019 <span class="comment"> | MRPT is distributed in the hope that it will be useful, |</span> <a name="l00020"></a>00020 <span class="comment"> | but WITHOUT ANY WARRANTY; without even the implied warranty of |</span> <a name="l00021"></a>00021 <span class="comment"> | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |</span> <a name="l00022"></a>00022 <span class="comment"> | GNU General Public License for more details. |</span> <a name="l00023"></a>00023 <span class="comment"> | |</span> <a name="l00024"></a>00024 <span class="comment"> | You should have received a copy of the GNU General Public License |</span> <a name="l00025"></a>00025 <span class="comment"> | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |</span> <a name="l00026"></a>00026 <span class="comment"> | |</span> <a name="l00027"></a>00027 <span class="comment"> +---------------------------------------------------------------------------+ */</span> <a name="l00028"></a>00028 <span class="preprocessor">#ifndef mrpt_math_distributions_H</span> <a name="l00029"></a>00029 <span class="preprocessor"></span><span class="preprocessor">#define mrpt_math_distributions_H</span> <a name="l00030"></a>00030 <span class="preprocessor"></span> <a name="l00031"></a>00031 <span class="preprocessor">#include <<a class="code" href="utils__defs_8h.html">mrpt/utils/utils_defs.h</a>></span> <a name="l00032"></a>00032 <span class="preprocessor">#include <<a class="code" href="math__frwds_8h.html">mrpt/math/math_frwds.h</a>></span> <a name="l00033"></a>00033 <span class="preprocessor">#include <<a class="code" href="_c_matrix_template_numeric_8h.html">mrpt/math/CMatrixTemplateNumeric.h</a>></span> <a name="l00034"></a>00034 <a name="l00035"></a>00035 <span class="preprocessor">#include <<a class="code" href="ops__matrices_8h.html" title="This file implements miscelaneous matrix and matrix/vector operations, plus internal functions in mrp...">mrpt/math/ops_matrices.h</a>></span> <a name="l00036"></a>00036 <a name="l00037"></a>00037 <span class="comment">/*---------------------------------------------------------------</span> <a name="l00038"></a>00038 <span class="comment"> Namespace</span> <a name="l00039"></a>00039 <span class="comment"> ---------------------------------------------------------------*/</span> <a name="l00040"></a>00040 <span class="keyword">namespace </span>mrpt <a name="l00041"></a>00041 { <a name="l00042"></a>00042 <span class="keyword">namespace </span>math <a name="l00043"></a>00043 { <a name="l00044"></a>00044 <span class="keyword">using namespace </span>mrpt::utils; <a name="l00045"></a>00045 <span class="comment"></span> <a name="l00046"></a>00046 <span class="comment"> /** \addtogroup stats_grp Statistics functions, probability distributions</span> <a name="l00047"></a>00047 <span class="comment"> * \ingroup mrpt_base_grp</span> <a name="l00048"></a>00048 <span class="comment"> * @{ */</span> <a name="l00049"></a>00049 <span class="comment"></span> <a name="l00050"></a>00050 <span class="comment"> /** Evaluates the univariate normal (Gaussian) distribution at a given point "x".</span> <a name="l00051"></a>00051 <span class="comment"> */</span> <a name="l00052"></a>00052 <span class="keywordtype">double</span> <a class="code" href="base_2include_2mrpt_2base_2link__pragmas_8h.html#a6045fa0129b1a3d6c8bf895470e66574">BASE_IMPEXP</a> <a class="code" href="group__stats__grp.html#ga2ec8bf7897f7b8b0fee1ca9ae46443bb" title="Evaluates the univariate normal (Gaussian) distribution at a given point "x".">normalPDF</a>(<span class="keywordtype">double</span> x, <span class="keywordtype">double</span> mu, <span class="keywordtype">double</span> std); <a name="l00053"></a>00053 <span class="comment"></span> <a name="l00054"></a>00054 <span class="comment"> /** Evaluates the multivariate normal (Gaussian) distribution at a given point "x".</span> <a name="l00055"></a>00055 <span class="comment"> * \param x A vector or column or row matrix with the point at which to evaluate the pdf.</span> <a name="l00056"></a>00056 <span class="comment"> * \param mu A vector or column or row matrix with the Gaussian mean.</span> <a name="l00057"></a>00057 <span class="comment"> * \param cov The covariance matrix of the Gaussian.</span> <a name="l00058"></a>00058 <span class="comment"> * \param scaled_pdf If set to true, the PDF will be scaled to be in the range [0,1].</span> <a name="l00059"></a>00059 <span class="comment"> */</span> <a name="l00060"></a>00060 <span class="keyword">template</span> <<span class="keyword">class</span> VECTORLIKE1,<span class="keyword">class</span> VECTORLIKE2,<span class="keyword">class</span> MATRIXLIKE> <a name="l00061"></a>00061 <span class="keyword">inline</span> <span class="keyword">typename</span> MATRIXLIKE<a class="code" href="eigen__plugins_8h.html#afd07186978da46f9908364e389f8a403" title="Type of the elements.">::value_type</a> <a name="l00062"></a><a class="code" href="group__stats__grp.html#gadeb42799aea7897137bfb75ea7de4ebf">00062</a> <a class="code" href="group__stats__grp.html#ga2ec8bf7897f7b8b0fee1ca9ae46443bb" title="Evaluates the univariate normal (Gaussian) distribution at a given point "x".">normalPDF</a>( <a name="l00063"></a>00063 <span class="keyword">const</span> VECTORLIKE1 & x, <a name="l00064"></a>00064 <span class="keyword">const</span> VECTORLIKE2 & mu, <a name="l00065"></a>00065 <span class="keyword">const</span> MATRIXLIKE & <a class="code" href="namespacemrpt_1_1math.html#a43f4e051fc574fd75b6800ad4fb25037" title="Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...">cov</a>, <a name="l00066"></a>00066 <span class="keyword">const</span> <span class="keywordtype">bool</span> scaled_pdf = <span class="keyword">false</span> ) <a name="l00067"></a>00067 { <a name="l00068"></a>00068 <a class="code" href="mrpt__macros_8h.html#a45b840af519f33816311acdbb28d7c10">MRPT_START</a> <a name="l00069"></a>00069 <span class="keyword">typedef</span> <span class="keyword">typename</span> MATRIXLIKE<a class="code" href="eigen__plugins_8h.html#afd07186978da46f9908364e389f8a403" title="Type of the elements.">::value_type</a> T; <a name="l00070"></a>00070 <a class="code" href="mrpt__macros_8h.html#a5ad4d8d68e2f6664f247407bf89aac55" title="Defines an assertion mechanism - only when compiled in debug.">ASSERTDEB_</a>(cov.isSquare()) <a name="l00071"></a>00071 <a class="code" href="mrpt__macros_8h.html#a5ad4d8d68e2f6664f247407bf89aac55" title="Defines an assertion mechanism - only when compiled in debug.">ASSERTDEB_</a>(<span class="keywordtype">size_t</span>(cov.getColCount())==<span class="keywordtype">size_t</span>(x.size()) && <span class="keywordtype">size_t</span>(cov.getColCount())==<span class="keywordtype">size_t</span>(mu.size())) <a name="l00072"></a>00072 T ret = ::exp( static_cast<T>(-0.5) * <a class="code" href="namespacemrpt_1_1math.html#aa8357cce481e22376ecadae393167977" title="r (a scalar) = H * C * H^t (with a vector H and a symmetric matrix C)">mrpt::math::multiply_HCHt_scalar</a>((x-mu), cov.inverse() ) ); <a name="l00073"></a>00073 <span class="keywordflow">return</span> scaled_pdf ? ret : ret / (::pow(static_cast<T>(<a class="code" href="mrpt__macros_8h.html#a4aa2c43b09c1300c334821f5507d6f71">M_2PI</a>),static_cast<T>( <a class="code" href="namespacemrpt_1_1math.html#a632ae0aecf78103f87f18f9ac33f7170">size</a>(cov,1) )) * ::sqrt(cov.det())); <a name="l00074"></a>00074 <a class="code" href="mrpt__macros_8h.html#a88a917260793b56abd83ad2a0d849eb1">MRPT_END</a> <a name="l00075"></a>00075 } <a name="l00076"></a>00076 <span class="comment"></span> <a name="l00077"></a>00077 <span class="comment"> /** Evaluates the multivariate normal (Gaussian) distribution at a given point given its distance vector "d" from the Gaussian mean.</span> <a name="l00078"></a>00078 <span class="comment"> */</span> <a name="l00079"></a>00079 <span class="keyword">template</span> <<span class="keyword">typename</span> VECTORLIKE,<span class="keyword">typename</span> MATRIXLIKE> <a name="l00080"></a>00080 <span class="keyword">typename</span> MATRIXLIKE<a class="code" href="eigen__plugins_8h.html#afd07186978da46f9908364e389f8a403" title="Type of the elements.">::value_type</a> <a name="l00081"></a><a class="code" href="group__stats__grp.html#gac921a40dc9d7a5d14fb8333300111e86">00081</a> <a class="code" href="group__stats__grp.html#ga2ec8bf7897f7b8b0fee1ca9ae46443bb" title="Evaluates the univariate normal (Gaussian) distribution at a given point "x".">normalPDF</a>(<span class="keyword">const</span> VECTORLIKE &d,<span class="keyword">const</span> MATRIXLIKE &<a class="code" href="namespacemrpt_1_1math.html#a43f4e051fc574fd75b6800ad4fb25037" title="Computes the covariance matrix from a list of samples in an NxM matrix, where each row is a sample...">cov</a>) <a name="l00082"></a>00082 { <a name="l00083"></a>00083 <a class="code" href="mrpt__macros_8h.html#a45b840af519f33816311acdbb28d7c10">MRPT_START</a> <a name="l00084"></a>00084 <a class="code" href="mrpt__macros_8h.html#a5ad4d8d68e2f6664f247407bf89aac55" title="Defines an assertion mechanism - only when compiled in debug.">ASSERTDEB_</a>(cov.isSquare()) <a name="l00085"></a>00085 <a class="code" href="mrpt__macros_8h.html#a5ad4d8d68e2f6664f247407bf89aac55" title="Defines an assertion mechanism - only when compiled in debug.">ASSERTDEB_</a>(<span class="keywordtype">size_t</span>(cov.getColCount())==<span class="keywordtype">size_t</span>(d.size())) <a name="l00086"></a>00086 <span class="keywordflow">return</span> std::exp( static_cast<typename MATRIXLIKE::value_type>(-0.5)*<a class="code" href="namespacemrpt_1_1math.html#aa8357cce481e22376ecadae393167977" title="r (a scalar) = H * C * H^t (with a vector H and a symmetric matrix C)">mrpt::math::multiply_HCHt_scalar</a>(d,cov.inverse())) <a name="l00087"></a>00087 / (::pow( <a name="l00088"></a>00088 static_cast<typename MATRIXLIKE::value_type>(<a class="code" href="mrpt__macros_8h.html#a4aa2c43b09c1300c334821f5507d6f71">M_2PI</a>), <a name="l00089"></a>00089 static_cast<typename MATRIXLIKE::value_type>(cov.getColCount())) <a name="l00090"></a>00090 * ::sqrt(cov.det())); <a name="l00091"></a>00091 <a class="code" href="mrpt__macros_8h.html#a88a917260793b56abd83ad2a0d849eb1">MRPT_END</a> <a name="l00092"></a>00092 } <a name="l00093"></a>00093 <span class="comment"></span> <a name="l00094"></a>00094 <span class="comment"> /** Kullback-Leibler divergence (KLD) between two independent multivariate Gaussians.</span> <a name="l00095"></a>00095 <span class="comment"> *</span> <a name="l00096"></a>00096 <span class="comment"> * \f$ D_\mathrm{KL}(\mathcal{N}_0 \| \mathcal{N}_1) = { 1 \over 2 } ( \log_e ( { \det \Sigma_1 \over \det \Sigma_0 } ) + \mathrm{tr} ( \Sigma_1^{-1} \Sigma_0 ) + ( \mu_1 - \mu_0 )^\top \Sigma_1^{-1} ( \mu_1 - \mu_0 ) - N ) \f$</span> <a name="l00097"></a>00097 <span class="comment"> */</span> <a name="l00098"></a>00098 <span class="keyword">template</span> <<span class="keyword">typename</span> VECTORLIKE1,<span class="keyword">typename</span> MATRIXLIKE1,<span class="keyword">typename</span> VECTORLIKE2,<span class="keyword">typename</span> MATRIXLIKE2> <a name="l00099"></a><a class="code" href="group__stats__grp.html#ga9ddb4b383120316162071a6c125537ef">00099</a> <span class="keywordtype">double</span> <a class="code" href="group__stats__grp.html#ga9ddb4b383120316162071a6c125537ef" title="Kullback-Leibler divergence (KLD) between two independent multivariate Gaussians.">KLD_Gaussians</a>( <a name="l00100"></a>00100 <span class="keyword">const</span> VECTORLIKE1 &mu0, <span class="keyword">const</span> MATRIXLIKE1 &cov0, <a name="l00101"></a>00101 <span class="keyword">const</span> VECTORLIKE2 &mu1, <span class="keyword">const</span> MATRIXLIKE2 &cov1) <a name="l00102"></a>00102 { <a name="l00103"></a>00103 <a class="code" href="mrpt__macros_8h.html#a45b840af519f33816311acdbb28d7c10">MRPT_START</a> <a name="l00104"></a>00104 <a class="code" href="mrpt__macros_8h.html#a47eb5a445c2bf3d9190396510ea9683e">ASSERT_</a>(<span class="keywordtype">size_t</span>(mu0.size())==<span class="keywordtype">size_t</span>(mu1.size()) && <span class="keywordtype">size_t</span>(mu0.size())==<span class="keywordtype">size_t</span>(<a class="code" href="namespacemrpt_1_1math.html#a632ae0aecf78103f87f18f9ac33f7170">size</a>(cov0,1)) && <span class="keywordtype">size_t</span>(mu0.size())==<span class="keywordtype">size_t</span>(<a class="code" href="namespacemrpt_1_1math.html#a632ae0aecf78103f87f18f9ac33f7170">size</a>(cov1,1)) && cov0.isSquare() && cov1.isSquare() ) <a name="l00105"></a>00105 <span class="keyword">const</span> <span class="keywordtype">size_t</span> N = mu0.size(); <a name="l00106"></a>00106 MATRIXLIKE2 cov1_inv; <a name="l00107"></a>00107 cov1.inv(cov1_inv); <a name="l00108"></a>00108 <span class="keyword">const</span> VECTORLIKE1 mu_difs = mu0-mu1; <a name="l00109"></a>00109 <span class="keywordflow">return</span> 0.5*( log(cov1.det()/cov0.det()) + (cov1_inv*cov0).trace() + <a class="code" href="namespacemrpt_1_1math.html#aa8357cce481e22376ecadae393167977" title="r (a scalar) = H * C * H^t (with a vector H and a symmetric matrix C)">multiply_HCHt_scalar</a>(mu_difs,cov1_inv) - N ); <a name="l00110"></a>00110 <a class="code" href="mrpt__macros_8h.html#a88a917260793b56abd83ad2a0d849eb1">MRPT_END</a> <a name="l00111"></a>00111 } <a name="l00112"></a>00112 <a name="l00113"></a>00113 <span class="comment"></span> <a name="l00114"></a>00114 <span class="comment"> /** The complementary error function of a Normal distribution</span> <a name="l00115"></a>00115 <span class="comment"> */</span> <a name="l00116"></a>00116 <span class="preprocessor">#ifdef HAVE_ERF</span> <a name="l00117"></a>00117 <span class="preprocessor"></span> <span class="keyword">inline</span> <span class="keywordtype">double</span> <a class="code" href="group__stats__grp.html#ga700b695a783f89132853f9a3cbf37abf" title="The complementary error function of a Normal distribution.">erfc</a>(<span class="keywordtype">double</span> x) { <a class="code" href="group__stats__grp.html#ga700b695a783f89132853f9a3cbf37abf" title="The complementary error function of a Normal distribution.">return ::erfc</a>(x); } <a name="l00118"></a>00118 <span class="preprocessor">#else</span> <a name="l00119"></a>00119 <span class="preprocessor"></span> <span class="keywordtype">double</span> <a class="code" href="base_2include_2mrpt_2base_2link__pragmas_8h.html#a6045fa0129b1a3d6c8bf895470e66574">BASE_IMPEXP</a> <a class="code" href="group__stats__grp.html#ga700b695a783f89132853f9a3cbf37abf" title="The complementary error function of a Normal distribution.">erfc</a>(<span class="keywordtype">double</span> x); <a name="l00120"></a>00120 <span class="preprocessor">#endif</span> <a name="l00121"></a>00121 <span class="preprocessor"></span><span class="comment"></span> <a name="l00122"></a>00122 <span class="comment"> /** The error function of a Normal distribution</span> <a name="l00123"></a>00123 <span class="comment"> */</span> <a name="l00124"></a>00124 <span class="preprocessor">#ifdef HAVE_ERF</span> <a name="l00125"></a>00125 <span class="preprocessor"></span> <span class="keyword">inline</span> <span class="keywordtype">double</span> <a class="code" href="group__stats__grp.html#ga31b77faefe845a607e6661c2a24e7e5f" title="The error function of a Normal distribution.">erf</a>(<span class="keywordtype">double</span> x) { <a class="code" href="group__stats__grp.html#ga31b77faefe845a607e6661c2a24e7e5f" title="The error function of a Normal distribution.">return ::erf</a>(x); } <a name="l00126"></a>00126 <span class="preprocessor">#else</span> <a name="l00127"></a>00127 <span class="preprocessor"></span> <span class="keywordtype">double</span> <a class="code" href="base_2include_2mrpt_2base_2link__pragmas_8h.html#a6045fa0129b1a3d6c8bf895470e66574">BASE_IMPEXP</a> <a class="code" href="group__stats__grp.html#ga31b77faefe845a607e6661c2a24e7e5f" title="The error function of a Normal distribution.">erf</a>(<span class="keywordtype">double</span> x); <a name="l00128"></a>00128 <span class="preprocessor">#endif</span> <a name="l00129"></a>00129 <span class="preprocessor"></span><span class="comment"> /** Evaluates the Gaussian distribution quantile for the probability value p=[0,1].</span> <a name="l00130"></a>00130 <span class="comment"> * The employed approximation is that from Peter J. Acklam (pjacklam@online.no),</span> <a name="l00131"></a>00131 <span class="comment"> * freely available in http://home.online.no/~pjacklam.</span> <a name="l00132"></a>00132 <span class="comment"> */</span> <a name="l00133"></a>00133 <span class="keywordtype">double</span> <a class="code" href="base_2include_2mrpt_2base_2link__pragmas_8h.html#a6045fa0129b1a3d6c8bf895470e66574">BASE_IMPEXP</a> <a class="code" href="group__stats__grp.html#gae1e6f25a0c6a318d6edd2bc9ef124bed" title="Evaluates the Gaussian distribution quantile for the probability value p=[0,1].">normalQuantile</a>(<span class="keywordtype">double</span> p); <a name="l00134"></a>00134 <span class="comment"></span> <a name="l00135"></a>00135 <span class="comment"> /** Evaluates the Gaussian cumulative density function.</span> <a name="l00136"></a>00136 <span class="comment"> * The employed approximation is that from W. J. Cody</span> <a name="l00137"></a>00137 <span class="comment"> * freely available in http://www.netlib.org/specfun/erf</span> <a name="l00138"></a>00138 <span class="comment"> */</span> <a name="l00139"></a>00139 <span class="keywordtype">double</span> <a class="code" href="base_2include_2mrpt_2base_2link__pragmas_8h.html#a6045fa0129b1a3d6c8bf895470e66574">BASE_IMPEXP</a> <a class="code" href="group__stats__grp.html#ga09f5b985d95b570e05c2b9cef91865b0" title="Evaluates the Gaussian cumulative density function.">normalCDF</a>(<span class="keywordtype">double</span> p); <a name="l00140"></a>00140 <span class="comment"></span> <a name="l00141"></a>00141 <span class="comment"> /** The "quantile" of the Chi-Square distribution, for dimension "dim" and probability 0<P<1 (the inverse of chi2CDF)</span> <a name="l00142"></a>00142 <span class="comment"> * An aproximation from the Wilson-Hilferty transformation is used.</span> <a name="l00143"></a>00143 <span class="comment"> */</span> <a name="l00144"></a>00144 <span class="keywordtype">double</span> <a class="code" href="base_2include_2mrpt_2base_2link__pragmas_8h.html#a6045fa0129b1a3d6c8bf895470e66574">BASE_IMPEXP</a> <a class="code" href="group__stats__grp.html#ga68b8a2208349a297c19f3a45c24457b1" title="The "quantile" of the Chi-Square distribution, for dimension "dim" and probability 0<P<1 (the inverse...">chi2inv</a>(<span class="keywordtype">double</span> P, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> dim=1); <a name="l00145"></a>00145 <span class="comment"></span> <a name="l00146"></a>00146 <span class="comment"> /*! Cumulative non-central chi square distribution (approximate).</span> <a name="l00147"></a>00147 <span class="comment"></span> <a name="l00148"></a>00148 <span class="comment"> Computes approximate values of the cumulative density of a chi square distribution with \a degreesOfFreedom,</span> <a name="l00149"></a>00149 <span class="comment"> and noncentrality parameter \a noncentrality at the given argument</span> <a name="l00150"></a>00150 <span class="comment"> \a arg, i.e. the probability that a random number drawn from the distribution is below \a arg</span> <a name="l00151"></a>00151 <span class="comment"> It uses the approximate transform into a normal distribution due to Wilson and Hilferty</span> <a name="l00152"></a>00152 <span class="comment"> (see Abramovitz, Stegun: "Handbook of Mathematical Functions", formula 26.3.32).</span> <a name="l00153"></a>00153 <span class="comment"> The algorithm's running time is independent of the inputs. The accuracy is only</span> <a name="l00154"></a>00154 <span class="comment"> about 0.1 for few degrees of freedom, but reaches about 0.001 above dof = 5.</span> <a name="l00155"></a>00155 <span class="comment"></span> <a name="l00156"></a>00156 <span class="comment"> \note Function code from the Vigra project (http://hci.iwr.uni-heidelberg.de/vigra/); code under "MIT X11 License", GNU GPL-compatible.</span> <a name="l00157"></a>00157 <span class="comment"> */</span> <a name="l00158"></a>00158 <span class="keyword">template</span> <<span class="keyword">class</span> T> <a name="l00159"></a><a class="code" href="group__stats__grp.html#ga3bff4b9c33b2b9b2982ce7358cd16039">00159</a> <span class="keywordtype">double</span> <a class="code" href="group__stats__grp.html#ga3bff4b9c33b2b9b2982ce7358cd16039">noncentralChi2CDF</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> degreesOfFreedom, T noncentrality, T arg) <a name="l00160"></a>00160 { <a name="l00161"></a>00161 <span class="keyword">const</span> <span class="keywordtype">double</span> a = degreesOfFreedom + noncentrality; <a name="l00162"></a>00162 <span class="keyword">const</span> <span class="keywordtype">double</span> b = (a + noncentrality) / <a class="code" href="namespacemrpt_1_1utils.html#a67cb05bb8ad4e725875a7ee54b7042ae" title="Inline function for the square of a number.">square</a>(a); <a name="l00163"></a>00163 <span class="keyword">const</span> <span class="keywordtype">double</span> <a class="code" href="eigen__plugins_8h.html#a7b88b312dc3827120dbfc60da344625d" title="Transpose.">t</a> = (std::pow((<span class="keywordtype">double</span>)arg / a, 1.0/3.0) - (1.0 - 2.0 / 9.0 * b)) / std::sqrt(2.0 / 9.0 * b); <a name="l00164"></a>00164 <span class="keywordflow">return</span> 0.5*(1.0 + <a class="code" href="group__stats__grp.html#ga31b77faefe845a607e6661c2a24e7e5f" title="The error function of a Normal distribution.">mrpt::math::erf</a>(t/std::sqrt(2.0))); <a name="l00165"></a>00165 } <a name="l00166"></a>00166 <span class="comment"></span> <a name="l00167"></a>00167 <span class="comment"> /*! Cumulative chi square distribution.</span> <a name="l00168"></a>00168 <span class="comment"></span> <a name="l00169"></a>00169 <span class="comment"> Computes the cumulative density of a chi square distribution with \a degreesOfFreedom</span> <a name="l00170"></a>00170 <span class="comment"> and tolerance \a accuracy at the given argument \a arg, i.e. the probability that</span> <a name="l00171"></a>00171 <span class="comment"> a random number drawn from the distribution is below \a arg</span> <a name="l00172"></a>00172 <span class="comment"> by calling <tt>noncentralChi2CDF(degreesOfFreedom, 0.0, arg, accuracy)</tt>.</span> <a name="l00173"></a>00173 <span class="comment"></span> <a name="l00174"></a>00174 <span class="comment"> \note Function code from the Vigra project (http://hci.iwr.uni-heidelberg.de/vigra/); code under "MIT X11 License", GNU GPL-compatible.</span> <a name="l00175"></a>00175 <span class="comment"> */</span> <a name="l00176"></a><a class="code" href="group__stats__grp.html#gadaf15d0ae96eb360f18107703ff8ffc9">00176</a> <span class="keyword">inline</span> <span class="keywordtype">double</span> <a class="code" href="group__stats__grp.html#gadaf15d0ae96eb360f18107703ff8ffc9">chi2CDF</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> degreesOfFreedom, <span class="keywordtype">double</span> arg) <a name="l00177"></a>00177 { <a name="l00178"></a>00178 <span class="keywordflow">return</span> <a class="code" href="group__stats__grp.html#ga3bff4b9c33b2b9b2982ce7358cd16039">noncentralChi2CDF</a>(degreesOfFreedom, 0.0, arg); <a name="l00179"></a>00179 } <a name="l00180"></a>00180 <a name="l00181"></a>00181 <span class="keyword">namespace </span>detail <a name="l00182"></a>00182 { <a name="l00183"></a>00183 <span class="keyword">template</span> <<span class="keyword">class</span> T> <a name="l00184"></a><a class="code" href="namespacemrpt_1_1math_1_1detail.html#ab0c42732251b23eb7f2c40be3064f830">00184</a> <span class="keywordtype">void</span> <a class="code" href="namespacemrpt_1_1math_1_1detail.html#ab0c42732251b23eb7f2c40be3064f830">noncentralChi2OneIteration</a>(T arg, T & lans, T & dans, T & pans, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> & j) <a name="l00185"></a>00185 { <a name="l00186"></a>00186 <span class="keywordtype">double</span> tol = -50.0; <a name="l00187"></a>00187 <span class="keywordflow">if</span>(lans < tol) <a name="l00188"></a>00188 { <a name="l00189"></a>00189 lans = lans + std::log(arg / j); <a name="l00190"></a>00190 dans = std::exp(lans); <a name="l00191"></a>00191 } <a name="l00192"></a>00192 <span class="keywordflow">else</span> <a name="l00193"></a>00193 { <a name="l00194"></a>00194 dans = dans * arg / j; <a name="l00195"></a>00195 } <a name="l00196"></a>00196 pans = pans - dans; <a name="l00197"></a>00197 j += 2; <a name="l00198"></a>00198 } <a name="l00199"></a>00199 <a name="l00200"></a>00200 <span class="keyword">template</span> <<span class="keyword">class</span> T> <a name="l00201"></a><a class="code" href="namespacemrpt_1_1math_1_1detail.html#a65738e21624fe5ef6f4b645c4dfed0d4">00201</a> std::pair<double, double> <a class="code" href="namespacemrpt_1_1math_1_1detail.html#a65738e21624fe5ef6f4b645c4dfed0d4">noncentralChi2CDF_exact</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> degreesOfFreedom, T noncentrality, T arg, T eps) <a name="l00202"></a>00202 { <a name="l00203"></a>00203 <a class="code" href="mrpt__macros_8h.html#ad30ea0382c594c0e2efe88212e9352b0">ASSERTMSG_</a>(noncentrality >= 0.0 && arg >= 0.0 && eps > 0.0,<span class="stringliteral">"noncentralChi2P(): parameters must be positive."</span>); <a name="l00204"></a>00204 <span class="keywordflow">if</span> (arg == 0.0 && degreesOfFreedom > 0) <a name="l00205"></a>00205 <span class="keywordflow">return</span> std::make_pair(0.0, 0.0); <a name="l00206"></a>00206 <a name="l00207"></a>00207 <span class="comment">// Determine initial values</span> <a name="l00208"></a>00208 <span class="keywordtype">double</span> b1 = 0.5 * noncentrality, <a name="l00209"></a>00209 ao = std::exp(-b1), <a name="l00210"></a>00210 eps2 = eps / ao, <a name="l00211"></a>00211 lnrtpi2 = 0.22579135264473, <a name="l00212"></a>00212 probability, density, lans, dans, pans, <a class="code" href="namespacemrpt_1_1math.html#a0c97e69e10a10499133480daa055e7c4" title="Computes the sum of all the elements.">sum</a>, am, hold; <a name="l00213"></a>00213 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> maxit = 500, <a name="l00214"></a>00214 i, m; <a name="l00215"></a>00215 <span class="keywordflow">if</span>(degreesOfFreedom % 2) <a name="l00216"></a>00216 { <a name="l00217"></a>00217 i = 1; <a name="l00218"></a>00218 lans = -0.5 * (arg + std::log(arg)) - lnrtpi2; <a name="l00219"></a>00219 dans = std::exp(lans); <a name="l00220"></a>00220 pans = <a class="code" href="group__stats__grp.html#ga31b77faefe845a607e6661c2a24e7e5f" title="The error function of a Normal distribution.">erf</a>(std::sqrt(arg/2.0)); <a name="l00221"></a>00221 } <a name="l00222"></a>00222 <span class="keywordflow">else</span> <a name="l00223"></a>00223 { <a name="l00224"></a>00224 i = 2; <a name="l00225"></a>00225 lans = -0.5 * arg; <a name="l00226"></a>00226 dans = std::exp(lans); <a name="l00227"></a>00227 pans = 1.0 - dans; <a name="l00228"></a>00228 } <a name="l00229"></a>00229 <a name="l00230"></a>00230 <span class="comment">// Evaluate first term</span> <a name="l00231"></a>00231 <span class="keywordflow">if</span>(degreesOfFreedom == 0) <a name="l00232"></a>00232 { <a name="l00233"></a>00233 m = 1; <a name="l00234"></a>00234 degreesOfFreedom = 2; <a name="l00235"></a>00235 am = b1; <a name="l00236"></a>00236 sum = 1.0 / ao - 1.0 - am; <a name="l00237"></a>00237 density = am * dans; <a name="l00238"></a>00238 probability = 1.0 + am * pans; <a name="l00239"></a>00239 } <a name="l00240"></a>00240 <span class="keywordflow">else</span> <a name="l00241"></a>00241 { <a name="l00242"></a>00242 m = 0; <a name="l00243"></a>00243 degreesOfFreedom = degreesOfFreedom - 1; <a name="l00244"></a>00244 am = 1.0; <a name="l00245"></a>00245 sum = 1.0 / ao - 1.0; <a name="l00246"></a>00246 <span class="keywordflow">while</span>(i < degreesOfFreedom) <a name="l00247"></a>00247 <a class="code" href="namespacemrpt_1_1math_1_1detail.html#ab0c42732251b23eb7f2c40be3064f830">detail::noncentralChi2OneIteration</a>(arg, lans, dans, pans, i); <a name="l00248"></a>00248 degreesOfFreedom = degreesOfFreedom + 1; <a name="l00249"></a>00249 density = dans; <a name="l00250"></a>00250 probability = pans; <a name="l00251"></a>00251 } <a name="l00252"></a>00252 <span class="comment">// Evaluate successive terms of the expansion</span> <a name="l00253"></a>00253 <span class="keywordflow">for</span>(++m; m<maxit; ++m) <a name="l00254"></a>00254 { <a name="l00255"></a>00255 am = b1 * am / m; <a name="l00256"></a>00256 <a class="code" href="namespacemrpt_1_1math_1_1detail.html#ab0c42732251b23eb7f2c40be3064f830">detail::noncentralChi2OneIteration</a>(arg, lans, dans, pans, degreesOfFreedom); <a name="l00257"></a>00257 sum = sum - am; <a name="l00258"></a>00258 density = density + am * dans; <a name="l00259"></a>00259 hold = am * pans; <a name="l00260"></a>00260 probability = probability + hold; <a name="l00261"></a>00261 <span class="keywordflow">if</span>((pans * sum < eps2) && (hold < eps2)) <a name="l00262"></a>00262 <span class="keywordflow">break</span>; <span class="comment">// converged</span> <a name="l00263"></a>00263 } <a name="l00264"></a>00264 <span class="keywordflow">if</span>(m == maxit) <a name="l00265"></a>00265 <a class="code" href="mrpt__macros_8h.html#aaa3f404ea85a6575a7139f8d101370ba">THROW_EXCEPTION</a>(<span class="stringliteral">"noncentralChi2P(): no convergence."</span>); <a name="l00266"></a>00266 <span class="keywordflow">return</span> std::make_pair(0.5 * ao * density, std::min(1.0, std::max(0.0, ao * probability))); <a name="l00267"></a>00267 } <a name="l00268"></a>00268 } <span class="comment">// namespace detail</span> <a name="l00269"></a>00269 <span class="comment"></span> <a name="l00270"></a>00270 <span class="comment"> /*! Chi square distribution.</span> <a name="l00271"></a>00271 <span class="comment"></span> <a name="l00272"></a>00272 <span class="comment"> Computes the density of a chi square distribution with \a degreesOfFreedom</span> <a name="l00273"></a>00273 <span class="comment"> and tolerance \a accuracy at the given argument \a arg</span> <a name="l00274"></a>00274 <span class="comment"> by calling <tt>noncentralChi2(degreesOfFreedom, 0.0, arg, accuracy)</tt>.</span> <a name="l00275"></a>00275 <span class="comment"></span> <a name="l00276"></a>00276 <span class="comment"> \note Function code from the Vigra project (http://hci.iwr.uni-heidelberg.de/vigra/); code under "MIT X11 License", GNU GPL-compatible.</span> <a name="l00277"></a>00277 <span class="comment"> */</span> <a name="l00278"></a><a class="code" href="group__stats__grp.html#ga031b06489a1ba993cc1200e4da42dfac">00278</a> <span class="keyword">inline</span> <span class="keywordtype">double</span> <a class="code" href="group__stats__grp.html#ga031b06489a1ba993cc1200e4da42dfac">chi2PDF</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> degreesOfFreedom, <span class="keywordtype">double</span> arg, <span class="keywordtype">double</span> accuracy = 1e-7) <a name="l00279"></a>00279 { <a name="l00280"></a>00280 <span class="keywordflow">return</span> <a class="code" href="namespacemrpt_1_1math_1_1detail.html#a65738e21624fe5ef6f4b645c4dfed0d4">detail::noncentralChi2CDF_exact</a>(degreesOfFreedom, 0.0, arg, accuracy).first; <a name="l00281"></a>00281 } <a name="l00282"></a>00282 <span class="comment"></span> <a name="l00283"></a>00283 <span class="comment"> /** Return the mean and the 10%-90% confidence points (or with any other confidence value) of a set of samples by building the cummulative CDF of all the elements of the container.</span> <a name="l00284"></a>00284 <span class="comment"> * The container can be any MRPT container (CArray, matrices, vectors).</span> <a name="l00285"></a>00285 <span class="comment"> * \param confidenceInterval A number in the range (0,1) such as the confidence interval will be [100*confidenceInterval, 100*(1-confidenceInterval)].</span> <a name="l00286"></a>00286 <span class="comment"> */</span> <a name="l00287"></a>00287 <span class="keyword">template</span> <<span class="keyword">typename</span> CONTAINER> <a name="l00288"></a><a class="code" href="group__stats__grp.html#ga11d16d8fc45991a4676b7200ec69465a">00288</a> <span class="keywordtype">void</span> <a class="code" href="group__stats__grp.html#ga11d16d8fc45991a4676b7200ec69465a" title="Return the mean and the 10%-90% confidence points (or with any other confidence value) of a set of sa...">condidenceIntervals</a>( <a name="l00289"></a>00289 <span class="keyword">const</span> CONTAINER &data, <a name="l00290"></a>00290 <span class="keyword">typename</span> <a class="code" href="eigen__plugins_8h.html#afd07186978da46f9908364e389f8a403" title="Type of the elements.">CONTAINER::value_type</a> &out_mean, <a name="l00291"></a>00291 <span class="keyword">typename</span> <a class="code" href="eigen__plugins_8h.html#afd07186978da46f9908364e389f8a403" title="Type of the elements.">CONTAINER::value_type</a> &out_lower_conf_interval, <a name="l00292"></a>00292 <span class="keyword">typename</span> <a class="code" href="eigen__plugins_8h.html#afd07186978da46f9908364e389f8a403" title="Type of the elements.">CONTAINER::value_type</a> &out_upper_conf_interval, <a name="l00293"></a>00293 <span class="keyword">const</span> <span class="keywordtype">double</span> confidenceInterval = 0.1, <a name="l00294"></a>00294 <span class="keyword">const</span> <span class="keywordtype">size_t</span> histogramNumBins = 1000 ) <a name="l00295"></a>00295 { <a name="l00296"></a>00296 <a class="code" href="mrpt__macros_8h.html#a45b840af519f33816311acdbb28d7c10">MRPT_START</a> <a name="l00297"></a>00297 <a class="code" href="mrpt__macros_8h.html#a47eb5a445c2bf3d9190396510ea9683e">ASSERT_</a>(data.size()!=0) <span class="comment">// don't use .empty() here to allow using matrices</span> <a name="l00298"></a>00298 <a class="code" href="mrpt__macros_8h.html#a47eb5a445c2bf3d9190396510ea9683e">ASSERT_</a>(confidenceInterval>0 && confidenceInterval<1) <a name="l00299"></a>00299 <a name="l00300"></a>00300 out_mean = <a class="code" href="namespacemrpt_1_1math.html#a414456e3e3b75b19cfda5e0a37c46e31" title="Computes the mean value of a vector.">mean</a>(data); <a name="l00301"></a>00301 <span class="keyword">typename</span> CONTAINER<a class="code" href="eigen__plugins_8h.html#afd07186978da46f9908364e389f8a403" title="Type of the elements.">::value_type</a> x_min,x_max; <a name="l00302"></a>00302 <a class="code" href="namespacemrpt_1_1math.html#ae697456de4bc4c09facfe31f7fd4d4ae" title="Return the maximum and minimum values of a std::vector.">minimum_maximum</a>(data,x_min,x_max); <a name="l00303"></a>00303 <a name="l00304"></a>00304 <span class="comment">//std::vector<typename CONTAINER::value_type> xs;</span> <a name="l00305"></a>00305 <span class="comment">//linspace(x_min,x_max,histogramNumBins, xs);</span> <a name="l00306"></a>00306 <span class="keyword">const</span> <span class="keyword">typename</span> CONTAINER<a class="code" href="eigen__plugins_8h.html#afd07186978da46f9908364e389f8a403" title="Type of the elements.">::value_type</a> binWidth = (x_max-x_min)/histogramNumBins; <a name="l00307"></a>00307 <a name="l00308"></a>00308 <span class="keyword">const</span> <a class="code" href="structmrpt_1_1dynamicsize__vector.html" title="The base class of MRPT vectors, actually, Eigen column matrices of dynamic size with specialized cons...">vector_double</a> H = <a class="code" href="namespacemrpt_1_1math.html#a3d873f443e014c6c361a0d4cc32126af" title="Computes the normalized or normal histogram of a sequence of numbers given the number of bins and the...">mrpt::math::histogram</a>(data,x_min,x_max,histogramNumBins); <a name="l00309"></a>00309 <a class="code" href="structmrpt_1_1dynamicsize__vector.html" title="The base class of MRPT vectors, actually, Eigen column matrices of dynamic size with specialized cons...">vector_double</a> Hc; <a name="l00310"></a>00310 <a class="code" href="namespacemrpt_1_1math.html#ae8114c744c0608670c4df1fd59be8909" title="Computes the cumulative sum of all the elements, saving the result in another container.">cumsum</a>(H,Hc); <span class="comment">// CDF</span> <a name="l00311"></a>00311 Hc*=1.0/Hc.maximum(); <a name="l00312"></a>00312 <a name="l00313"></a>00313 <a class="code" href="structmrpt_1_1dynamicsize__vector.html" title="The base class of MRPT vectors, actually, Eigen column matrices of dynamic size with specialized cons...">vector_double</a><a class="code" href="eigen__plugins_8h.html#a39c5d6430ea9395ae7ae729dd0c3f18c">::iterator</a> it_low = std::lower_bound(Hc.begin(),Hc.end(),confidenceInterval); <a class="code" href="mrpt__macros_8h.html#a47eb5a445c2bf3d9190396510ea9683e">ASSERT_</a>(it_low!=Hc.end()) <a name="l00314"></a>00314 <a class="code" href="eigen__plugins_8h.html#a39c5d6430ea9395ae7ae729dd0c3f18c">vector_double::iterator</a> it_high = std::upper_bound(Hc.begin(),Hc.end(),1-confidenceInterval); <a class="code" href="mrpt__macros_8h.html#a47eb5a445c2bf3d9190396510ea9683e">ASSERT_</a>(it_high!=Hc.end()) <a name="l00315"></a>00315 <span class="keyword">const</span> <span class="keywordtype">size_t</span> idx_low = <a class="code" href="group__geometry__grp.html#ga8c0a76e906f12560cfa49fcd269c8398" title="Gets the distance between two points in a 2D space.">std::distance</a>(Hc.begin(),it_low); <a name="l00316"></a>00316 <span class="keyword">const</span> <span class="keywordtype">size_t</span> idx_high = <a class="code" href="group__geometry__grp.html#ga8c0a76e906f12560cfa49fcd269c8398" title="Gets the distance between two points in a 2D space.">std::distance</a>(Hc.begin(),it_high); <a name="l00317"></a>00317 out_lower_conf_interval = x_min + idx_low * binWidth; <a name="l00318"></a>00318 out_upper_conf_interval = x_min + idx_high * binWidth; <a name="l00319"></a>00319 <a name="l00320"></a>00320 <a class="code" href="mrpt__macros_8h.html#a88a917260793b56abd83ad2a0d849eb1">MRPT_END</a> <a name="l00321"></a>00321 } <a name="l00322"></a>00322 <span class="comment"></span> <a name="l00323"></a>00323 <span class="comment"> /** @} */</span> <a name="l00324"></a>00324 <a name="l00325"></a>00325 } <span class="comment">// End of MATH namespace</span> <a name="l00326"></a>00326 <a name="l00327"></a>00327 } <span class="comment">// End of namespace</span> <a name="l00328"></a>00328 <a name="l00329"></a>00329 <a name="l00330"></a>00330 <span class="preprocessor">#endif</span> </pre></div></div> </div> <br><hr><br> <table border="0" width="100%"> <tr> <td> Page generated by <a href="http://www.doxygen.org" target="_blank">Doxygen 1.7.5</a> for MRPT 0.9.5 SVN: at Sun Sep 25 17:20:18 UTC 2011</td><td></td> <td width="100"> </td> <td width="150"> </td></tr> </table> </body></html>