- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 Python
Suppose we have two different series A1 and A2 of size b and a respectively. We have to find the mean and variance of combined series.
So, if the input is like A1 = [24, 46, 35, 79, 13, 77, 35] and A2 = [66, 68, 35, 24, 46], then the output will be Mean = [44.1429, 47.8], sd = [548.694, 294.56], combined mean = 45.6667, d1_square = 2.322, d2_square = 4.5511, combined_var = 446.056
To solve this, we will follow these steps −
Define a function mean() . This will take arr
return average of arr elements
Define a function sd() . This will take arr, n
sum := 0;
for i in range 0 to n, do
sum := sum +((arr[i] - mean(arr)) * (arr[i] - mean(arr)))
sdd := sum / n
From the main method, do the following −
n := size of A1, m := size of A2
mean1 := mean(A1), mean2 := mean(A2)
display mean1 and mean2
sd1 := sd(A1, n), sd2 := sd(A2, m)
display sd1 and sd2
combinedMean :=(n * mean1 + m * mean2) /(n + m)
d1_square :=(mean1 - combinedMean) *(mean1 - combinedMean)
d2_square :=(mean2 - combinedMean) *(mean2 - combinedMean)
display d1_square, d2_square
comb_var :=(n *(sd1 + d1_square) + m *(sd2 + d2_square)) /(n + m)
Let us see the following implementation to get better understanding −
def mean(arr): return sum(arr)/len(arr) def sd(arr, n): sum = 0; for i in range(n): sum = sum + ((arr[i] - mean(arr)) * (arr[i] - mean(arr))) sdd = sum / n return sdd def combinedVariance(A1, A2): n = len(A1) m = len(A2) mean1 = mean(A1) mean2 = mean(A2) print("mean_1: ", round(mean1, 2), " mean_2: ", round(mean2, 2)) sd1 = sd(A1, n) sd2 = sd(A2, m) print("sd_1: ", round(sd1, 2)," sd_2: ", round(sd2, 2)) combinedMean = (n * mean1 + m * mean2) / (n + m) print("Combined Mean: ", round(combinedMean, 2)) d1_square = ((mean1 - combinedMean) * (mean1 - combinedMean)) d2_square = ((mean2 - combinedMean) * (mean2 - combinedMean)) print("d1_square: ", round(d1_square, 2), " d2_square: ", round(d2_square, 2)) comb_var = (n * (sd1 + d1_square) + m * (sd2 + d2_square)) / (n + m) print("Combined Variance: ", round(comb_var, 2)) A1 = [24, 46, 35, 79, 13, 77, 35 ] A2 = [66, 68, 35, 24, 46 ] n = len(A1) m = len(A2) combinedVariance(A1, A2)
[24, 46, 35, 79, 13, 77, 35 ],[66, 68, 35, 24, 46 ]
mean_1: 44.14 mean_2: 47.8 sd_1: 548.69 sd_2: 294.56 Combined Mean: 45.67 d1_square: 2.32 d2_square: 4.55 Combined Variance: 446.06
- Find combined mean and variance of two series in C++
- Mean-Variance criterion for defining efficient and inefficient assets
- Count from two tables and give combined count of string in MySQL?
- Python Pandas - Select a subset of rows and columns combined
- Difference between Cost Variance and Schedule Variance
- Print the mean of a Pandas series
- Find Harmonic mean using Arithmetic mean and Geometric mean using C++.
- Find Mean of a List of Numpy Array in Python
- What does series mean in pandas?
- How is the standard deviation and variance of a two-asset portfolio calculated?
- Variance and Standard Deviation
- Find Rolling Mean – Python Pandas
- How to convert a variable into zero mean and unit variance in an R data frame?
- C++ program to find first collision point of two series
- How to find the variance of frequency data in R?