Advertisement
arcanosam

How to Calculate the Chi-Squared P-Value in BCB

Aug 15th, 2012
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. // original code from http://www.codeproject.com/Articles/432194/How-to-Calculate-the-Chi-Squared-P-Value
  2. // this code work on C++ Builder XE.
  3. // Just create a console project using C++ and choose option 'Use VCL'
  4. // delete the generate Unit1.cpp (or something) file and add this one
  5.  
  6.  
  7. //---------------------------------------------------------------------------
  8.  
  9. #include <vcl.h>
  10. #pragma hdrstop
  11.  
  12. #include <tchar.h>
  13. #include <math.h>
  14. #include <stdio.h>
  15. #include <math.hpp>
  16.  
  17. #define A 15
  18.  
  19. //---------------------------------------------------------------------------
  20.  
  21. double approx_gamma(double Z)
  22. {
  23.     const double RECIP_E = 0.36787944117144232159552377016147;  // RECIP_E = (E^-1) = (1.0 / E)
  24.     const double TWOPI = 6.283185307179586476925286766559;  // TWOPI = 2.0 * PI
  25.  
  26.     double D = 1.0 / (10.0 * Z);
  27.     D = 1.0 / ((12 * Z) - D);
  28.     D = (D + Z) * RECIP_E;
  29.     D = pow(D, Z);
  30.     D *= sqrt(TWOPI / Z);
  31.  
  32.     return D;
  33. }
  34.  
  35. double mygamma(double N)
  36. {
  37.     /*
  38.         The constant SQRT2PI is defined as sqrt(2.0 * PI);
  39.         For speed the constant is already defined in decimal
  40.         form.  However, if you wish to ensure that you achieve
  41.         maximum precision on your own machine, you can calculate
  42.         it yourself using (sqrt(atan(1.0) * 8.0))
  43.     */
  44.  
  45.     //const long double SQRT2PI = sqrtl(atanl(1.0) * 8.0);
  46.     const long double SQRT2PI = 2.5066282746310005024157652848110452530069867406099383;
  47.     long double F = 1.0;
  48.     long double Ck;
  49.     long double Sum = SQRT2PI;
  50.     long double Z = (long double)N;
  51.     long double Sc = powl((Z + A), (Z + 0.5));
  52.     int K;
  53.  
  54.     Sc *= expl(-1.0 * (Z + A));
  55.     Sc /= Z;
  56.  
  57.     for(K = 1; K < A; K++)
  58.     {
  59.         Z++;
  60.         Ck = powl(A - K, K - 0.5);
  61.         Ck *= expl(A - K);
  62.         Ck /= F;
  63.  
  64.         Sum += (Ck / Z);
  65.  
  66.         F *= (-1.0 * K);
  67.     }
  68.  
  69.     return (double)(Sum * Sc);
  70. }
  71.  
  72. static double igf(double S, double Z)
  73. {
  74.     long double Sc = (1.0 / S);
  75.     long double Sum = 1.0;
  76.     long double Nom = 1.0;
  77.     long double Denom = 1.0;
  78.     int I;
  79.  
  80.     if(Z < 0.0)
  81.     {
  82.         return 0.0;
  83.     }
  84.  
  85.     Sc *= powl(Z, S);
  86.     Sc *= expl(-Z);
  87.  
  88.  
  89.     for(I = 0; I < 200; I++) // 200
  90.     {
  91.         Nom *= Z;
  92.         S++;
  93.         Denom *= S;
  94.         Sum += (Nom / Denom);
  95.     }
  96.  
  97.     return Sum * Sc;
  98. }
  99.  
  100. double chisqr(int Dof, double Cv)
  101. {
  102.     double K = ((double)Dof) * 0.5;
  103.     double X = Cv * 0.5;
  104.     double PValue;
  105.  
  106.     printf("Dof:  %i\n", Dof);
  107.     printf("Cv:  %f\n", Cv);
  108.  
  109.     if(Cv < 0 || Dof < 1)
  110.     {
  111.         return 0.0;
  112.     }
  113.  
  114.     if(Dof == 2)
  115.     {
  116.         return exp(-1.0 * X);
  117.     }
  118.  
  119.     PValue = igf(K, X);
  120.     if(IsNan(PValue) || IsInfinite(PValue) || PValue <= 1e-8)
  121.     {
  122.         return 1e-14;
  123.     }
  124.     //PValue /= approx_gamma(K);
  125.     PValue /= mygamma(K);  // First verion in gamma.c
  126.     //PValue /= tgamma(K);  // <math.h> implementation of the gamma function (slightly more accurate & probably faster)
  127.  
  128.     printf("%.28f",(1.0 - PValue));
  129.  
  130.     return 0;
  131. }
  132.  
  133. #pragma argsused
  134. int _tmain(int argc, _TCHAR* argv[])
  135. {
  136.     chisqr(255, 290.285192);
  137.  
  138.     getchar();
  139.     return 0;
  140. }
  141. //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement