Thursday, 21 April 2016

Chi Square Test



import java.io.*;

class Main
{
            public static void main(String args[])
            {
                        try
                        {
                                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                                    System.out.println("Enter total numbers : ");
                                    int n = Integer.parseInt(br.readLine());
                                    double a[] = new double[n];
                                    System.out.println("Enter numbers : ");
                                    for (int i = 0; i < n; i++)            
                                    {
                                                            a[i] = Double.parseDouble(br.readLine());
                                    }
                                    double X,E;
                                    System.out.println("Enter critcal value : ");
                                    X = Double.parseDouble(br.readLine());
                                    System.out.println("Enter expected value : ");
                                    E = Double.parseDouble(br.readLine());
                                    ChiSquareTest cs=new ChiSquareTest();
                                    cs.chisqr(a,X,E);
                                   
                        }
                        catch(Exception e)         
                        {
                        }
            }
}

class ChiSquareTest
{
            public void chisqr(double a[],double X,double E)
            {
                        double b[]=new double[a.length];
                        double sum=0.0;
                        for(int i=0;i<a.length;i++)
                        {
                                    b[i]=a[i]-E;
                                    b[i]=b[i]*b[i];
                                    b[i]=(b[i]/E)+(b[i]%E);
                                    sum+=b[i];
                        }
                       
                        if(X>sum)
                        {
                                    System.out.println("Hypothesis accepted");
                        }
                        else
                        {
                                    System.out.println("Hypothesis rejected");
                        }
            }         
}
                                                     





Infinite Markovian

#include<stdio.h>
#include<conio.h>
#include<math.h>
struct infi
{ int s;
float m,l,p,lq,w,wq;
};
void main()
{ char c;
int i;
float l;
struct infi v[2];
clrscr();
printf("\nEnter rate per hour: ");
scanf("%f",&l);
printf("\nEnter average service time of Pawan: ");
scanf("%f",&v[0].m);
printf("\nEnter standard deviation: ");
scanf("%d",&v[0].s);
printf("\nEnter average service time of Ramdas: ");
scanf("%f",&v[1].m);
printf("\nEnter standard deviation: ");
scanf("%d",&v[1].s);
l=l/60;

for(i=0;i<2;i++)
{ v[i].m=1/v[i].m;
v[i].p=l/v[i].m;
v[i].l=v[i].p+((l*l)*(1/pow(v[i].m,2)+pow(v[i].s,2)))/(1-v[i].p);
v[i].w=1/v[i].m + (l*(1/v[i].m+pow(v[i].s,2))/(2*(1-v[i].p)));
v[i].wq=(l*(1/pow(v[i].m,2) + pow(v[i].s,2)))/(2*(1-v[i].p));
v[i].lq=(pow(v[i].p,2)*(1+pow(v[i].s,2)*pow(v[i].m,2)))/(2*(1-v[i].p));
}

printf("\n\t------------------------------------------");
printf("\n\t Parameters||\tPawan\t|\tRamdas");
printf("\n\t------------------------------------------");
printf("\n\t P\t   ||\t%.2f\t|\t%.2f",v[0].p,v[1].p);
printf("\n\t L\t   ||\t%.2f\t|\t%.2f",v[0].l,v[1].l);
printf("\n\t Lq\t   ||\t%.2f\t|\t%.2f",v[0].lq,v[1].lq);
printf("\n\t w(mins)   ||\t%.2f\t|\t%.2f",v[0].w,v[1].w);
printf("\n\t wq(mins)  ||\t%.2f\t|\t%.2f",v[0].wq,v[1].wq);
printf("\n\t------------------------------------------");
printf("\n\nP = Steady State");
printf("\nL = No. of customers in System");
printf("\nLq = No.of customers in queue");
printf("\nw = Time spent in System");
printf("\nwq = Time spent in queue");
if(v[0].lq<v[1].lq)
printf("\n\n\tPawan will be Hired.");
else
printf("\n\n\tRamdas will be Hired.");
getch();
}


