Variance using Gaussian distribution Python

Sandy.Arv

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 
sve

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

square of a number using gaussian distribution

From Dev

Python: Creating a Gaussian distribution for a variable and running a program on a loop using the Gaussian values

From Dev

Python: Creating a Gaussian distribution for a variable and running a program on a loop using the Gaussian values

From Dev

Calculate moments (mean, variance) of distribution in python

From Dev

Generating 3D Gaussian distribution in Python

From Dev

How to fit a double Gaussian distribution in Python?

From Dev

Python - Integrating entire list with the Gaussian distribution

From Dev

Draw multivariate Gaussian distribution samples using Python numpy.random.randn

From Dev

Matlab Find the mean and variance of a Gaussian

From Dev

gaussian fitting not working using Python

From Dev

From Matlab to Python: N point with Gaussian Distribution inside a rectangle

From Dev

How to efficiently compute the heat map of two Gaussian distribution in Python?

From Dev

Fitting an image to Gaussian distribution

From Dev

Multiple Gaussian distribution

From Dev

Dealing with normal (Gaussian) distribution

From Dev

Separating gaussian components of a curve using python

From Dev

Strange behaviour with Gaussian random distribution

From Dev

Fail to generate gaussian distribution with seed

From Dev

Gaussian distribution for censored regression with cenreg

From Dev

Inverse of the cumulative gaussian distribution in R

From Dev

Multivariate Gaussian distribution formula implementation

From Dev

Incanter sample mean and variance not close to distribution mean and variance

From Dev

Incanter sample mean and variance not close to distribution mean and variance

From Dev

Sample mean and variance of a Weibull distribution sample

From Dev

empirical mean and variance plot in matlab with the normal distribution

From Java

Gaussian distribution function different answer for program and math

From Dev

How to plot a Gaussian Distribution on y-axis?

From Dev

Apply a gaussian distribution in a specific part of an image

From Dev

Fitting bimodal gaussian distribution with some parameters fixed

Related Related

  1. 1

    square of a number using gaussian distribution

  2. 2

    Python: Creating a Gaussian distribution for a variable and running a program on a loop using the Gaussian values

  3. 3

    Python: Creating a Gaussian distribution for a variable and running a program on a loop using the Gaussian values

  4. 4

    Calculate moments (mean, variance) of distribution in python

  5. 5

    Generating 3D Gaussian distribution in Python

  6. 6

    How to fit a double Gaussian distribution in Python?

  7. 7

    Python - Integrating entire list with the Gaussian distribution

  8. 8

    Draw multivariate Gaussian distribution samples using Python numpy.random.randn

  9. 9

    Matlab Find the mean and variance of a Gaussian

  10. 10

    gaussian fitting not working using Python

  11. 11

    From Matlab to Python: N point with Gaussian Distribution inside a rectangle

  12. 12

    How to efficiently compute the heat map of two Gaussian distribution in Python?

  13. 13

    Fitting an image to Gaussian distribution

  14. 14

    Multiple Gaussian distribution

  15. 15

    Dealing with normal (Gaussian) distribution

  16. 16

    Separating gaussian components of a curve using python

  17. 17

    Strange behaviour with Gaussian random distribution

  18. 18

    Fail to generate gaussian distribution with seed

  19. 19

    Gaussian distribution for censored regression with cenreg

  20. 20

    Inverse of the cumulative gaussian distribution in R

  21. 21

    Multivariate Gaussian distribution formula implementation

  22. 22

    Incanter sample mean and variance not close to distribution mean and variance

  23. 23

    Incanter sample mean and variance not close to distribution mean and variance

  24. 24

    Sample mean and variance of a Weibull distribution sample

  25. 25

    empirical mean and variance plot in matlab with the normal distribution

  26. 26

    Gaussian distribution function different answer for program and math

  27. 27

    How to plot a Gaussian Distribution on y-axis?

  28. 28

    Apply a gaussian distribution in a specific part of an image

  29. 29

    Fitting bimodal gaussian distribution with some parameters fixed

HotTag

Archive