<!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>The MRPT project: 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.6.2-20100208 --> <script type="text/javascript"><!-- var searchBox = new SearchBox("searchBox", "search",false,'Search'); --></script> <div class="navigation" id="top"> <div class="tabs"> <ul> <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="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"> <img id="MSearchSelect" src="search/search.png" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" alt=""/> <input type="text" id="MSearchField" value="Search" accesskey="S" onfocus="searchBox.OnSearchFieldFocus(true)" onblur="searchBox.OnSearchFieldFocus(false)" onkeyup="searchBox.OnSearchFieldChange(event)"/> <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a> </div> </li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <h1>distributions.h</h1><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://mrpt.sourceforge.net/ |</span> <a name="l00005"></a>00005 <span class="comment"> | |</span> <a name="l00006"></a>00006 <span class="comment"> | Copyright (C) 2005-2010 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 <span class="preprocessor">#include <<a class="code" href="_c_vector_template_8h.html">mrpt/math/CVectorTemplate.h</a>></span> <a name="l00035"></a>00035 <a name="l00036"></a>00036 <span class="preprocessor">#include <<a class="code" href="ops__matrices_8h.html" title="This file implements miscelaneous matrix and matrix/vector operations, plus internal...">mrpt/math/ops_matrices.h</a>></span> <a name="l00037"></a>00037 <span class="preprocessor">#include <<a class="code" href="matrices__metaprogramming_8h.html">mrpt/math/matrices_metaprogramming.h</a>></span> <a name="l00038"></a>00038 <a name="l00039"></a>00039 <span class="comment">/*---------------------------------------------------------------</span> <a name="l00040"></a>00040 <span class="comment"> Namespace</span> <a name="l00041"></a>00041 <span class="comment"> ---------------------------------------------------------------*/</span> <a name="l00042"></a>00042 <span class="keyword">namespace </span>mrpt <a name="l00043"></a>00043 { <a name="l00044"></a>00044 <span class="keyword">namespace </span>math <a name="l00045"></a>00045 { <a name="l00046"></a>00046 <span class="keyword">using namespace </span>mrpt::utils; <a name="l00047"></a>00047 <span class="comment"></span> <a name="l00048"></a>00048 <span class="comment"> /** @name Statistics functions</span> <a name="l00049"></a>00049 <span class="comment"> @{ */</span> <a name="l00050"></a>00050 <span class="comment"></span> <a name="l00051"></a>00051 <span class="comment"> /** Evaluates the univariate normal (Gaussian) distribution at a given point "x".</span> <a name="l00052"></a>00052 <span class="comment"> */</span> <a name="l00053"></a>00053 <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="namespacemrpt_1_1math.html#a2ec8bf7897f7b8b0fee1ca9ae46443bb" title="Evaluates the univariate normal (Gaussian) distribution at a given point &quot;x&quot;...">normalPDF</a>(<span class="keywordtype">double</span> x, <span class="keywordtype">double</span> mu, <span class="keywordtype">double</span> std); <a name="l00054"></a>00054 <span class="comment"></span> <a name="l00055"></a>00055 <span class="comment"> /** Evaluates the multivariate normal (Gaussian) distribution at a given point "x".</span> <a name="l00056"></a>00056 <span class="comment"> * \param x A vector or column or row matrix with the point at which to evaluate the pdf.</span> <a name="l00057"></a>00057 <span class="comment"> * \param mu A vector or column or row matrix with the Gaussian mean.</span> <a name="l00058"></a>00058 <span class="comment"> * \param cov The covariance matrix of the Gaussian.</span> <a name="l00059"></a>00059 <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="l00060"></a>00060 <span class="comment"> */</span> <a name="l00061"></a>00061 <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="l00062"></a>00062 <span class="keyword">inline</span> <span class="keyword">typename</span> MATRIXLIKE::value_type <a name="l00063"></a><a class="code" href="namespacemrpt_1_1math.html#af9d60bc1d50af36334050b7277739f4d">00063</a> <a class="code" href="namespacemrpt_1_1math.html#a2ec8bf7897f7b8b0fee1ca9ae46443bb" title="Evaluates the univariate normal (Gaussian) distribution at a given point &quot;x&quot;...">normalPDF</a>( <a name="l00064"></a>00064 <span class="keyword">const</span> VECTORLIKE1 & x, <a name="l00065"></a>00065 <span class="keyword">const</span> VECTORLIKE2 & mu, <a name="l00066"></a>00066 <span class="keyword">const</span> MATRIXLIKE & cov, <a name="l00067"></a>00067 <span class="keyword">const</span> <span class="keywordtype">bool</span> scaled_pdf = <span class="keyword">false</span> ) <a name="l00068"></a>00068 { <a name="l00069"></a>00069 <a class="code" href="utils__defs_8h.html#a45b840af519f33816311acdbb28d7c10">MRPT_START</a> <a name="l00070"></a>00070 <span class="keyword">typedef</span> <span class="keyword">typename</span> MATRIXLIKE::value_type T; <a name="l00071"></a>00071 <a class="code" href="utils__defs_8h.html#a5ad4d8d68e2f6664f247407bf89aac55" title="Defines an assertion mechanism - only when compiled in debug.">ASSERTDEB_</a>(cov.IsSquare()) <a name="l00072"></a>00072 <a class="code" href="utils__defs_8h.html#a5ad4d8d68e2f6664f247407bf89aac55" title="Defines an assertion mechanism - only when compiled in debug.">ASSERTDEB_</a>(cov.getColCount()==x.size() && cov.getColCount()==mu.size()) <a name="l00073"></a>00073 <a class="code" href="matrices__metaprogramming_8h.html#a08057eb77ac3effc33c55cad4c30a9da">MAT_TYPE_SAMESIZE_OF</a>(MATRIXLIKE) C_inv(<a class="code" href="math__frwds_8h.html#a8c37a9d2fbd2dbc8089842ac69495710" title="For usage in one of the constructors of CMatrixFixedNumeric or CMatrixTemplate (and...">UNINITIALIZED_MATRIX</a>); <a name="l00074"></a>00074 cov.inv(C_inv); <a name="l00075"></a>00075 T ret = ::exp( <span class="keyword">static_cast<</span>T<span class="keyword">></span>(-0.5) * <a class="code" href="namespacemrpt_1_1math.html#afca563cb211788e6922cb93e8337a416" title="Just like s=H.multiply_HCHt_scalar(C), but defined in mrpt::math for backward compatibility...">mrpt::math::multiply_HCHt_scalar</a>((x-mu),C_inv) ); <a name="l00076"></a>00076 <span class="keywordflow">return</span> scaled_pdf ? ret : ret / (::pow(<span class="keyword">static_cast<</span>T<span class="keyword">></span>(M_2PI),static_cast<T>( <a class="code" href="namespacemrpt_1_1math.html#a34f37758cc35f29279a8e6ad91215ad1">size</a>(cov,1) )) * ::sqrt(cov.det())); <a name="l00077"></a>00077 <a class="code" href="utils__defs_8h.html#a88a917260793b56abd83ad2a0d849eb1">MRPT_END</a> <a name="l00078"></a>00078 } <a name="l00079"></a>00079 <span class="comment"></span> <a name="l00080"></a>00080 <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="l00081"></a>00081 <span class="comment"> */</span> <a name="l00082"></a>00082 <span class="keyword">template</span> <<span class="keyword">typename</span> VECTORLIKE,<span class="keyword">typename</span> MATRIXLIKE> <a name="l00083"></a>00083 <span class="keyword">typename</span> MATRIXLIKE::value_type <a name="l00084"></a><a class="code" href="namespacemrpt_1_1math.html#a08b51cc7c299612edf16d5bc2e2aac62">00084</a> <a class="code" href="namespacemrpt_1_1math.html#a2ec8bf7897f7b8b0fee1ca9ae46443bb" title="Evaluates the univariate normal (Gaussian) distribution at a given point &quot;x&quot;...">normalPDF</a>(<span class="keyword">const</span> VECTORLIKE &d,<span class="keyword">const</span> MATRIXLIKE &cov) <a name="l00085"></a>00085 { <a name="l00086"></a>00086 <a class="code" href="utils__defs_8h.html#a45b840af519f33816311acdbb28d7c10">MRPT_START</a> <a name="l00087"></a>00087 <a class="code" href="utils__defs_8h.html#a5ad4d8d68e2f6664f247407bf89aac55" title="Defines an assertion mechanism - only when compiled in debug.">ASSERTDEB_</a>(cov.IsSquare()) <a name="l00088"></a>00088 <a class="code" href="utils__defs_8h.html#a5ad4d8d68e2f6664f247407bf89aac55" title="Defines an assertion mechanism - only when compiled in debug.">ASSERTDEB_</a>(cov.getColCount()==d.size()) <a name="l00089"></a>00089 MATRIXLIKE C_inv; <a name="l00090"></a>00090 cov.inv(C_inv); <a name="l00091"></a>00091 <span class="keywordflow">return</span> std::exp( static_cast<typename MATRIXLIKE::value_type>(-0.5)*<a class="code" href="namespacemrpt_1_1math.html#afca563cb211788e6922cb93e8337a416" title="Just like s=H.multiply_HCHt_scalar(C), but defined in mrpt::math for backward compatibility...">mrpt::math::multiply_HCHt_scalar</a>(d,C_inv)) <a name="l00092"></a>00092 / (::pow( <a name="l00093"></a>00093 <span class="keyword">static_cast<</span>typename MATRIXLIKE::value_type<span class="keyword">></span>(M_2PI), <a name="l00094"></a>00094 static_cast<typename MATRIXLIKE::value_type>(cov.getColCount())) <a name="l00095"></a>00095 * ::sqrt(cov.det())); <a name="l00096"></a>00096 <a class="code" href="utils__defs_8h.html#a88a917260793b56abd83ad2a0d849eb1">MRPT_END</a> <a name="l00097"></a>00097 } <a name="l00098"></a>00098 <span class="comment"></span> <a name="l00099"></a>00099 <span class="comment"> /** Kullback-Leibler divergence (KLD) between two independent multivariate Gaussians.</span> <a name="l00100"></a>00100 <span class="comment"> *</span> <a name="l00101"></a>00101 <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="l00102"></a>00102 <span class="comment"> */</span> <a name="l00103"></a>00103 <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="l00104"></a><a class="code" href="namespacemrpt_1_1math.html#aede55ba0a3b97c18a06dd19322c16578">00104</a> <span class="keywordtype">double</span> <a class="code" href="namespacemrpt_1_1math.html#aede55ba0a3b97c18a06dd19322c16578" title="Kullback-Leibler divergence (KLD) between two independent multivariate Gaussians...">KLD_Gaussians</a>( <a name="l00105"></a>00105 <span class="keyword">const</span> VECTORLIKE1 &mu0, <span class="keyword">const</span> MATRIXLIKE1 &cov0, <a name="l00106"></a>00106 <span class="keyword">const</span> VECTORLIKE2 &mu1, <span class="keyword">const</span> MATRIXLIKE2 &cov1) <a name="l00107"></a>00107 { <a name="l00108"></a>00108 <a class="code" href="utils__defs_8h.html#a45b840af519f33816311acdbb28d7c10">MRPT_START</a> <a name="l00109"></a>00109 <a class="code" href="utils__defs_8h.html#a47eb5a445c2bf3d9190396510ea9683e">ASSERT_</a>(mu0.size()==mu1.size() && mu0.size()==<a class="code" href="namespacemrpt_1_1math.html#a34f37758cc35f29279a8e6ad91215ad1">size</a>(cov0,1) && mu0.size()==<a class="code" href="namespacemrpt_1_1math.html#a34f37758cc35f29279a8e6ad91215ad1">size</a>(cov1,1) && cov0.IsSquare() && cov1.IsSquare() ) <a name="l00110"></a>00110 <span class="keyword">const</span> <span class="keywordtype">size_t</span> N = mu0.size(); <a name="l00111"></a>00111 MATRIXLIKE2 cov1_inv; <a name="l00112"></a>00112 cov1.inv(cov1_inv); <a name="l00113"></a>00113 <span class="keyword">const</span> VECTORLIKE1 mu_difs = mu0-mu1; <a name="l00114"></a>00114 <span class="keywordflow">return</span> 0.5*( log(cov1.det()/cov0.det()) + (cov1_inv*cov0).trace() + <a class="code" href="namespacemrpt_1_1math.html#afca563cb211788e6922cb93e8337a416" title="Just like s=H.multiply_HCHt_scalar(C), but defined in mrpt::math for backward compatibility...">multiply_HCHt_scalar</a>(mu_difs,cov1_inv) - N ); <a name="l00115"></a>00115 <a class="code" href="utils__defs_8h.html#a88a917260793b56abd83ad2a0d849eb1">MRPT_END</a> <a name="l00116"></a>00116 } <a name="l00117"></a>00117 <a name="l00118"></a>00118 <span class="comment"></span> <a name="l00119"></a>00119 <span class="comment"> /** The complementary error function of a Normal distribution</span> <a name="l00120"></a>00120 <span class="comment"> */</span> <a name="l00121"></a>00121 <span class="preprocessor">#ifdef HAVE_ERF</span> <a name="l00122"></a>00122 <span class="preprocessor"></span> <span class="keyword">inline</span> <span class="keywordtype">double</span> <a class="code" href="namespacemrpt_1_1math.html#a700b695a783f89132853f9a3cbf37abf" title="The complementary error function of a Normal distribution.">erfc</a>(<span class="keywordtype">double</span> x) { <a class="code" href="namespacemrpt_1_1math.html#a700b695a783f89132853f9a3cbf37abf" title="The complementary error function of a Normal distribution.">return ::erfc</a>(x); } <a name="l00123"></a>00123 <span class="preprocessor">#else</span> <a name="l00124"></a>00124 <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="namespacemrpt_1_1math.html#a700b695a783f89132853f9a3cbf37abf" title="The complementary error function of a Normal distribution.">erfc</a>(<span class="keywordtype">double</span> x); <a name="l00125"></a>00125 <span class="preprocessor">#endif</span> <a name="l00126"></a>00126 <span class="preprocessor"></span><span class="comment"></span> <a name="l00127"></a>00127 <span class="comment"> /** The error function of a Normal distribution</span> <a name="l00128"></a>00128 <span class="comment"> */</span> <a name="l00129"></a>00129 <span class="preprocessor">#ifdef HAVE_ERF</span> <a name="l00130"></a>00130 <span class="preprocessor"></span> <span class="keyword">inline</span> <span class="keywordtype">double</span> <a class="code" href="namespacemrpt_1_1math.html#a31b77faefe845a607e6661c2a24e7e5f" title="The error function of a Normal distribution.">erf</a>(<span class="keywordtype">double</span> x) { <a class="code" href="namespacemrpt_1_1math.html#a31b77faefe845a607e6661c2a24e7e5f" title="The error function of a Normal distribution.">return ::erf</a>(x); } <a name="l00131"></a>00131 <span class="preprocessor">#else</span> <a name="l00132"></a>00132 <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="namespacemrpt_1_1math.html#a31b77faefe845a607e6661c2a24e7e5f" title="The error function of a Normal distribution.">erf</a>(<span class="keywordtype">double</span> x); <a name="l00133"></a>00133 <span class="preprocessor">#endif</span> <a name="l00134"></a>00134 <span class="preprocessor"></span><span class="comment"> /** Evaluates the Gaussian distribution quantile for the probability value p=[0,1].</span> <a name="l00135"></a>00135 <span class="comment"> * The employed approximation is that from Peter J. Acklam (pjacklam@online.no),</span> <a name="l00136"></a>00136 <span class="comment"> * freely available in http://home.online.no/~pjacklam.</span> <a name="l00137"></a>00137 <span class="comment"> */</span> <a name="l00138"></a>00138 <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="namespacemrpt_1_1math.html#ae1e6f25a0c6a318d6edd2bc9ef124bed" title="Evaluates the Gaussian distribution quantile for the probability value p=[0,1].">normalQuantile</a>(<span class="keywordtype">double</span> p); <a name="l00139"></a>00139 <span class="comment"></span> <a name="l00140"></a>00140 <span class="comment"> /** Evaluates the Gaussian cumulative density function.</span> <a name="l00141"></a>00141 <span class="comment"> * The employed approximation is that from W. J. Cody</span> <a name="l00142"></a>00142 <span class="comment"> * freely available in http://www.netlib.org/specfun/erf</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="namespacemrpt_1_1math.html#a09f5b985d95b570e05c2b9cef91865b0" title="Evaluates the Gaussian cumulative density function.">normalCDF</a>(<span class="keywordtype">double</span> p); <a name="l00145"></a>00145 <span class="comment"></span> <a name="l00146"></a>00146 <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="l00147"></a>00147 <span class="comment"> * An aproximation from the Wilson-Hilferty transformation is used.</span> <a name="l00148"></a>00148 <span class="comment"> */</span> <a name="l00149"></a>00149 <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="namespacemrpt_1_1math.html#a68b8a2208349a297c19f3a45c24457b1" title="The &quot;quantile&quot; of the Chi-Square distribution, for dimension &quot;dim&quot;...">chi2inv</a>(<span class="keywordtype">double</span> P, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> dim=1); <a name="l00150"></a>00150 <span class="comment"></span> <a name="l00151"></a>00151 <span class="comment"> /*! Cumulative non-central chi square distribution (approximate).</span> <a name="l00152"></a>00152 <span class="comment"></span> <a name="l00153"></a>00153 <span class="comment"> Computes approximate values of the cumulative density of a chi square distribution with \a degreesOfFreedom,</span> <a name="l00154"></a>00154 <span class="comment"> and noncentrality parameter \a noncentrality at the given argument</span> <a name="l00155"></a>00155 <span class="comment"> \a arg, i.e. the probability that a random number drawn from the distribution is below \a arg</span> <a name="l00156"></a>00156 <span class="comment"> It uses the approximate transform into a normal distribution due to Wilson and Hilferty</span> <a name="l00157"></a>00157 <span class="comment"> (see Abramovitz, Stegun: "Handbook of Mathematical Functions", formula 26.3.32).</span> <a name="l00158"></a>00158 <span class="comment"> The algorithm's running time is independent of the inputs. The accuracy is only</span> <a name="l00159"></a>00159 <span class="comment"> about 0.1 for few degrees of freedom, but reaches about 0.001 above dof = 5.</span> <a name="l00160"></a>00160 <span class="comment"></span> <a name="l00161"></a>00161 <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="l00162"></a>00162 <span class="comment"> */</span> <a name="l00163"></a>00163 <span class="keyword">template</span> <<span class="keyword">class</span> T> <a name="l00164"></a><a class="code" href="namespacemrpt_1_1math.html#abe14bc934138342f5aeb1e92516eaca7">00164</a> <span class="keywordtype">double</span> <a class="code" href="namespacemrpt_1_1math.html#abe14bc934138342f5aeb1e92516eaca7">noncentralChi2CDF</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> degreesOfFreedom, T noncentrality, T arg) <a name="l00165"></a>00165 { <a name="l00166"></a>00166 <span class="keyword">const</span> <span class="keywordtype">double</span> a = degreesOfFreedom + noncentrality; <a name="l00167"></a>00167 <span class="keyword">const</span> <span class="keywordtype">double</span> b = (a + noncentrality) / <a class="code" href="namespacemrpt_1_1utils.html#a9e4c229e3e0ff2041d65fa9748ce5f6b" title="Inline function for the square of a number.">square</a>(a); <a name="l00168"></a>00168 <span class="keyword">const</span> <span class="keywordtype">double</span> t = (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="l00169"></a>00169 <span class="keywordflow">return</span> 0.5*(1.0 + <a class="code" href="namespacemrpt_1_1math.html#a31b77faefe845a607e6661c2a24e7e5f" title="The error function of a Normal distribution.">mrpt::math::erf</a>(t/std::sqrt(2.0))); <a name="l00170"></a>00170 } <a name="l00171"></a>00171 <span class="comment"></span> <a name="l00172"></a>00172 <span class="comment"> /*! Cumulative chi square distribution.</span> <a name="l00173"></a>00173 <span class="comment"></span> <a name="l00174"></a>00174 <span class="comment"> Computes the cumulative density of a chi square distribution with \a degreesOfFreedom</span> <a name="l00175"></a>00175 <span class="comment"> and tolerance \a accuracy at the given argument \a arg, i.e. the probability that</span> <a name="l00176"></a>00176 <span class="comment"> a random number drawn from the distribution is below \a arg</span> <a name="l00177"></a>00177 <span class="comment"> by calling <tt>noncentralChi2CDF(degreesOfFreedom, 0.0, arg, accuracy)</tt>.</span> <a name="l00178"></a>00178 <span class="comment"></span> <a name="l00179"></a>00179 <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="l00180"></a>00180 <span class="comment"> */</span> <a name="l00181"></a><a class="code" href="namespacemrpt_1_1math.html#adaf15d0ae96eb360f18107703ff8ffc9">00181</a> <span class="keyword">inline</span> <span class="keywordtype">double</span> <a class="code" href="namespacemrpt_1_1math.html#adaf15d0ae96eb360f18107703ff8ffc9">chi2CDF</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> degreesOfFreedom, <span class="keywordtype">double</span> arg) <a name="l00182"></a>00182 { <a name="l00183"></a>00183 <span class="keywordflow">return</span> <a class="code" href="namespacemrpt_1_1math.html#abe14bc934138342f5aeb1e92516eaca7">noncentralChi2CDF</a>(degreesOfFreedom, 0.0, arg); <a name="l00184"></a>00184 } <a name="l00185"></a>00185 <a name="l00186"></a>00186 <span class="keyword">namespace </span>detail <a name="l00187"></a>00187 { <a name="l00188"></a>00188 <span class="keyword">template</span> <<span class="keyword">class</span> T> <a name="l00189"></a><a class="code" href="namespacemrpt_1_1math_1_1detail.html#a7211d347b1b0460cce697e445be9dd51">00189</a> <span class="keywordtype">void</span> <a class="code" href="namespacemrpt_1_1math_1_1detail.html#a7211d347b1b0460cce697e445be9dd51">noncentralChi2OneIteration</a>(T arg, T & lans, T & dans, T & pans, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> & j) <a name="l00190"></a>00190 { <a name="l00191"></a>00191 <span class="keywordtype">double</span> tol = -50.0; <a name="l00192"></a>00192 <span class="keywordflow">if</span>(lans < tol) <a name="l00193"></a>00193 { <a name="l00194"></a>00194 lans = lans + std::log(arg / j); <a name="l00195"></a>00195 dans = std::exp(lans); <a name="l00196"></a>00196 } <a name="l00197"></a>00197 <span class="keywordflow">else</span> <a name="l00198"></a>00198 { <a name="l00199"></a>00199 dans = dans * arg / j; <a name="l00200"></a>00200 } <a name="l00201"></a>00201 pans = pans - dans; <a name="l00202"></a>00202 j += 2; <a name="l00203"></a>00203 } <a name="l00204"></a>00204 <a name="l00205"></a>00205 <span class="keyword">template</span> <<span class="keyword">class</span> T> <a name="l00206"></a><a class="code" href="namespacemrpt_1_1math_1_1detail.html#a6b1f245cfebd2a2b05a80b9af4372fcb">00206</a> std::pair<double, double> <a class="code" href="namespacemrpt_1_1math_1_1detail.html#a6b1f245cfebd2a2b05a80b9af4372fcb">noncentralChi2CDF_exact</a>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> degreesOfFreedom, T noncentrality, T arg, T eps) <a name="l00207"></a>00207 { <a name="l00208"></a>00208 <a class="code" href="utils__defs_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="l00209"></a>00209 <span class="keywordflow">if</span> (arg == 0.0 && degreesOfFreedom > 0) <a name="l00210"></a>00210 <span class="keywordflow">return</span> std::make_pair(0.0, 0.0); <a name="l00211"></a>00211 <a name="l00212"></a>00212 <span class="comment">// Determine initial values</span> <a name="l00213"></a>00213 <span class="keywordtype">double</span> b1 = 0.5 * noncentrality, <a name="l00214"></a>00214 ao = std::exp(-b1), <a name="l00215"></a>00215 eps2 = eps / ao, <a name="l00216"></a>00216 lnrtpi2 = 0.22579135264473, <a name="l00217"></a>00217 probability, density, lans, dans, pans, <a class="code" href="namespacemrpt_1_1math.html#a73bb4427a78a19d9b7dc9feeb7c13b5a">sum</a>, am, hold; <a name="l00218"></a>00218 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> maxit = 500, <a name="l00219"></a>00219 i, m; <a name="l00220"></a>00220 <span class="keywordflow">if</span>(degreesOfFreedom % 2) <a name="l00221"></a>00221 { <a name="l00222"></a>00222 i = 1; <a name="l00223"></a>00223 lans = -0.5 * (arg + std::log(arg)) - lnrtpi2; <a name="l00224"></a>00224 dans = std::exp(lans); <a name="l00225"></a>00225 pans = <a class="code" href="namespacemrpt_1_1math.html#a31b77faefe845a607e6661c2a24e7e5f" title="The error function of a Normal distribution.">erf</a>(std::sqrt(arg/2.0)); <a name="l00226"></a>00226 } <a name="l00227"></a>00227 <span class="keywordflow">else</span> <a name="l00228"></a>00228 { <a name="l00229"></a>00229 i = 2; <a name="l00230"></a>00230 lans = -0.5 * arg; <a name="l00231"></a>00231 dans = std::exp(lans); <a name="l00232"></a>00232 pans = 1.0 - dans; <a name="l00233"></a>00233 } <a name="l00234"></a>00234 <a name="l00235"></a>00235 <span class="comment">// Evaluate first term</span> <a name="l00236"></a>00236 <span class="keywordflow">if</span>(degreesOfFreedom == 0) <a name="l00237"></a>00237 { <a name="l00238"></a>00238 m = 1; <a name="l00239"></a>00239 degreesOfFreedom = 2; <a name="l00240"></a>00240 am = b1; <a name="l00241"></a>00241 sum = 1.0 / ao - 1.0 - am; <a name="l00242"></a>00242 density = am * dans; <a name="l00243"></a>00243 probability = 1.0 + am * pans; <a name="l00244"></a>00244 } <a name="l00245"></a>00245 <span class="keywordflow">else</span> <a name="l00246"></a>00246 { <a name="l00247"></a>00247 m = 0; <a name="l00248"></a>00248 degreesOfFreedom = degreesOfFreedom - 1; <a name="l00249"></a>00249 am = 1.0; <a name="l00250"></a>00250 sum = 1.0 / ao - 1.0; <a name="l00251"></a>00251 <span class="keywordflow">while</span>(i < degreesOfFreedom) <a name="l00252"></a>00252 <a class="code" href="namespacemrpt_1_1math_1_1detail.html#a7211d347b1b0460cce697e445be9dd51">detail::noncentralChi2OneIteration</a>(arg, lans, dans, pans, i); <a name="l00253"></a>00253 degreesOfFreedom = degreesOfFreedom + 1; <a name="l00254"></a>00254 density = dans; <a name="l00255"></a>00255 probability = pans; <a name="l00256"></a>00256 } <a name="l00257"></a>00257 <span class="comment">// Evaluate successive terms of the expansion</span> <a name="l00258"></a>00258 <span class="keywordflow">for</span>(++m; m<maxit; ++m) <a name="l00259"></a>00259 { <a name="l00260"></a>00260 am = b1 * am / m; <a name="l00261"></a>00261 <a class="code" href="namespacemrpt_1_1math_1_1detail.html#a7211d347b1b0460cce697e445be9dd51">detail::noncentralChi2OneIteration</a>(arg, lans, dans, pans, degreesOfFreedom); <a name="l00262"></a>00262 sum = sum - am; <a name="l00263"></a>00263 density = density + am * dans; <a name="l00264"></a>00264 hold = am * pans; <a name="l00265"></a>00265 probability = probability + hold; <a name="l00266"></a>00266 <span class="keywordflow">if</span>((pans * sum < eps2) && (hold < eps2)) <a name="l00267"></a>00267 <span class="keywordflow">break</span>; <span class="comment">// converged</span> <a name="l00268"></a>00268 } <a name="l00269"></a>00269 <span class="keywordflow">if</span>(m == maxit) <a name="l00270"></a>00270 <a class="code" href="utils__defs_8h.html#aaa3f404ea85a6575a7139f8d101370ba">THROW_EXCEPTION</a>(<span class="stringliteral">"noncentralChi2P(): no convergence."</span>); <a name="l00271"></a>00271 <span class="keywordflow">return</span> std::make_pair(0.5 * ao * density, std::min(1.0, std::max(0.0, ao * probability))); <a name="l00272"></a>00272 } <a name="l00273"></a>00273 } <span class="comment">// namespace detail</span> <a name="l00274"></a>00274 <span class="comment"></span> <a name="l00275"></a>00275 <span class="comment"> /*! Chi square distribution.</span> <a name="l00276"></a>00276 <span class="comment"></span> <a name="l00277"></a>00277 <span class="comment"> Computes the density of a chi square distribution with \a degreesOfFreedom</span> <a name="l00278"></a>00278 <span class="comment"> and tolerance \a accuracy at the given argument \a arg</span> <a name="l00279"></a>00279 <span class="comment"> by calling <tt>noncentralChi2(degreesOfFreedom, 0.0, arg, accuracy)</tt>.</span> <a name="l00280"></a>00280 <span class="comment"></span> <a name="l00281"></a>00281 <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="l00282"></a>00282 <span class="comment"> */</span> <a name="l00283"></a><a class="code" href="namespacemrpt_1_1math.html#a031b06489a1ba993cc1200e4da42dfac">00283</a> <span class="keyword">inline</span> <span class="keywordtype">double</span> <a class="code" href="namespacemrpt_1_1math.html#a031b06489a1ba993cc1200e4da42dfac">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="l00284"></a>00284 { <a name="l00285"></a>00285 <span class="keywordflow">return</span> <a class="code" href="namespacemrpt_1_1math_1_1detail.html#a6b1f245cfebd2a2b05a80b9af4372fcb">detail::noncentralChi2CDF_exact</a>(degreesOfFreedom, 0.0, arg, accuracy).first; <a name="l00286"></a>00286 } <a name="l00287"></a>00287 <span class="comment"></span> <a name="l00288"></a>00288 <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="l00289"></a>00289 <span class="comment"> * The container can be any MRPT container (CArray, matrices, vectors).</span> <a name="l00290"></a>00290 <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="l00291"></a>00291 <span class="comment"> */</span> <a name="l00292"></a>00292 <span class="keyword">template</span> <<span class="keyword">typename</span> CONTAINER> <a name="l00293"></a><a class="code" href="namespacemrpt_1_1math.html#a580a20d9c7270a89d5ef8f99e68db932">00293</a> <span class="keywordtype">void</span> <a class="code" href="namespacemrpt_1_1math.html#a580a20d9c7270a89d5ef8f99e68db932" title="Return the mean and the 10-90% confidence points (or with any other confidence value)...">condidenceIntervals</a>( <a name="l00294"></a>00294 <span class="keyword">const</span> CONTAINER &data, <a name="l00295"></a>00295 <span class="keyword">typename</span> CONTAINER::value_type &out_mean, <a name="l00296"></a>00296 <span class="keyword">typename</span> CONTAINER::value_type &out_lower_conf_interval, <a name="l00297"></a>00297 <span class="keyword">typename</span> CONTAINER::value_type &out_upper_conf_interval, <a name="l00298"></a>00298 <span class="keyword">const</span> <span class="keywordtype">double</span> confidenceInterval = 0.1, <a name="l00299"></a>00299 <span class="keyword">const</span> <span class="keywordtype">size_t</span> histogramNumBins = 1000 ) <a name="l00300"></a>00300 { <a name="l00301"></a>00301 <a class="code" href="utils__defs_8h.html#a45b840af519f33816311acdbb28d7c10">MRPT_START</a> <a name="l00302"></a>00302 <a class="code" href="utils__defs_8h.html#a47eb5a445c2bf3d9190396510ea9683e">ASSERT_</a>(data.size()!=0) <span class="comment">// don't use .empty() here to allow using matrices</span> <a name="l00303"></a>00303 <a class="code" href="utils__defs_8h.html#a47eb5a445c2bf3d9190396510ea9683e">ASSERT_</a>(confidenceInterval>0 && confidenceInterval<1) <a name="l00304"></a>00304 <a name="l00305"></a>00305 out_mean = <a class="code" href="namespacemrpt_1_1math.html#a42afad98eee7cd7448bd39832f5e8c0e">mean</a>(data); <a name="l00306"></a>00306 <span class="keyword">typename</span> CONTAINER::value_type x_min,x_max; <a name="l00307"></a>00307 <a class="code" href="namespacemrpt_1_1math.html#aeef5f72ed9e3041def8ca9729b729d42">minimum_maximum</a>(data,x_min,x_max); <a name="l00308"></a>00308 <a name="l00309"></a>00309 <span class="comment">//std::vector<typename CONTAINER::value_type> xs;</span> <a name="l00310"></a>00310 <span class="comment">//linspace(x_min,x_max,histogramNumBins, xs);</span> <a name="l00311"></a>00311 <span class="keyword">const</span> <span class="keyword">typename</span> CONTAINER::value_type binWidth = (x_max-x_min)/histogramNumBins; <a name="l00312"></a>00312 <a name="l00313"></a>00313 <span class="keyword">const</span> <a class="code" href="structmrpt_1_1mrpt__base__vector.html">vector_double</a> H = <a class="code" href="namespacemrpt_1_1math.html#a8efc75310d1e955ad47e55a8a06da3d8">mrpt::math::histogram</a>(data,x_min,x_max,histogramNumBins); <a name="l00314"></a>00314 <a class="code" href="structmrpt_1_1mrpt__base__vector.html">vector_double</a> Hc; <a name="l00315"></a>00315 <a class="code" href="namespacemrpt_1_1math.html#a56cf8da4f694023de52be20605961e0e">cumsum</a>(H,Hc); <span class="comment">// CDF</span> <a name="l00316"></a>00316 Hc*=1.0/<a class="code" href="namespacemrpt_1_1math.html#a8101cfd988397e07443ea6519bd1c588">maximum</a>(Hc); <a name="l00317"></a>00317 <a name="l00318"></a>00318 vector_double::iterator it_low = std::lower_bound(Hc.begin(),Hc.end(),confidenceInterval); <a class="code" href="utils__defs_8h.html#a47eb5a445c2bf3d9190396510ea9683e">ASSERT_</a>(it_low!=Hc.end()) <a name="l00319"></a>00319 vector_double::iterator it_high = std::upper_bound(Hc.begin(),Hc.end(),1-confidenceInterval); <a class="code" href="utils__defs_8h.html#a47eb5a445c2bf3d9190396510ea9683e">ASSERT_</a>(it_high!=Hc.end()) <a name="l00320"></a>00320 <span class="keyword">const</span> <span class="keywordtype">size_t</span> idx_low = <a class="code" href="namespacemrpt_1_1math.html#a8c0a76e906f12560cfa49fcd269c8398" title="Gets the distance between two points in a 2D space.">std::distance</a>(Hc.begin(),it_low); <a name="l00321"></a>00321 <span class="keyword">const</span> <span class="keywordtype">size_t</span> idx_high = <a class="code" href="namespacemrpt_1_1math.html#a8c0a76e906f12560cfa49fcd269c8398" title="Gets the distance between two points in a 2D space.">std::distance</a>(Hc.begin(),it_high); <a name="l00322"></a>00322 out_lower_conf_interval = x_min + idx_low * binWidth; <a name="l00323"></a>00323 out_upper_conf_interval = x_min + idx_high * binWidth; <a name="l00324"></a>00324 <a name="l00325"></a>00325 <a class="code" href="utils__defs_8h.html#a88a917260793b56abd83ad2a0d849eb1">MRPT_END</a> <a name="l00326"></a>00326 } <a name="l00327"></a>00327 <span class="comment"></span> <a name="l00328"></a>00328 <span class="comment"> /** @} */</span> <a name="l00329"></a>00329 <a name="l00330"></a>00330 } <span class="comment">// End of MATH namespace</span> <a name="l00331"></a>00331 <a name="l00332"></a>00332 } <span class="comment">// End of namespace</span> <a name="l00333"></a>00333 <a name="l00334"></a>00334 <a name="l00335"></a>00335 <span class="preprocessor">#endif</span> </pre></div></div> <!--- window showing the filter options --> <div id="MSearchSelectWindow" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" onkeydown="return searchBox.OnSearchSelectKey(event)"> <a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark"> </span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark"> </span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark"> </span>Defines</a></div> <!-- iframe showing the search results (closed by default) --> <div id="MSearchResultsWindow"> <iframe src="" frameborder="0" name="MSearchResults" id="MSearchResults"> </iframe> </div> <br><hr><br> <table border="0" width="100%"> <tr> <td> Page generated by <a href="http://www.doxygen.org" target="_blank">Doxygen 1.6.2-20100208</a> for MRPT 0.9.0 SVN: at Wed Jul 14 12:48:09 UTC 2010</td><td></td> <td width="100"> </td> <td width="150"> </td></tr> </table> </body></html>