You are on page 1of 2

Data Analytics Assignment 1

1. Write a python program to visualise the Central limit theorem. (Use case example: Rolling
a die,
Use numpy and matplotlib)
Hint : Import necessary libraries, create 1000 simulations of 10 die rolls, and in each
simulation,
find the average of the die outcome. Write a function to plot a histogram of the above
generated
Values. Animate the histogram generation for a better visualisation.

1. Import necessary libraries - numpy, matplotlib and wand


In [11]: import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from wand.image import Image
from wand.display import display
%matplotlib notebook

2. Create 1000 simulations of die rolls


In [12]:# 1000 simulations of die roll
n = 1000
# In each simulation, there is one trial more than the previous simulation
avg = []
for i in range(2,n):
a = np.random.randint(1,7,i)
avg.append(np.average(a))
In [13]:# sample 10 expected value of die rolls
avg[1:10]
Out[13]:
[5.0,
2.25,
2.0,
3.3333333333333335,
3.4285714285714284,
3.125,
2.4444444444444446,
3.0,
3.090909090909091]

3. Function to plot histogram and animation


In [14]:
# Function that will plot the histogram, where current is the latest figure
def clt(current):
# if animation is at the last frame, stop it
plt.cla()
if current == 1000:
a.event_source.stop()

plt.hist(avg[0:current])

plt.gca().set_title('Expected value of die rolls')


plt.gca().set_xlabel('Average from die roll')
plt.gca().set_ylabel('Frequency')

plt.annotate('Die roll = {}'.format(current), [3,27])

In [15]:
fig = plt.figure()
a = animation.FuncAnimation(fig, clt, interval=1)

You might also like