0% found this document useful (0 votes)
327 views

Probability and Statistics (MMT-008) : Practical Assignment File

This document contains a C program to compute Hotelling's T-squared statistic. It begins by defining constants and functions for matrix operations. It then prompts the user to input the sample size, number of variables, data matrix, and specified mean vector. It calculates the sample mean vector and performs matrix operations to compute Hotelling's T-squared statistic.

Uploaded by

Anurag Garg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
327 views

Probability and Statistics (MMT-008) : Practical Assignment File

This document contains a C program to compute Hotelling's T-squared statistic. It begins by defining constants and functions for matrix operations. It then prompts the user to input the sample size, number of variables, data matrix, and specified mean vector. It calculates the sample mean vector and performs matrix operations to compute Hotelling's T-squared statistic.

Uploaded by

Anurag Garg
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Probability

and
Statistics
(MMT-008)
Practical Assignment File

Anurag Garg
Roll Number: 142396116
Phone Number: 9811339877
Study Center: Maharaja Agrasen College (07107)

Write a program in C language to find the lower triangular square root of a


pd matrix. Also test your program on E6) of Unit 15.
#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
clrscr();
int i,j,k,n,m,h;
float A[10][10],T[10][10];
float t;
printf("\n Enter the order of matrix: ");
scanf("%d",&n);
printf("\n Enter the matrix data:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%f",&A[i][j]);
}
printf("\n");
}

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
T[i][j]=0;
}
}

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

{
t=A[k][k];
for(i=k;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==k)
T[i][j]=A[i][j]/sqrt(t);
else
T[i][j]=A[i][j]-(A[i][k]/t)*A[k][j];
}
}
/*Replacing the old matrix with new matrix */
for(h=0;h<n;h++)
{
for(m=0;m<n;m++)
{
A[h][m]=T[h][m];
}
}
}
printf("\nLower Triangular Square Root of Matrix is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%.1f",T[j][i]);
printf("\t");
}
printf("\n");
}
return 0;
}

OUTPUT

Consider y = (y1, y2, y3)t ~ N3( , ). Write a program in C language to find


the marginal distributions of y1, y2 and y3. Also extend it to find the
conditional distribution of y1 given y2 and y3. Also test your program on
Example 1 on Unit 16
#include<stdio.h>
#include<conio.h>
#include<math.h>

float B[10][10];
float determinant(float a[10][10],int k);
void cofactor(float num[10][10],int f);
void transpose(float num[10][10],float fac[10][10],int r);
void matrixmultiplication(float X[10][10],float Y[10][10],float Z[10][10],int r1,int r2,int c2);
void transposematrix(float X[10][10],float Y[10][10],int r1,int c1);

int main()
{
clrscr();
int i,j,k,n,m;
float A[10][10],C[10][10],S[10][10];
float s[10][10],Z[10][10],z[10][10],y[10][10],Y[10][10];
float x[10][10],X[10][10],e[10][10];
printf("\nEnter the number of variables: ");
scanf("%d",&n);
printf("\nEnter the mean vector:\n");
for(i=0;i<n;i++)
{
scanf("%f",&A[i][0]);
}

printf("\nEnter variance Covariance Matrix:\n");


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

for(j=0;j<n;j++)
{
scanf("%f",&S[i][j]);
}
printf("\n");
}

printf("\n Marginal Distribution of Y[1], Y[2] and Y[3] is given by:\n");


for(i=0;i<n;i++)
{
printf("Y[%d] ~ N(%.1f,%.1f)",i+1,A[i][0],S[i][i]);
printf("\n");
}

printf("\nCondition Distribution of Y[1] given:\n");


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

//Input Values of Y[2] and Y[3]

{
printf("Y[%d]=",i+2);
scanf("%f",&Y[i]);
printf("\n");
}
/* Conditional Mean of Y[1] calculation*/
for(i=0;i<(n-1);i++)
{
Z[0][i]=S[0][i+1];
}

for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-1);j++)
s[i][j]=S[i+1][j+1];
}

cofactor(s,n-1);

for(i=0;i<(n-1);i++)
{
C[i][0]=Y[i][0]-A[i+1][0];
}

matrixmultiplication(B,C,z,n-1,n-1,1);
matrixmultiplication(Z,z,y,1,n-1,1);

/* Conditonal variance of Y[1] calculation*/

transposematrix(Z,x,1,n-1);
matrixmultiplication(Z,B,X,1,n-1,n-1);
matrixmultiplication(X,x,e,1,n-1,1);

printf("Conditional Distribution of Y[1] is N(%.1f,%.1f)",y[0][0]+A[0][0],S[0][0]-e[0][0]);


return 0;
}

float determinant(float a[10][10],int k)


{
float s=1,det=0,b[10][10];
int i,j,m,n,c;
if (k==1)
{
return (a[0][0]);
}
else
{
det=0;
for (c=0;c<k;c++)
{
m=0;
n=0;
for (i=0;i<k;i++)

{
for (j=0;j<k;j++)
{
b[i][j]=0;
if (i != 0 && j != c)
{
b[m][n]=a[i][j];
if (n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det + s * (a[0][c] * determinant(b,k-1));
s=-1 * s;
}
}

return (det);
}

void cofactor(float num[10][10],int f)


{
float b[10][10],fac[10][10];
int p,q,m,n,i,j;
for (q=0;q<f;q++)
{
for (p=0;p<f;p++)
{
m=0;

n=0;
for (i=0;i<f;i++)
{
for (j=0;j<f;j++)
{
if (i != q && j != p)
{
b[m][n]=num[i][j];
if (n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q + p) * determinant(b,f-1);
}
}
transpose(num,fac,f);
}

void transpose(float num[10][10],float fac[10][10],int r)


{
int i,j;
float b[10][10],d;

for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
b[i][j]=fac[j][i];

}
}
d=determinant(num,r);
for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
B[i][j]=b[i][j] / d;
}
}
}

void transposematrix(float X[10][10],float Y[10][10],int r1,int c1)


{
int i,j;
for (i=0;i<c1;i++)
{
for (j=0;j<r1;j++)
{
Y[i][j]=X[j][i];
}
}
}

void matrixmultiplication(float X[10][10],float Y[10][10],float Z[10][10],int r1,int r2,int c2)


{
int i,j,k;
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
for(k=0;k<r2;k++)
{
Z[i][j]+=X[i][k]*Y[k][j];

}
}
}
}

OUTPUT

Write a program in C language to fit the model Yi = b0 + b1X1i + b2X2i + ei


where i=1,2,3,4,5 using the least square estimates.
#include<stdio.h>
#include<conio.h>
#include<math.h>

float B[10][10];
float determinant(float a[10][10],int k);
void cofactor(float num[10][10],int f);
void transpose(float num[10][10],float fac[10][10],int r);
void transposematrix(float X[10][10],float Y[10][10],int r1,int c1);
void matrixmultiplication(float X[10][10],float Y[10][10],float Z[10][10],int r1,int r2,int c2);

int main()
{
clrscr();
int i,j,k,n;
float Y[10][10];
float X1[10],X2[10];
float X[10][10],x[10][10],A[10][10];
float d=0;
float z[10][10],y[10][10];
printf("\n Enter the sample size: ");
scanf("%d",&n);
printf("\n Enter the values of Y :\n");
for(i=0;i<n;i++)
{
printf("%d. ",i+1);
scanf("%f",&Y[i][0]);
}

printf("\n Enter the values of X1 :\n");


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

{
printf("%d. ",i+1);
scanf("%f",&X1[i]);
}

printf("\n Enter the values of X2 :\n");


for(i=0;i<n;i++)
{
printf("%d. ",i+1);
scanf("%f",&X2[i]);
}

for(i=0;i<n;i++)
{
for(j=0;j<3;j++)
{
if(j==0)
X[i][j]=1;
else if (j==1)
X[i][j]=X1[i];
else
X[i][j]=X2[i];
}
}

OUTPUT

Write a program in C language to find the multiple correlation coefficient


and the mean square error, if xx , yy and xy are given.
#include<stdio.h>
#include<conio.h>
#include<math.h>

float B[10][10];
float determinant(float a[10][10],int k);
void cofactor(float num[10][10],int f);
void transpose(float num[10][10],float fac[10][10],int r);
void matrixmultiplication(float X[10][10],float Y[10][10],float Z[10][10],int r1,int r2,int c2);
void transposematrix(float X[10][10],float Y[10][10],int r1,int c1);

int main()
{
clrscr();
int i,j,k,n;
float Y[10][10];
float X1[10][10],X2;
float x[10][10],A[10][10];
float R,y[10][10],M;
printf("\n Enter the order of variance covariance matrix (xx): ");
scanf("%d",&n);
printf("\n Enter the values of variance covariance matrix (xx):\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%f",&Y[i][j]);
}
printf("\n");
}

printf("\n Enter the values of variance(xy) :\n");


for(i=0;i<n;i++)
{
scanf("%f",&X1[i][0]);
}

printf("\n Enter the values of variance(yy): ");


scanf("%f",&X2);

transposematrix(X1,x,n,1);

cofactor(Y,n);

matrixmultiplication(x,B,y,1,n,n);
matrixmultiplication(y,X1,A,1,n,1);

R=sqrt(A[0][0]/X2);
M=X2*(1-pow(R,2));
printf("\n The multiple correlation coefficient : %.2f ",R);
printf("\n Mean square error: %.2f",M);
return 0;
}

