แนวคิด
ด้วยความเคารพของสองชุดที่แตกต่างกันสองชุด 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
ส1 =ค่าเบี่ยงเบนมาตรฐานของภาค 1
ส2 =ค่าเบี่ยงเบนมาตรฐานของภาค 2
ส1 2 =ความแปรปรวนของภูมิภาค 1
ส2 2 =ความแปรปรวนของภูมิภาค 2
ให้ X=ค่าเฉลี่ยของกลุ่มทั้งหมด
ดี1 =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