fbpx

C++: Sample Covariance

Bjarne-stroustrup
 

double sample_covariance(double A[], double B[], int length) 
// Function calculates the sample covariance the array A and array B, whose lengths are "length"
{  
	double meanA = mean_value(A, length);
	double meanB = mean_value(B, length);
	double SOS = 0;
	for (int i=0; i<length; i++) 
	{  SOS += (A[i] - meanA) * (B[i] - meanB); }
	return double sample_cov = SOS / length; 
}