# C code to find Covariance and Correlation

How to write a C program to find the Covariance and Correlation.

## C code to find Covariance and Correlation

```#include <stdio.h>

#include <math.h>

class StdDeviation

{

private:

int max;

double value[100];

double mean;

public:

double CalculateMean()

{

double sum = 0;

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

sum += value[i];

return (sum / max);

}

double CalculateVariane()

{

mean = CalculateMean();

double temp = 0;

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

{

temp += (value[i] - mean) * (value[i] - mean) ;

}

return temp / max;

}

double CalculateSampleVariane()

{

mean = CalculateMean();

double temp = 0;

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

{

temp += (value[i] - mean) * (value[i] - mean) ;

}

return temp / (max - 1);

}

int SetValues(double *p, int count)

{

if(count > 100)

return -1;

max = count;

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

value[i] = p[i];

return 0;

}

double Calculate_StandardDeviation()

{

return sqrt(CalculateVariane());

}

double Calculate_SampleStandardDeviation()

{

return sqrt(CalculateSampleVariane());

}

};

class FinanceCalculator

{

private:

double XSeries[100];

double YSeries[100];

int max;

StdDeviation x;

StdDeviation y;

public:

void SetValues(double *xvalues, double *yvalues, int count)

{

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

{

XSeries[i] = xvalues[i];

YSeries[i] = yvalues[i];

}

x.SetValues(xvalues, count);

y.SetValues(yvalues, count);

max = count;

}

double Calculate_Covariance()

{

double xmean = x.CalculateMean();

double ymean = y.CalculateMean();

double total = 0;

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

{

total += (XSeries[i] - xmean) * (YSeries[i] - ymean);

}

}

double Calculate_Correlation()

{

double cov = Calculate_Covariance();

double correlation = cov / (x.Calculate_StandardDeviation() * y.Calculate_StandardDeviation());

return correlation;

}

};

int main()

{

FinanceCalculator calc;

{

printf("\n\nZero Correlation and Covariance Data Set\n");

double xarr[] = { 8, 6, 4, 6, 8 };

double yarr[] = { 10, 12, 14, 16, 18 };

calc.SetValues(xarr,yarr,sizeof(xarr) / sizeof(xarr[0]));

printf("Covariance = %.10lf\n", calc.Calculate_Covariance());

printf("Correlation = %.10lf\n", calc.Calculate_Correlation());

}

{

printf("\n\nPositive Correlation and Low Covariance Data Set\n");

double xarr[] = { 0, 2, 4, 6, 8 };

double yarr[] = { 6, 13, 15, 16, 20 };
calc.SetValues(xarr,yarr,sizeof(xarr) / sizeof(xarr[0]));

printf("Covariance = %.10lf\n", calc.Calculate_Covariance());

printf("Correlation = %.10lf\n", calc.Calculate_Correlation());

}

{

printf("\n\nNegative Correlation and Low Covariance Data Set\n");

double xarr[] = { 8, 6, 4, 2, 0 };

double yarr[] = { 6, 13, 15, 16, 20 };

calc.SetValues(xarr,yarr,sizeof(xarr) / sizeof(xarr[0]));

printf("Covariance = %.10lf\n", calc.Calculate_Covariance());

printf("Correlation = %.10lf\n", calc.Calculate_Correlation());

}
{

printf("\n\nPositive Correlation and High Covariance Data Set\n");

double xarr[] = { 8, 6, 4, 2, 0 };

double yarr[] = { 1006, 513, 315, 216, 120 };

calc.SetValues(xarr,yarr,sizeof(xarr) / sizeof(xarr[0]));

printf("Covariance = %.10lf\n", calc.Calculate_Covariance());

printf("Correlation = %.10lf\n", calc.Calculate_Correlation());

}

{

printf("\n\nNegative Correlation and High Covariance Data Set\n");

double xarr[] = { 8, 6, 4, 2, 0 };

double yarr[] = { 120, 216, 315, 513, 1006 };

calc.SetValues(xarr,yarr,sizeof(xarr) / sizeof(xarr[0]));

printf("Covariance = %.10lf\n", calc.Calculate_Covariance());

printf("Correlation = %.10lf\n", calc.Calculate_Correlation());

}

}```

## Write a C program to find Correlation

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

int main() {
int x[100], y[100], xy[100], xsquare[100], ysquare[100];
int i, n, xsum, ysum, xysum, xsqr_sum, ysqr_sum;
float coeff, num, deno;

xsum = ysum = xysum = xsqr_sum = ysqr_sum = 0;

/* get the number of entries from the user */
printf("Enter the value for n:");
scanf("%d", &n);

/* get the values for x and y  from the user */
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]);
}

/* find the needed data to manipulate correlation coeff */
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 = xsum + x[i];
ysum = ysum + y[i];
xysum = xysum + xy[i];
xsqr_sum = xsqr_sum + xsquare[i];
ysqr_sum = ysqr_sum + ysquare[i];
}

num = 1.0 * ((n * xysum) - (xsum * ysum));
deno = 1.0 * ((n * xsqr_sum - xsum * xsum)* (n * ysqr_sum - ysum * ysum));

/* calculate correlation coefficient */
coeff = num / sqrt(deno);

/* print the result */
printf("Correlation Coefficient : %.4f\n", coeff);
return 0;
}
```