Skip to content

Instantly share code, notes, and snippets.

@inaz2
Created August 1, 2016 02:03
Show Gist options
  • Save inaz2/07b736fc57f10f57514168733e103c4d to your computer and use it in GitHub Desktop.
Save inaz2/07b736fc57f10f57514168733e103c4d to your computer and use it in GitHub Desktop.
母集団サイズが100000のときサンプル数400で信頼度95%で誤差±5%以内の支持率が得られることの確認
$ python test.py
population mean: 0.500000
rate of between ±5%: 0.962000
# -*- coding: utf-8 -*-
import numpy as np
N = 100000
n = 400
p = 0.5
num_trial = 10000
S = np.r_[np.ones(N * p), np.zeros(N * (1-p))]
print "population mean: %f" % np.mean(S)
results = []
for i in xrange(num_trial):
X = np.random.choice(S, 400)
results.append(np.mean(X))
results = np.array(results)
is_included = np.logical_and(p-0.05 <= results, results <= p+0.05)
print "rate of between ±5%%: %f" % np.mean(is_included)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment