
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
#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
Advertisements