Loading [MathJax]/extensions/tex2jax.js

2018/07/22

[Python3]データをビニングしてプロット。標準誤差も計算。

reference
https://mycourses.aalto.fi/pluginfile.php/146910/mod_resource/content/1/binning_tutorial.pdf

take binning data, and calculate standard errors for them.

import matplotlib.pyplot as pl, numpy as np
from scipy.stats import binned_statistic

x, y = np.linspace(0,100,num=10000), np.random.normal(loc=3, scale=2, size=10000)
nbin = 50
cols=['b']
bx, _, _ = binned_statistic(x, x, bins=nbin, statistic='mean')
# count data for each bin (それぞれのbinにあるデータ数)
bc, _, _ = binned_statistic(x, x, bins=nbin, statistic='count')
# take edge info for bin span (binの範囲を表示するためにedgeの情報を代入)
by, bed, bn = binned_statistic(x, y, bins=nbin, statistic='median') 
# standard deviations expecting to be ~2 (乱数の標準偏差は2なので、無論ここはほぼ2である)
berr, _, _ = binned_statistic(x, y, bins=nbin, statistic='std') 

bw = (bed[1] - bed[0])
bc = bed[1:] - bw/2
pl.plot(x, y, '.', alpha=0.05, c='k')
pl.hlines(by, bed[:-1], bed[1:], lw=1, colors=cols[0])
#show just stddev for errors
pl.errorbar(bx, by, yerr=berr, marker='', ls='', mfc=cols[0], mec=cols[0], ecolor='r', alpha=0.5)
# calculating standard-error by dividing n_bin**0.5
pl.errorbar(bx, by, yerr=berr/bc**0.5, marker='o', ls='', mfc=cols[0], mec=cols[0], ecolor=cols[0])
pl.show()
pl.close('all')

result(結果)

0 件のコメント: