- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find combined mean and variance of two series in C++
Concept
With respect of two given two different series arr1[b] and arr2[a] of size b and a. Our task is to determine the mean and variance of combined series.
Input
Arr1[] = { 24, 46, 35, 79, 13, 77, 35 }; Arr2[] = { 66, 68, 35, 24, 46 };
Output
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
Method
Now suppose,
n1= No. of observations in 'region 1'
n2= No. of observations in 'region 1'
X1= mean of region 1.
X2=mean of region 2.
S1=standard deviation of region 1.
S2=standard deviation of region 2.
S12 = variance of region 1.
S22 = variance of region 2.
Let X=mean of total group
So d1=X – X1
and d2= X – X2
Calculate the mean of total group X as
(n1*X1+n2*X2)/(n1+n2)
Calculate the variance of total group as
n1*(S1 2+d1 2)+n2*(S2 2+d2 2)/(n1+n2)
Example
// 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; }
Output
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
Advertisements