Error in formula Variance


#1

In task is wrong formula.

04 for each score in scores: Compute its squared difference: (average - score) ** 2 and add that to variance.

Correct formula should be:
(score - average) ** 2

Mathematical formula:

(1/Number of occurrences) * Summation (sigma symbol) i = 1 to Number of occurrences (xi - average) **2

link:https://en.wikipedia.org/wiki/Variance

Code:

def grades_variance(scores):
    average = grades_average(scores)
    variance = 0
    
    for score in scores:
        variance += (score - average) ** 2
    return variance/len(scores)

#2

Be sure your grades_average function is returning a float.


#3

Yes, it returns float.


#4

What error message are you getting?


#5

Way to go! :slight_smile:
No error.


#6

Then I don't understand the issue. Are you referring to the order of subtraction?

Think about it... (a - b) ** 2 == (b - a) ** 2

It doesn't matter the order, the outcome is still the same. That is the purpose of squaring. Recall that variance is sigma squared (sigma being standard deviation).


#7

It is possible, but after entering the code by the formula, program works.
Previously showed errors.
(Something like: code returns 0.27485..... but expected 7.264855.....)
I switched it and then it worked.

Maybe parser error.


#8

Is this the exercise with four functions?

grades_sum
grades_average
grades_variance
standard_deviation

(Names may be off).


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.