Tuesday, November 17, 2009

Continuous Distribution

1. Write a C/C++ / Excel Program to find the Probability Density Function (pdf) and Cumulative disturibution function (cdf) for the Uniform Distribution. Accept the prameters, a and b from the user for the random variable X which is distributed uniformly between a and b.
2. Write a C/C++ / Excel Program to find the Probability Density Function
(pdf) and Cumulative disturibution function (cdf) for the Exponential Distribution. Accept the prameters of distribution λ>0

3. Write a C/C++ / Excel Program to find the Probability Density Function
(pdf) and Cumulative disturibution function (cdf) for the Gamma Distribution. Accept the prameters of distribution β and θ from the user.

4. Write a C/C++ / Excel Program to find the Probability Density Function
(pdf) and Cumulative disturibution function (cdf) for the Erlang Distribution. Accept the order k of the distribution and the parameters k2 from the user.

5. Write a C/C++ / Excel Program to find the Probability Density Function (pdf) and Cumulative disturibution function (cdf) for the Weibull Distribution. Accept for a random variable x with location parameter (υ), scale parameter (α) > 0 and the shape parameter (β)> 0 from the user.

6. Write a C/C++ / Excel Program to find the Probability Density Function (pdf) and Cumulative disturibution function (cdf) for the Triangular Distribution. Accept parameters a,b,c where a<=b<=c from the user.



SOURCE CODE

#include
#include
#include
#include

