12const char MinDecl[] =
"#define Min 2.22507e-308\n";
18"#define fMaxGammaArgument 171.624376956302\n";
20"double GetValue( double x,double fp,double fDF );\n";
22"double GetValue( double x,double fp,double fDF )\n"
24" return fp - 2 * GetTDist(x, fDF);\n"
26const char fmin_countDecl[] =
"double fmin_count(double a, double b, __private int *p);\n";
28"double fmin_count(double a, double b, __private int *p) {\n"
29" double result = fmin(a, b);\n"
30" bool t = isnan(result);\n"
34const char fmax_countDecl[] =
"double fmax_count(double a, double b, __private int *p);\n";
36"double fmax_count(double a, double b, __private int *p) {\n"
37" double result = fmax(a, b);\n"
38" bool t = isnan(result);\n"
42const char fsum_countDecl[] =
"double fsum_count(double a, double b, __private int *p);\n";
44"double fsum_count(double a, double b, __private int *p) {\n"
45" bool t = isnan(a);\n"
50"double GetGammaSeries( double fA, double fX );\n";
52"double GetGammaSeries( double fA, double fX )\n"
54" double fDenomfactor = fA;\n"
55" double fSummand = 1.0/fA;\n"
56" double fSum = fSummand;\n"
60" fDenomfactor = fDenomfactor + 1.0;\n"
61" fSummand = fSummand * fX/fDenomfactor;\n"
62" fSum = fSum + fSummand;\n"
63" nCount = nCount+1;\n"
64" } while ( fSummand/fSum > fHalfMachEps && nCount<=10000);\n"
73"double GetGammaContFraction( double fA, double fX )\n"
75" double fBig = 1.0/fBigInv;\n"
76" double fCount = 0.0;\n"
77" double fNum = 0.0;\n"
78" double fY = 1.0 - fA;\n"
79" double fDenom = fX + 2.0-fA;\n"
81" double fPkm1 = fX + 1.0;\n"
82" double fPkm2 = 1.0;\n"
84" double fQkm1 = fDenom * fX;\n"
85" double fQkm2 = fX;\n"
86" double fApprox = fPkm1/fQkm1;\n"
87" bool bFinished = false;\n"
91" fCount = fCount +1.0;\n"
93" fNum = fY * fCount;\n"
94" fDenom = fDenom +2.0;\n"
95" fPk = fPkm1 * fDenom - fPkm2 * fNum;\n"
96" fQk = fQkm1 * fDenom - fQkm2 * fNum;\n"
100" bFinished = (fabs( (fApprox - fR)/fR ) <= fHalfMachEps);\n"
107" if (fabs(fPk) > fBig)\n"
109" fPkm2 = fPkm2 * fBigInv;\n"
110" fPkm1 = fPkm1 * fBigInv;\n"
111" fQkm2 = fQkm2 * fBigInv;\n"
112" fQkm1 = fQkm1 * fBigInv;\n"
114" } while (!bFinished && fCount<10000);\n"
123"double GetLowRegIGamma( double fA, double fX )\n"
125" double fLnFactor = fA * log(fX) - fX - lgamma(fA);\n"
126" double fFactor = exp(fLnFactor);\n"
128" return 1.0 - fFactor * GetGammaContFraction(fA,fX);\n"
130" return fFactor * GetGammaSeries(fA,fX);\n"
133"fAlpha, double fLambda );\n";
135"double GetGammaDist( double fX, double fAlpha, double fLambda )\n"
140" return GetLowRegIGamma( fAlpha, fX / fLambda);\n"
143", double fAlpha, double fLambda );\n";
145"double GetGammaDistPDF( double fX, double fAlpha, double fLambda )\n"
149" else if (fX == 0)\n"
151" if (fAlpha < 1.0)\n"
155" else if (fAlpha == 1)\n"
157" return (1.0 / fLambda);\n"
166" double fXr = fX / fLambda;\n"
169" if (log(fXr) * (fAlpha-1.0) < fLogDblMax &&"
170"fAlpha < fMaxGammaArgument)\n"
172" return pow( fXr, fAlpha-1.0) * exp(-fXr) / "
173"fLambda / tgamma(fAlpha);\n"
177" return exp( (fAlpha-1.0) * log(fXr) - fXr - "
178"log(fLambda) - lgamma(fAlpha));\n"
183" if (fAlpha<fMaxGammaArgument)\n"
185" return pow( fXr, fAlpha-1.0) * exp(-fXr) / "
186"fLambda / tgamma(fAlpha);\n"
190" return pow( fXr, fAlpha-1.0) * exp(-fXr) / "
191"fLambda / exp( lgamma(fAlpha));\n"
197"double GetBetaDist(double fXin, double fAlpha, double fBeta);\n";
199"double GetBetaDist(double fXin, double fAlpha, double fBeta)\n"
205" if (fBeta == 1.0)\n"
206" return pow(fXin, fAlpha);\n"
207" if (fAlpha == 1.0)\n"
208" return -expm1(fBeta * log1p(-fXin));\n"
210" double fY = (0.5-fXin)+0.5;\n"
211" double flnY = log1p(-fXin);\n"
212" double fX = fXin;\n"
213" double flnX = log(fXin);\n"
214" double fA = fAlpha;\n"
215" double fB = fBeta;\n"
216" bool bReflect = fXin > fAlpha/(fAlpha+fBeta);\n"
224" flnY = log(fXin);\n"
226" fResult = lcl_GetBetaHelperContFrac(fX,fA,fB)/fA;\n"
227" double fP = fA/(fA+fB);\n"
228" double fQ = fB/(fA+fB);\n"
229" if (fA > 1.0 && fB > 1.0 && fP < 0.97 && fQ < 0.97)\n"
230" fResult *= GetBetaDistPDF(fX,fA,fB)*fX*fY;\n"
232" fResult *= pow(exp(1.0),(fA*flnX + fB*flnY - GetLogBeta(fA,fB)));\n"
234" fResult = 0.5 - fResult + 0.5;\n"
235" if (fResult > 1.0)\n"
237" if (fResult < 0.0)\n"
243 "double GetFDist(double x, double fF1, double fF2);\n";
245"double GetFDist(double x, double fF1, double fF2)\n"
247" double arg = fF2/(fF2+fF1*x);\n"
248" double alpha = fF2/2.0;\n"
249" double beta = fF1/2.0;\n"
250" return (GetBetaDist(arg, alpha, beta));\n"
253" GetGammaInvValue(double fAlpha,double fBeta,double fX1 );\n";
255"double GetGammaInvValue(double fAlpha,double fBeta,double fX1 )\n"
261" double fX=fX1/fBeta;\n"
262" double fLnFactor = fAlpha * log(fX) - fX - lgamma(fAlpha);\n"
263" double fFactor = exp(fLnFactor);\n"
264" if (fX>fAlpha+1.0)\n"
265" return 1.0 - fFactor * GetGammaContFraction(fAlpha,fX);\n"
267" return fFactor * GetGammaSeries(fAlpha,fX);\n"
273"double GetFInvValue(double fF1,double fF2,double fX1 )\n"
275" double arg = fF2/(fF2+fF1*fX1);\n"
276" double alpha = fF2/2.0;\n"
277" double beta = fF1/2.0;\n"
278" double fXin,fAlpha,fBeta;\n"
279" fXin=arg;fAlpha=alpha;fBeta=beta;\n"
284" if (fBeta == 1.0)\n"
285" return pow(fXin, fAlpha);\n"
286" if (fAlpha == 1.0)\n"
287" return -expm1(fBeta * log1p(-fXin));\n"
289" double fY = (0.5-fXin)+0.5;\n"
290" double flnY = log1p(-fXin);\n"
291" double fX = fXin;\n"
292" double flnX = log(fXin);\n"
293" double fA = fAlpha;\n"
294" double fB = fBeta;\n"
295" bool bReflect = fXin > fAlpha/(fAlpha+fBeta);\n"
303" flnY = log(fXin);\n"
305" fResult = lcl_GetBetaHelperContFrac(fX,fA,fB);\n"
306" fResult = fResult/fA;\n"
307" double fP = fA/(fA+fB);\n"
308" double fQ = fB/(fA+fB);\n"
310" if (fA > 1.0 && fB > 1.0 && fP < 0.97 && fQ < 0.97)\n"
311" fTemp = GetBetaDistPDF(fX,fA,fB)*fX*fY;\n"
313" fTemp = exp(fA*flnX + fB*flnY - GetLogBeta(fA,fB));\n"
314" fResult *= fTemp;\n"
316" fResult = 0.5 - fResult + 0.5;\n"
317" if (fResult > 1.0)\n"
319" if (fResult < 0.0)\n"
324 "double GetBinomDistPMF(double x, double n, double p);";
326"double GetBinomDistPMF(double x, double n, double p)\n"
328" double q = (0.5 - p) + 0.5;\n"
329" double fFactor = pow(q, n);\n"
330" if (fFactor <= Min)\n"
332" fFactor = pow(p, n);\n"
333" if (fFactor <= Min)\n"
334" return GetBetaDistPDF(p, x + 1.0, n - x + 1.0)/(n + 1.0);\n"
337" uint max = (uint)(n - x);\n"
338" for (uint i = 0; i < max && fFactor > 0.0; ++i)\n"
339" fFactor *= (n - i)/(double)(i + 1)*q/p;\n"
345" uint max = (uint)x;\n"
346" for (uint i = 0; i < max && fFactor > 0.0; ++i)\n"
347" fFactor *= (n - i)/(double)(i + 1)*p/q;\n"
353 "double lcl_GetBinomDistRange(double n, \n"
354"double xs, double xe, double fFactor, double p, double q);";
356"double lcl_GetBinomDistRange(double n, double xs, double xe,\n"
357" double fFactor, double p, double q)\n"
361" uint nXs = (uint)xs;\n"
362" for (i = 1; i <= nXs && fFactor > 0.0; ++i)\n"
363" fFactor *= (n - i + 1)/(double)(i)*p/q;\n"
365" uint nXe =(uint)xe;\n"
366" for (i = nXs + 1; i <= nXe && fFactor > 0.0; ++i)\n"
368" fFactor *= (n - i + 1)/(double)(i)*p/q;\n"
371" return (fSum > 1.0) ? 1.0 : fSum;\n"
376"double GetLogGamma(double fZ)\n"
378" if (fZ >= fMaxGammaArgument)\n"
379" return lcl_GetLogGammaHelper(fZ);\n"
381" return log(lcl_GetGammaHelper(fZ));\n"
383" return log( lcl_GetGammaHelper(fZ+1) / fZ);\n"
384" return lcl_GetLogGammaHelper(fZ+2) - log(fZ+1) - log(fZ);\n"
389"double GetChiDist(double fX, double fDF)\n"
394" return GetUpRegIGamma( fDF/2.0, fX/2.0);\n"
398"double GetChiSqDistCDF(double fX, double fDF);\n";
400"double GetChiSqDistCDF(double fX, double fDF)\n"
405" return GetLowRegIGamma( fDF/2.0, fX/2.0);\n"
409"double GetChiSqDistPDF(double fX, double fDF);\n";
411"double GetChiSqDistPDF(double fX, double fDF)\n"
416" if (fDF*fX > 1391000.0)\n"
418" fValue = exp((0.5*fDF - 1) * log(fX*0.5) - 0.5 * fX - log(2.0)"
419" - lgamma(0.5*fDF));\n"
424" if (fmod(fDF,2.0)<0.5)\n"
431" fValue = 1.0/sqrt(fX*2*M_PI);\n"
434" while ( fCount < fDF)\n"
436" fValue *= (fX / fCount);\n"
440" fValue = exp(log(fValue)-fX/2);\n"
442" fValue *= exp(-fX/2);\n"
448"static double lcl_IterateInverseBetaInv(double fp, double fAlpha, \n"
449" double fBeta, double fAx, double fBx, bool *rConvError );\n";
451"static double lcl_IterateInverseBetaInv(double fp, double fAlpha, \n"
452" double fBeta, double fAx, double fBx, bool *rConvError )\n"
454" *rConvError = false;\n"
455" double fYEps = 1.0E-307;\n"
456" double fXEps = fMachEps;\n"
461" double fAy = fp - GetBetaDist(fAx, fAlpha, fBeta);\n"
462" double fBy = fp - GetBetaDist(fBx, fAlpha, fBeta);\n"
464" unsigned short nCount;\n"
465" for (nCount = 0; nCount < 1000 && !lcl_HasChangeOfSign(fAy,fBy);"
468" if (fabs(fAy) <= fabs(fBy))\n"
471" fAx += 2.0 * (fAx - fBx);\n"
476" fAy = fp - GetBetaDist(fAx, fAlpha, fBeta);\n"
481" fBx += 2.0 * (fBx - fAx);\n"
484" fBy = fp - GetBetaDist(fBx, fAlpha, fBeta);\n"
491" if (!lcl_HasChangeOfSign( fAy, fBy))\n"
493" *rConvError = true;\n"
496" double fPx = fAx;\n"
497" double fPy = fAy;\n"
498" double fQx = fBx;\n"
499" double fQy = fBy;\n"
500" double fRx = fAx;\n"
501" double fRy = fAy;\n"
502" double fSx = 0.5 * (fAx + fBx);\n"
503" bool bHasToInterpolate = true;\n"
505" while ( nCount < 500 && fabs(fRy) > fYEps &&\n"
506" (fBx-fAx) > fmax( fabs(fAx), fabs(fBx)) * fXEps )\n"
508" if (bHasToInterpolate)\n"
510" if (fPy!=fQy && fQy!=fRy && fRy!=fPy)\n"
512" fSx = fPx*fRy*fQy/(fRy-fPy)/(fQy-fPy)\n"
513" + fRx*fQy*fPy/(fQy-fRy)/(fPy-fRy)\n"
514" + fQx*fPy*fRy/(fPy-fQy)/(fRy-fQy);\n"
515" bHasToInterpolate = (fAx < fSx) && (fSx < fBx);\n"
518" bHasToInterpolate = false;\n"
520" if(!bHasToInterpolate)\n"
522" fSx = 0.5 * (fAx + fBx);\n"
523" fPx = fAx; fPy = fAy;\n"
524" fQx = fBx; fQy = fBy;\n"
525" bHasToInterpolate = true;\n"
527" fPx = fQx; fQx = fRx; fRx = fSx;\n"
528" fPy = fQy; fQy = fRy; fRy = fp - GetBetaDist(fSx, fAlpha, fBeta);\n"
529" if (lcl_HasChangeOfSign( fAy, fRy))\n"
531" fBx = fRx; fBy = fRy;\n"
535" fAx = fRx; fAy = fRy;\n"
537" bHasToInterpolate = bHasToInterpolate && (fabs(fRy) *"
538" 2.0 <= fabs(fQy));\n"
545"static double lcl_IterateInverseChiInv"
546"(double fp, double fdf, double fAx, double fBx, bool *rConvError);\n";
548"static double lcl_IterateInverseChiInv"
549"(double fp, double fdf, double fAx, double fBx, bool *rConvError)\n"
551" *rConvError = false;\n"
552" double fYEps = 1.0E-307;\n"
553" double fXEps = fMachEps;\n"
558" double fAy = fp - GetChiDist(fAx, fdf);\n"
559" double fBy = fp - GetChiDist(fBx, fdf);\n"
561" unsigned short nCount;\n"
562" for (nCount = 0; nCount < 1000 && "
563"!lcl_HasChangeOfSign(fAy,fBy); nCount++)\n"
565" if (fabs(fAy) <= fabs(fBy))\n"
568" fAx += 2.0 * (fAx - fBx);\n"
573" fAy = fp - GetChiDist(fAx, fdf);\n"
578" fBx += 2.0 * (fBx - fAx);\n"
581" fBy = fp - GetChiDist(fBx, fdf);\n"
588" if (!lcl_HasChangeOfSign( fAy, fBy))\n"
590" *rConvError = true;\n"
593" double fPx = fAx;\n"
594" double fPy = fAy;\n"
595" double fQx = fBx;\n"
596" double fQy = fBy;\n"
597" double fRx = fAx;\n"
598" double fRy = fAy;\n"
599" double fSx = 0.5 * (fAx + fBx);\n"
600" bool bHasToInterpolate = true;\n"
602" while ( nCount < 500 && fabs(fRy) > fYEps &&\n"
603" (fBx-fAx) > fmax( fabs(fAx), fabs(fBx)) * fXEps )\n"
605" if (bHasToInterpolate)\n"
607" if (fPy!=fQy && fQy!=fRy && fRy!=fPy)\n"
609" fSx = fPx * fRy * fQy/(fRy-fPy)/(fQy-fPy)\n"
610" + fRx * fQy * fPy/(fQy-fRy)/(fPy-fRy)\n"
611" + fQx * fPy * fRy/(fPy-fQy)/(fRy-fQy);\n"
612" bHasToInterpolate = (fAx < fSx) && (fSx < fBx);\n"
615" bHasToInterpolate = false;\n"
617" if(!bHasToInterpolate)\n"
619" fSx = 0.5 * (fAx + fBx);\n"
620" fPx = fAx; fPy = fAy;\n"
621" fQx = fBx; fQy = fBy;\n"
622" bHasToInterpolate = true;\n"
624" fPx = fQx; fQx = fRx; fRx = fSx;\n"
625" fPy = fQy; fQy = fRy; fRy = fp - GetChiDist(fSx, fdf);\n"
626" if (lcl_HasChangeOfSign( fAy, fRy))\n"
628" fBx = fRx; fBy = fRy;\n"
632" fAx = fRx; fAy = fRy;\n"
634" bHasToInterpolate = bHasToInterpolate && (fabs(fRy)"
635" * 2.0 <= fabs(fQy));\n"
642"static double lcl_IterateInverseChiSQInv( double fp, double fdf, \n"
643" double fAx, double fBx, bool *rConvError );\n";
645"static double lcl_IterateInverseChiSQInv( double fp, double fdf, \n"
646" double fAx, double fBx, bool *rConvError )\n"
648" *rConvError = false;\n"
649" double fYEps = 1.0E-307;\n"
650" double fXEps = fMachEps;\n"
656" double fAy = fp - GetChiSqDistCDF(fAx, fdf);\n"
657" double fBy = fp - GetChiSqDistCDF(fBx, fdf);\n"
659" unsigned short nCount;\n"
660" for (nCount = 0; nCount < 1000 && !lcl_HasChangeOfSign(fAy,fBy);"
663" if (fabs(fAy) <= fabs(fBy))\n"
666" fAx += 2.0 * (fAx - fBx);\n"
671" fAy = fp - GetChiSqDistCDF(fAx, fdf);\n"
676" fBx += 2.0 * (fBx - fAx);\n"
679" fBy = fp - GetChiSqDistCDF(fBx, fdf);\n"
686" if (!lcl_HasChangeOfSign( fAy, fBy))\n"
688" *rConvError = true;\n"
691" double fPx = fAx;\n"
692" double fPy = fAy;\n"
693" double fQx = fBx;\n"
694" double fQy = fBy;\n"
695" double fRx = fAx;\n"
696" double fRy = fAy;\n"
697" double fSx = 0.5 * (fAx + fBx);\n"
698" bool bHasToInterpolate = true;\n"
700" while ( nCount < 500 && fabs(fRy) > fYEps &&\n"
701" (fBx-fAx) > fmax( fabs(fAx), fabs(fBx)) * fXEps )\n"
703" if (bHasToInterpolate)\n"
705" if (fPy!=fQy && fQy!=fRy && fRy!=fPy)\n"
707" fSx = fPx * fRy * fQy / (fRy-fPy) / (fQy-fPy)\n"
708" + fRx * fQy * fPy / (fQy-fRy) / (fPy-fRy)\n"
709" + fQx * fPy * fRy / (fPy-fQy) / (fRy-fQy);\n"
710" bHasToInterpolate = (fAx < fSx) && (fSx < fBx);\n"
713" bHasToInterpolate = false;\n"
715" if(!bHasToInterpolate)\n"
717" fSx = 0.5 * (fAx + fBx);\n"
718" fPx = fAx; fPy = fAy;\n"
719" fQx = fBx; fQy = fBy;\n"
720" bHasToInterpolate = true;\n"
722" fPx = fQx; fQx = fRx; fRx = fSx;\n"
723" fPy = fQy; fQy = fRy; fRy = fp - GetChiSqDistCDF(fSx, fdf);\n"
724" if (lcl_HasChangeOfSign( fAy, fRy))\n"
726" fBx = fRx; fBy = fRy;\n"
730" fAx = fRx; fAy = fRy;\n"
732" bHasToInterpolate = bHasToInterpolate && (fabs(fRy) * 2.0"
741"double gaussinv(double x)\n"
745" if(fabs(q)<=.425)\n"
757" t*2509.0809287301226727+33430.575583588128105\n"
759" *t+67265.770927008700853\n"
761" *t+45921.953931549871457\n"
763" *t+13731.693765509461125\n"
765" *t+1971.5909503065514427\n"
767" *t+133.14166789178437745\n"
769" *t+3.387132872796366608\n"
779" t*5226.495278852854561+28729.085735721942674\n"
781" *t+39307.89580009271061\n"
783" *t+21213.794301586595867\n"
785" *t+5394.1960214247511077\n"
787" *t+687.1870074920579083\n"
789" *t+42.313330701600911252\n"
809" t*7.7454501427834140764e-4+0.0227238449892691845833\n"
811" *t+0.24178072517745061177\n"
813" *t+1.27045825245236838258\n"
815" *t+3.64784832476320460504\n"
817" *t+5.7694972214606914055\n"
819" *t+4.6303378461565452959\n"
821" *t+1.42343711074968357734\n"
831" t*1.05075007164441684324e-9+5.475938084995344946e-4\n"
833" *t+0.0151986665636164571966\n"
835" *t+0.14810397642748007459\n"
837" *t+0.68976733498510000455\n"
839" *t+1.6763848301838038494\n"
841" *t+2.05319162663775882187\n"
856" t*2.01033439929228813265e-7+2.71155556874348757815e-5\n"
858" *t+0.0012426609473880784386\n"
860" *t+0.026532189526576123093\n"
862" *t+0.29656057182850489123\n"
864" *t+1.7848265399172913358\n"
866" *t+5.4637849111641143699\n"
868" *t+6.6579046435011037772\n"
878" t*2.04426310338993978564e-15+1.4215117583164458887e-7\n"
880" *t+1.8463183175100546818e-5\n"
882" *t+7.868691311456132591e-4\n"
884" *t+0.0148753612908506148525\n"
886" *t+0.13692988092273580531\n"
888" *t+0.59983220655588793769\n"
898"static double lcl_GetLogGammaHelper(double fZ);\n";
900"static double lcl_GetLogGammaHelper(double fZ)\n"
902" double fg = 6.024680040776729583740234375;\n"
903" double fZgHelp = fZ + fg - 0.5;\n"
904" return log( lcl_getLanczosSum(fZ)) + (fZ-0.5) * log(fZgHelp) - fZgHelp;\n"
907"static double lcl_GetGammaHelper(double fZ);\n";
909"static double lcl_GetGammaHelper(double fZ)\n"
911" double fGamma = lcl_getLanczosSum(fZ);\n"
912" double fg = 6.024680040776729583740234375;\n"
913" double fZgHelp = fZ + fg - 0.5;\n"
914" double fHalfpower = pow( fZgHelp, fZ/2 - 0.25);\n"
915" fGamma *= fHalfpower;\n"
916" fGamma = fGamma/exp(fZgHelp);\n"
917" fGamma *= fHalfpower;\n"
919" if (fZ <= 20.0 && fZ == (int)fZ)\n"
921" fGamma = (int)(fGamma+0.5);\n"
926"static double lcl_getLanczosSum(double fZ);\n";
928"static double lcl_getLanczosSum(double fZ) \n"
930" double fNum[13] ={ \n"
931" 23531376880.41075968857200767445163675473, \n"
932" 42919803642.64909876895789904700198885093, \n"
933" 35711959237.35566804944018545154716670596, \n"
934" 17921034426.03720969991975575445893111267, \n"
935" 6039542586.35202800506429164430729792107, \n"
936" 1439720407.311721673663223072794912393972, \n"
937" 248874557.8620541565114603864132294232163, \n"
938" 31426415.58540019438061423162831820536287, \n"
939" 2876370.628935372441225409051620849613599, \n"
940" 186056.2653952234950402949897160456992822, \n"
941" 8071.672002365816210638002902272250613822, \n"
942" 210.8242777515793458725097339207133627117, \n"
943" 2.506628274631000270164908177133837338626 \n"
945" double fDenom[13] = { \n"
961" double fSumDenom;\n"
965" fSumNum = fNum[12];\n"
966" fSumDenom = fDenom[12];\n"
968" fSumNum = fSumNum*fZ+fNum[nI];\n"
969" fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
971" fSumNum = fSumNum*fZ+fNum[nI];\n"
972" fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
974" fSumNum = fSumNum*fZ+fNum[nI];\n"
975" fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
977" fSumNum = fSumNum*fZ+fNum[nI];\n"
978" fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
980" fSumNum = fSumNum*fZ+fNum[nI];\n"
981" fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
983" fSumNum = fSumNum*fZ+fNum[nI];\n"
984" fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
986" fSumNum = fSumNum*fZ+fNum[nI];\n"
987" fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
989" fSumNum = fSumNum*fZ+fNum[nI];\n"
990" fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
992" fSumNum = fSumNum*fZ+fNum[nI];\n"
993" fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
995" fSumNum = fSumNum*fZ+fNum[nI];\n"
996" fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
998" fSumNum = fSumNum*fZ+fNum[nI];\n"
999" fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
1001" fSumNum = fSumNum*fZ+fNum[nI];\n"
1002" fSumDenom = fSumDenom*fZ+fDenom[nI];\n"
1006" double fZInv = 1.0/fZ;\n"
1007" fSumNum = fNum[0];\n"
1008" fSumDenom = fDenom[0];\n"
1010" fSumNum = fSumNum*fZInv+fNum[nI];\n"
1011" fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1013" fSumNum = fSumNum*fZInv+fNum[nI];\n"
1014" fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1016" fSumNum = fSumNum*fZInv+fNum[nI];\n"
1017" fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1019" fSumNum = fSumNum*fZInv+fNum[nI];\n"
1020" fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1022" fSumNum = fSumNum*fZInv+fNum[nI];\n"
1023" fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1025" fSumNum = fSumNum*fZInv+fNum[nI];\n"
1026" fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1028" fSumNum = fSumNum*fZInv+fNum[nI];\n"
1029" fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1031" fSumNum = fSumNum*fZInv+fNum[nI];\n"
1032" fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1034" fSumNum = fSumNum*fZInv+fNum[nI];\n"
1035" fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1037" fSumNum = fSumNum*fZInv+fNum[nI];\n"
1038" fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1040" fSumNum = fSumNum*fZInv+fNum[nI];\n"
1041" fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1043" fSumNum = fSumNum*fZInv+fNum[nI];\n"
1044" fSumDenom = fSumDenom*fZInv+fDenom[nI];\n"
1046" return fSumNum/fSumDenom;\n"
1050" double GetUpRegIGamma( double fA, double fX ) ;\n";
1052"double GetUpRegIGamma( double fA, double fX )\n"
1054" double fLnFactor= fA*log(fX)-fX-lgamma(fA);\n"
1055" double fFactor = exp(fLnFactor); \n"
1057" return fFactor * GetGammaContFraction(fA,fX);\n"
1059" return 1.0 -fFactor * GetGammaSeries(fA,fX);\n"
1063"static inline bool lcl_HasChangeOfSign( double u, double w );\n";
1065"static inline bool lcl_HasChangeOfSign( double u, double w )\n"
1067" return (u < 0.0 && w > 0.0) || (u > 0.0 && w < 0.0);\n"
1072"double GetTDist(double T, double fDF)\n"
1074" return 0.5 * GetBetaDist(fDF/(fDF+T*T),fDF/2.0, 0.5);\n"
1077const char GetBetaDecl[] =
" double GetBeta(double fAlpha, double fBeta);\n";
1079"double GetBeta(double fAlpha, double fBeta)\n"
1083" fAlpha>fBeta?(fA = fAlpha,fB = fBeta):(fA = fBeta,fB = fAlpha);\n"
1084" double fAB = fA + fB;\n"
1086" if (fAB < fMaxGammaArgument)\n"
1087" return tgamma(fA)/tgamma(fAB)*tgamma(fB);\n"
1088" double fgm = 5.524680040776729583740234375;\n"
1089" double fLanczos = lcl_getLanczosSum(fA)*lcl_getLanczosSum(fB)\n"
1090" /lcl_getLanczosSum(fAB);\n"
1091" fLanczos *= sqrt(((fAB + fgm)/(fA + fgm))/(fB + fgm));\n"
1092" return fLanczos * pow(exp(1.0),(-fA*log1p(fB/(fA + fgm)))"
1093" - fB*log1p(fA/(fB + fgm)) - fgm);\n"
1097" double GetLogBeta(double fAlpha, double fBeta);\n";
1099"double GetLogBeta(double fAlpha, double fBeta)\n"
1103" fAlpha>fBeta?(fA = fAlpha,fB = fBeta):(fA = fBeta,fB = fAlpha);\n"
1104" double fgm = 5.524680040776729583740234375;\n"
1106" double fLanczos = lcl_getLanczosSum(fA)*lcl_getLanczosSum(fB)\n"
1107" /lcl_getLanczosSum(fA + fB);\n"
1108" double fResult= -fA *log1p(fB/(fA + fgm))"
1109"-fB *log1p(fA/(fB + fgm))-fgm;\n"
1110" fResult += log(fLanczos)+0.5*(log(fA + fB + fgm) - log(fA + fgm)\n"
1111" - log(fB + fgm));\n"
1116"double GetBetaDistPDF(double fX, double fA, double fB);\n";
1118"double GetBetaDistPDF(double fX, double fA, double fB)\n"
1125" return -2.0*fX + 2.0;\n"
1126" if (fX == 1.0 && fB < 1.0)\n"
1128" return HUGE_VAL;\n"
1131" return fB + fB * expm1((fB-1.0) * log1p(-fX));\n"
1133" return fB * pow(0.5-fX+0.5,fB-1.0);\n"
1139" if (fX == 0.0 && fA < 1.0)\n"
1141" return HUGE_VAL;\n"
1143" return fA * pow(fX,fA-1);\n"
1147" if (fA < 1.0 && fX == 0.0)\n"
1149" return HUGE_VAL;\n"
1156" if (fB < 1.0 && fX == 1.0)\n"
1158" return HUGE_VAL;\n"
1163" double fLogDblMax = log( 1.79769e+308 );\n"
1164" double fLogDblMin = log( 2.22507e-308 );\n"
1165" double fLogY = (fX < 0.1) ? log1p(-fX) : log(0.5-fX+0.5);\n"
1166" double fLogX = log(fX);\n"
1167" double fAm1LogX = (fA-1.0) * fLogX;\n"
1168" double fBm1LogY = (fB-1.0) * fLogY;\n"
1169" double fLogBeta = GetLogBeta(fA,fB);\n"
1170" if ( fAm1LogX < fLogDblMax && fAm1LogX > fLogDblMin\n"
1171" && fBm1LogY < fLogDblMax && fBm1LogY > fLogDblMin\n"
1172" && fLogBeta < fLogDblMax && fLogBeta > fLogDblMin\n"
1173" && fAm1LogX + fBm1LogY < fLogDblMax && fAm1LogX + fBm1LogY > \n"
1175" return pow(fX,fA-1.0)*pow(0.5-fX+0.5,fB-1.0)/GetBeta(fA,fB);\n"
1177" return exp( fAm1LogX + fBm1LogY - fLogBeta);\n"
1181"double lcl_GetBetaHelperContFrac(double fX, double fA, double fB);\n";
1183"double lcl_GetBetaHelperContFrac(double fX, double fA, double fB)\n"
1186" double a1, b1, a2, b2, fnorm, apl2m, d2m, d2m1, cfnew, cf;\n"
1187" a1 = 1.0; b1 = 1.0;\n"
1188" b2 = 1.0 - (fA+fB)/(fA+1.0)*fX;\n"
1189" b2==0.0?(a2 = 0.0,fnorm = 1.0,cf = 1.0):\n"
1190" (a2 = 1.0,fnorm = 1.0/b2,cf = a2*fnorm);\n"
1192" double rm = 1.0;\n"
1193" double fMaxIter = 50000.0;\n"
1194" bool bfinished = false;\n"
1197" apl2m = fA + 2.0*rm;\n"
1198" d2m = (rm*(fB-rm))*fX/(apl2m*(apl2m-1.0));\n"
1199" d2m1 = -((fA+rm)*(fA+rm+fB))*fX/(apl2m*(apl2m+1.0));\n"
1200" a1 = (a2+d2m*a1)*fnorm;\n"
1201" b1 = (b2+d2m*b1)*fnorm;\n"
1202" a2 = a1 + d2m1*a2*fnorm;\n"
1203" b2 = b1 + d2m1*b2*fnorm;\n"
1207" cfnew = a2*fnorm;\n"
1208" bfinished = (fabs(cf-cfnew) < fabs(cf)*fMachEps);\n"
1213" while (rm < fMaxIter && !bfinished);\n"
1218"double lcl_IterateInverse("
1219"double fAx, double fBx, bool* rConvError,double fp,double fDF );\n";
1221"double lcl_IterateInverse( "
1222"double fAx, double fBx, bool* rConvError,double fp,double fDF )\n"
1224" *rConvError = false;\n"
1225" double fYEps = 1.0E-307;\n"
1226" double fXEps =DBL_EPSILON;\n"
1229" double fAy = GetValue(fAx,fp,fDF);\n"
1230" double fBy = GetValue(fBx,fp,fDF);\n"
1232" unsigned short nCount;\n"
1235" for (nCount =0;nCount<1000&&!lcl_HasChangeOfSign(fAy,fBy);nCount++)\n"
1237" inter = 2.0 * (fAx - fBx);\n"
1238" if (fabs(fAy) <= fabs(fBy)) \n"
1258" fTemp = GetValue(fTemp,fp,fDF);\n"
1259" sign?(fAy = fTemp):(fBy = fTemp);\n"
1265" if (!lcl_HasChangeOfSign( fAy, fBy))\n"
1267" *rConvError = true;\n"
1270" double fPx = fAx;\n"
1271" double fPy = fAy;\n"
1272" double fQx = fBx;\n"
1273" double fQy = fBy;\n"
1274" double fRx = fAx;\n"
1275" double fRy = fAy;\n"
1276" double fSx = 0.5 * (fAx + fBx); \n"
1277" bool bHasToInterpolate = true;\n"
1279" while ( nCount < 500 && fabs(fRy) > fYEps &&\n"
1280" (fBx-fAx) > max( fabs(fAx), fabs(fBx)) * fXEps )\n"
1282" if (bHasToInterpolate)\n"
1284" if (fPy!=fQy && fQy!=fRy && fRy!=fPy)\n"
1286" fSx = fPx * fRy * fQy / (fRy-fPy) / (fQy-fPy)\n"
1287" + fRx * fQy * fPy / (fQy-fRy) / (fPy-fRy)\n"
1288" + fQx * fPy * fRy / (fPy-fQy) / (fRy-fQy);\n"
1289" bHasToInterpolate = (fAx < fSx) && (fSx < fBx);\n"
1292" bHasToInterpolate = false;\n"
1294" if(!bHasToInterpolate)\n"
1296" fSx = 0.5 * (fAx + fBx);\n"
1298" fPx = fAx; fPy = fAy;\n"
1299" fQx = fBx; fQy = fBy;\n"
1300" bHasToInterpolate = true;\n"
1302" fPx = fQx; fQx = fRx; fRx = fSx;\n"
1303" fPy = fQy; fQy = fRy; fRy = GetValue(fSx,fp,fDF);\n"
1304" lcl_HasChangeOfSign( fAy, fRy)?(fBx = fRx,fBy = fRy):\n"
1305" (fAx = fRx,fAy = fRy);\n"
1306" bHasToInterpolate =\n"
1307" bHasToInterpolate && (fabs(fRy) * 2.0 <= fabs(fQy));\n"
1313"double phi(double x);\n";
1315"double phi(double x)\n"
1317" return 0.39894228040143268 * exp(-(x * x) / 2.0);\n"
1320"double taylor(double* pPolynom, uint nMax, double x);\n";
1322"double taylor(double* pPolynom, uint nMax, double x)\n"
1324" double nVal = pPolynom[nMax];\n"
1325" for (short i = nMax-1; i >= 0; i--)\n"
1327" nVal = pPolynom[i] + (nVal * x);\n"
1333"double gauss(double x)\n"
1335" double xAbs = fabs(x);\n"
1336" uint xShort = (uint)(floor(xAbs));\n"
1337" double nVal = 0.0;\n"
1338" if (xShort == 0)\n"
1341" { 0.39894228040143268, -0.06649038006690545, 0.00997355701003582,\n"
1342" -0.00118732821548045, 0.00011543468761616, -0.00000944465625950,\n"
1343" 0.00000066596935163, -0.00000004122667415, 0.00000000227352982,\n"
1344" 0.00000000011301172, 0.00000000000511243, -0.00000000000021218 };\n"
1345" nVal = taylor(t0, 11, (xAbs * xAbs)) * xAbs;\n"
1347" else if ((xShort >= 1) && (xShort <= 2))\n"
1350" { 0.47724986805182079, 0.05399096651318805, -0.05399096651318805,\n"
1351" 0.02699548325659403, -0.00449924720943234, -0.00224962360471617,\n"
1352" 0.00134977416282970, -0.00011783742691370, -0.00011515930357476,\n"
1353" 0.00003704737285544, 0.00000282690796889, -0.00000354513195524,\n"
1354" 0.00000037669563126, 0.00000019202407921, -0.00000005226908590,\n"
1355" -0.00000000491799345, 0.00000000366377919, -0.00000000015981997,\n"
1356" -0.00000000017381238, 0.00000000002624031, 0.00000000000560919,\n"
1357" -0.00000000000172127, -0.00000000000008634, 0.00000000000007894 };\n"
1358" nVal = taylor(t2, 23, (xAbs - 2.0));\n"
1360" else if ((xShort >= 3) && (xShort <= 4))\n"
1363" { 0.49996832875816688, 0.00013383022576489, -0.00026766045152977,\n"
1364" 0.00033457556441221, -0.00028996548915725, 0.00018178605666397,\n"
1365" -0.00008252863922168, 0.00002551802519049, -0.00000391665839292,\n"
1366" -0.00000074018205222, 0.00000064422023359, -0.00000017370155340,\n"
1367" 0.00000000909595465, 0.00000000944943118, -0.00000000329957075,\n"
1368" 0.00000000029492075, 0.00000000011874477, -0.00000000004420396,\n"
1369" 0.00000000000361422, 0.00000000000143638, -0.00000000000045848 };\n"
1370" nVal = taylor(t4, 20, (xAbs - 4.0));\n"
1374" double asympt[] = { -1.0, 1.0, -3.0, 15.0, -105.0 };\n"
1375" nVal = 0.5 + phi(xAbs) * taylor(asympt, 4, 1.0/(xAbs * xAbs))/xAbs;\n"
const char lcl_GetGammaHelperDecl[]
const char GetGammaContFraction[]
const char GetFInvValue[]
const char lcl_GetLogGammaHelperDecl[]
const char GetGammaInvValueDecl[]
const char fMachEpsDecl[]
const char GetLogBetaDecl[]
const char lcl_IterateInverseBetaInv[]
const char GetChiSqDistPDF[]
const char lcl_IterateInverseDecl[]
const char lcl_IterateInverseBetaInvDecl[]
const char lcl_GetGammaHelper[]
const char GetBetaDistPDFDecl[]
const char GetUpRegIGamma[]
const char fMaxGammaArgumentDecl[]
const char gaussinvDecl[]
const char fsum_countDecl[]
const char GetFInvValueDecl[]
const char lcl_GetBetaHelperContFracDecl[]
const char GetBetaDistDecl[]
const char GetGammaInvValue[]
const char GetLogGammaDecl[]
const char GetBinomDistPMF[]
const char GetLowRegIGamma[]
const char lcl_IterateInverseChiInv[]
const char GetChiDistDecl[]
const char lcl_IterateInverseChiSQInvDecl[]
const char GetValueDecl[]
const char GetChiSqDistPDFDecl[]
const char lcl_IterateInverse[]
const char lcl_GetBinomDistRange[]
const char lcl_IterateInverseChiInvDecl[]
const char GetLowRegIGammaDecl[]
const char lcl_getLanczosSumDecl[]
const char GetGammaSeriesDecl[]
const char fmin_countDecl[]
const char GetGammaDistPDFDecl[]
const char lcl_HasChangeOfSignDecl[]
const char lcl_GetLogGammaHelper[]
const char GetFDistDecl[]
const char lcl_GetBinomDistRangeDecl[]
const char fHalfMachEpsDecl[]
const char lcl_HasChangeOfSign[]
const char GetChiSqDistCDFDecl[]
const char fLogDblMaxDecl[]
const char GetTDistDecl[]
const char GetGammaContFractionDecl[]
const char lcl_IterateInverseChiSQInv[]
const char lcl_getLanczosSum[]
const char GetGammaDistPDF[]
const char GetChiSqDistCDF[]
const char GetGammaSeries[]
const char GetGammaDistDecl[]
const char GetUpRegIGammaDecl[]
const char GetBinomDistPMFDecl[]
const char fmax_countDecl[]
const char GetGammaDist[]
const char GetBetaDistPDF[]
const char lcl_GetBetaHelperContFrac[]