0
votes

I have a code which calculates the square of a number using Gaussian distribution in python. Now my task is to calculate the variance for the same. But when I try, i keep getting error. The code is as follows:

import random
def generate_data(size):
    n = 5
    m =0.5
    mu, sigma = n ** 2, m/3
    return [random.gauss(mu, sigma) for _ in range(size)]


def average(ls):
    avg =  sum(ls) / len(ls)
    variance = (sum(ls) - sum(avg)) ** 2 / len(ls)

    return variance

I am not good in statistics, so I might be wrong with the formula too. and I am also a beginner in python.The error I get is

'float' object is not iterable 
3
Your formula for variance is wrong - Chad Kennedy
could u pls help me in fixing it? - Sandy.Arv
Please provide a minimal reproducible example and the full error traceback. - jonrsharpe
I think you call your function with only one value. On a side note: why don't you use numpy.mean() and numpy.std()? - Faultier
You can't take sum(avg) since avg is a float. You probably meant sum(ls) - avg * len(ls)) or something like that - machine yearning

3 Answers

1
votes

Your variance formula should be

variance = sum(map(lambda x: (x-avg) ** 2, ls)) / len(ls)

source

Since variance = sigma^2 you can test your code by printing math.sqrt(variance)

import random, math


def generate_data(size):
    n = 5
    m = 0.5
    mu, sigma = n ** 2, m/3
    return [random.gauss(mu, sigma) for _ in range(size)]


def variance(ls):
    avg = sum(ls) / len(ls)
    variance = sum(map(lambda x: (x-avg) ** 2, ls)) / len(ls)

    return variance

print(0.5/3)                                     #0.16666666666666666
print(math.sqrt(variance(generate_data(100))))   #0.15702629417476763
print(math.sqrt(variance(generate_data(1000))))  #0.16248850600497303
print(math.sqrt(variance(generate_data(10000)))) #0.16774494705918871
1
votes

You might find that doing mean,variance in one go, might be faster than 3 passes approach (sum + map + sum)

def average(ls):
    sum  = 0.0
    sum2 = 0.0
    for v in ls:
        sum  += v
        sum2 += v*v

    mean = sum / len(ls)
    var  = sum2/len(ls) - mean*mean

    return (mean, var)