-2

so basically I'm trying to run an stochastic experiment. It's very simple. Basically I wanted to see if what the central limit theorem says holds.

So simply put the idea of the central limit theorem is, that if we sample infinite samples of the same size from our stochastic experiment, the means of those samples are normally distributed.

So consider a dice. That dice is a magic dice and if you throw it, you can only get a 1,3,4 or 6. So you can't get a 2 and a 5. The probabilities are as follows:

P[1] = 2/6
P[3] = 1/6
P[4] = 1/6
P[6] = 2/6

Now if we take a sample size of 4, i.e. we throw the dice 4 times, write down what we got, take the mean and do that for lets say 100'000 times, we should, when plotted as an histogram, see a normal distribution.

I implemented that like this using python:

""" Set up the probability space """
experiment = [1,1,3,4,6,6]

    """ Experiment configuration """
    n = 4
    m = 100000
    bins = 20
    
    def throwDice():
        result = []
    
        for i in range(0,n):
            k = randrange(0,6)
            print(k)
            result.append(experiment[k])
    
        return result
    
    def sampleMeans():
        means = []
    
        for i in range(0,m):
            means.append(sum(throwDice())/4)
    
        return means
    
    def createHistrogram():
        means = sampleMeans()
    
        plt.hist(means, bins)
        plt.show()
    
    """ Run he experiment """
    createHistrogram()

which got me this

enter image description here

it's not surprising, that we have "holes" in e.g. 2.75 and 4.75 since we are missing 2 and 5 i.e. there are less possible samples which have a mean of 2.75 and 4.75. Same argument can be done for the others.

Now while everything looks good, my question is actually about the random generator of python. Is it fine to do it like this? What kind of random number generator would be best suited for such a simple "numerical experiment"?

3
  • First: You don't let the sample size go to infinity but the amount of samples. Second: I just assumed that if one generates the data for a stochastic numerical experiment using a RNG, one has to make sure it doesn't introduce a bias. I'm not talking about a bias inherent to a specific RNG. You pointed out correctly in another comment, that the CLT doesn't restrict the type of distribution we pull data from, so it's unlikely one runs into a problem here. I'm still new to stochastic and especially coding stochastic experiments so I wasn't sure if my data generation is valid.
    – handy
    Aug 4, 2020 at 8:24
  • Also: Yeah true the sample size is hardcoded - it was late. I actually wanted to make it variable but in the end, so that's a "bug" but it doesn't matter.
    – handy
    Aug 4, 2020 at 8:29
  • I want to add: If you increase the sampel size n, you get closer to the normal distribution. So it's a nicer fit - you make the error smaller but the limit in the theorem is about the number of such samples.
    – handy
    Aug 4, 2020 at 8:48

1 Answer 1

0

According to the documentation Python random, the random library uses Almost all module functions depend on the basic function random(), which generates a random float uniformly in the semi-open range [0.0, 1.0), so it's Uniform distribution.

Since most modules are pseudo-random, it is not good to confirm your experiment (because it is either follow or not follow the distribution). I think you should generate pure random number from random.org to test the result.

3
  • I think because if you use normal distribution, it is obvious that the experiment is valid (follows the CLT), and if you do not use normal distribution, it may not follow the CLT. Aug 3, 2020 at 13:25
  • So the concern AerysS brings up is: If you take samples from a normal distribution it's not surprising that you get a normal distribution (since we are using the same seed for all values). That was kind of my concern too but 1. I'm just using the RNG to pick a value out of my probability space i.e. my "picking" is normally distributed which doesn't imply (I think) that the values I pick are normally distributed 2. I think Paul Hanking does have a point here. I think using "true" random numbers like the ones from random.org is better anyway.
    – handy
    Aug 4, 2020 at 8:19
  • So the concern from AerysS is in general valid although I'd agree with Paul Hankin that it doesn't apply here. And discussing that was basically my point.
    – handy
    Aug 4, 2020 at 8:20

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.