float determinant(float a[10][10],int k)


{
float s=1,det=0,b[10][10];
int i,j,m,n,c;
if (k==1)
{
return (a[0][0]);
}
else
{
det=0;

for (c=0;c<k;c++)
{
m=0;
n=0;
for (i=0;i<k;i++)
{
for (j=0;j<k;j++)
{
b[i][j]=0;
if (i != 0 && j != c)
{
b[m][n]=a[i][j];
if (n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det + s * (a[0][c] * determinant(b,k-1));
s=-1 * s;
}
}

return (det);
}

void cofactor(float num[10][10],int f)


{
float b[10][10],fac[10][10];
int p,q,m,n,i,j;

for (q=0;q<f;q++)
{
for (p=0;p<f;p++)
{
m=0;
n=0;
for (i=0;i<f;i++)
{
for (j=0;j<f;j++)
{
if (i != q && j != p)
{
b[m][n]=num[i][j];
if (n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q + p) * determinant(b,f-1);
}
}
transpose(num,fac,f);
}

/*Finding transpose of matrix*/

void transpose(float num[10][10],float fac[10][10],int r)


{

int i,j;
float b[10][10],d;

for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
d=determinant(num,r);
for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
B[i][j]=b[i][j] / d;
}
}
}

void transposematrix(float X[10][10],float Y[10][10],int r1,int c1)


{
int i,j;
for (i=0;i<c1;i++)
{
for (j=0;j<r1;j++)
{
Y[i][j]=X[j][i];
}
}
}

void matrixmultiplication(float X[10][10],float Y[10][10],float Z[10][10],int r1,int r2,int c2)


{

int i,j,k;
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
for(k=0;k<r2;k++)
{
Z[i][j]+=X[i][k]*Y[k][j];
}
}
}
}

OUTPUT

Write a programme in C language to find the ML estimation of mean ()


and variance (2).
#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
clrscr();
int i,n;
float A[30];
float sum,X,V;
printf("\n Enter the sample size n: ");
scanf("%d",&n);
printf("\n Enter the data :\n");
for(i=0;i<n;i++)
{
printf("%d. ",i+1);
scanf("%f",&A[i]);
}

for(i=0;i<n;i++)
{
sum+=A[i];
}
X=sum/n;
printf("\n Maximum likelihood estimator of mean is equal to sample mean: %.3f",X);
sum=0;
for(i=0;i<n;i++)
{
sum+=pow((A[i]-X),2);
}
V=sum/n;

printf("\n Maximum likelihood estimator of variance is %.3f : ",V);


return 0;
}

OUTPUT

Write a program in C language to compute Hotelling T2.


#include<stdio.h>
#include<conio.h>
#include<math.h>

#define max 10

float B[max][max];
float determinant(float a[max][max],int k);
void cofactor(float num[max][max],int f);
void transpose(float num[max][max],float fac[max][max],int r);
void matrixmultiplication(float X[max][max],float Y[max][max],float Z[max][max],int r1,int r2,int
c2);
void transposematrix(float X[max][max],float Y[max][max],int r1,int c1);

int main()
{
clrscr();
int i,j,k,n,m;
float A[max][max],C[max][max],S[max][max];
float sum,d,T;
float U[max][max],Z[max][max],z[max][max],y[max][max],Y[max][max];
printf("\n Enter the sample size n: ");
scanf("%d",&n);
printf("\n Enter the number of variables: ");
scanf("%d",&m);
printf("\n Enter the data matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%f",&A[i][j]);
}

printf("\n");
}

for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%.1f",A[i][j]);
printf("\t");
}
printf("\n");
}

printf("\n Enter the specifi mean value vector:\n");


for(i=0;i<m;i++)
{
for(j=0;j<1;j++)
scanf("%f",&U[i][j]);
}

printf("\n Sample Mean Vector:\n");


for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
sum+=A[j][i];

C[i][0]=sum/n;
}

for(i=0;i<m;i++)
{
for(j=0;j<1;j++)
{

printf("%.1f",C[i][j]);
printf("\t");
}
printf("\n");
}

printf("\n Sample Covariance Matrix:\n");


for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
sum=0;
for(k=0;k<n;k++)
{
sum+=((A[k][i]-C[i][0])*(A[k][j]-C[j][0]));
}
S[i][j]=sum/2;
}
}

for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%.1f",S[i][j]);
printf("\t");
}
printf("\n");
}

cofactor(S,m);

/*Calculating T square*/

for(i=0;i<m;i++)
{
for(j=0;j<1;j++)
{
Z[i][j]=C[i][j]-U[i][j];
}
}

transposematrix(Z,z,m,1);

matrixmultiplication(B,Z,y,m,m,1);

matrixmultiplication(z,y,Y,1,m,1);

T=n*Y[0][0];

printf("\nCalculated Hotelling T square is %.2f:",T);


return 0;
}

float determinant(float a[max][max],int k)


{
float s=1,det=0,b[max][max];
int i,j,m,n,c;
if (k==1)
{
return (a[0][0]);
}
else
{
det=0;
for (c=0;c<k;c++)
{
m=0;

n=0;
for (i=0;i<k;i++)
{
for (j=0;j<k;j++)
{
b[i][j]=0;
if (i != 0 && j != c)
{
b[m][n]=a[i][j];
if (n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det + s * (a[0][c] * determinant(b,k-1));
s=-1 * s;
}
}

return (det);
}

void cofactor(float num[max][max],int f)


{
float b[max][max],fac[max][max];
int p,q,m,n,i,j;
for (q=0;q<f;q++)
{
for (p=0;p<f;p++)

{
m=0;
n=0;
for (i=0;i<f;i++)
{
for (j=0;j<f;j++)
{
if (i != q && j != p)
{
b[m][n]=num[i][j];
if (n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q + p) * determinant(b,f-1);
}
}
transpose(num,fac,f);
}

/*Finding transpose of matrix*/

void transpose(float num[max][max],float fac[max][max],int r)


{
int i,j;
float b[max][max],d;

for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
d=determinant(num,r);
for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
B[i][j]=b[i][j] / d;
}
}
}

void transposematrix(float X[max][max],float Y[max][max],int r1,int c1)


{
int i,j;
for (i=0;i<c1;i++)
{
for (j=0;j<r1;j++)
{
Y[i][j]=X[j][i];
}
}
}

void matrixmultiplication(float X[max][max],float Y[max][max],float Z[max][max],int r1,int r2,int


c2)
{
int i,j,k;
for(i=0;i<r1;i++)

{
for(j=0;j<c2;j++)
{
for(k=0;k<r2;k++)
{
Z[i][j]+=X[i][k]*Y[k][j];
}
}
}
}

OUTPUT

Write a programme in C language to test the equality of mean vectors


when covariance matrix are equal.
#include<stdio.h>
#include<conio.h>
#include<math.h>

#define max 10

float B[max][max];
float determinant(float a[10][10],int k);
void cofactor(float num[10][10],int f);
void transpose(float num[10][10],float fac[10][10],int r);
void matrixmultiplication(float X[10][10],float Y[10][10],float Z[10][10],int r1,int r2,int c2);
void transposematrix(float X[10][10],float Y[10][10],int r1,int c1);

int main()
{
clrscr();
int i,j,k,n;
float X1[10][10],X2[10][10],C[10][10],S1[10][10],S2[10][10],U[10][10];
float sum,a,b,T,L,result,v,H,F,n1,n2,m;
float Z[10][10],z[10][10],y[10][10],Y[10][10];
float S[10][10],X[10][10];
printf("\nEnter the number of variables: ");
scanf("%d",&n);
printf("\nEnter Sample size n1: ");
scanf("%f",&n1);
printf("\nEnter Sample size n2: ");
scanf("%f",&n2);
printf("\nEnter Mean Vector X1:\n");
for(i=0;i<n;i++)
{

scanf("%f",&X1[i][0]);
}

printf("\nEnter Mean Vector X2:\n");


for(i=0;i<n;i++)
{
scanf("%f",&X2[i][0]);
}

printf("\nEnter Variance Covariance Matrix S1:\n");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%f",&S1[i][j]);
}
printf("\n");
}

printf("\nEnter Variance Covariance Matrix S2:\n");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%f",&S2[i][j]);
}
printf("\n");
}
a=(n1/(n1+n2));
b=(n2/(n1+n2));
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{

S[i][j]=(a*S1[i][j])+(b*S2[i][j]);
}
}

printf("\nSample Variance Covariance Matrix S:\n");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%.1f",S[i][j]);
printf("\t");
}
printf("\n");
}

