Skip to content

Instantly share code, notes, and snippets.

@yk-tanigawa
Created January 20, 2016 20:52
Show Gist options
  • Save yk-tanigawa/745ce49be3bcc774d67c to your computer and use it in GitHub Desktop.
Save yk-tanigawa/745ce49be3bcc774d67c to your computer and use it in GitHub Desktop.
Scipyで標本分布を計算する(離散分布) ref: http://qiita.com/yk-tanigawa/items/0e4bd9480216c7a84106
from scipy import stats
help(stats.rv_discrete)
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))
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()
import numpy as np
# np.random.seed(0)
sampled_rvs = custm.rvs(size=1000)
f = np.histogram(sampled_rvs, bins = np.arange(7 + 1), density=True)[0]
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