In [31]:
%matplotlib notebook
import numpy as np
import seaborn as sns
sns.set_context("paper")
In [33]:
# Define info for die population
die = np.arange(6)+1
die_dist = np.array([1/len(values)]*len(values))
In [22]:
# Expected value
sum([v*p for v, p in zip(die, die_dist)])
Out[22]:
In [11]:
# Simulate N die rolls
num_rolls = 10000
rolls_res = np.random.choice(die, num_rolls, p=die_dist)
In [32]:
sns.plt.plot(np.arange(1, num_rolls), [rolls_res[:i].mean() for i in range(1, num_rolls)])
sns.plt.xscale('log')
sns.plt.show()
In [35]:
sns.barplot(die, die_dist)
sns.plt.show()
In [70]:
np.random.choice(die, (5, 3), p=die_dist)
Out[70]:
In [76]:
num_rolls = 1000
num_dice = [1, 2, 3, 4, 5]
fig, axes = sns.plt.subplots(len(num_dice))
fig.tight_layout()
for i, num in enumerate(num_dice):
rolls_res = np.random.choice(die, (num_rolls, num), p=die_dist).sum(axis=1)
sns.distplot(rolls_res, ax=axes[i])
#axes[i].set_xticklabels(axes[i].xaxis.get_majorticklabels(), rotation=30)
#axes[i].set_xlabel(name)
sns.plt.show()
In [77]:
# Define info for die population
die = np.arange(6)+1
die_dist = np.array([1/len(values)]*len(values))
In [83]:
rolls_res = np.random.choice(die, (10000, 5), p=die_dist).sum(axis=1)
Out[83]:
In [86]:
vals, counts = np.unique(rolls_res, return_counts=True)
num_vals = len(vals)
total_count = counts.sum()
In [88]:
sns.barplot(vals, counts/total_count)
sns.plt.show()
In [92]:
sns.plt.plot(vals, counts/total_count)
sns.plt.show()
In [103]:
data = counts/total_count
p = np.arange(26) / float(26)
sns.plt.plot(vals, np.cumsum(data))
sns.plt.show()
In [ ]: