# 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

• return sdd

• 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)

• display combinedMean

• 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)

• display comb_var

## Example

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)

## Input

[24, 46, 35, 79, 13, 77, 35 ],[66, 68, 35, 24, 46 ]

## Output

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

Updated on: 25-Aug-2020

466 Views