Created
February 23, 2013 02:40
-
-
Save satomacoto/5018109 to your computer and use it in GitHub Desktop.
Score simulation with batting averages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding:utf-8 -*- | |
""" | |
# 打率列から得点のシミュレーション | |
- 9回 | |
- 3アウトでチェンジ | |
- すべて単打 | |
- 4回以上連続でヒットで得点 | |
""" | |
import numpy | |
def game(batting_averages): | |
score = 0 | |
order = 0 | |
for i in range(9): | |
base = 0 | |
out = 0 | |
while out < 3: | |
if numpy.random.random() < batting_averages[order]: | |
base += 1 | |
else: | |
out += 1 | |
order = (order + 1) // len(batting_averages) | |
if base > 3: | |
score = base - 3 | |
return score | |
# 打率列 | |
batting_averages = [0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3] | |
scores = [] | |
# シミュレーション | |
for i in range(10000): | |
scores.append(game(batting_averages)) | |
# ヒストグラム | |
print numpy.bincount(scores) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment