在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=“区域1”中的观测数
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*(S12+d12)+n2*(S22+d22)/(n1+n2)
示例
// C++ program to find combined mean //和两个系列的方差。 #include <bits/stdc++.h> using namespace std; //显示查找序列均值的功能。 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; } //显示功能以查找标准 //系列的偏差。 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; } //显示函数以查找组合方差 //两个不同的系列。 float combinedVariance(int Arr1[], int Arr2[], int b, int a){ //在这里,mean1和mean2是平均值 //两个数组。 float mean1 = mean(Arr1, b); float mean2 = mean(Arr2, a); cout << "Mean1: " << mean1 << " mean2: " << mean2 << endl; //在这里,sd1和sd2是标准 //两个数组的偏差。 float sd1 = sd(Arr1, b); float sd2 = sd(Arr2, a); cout << "StandardDeviation1: " << sd1 << " StandardDeviation2: " << sd2 << endl; //在这里,combinedMean是要存储的变量 //两个数组的组合均值。 float combinedMean = (float)(b * mean1 + a * mean2) / (b + a); cout << "Combined Mean: " << combinedMean << endl; //在这里,d1_square和d2_square是 //组合平均偏差。 float d1_square = (mean1 - combinedMean) *(mean1 - combinedMean); float d2_square = (mean2 - combinedMean) *(mean2 - combinedMean); cout << "d1 square: " << d1_square<< " d2_square: " << d2_square << endl; //在这里,combinedVar是要存储的变量 //两个数组的组合方差。 float combinedVar = (b * (sd1 + d1_square) + a *(sd2 + d2_square)) / (b + a); cout << "Combined Variance: " << combinedVar; } //驱动程序功能。 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]); //显示对组合均值的函数调用。 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