在 C++ 中查找两个序列的组合均值和方差


概念

针对给定的两个不同序列 arr1[b] 和 arr2[a],它们的大小分别为 b 和 a。我们的任务是确定组合序列的均值和方差。

输入

Arr1[] = { 24, 46, 35, 79, 13, 77, 35 };
Arr2[] = { 66, 68, 35, 24, 46 };

输出

Mean1: 44.1429
Mean2: 47.8
StandardDeviation1: 548.694
StandardDeviation2: 294.56
Combined Mean: 45.6667
d1 square: 2.322
d2_square: 4.5511
Combined Variance: 446.056

方法

现在假设:

n1 = “区域 1”中的观测值数量

n2 = “区域 2”中的观测值数量

X1 = 区域 1 的均值。

X2 = 区域 2 的均值。

S1 = 区域 1 的标准差。

S2 = 区域 2 的标准差。

S12 = 区域 1 的方差。

S22 = 区域 2 的方差。

设 X = 总群体的均值

所以 d1 = X – X1

以及 d2 = X – X2

计算总群体的均值 X 为

(n1*X1+n2*X2)/(n1+n2)

计算总群体的方差为

n1*(S1 2+d1 2)+n2*(S2 2+d2 2)/(n1+n2)

示例

 实时演示

// C++ program to find combined mean
// and variance of two series.
#include <bits/stdc++.h>
using namespace std;
// Shows function to find mean of series.
float mean(int Arr[], int b){
   int sum1 = 0;
   for (int i = 0; i < b; i++)
      sum1 = sum1 + Arr[i];
   float mean = (float)sum1 / b;
   return mean;
}
// Shows function to find the standard
// deviation of series.
float sd(int Arr[], int b){
   float sum1 = 0;
   for (int i = 0; i < b; i++)
      sum1 = sum1 + (Arr[i] - mean(Arr, b)) *
   (Arr[i] - mean(Arr, b));
   float sdd = sum1 / b;
   return sdd;
}
//Shows function to find combined variance
// of two different series.
float combinedVariance(int Arr1[], int Arr2[],
int b, int a){
   // Here, mean1 and mean2 are the mean
   // of two arrays.
   float mean1 = mean(Arr1, b);
   float mean2 = mean(Arr2, a);
   cout << "Mean1: " << mean1
   << " mean2: " << mean2 << endl;
   // Here, sd1 and sd2 are the standard
   // deviation of two array.
   float sd1 = sd(Arr1, b);
   float sd2 = sd(Arr2, a);
   cout << "StandardDeviation1: " << sd1
   << " StandardDeviation2: " << sd2
   << endl;
   // Here, combinedMean is variable to store
   // the combined mean of both array.
   float combinedMean = (float)(b * mean1 +
   a * mean2) / (b + a);
   cout << "Combined Mean: " << combinedMean
   << endl;
   // Here, d1_square and d2_square are
   // the combined mean deviation.
   float d1_square = (mean1 - combinedMean) *(mean1 - combinedMean);
   float d2_square = (mean2 - combinedMean) *(mean2 - combinedMean);
   cout << "d1 square: " << d1_square<< " d2_square: " << d2_square
   << endl;
   // Here, combinedVar is variable to store
   // combined variance of both array.
   float combinedVar = (b * (sd1 + d1_square) + a *(sd2 + d2_square)) / (b + a);
   cout << "Combined Variance: " << combinedVar;
}
// Driver function.
int main(){
   int Arr1[] = { 24, 46, 35, 79, 13, 77, 35 };
   int Arr2[] = { 66, 68, 35, 24, 46 };
   int b = sizeof(Arr1) / sizeof(Arr1[0]);
   int a = sizeof(Arr2) / sizeof(Arr2[0]);
   // Shows function call to combined mean.
   combinedVariance(Arr1, Arr2, b, a);
   return 0;
}

输出

Mean1: 44.1429 mean2: 47.8
StandardDeviation1: 548.694 StandardDeviation2: 294.56
Combined Mean: 45.6667
d1 square: 2.322 d2_square: 4.5511
Combined Variance: 446.056

更新于: 2020-07-24

113 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.