cofactor(S,n);

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%.1f",B[i][j]);
printf("\t");
}
printf("\n");
}
for(i=0;i<n;i++)
{
for(j=0;j<1;j++)
{
U[i][j]=X1[i][j]-X2[i][j];
}
}

transposematrix(U,Z,n,1);

matrixmultiplication(Z,B,X,1,n,n);
matrixmultiplication(X,U,Y,1,n,1);
printf("\nEnter level of significance: ");
scanf("%f",&L);
m=n;
T=(n1*n2*Y[0][0])/(n1+n2);
printf("\nT sq:%.4f",T);
F=((n1+n2-n-1)*T)/((n1+n2-2)*m);
printf("\nF: %.4f",F);
printf("\n Enter the value of F(%d,%d) from table: ",n,n1+n2-n-1,L);
scanf("%f",&H);
if( F>H)
{
printf("\n We reject the hypothesis");
printf("\n Mean Vectors are not equal");
}
else
{
printf("\n We accept the hypothesis");
printf("\n Mean Vectors are equal");
}
return 0;
}

float determinant(float a[10][10],int k)


{
float s=1,det=0,b[10][10];
int i,j,m,n,c;
if (k==1)
{
return (a[0][0]);

}
else
{
det=0;
for (c=0;c<k;c++)
{
m=0;
n=0;
for (i=0;i<k;i++)
{
for (j=0;j<k;j++)
{
b[i][j]=0;
if (i != 0 && j != c)
{
b[m][n]=a[i][j];
if (n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det + s * (a[0][c] * determinant(b,k-1));
s=-1 * s;
}
}

return (det);
}

void cofactor(float num[10][10], int f)


{
float b[10][10],fac[10][10];
int p,q,m,n,i,j;
for (q=0;q<f;q++)
{
for (p=0;p<f;p++)
{
m=0;
n=0;
for (i=0;i<f;i++)
{
for (j=0;j<f;j++)
{
if (i != q && j != p)
{
b[m][n]=num[i][j];
if (n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q + p) * determinant(b,f-1);
}
}
transpose(num,fac,f);
}

/*Finding transpose of matrix*/

void transpose(float num[10][10],float fac[10][10],int r)


{
int i,j;
float b[10][10],d;

for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
d=determinant(num,r);
for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
B[i][j]=b[i][j] / d;
}
}
}

void transposematrix(float X[10][10],float Y[10][10],int r1,int c1)


{
int i,j;
for (i=0;i<c1;i++)
{
for (j=0;j<r1;j++)
{
Y[i][j]=X[j][i];
}
}

void matrixmultiplication(float X[10][10],float Y[10][10],float Z[10][10],int r1,int r2,int c2)


{
int i,j,k;
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
for(k=0;k<r2;k++)
{
Z[i][j]+=X[i][k]*Y[k][j];
}
}
}
}

OUTPUT

Write a programme in C language to test for covariance matrix to be equal


to a given matrix.
#include<stdio.h>
#include<conio.h>
#include<math.h>

#define max 10

float B[max][max];
float determinant(float a[10][10],int k);
void cofactor(float num[10][10],int f);
void transpose(float num[10][10],float fac[10][10],int r);
float trace(float a[10][10],int k);
void samplemeanvector(float X[max][max], float Y[max][max], int n, int m );
void matrixmultiplication(float X[max][max],float Y[max][max], float Z[max][max], int m, int n);
void transposematrix(float X[max][max],float Y[max][max],int r);
void covariance(float X[max][max],float Y[max][max],float Z[max][max], int n, int m);

int main()
{
clrscr();
int i,j,k,n,m;
float A[10][10],C[10][10],S[10][10],U[10][10];
float sum,D,T,L,result,v,H,result1;
float Z[10][10],z[10][10],y[10][10],Y[10][10];
printf("\n Enter the number of variables: ");
scanf("%d",&m);
printf("\n Enter sample size: ");
scanf("%d",&n);
printf("\n Enter the variance covariance matrix to be tested:\n");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)

{
scanf("%f",&U[i][j]);
}
printf("\n");
}

printf("\n Calculated Variance covariance matrix:\n");


for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
scanf("%f",&S[i][j]);
}
printf("\n");
}

printf("\nEnter level of significance: ");


scanf("%f",&L);

// Testing the hyphothesis


cofactor(U,m);
matrixmultiplication(B,S,Z,m,n);
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%.1f",Z[i][j]);
printf("\t");
}
printf("\n");
}

T=trace(Z,m);
printf("\nTrace:%.4f",T);

D=determinant(Z,m);
printf("\nDeterminant:%.4f",D);
result= (n*T)-(n*m)-(n*log10(D));
v=((m+1)*m)/2;

// Degree of Freedom

printf("\n Enter the value of chi square from table at %.1f degree of freedom and %.1f level of
significance:",v,L);
scanf("%f",&H);
printf("\nResult:%.3f",result);
if( result>H)
{
printf("\n We reject the hypothesis");
printf("\n Given Matrix is not equal to covariance matrix");
}
else
{
printf("\n We accept the hypothesis");
printf("\n Given Matrix is equal to covariance matrix");
}
return 0;
}

float determinant(float a[10][10],int k)


{
float s=1,det=0,b[10][10];
int i,j,m,n,c;
if (k==1)
{
return (a[0][0]);
}
else
{
det=0;
for (c=0;c<k;c++)
{

m=0;
n=0;
for (i=0;i<k;i++)
{
for (j=0;j<k;j++)
{
b[i][j]=0;
if (i != 0 && j != c)
{
b[m][n]=a[i][j];
if (n<(k-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det + s * (a[0][c] * determinant(b,k-1));
s=-1 * s;
}
}

return (det);
}

void cofactor(float num[10][10], int f)


{
float b[10][10],fac[10][10];
int p,q,m,n,i,j;
for (q=0;q<f;q++)
{

for (p=0;p<f;p++)
{
m=0;
n=0;
for (i=0;i<f;i++)
{
for (j=0;j<f;j++)
{
if (i != q && j != p)
{
b[m][n]=num[i][j];
if (n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q + p) * determinant(b,f-1);
}
}
transpose(num,fac,f);
}

/*Finding transpose of matrix*/

void transpose(float num[10][10],float fac[10][10],int r)


{
int i,j;
float b[10][10],d;

for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
d=determinant(num,r);
for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
B[i][j]=b[i][j] / d;
}
}
}

void covariance(float X[max][max],float Y[max][max],float Z[max][max], int n, int m)


{
int i,j,k;
float sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
sum=0;
for(k=0;k<n;k++)
{
sum+=((X[k][i]-Y[i][0])*(X[k][j]-Y[j][0]));
}
Z[i][j]=sum/n;
}
}

void transposematrix(float X[max][max],float Y[max][max],int r)


{
int i,j;

for (i=0;i<r;i++)
{
for (j=0;j<r;j++)
{
Y[i][j]=X[j][i];
}
}
}

void matrixmultiplication(float X[max][max],float Y[max][max], float Z[max][max], int m, int n)


{
int i,j,k;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<m;k++)
{
Z[i][j]+=X[i][k]*Y[k][j];
}
}
}
}

void samplemeanvector(float X[max][max], float Y[max][max], int n, int m )


{
int i,j;
float sum=0;

for(i=0;i<m;i++)
{
sum=0;
for(j=0;j<n;j++)
sum+=X[j][i];

Y[i][0]=sum/n;
}
}

float trace(float a[10][10],int k)


{
float tr=0;
int i,j;
for (i=0;i<k;i++)
{
for (j=0;j<k;j++)
{
if (i==j)
tr+=a[i][j];
}
}
return (tr);
}

OUTPUT

You might also like