Here is an experiment I ran to better understand pymc3. I have three biased coins and I perform the below experiment:
1. Toss Coin1. If head choose Coin2 else choose Coin3
2. Randomly choose a number n (between 1 and 10) that implies coin tosses to perform
3. Toss Coin2/3 (as per 1. above) n times and observe number of heads.
4. Perform 1,2,3 above multiple times
The ask is given data from above experiment, can we model the three coins ?
Code:
Data Generator:
coin1 = 0.4
coin2 = 0.9
coin3 = 0.1
numberOfDataPoints = 20000
maxTosses = 10
percentOfmaxTosses = 0.65
headTosses = []
totalTosses = []
for i in range(numberOfDataPoints):
heads = 0
total = 0
isCoint2 = random() < coin1
for rater in range(maxTosses):
if random() <= percentOfmaxTosses:
total = total + 1
if isCoint2:
if random() < coin2:
heads += 1
else:
if random() < coin3:
heads += 1
if total > 5:
headTosses.append(heads)
totalTosses.append(total)
Model Code:
with pm.Model() as coins:
coin1 = pm.Beta('coin1',2,2)
coin2 = pm.Beta('coin2',5,2)
coin3 = pm.Beta('coin3',2,5)
first_toss = pm.Bernoulli('first_toss', coin1)
p = pm.math.switch(first_toss > 0.5, coin2, coin3)
output = pm.Binomial('output', n=totalTosses, p=p,observed=headTosses)
exp = pm.sample(10000, tune = 5000)
Results:
* coin1 is found to be decently well modelled
* coin2 seems to be near to the original value
* coin3 is far off
Doubts:
- Is there anything wrong in the above experiment?
- Are there better ways to model this experiment?