Created
January 20, 2016 20:52
-
-
Save yk-tanigawa/745ce49be3bcc774d67c to your computer and use it in GitHub Desktop.
Scipyで標本分布を計算する(離散分布) ref: http://qiita.com/yk-tanigawa/items/0e4bd9480216c7a84106
This file contains 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
from scipy import stats | |
help(stats.rv_discrete) |
This file contains 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
from scipy import stats | |
xk = np.arange(7) | |
pk = (0.1, 0.2, 0.3, 0.1, 0.1, 0.0, 0.2) | |
custm = stats.rv_discrete(name='custm', values=(xk, pk)) |
This file contains 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
import matplotlib.pyplot as plt | |
fig, ax = plt.subplots(1, 1) | |
ax.plot(xk, custm.pmf(xk), 'ro', ms=12, mec='r') | |
ax.vlines(xk, 0, custm.pmf(xk), colors='r', lw=4) | |
plt.show() |
This file contains 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
import numpy as np | |
# np.random.seed(0) | |
sampled_rvs = custm.rvs(size=1000) |
This file contains 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
f = np.histogram(sampled_rvs, bins = np.arange(7 + 1), density=True)[0] |
This file contains 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
import numpy as np | |
from scipy import stats | |
def sampling_dist(p, size, seed = 0): | |
xk = np.arange(p.size) | |
pk = p.ravel() | |
true_dist = stats.rv_discrete(name='true_dist', values=(xk, pk)) | |
np.random.seed(seed) | |
sampled_rvs = true_dist.rvs(size=size) | |
f = np.histogram(sampled_rvs, bins = np.arange(p.size + 1), density=True)[0] | |
f.reshape(p.shape) | |
return(f.reshape(p.shape)) | |
p = np.array([[0.10,0.10],[0.10,0.15],[0.15,0.10],[0.10,0.20]]) | |
p_est = sampling_dist(p, 1000) | |
print(p) | |
print(p_est) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment