Mercurial > hg > python
annotate distr_3.py @ 60:bc1acb1416ab
working on fixing gnus home foulup, see /disk/scratch/{mail,gnus}
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Wed, 13 Dec 2023 17:31:28 +0000 |
parents | 1252f8100c6a |
children |
rev | line source |
---|---|
29
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
1 #!/usr/bin/python3 |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
2 '''Simulate stats for showing segments are the same as the whole''' |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
3 # Simulate normal distributions of coin flips with slightly |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
4 # unfair coins |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
5 # Usage: distr_3.py segsize flipcount nsegs max-bias |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
6 # Requires python3.6 as of 2021-07-28 |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
7 # Runs nsegs*segsize experiments, each involving flipcount flips. |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
8 # Experiments in a given segment have a bias around 0.5, |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
9 # randomly chosen between 0 and max-bias (which must be < 0.5 |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
10 |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
11 import sys, random, signif |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
12 from nltk import FreqDist |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
13 |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
14 n=int(sys.argv[1]) |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
15 m=int(sys.argv[2]) |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
16 ss=int(sys.argv[3]) |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
17 b=float(sys.argv[4]) |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
18 |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
19 whole=FreqDist() |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
20 |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
21 def fd(n,m,bias): |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
22 global whole |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
23 f=FreqDist() |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
24 split=random.uniform(0.5-bias,0.5+bias) |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
25 for i in range(n): |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
26 res=sum(int(random.uniform(0,1)>split) for j in range(m)) |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
27 f[res]+=1 |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
28 whole[res]+=1 |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
29 return f |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
30 |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
31 segs=[fd(n,m,b) for s in range(ss)] |
31
1252f8100c6a
printouts to aid further testing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
30
diff
changeset
|
32 |
1252f8100c6a
printouts to aid further testing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
30
diff
changeset
|
33 print(whole[1],whole.mean(), whole.sd(),sep='\t') |
1252f8100c6a
printouts to aid further testing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
30
diff
changeset
|
34 print() |
1252f8100c6a
printouts to aid further testing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
30
diff
changeset
|
35 |
30
9f40651f6080
works, but not quite right for cc simulation
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
29
diff
changeset
|
36 for s in segs: |
31
1252f8100c6a
printouts to aid further testing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
30
diff
changeset
|
37 print(s[1],s.mean(), s.sd(),sep='\t') |
1252f8100c6a
printouts to aid further testing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
30
diff
changeset
|
38 |
1252f8100c6a
printouts to aid further testing
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
30
diff
changeset
|
39 exit() |
29
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
40 |
30
9f40651f6080
works, but not quite right for cc simulation
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
29
diff
changeset
|
41 whole.bell(block=False) |
9f40651f6080
works, but not quite right for cc simulation
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
29
diff
changeset
|
42 segs[4].bell() |
29
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
43 |
7d6bc790b213
simulation of multi-segment coin flip with bias
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff
changeset
|
44 |