Kamila
Kamila

Reputation: 45

Calculate variance of an array using for-loops, Python

I have literally just started learning Python and now I have to calculate mean and variance in an array using for-loops.

At the moment, I am getting an incorrent variance value and I'm not sure why.

numbers = [7, 16, 0.3, 0, 15, -4, 5, 3, 15]

sum = 0

for value in range(0, len(numbers)):
    sum = sum + numbers[value]
    mean_value = sum / len(numbers)

print(mean_value)

n = len(numbers)
sum_squares = 0
for value in range(0, len(numbers)):
    sum_squares = sum_squares + (value - mean_value) ** 2
    var_value = sum_squares / n

print(var_value)

According to an online calculator I should get variance = 55.035 and I am getting a variance of 12.267

I would appreciate any help.

K

EDIT: I just realised that changing the second part to this works:

for value in numbers:
    sum_squares = sum_squares + ((value - mean_value) ** 2)
    var_value = sum_squares / n

Not sure what the exact difference between "for value in numbers:" and "for value in range(0, len(numbers)):" is but it this is what helped.

Upvotes: 0

Views: 1372

Answers (1)

John Mommers
John Mommers

Reputation: 140

Variance is deviding by (n-1)

numbers = [7, 16, 0.3, 0, 15, -4, 5, 3, 15]
sum = 0
count = 0
sum_squares = 0

for value in numbers:
  sum = sum + value
  count = count + 1
  
mean_value = (sum / count)

for value in numbers:
    sum_squares = sum_squares + ((value - mean_value)** 2)
    var_value = sum_squares / (count-1)

print (mean_value)
print (var_value)

Out:
6.366666666666666
55.035

Upvotes: 2

Related Questions