Mercurial > hg > python
comparison distr_3.py @ 29:7d6bc790b213
simulation of multi-segment coin flip with bias
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Wed, 28 Jul 2021 17:55:36 +0100 |
parents | |
children | 9f40651f6080 |
comparison
equal
deleted
inserted
replaced
28:bb46148fab91 | 29:7d6bc790b213 |
---|---|
1 #!/usr/bin/python3 | |
2 '''Simulate stats for showing segments are the same as the whole''' | |
3 # Simulate normal distributions of coin flips with slightly | |
4 # unfair coins | |
5 # Usage: distr_3.py segsize flipcount nsegs max-bias | |
6 # Requires python3.6 as of 2021-07-28 | |
7 # Runs nsegs*segsize experiments, each involving flipcount flips. | |
8 # Experiments in a given segment have a bias around 0.5, | |
9 # randomly chosen between 0 and max-bias (which must be < 0.5 | |
10 | |
11 import sys, random, signif | |
12 from nltk import FreqDist | |
13 | |
14 n=int(sys.argv[1]) | |
15 m=int(sys.argv[2]) | |
16 ss=int(sys.argv[3]) | |
17 b=float(sys.argv[4]) | |
18 | |
19 whole=FreqDist() | |
20 | |
21 def fd(n,m,bias): | |
22 global whole | |
23 f=FreqDist() | |
24 split=random.uniform(0.5-bias,0.5+bias) | |
25 for i in range(n): | |
26 res=sum(int(random.uniform(0,1)>split) for j in range(m)) | |
27 f[res]+=1 | |
28 whole[res]+=1 | |
29 return f | |
30 | |
31 segs=[fd(n,m,b) for s in range(ss)] | |
32 | |
33 whole.bell() | |
34 | |
35 |