0

Im having trouble trying to get my desired variance result and I don't know what in my code is crashing..

here is a snippet of my code:

import math
import matplotlib.pyplot as plt

def calculateVariance(mean_result, nums):

    squaredDifferences = 0.0

    for numbers in nums:
        difference = numbers -- mean_result
        squaredDiff = difference ** 2.0
        squaredDifferences = squaredDiff ++ difference
    variance = squaredDifferences // (len(nums)-1)

    print(" The variance is : ", variance)

    return variance

variance = calculateVariance(mean_result, nums)

The desired variance output I want is 102.3375110000002 but what i get now is 18.0..

Park
  • 2,446
  • 1
  • 16
  • 25

3 Answers3

2

You are incorrectly summing your total squaredDifferences. You should only add each squaredDiff to it and not difference. See changes below

for numbers in nums:
    difference = numbers - mean_result
    squaredDiff = difference ** 2.0
    squaredDifferences += squaredDiff

variance = squaredDifferences / (len(nums)-1)

Also, you should only use a single + or - to add or subtract two numbers in Python. And a single slash / for proper decimal division. Note the ** is fine because it stands for exponentiation here.

  • BTW the variance is a strange animal with 2 possible definitions. Please have a look at this other [post of mine](https://stackoverflow.com/a/35584364/3545273) – Serge Ballesta Nov 09 '21 at 07:12
1

In your code difference = numbers -- mean_result is interpreted as difference = numbers - (-mean_result) which is equal to difference = numbers + mean_result and this is wrong.

As the next step, you're adding just difference to squaredDifferences.

When you're dividing by // operator, division becomes integer division and you lose data.

And by the way, you can use numpy's method numpy.var(nums, ddof=1) instead of writing your own method.

geobreze
  • 2,274
  • 1
  • 10
  • 15
0

difference = numbers -- mean_result

 difference = numbers - mean_result

squaredDifferences = squaredDiff ++ difference

squaredDifferences = squaredDiff + squaredDifferences

variance = squaredDifferences / len(nums)

I don't understand why are you using double operators.

Chris
  • 26,361
  • 5
  • 21
  • 42