-
Notifications
You must be signed in to change notification settings - Fork 0
/
15_6.py
43 lines (41 loc) · 1.67 KB
/
15_6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def makePlot(xVals, yVals, title, xLabel, yLabel, style, logX = False, logY = False):
pylab.figure()
pylab.title(title)
pylab.xlabel(xLabel)
pylab.ylabel(yLabel)
pylab.plot(xVals, yVals, style)
if logX:
pylab.semilogx()
if logY:
pylab.semilogy()
def runTrial(numFlips):
numHeads = 0
for n in range(numFlips):
if random.choice(('H', 'T')) == 'H':
numHeads += 1
numTails = numFlips - numHeads
return (numHeads, numTails)
def flipPlot1(minExp, maxExp, numTrials):
"""minExpとmaxExpは、minExp<maxExpを満たす正の整数
numTrialsは正の整数とする
2**minExpから2**maxExp回のコイン投げをnumTrials回
行った結果の要約をプロットする"""
ratiosMeans, diffsMeans, ratiosSDs, diffsSDs = [], [], [], []
xAxis = []
for exp in range(minExp, maxExp+1):
xAxis.append(2**exp)
for numFlips in xAxis:
ratios, diffs = [], []
for t in range(numTrials):
numHeads, numTails = runTrial(numFlips)
ratios.append(numHeads/numTails)
diffs.append(abs(numHeads-numTails))
ratiosMeans.append(sum(ratios)/numTrials)
diffsMeans.append(sum(diffs)/numTrials)
ratiosSDs.append(stdDev(ratios))
diffsSDs.append(stdDev(diffs))
numTrialsString = '(' + str(numTrials) + ' Trials)'
title = 'Mean Heads/Tails Ratios' + numTrialsString
makePlot(xAxis, ratiosMeans, title, 'Number of flips', 'Mean Head/Tails', 'ko', logX = True)
title = 'SD Heads/Tails Ratios' + numTrialsString
makePlot(xAxis, ratiosSDs, title, 'Number of Flips', 'Standard Deviation', 'ko', logX = True, logY = True)