在本教程中,我们将讨论一个寻找协方差的程序。
为此,我们将提供两组随机变量。我们的任务是计算它们的协方差,即这两个值相差多少的度量。
#include<bits/stdc++.h> using namespace std; //查找均值的功能 float mean(float arr[], int n){ float sum = 0; for(int i = 0; i < n; i++) sum = sum + arr[i]; return sum / n; } //寻找协方差 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