将协方差应用在 C++ 中的程序
在本教程中,我们将讨论一个查找协方差的程序。
为此,我们将提供两组随机变量。我们的任务是计算它们的协方差,即衡量这两个值差异的程度。
示例
#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; }
输出
-0.0580511
广告