Tuesday, November 17, 2009

Chi-Square Test


Write a c/c++ program to find Goodness of Fit Test for a given random number by
1. Chi-Square test for Poisson
2. Chi-Square test for Exponential



SOURCE CODE

#include
#include
#include
#include
void poisson();
void exponential();

void main()
{
int op, flag=1;
clrscr();
while(flag==1)
{
printf("\n1.poisson");
printf("\n2.exponential");
printf("\n3.Exit");
printf("\n Enter the above option");
scanf("%d",&op);
if (op>3)
{
printf("give the correct option");
getch();
exit(0);
}
switch(op)
{
case 1: poisson();
break;
case 2: exponential();
break;
case 3:exit(0);
}
}
getch();
}
void poisson()
{
double OEE[12],x,a,p[12],E[12],OE,OE2;
int i,j,k=12,f,O[12],sum=0;
double facti,s;

printf("\t*** Chi-square Test Applied to Poisson Assumption ***\n\n");
printf("Enter 12 Observed Frequency Values:\n");
for(i=0;i<12;i++)
{
scanf("%d",&O[i]);
}
for(i=0;i<12;i++)
{
sum=sum+(i*O[i]);
}
a=(float)sum/100;
p[0]=(float)(exp(-a));
E[0]=100.0*p[0];

for(i=1;i<12;i++)
{
facti=1.0;
for(s=1.0;s<=i;s++)
{
facti=facti*s;
}
p[i]=(exp(-a)*pow(a,i))/facti;
E[i]=100.0*p[i];
}
for(i=0;i {
if(E[i]<5)
{
E[i]=E[i]+E[i+1];
O[i]=O[i]+O[i+1];
for(j=i+1;j+1 {
E[j]=E[j+1];
O[j]=O[j+1];
}
if(i!=8)
{
for(s=k-1;s<12;s++)
{
E[s]=0.0;O[s]=0;
}
}
k=k-1;
}
}
k=k+1;
for(i=k-1;i>=0;i--)
{
if(E[i]<5)
{
E[i-1]=E[i]+E[i-1];
O[i-1]=O[i]+O[i-1];
for(s=i;s<12;s++)
{
E[s]=0.0;O[s]=0;
}
k=k-1;
}
}

for(i=0;i<7;i++)
{
OE=(float)O[i]-E[i];
OE2=OE*OE;
OEE[i]=OE2/E[i];
x=x+OEE[i];
}
printf("-----------------------------------------------------\n");
printf("xi\tObserved\tExpected\t(Oi-Ei)^2/Ei\n\tfrequency,Oi\tfrequency,Ei\n");
printf("-----------------------------------------------------\n");

for(i=0;i<7;i++)
{
printf("%d\t%5d\t\t%2.1f\t\t%2.2f\n",i,O[i],E[i],OEE[i]);
}
printf("-----------------------------------------------------\n");
printf("\t\t\t\t\tx:%0.2f\n",x);
printf("-----------------------------------------------------\n");
printf("\nSince x0=%0.2f and x0.05=11.1\nNull hypothesis is rejected.",x);

}

void exponential()
{
float p=0.125,l=0.084,a[10],E,OEE[10],OE,OE2,x;
int i,O[10];

printf("*** Chi-square test for exponential distribution ***\n");
printf("\nEnter 8 observed freq values:\n");

for(i=0;i<7;i++)
{
scanf("%d\n",&O[i]);
}
E=p*50.0;
a[0]=0;

for(i=1;i<7;i++)
{
a[i]=-(1/l)*log(1-(i*p));
}

for(i=0;i<7;i++)
{
OE=(float)O[i]-E;
OE2=OE*OE;
OEE[i]=OE2/E;
x=x+OEE[i];
}
printf("\n----------------------------------------------------------\n");
printf("Class Interval\tObserved\tExpected\t(Oi-Ei)^2/Ei\n\t\tfrequency,Oi\tfrequency,Ei\n");
printf("------------------------------------------------------------\n");

for(i=0;i<7;i++)
{
printf("[%0.3f,%0.3f)\t%5d\t\t%5.2f\t\t%5.2f\n",a[i],a[i+1],O[i],E,OEE[i] );
}
printf("[%0.3f,--)\t%5d\t\t%5.2f\t\t%5.2f\n",a[7],O[7],E,OEE[7]);
printf("------------------------------------------------------------\n");
printf("\t\t\t\t\t\tx=%4.1f\n",x);
printf("------------------------------------------------------------\n");
printf("Since x0=%0.1f and x0.05=12.6\nNull hypothesis is rejected.",x);

}

No comments:

Post a Comment