Orianna Sanele
Orianna Sanele

Reputation: 13

How to calculate variance in a function using for loop?

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..

Upvotes: 0

Views: 453

Answers (3)

Tauqeer Akhtar
Tauqeer Akhtar

Reputation: 77

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.

Upvotes: 0

geobreze
geobreze

Reputation: 2422

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.

Upvotes: 1

Pedro Contipelli
Pedro Contipelli

Reputation: 387

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.

Upvotes: 2

Related Questions