contrib/mul/mbl/mbl_jarque_bera.h
Go to the documentation of this file.
00001 #ifndef mbl_jarque_bera_h_
00002 #define mbl_jarque_bera_h_
00003 
00004 #include <vnl/vnl_gamma.h>
00005 //:
00006 // \file
00007 // \author Ian Scott
00008 // \date 20-Aug-2003
00009 // \brief Jarque Bera test for normality
00010 
00011 
00012 //: Jarque Bera test for normality
00013 // Returns the p-value - the probability that
00014 // the data is normal. If you want a confidence alpha of 0.05
00015 // then the data is normal if (mbl_jarque_bera(start,end) > 0.05)
00016 //
00017 // Note that the test is not particular valuable for small data set.
00018 // For more info, see J. B. Cromwell, W. C. Labys and M. Terraza (1994):
00019 // Univariate Tests for Time Series Models, Sage, California, USA, pages 20-22.
00020 template <class InIt>
00021 double mbl_jarque_bera(InIt start, InIt finish)
00022 {
00023   unsigned count = 0;
00024   double mean = 0.0;
00025   double stddev = 0.0;
00026   for (InIt it=start; it!=finish; ++it)
00027   {
00028     ++count;
00029     const double x = *it;
00030     mean += x;
00031     stddev += x*x;
00032   }
00033 
00034   const double n = count;
00035   mean /= n;
00036   stddev = vcl_sqrt((stddev - mean*mean*n)/(n-1));
00037 
00038 
00039   double S=0.0, K=0.0;
00040   for (InIt it=start; it!=finish; ++it)
00041   {
00042     const double x = ((*it) - mean)/stddev;
00043     const double x_cubed = x*x*x;
00044     S += x_cubed;
00045     K += x_cubed * x;
00046   }
00047   S /= n;
00048   K = K/n - 3;
00049 
00050   const double jbstat = n * ( (S*S)/6  +  (K*K)/24 );
00051 
00052   return 1 - vnl_cum_prob_chi2(2, jbstat);
00053 }
00054 
00055 
00056 #endif // mbl_jarque_bera_h_