Tuesday, October 25, 2011

Coding for distributions (geometric/weibull)




#include
#include
#include

long fact(int n)
{
if(n<=0)
return 1;
else
return n*fact(n-1);
}


double geomean(int x,double p,double q)
{
if(x>0)
return pow(q,x-1)*p;
else
return 0;
}


double weibpdf(int x, double v, double a, double b)
{
if(x return 0;
else
{
return b/a*(pow((x-v)/a,b-1))*exp(-pow((x-v)/a,b));
}
}


double weibcdf(int x, double v, double a, double b)
{
if(x return 0;
else
{
return (1 - exp(-pow((x-v)/a,b)));
}
}

double poisson(int x, double a)
{
if(x<0)
return 0;
else
{
return (exp(-a)*pow(a,x))/fact(x) ;
}
}


double binom(int x,double p,double q,int n)
{
if(x>=0)
return fact(n)/(fact(x)*fact(n-x))*pow(p,x)*pow(q,n-x);
else
return 0;
}


int main()
{
int ch;
double p,q,v,a,b;
int x,n;
clrscr();


do
{
printf("\nExit.");
printf("\nGeometric Mean.");
printf("\nWeibull Distribution.");
printf("\nPoisson Distribution.");
printf("\nBinomial Distribution.");
printf("\nEnter your choice => ");
scanf("%d",&ch);
if(ch==1)
{
printf("\n\tEnter the value of x,p,q => ");
scanf("%d%lf%lf",&x,&p,&q);
printf("\n\tGeometric Mean = %lf",geomean(x,p,q));
}
else if(ch==2)
{
printf("\n\tEnter the value of x,v,a,b => ");
scanf("%d%lf%lf%lf",&x,&v,&a,&b);
printf("\n\tWeibull pdf, f(%d) = %lf",x,weibpdf(x,v,a,b));
printf("\n\n\tWeibull cdf, F(%d) = %lf",x,weibcdf(x,v,a,b));
}

else if(ch==3)
{
printf("\n\tEnter the value of x,a => ");
scanf("%d%lf",&x,&a);
printf("\n\tPoisson Distribution, p(%d) = %lf",x,poisson(x,a));

}
else if(ch==4)
{
printf("\n\tEnter the value of x,p,q,n => ");
scanf("%d%lf%lf%d",&x,&p,&q,&n);
printf("\n\tBinomial Distribution, p(%d) = %lf",x,binom(x,p,q,n));
}
}while(ch!=0);
getch();
return 0;
}


Astha
MCA 4510/09


No comments:

Post a Comment