Last active
February 7, 2021 05:56
-
-
Save zemse/d1aee88bba0612bb9c9e185659ba725b to your computer and use it in GitHub Desktop.
ISI Kolkata - Statistics Assignment 3 - Problem 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Question 9 | |
# | |
# Suppose X1 and X2 are IID from the discrete uniform distribution on | |
# {1, 2, ... , 5}. Derive the sampling distribution of (X1 + X2)/2. Verify | |
# your result by means of simulation. | |
from random import randint | |
# number of trials, more the trials accuracy increases | |
TRIALS = 10000000 | |
def sample(a, b): | |
return float(X1 + X2)/float(2) | |
# dictionary to record the frequency of sample result during each trial | |
count = {} | |
# executing trials | |
for _ in range(TRIALS): | |
# Since X1 and X2 are independent and identically distributed from | |
# the discrete uniform distribution on {1, 2, .., 5}. | |
X1 = randint(1, 5) | |
X2 = randint(1, 5) | |
result = sample(X1, X2) | |
# incrementing the count in the dictionary | |
if result not in count.keys(): | |
count[result] = 0 | |
count[result] = count[result] + 1 | |
# printing results | |
for key in sorted(count): | |
print "%s: %s" % (key, count[key]) | |
# results for 1,00,00,000 trials | |
# 1.0: 400795 | |
# 1.5: 801592 | |
# 2.0: 1199602 | |
# 2.5: 1599320 | |
# 3.0: 1997694 | |
# 3.5: 1601549 | |
# 4.0: 1198943 | |
# 4.5: 798757 | |
# 5.0: 401748 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment