Aayushi Saini 20191102 B.SC (H) Statistics 5 Semester Statistical Computing Using C/C++ Programming

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

AAYUSHI SAINI

20191102

B.Sc (H) STATISTICS

5th SEMESTER

STATISTICAL COMPUTING USING


C/C++ PROGRAMMING
PRACTICAL-1(A)

AIM : Find the roots of quadratic equation (including imaginary roots).

OBJECTIVE : Write a program to find roots of quadratic equation . Take value a,b,c from user.

THEORY : The given program is coded using if-else conditional statement. First we calculated value of discriminant such
as

Using if statement we check if D>=0 then roots are real, given by:

In case of D<0, then roots are imaginary. We calculate real and imaginary parts of rootseparately,
Real part =

Imaginary part =

INPUT :
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()

{
float a,b,c, D;
double r1,r2;
printf("Enter the value of a, b and c\n");
scanf("%f" "%f""%f", &a, &b, &c);

D=(b*b)-(4*a*c);
if(D<0) /*complex root or imaginary roots*/
{
printf("First root = %.3f + j%.2f\n", -b/(double)(2*a),sqrt(-D)/(2*a));
printf("Second root = %.3f - j%.2f\n", -b/(double)(2*a),sqrt(-D)/(2*a));
}
else /*real roots*/
{
printf("Roots are real");
r1=(-b+sqrt(D))/(2*a);
r2=(-b-sqrt(D))/(2*a);
printf("\n\n Root1=%2.3f\n\n Root2=%3.3f\n",r1,r2);

}
}

OUTPUT :
Enter the value of a, b and c
13 10 7
First root = -0.385 + j0.62
Second root = -0.385 - j0.62
PRACTICAL-1(B)

AIM : To find the roots for quadratic equation(without imaginary roots).

OBJECTIVE : Write a program to find roots of quadratic equation . Take value a,b,c from user.

Theory: The given program is coded using if-else conditional statement. First we calculated
value of discriminant such as

Using if statement we check if D>=0 then roots are real, given by:

Here , if roots are imaginary then output will be designate such as “The roots are imaginary”
only.

INPUT :
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float a,b,c, D,r1,r2;
printf("Enter the value of a,b and c\n");
scanf("%f %f %f",&a,&b,&c);
D=b*b-4*a*c;

if(D<0)
printf("\n\n Roots are imaginary\n");
else
{
r1=(-b+sqrt(D))/(2.0*a);
r2=(-b-sqrt(D))/(2.0*a);
printf("\n\n Root1=%2.3f\n\n Root2= %3.3f\n",r1,r2);
}
}

OUTPUT :
Enter the value of a,b and c
10 5 7
Roots are imaginary
PRACTICAL-2

AIM : Plotting the graph .

OBJECTIVE: Write a program to plot graph –

THEORY: Concept of nested loop is used to plot the graph.


A loop is formed to calculate , for each value of y another loop is created.
'for' loop: The for loop is entry controlled loop provides a more concise loop control structure.The general form of the 'for'
loop is:
for(initialization; test expression/condition; increment)
{
body of the loop;
}
The execution of the for loop is as follows:
1) Initialization of the control variable is done first, using assignment statement such as i=1 and
count=0.
2) The value of the control variable is tested using test condition.
3) When the body of the loop is executed the control is transferred back to the „for‟ statement.

INPUT :
/*Practical-2: Plot the graph of y=f(x)=x^2 */
#include<conio.h>
#include<stdio.h>
#include <math.h>
main()

{
int i, j, s, r;
for(i=0; i<80; i++)
{
printf("-");
}

printf("\n x-axis\n");
for(s=8;s>=-8;s=s-1)
{
r=pow(s,2);
printf("|");

for(i=r;i>0;i--)
{
printf("*");
}
printf("#");

if(s==0)
{
for(j=0;j<70;j++)
{
printf("-");
}
printf("y-axis");
}
printf("\n");
}
for(i=0;i<80;i++)
{
printf("-");
}
}

OUTPUT :
---------------------------------------------------------------------

x-axis
|****************************************************************#
|*************************************************#
|************************************#
|************************#
|****************#
|*********#
|****#
|*#
|#--------------------------------------------------------------y-axis
|*#
|****#
|*********#
|****************#
|************************#
|************************************#
|*************************************************#
|****************************************************************#
PRACTICAL-3

AIM: Preparing a frequency table.

OBJECTIVE: Write a program to take raw data from the user divide the data into following categories:
0-9, 10-19, 20-29,............100 and find frequency of each.

THEORY: This program uses the array group containing 50 elements, one for each range. Each element counts those
values falling within the range of values it represents. For any value, we can determine the correct group element by
dividing the value by no. of classes has been formed.

For example: consider the value (59).The integer division by 10 yields 5.This is the element into which 59 is counted.
The initialization statement:
Int group[counter]={ ......}
Can be replaced by
Int group[counter]={}
This will initialize all elements to 0.

INPUT :
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAXVAL 50
#define COUNTER 10
main()

{
float value[MAXVAL];
int i, low, high;
int group[COUNTER]={0};
printf("Enter the data values:\n");

for(i=0; i<MAXVAL;i++)
{
scanf("%f",&value[i]);
++group[(int)(value[i])/10];
}
printf("\n");
printf("---------------------------------------------------\n");
printf("Group\t Range\t Frequency\n");
printf("---------------------------------------------------\n");

for(i=0; i<COUNTER; i++)


{
low=i*10;
if(i==10)
high=100;
else
high=low+9;

printf("%2d\t %3d to %3d\t %d \n", i+1, low,high,group[i]);


}
printf("---------------------------------------------------\n");
}

OUTPUT:
Enter the data values:
5 6 4 88 5 2 77 22 44 4 22 36 75 99 22 14 6 5 4 55 41 99 32 75 62 1 55 23 76 24
14 96 75 34 98 25 12 10 56 74 42 68 74 76 12 85 94 63 14 26

-------------------------------------------------------
Group Range Frequency
-------------------------------------------------------
1 0 to 9 10
2 10 to 19 6
3 20 to 29 7
4 30 to 39 3
5 40 to 49 3
6 50 to 59 3
7 60 to 69 3
8 70 to 79 8
9 80 to 89 2
10 90 to 99 5
-------------------------------------------------------
PRACTICAL-4

AIM: Computation of mean, variance and standard deviation (SD) for grouped frequency data.

OBJECTIVE: Write a program to find mean and variance taking data from user.

THEORY: To calculate grouped frequency data mean ,we store lower limit of groups (class interval) in an array name say
„a‟ and upper limit of grouping array name „b‟.we calculate mid-points of each group and store in another array.

where is mid-point of each group and is the frequency of ith class.

Where,

INPUT :
#include<conio.h>
#include<stdio.h>
#include<math.h>
main()
{
float value[20];
int i,low,high,sd,sumfx=0,sumf=0,sumvar=0;
float mean,var;
int group[5]={0,0,0,0,0};
float x[5];
printf("Enter the data\n");

for(i=0;i<20;i++)
{
scanf("%f",&value[i]);
++group[(int)(value[i])/5];
}
printf("\n");
printf("Group Range Frequency\n\n");

for(i=0;i<5;i++)
{
low=i*5;
if(i==5)
high=25;
else
high=low+4;
printf("%2d %3d to %3d %d\n",i+1,low,high,group[i]);
x[i]=((float)(low+high))/2;
}
printf("\nMidpoints:\n");

for(i=0;i<5;i++)
{
printf("%f\t",x[i]);
sumfx=sumfx+(x[i]*group[i]);
sumvar=sumvar+(x[i]*x[i]*group[i]);
sumf=sumf+group[i];
}
printf("\n");
mean=sumfx/(float)sumf;
var=(sumvar/(float)sumf)-(mean*mean);
sd=sqrt(var);
printf("mean=%f\n variance=%f\n sd=%f",mean,var,sd);
}

OUTPUT:
Enter the data
12 4 55 16 17 19 17 15 2 2 4 6 8 9 19 20 55 43 28 21 63 34 59 45 6 28

Group Range Frequency

1 0 to 4 4
2 5 to 9 3
3 10 to 14 1
4 15 to 19 6
5 20 to 24 2

Midpoints:
2.000000 7.000000 12.000000 17.000000 22.000000

mean=11.687500
variance=51.464844
sd=7.173900
PRACTICAL-5

AIM: Addition, Subtraction, Transpose, Trace and Multiplication of a square matrix N*N.

OBJECTIVE: Write a program to enter two nxn (square) matrices from user and calculate its sum, difference, transpose,
trace and product.

THEORY: To work on matrix, we need to declare two dimensional array.


Syntax: type array_name[row_size][column_size]
Two dimensional array take value from user using nested loop, where first loop represent rows and second loop is for
columns of matrix.
Sum: Sum of two matrix is calculated by adding each element of first matrix with corresponding element of second matrix
and sorting value in a new matrix.
Difference: Subtract each element of first matrix with corresponding element of first matrix and sorting value in new matrix
using nested loop.
Transpose: We store value of row elements of a matrix in a column of other new matrix

For e.g... If A = ; then AT =

Product: If we consider

Trace: Sum of diagonal elements of a matrix is called trace.

A= ; Trace = a + e + j

IINPUT:
#include<stdio.h>
#include<conio.h>
main()
{
int a[3][3],b[3][3],c[3][3],d[3][3],transpose[3][3],mult[3][3]={0,0},trace;
int i,j,k,l,m,n,v,f,g,h,u;

/*Creation of Two Square Matrices*/


for(i=0; i<3;i++)
{
for(j=0; j<3;j++)
{
printf("Enter%d%d element of a:",i,j);
scanf("%d",&a[i][j]);
printf("Enter%d%d element of b:",i,j);
scanf("%d",&b[i][j]);
}
printf("\n");
}

/*ADDITION*/
printf("Addition:c=a+b\n");

for(k=0; k<3; k++)


{
for(m=0; m<3; m++)
{
c[k][m]=a[k][m]+b[k][m];
printf("%d\t",c[k][m]);
}
printf("\n");
}

/*SUBSTRACTION*/
printf("Substraction:d=a-b\n");

for(l=0; l<3; l++)


{
for(n=0; n<3; n++)
{
d[l][n]=a[l][n]-b[l][n];
printf("%d\t",d[l][n]);
}
printf("\n");
}

/*TRANSPOSE*/
printf("Transpose[v][f]=a[f][v]\n");
for(v=0; v<3; v++)
{
for(f=0; f<3; f++)
{
transpose[v][f]=a[v][f]-b[v][f];
printf("%d\t",transpose[v][f]);
}
printf("\n");
}

/*TRACE*/
printf("Trace=a[0][0]+a[1][1]+a[2][2]\n");
trace=a[0][0]+a[1][1]+a[2][2];
printf("Trace=%d\n",trace);

printf("\n");

/*MULTIPLICATION*/
printf("Multiplication: mult=a*b\n");
for(u=0; u<3; u++)
{
for(g=0; g<3; g++)
{
for(h=0; h<3; h++)
{

mult[g][u]=mult[g][u]+(a[g][h]*b[h][g]);
}
printf("%d\t",mult[g][u]);
}
printf("\n");
}
}

OUTPUT:
Enter00 element of a:5
Enter00 element of b:4
Enter01 element of a:2
Enter01 element of b:2
Enter02 element of a:9
Enter02 element of b:1

Enter10 element of a:5


Enter10 element of b:7
Enter11 element of a:8
Enter11 element of b:6
Enter12 element of a:1
Enter12 element of b:2

Enter20 element of a:3


Enter20 element of b:3
Enter21 element of a:4
Enter21 element of b:9
Enter22 element of a:8
Enter22 element of b:5

Addition:c=a+b
9 4 10
12 14 3
6 13 13
Substraction:d=a-b
1 0 8
-2 2 -1
0 -5 3
Transpose[v][f]=a[f][v]
1 0 8
-2 2 -1
0 -5 3
Trace=a[0][0]+a[1][1]+a[2][2]
Trace=21

Multiplication: mult=a*b
61 67 51
61 67 51
61 67 51
PRACTICAL-6

AIM: Addition, Subtraction, Transpose, Trace and Multiplication of a square matrix N*M.

OBJECTIVE: Write a program to enter two N×M (square) matrices from user and calculate its sum, difference, transpose,
trace and product.

THEORY: To work on matrix, we need to declare two dimensional array.


Syntax: type array_name[row_size][column_size]

Two dimensional array take value from user using nested loop, where first loop represent rows and second loop is for
columns of matrix.
Sum: Sum of two matrix is calculated by adding each element of first matrix with corresponding element of second matrix
and sorting value in a new matrix.
Difference: Subtract each element of first matrix with corresponding element of first matrix and sorting value in new matrix
using nested loop.
Transpose: We store value of row elements of a matrix in a column of other new matrix

For e.g... If A = ; then AT =

Product: If we consider

Trace: Sum of diagonal elements of a matrix is called trace.

A= ; Trace = a + e + j

INPUT:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int A[10][10], B[10][10], C[10][10], i, j, k, a, b, p, q;
printf("Enter the size of 1st matrix\n");
scanf("%d%d",&a,&b);

printf("Enter the size of 2nd matrix\n");


scanf("%d%d",&p,&q);

printf("Enter element row wise\n");


printf("Enter the element of 1st matrix\n");
for (i=0; i<a; i++)
{
for(j=0; j<b; j++)
{
scanf("%d",&A[i][j]);
}
}

printf("Enter the element of 2nd matrix\n");


for (i=0; i<p; i++)
{
for(j=0; j<q; j++)
{
scanf("%d",&B[i][j]);
}
}

/*PRINT MATRIX A & B*/


printf("Enter matrix is \n\n");
for(i=0; i<a; i++)
{
printf("\n");
for(j=0; j<b; j++)
{
printf("%d",A[i][j]);
}
}

printf("\n");
for(i=0; i<p; i++)
{
printf("\n");
for(j=0; j<q; j++)
{
printf("%d",B[i][j]);
}
}

/*MULTIPLICATION OF A&B MATRIX*/

printf("\nMultiplication of two matrix is:\n");


for(i=0; i<a; i++)
{
for(j=0; j<q; j++)
{
C[i][j]=0;
for(k=0; k<b; k++)
{
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}

for(i=0; i<a; i++)


{
printf("\n\n");
for(j=0; j<q; j++)
{
printf("\t%d",C[i][j]);
}
}
}

OUTPUT:
Enter the size of 1st matrix
2 3
Enter the size of 2nd matrix
3 2
Enter element row wise
Enter the element of 1st matrix
5 4 6 8 9 2
Enter the element of 2nd matrix
1 5 3 4 6 9
Enter matrix is

546
892

15
34
69
Multiplication of two matrix is:

53 95

47 94
PRACTICAL-7(A)

AIM: Generate random numbers from uniform distribution. Calculate sample mean and variance and compare with
population parameter.

OBJECTIVE: Generate random number using uniform distribution.

THEORY: Random number are generated using function rand ( ) function, we divide generated random variable by 65536
and generate values between (0, 1).
Uniform Random Variables:
To generate values between the given parameters of uniform distribution, we use formula:

Where (a, b) are parameters of uniform distribution and are values (0, 1).

INPUT:
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int i,a,b;
float x[30],u[30] ,mean,meanp,variance,varp,sum=0,sumsq=0;

for(i=0;i<30;i++)
{
x[i]=(float)(rand())/65536;
printf("%f\t",x[i]);
}
printf("\nEnter the lower limit of x:\n");
scanf("%d",&a);
printf("\nEnter the upper limit of x:\n");
scanf("%d",&b);
for(i=0;i<30;i++)
{
u[i]=a+(b-a)*x[i];

printf("u[%d]=%f\t",i,u[i]);
sum=sum+u[i];
sumsq+=pow(u[i],2);
}
mean=sum/30;
variance=(sumsq/30)-(mean*mean);
meanp=(a+b)/2;
varp=pow((b-a),2)/12;
printf("\n\nSample mean: %f\nSmaple Variance: %f\nPopulation mean: %f\nPopulation variance: %f\n
",mean,variance,meanp,varp);
}
OUTPUT:
0.000626 0.281784 0.096649 0.404358 0.292496
0.239929 0.175140 0.447968 0.411407 0.373291
0.087051 0.429459 0.355240 0.256760 0.151993
0.007492 0.045700 0.182220 0.073654 0.082947
0.494247 0.222839 0.059540 0.002335 0.004456
0.188934 0.265823 0.285583 0.300873 0.303574

Enter the lower limit of x:


2

Enter the upper limit of x:


5
u[0]=2.001877 u[1]=2.845352 u[2]=2.289948 u[3]=3.213074 u[4]=2.877487
u[5]=2.719788 u[6]=2.525421 u[7]=3.343903 u[8]=3.234222 u[9]=3.119873
u[10]=2.261154 u[11]=3.288376 u[12]=3.065720 u[13]=2.770279 u[14]=2.455978
u[15]=2.022476 u[16]=2.137100 u[17]=2.546661 u[18]=2.220963 u[19]=2.248840
u[20]=3.482742 u[21]=2.668518 u[22]=2.178619 u[23]=2.007004 u[24]=2.013367
u[25]=2.566803 u[26]=2.797470 u[27]=2.856750 u[28]=2.902618 u[29]=2.910721

Sample mean: 2.652437


Smaple Variance: 0.191821
Population mean: 3.000000
Population variance: 0.750000
PRACTICAL-7(B)

AIM: Generate random numbers from exponential, distribution. Calculate sample mean and variance and compare with
population parameter.

OBJECTIVE: Generate random number using Exponential distribution.

THEORY: Random number are generated using function rand ( ) function, we divide generated random variable by 65536
and generate values between (0, 1).
Exponential Random Variables:
To generate values between the given parameters of exponential distribution, we use formula:

Where,

INPUT:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
main()
{
int i;
float x[30], exp[30], expmean, expvar, expmean_p, expvar_p, lambda, sum=0, sumsq=0;

for(i=0; i<30; i++)


{
x[i]=(float)(rand())/65536;
printf("%f",x[i]);

printf("\nEnter lambda:\n");
scanf("%f",&lambda);
sum=0;
sumsq=0;

for(i=0; i<30; i++)


{
exp[i]= -(log(1-x[i]))/lambda;
printf("exp[%d]=%f",i,exp[i]);
sum=sum+exp[i];
sumsq=sumsq+pow(exp[i],2);
}

expmean=sum/30;
expvar=(sumsq/30)-pow(expmean,2);
expmean_p=1/lambda;
expvar_p=1/pow(lambda,2);

printf("\n\nSample Mean_exp:%f\n Sample Variance_exp:%f\n Population mean_exp:%f\n


Population Variance_exp:%f\n",expmean, expvar, expmean_p, expvar_p);
}

OUTPUT:
0.0006260.2817840.0966490.4043580.2924960.2399290.1751400.4479680.4114070.373291
0.0870510.4294590.3552400.2567600.1519930.0074920.0457000.1822200.0736540.082947
0.4942470.2228390.0595400.0023350.0044560.1889340.2658230.2855830.3008730.303574

Enter lambda:
5
exp[0]=0.000125exp[1]=0.066197exp[2]=0.020329exp[3]=0.103623exp[4]=0.069202exp[5
]=0.054869exp[6]=0.038508exp[7]=0.118830exp[8]=0.106004exp[9]=0.093455exp[10]=0.
018215exp[11]=0.112234exp[12]=0.087775exp[13]=0.059347exp[14]=0.032973exp[15]=0.
001504exp[16]=0.009355exp[17]=0.040232exp[18]=0.015302exp[19]=0.017318exp[20]=0.
136342exp[21]=0.050422exp[22]=0.012277exp[23]=0.000467exp[24]=0.000893exp[25]=0.
041881exp[26]=0.061801exp[27]=0.067258exp[28]=0.071585exp[29]=0.072359

Sample Mean_exp:0.052689
Sample Variance_exp:0.001495
Population mean_exp:0.200000
Population Variance_exp:0.040000
PRACTICAL-8(A)

AIM: Fitting of Binomial and apply chi - square test for goodness of fit.

OBJECTIVE : Fit binomial distribution taking random variable and frequency from user.

THEORY: P.d.f. of binomial distribution , for all x=0,1,2.....

Hence to calculate p, we use formula;

And,

Goodness of fit:

If , the null hypothesis is rejected at level of significance i.e.,binomial distribution is not a good fit to
data; otherwise hypothesis is accepted.

INPUT:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int n,i,count=0,N=0,add=0;
float x[10],f[10],p,q,mean,chi,P[10],e[10],re[10],sum=0,resum=0,chisq=0;
printf("Enter the value of n:\n");
scanf("%d",&n);
printf("Value of random variable and its frequency:\n");
for(i=0;i<=n;i++)
{
scanf("%f%f",&x[i],&f[i]);
N=N+f[i];
sum=sum+x[i]*f[i];
}
mean=sum/N;
p=mean/n;
q=1-p;

printf("n=%d\n N=%d\n mean=%f\n p=%f\n q=%f\n\n",n,N,mean,p,q);


P[0]=pow(q,n);
for(i=0;i<n;i++)
{
P[i+1]=((n-i)*p*P[i])/((i+1)*q);
}
for(i=0;i<=n;i++)
{
e[i]=N*P[i];
re[i]=(int)(e[i]+0.5);
resum=resum+re[i];
}
printf("x[i]\tf[i]\tP[i]\t\te[i]\t\tre[i]\n");
for(i=0;i<n;i++)
{
printf("%.0f\t%.0f\t%f\t%f\t%.0f\n",x[i],f[i],P[i],e[i],re[i]);
}
printf("total\t%d\t\t\t\t%.0f",N,resum);
for(i=0;i<(n+1)/2;i++)
{
re[i+1]=re[i+1]+re[i];
f[i+1]=f[i+1]+f[i];
re[i]=0;
f[i]=0;
count++;
}
for(i=n;i>(n/2);i--)
{
if(f[i]<5||re[i]<5)
{
re[i-1]=re[i-1]+re[i];
f[i-1]=f[i-1]+f[i];
re[i]=0;
f[i]=0;
add++;
}
}
printf("\n\nPooled frequencies are:\n\n");
printf("x[i]\tf[i]\tre[i]\n");
for(i=0;i<=n;i++)
{
printf("%.0f\t%.0f\t%.0f\n",x[i],f[i],re[i]);
}
printf("Total\t5%d\t%f",N,resum);
for(i=count;i<=(n-add);i++)
{
chi=pow(re[i]-f[i],2)/re[i];
chisq=chisq+chi;
}
printf("\n\n calculated chisquare=%f",chisq);
printf("\n tabulated chisq=11.070");
if(chisq>11.070)
{
printf("\nnull hypothesis is rejected at alpha=0.05");
}
else
{
printf("\nnull hypothesis is accepted at alpha=0.05");
}
}

OUTPUT:
Enter the value of n:
7
Value of random variable and its frequency:
0 7
1 6
2 19
3 35
4 30
5 23
6 7
7 1
n=7
N=128
mean=3.382813
p=0.483259
q=0.516741

x[i] f[i] P[i] e[i] re[i]


0 7 0.009838 1.259275 1
1 6 0.064404 8.243766 8
2 19 0.180694 23.128839 23
3 35 0.281643 36.050354 36
4 30 0.263394 33.714481 34
5 23 0.147797 18.917976 19
6 7 0.046073 5.897397 6
total 128 128

Pooled frequencies are:


x[i] f[i] re[i]
0 0 0
1 0 0
2 0 0
3 0 0
4 97 102
5 23 19
6 8 7
7 0 0
Total 5128 128.000000

calculated chisquare=1.230060
tabulated chisq=11.070
null hypothesis is accepted at alpha=0.05
PRACTICAL-8(B)

AIM: Fitting of poisson and apply chi - square test for goodness of fit.

OBJECTIVE: Fitting of poisson distribution:

THEORY: P.d.f. of poisson distribution

Goodness of fit:

If , H0 is rejected i.e.,poisson distribution is not a good fit for data;otherwise

accept H0

INPUT:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int i, n, count=0;
float x[10],f[10],e[10],re[10],P[10],lambda,sum=0,sf=0,chi=0,chisq=0,esum=0;
printf("\n enter the number of values of random variable X takes:\n");
scanf("%d",&n);
printf("enter the values of x:\n");

for(i=0;i<n;i++)
{
scanf("%f",&x[i]);
}
for(i=0;i<n;i++)
{
printf("enter the frequency of %0.1f\n",x[i]);
scanf("%f",&f[i]);
}
for(i=0;i<n;i++)
{
sum = sum+f[i]*x[i];
sf= sf+f[i];
}
lambda=sum/sf;
printf("lambda=%f\n",lambda);
P[0]=exp(-1*lambda);
for(i=1;i<n;i++)
{
P[i]=lambda*P[i-1]/i;
}
for(i=0;i<n;i++)
{
e[i]=sf*P[i];
}
for(i=0;i<n;i++)
{
esum=esum+e[i];
}
printf("esum=%f\n",esum);
printf("x\tf(x)\tP[i]\te[i]\n");
for(i=0;i<n;i++)
{
printf("%0.1f\t%0.1f\t%0.5f\t%0.5f\n",x[i],f[i],P[i],e[i]);
}
for(i=n-1;i>=0;i--)
{
if(f[i]<5||e[i]<5)
{
e[i-1]=e[i-1]+e[i];
e[i]=0;
f[i-1]=f[i-1]+f[i];
f[i]=0;
count++;
}
}
for(i=0;i<n;i++)
{
if(e[i]-(int)e[i]>0.5)
{
re[i]=(int)(e[i]+1);
}
else
re[i]=(int)e[i];
}
for(i=0;i<(n-count);i++)
{
chi=pow(re[i]-f[i],2)/re[i];
chisq=chisq+chi;
}
printf("H0:the poisson distribution is a good fit for the data\n");
printf("H1:the poisson distribution is not a good fit for the data\n");
if(chisq>7.815)
{
printf("the null hypothesis is rejected.\n");
}
else
printf("the null hypothesis is accepted.\n");
}

OUTPUT:
enter the number of values of random variable X takes:
6
enter the values of x:
0 1 2 3 4 5
enter the frequency of 0.0
107
enter the frequency of 1.0
63
enter the frequency of 2.0
20
enter the frequency of 3.0
7
enter the frequency of 4.0
7
enter the frequency of 5.0
12
lambda=0.981481
esum=215.883469
x f(x) P[i] e[i]
0.0 107.0 0.37476 80.94719
1.0 63.0 0.36782 79.44817
2.0 20.0 0.18050 38.98845
3.0 7.0 0.05905 12.75548
4.0 7.0 0.01449 3.12982
5.0 12.0 0.00284 0.61437
H0:the poisson distribution is a good fit for the data
H1:the poisson distribution is not a good fit for the data
the null hypothesis is rejected.
PRACTICAL-8(C)

AIM: To apply Chi-sq test .

OBJECTIVE: The demand for a particular spare part in a factory was found to vary from day-to-day. In a sample study the
following information was obtained:

DAYS MON. TUE. WED. THU. FRI. SAT.


NO.OF PARTS DEMANDED 1124 1125 1110 1120 1126 1115

INPUT:
#include<stdio.h>
#include<math.h>
main()
{
int i,n;
int a[7],sum,mean,deviation;
float goodness_fit,sumsqr,tab_chi_g_fit;
sum=sumsqr=n=0;

tab_chi_g_fit=11.07;
printf("Input the values for 6 days:\n");
for (i=1;i<7;i++)
{
scanf("%d",&a[i]);
sum=sum+a[i];
n=n+1;
}
mean=sum/n;
for(i=1;i<=6;i++)
{
deviation=a[i]-mean;
sumsqr=sumsqr+(deviation*deviation);
}
goodness_fit=sumsqr/mean;
printf("mean:%d\n",mean);
printf("\ngoodnessof fit for the above data:%f\n",goodness_fit);
printf("degree of freedom:%d\n",n-1);
if(goodness_fit<tab_chi_g_fit)
{
printf("hypothesis is accepted at 5percent level of Significance");
}
else
{
printf("hypothesis is rejected at 5percent level ofsignificance");
}
}

OUTPUT:
Input the values for 6 days:
1124
1125
1110
1120
1126
1115
mean:1120

goodnessof fit for the above data:0.180357


degree of freedom:5
hypothesis is accepted at 5percent level of Significance.
PRACTICAL-9
AIM: Chi - square Contingency Table.

OBJECTIVE: Two sample polls of votes for two candidates A and B for a public office are taken, one form among the
residents of rural areas and other from urban areas. Enter results from user. Write a program to examine whether the nature
of the area is related to voting preference in this election.

THEORY: The problem is to test if the two attributes A and B under consideration are independent, the theoretical cell
frequencies are calculated as:
; i=1,2,……
; j=1,2,……

Degree Of Freedom: - The number of independent variates which make up the statistic (χ2). Compare χ2 cal with χ2 tab (at
given level of significance for the given d.f (r-1) (s-1)).
Test criteria:
If χ2 cal> χ2 tab, H0 is rejected, otherwise H0 is accepted.

INPUT-
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int i,a[3],b[3],N,sum_r1,sum_r2,sum_c1,sum_c2,e_11,e_12,e_21,e_22;
float chisq;
printf("Contigency Table:\n\nA\tB\n");
for(i=0;i<2;i++)
{
scanf("%d%d",&a[i],&b[i]);
}
sum_r1=a[0]+b[0];
sum_r2=a[1]+b[1];
sum_c1=a[0]+a[1];
sum_c2=b[0]+b[1];
N=sum_c1+sum_c2;
e_11=(sum_c1*sum_r1)/N;
printf("Expected frequency:\n\n%d\n",e_11);

e_12=(sum_c2*sum_r1)/N;
e_21=(sum_c1*sum_r2)/N;
e_22=(sum_c2*sum_r2)/N;
printf("%d\n%d\n%d\n",e_12,e_21,e_22);
printf("\nH0:Two attributes are independent\n");
printf("\nH1:Two attributes are dependent\n");
chisq=((pow(a[0]-e_11,2)/e_11)+(pow(a[1]-e_21,2)/e_21)+(pow(b[0]
e_12,2)/e_12)+(pow(b[1]-e_22,2)/e_22));
printf("\nChisq: %f\n",chisq);

if(chisq<0)
chisq=(-1)*chisq;
if(chisq>3.841)
printf("\nChisq Calculated is greater than the tabulated chisq at 1 degree of
freedom at alpha=0.05 then we reject H0\n");
else
printf("\nChisq Calculated is less than the tabulated chisq at 1 degree of
freedom at alpha=0.05 then we accept H0\n");
}

OUTPUT-
Contigency Table:

A B
628 380
550 450
Expected frequency:

591
416
586
413

H0:Two attributes are independent

H1:Two attributes are dependent

Chisq: 10.958172

Chisq Calculated is greater than the tabulated chisq at 1 degree of freedom at a


lpha=0.05 then we reject H0
PRACTICAL-10
AIM: T - Test for difference of means.

OBJECTIVE: Taking input from user, test if two independent samples of different sizes have been drawn from
two normal population with different means. Sample data is from a group of men and women who did workouts
at a gym three times a week for a year. Then, their trainer measured the body fat. The table below shows the data.

Body fat percentage data grouped by gender

Group Body Fat Percentages

13.3 6.0 20.0 8.0 14.0

Men 19.0 18.0 25.0 16.0 24.0

15.0 1.0 15.0

22.0 16.0 21.7 21.0 30.0


Women
26.0 12.0 23.2 28.0 23.0

THEORY: - Under the null hypothesis, H0 that the samples have been drawn from the normal population with mean and
and under the assumption that the population variance are equal i.e , (say)

Where, ;

And,

INPUT:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int i,n1,n2;
float x[20],xsum=0,xmean,y[20],ysum=0,ymean,xsqsum=0,ysqsum=0,s,t,rterm,p,q;
printf("Enter the values of n1 and n2:\n");
scanf("%d %d",&n1,&n2);
printf("Enter the data for x:\n");
for(i=0;i<n1;i++)
{
scanf("%f",&x[i]);
}
printf("Enter the data for y:\n");
for(i=0;i<n2;i++)
{
scanf("%f",&y[i]);
}

for(i=0;i<n1;i++)
{
xsum=xsum+x[i];
}

for(i=0;i<n2;i++)
{
ysum=ysum+y[i];
}

xmean=xsum/(float)n1;
ymean=ysum/(float)n2;
printf("\nXMean: %f\nYMean: %f\n",xmean,ymean);
for(i=0;i<n1;i++)
{
xsqsum+=pow((x[i]-xmean),2);
}

for(i=0;i<n2;i++)
{
ysqsum+=pow((y[i]-ymean),2);
}

s=sqrt((xsqsum+ysqsum)/(n1+n2-2));
printf("\nS Sqaure: %f\n",s*s);
p=1/(float)n1;
q=1/(float)n2;
rterm=sqrt(p+q);
t=(xmean-ymean)/(s*rterm);
printf("The calculated value of t: %f\n",t);
printf("Tabulated t=2.06");
if(t<0)
{
t=-1*t;
}

if(t>2.06)
printf("\nSince t calculated is greater than the t tabulated so we reject the
null hypothesis at 5 percent level of significance.\nAnd we conclude that there is
significant difference in the sample means.");
else
printf("\nSince t calculated is less than the t tabulated so we fail to reject
the null hypothesis at 5 percent level of significance.\nAnd we conclude that there
is no significant difference between sample means.");
}

OUTPUT:
Enter the values of n1 and n2:
13
10
Enter the data for x:
13.3 6.0 20.0 8.0 14.0 19.0 18.0 25.0 16.0 24.0 15.0 1.0 15.0
Enter the data for y:
22.0 16.0 21.7 21.0 30.0 26.0 12.0 23.2 28.0 23.0

XMean: 14.946154
YMean: 22.289999

S Sqaure: 38.882921
The calculated value of t: -2.799960
Tabulated t=2.06
Since t calculated is greater than the t tabulated so we reject the null hypothe
sis at 5 percent level of significance.
And we conclude that there is significant difference in the sample means.
PRACTICAL-11
AIM: Paired t - test.

OBJECTIVE: Taking input from user, Write a program to test if two samples with equal sample size differ significantly as
regard on their effect on any attribute.

THEORY: Consider two samples xi and yi

Under : There is no significant difference between xi and yi

i.e. , , means ‘t’ follows student-t distribution with (n-1) degree of freedom. Where,
and,

INPUT:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int i,n=8;
int x[10],sum=0,y[10],d[10],sqsum=0;
float tcal,mean,ssquare;
printf("Enter the data for x:\n");
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}

printf("Enter the data for y:\n");


for(i=0;i<n;i++)
{
scanf("%d",&y[i]);
}
printf("Data for d:\n");
for(i=0;i<n;i++)
{
d[i]=x[i]-y[i];
sum=sum+d[i];
printf("%d ",d[i]);
}
mean=sum/(float)n;
for(i=0;i<n;i++)
{
sqsum+=pow((d[i]-mean),2);
}
printf("\nSquare values sum=%d\n",sqsum);
printf("Mean: %f\n",mean);
ssquare=sqsum/(float)(n-1);

printf("S Sqaure: %f\n",ssquare);


tcal=mean/(sqrt(ssquare/(float)n));

printf("The calculated value of t: %f\n",tcal);


printf("Tabulated t=1.90");

if(tcal<0)
{
tcal=-1*tcal;
}
if(tcal>1.90)
printf("\nSince |t| calculated is greater than the t tabulated so we reject the
null hypothesis at 5 percent level of significance.\nAnd we conclude that there is
change in the sample.");
else
printf("\nSince |t| calculated is less than the t tabulated so we fail to reject
the null hypothesis at 5 percent level of significance.\nAnd we conclude that the
changes are due to fluctuations of sampling. ");
}

OUTPUT:
Enter the data for x:
3 3 3 12 15 16 17 19
Enter the data for y:
20 13 13 20 29 32 23 20
Data for d:
-17 -10 -10 -8 -14 -16 -6 -1
Square values sum=200
Mean: -10.250000
S Sqaure: 28.571428
The calculated value of t: -5.423790
Tabulated t=1.90
Since |t| calculated is greater than the t tabulated so we reject the null
hypothesis at 5 percent level of significance.
And we conclude that there is change in the sample.
PRACTICAL-12

AIM: F ratio Test.

OBJECTIVE: Write a program to test


i) Whether two independent samples xi (i=1, 2 ...n1) and yj(j=1, 2 ...n2) have been drawn from normal populations with the
same variance (say) or
ii) Whether two independent estimates of population variance or homogeneous or not.

THEORY: Under H0:


i) (say), the population variances are equal or,
ii) Two independent estimates of the population variances are homogeneous, the F-statistic is given by:

where, and, are unbiased estimates of common population variance


obtained from two independent samples and it follows F-distribution with (v1,v2)of where v1=(n1 -1) & v2=(n2-1)

INPUT:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int n1,n2,i;
float x[15],y[15],xsum=0,ysum=0,xmean,ymean,sqsum1=0,sqsum2=0,s1sq,s2sq,F;
printf("Enter the number of sample item in sample 1 and 2\n");
scanf("%d %d",&n1,&n2);
printf("The data points of the sample1 are: \n");
for(i=0;i<n1;i++)
{
scanf("%f",&x[i]);
}
printf("\nThe data points of the sample2 are: \n");
for(i=0;i<n2;i++)
{
scanf("%f",&y[i]);
}

for(i=0;i<n1;i++)
{
xsum+=x[i];
}

for(i=0;i<n2;i++)
{
ysum+=y[i];
}

xmean=xsum/n1;
ymean=ysum/n2;
printf("Mean of Sample 1 is: %f\n",xmean);
printf("Mean of Sample 2 is: %f\n",ymean);
for(i=0;i<n1;i++)
{
sqsum1+=pow((x[i]-xmean),2);
}
for(i=0;i<n2;i++)
{
sqsum2+=pow((y[i]-ymean),2);
}

s1sq=sqsum1/(n1-1);
s2sq=sqsum2/(n2-1);
printf("Sample Mean sqaure of Sample 1 is : %f\n",s1sq);
printf("Sample Mean square of Sample 2 is : %f\n",s2sq);
F=s2sq/s1sq;
printf("The Calculated value of F is: %f\n",F);
printf("The tabulated value of F is:3.39\n");

if(F>3.39)
{
printf("\nSince value of F is greater than the tabulated value of F so null
hypothesis is rejected at 5 percent level of significance that is population
variance are equal.\n");
}
else
{
printf("\nSince value of F is less than the tabulated value of F so null
hypothesis is fail to reject at 5 percent level of significance that is population
variance is.\n");
}

OUTPUT:
Enter the number of sample item in sample 1 and 2
9
11
The data points of the sample1 are:
19 22 24 27 24 18 20 19 25

The data points of the sample2 are:


26 37 40 35 30 30 40 26 30 35 45
Mean of Sample 1 is: 22.000000
Mean of Sample 2 is: 34.000000
Sample Mean sqaure of Sample 1 is : 10.000000
Sample Mean square of Sample 2 is : 38.000000
The Calculated value of F is: 3.800000
The tabulated value of F is:3.39

Since value of F is greater than the tabulated value of F so null hypothesis is


rejected at 5 percent level of significance that is population variance are equal.
PRACTICAL-13(A)
AIM: Compute correlation coefficient between two random variables X and Y.

OBJECTIVE: Write a program to calculate correlation on between two random variables X and Y.

THEORY: Let X and Y are two random variables. The correlation coefficient between these two random variables X and
Y, usually denoted by r(x, y) or simply rxy is numerical measure of linear relationship between them and is defined as –

INPUT:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int x[100],y[100],xy[100],xsquare[100],ysquare[100];
int i,n,xsum,ysum,xysum,xsqr_sum,ysqr_sum;
float coeff_r,num,deno;
xsum=ysum=xysum=xsqr_sum=ysqr_sum=0;
printf("Enter the value of n:\n");
scanf("%d",&n);
printf("Enter the value for x and y:\n");
for(i=0;i<n;i++)
{
printf("x[%d] and y[%d]:",i,i);
scanf("%d%d",&x[i],&y[i]);
}
for(i=0;i<n;i++)
{
xy[i]=x[i]*y[i];
xsquare[i]=x[i]*x[i];
ysquare[i]=y[i]*y[i];
xsum+=x[i];
ysum+=y[i];

xysum+=xy[i];
xsqr_sum+=xsquare[i];
ysqr_sum+=ysquare[i];
}
num=1.0*((n*xysum)-(xsum*ysum));
deno=1.0*((n*xsqr_sum-xsum*xsum)*(n*ysqr_sum-ysum*ysum));
coeff_r=num/sqrt(deno);
printf("Correlation Coefficient, r=%.4f\n",coeff_r);
}

OUTPUT:
Enter the value of n:
6
Enter the value for x and y:
x[0] and y[0]:40 78
x[1] and y[1]:21 70
x[2] and y[2]:25 60
x[3] and y[3]:31 55
x[4] and y[4]:38 80
x[5] and y[5]:47 66
Correlation Coefficient, r=0.3471
PRACTICAL-13(B)
AIM: Compute Rank correlation coefficient between two random variables X and Y.

OBJECTIVE: Write a program to calculate Rank correlation on between two random variables X and Y.

INPUT:
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int i,x[9]={3,5,1,6,7,2,8,9,4},y[9]={6,3,2,5,8,1,7,9,4},n=9;
float d[9],d_sq[9],d_sq_sum=0,R;
printf("Rank of x={3,5,1,6,7,2,8,9,4}\n");
printf("Rank of y={6,3,2,5,8,1,7,9,4}\n");

for (i=0 ; i<n ; i++)


{
d[i]=x[i]-y[i];
}

for (i=0 ; i<n ; i++)


{
d_sq[i]=d[i]*d[i];
}

for (i=0 ; i<n ; i++)


{
d_sq_sum=d_sq_sum+d_sq[i];
}

R=1-((6*d_sq_sum)/(n*((n*n)-1)));
printf("Value of rabk correlation coeffecient_R=%.3f",R);
}

OUTPUT:
Rank of x={3,5,1,6,7,2,8,9,4}
Rank of y={6,3,2,5,8,1,7,9,4}
Value of rabk correlation coeffecient_R=0.850
PRACTICAL-14(A)

AIM: Multiple and Partial correlation

OBJECTIVE: Write a program to calculate multiple and partial correlation coefficient of data input by user.

THEORY: The multiple correlation coefficient of X1 on X2 and X3, denoted by R1.23 is the simple correlation coefficient
between X1 and the joint effect of X2 and X3 on X1.

Partial Correlation : The correlation coefficient between X1 and X2 after linear effect of X3 on each of
them has been eliminated is called partial correlation coefficient:

INPUT:-
#include<stdio.h>
#include<conio.h>
#include<math.h>

float corrcoeff(float x[],float y[], int n);


float covar(float x[],float y[], int n);
float mean(float x[], int n);
float mcorrcoeff(float x,float y, float z);
float pcorrcoeff(float x,float y, float z);
main()

{
int n,i;
float x1[20],x2[20],x3[20],r12,r23,r31,R1_23,R2_31,R3_12,r12_3,r23_1,r31_2;
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the given data:\n");
printf("x1[i]\tx2[i]\tx3[i]\n");
for(i=0;i<n;i++)
{
scanf("%f%f%f",&x1[i],&x2[i],&x3[i]);
}
r12=corrcoeff(x1,x2,n);
r23=corrcoeff(x2,x3,n);
r31=corrcoeff(x3,x1,n);
printf("\n\nPairwise Correlation Coefficient are:\n");
printf("r12=%f\tr23=%f\tr31=%f\n",r12,r23,r31);
R1_23=mcorrcoeff(r12,r31,r23);
R2_31=mcorrcoeff(r12,r23,r31);
R3_12=mcorrcoeff(r31,r23,r12);
printf("\n\nThe Multiple Correlation Coefficient are:\n");
printf("R1_23=%f\tR2_31=%f\tR3_12=%f\n",R1_23,R2_31,R3_12);
r12_3=pcorrcoeff(r12,r31,r23);
r23_1=pcorrcoeff(r23,r12,r31);
r31_2=pcorrcoeff(r31,r23,r12);
printf("\n\nPairtial Correlation Coefficient are:\n");
printf("r12_3=%f\tr23_1=%f\tr31_2=%f\n",r12_3,r23_1,r31_2);
}

float corrcoeff(float x[],float y[], int n)


{
float covxy,varx,vary,corr;
covxy=covar(x,y,n);
varx=covar(x,x,n);

vary=covar(y,y,n);
corr=covxy/(sqrt(varx)*sqrt(vary));
return(corr);
}

float covar(float x[],float y[], int n)


{
int i;
float sum=0,xmean,ymean,cov;
for(i=0;i<n;i++)
{
sum+=x[i]*y[i];
}
xmean=mean(x,n);
ymean=mean(y,n);
cov=(sum/n)-(xmean*ymean);
return(cov);
}

float mean(float x[], int n)


{
int i;
float sum=0,m;
for(i=0;i<n;i++)
{
sum+=x[i];
}
m=sum/n;
return(m);
}

float mcorrcoeff(float x,float y, float z)


{

float mcorr;
mcorr=sqrt((x*x+y*y-2*x*y*z)/(1-(z*z)));
return(mcorr);
}

float pcorrcoeff(float x,float y, float z)


{
float pcorr;
pcorr=(x-y*z)/(sqrt(1-y*y)*sqrt(1-z*z));
return(pcorr);
}

OUTPUT:
Enter the value of n
10
Enter the given data:
x1[i] x2[i] x3[i]
15 6 25
18 3 29
13 8 27
14 6 24
19 2 30
11 3 21
17 4 26
20 4 31
10 5 20
16 7 25

Pairwise Correlation Coefficient are:


r12=-0.369002 r23=-0.244989 r31=0.917991

The Multiple Correlation Coefficient are:


R1_23=0.929946 R2_31=0.438224 R3_12=0.923516

Pairtial Correlation Coefficient are:


r12_3=-0.374767 r23_1=0.254334 r31_2=0.918416
PRACTICAL-14(B)

AIM: Fitting of lines of regression (Y on X and X on Y).

OBJECTIVE: Write a program to obtain the equation of two lines of regression for the data given by user.

THEORY: The equation of line of regression of Y on X is given by:

This implies,

or
b = slope
c = intercept
where,

and

Similarly , X on Y is given by:

This implies,

or

B = slope
C = Intercept
Where,

and

INPUT:
#include<stdio.h>
#include<conio.h>
#define S 50
int main()
{
int n, i;
float x[S], y[S], sumX=0, sumX2=0, sumY=0, sumXY=0, c, b;
/* Input */
printf("How many data points?\n");
scanf("%d", &n);
printf("Enter data:\n");
for(i=1;i<=n;i++)
{
printf("x[%d]=",i);
scanf("%f", &x[i]);
printf("y[%d]=",i);
scanf("%f", &y[i]);
}
/* Calculating Required Sum */
for(i=1;i<=n;i++)
{
sumX = sumX + x[i];
sumX2 = sumX2 + x[i]*x[i];
sumY = sumY + y[i];
sumXY = sumXY + x[i]*y[i];
}
/* Calculating c and b */
b = (n*sumXY-sumX*sumY)/(n*sumX2-sumX*sumX);

c = (sumY - b*sumX)/n;
/* Displaying value of c and b */
printf("Values are: c=%0.2f and b = %0.2f",c,b);
printf("\nEquation of best fit is: y = %0.2f + %0.2fx",c,b);
getch();
return(0);
}

OUTPUT:
How many data points?
7
Enter data:
x[1]=0
y[1]=2.4
x[2]=1
y[2]=4.3
x[3]=2
y[3]=5
x[4]=3
y[4]=6.9
x[5]=4
y[5]=9.1
x[6]=5
y[6]=11.4
x[7]=6
y[7]=13.5
Values are: c=1.99 and b = 1.84
Equation of best fit is: y = 1.99 + 1.84x

You might also like