Ralph Vince - Portfolio Mathematics

(Brent) #1

ch02 JWBK035-Vince February 12, 2007 6:50 Char Count= 0


90 HANDBOOK OF PORTFOLIO MATHEMATICS


(which is how the tables were drawn up in the first place). However, the
math requires the use of incomplete gamma functions, not be treated in this
text. However, most readers who would want to know how to calculate a
significance level from a given chi-square statistic would want to know this
because tables are rather awkward to use from a programming standpoint.
Therefore, what follows is a snippet of Java code to convert from a given
chi-square statistic to a significance level.

Public void ChiSquareTest(int nmbrOfBins, double chiSquareStatistic){
double confidenceLevel = 1.0;
double a = 0.0, b = 0.0, c=1.0, d = 0.0, e = 0.0,
f = 1.0;
int nbins = nmbrOfBins -3;
System.out.println(“Chi-Square Statistic at ” + nbins + “degrees of
freedom is ”+chiSquareStatistic);
if(chiSquareStatistic<31.0||nbins>2){
e = nbins/2 -1;
a=1;
for(int i = 1;i<=nbons/2 - .5; i++){
a*=e;
e-=1.0;
}
if(nbins% 2 !=0){
a *= 1.77245374942627;
}
b = Math.pow((chiSquareStatistic/2.0), (double) (nbins/2)) * 2.0/
(Math.exp(chiSquareStatistic/2.0)* a * nbins);
d = nbins + 2;
do{
c *=chiSquareStatistic/d;
f+=c;
d+=2.0;
}while(c>0.0);
confidenceLevel = 1.0 - b *f;
}
System.out.println(“For a Significance level of ”+confidenceLevel);
}
Free download pdf