### Thread: How do you write a program for this in Python?

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
3
Rep Power
0

#### How do you write a program for this in Python?

I have no idea how to write in Python. Please help me with this! Best answer if you write the program!

Central Limit Theorem I
Goal
Write a python program that will use your accept / reject Monte Carlo algorithm to
demonstrate the central limit theorem.
Recall:
The central limit theorem states that if s = ∑j=1 N xj
(ie. s is a sum of M random variables x1, ..., xM), then the distribution of s approaches a Gaussian distribution as M increases.
Here the index j is being used to indicate different variables. Furthermore, the mean of
the resulting Gaussian is just the sum of means of each of the xj and the σ² of the
resulting Gaussian is the sum of σ² for each of the xj.
In general, there could be a different distribution for each of the xj. For instance, f(x1)
might be a Gaussian, while f(x2) might be our absolute |x2| function above, and f(x3)
might be something totally different. For this problem, we consider the case where all
the xj are sampled from the same distribution (the |x| distribution from above).
Your program should perform N Monte Carlo ‘experiments’. Each experiment will
produce one value for s, which will come from summing over M ‘measurements’ of x1, ..., xM.
Instructions:
1. form a loop of N experiments. For each experiment use acceptReject(M) to
produce M measurements of {xi} and calculate the sum s using “np.sum( ... )”.
2. plot a normalized histogram of the variable s
3. overlay a Gaussian distribution with a mean and standard deviation expected from
the central limit theorem for that value of M.
4. make plots for N=10,000 and M=1,2,4,8,16, 32

Hint: The standard deviation of f(x)=|x| for x in the range [-1,1] is √½:
σ² = ∫ f(x) (x-µ)² dx = 2 ∫₀¹ x (x-0)² dx = 1/2
2. Originally Posted by alexamicaa
I have no idea how to write in Python. Please help me with this! Best answer if you write the program!
And meanwhile, what you will be doing ???
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2013
Posts
3
Rep Power
0
Originally Posted by dariyoosh
And meanwhile, what you will be doing ???
... trying to figure it out myself. all i know how to do so far is pull up numpy...
4. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
31
Rep Power
2
Which language are you coming from?......just curious
5. Maybe you could plot histograms?

Try this with N=3

Code:
```import numpy
M = 99
print(numpy.random.random((N,M)).sum(axis=1))
# I hope you'll see three numbers in the array,
# showing that axis 1 corresponds to the M size axis.

N = 9999 # Now make a lot of these sums
MyArray = numpy.random.random((N,M)).sum(axis=1)

# plot histograms of MyArray
# and perform other statistical tests to convince yourself
# or dissuade you from the central limit theorem.

# Use

# numpy.info('histogram')```