给定两组随机变量,找到协方差。协方差是两个随机变量一起变化的量度。它与方差相似,但是方差告诉您单个变量如何变化,协方差告诉您两个变量如何一起变化。可以使用以下公式计算协方差
其中x’和y’是两个给定集合的均值。
例子:
Input : arr1[] = {65.21, 64.75, 65.26, 65.76, 65.96}
arr2[] = {67.25, 66.39, 66.12, 65.70, 66.64}
Output : -0.0580511
Input : arr1[] = {5, 20, 40, 80, 100}
arr2[] = {10, 24, 33, 54, 10}
Output : 187.75
C++
// C++ Program to find
// covariance of two set.
#include
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;
}
// Function to find 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);
}
// Driver function.
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;
}
Java
// Java Program to find
// covariance of two set.
import java.io.*;
class GFG {
// Function to find mean.
static float mean(float arr[], int n)
{
float sum = 0;
for(int i = 0; i < n; i++)
sum = sum + arr[i];
return sum / n;
}
// Function to find covariance.
static 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);
}
// Driver code
public static void main (String[] args) {
float arr1[] = {65.21f, 64.75f,
65.26f, 65.76f, 65.96f};
int n = arr1.length;
float arr2[] = {67.25f, 66.39f,
66.12f, 65.70f, 66.64f};
int m = arr2.length;
if (m == n)
System.out.println(covariance(arr1, arr2, m));
}
}
// This code is contributed by Gitanjali.
Python3
# Python3 Program to find
# covariance of two set.
import math
# Function to find mean.
def mean(arr, n):
sum = 0
for i in range(0, n):
sum = sum + arr[i]
return sum / n
# Function to find covariance.
def covariance(arr1, arr2, n):
sum = 0
for i in range(0, n):
sum = (sum + (arr1[i] - mean(arr1, n)) *
(arr2[i] - mean(arr2, n)))
return sum / (n - 1)
# Driver method
arr1 = [65.21, 64.75, 65.26, 65.76, 65.96]
n = len(arr1)
arr2 = [67.25, 66.39, 66.12, 65.70, 66.64]
m = len(arr2)
if (m == n):
print (covariance(arr1, arr2, m))
# This code is contributed by Gitanjali.
C#
// C# Program to find
// covariance of two set.
using System;
class GFG {
// Function to find mean.
static float mean(float []arr, int n)
{
float sum = 0;
for(int i = 0; i < n; i++)
sum = sum + arr[i];
return sum / n;
}
// Function to find covariance.
static 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);
}
// Driver code
public static void Main () {
float []arr1 = {65.21f, 64.75f,
65.26f, 65.76f, 65.96f};
int n = arr1.Length;
float []arr2 = {67.25f, 66.39f,
66.12f, 65.70f, 66.64f};
int m = arr2.Length;
if (m == n)
Console.WriteLine(covariance(arr1, arr2, m));
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出:
-0.0580511