(PMF) -> p(x), mean -> E(x) and Variance V(x), for the Bernoulli Distribution. Accept the probability of success p, the probability of failure q, no. of trials n from the user and display the value of pmf.
2. Write a C/C++ / Excel Program to find the Probability Mass Function
(pmf)-> p(x), mean -> E(X) and V(x) variance for the binomial distribution. Accept the probability of success P, the probability Q , no of trials from the user and display the value of PMF.
3. Write a C/C++ / Excel Program to find the Probability Mass Function
(pmf)-> p(x), mean -> E(X) and V(x) variance for the Geometric Distribution. Accept the probability of success P, the probability Q , no of trials from the user and display the value of PMF.
4. Write a C/C++ / Excel Program to find the Probability Mass Function
(pmf)-> p(x), mean -> E(X) and V(x) variance for the Poisson Distribution. Accept the probability of success P, the probability Q , no of trials from the user and display the value of PMF.
#include
#include
#include
#include
const int X[40];
void bernoulli();
void binomial();
void geometric();
void poision();
void main()
{
int ch,j,flag=1,op;
clrscr();
while(flag==1)
{
printf("\n1:Bernoulli Distribution");
printf("\n2:Binomial Distribution");
printf("\n3:Geometric Distribution");
printf("\n4:Poision Distribution");
printf("\n5:exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
bernoulli();
break;
case 2:
binomial();
break;
case 3:
geometric();
break;
case 4:
poision();
break;
case 5:
exit(0);
break;
default:
flag=0;
exit(0);
}
getch();
}
}
/*-----------------------------------------------------------------------*/
void bernoulli()
{
float p1,q1,p,q,x[100],pmf;
int n,j,i;
printf("\t\t***********BERNOULLI DFISTRIBUTION************");
printf("\n\n Enter the total number of trial for an experiment:");
scanf("%d",&n);
if(n<0)
{
printf("\nentered value of n is wrong");
getch();exit(0);
}
printf("\n");
if(n>100)
{
printf("the number of trial should be within 100");
printf("\n enter the total number of trial for an experiment :");
scanf("%d",&n);
}
printf("\nEnter the trial number where in you want to check the probability :");
scanf("%d",&j);
printf("\nthe probability of succes in that experiment:");
scanf("%f",p1);
q1=1-p1;
p=p1;
q=q1;
randomize();
for(i=0;i
x[i]=random(10);
printf("\n\tx[%d]=%f",i,x[i]);
}
if(x[j]==1)
{
pmf=p;
}
else if(x[j]==0)
{
pmf=q;
}
else
{
pmf=0;
}
printf("\n\n\n\n");
printf("results:");
printf("\n\n");
printf("the pmf of the given trial is =%f",pmf);
printf("\n\nthe variance of the trial is v[x]=%f",p*q);
printf("the mean of the trial isE[x]=%f",p);
}
/*-------------------------------------------------------- */
void binomial()
{
double fact(double);
int i,j;
double Ex,Vx,r,m,n,x,prob[5],f1[5],f2[5],f3[5],p1[5],p2[5],p,q,sum[5],sum1;
clrscr();
printf("*************BINOMIAL DISTRIBUTION******************");
printf("\nenter the number of n & x");
scanf("%lf%lf",&n,&x);
r=n;
m=x;
printf("\nEnter value for p ");
scanf("%lf",&p);
q=(1-p);
sum1=0;x=0;
for(j=0;j<=m;j++)
{
f1[j]=fact(n);
f2[j]=fact(n-x);
f3[j]=fact(x);
prob[j]=(double)f1[j]/(f2[j]*f3[j]);
p1[j]=pow(p,x);
p2[j]=pow(q,r-x);
sum[j]=(prob[j]*p1[j]*p2[j]);
sum1+=sum[j];
// x++;
}
printf("p(x)=%lf\n",sum1);
Ex=n*p;
Vx=n*p*q;
printf("\nE(X)=%lf\tV(X)=%lf",Ex,Vx);
getch();
}
double fact(double b)
{
int i;
double fact1=1;
for(i=1;i<=b;i++)
{
fact1=fact1*i;
}return fact1;
}
/*-------------------------------------------------------*/
void geometric()
{
int i,j;
double Ex,Vx,x,p,q,probability;
clrscr();
printf("**************GEOMETRIC DISTRIBUTION****************");
printf("\nenter value of x");
scanf("%lf",&x);
printf("\nEnter value for p ");
scanf("%lf",&p);
q=(1-p);
probability= pow(q,x-1)*p ;
printf("\nprobability=%f",probability);
Ex=1/p;
Vx=q/(p*p);
printf("\nE(X)=%lf\tV(X)=%lf",Ex,Vx);
getch();
}
/*---------------------------------------------------------------------*/
void poision()
{
int i,x,fact1=1;
double px,lambda,Ex,Vx;
clrscr();
printf("*****************POISION DISRIBUTION****************");
printf("\nEnter the value for lambda & x");
scanf("%lf%d",&lambda,&x);
for(i=1;i<=x;i++){
fact1=fact1*i;
}
px=exp(-lambda)*pow(lambda,x)/fact1;
Ex=Vx=lambda;
printf("\nE(X)=%lf\tV(X)=%lf\np(x)=%lf",Ex,Vx,px);
getch();
}
No comments:
Post a Comment