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 arrreturn average of arr elements
Define a function mean() . This will take arr
return average of arr elements
return average of arr elements
Define a function sd() . This will take arr, n
Define a function sd() . This will take arr, n
sum := 0;
sum := 0;
for i in range 0 to n, dosum := sum +((arr[i] - mean(arr)) * (arr[i] - mean(arr)))
for i in range 0 to n, do
sum := sum +((arr[i] - mean(arr)) * (arr[i] - mean(arr)))
sum := sum +((arr[i] - mean(arr)) * (arr[i] - mean(arr)))
sdd := sum / n
sdd := sum / n
return sdd
return sdd
From the main method, do the following −
From the main method, do the following −
n := size of A1, m := size of A2
n := size of A1, m := size of A2
mean1 := mean(A1), mean2 := mean(A2)
mean1 := mean(A1), mean2 := mean(A2)
display mean1 and mean2
display mean1 and mean2
sd1 := sd(A1, n), sd2 := sd(A2, m)
sd1 := sd(A1, n), sd2 := sd(A2, m)
display sd1 and sd2
display sd1 and sd2
combinedMean :=(n * mean1 + m * mean2) /(n + m)
combinedMean :=(n * mean1 + m * mean2) /(n + m)
display combinedMean
display combinedMean
d1_square :=(mean1 - combinedMean) *(mean1 - combinedMean)
d1_square :=(mean1 - combinedMean) *(mean1 - combinedMean)
d2_square :=(mean2 - combinedMean) *(mean2 - combinedMean)
d2_square :=(mean2 - combinedMean) *(mean2 - combinedMean)
display d1_square, d2_square
display d1_square, d2_square
comb_var :=(n *(sd1 + d1_square) + m *(sd2 + d2_square)) /(n + m)
comb_var :=(n *(sd1 + d1_square) + m *(sd2 + d2_square)) /(n + m)
display comb_var
display comb_var
Let us see the following implementation to get better understanding −
 Live Demo
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