const int X[40];
void uniform();
void exponential();
void gamma();
void erlang();
void weibull();
void triangular();
void main()
{
int ch,j,flag=1,op;
clrscr();
while(flag==1)
{
printf("\n1:Uniform Distribution");
printf("\n2:Exponential Distribution");
printf("\n3:Gamma Distribution");
printf("\n4:Erlang Distribution");
printf("\n5:Weibull Distribution");
printf("\n6:Triangular Distribution");
printf("\n7:Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
uniform();
break;
case 2:
exponential();
break;
case 3:
gamma();
break;
case 4:
erlang();
break;
case 5:
weibull();
break;
case 6:
triangular();
break;
case 7:
exit(0);
break;
default:
flag=0;
exit(0);
}

getch();
}
}
void uniform()
{
float a,b,pdf,cdf,Ex,Vx,x;
int n;
clrscr();
printf("**********UNIFORM DISTRIBUTION************");
printf("\nenter value for a and b : ");
scanf("%f%f",&a,&b);
printf("\nenter value for x = ");
scanf("%f",&x);
printf("\nEnter your choice\n1*PDF\n2*CDF\n");
scanf("%d",&n);
switch(n)
{
case 1:
if (a<= x <=b)
pdf=1/(b-a);
else
pdf=0;
printf("pdf=%f",pdf);
break;
case 2:
if(x cdf=0;
else if(a<= x cdf=(x-a)/(b-a);
else if(x>=b)
cdf=1;
printf("cdf=%f",cdf);
break;
default:
printf("enter proper value");
}
Ex=(a+b)/2;
printf("E(x)=%f",Ex);
Vx=pow(b-a,2)/12;
printf("V(x)=%f",Vx);
getch();
}
/*------------------------------------------------------------------*/
void exponential()
{
float L,E,e,V,x,cdf,pdf;
int t,n;
clrscr();
printf("*************EXPONENTIAL DISTRIBUTION*******************");
printf("\nenter value for t=");
scanf("%d",&t);
printf("\nenter value for lambda & x = ");
scanf("%f%f",&L,&x);
printf("\nenter your choice");
scanf("%d",&n);
switch(n)
{
case 1:
if(x<=t)
pdf=L*exp(-L*x);
else
pdf=0;
printf("pdf=%f",pdf);
break;
case 2:
if (x>0 && x cdf=1-exp(-L*x);
else
cdf=0;
printf("cdf=%f",cdf);
break;
default:
exit(1);
}
E=1/L;
printf("E(x)=%f",E);
V=1/L*L;
printf("V(x)=%f",V);
getch();
}
/*--------------------------------------------------------------------*/
void gamma()
{
double calpdf(double, double, double, double);
double fact(double);
double x,B,T,th,pdf,b,factorial;
int i;
//B: Beta; T: Tau, th: thita,factorial : variable to calculate factorial
clrscr();
x=0;B=0;th=0;
printf("***************GAMMA DISTRIBUTION****************");
printf("\nEnter the values of x, B (beta), Thita\t");
scanf("%lf%lf%lf",&x,&B,&th);
if(B>=0 && B<=1)
{
if(th>=0 && th<=1)
{
if(B==1)
{
b=1;
}
else
{
b=B-1;
}
factorial=fact(b);
pdf=calpdf(B,th,factorial,x);
printf("Factorial= \t%lf\nPdf=\t%lf",factorial,pdf);
}
else
{
printf("\nthe value of thita should be between 0 and 1");
}
}
else
{
printf("\nthe value of beta should be between 0 and 1");
}
getch();
}
double calpdf(double Beta, double thita, double taub, double x)
{
double pdf=((Beta*thita)/taub)*(pow((Beta*thita*x),(Beta-1))*(exp(-Beta*thita*x)));
return(pdf);
}
double fact(double b)
{
int i;
double fact1=1;
for( i=1;i<=b;i++)
{
fact1=fact1*i;
}
return fact1;
}
/*--------------------------------------------------------------------------*/
void erlang()
{
double k,x,th,b,cdf,sum;
int i,j,facti,a;
clrscr();
printf("***************************************");
printf("\n \t Erling Distribution");
printf("\n***************************************");
printf("\n enter value for k x th = ");
scanf("%lf %lf %lf",&k,&x,&th);
if(x>=0)
{
facti=1;
sum=0;
for(i=0;i {
for(j=i;j>=1;j--)
{

facti=j*facti;
}
b=(exp(-(k*th*x))*pow((k*th*x),i))/facti;
sum=b+sum;
}
printf("\nsum=%lf",sum);
cdf=1-sum;
}
else
cdf=0;
printf("\ncdf=%lf",cdf);
getch();
}
/*------------------------------------------------------------------------*/
void weibull()
{
double Tau(double);
double pdf, cdf, B,a,v,x,temp,T,E,V,F;
int n=0;
clrscr();
printf("*************Weibul Distribution****************\n\n\n");
printf("Enter the values for a(alpha), B(beta), v, x");
scanf("%lf%lf%lf%lf",&a,&B,&v,&x);
printf("\nEnter Choice \n1* PDF\t2*CDF\n");
scanf("%d",&n);

switch(n)
{
case 1:
temp=0;
temp=exp(-(pow(((x-v)/a),B)));
F=(B/a)*(pow((x-v/a),(B-1)))*temp;
printf("pdf=%lf",F);
break;
case 2:
temp=0;
temp=pow((x-v)/a,B);
if(x>v)
{
F=1-(exp(-temp));
}
printf("cdf=%lf",F);
E=v+(a*Tau((1/B)+1));
V=(pow(a,2))*((Tau((2/B)+1))-Tau(pow(((1/B)+1),2)));
printf("\nE=%lf \n v=%lf",E,V);
break;
default:
printf("Enter a proper input\n");
}
getch();
}
double Tau(double t)
{
double fact1=1;
int i;
for(i=1;i {
fact1=fact1*i;
}
return fact1;
}
/*--------------------------------------------------------------------------*/
void triangular()
{
double fcdf(double,double,double,double);
double fpdf(double,double,double,double);
int n;
double a,b,c,x,pdf,cdf,E,V;
clrscr();
printf("Enter the values of a,b,c,d\n");
scanf("%lf%lf%lf%lf",&a,&b,&c,&x);
printf("Enter your choice\n1*PDF\n2*CDF\n");
scanf("%d",&n);
switch(n)
{
case 1:
pdf=fpdf(a,b,c,x);
printf("PDF=%lf",pdf);
break;
case 2:
cdf=fcdf(a,b,c,x);
printf("CDF=%lf",cdf);
break;
default:
printf("Please select appropriate input");
break;
}
E=(a+b+c)/3;
V=(3*E)-(a+c);
printf("\nMean =%lf",E);
printf("\nVariance =%lf",V);
getch();
}

double fcdf(double a,double b,double c,double x)
{
double cdf=0;
if(a {
cdf=pow((x-a),2)/((b-a)*(c-a));
}
else if(b {
cdf=1-(pow((c-x),2)/((c-b)*(c-a)));
}
else if(x<=a)
cdf=0;
else
cdf=1;
return cdf;
}
double fpdf(double a,double b,double c,double x)
{
double pdf=0;
if(a<=x && x<=b)
{
pdf=2*(x-a)/((b-a)*(c-a));
}
else if(b {
pdf=2*(c-x)/((c-b)*(c-a));
}
else pdf=0;
return pdf;
}

No comments:

Post a Comment