0% found this document useful (0 votes)
22 views2 pages

Qu 1 (DA Assignment)

This document provides instructions for visualizing the central limit theorem using Python. It involves: 1) Simulating 1000 trials of rolling a die multiple times and calculating the average for each trial. 2) Plotting a histogram of the averages from each trial and animating it to show the distribution converging to a normal distribution as more trials are added. 3) Defining an animation function that plots the histogram, updates the title with the current number of trials, and stops the animation after 1000 trials.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Qu 1 (DA Assignment)

This document provides instructions for visualizing the central limit theorem using Python. It involves: 1) Simulating 1000 trials of rolling a die multiple times and calculating the average for each trial. 2) Plotting a histogram of the averages from each trial and animating it to show the distribution converging to a normal distribution as more trials are added. 3) Defining an animation function that plots the histogram, updates the title with the current number of trials, and stops the animation after 1000 trials.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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