# C++ - Calculate Covariance and Correlation

Mean, Variance and Standard Deviation are widely used in statistical application for single series. If we have two sets of series, then we may need covariance and correlation to find the relationship between the two. It is a good idea to start writing program in C++ on this.

To calculate covariance between two sets of series, we need to multiply the difference between its mean for each term for the two series and add each term resultant value. Finally divide by N - number of terms in the series.

Correlation coefficient is the ratio between the covariance and the multiplication of the standard deviation for the two series. The range for correlation is always between -1 to +1.

The complete program compatible in Turbo C++ and Visual C++ Compilers and test run output are given below:

## Source Code

#include <stdio.h>

#include <math.h>

class StdDeviation

{

private:

int max;

double value;

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;

double YSeries;

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;

}

};

void 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));

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));

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));

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));

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));

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

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

}

}

## Output

Zero Correlation and Covariance Data Set

Covariance = 0.0000000000

Correlation = 0.0000000000

Positive Correlation and Low Covariance Data Set

Covariance = 12.4000000000

Correlation = 0.9521574311

Negative Correlation and Low Covariance Data Set

Covariance = -12.4000000000

Correlation = -0.9521574311

Positive Correlation and High Covariance Data Set

Covariance = 827.6000000000

Correlation = 0.9311642376

Negative Correlation and High Covariance Data Set

Covariance = -827.6000000000

Correlation = -0.9311642376