Created
June 1, 2016 16:07
-
-
Save alexpearce/76d89d58e3ca31ab2556ed84c1d3b876 to your computer and use it in GitHub Desktop.
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
import numpy as np | |
import matplotlib.pyplot as plt | |
def normfactor(hist, edges): | |
a = np.sum(hist)*(edges[1:] - edges[:-1]) | |
return a | |
def gauss(xs, loc=0.0, scale=1.0): | |
norm = 1/(scale*np.sqrt(2*np.pi)) | |
return norm*np.exp(-np.square((xs - loc)/(np.sqrt(2)*scale))) | |
np.random.seed(123987) | |
def draw(loc, scale, color): | |
range = (-0, 10e4) | |
nbins = 150 | |
xs = np.random.normal(loc, scale, size=int(1e6)) | |
hist, edges = np.histogram(xs, bins=nbins, range=range) | |
hist = hist.astype(edges.dtype) | |
norm_factor = normfactor(hist, edges) | |
hist /= norm_factor | |
plt.errorbar((edges[:-1] + edges[1:])/2, hist, fmt='.', | |
color=color, alpha=0.5) | |
xs = np.linspace(range[0], range[1], 2*nbins) | |
ys = gauss(xs, loc, scale) | |
plt.plot(xs, ys, color=color, alpha=0.5, | |
label=r'$\mu = {0}$, $\sigma = {1}$'.format(loc, scale)) | |
# draw(0, 1, 'blue') | |
# draw(2, 0.5, 'red') | |
# draw(-3, 2, 'green') | |
draw(1e4, 1e3, 'blue') | |
plt.legend(loc='best') | |
plt.savefig('norm.pdf') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment