# HG changeset patch # User Henry S. Thompson # Date 1627491336 -3600 # Node ID 7d6bc790b2137550bccfd5637d81b699746ac914 # Parent bb46148fab910a7dfa574e77ef7b0e0ba154e7e4 simulation of multi-segment coin flip with bias diff -r bb46148fab91 -r 7d6bc790b213 distr_3.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/distr_3.py Wed Jul 28 17:55:36 2021 +0100 @@ -0,0 +1,35 @@ +#!/usr/bin/python3 +'''Simulate stats for showing segments are the same as the whole''' +# Simulate normal distributions of coin flips with slightly +# unfair coins +# Usage: distr_3.py segsize flipcount nsegs max-bias +# Requires python3.6 as of 2021-07-28 +# Runs nsegs*segsize experiments, each involving flipcount flips. +# Experiments in a given segment have a bias around 0.5, +# randomly chosen between 0 and max-bias (which must be < 0.5 + +import sys, random, signif +from nltk import FreqDist + +n=int(sys.argv[1]) +m=int(sys.argv[2]) +ss=int(sys.argv[3]) +b=float(sys.argv[4]) + +whole=FreqDist() + +def fd(n,m,bias): + global whole + f=FreqDist() + split=random.uniform(0.5-bias,0.5+bias) + for i in range(n): + res=sum(int(random.uniform(0,1)>split) for j in range(m)) + f[res]+=1 + whole[res]+=1 + return f + +segs=[fd(n,m,b) for s in range(ss)] + +whole.bell() + +