Created
August 1, 2016 02:03
-
-
Save inaz2/07b736fc57f10f57514168733e103c4d to your computer and use it in GitHub Desktop.
母集団サイズが100000のときサンプル数400で信頼度95%で誤差±5%以内の支持率が得られることの確認
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
$ python test.py | |
population mean: 0.500000 | |
rate of between ±5%: 0.962000 |
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
# -*- 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