## 2018年6月8日金曜日

### Python - 数学 - 集合と確率を操作する - 大数の法則(確率変数、期待値、平均値)

Pythonからはじめる数学入門 (Amit Saha (著)、黒川 利明 (翻訳)、オライリージャパン)の5章(集合と確率を操作する)、5.4(プログラミングチャレンジ)、問題5-2(大数の法則)を取り組んでみる。

コード(Emacs)

Python 3

```#!/usr/bin/env python3
import random

def f(n):
return sum([random.randrange(1, 7) for _ in range(n)]) / n

if __name__ == '__main__':
e = [i * 1 / 6 for i in range(1, 7)]
print(f'Expeted value: {sum(e)}')
for _ in range(5):
for n in [10 ** 2, 10 ** 3, 10 ** 4, 10 ** 5, 5 * 10 ** 5]:
print(f'Trials: {n} Trial average {f(n)}')
print()
```

```\$ ./sample2.py
Expeted value: 3.5
Trials: 100 Trial average 3.5
Trials: 1000 Trial average 3.493
Trials: 10000 Trial average 3.518
Trials: 100000 Trial average 3.50077
Trials: 500000 Trial average 3.49947

Trials: 100 Trial average 3.36
Trials: 1000 Trial average 3.559
Trials: 10000 Trial average 3.486
Trials: 100000 Trial average 3.49783
Trials: 500000 Trial average 3.500806

Trials: 100 Trial average 3.66
Trials: 1000 Trial average 3.486
Trials: 10000 Trial average 3.4934
Trials: 100000 Trial average 3.49794
Trials: 500000 Trial average 3.503908

Trials: 100 Trial average 3.49
Trials: 1000 Trial average 3.515
Trials: 10000 Trial average 3.4934
Trials: 100000 Trial average 3.49752
Trials: 500000 Trial average 3.50299

Trials: 100 Trial average 3.5
Trials: 1000 Trial average 3.442
Trials: 10000 Trial average 3.478
Trials: 100000 Trial average 3.49932
Trials: 500000 Trial average 3.501682

\$
```