00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef mrpt_math_distributions_H
00029 #define mrpt_math_distributions_H
00030
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/math/math_frwds.h>
00033 #include <mrpt/math/CMatrixTemplateNumeric.h>
00034 #include <mrpt/math/CVectorTemplate.h>
00035
00036 #include <mrpt/math/ops_matrices.h>
00037
00038
00039
00040
00041 namespace mrpt
00042 {
00043 namespace math
00044 {
00045 using namespace mrpt::utils;
00046
00047
00048
00049
00050
00051
00052 double MRPTDLLIMPEXP normalPDF(double x, double mu, double std);
00053
00054
00055
00056 template <typename T>
00057 T normalPDF(
00058 const CMatrixTemplateNumeric<T> &x,
00059 const CMatrixTemplateNumeric<T> &mu,
00060 const CMatrixTemplateNumeric<T> &cov)
00061 {
00062 MRPT_START;
00063
00064 CMatrixTemplateNumeric<T> S = cov.inv();
00065 size_t d = cov.getRowCount();
00066 CMatrixTemplateNumeric<T> X, MU, X_MU;
00067
00068 if (x.getRowCount()==1)
00069 X = ~x;
00070 else X = x;
00071
00072 if (mu.getRowCount()==1)
00073 MU = ~mu;
00074 else MU = mu;
00075
00076 X_MU = X - MU;
00077
00078 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()));
00079
00080 MRPT_END;
00081 }
00082
00083
00084
00085 template <typename T,size_t N>
00086 T normalPDF(
00087 const CMatrixFixedNumeric<T,N,1> &x,
00088 const CMatrixFixedNumeric<T,N,1> &mu,
00089 const CMatrixFixedNumeric<T,N,N> &cov)
00090 {
00091 MRPT_START;
00092
00093 CMatrixFixedNumeric<T,N,N> S;
00094 cov.inv(S);
00095
00096 CMatrixFixedNumeric<T,N,1> X_MU = x;
00097 X_MU-= mu;
00098
00099 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()));
00100
00101 MRPT_END;
00102 }
00103
00104
00105
00106 template <typename VECTORLIKE,typename MATRIXLIKE>
00107 typename MATRIXLIKE::value_type
00108 normalPDF(const VECTORLIKE &d,const MATRIXLIKE &cov)
00109 {
00110 MRPT_START
00111 ASSERTDEB_(cov.IsSquare())
00112 ASSERTDEB_(cov.getColCount()==d.size())
00113 MATRIXLIKE C_inv;
00114 cov.inv(C_inv);
00115 return std::exp( static_cast<typename MATRIXLIKE::value_type>(-0.5)*mrpt::math::multiply_HCHt_scalar(d,C_inv))
00116 / (::pow(
00117 static_cast<typename MATRIXLIKE::value_type>(M_2PI),
00118 static_cast<typename MATRIXLIKE::value_type>(cov.getColCount()))
00119 * ::sqrt(cov.det()));
00120 MRPT_END
00121 }
00122
00123
00124
00125
00126
00127 template <typename VECTORLIKE1,typename MATRIXLIKE1,typename VECTORLIKE2,typename MATRIXLIKE2>
00128 double KLD_Gaussians(
00129 const VECTORLIKE1 &mu0, const MATRIXLIKE1 &cov0,
00130 const VECTORLIKE2 &mu1, const MATRIXLIKE2 &cov1)
00131 {
00132 MRPT_START
00133 ASSERT_(mu0.size()==mu1.size() && mu0.size()==size(cov0,1) && mu0.size()==size(cov1,1) && cov0.IsSquare() && cov1.IsSquare() )
00134 const size_t N = mu0.size();
00135 MATRIXLIKE2 cov1_inv;
00136 cov1.inv(cov1_inv);
00137 const VECTORLIKE1 mu_difs = mu0-mu1;
00138 return 0.5*( log(cov1.det()/cov0.det()) + (cov1_inv*cov0).trace() + multiply_HCHt_scalar(mu_difs,cov1_inv) - N );
00139 MRPT_END
00140 }
00141
00142
00143
00144
00145 #ifdef HAVE_ERF
00146 inline double erfc(double x) { return ::erfc(x); }
00147 #else
00148 double MRPTDLLIMPEXP erfc(double x);
00149 #endif
00150
00151
00152
00153 #ifdef HAVE_ERF
00154 inline double erf(double x) { return ::erf(x); }
00155 #else
00156 double MRPTDLLIMPEXP erf(double x);
00157 #endif
00158
00159
00160
00161
00162 double MRPTDLLIMPEXP normalQuantile(double p);
00163
00164
00165
00166
00167
00168 double MRPTDLLIMPEXP normalCDF(double p);
00169
00170
00171
00172
00173 double MRPTDLLIMPEXP chi2inv(double P, unsigned int dim=1);
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187 template <class T>
00188 double noncentralChi2CDF(unsigned int degreesOfFreedom, T noncentrality, T arg)
00189 {
00190 const double a = degreesOfFreedom + noncentrality;
00191 const double b = (a + noncentrality) / square(a);
00192 const double t = (std::pow((double)arg / a, 1.0/3.0) - (1.0 - 2.0 / 9.0 * b)) / std::sqrt(2.0 / 9.0 * b);
00193 return 0.5*(1.0 + mrpt::math::erf(t/std::sqrt(2.0)));
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 inline double chi2CDF(unsigned int degreesOfFreedom, double arg)
00206 {
00207 return noncentralChi2CDF(degreesOfFreedom, 0.0, arg);
00208 }
00209
00210 namespace detail
00211 {
00212 template <class T>
00213 void noncentralChi2OneIteration(T arg, T & lans, T & dans, T & pans, unsigned int & j)
00214 {
00215 double tol = -50.0;
00216 if(lans < tol)
00217 {
00218 lans = lans + std::log(arg / j);
00219 dans = std::exp(lans);
00220 }
00221 else
00222 {
00223 dans = dans * arg / j;
00224 }
00225 pans = pans - dans;
00226 j += 2;
00227 }
00228
00229 template <class T>
00230 std::pair<double, double> noncentralChi2CDF_exact(unsigned int degreesOfFreedom, T noncentrality, T arg, T eps)
00231 {
00232 ASSERTMSG_(noncentrality >= 0.0 && arg >= 0.0 && eps > 0.0,"noncentralChi2P(): parameters must be positive.");
00233 if (arg == 0.0 && degreesOfFreedom > 0)
00234 return std::make_pair(0.0, 0.0);
00235
00236
00237 double b1 = 0.5 * noncentrality,
00238 ao = std::exp(-b1),
00239 eps2 = eps / ao,
00240 lnrtpi2 = 0.22579135264473,
00241 probability, density, lans, dans, pans, sum, am, hold;
00242 unsigned int maxit = 500,
00243 i, m;
00244 if(degreesOfFreedom % 2)
00245 {
00246 i = 1;
00247 lans = -0.5 * (arg + std::log(arg)) - lnrtpi2;
00248 dans = std::exp(lans);
00249 pans = erf(std::sqrt(arg/2.0));
00250 }
00251 else
00252 {
00253 i = 2;
00254 lans = -0.5 * arg;
00255 dans = std::exp(lans);
00256 pans = 1.0 - dans;
00257 }
00258
00259
00260 if(degreesOfFreedom == 0)
00261 {
00262 m = 1;
00263 degreesOfFreedom = 2;
00264 am = b1;
00265 sum = 1.0 / ao - 1.0 - am;
00266 density = am * dans;
00267 probability = 1.0 + am * pans;
00268 }
00269 else
00270 {
00271 m = 0;
00272 degreesOfFreedom = degreesOfFreedom - 1;
00273 am = 1.0;
00274 sum = 1.0 / ao - 1.0;
00275 while(i < degreesOfFreedom)
00276 detail::noncentralChi2OneIteration(arg, lans, dans, pans, i);
00277 degreesOfFreedom = degreesOfFreedom + 1;
00278 density = dans;
00279 probability = pans;
00280 }
00281
00282 for(++m; m<maxit; ++m)
00283 {
00284 am = b1 * am / m;
00285 detail::noncentralChi2OneIteration(arg, lans, dans, pans, degreesOfFreedom);
00286 sum = sum - am;
00287 density = density + am * dans;
00288 hold = am * pans;
00289 probability = probability + hold;
00290 if((pans * sum < eps2) && (hold < eps2))
00291 break;
00292 }
00293 if(m == maxit)
00294 THROW_EXCEPTION("noncentralChi2P(): no convergence.");
00295 return std::make_pair(0.5 * ao * density, std::min(1.0, std::max(0.0, ao * probability)));
00296 }
00297 }
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 inline double chi2PDF(unsigned int degreesOfFreedom, double arg, double accuracy = 1e-7)
00308 {
00309 return detail::noncentralChi2CDF_exact(degreesOfFreedom, 0.0, arg, accuracy).first;
00310 }
00311
00312
00313
00314
00315
00316 template <typename CONTAINER>
00317 void condidenceIntervals(
00318 const CONTAINER &data,
00319 typename CONTAINER::value_type &out_mean,
00320 typename CONTAINER::value_type &out_lower_conf_interval,
00321 typename CONTAINER::value_type &out_upper_conf_interval,
00322 const double confidenceInterval = 0.1,
00323 const size_t histogramNumBins = 1000 )
00324 {
00325 MRPT_START
00326 ASSERT_(data.size()!=0)
00327 ASSERT_(confidenceInterval>0 && confidenceInterval<1)
00328
00329 out_mean = mean(data);
00330 typename CONTAINER::value_type x_min,x_max;
00331 minimum_maximum(data,x_min,x_max);
00332
00333
00334
00335 const typename CONTAINER::value_type binWidth = (x_max-x_min)/histogramNumBins;
00336
00337 const vector_double H = mrpt::math::histogram(data,x_min,x_max,histogramNumBins);
00338 vector_double Hc;
00339 cumsum(H,Hc);
00340 Hc*=1.0/maximum(Hc);
00341
00342 vector_double::iterator it_low = std::lower_bound(Hc.begin(),Hc.end(),confidenceInterval); ASSERT_(it_low!=Hc.end())
00343 vector_double::iterator it_high = std::upper_bound(Hc.begin(),Hc.end(),1-confidenceInterval); ASSERT_(it_high!=Hc.end())
00344 const size_t idx_low = std::distance(Hc.begin(),it_low);
00345 const size_t idx_high = std::distance(Hc.begin(),it_high);
00346 out_lower_conf_interval = x_min + idx_low * binWidth;
00347 out_upper_conf_interval = x_min + idx_high * binWidth;
00348
00349 MRPT_END
00350 }
00351
00352
00353
00354 }
00355
00356 }
00357
00358
00359 #endif