00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2009 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 #ifndef mrpt_math_distributions_H 00029 #define mrpt_math_distributions_H 00030 00031 #include <mrpt/utils/utils_defs.h> 00032 #include <mrpt/math/CMatrixTemplateNumeric.h> 00033 #include <mrpt/math/CVectorTemplate.h> 00034 00035 /*--------------------------------------------------------------- 00036 Namespace 00037 ---------------------------------------------------------------*/ 00038 namespace mrpt 00039 { 00040 namespace math 00041 { 00042 /** @name Statistics functions 00043 @{ */ 00044 00045 /** Evaluates the univariate normal (Gaussian) distribution at a given point "x". 00046 */ 00047 double MRPTDLLIMPEXP normalPDF(double x, double mu, double std); 00048 00049 /** Evaluates the multivariate normal (Gaussian) distribution at a given point "x" ("x" and "mu" can be 1xN or Nx1 matrixes). 00050 */ 00051 template <typename T> 00052 T normalPDF( 00053 const CMatrixTemplateNumeric<T> &x, 00054 const CMatrixTemplateNumeric<T> &mu, 00055 const CMatrixTemplateNumeric<T> &cov) 00056 { 00057 MRPT_TRY_START; 00058 00059 CMatrixTemplateNumeric<T> S = cov.inv(); 00060 size_t d = cov.getRowCount(); // Dimension: 00061 CMatrixTemplateNumeric<T> X, MU, X_MU; 00062 00063 if (x.getRowCount()==1) 00064 X = ~x; 00065 else X = x; 00066 00067 if (mu.getRowCount()==1) 00068 MU = ~mu; 00069 else MU = mu; 00070 00071 X_MU = X - MU; 00072 00073 return ::exp( static_cast<T>(-0.5) * ( (~X_MU)*S*X_MU )(0,0) ) / (::pow(static_cast<T>(M_2PI),static_cast<T>(d)) * ::sqrt(cov.det())); 00074 00075 MRPT_TRY_END; 00076 } 00077 00078 /** Evaluates the multivariate normal (Gaussian) distribution at a given point "x" ("x" and "mu" can be 1xN or Nx1 matrixes). 00079 */ 00080 template <typename T,size_t N> 00081 T normalPDF( 00082 const CMatrixFixedNumeric<T,N,1> &x, 00083 const CMatrixFixedNumeric<T,N,1> &mu, 00084 const CMatrixFixedNumeric<T,N,N> &cov) 00085 { 00086 MRPT_TRY_START; 00087 00088 CMatrixFixedNumeric<T,N,N> S; 00089 cov.inv(S); 00090 00091 CMatrixFixedNumeric<T,N,1> X_MU = x; 00092 X_MU-= mu; 00093 00094 return ::exp( static_cast<T>(-0.5) * ( (~X_MU)*S*X_MU )(0,0) ) / (::pow(static_cast<T>(M_2PI),static_cast<T>(N)) * ::sqrt(cov.det())); 00095 00096 MRPT_TRY_END; 00097 } 00098 00099 /** The complementary error function of a Normal distribution 00100 */ 00101 double MRPTDLLIMPEXP erfc(double x); 00102 00103 /** The error function of a Normal distribution 00104 */ 00105 double MRPTDLLIMPEXP erf(double x); 00106 00107 /** Evaluates the Gaussian distribution quantile for the probability value p=[0,1]. 00108 * The employed approximation is that from Peter J. Acklam (pjacklam@online.no), 00109 * freely available in http://home.online.no/~pjacklam. 00110 */ 00111 double MRPTDLLIMPEXP normalQuantile(double p); 00112 00113 /** Evaluates the Gaussian cumulative density function. 00114 * The employed approximation is that from W. J. Cody 00115 * freely available in http://www.netlib.org/specfun/erf 00116 */ 00117 double MRPTDLLIMPEXP normalCDF(double p); 00118 00119 /** The "quantile" of the Chi-Square distribution, for dimension "dim" and probability 0<P<1 00120 * An aproximation from the Wilson-Hilferty transformation is used. 00121 */ 00122 double MRPTDLLIMPEXP chi2inv(double P, unsigned int dim=1); 00123 00124 /** @} */ 00125 00126 } // End of MATH namespace 00127 00128 } // End of namespace 00129 00130 00131 #endif
Page generated by Doxygen 1.6.1 for MRPT 0.7.1 SVN: at Tue Dec 22 08:29:35 CET 2009 |