In [9]:
import numpy as np
In [10]:
# The two distributions are a uniform distribution with probability 11/12,
# and a binomial distribution with n=10, p=1/12, with probability 1/12.
# We can use the composition method to split these up and sample them
# independently.
def composition(n=1000):
res = []
for i in range(n):
if np.random.rand() < 1/12:
u = np.random.rand(10)
x = np.sum(u < 1/12)
else:
x = np.floor(np.random.rand()*11)
res.append(x)
return(res)
cp = composition()
print(np.mean(cp))
4.696