Program to find covariance in C++


In this tutorial, we will be discussing a program to find covariance.

For this we will be provided with two sets of random variables. Our task is to calculate their covariance i.e, the measure of how much those two values differ together.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
//function to find mean
float mean(float arr[], int n){
   float sum = 0;
   for(int i = 0; i < n; i++)
   sum = sum + arr[i];
   return sum / n;
}
//finding covariance
float covariance(float arr1[], float arr2[], int n){
   float sum = 0;
   for(int i = 0; i < n; i++)
      sum = sum + (arr1[i] - mean(arr1, n)) * (arr2[i] - mean(arr2, n));
   return sum / (n - 1);
}
int main(){
   float arr1[] = {65.21, 64.75, 65.26, 65.76, 65.96};
   int n = sizeof(arr1) / sizeof(arr1[0]);
   float arr2[] = {67.25, 66.39, 66.12, 65.70, 66.64};
   int m = sizeof(arr2) / sizeof(arr2[0]);
   if (m == n)
      cout << covariance(arr1, arr2, m);
   return 0;
}

Output

-0.0580511

Updated on: 09-Sep-2020

476 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements