0

Is there a way to calculate the square of a number (closest approximation), say 4, using Gaussian distribution where mu is the number and sigma is 0.16. and for 1000 random points?

I searched the internet a lot, but couldn't find a solution to this. Any piece of code would be very much helpful as i am new to python.

Sandy.Arv
  • 607
  • 10
  • 34
  • What have you already tried? Exactly how are you trying to implement the guassian distribution? What do you expect the output to be? – MatsLindh Sep 30 '15 at 10:30
  • 1
    This is a very odd approach to estimate the square of a number... especially since you need the square in the first place to create your gaussian. Can you tell us more about the context? What do you need this for? – swenzel Sep 30 '15 at 10:31
  • Are you trying to implement a Monte Carlo integral surface estimator? – Reblochon Masque Sep 30 '15 at 10:32
  • Have you tried the `random.gauss()` function? – mhawke Sep 30 '15 at 10:37
  • Actually. I need to calculate the square of a number using Gaussian distribution. The main context is that, the square should be generated to the closest approximate figure. Since, 99% of a gaussian distribution would lie with the range of + or - 3 sigma. the 3sigma value given to me was 0.5. – Sandy.Arv Sep 30 '15 at 10:42
  • @Sandy.Arv Do you have your data points generated and you want to calculate an approximation of the n^2 or you want to generate the points and then you use them to find n^2? The second doesn't make any sense since in order to generate them you need n^2 which is your mean as you say. – sve Sep 30 '15 at 10:53
  • @svs ya I guess the first makes sense... So using the data points we can calculate the approximation of n^2. i was wrong..sorry.. is there a way to do it as u say?? – Sandy.Arv Sep 30 '15 at 11:43

2 Answers2

1

You can use numpy.random.randn to generate a standard Gaussian distribution, which can then be scaled as needed, from the docs,

For random samples from N(\mu, \sigma^2), use:

sigma * np.random.randn(...) + mu

which for your example,

import numpy as np
import matplotlib.pyplot as plt

N = 4.
mu = N**2
sigma = 1/N**2

dist = np.sqrt(sigma) * np.random.randn(1000) + mu
plt.hist(dist,30)
plt.show()

If you don't want to use numpy, you could also use random module,

import random

dist = [random.normalvariate(mu, sigma) for i in range(1000)]
Community
  • 1
  • 1
Ed Smith
  • 12,716
  • 2
  • 43
  • 55
1

Assuming that you have your data generated you could find an approximation of your mu (which is the square of your number) by taking the mean of your data. By the law of the large numbers you can be sure that as the size of your data grow the approximation become more accurate. Example:

import random


def generate_data(size):
    mu, sigma = 4 ** 2, 0.16
    return [random.gauss(mu, sigma) for _ in range(size)]


def mean(ls):
    return sum(ls) / len(ls)

print(mean(generate_data(10)))     #15.976644889526114
print(mean(generate_data(100)))    #16.004123848232233
print(mean(generate_data(1000)))   #16.00164187802018
print(mean(generate_data(10000)))  #16.001000022147206
sve
  • 4,336
  • 1
  • 19
  • 30
  • This is great.. its very simple and I was able understand this easily.. Thank u.. Could u also pls help me in generating a graph for this??? – Sandy.Arv Sep 30 '15 at 12:33
  • @Sandy.Arv sure but I think that it would be appropriate to make as a new question – sve Sep 30 '15 at 12:37