Uniform Distribution

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<stdlib.h>
float f[20],F[20];
int f1[20],F1[20];
int a,b,i,j,k;
int gm=EGAHI,gd=EGA,errorcode;
float temp[5],mean,var,a1,b1;
char str[20],str1[20];
void display(char str[],float t[],int f1[],int ch)
{ int i,j,k;
float temp[20];
char str1[20];
outtextxy(0,0,str);
for(i=0;i<=12;i++)
{ line(40*i,10,40*(i+1),10);
line(40*i,50,40*(i+1),50);
line(40*i,10,40*i,50);
}
line(40*i,10,40*i,50);
line(0,30,40*13,30);
line(38,10,38,50);
outtextxy(5,20,"x");
if(ch==0)
outtextxy(5,40,"f(x)");
else
outtextxy(5,40,"F(x)");
for(i=1,j=1;i<=12;i++,j++)
{ temp[j]=t[j];
sprintf(str,"%2d",j);
sprintf(str1,"%.2f",temp[j]);
outtextxy(40*i+5,20,str);
outtextxy(40*i+5,40,str1);
delay(150);
}
for(i=0,j=0;i<225;i++,j+=2)
{ putpixel(100,getmaxy()-50-i,getmaxcolor());
putpixel(100+j,getmaxy()-50,getmaxcolor());
putpixel(100+j+1,getmaxy()-50,getmaxcolor());
delay(10);
}
for(i=0;i<10;i++)
{ putpixel(550-i,getmaxy()-50-i,getmaxcolor());
putpixel(550-i,getmaxy()-50+i,getmaxcolor());
putpixel(100-i,getmaxy()-275+i,getmaxcolor());
putpixel(100+i,getmaxy()-275+i,getmaxcolor());
delay(25);
}
settextstyle(0,VERT_DIR,1);
if(ch==0)
outtextxy(50,150,"f(x)->");
else
outtextxy(50,150,"F(x)->");
settextstyle(0,HORIZ_DIR,1);
outtextxy(300,325,"x->");
for(i=0,j=0;i<12;i++,j++)
{ if(j<=10)
{ sprintf(str,"%4.2f",(float)j/10);
putpixel(99,getmaxy()-50-j*20,getmaxcolor());
putpixel(98,getmaxy()-50-j*20,getmaxcolor());
outtextxy(60,getmaxy()-50-j*20,str);
}
line(100+(i*30),getmaxy()-50-f1[i]*2,100+(i+1)*30,getmaxy()-50-f1[i+1]*2);
sprintf(str,"%2d",i);
putpixel(100+(i*30),getmaxy()-49,getmaxcolor());
putpixel(100+(i*30),getmaxy()-48,getmaxcolor());
outtextxy(90+(i*30),getmaxy()-40,str);
delay(150);
}
putpixel(100+(i*30),getmaxy()-49,getmaxcolor());
putpixel(100+(i*30),getmaxy()-48,getmaxcolor());
outtextxy(90+(i*30),getmaxy()-40,"12");
getch();
cleardevice();
}
void main()
{ clrscr();

printf("\nEnter value of intervals.\na:");
scanf("%d",&a);
printf("b:");
scanf("%d",&b);
mean=(float)(a+b)/2;
var=(float)(b-a)*(b-a)/2;
a1=mean;b1=var;
printf("\nMean = %.2f",mean);
printf("\nVariance = %.2f",var);
getch();
for(i=0,j=100;i<=12;i++)
{ if(i>=a && i<=b)
f[i]=(float)1/(b-a);
else
f[i]=0;
f1[i]=f[i]*j;
}
for(i=0,j=100;i<=12;i++)
{ if(i<a)
F[i]=0;
else
{ if(i>=a && i<=b)
F[i]=(float)(i-a)/(b-a);
else
F[i]=1;
}
F1[i]=F[i]*j;
}
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
errorcode=graphresult();
sprintf(str,"The Probability density function is as follows:");
display(str,f,f1,0);
sprintf(str,"The Cummulative density function is as follows:");
display(str,F,F1,1);
closegraph();
}

Poisson Distribution

#include<stdio.h>
#include<math.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
int a,x,i,j,k,p1[20],f1[20],gd=DETECT,gm;
float p[20],f[20];
char str[20];
int fact(int i)
{ if(i==0)
return(1);
else
return(i*fact(i-1));
}
void display(char str[],float t[],int f1[],int ch)
{ int i,j,k;
float temp[20];
char str1[20];
outtextxy(0,0,str);
for(i=0;i<=13;i++)
{   line(40*i,10,40*(i+1),10);
line(40*i,50,40*(i+1),50);
line(40*i,10,40*i,50);
}
line(40*i,10,40*i,50);
line(0,30,40*14,30);
line(38,10,38,50);
outtextxy(5,20,"x");
if(ch==0)
outtextxy(5,40,"f(x)");
else
outtextxy(5,40,"F(x)");
for(i=0,j=0;i<=12;i++,j++)
{   temp[j]=t[j];
sprintf(str,"%2d",j);
sprintf(str1,"%.2f",t[j]);
outtextxy(40*i+45,20,str);
outtextxy(40*i+45,40,str1);
delay(150);
}
for(i=0,j=0;i<225;i++,j+=2)
{   putpixel(100,getmaxy()-50-i,getmaxcolor());
putpixel(100+j,getmaxy()-50,getmaxcolor());
putpixel(100+j+1,getmaxy()-50,getmaxcolor());
delay(10);
}
for(i=0;i<10;i++)
{   putpixel(550-i,getmaxy()-50-i,getmaxcolor());
putpixel(550-i,getmaxy()-50+i,getmaxcolor());
putpixel(100-i,getmaxy()-275+i,getmaxcolor());
putpixel(100+i,getmaxy()-275+i,getmaxcolor());
delay(25);
}
settextstyle(0,VERT_DIR,1);
if(ch==0)
outtextxy(50,300,"p(x)->");
else
outtextxy(50,300,"F(x)->");
settextstyle(0,HORIZ_DIR,1);
outtextxy(300,getmaxy()-30,"x->");
for(i=0,j=0;i<12;i++,j++)
{ if(j<=10)
{   sprintf(str,"%4.2f",(float)j/10);
putpixel(99,getmaxy()-50-j*20,getmaxcolor());
putpixel(98,getmaxy()-50-j*20,getmaxcolor());
outtextxy(60,getmaxy()-50-j*20,str);
}
//line(100+(i*30),getmaxy()-50-f1[i]*100,100+(i*30),getmaxy()-50-f1[i]*100);
sprintf(str,"%2d",i);
putpixel(100+(i*30),getmaxy()-49,getmaxcolor());
putpixel(100+(i*30),getmaxy()-48,getmaxcolor());
outtextxy(90+(i*30),getmaxy()-40,str);
delay(150);
}
putpixel(100+(i*30),getmaxy()-49,getmaxcolor());
putpixel(100+(i*30),getmaxy()-48,getmaxcolor());
outtextxy(90+(i*30),getmaxy()-40,"12");
}
void main()
{
clrscr();

printf("Enter values of x:");
scanf("%d",&x);
printf("Enter value of alpha:");
scanf("%d",&a);
printf("\nMean = Variance = %d",a);
getch();
for(i=0;i<=12;i++)
{
if(i<=x)
{
p[i]=pow(M_E,-a)*pow(a,i)/fact(i);
}
else
p[i]=0;
p1[i]=p[i];
}
f1[0]=f[0]=p[0];
for(i=1;i<=12;i++)
{ f[i]=f[i-1]+p[i];
f1[i]=f[i];
}
initgraph(&gd,&gm,"c:\\tc\\bgi");
sprintf(str,"The Probability density function is as follows:");
display(str,p,p1,0);
for(i=0;i<=12;i++)
{ setcolor(getmaxcolor());
line(100+(i*30),getmaxy()-50,100+(i*30),getmaxy()-50-(p[i]*200));
}
getch();
cleardevice();
sprintf(str,"The Cummulative density function is as follows:");
f1[0]=f[0]=p[0];
for(i=1;i<=12;i++)
{
f[i]=f[i-1]+p[i];
f1[i]=(f[i]*100);
}
display(str,f,f1,1);
for(i=0;i<12;i++)
{ setcolor(getmaxcolor());
line(100+(i*30),getmaxy()-50-(f[i]*200),100+((i+1)*30),getmaxy()-50-(f[i]*200));
line(130+(i*30),getmaxy()-50-(f[i]*200),130+((i)*30),getmaxy()-50-(f[i+1]*200));
}
getch();
cleardevice();
closegraph();
}

Random Number

#include<stdio.h>
#include<conio.h>
int a,x[20],M,c,i;
float r[20];
void main()
{
clrscr();

printf("\nEnter value of a:");
scanf("%d",&a);
printf("\nEnter value of Xo:");
scanf("%d",&x[0]);
printf("\nEnter value of c:");
scanf("%d",&c);
printf("\nEnter value of M:");
scanf("%d",&M);

printf("\nUsing LINEAR CONGRUENTIAL METHOD:\nThe random numbers generated are as follows:\n");
for(i=1;i<=10;i++)
{
x[i]=(a*x[i-1]+c)%M;
r[i]=(float)x[i]/M;
printf("\nX[%d] = %d\tr[%d] = %.2f",i,x[i],i,r[i]);
}

getch();

}

M,N Inventory System

/*
DESCRIPTION;
IN simulating real world systems on computer like a Inventory system of some manufacturing or supply sector consisting of demand,supply and demand estimation or shortage probability.so we have its following simulation:
*/

import java.io.*;

class Inventory
{ public static void main(String a[])throws Exception
{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,j,no;
System.out.println("ENTER THE NUMBER OF WEEKS.");
no=Integer.parseInt(br.readLine());
int demand[]={0,1,2,3,4};
float demandprob[]={0.10f,0.25f,0.35f,0.21f,0.09f};
float cumdemand[]=new float[demand.length];
int demandrda[]=new int[demand.length];
int lead[]={1,2,3};
float leadprob[]={0.60f,0.30f,0.10f};
float cumlead[]=new float[lead.length];
int leadrda[]=new int[lead.length];
for(i=0;i<demand.length;i++)
{ if(i==0)
{ cumdemand[i]=demandprob[i];
demandrda[i]=(int)(cumdemand[i]*100);
}

else
{ cumdemand[i]=cumdemand[i-1]+demandprob[i];
demandrda[i]=(int)(cumdemand[i]*100);
}
}
for(i=0;i<lead.length;i++)
{ if(i==0)
{ cumlead[i]=leadprob[i];
leadrda[i]=(int)(cumlead[i]*100);
}
else
{ cumlead[i]=cumlead[i-1]+leadprob[i];
leadrda[i]=(int)(cumlead[i]*100);
}
}
int end[]=new int[no*5];
int beg[]=new int[no*5];
int dem[]=new int[no*5];
int lea[]=new int[no];
int short1[]=new int[no*5];
int randomdem[]=new int[no*5];
int randomlead[]=new int[no];
int order[]=new int[no];
for(i=0;i<no*5;i++)
randomdem[i]=(int)(Math.random()*100d);
for(i=0;i<no;i++)
randomlead[i]=(int)(Math.random()*100d);
for(i=0;i<no*5;i++)
for(j=0;j<demandrda.length;j++)
{ if(randomdem[i]==0)
{ dem[i]=demand[demand.length-1];
break;
}
if(randomdem[i]<=demandrda[j])
{ dem[i]=demand[j];
break;
}
}
for(i=0;i<no;i++)
for(j=0;j<leadrda.length;j++)
{ if(randomlead[i]==0)
{ lea[i]=lead[lead.length-1];
break;
}
if(randomlead[i]<=leadrda[j])
{ lea[i]=lead[j];
break;
}
}
beg[0]=11;
int week=0;
int sho=0;
for(i=0;i<no*5;i++)
{ if((dem[i]+sho)<=beg[i])
{ end[i]=beg[i]-dem[i]-sho;
sho=0;
if(i<(no*5-1))
beg[i+1]+=end[i];
}
else
{ end[i]=0;
short1[i]=dem[i]+sho-beg[i];
sho=short1[i];
}
if((i%5)==4&&i<(no*5-1))
{ order[week]=11-end[i]+sho;
beg[i+lea[week]]+=order[week];
week++;
}
}
System.out.println("WEE\tDAY\tBEG\tRDE\tDEM\tEND\tSHO\tORD\tRLE\tLEA");
System.out.println("--------------------------------------------------------------------------");
week=0;
for(i=0;i<no*5;i++)
{ if((i%5)==0&&i>0)
week++;
System.out.println((week+1)+"\t"+((i%5)+1)+"\t"+beg[i]+"\t"+randomdem[i]+"\t"+dem[i]+"\t"+end[i]+"\t"+short1[i]+"\t"+order[week]+"\t"+randomlead[week]+"\t"+lea[week]);
System.out.println("------------------------------------------------------------------------------");

}

}

}
/*OUTPUT
ENTER THE NUMBER OF WEEKS.
5
WEE     DAY     BEG     RDE     DEM     END     SHO     ORD     RLE     LEA
-----------------------------------------------------------------------------
1       1       11      42      2       9       0       9       87      2
------------------------------------------------------------------------------
1       2       9       48      2       7       0       9       87      2
------------------------------------------------------------------------------
1       3       7       5       0       7       0       9       87      2
------------------------------------------------------------------------------
1       4       7       56      2       5       0       9       87      2
------------------------------------------------------------------------------
1       5       5       87      3       2       0       9       87      2
------------------------------------------------------------------------------
2       1       2       6       0       2       0       9       36      1
------------------------------------------------------------------------------
2       2       11      16      1       10      0       9       36      1
------------------------------------------------------------------------------
2       3       10      65      2       8       0       9       36      1
------------------------------------------------------------------------------
2       4       8       87      3       5       0       9       36      1
------------------------------------------------------------------------------
2       5       5       72      3       2       0       9       36      1
------------------------------------------------------------------------------
3       1       11      44      2       9       0       11      79      2
------------------------------------------------------------------------------
3       2       9       54      2       7       0       11      79      2
------------------------------------------------------------------------------
3       3       7       35      1       6       0       11      79      2
------------------------------------------------------------------------------
3       4       6       41      2       4       0       11      79      2
------------------------------------------------------------------------------
3       5       4       98      4       0       0       11      79      2
------------------------------------------------------------------------------
4       1       0       26      1       0       1       9       21      1
------------------------------------------------------------------------------
4       2       11      45      2       8       0       9       21      1
------------------------------------------------------------------------------
4       3       8       59      2       6       0       9       21      1
------------------------------------------------------------------------------
4       4       6       54      2       4       0       9       21      1
------------------------------------------------------------------------------
4       5       4       45      2       2       0       9       21      1
------------------------------------------------------------------------------
5       1       11      60      2       9       0       0       8       1
------------------------------------------------------------------------------
5       2       9       38      2       7       0       0       8       1
------------------------------------------------------------------------------
5       3       7       3       0       7       0       0       8       1
------------------------------------------------------------------------------
5       4       7       53      2       5       0       0       8       1
------------------------------------------------------------------------------
5       5       5       39      2       3       0       0       8       1

Two Channel Queuing System

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{ int nt,np,i,j;
int t[10],at[10],bt[10];
float p[20],ap[20],bp[20],ct[20],ca[20],cb[20];
int ra[20],rs[20];
clrscr();
printf("Enter the no. of time between arrivals:");
scanf("%d",&nt);
printf("Enter the no. of service time: ");
scanf("%d",&np);
printf("Enter the time between arrivals:");
for(i=0;i<nt;i++)
scanf("%d%f",&t[i],&p[i]);
printf("Enter the Service time for Able:");
for(i=0;i<np;i++)
scanf("%d%f",&at[i],&ap[i]);
printf("Enter the Service time for Baker:");
for(i=0;i<np;i++)
scanf("%d%f",&bt[i],&bp[i]);
//Calculating cumulative freqs
ct[0]=p[0];ca[0]=ap[0];cb[0]=bp[0];
for(i=1;i<nt;i++)
ct[i]= ct[i-1]+p[i];
for(i=1;i<np;i++)
{ ca[i]=ca[i-1]+ap[i];
cb[i]=cb[i-1]+bp[i];
}
randomize();
for(i=0;i<12;i++)
{ ra[i]=rand()%100;
rs[i]=rand()%100;
}
int tba[20],atsb[20],btsb[20],ast[20],bst[20],atse[20],btse[20];
int tq[20],cta[20],ab[20],st[20],tsb[20],tse[20];
ra[0]=0;
for(i=0;i<12;i++)
{ for(j=0;j<nt;j++)
if(ra[i]< (ct[j]*100))
{ tba[i]=t[j];
break;
}
}
for(i=0;i<12;i++)
{ for(j=0;j<np;j++)
{ if(rs[i]< (ca[j]*100))
{ ast[i]=at[j];
break;
}
if(rs[i]< (cb[j]*100))
{ bst[i]=bt[j];
break;
}
}
}
tba[0]=0;cta[0]=0;tsb[0]=0;st[0]=ast[0];
tse[0]=tsb[0]+st[0];tq[0]=0;ab[0]=0;btse[0]=0;
for(i=1;i<12;i++)
{ cta[i]=cta[i-1]+tba[i];
if(tse[i-1]<=cta[i])
{ ab[i]=0;
tsb[i]=cta[i];
st[i]=ast[i];
tse[i]=tsb[i]+st[i];
}
else if(tse[i-1]>cta[i])
{ if(btse[i-1]<=tse[i-1])
{ ab[i]=1;
tsb[i]=cta[i];
st[i]=bst[i];
tse[i]=tsb[i]+st[i];
btse[i]=tse[i];
}
else
{ ab[i]=0;
tsb[i]=tse[i-1];
st[i]=ast[i];
tse[i]=tsb[i]+st[i];
}
}
tq[i]=tsb[i]-cta[i];
}
printf("\nThe Simulation Table is as follows:\n");
printf("\t\t\tAble        Baker\n");
printf("\nAoc  rda tba cta rds tsb  st tse tsb  st tse  tq");
for(i=0;i<5;i++)
{ printf("\n%2d   %2d   %2d  %2d  %2d",i+1,ra[i],tba[i],cta[i],rs[i]);
if(ab[i]==0)
printf("  %2d  %2d  %2d  --  --  --  ",tsb[i],st[i],tse[i]);
else
printf("  --  --  --  %2d  %2d  %2d  ",tsb[i],st[i],tse[i]);
printf("%2d",tq[i]);
}
getch();
}