Skip to content

Instantly share code, notes, and snippets.

@BigNerd
Created October 5, 2019 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigNerd/04aef94af57f72d4d94f13be3f7fde70 to your computer and use it in GitHub Desktop.
Save BigNerd/04aef94af57f72d4d94f13be3f7fde70 to your computer and use it in GitHub Desktop.
import math
import matplotlib.pyplot as plt
import numpy as np
import random
import scipy.stats as stats
die_e = 3.5 # expected value of an ordinary six-sided die
die_var = 17.5/6 # variance of an ordinary six-sided die
die_sigma = math.sqrt(die_var)
def create_statistics():
clt_sum_frequencies = np.zeros(601, dtype = int)
lln_averages = []
total_sum = 0
for repetition in range(0, 10000):
sum = 0
for cast in range(0, 100):
die_number = random.randint(1, 6)
sum = sum + die_number
clt_sum_frequencies[sum] += 1
total_sum += sum
lln_averages.append(total_sum / (100 * (repetition + 1)))
return clt_sum_frequencies / 10000, lln_averages
def plot_clt_chart(sum_frequencies, shift, scale):
sums = np.linspace(
(0 - shift) / scale,
(600 - shift) / scale,
len(sum_frequencies))
width = sums[1] - sums[0]
sum_densities = sum_frequencies / width
mu = 100 * die_e - shift
sigma = die_sigma * math.sqrt(100) / scale
normal_densities = [stats.norm.pdf(x, mu, sigma) for x in sums]
min_x = 280
max_x = 420
plot_chart(
sums[min_x:max_x],
sum_densities[min_x:max_x],
normal_densities[min_x:max_x],
width)
def plot_lln_chart(averages):
mu = die_e
indexes = np.arange(len(averages))
plt.ylim(3.48, 3.52)
plot_chart(indexes, averages, [mu for x in indexes])
def plot_chart(x_values, y_values, y_comparison_values, width=1.0):
plt.bar(x_values, y_values, width=width, align='center')
plt.plot(x_values, y_comparison_values, color='r')
plt.show()
clt_sum_frequencies, lln_averages = create_statistics()
plot_clt_chart(clt_sum_frequencies, 0, 1)
plot_lln_chart(lln_averages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment