You are on page 1of 14

Q.

Implementation any one of the frequency test for the random number generated from the
function Rand(),conclude whether the generated random number are acceptable or not.

Conditions:

K-S test: Generate 20 random numbers and consider the significance value =0.5

Chi-square test: Generate 20 random numbers and consider the significance value =0.5

Solution:

K-S test

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include<time.h>

float dplus(float num[], int n);

float dminus(float num[], int n);

float larges(float data[], int n);

int main()

time_t t;

srand((unsigned) time(&t));

printf("Kolmogorov Test\n");

int n;
float dvalue1;

n=20;

float num[n];

float dp, dn;

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

num[i]=(rand() %50);

//sorting in ascending order

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

for(int j=i+1; j<n; j++)

if(num[i]>num[j])

float temp = num[i];

num[i] = num[j];

num[j] = temp;

printf("\nNumbers in ascending order is: \t");


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

printf("%0.2f\n",num[i]);

dp = dplus(num, n);

dn = dminus(num, n);

printf("\ndp = %f",dp);

printf("\ndp = %f",dn);

if(dp>dn)

dvalue1 = dp;

else

dvalue1 = dn;

float dvalue = 0.565; //for alpha = 0.05

printf("\nCalculated D = %0.2f",dvalue1);

if(dvalue1 < dvalue)

printf("\n Since D is less tha Dalpha so the data is uniformily distributed.");

}
else

printf("\nSince D is greater than Dalpha so the data is not uniformily distributed.");

return 0;

float dplus(float num[], int n)

float data[n];

int count=1;

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

while(count<=n)

data[i] = ((count/n)-num[i]);

count++;

float lar = larges(data, n);

return lar;

float dminus(float num[], int n)


{

float data[n];

int count=1;

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

while(count<=n)

data[i] = (num[i]-((count-1)/n));

count++;

float lar = larges(data, n);

return lar;

float larges(float data[], int n)

for(int i=1; i<n; i++)

if(data[0]<data[i])
data[0] = data[i];

float lar = data[0];

return lar;

Output:

Kolmogorov Test

Numbers in ascending order is:

14.00

16.00

17.00

23.00

30.00

30.00

31.00

31.00

32.00

34.00

36.00

39.00

39.00
41.00

43.00

44.00

44.00

47.00

47.00

49.00

dp = 0.000000

dp = 14.000000

Calculated D = 14.00

Since D is greater than Dalpha so the data is not uniformily distributed.

Chi-Square Test

#include
<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

// maximum size for file name


#define LEN 100
double chiSquare(long long int dim, double expected[], double observed[]);
double distChiSquare(long long int k, double array[]);
double average(long long int N, double array[]);
double sigma(long long int N, double average, double array[]);
int fileLines(char file[]);

int main(){
long long int i, dim;
char expected[LEN], observed[LEN];
FILE *input;

printf("\nEnter the file name (or path) with the expected data: ");
fgets(expected,sizeof(expected),stdin);
expected[strlen(expected)-1] = 0;

printf("\nEnter the file name (or path) with the observed data: ");
fgets(observed,sizeof(expected),stdin);
observed[strlen(observed)-1] = 0;

// file lines expected == file line observed


dim = fileLines(expected);
if(dim <= 5){
printf("\nUnreliable test. Insufficient variables number.\n");
}

// arrays for expected data


double *vect1x = calloc(dim,sizeof(double));
double *vect1y = calloc(dim,sizeof(double));
// arrays for observed data
double *vect2x = calloc(dim,sizeof(double));
double *vect2y = calloc(dim,sizeof(double));
if(vect1x == NULL || vect1y == NULL || vect2x == NULL || vect2y ==
NULL){
perror("\nError");
exit(1);
}

// take the expected data


input = fopen(expected,"r");
if(input == NULL){
perror("\nError");
exit(1);
}

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


fscanf(input,"%lf %lf",&vect1x[i],&vect1y[i]);
}

fclose(input);

// take the observed data


input = fopen(observed,"r");
if(input == NULL){
perror("\nError");
exit(1);
}

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


fscanf(input,"%lf %lf",&vect2x[i],&vect2y[i]);
}

fclose(input);

printf("Computing...\n");

// observed data average


double x = average(dim,vect2y);
// computing chi square
double chi = chiSquare(dim,vect1y,vect2y);
// calculate the density of probability

double freeDegree = dim;


double probab = (1 / (pow(2,(int)freeDegree/2) * tgamma(freeDegree))) *
pow(x,(int)(freeDegree/2)-1) * exp(-(x/2));

//printf("Have a probability of %lf%% to have a chi square of


%lf\n",probab*100,chi);
printf("Chi square: %lf\n",chi);

free(vect1x);
free(vect1y);
free(vect2x);
free(vect2y);

return 0;
}

double chiSquare(long long int dim, double expected[], double observed[]){


long long int i, freeDegree=dim-1;
double sum=0;

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


sum += (pow((expected[i] - observed[i]),2))/expected[i];
}

return sum;
}

double distChiSquare(long long int k, double array[]){


long long int i;
double sumSquare=0;

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


sumSquare += pow(array[i],2);
}

return sumSquare;
}

double average(long long int N, double array[]){


long long int i;
double sum = 0;

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


sum += array[i];
}
sum = (double)sum/N;

return sum;
}

double sigma(long long int N, double average, double array[]){


long long int i;
double sigma, sum = 0;

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


sum += pow((array[i] - average),2);
}

sigma = sqrt(sum/(N-1));

return sigma;
}

int fileLines(char file[]){


int lines=0;
char c;

FILE *input = fopen(file,"r");


if(input == NULL){
perror("\nError");
exit(1);
}
while((c = getc(input)) != EOF){
if(c == '\n'){
lines++;
}
}

fclose(input);

return lines;
}

Output:

Number of Observations = 100

Sample Standard Deviation = 0.00628

Expected True Standard Deviation = 0.10000

Test Statistic = 0.39030

CDF of test statistic: = 1.438e-099

Upper Critical Value at alpha: = 1.232e+002

Upper Critical Value at alpha/2: = 1.284e+002

Lower Critical Value at alpha: = 7.705e+001

Lower Critical Value at alpha/2: = 7.336e+001

Results for Alternative Hypothesis and alpha = 0.0500

Alternative Hypothesis Conclusion

Standard Deviation != 0.100 NOT REJECTED


Standard Deviation < 0.100 NOT REJECTED

Standard Deviation > 0.100 REJECTED

You might also like