# import stuff
import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import norm
%matplotlib inline
# fonts
from astroML.plotting import setup_text_plots
setup_text_plots(fontsize=8, usetex=True)
#------------------------------------------------------------
# Create our data
# generate 10000 normally distributed points
np.random.seed(1)
dist = norm(1, 0.25) # THIS IS WHERE WE SET THE ERROR!
flux = dist.rvs(10000)
flux_fit = np.linspace(0.001, 2, 1000)
pdf_flux_fit = dist.pdf(flux_fit)
# transform this distribution into magnitude space
mag = -2.5 * np.log10(flux)
mag_fit = -2.5 * np.log10(flux_fit)
pdf_mag_fit = pdf_flux_fit.copy()
pdf_mag_fit[1:] /= abs(mag_fit[1:] - mag_fit[:-1])
pdf_mag_fit /= np.dot(pdf_mag_fit[1:], abs(mag_fit[1:] - mag_fit[:-1]))
#------------------------------------------------------------
# Plot the result
fig, ax = plt.subplots(figsize=(10, 7.5))
# first plot the flux distribution
ax.hist(flux, bins=np.linspace(0, 2, 50),
histtype='stepfilled', fc='gray', alpha=0.5, normed=True)
ax.plot(flux_fit, pdf_flux_fit, '-k')
ax.plot([1, 1], [0, 2], ':k', lw=1)
ax.set_xlim(-0.1, 2.1)
ax.set_ylim(0, 1.8)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
ax.set_xlabel(r'${\rm flux}$',fontsize=20)
ax.set_ylabel(r'$p({\rm flux})$',fontsize=20)
ax.yaxis.set_major_locator(plt.MultipleLocator(0.4))
ax.text(0.04, 0.98, r'${\rm 20\%\ flux\ error}$',
ha='left', va='top', transform=ax.transAxes,
bbox=dict(ec='none', fc='w'),fontsize=20)
<matplotlib.text.Text at 0x10c37c150>
# next plot the magnitude distribution
fig, ax = plt.subplots(figsize=(10, 7.5))
ax.hist(mag, bins=np.linspace(-1, 2, 50),
histtype='stepfilled', fc='gray', alpha=0.5, normed=True)
ax.plot(mag_fit, pdf_mag_fit, '-k')
ax.plot([0, 0], [0, 2], ':k', lw=1)
ax.set_xlim(-1.1, 1.1)
ax.set_ylim(0, 1.8)
ax.yaxis.set_major_locator(plt.MultipleLocator(0.4))
ax.text(0.04, 0.98, r'${\rm mag} = -2.5\log_{10}({\rm flux})$',
ha='left', va='top', transform=ax.transAxes,
bbox=dict(ec='none', fc='w'), fontsize=20)
ax.set_xlabel(r'${\rm mag}$',fontsize=20)
ax.set_ylabel(r'$p({\rm mag})$',fontsize=20)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
(array([-0.4, 0. , 0.4, 0.8, 1.2, 1.6, 2. ]), <a list of 7 Text yticklabel objects>)
# importing
import numpy as np
from scipy import stats
from matplotlib import pyplot as plt
%matplotlib inline
# fonts
from astroML.plotting import setup_text_plots
setup_text_plots(fontsize=8, usetex=True)
# begin the plotting
fig, ax = plt.subplots(figsize=(10, 7.5))
# First show distributions with different skewness
x = np.linspace(-8, 8, 1000)
N = stats.norm(0, 1)
# The modified Gaussian is a normal distribution multiplied by a Gram–Charlier series (see eq. 4.70),
# with a0 = 2, a1 = 1, and a2 = 0.5. The log-normal has σ = 1.2.
# If anyone knows how to play with the skewness using these parameters, please let me know!
l1, = ax.plot(x, N.pdf(x), '-k',
label=r'${\rm Gaussian,}\ \Sigma=0$')
l2, = ax.plot(x, 0.5 * N.pdf(x) * (2 + x + 0.5 * (x * x - 1)),
'--k', label=r'${\rm mod.\ Gauss,}\ \Sigma=-0.36$')
l3, = ax.plot(x[499:], stats.lognorm(1.2).pdf(x[499:]), '-.k',
label=r'$\rm log\ normal,\ \Sigma=11.2$')
ax.set_xlim(-5, 5)
ax.set_ylim(0, 0.7001)
ax.set_ylabel('$p(x)$',fontsize=20)
ax.set_ylabel('x',fontsize=20)
# trick to show multiple legends
leg1 = ax.legend([l1], [l1.get_label()], loc=1,fontsize=16)
leg2 = ax.legend([l2, l3], (l2.get_label(), l3.get_label()), loc=2,fontsize=16)
ax.add_artist(leg1)
ax.set_title('Skew $\Sigma$',fontsize=20)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
(array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]), <a list of 9 Text yticklabel objects>)
# next show distributions with different kurtosis
fig, ax = plt.subplots(figsize=(10, 7.5))
x = np.linspace(-5, 5, 1000)
l1, = ax.plot(x, stats.laplace(0, 1).pdf(x), '--k',
label=r'${\rm Laplace,}\ K=+3$')
l2, = ax.plot(x, stats.norm(0, 1).pdf(x), '-k',
label=r'${\rm Gaussian,}\ K=0$')
l3, = ax.plot(x, stats.cosine(0, 1).pdf(x), '-.k',
label=r'${\rm Cosine,}\ K=-0.59$')
l4, = ax.plot(x, stats.uniform(-2, 4).pdf(x), ':k',
label=r'${\rm Uniform,}\ K=-1.2$')
ax.set_xlim(-5, 5)
ax.set_ylim(0, 0.55)
ax.set_xlabel('$x$',fontsize=20)
ax.set_ylabel('$p(x)$',fontsize=20)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
# trick to show multiple legends
leg1 = ax.legend((l1, l2), (l1.get_label(), l2.get_label()), loc=2,fontsize=16)
leg2 = ax.legend((l3, l4), (l3.get_label(), l4.get_label()), loc=1,fontsize=16)
ax.add_artist(leg1)
<matplotlib.legend.Legend at 0x10f5d7b90>
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
%matplotlib inline
fig,ax = plt.subplots(figsize=(10, 7.5))
plt.axis('off')
ax.text(0.35, 0.98, 'Error in Mean',
ha='left', va='top',
bbox=dict(ec='none', fc='w'), fontsize=30)
ax.text(0.4, 0.9, r'$\sigma_{\bar{x}} = \frac{s}{\sqrt{N}}$',
ha='left', va='top',
bbox=dict(ec='none', fc='w'), fontsize=40)
ax.text(0.15, 0.68, 'Error in Standard Deviation',
ha='left', va='top',
bbox=dict(ec='none', fc='w'), fontsize=30)
ax.text(0.15, 0.6, r'$\sigma_{s} = \frac{s}{\sqrt{2(N-1)}} = \frac{1}{\sqrt{2}}\sqrt{\frac{N}{N-1}}$',
ha='left', va='top',
bbox=dict(ec='none', fc='w'), fontsize=40)
ax.text(0.35, 0.28, 'Error in Median',
ha='left', va='top',
bbox=dict(ec='none', fc='w'), fontsize=30)
ax.text(0.35, 0.2, r'$\sigma_{q50} = s \sqrt{\frac{\pi}{2N}}$',
ha='left', va='top',
bbox=dict(ec='none', fc='w'), fontsize=40)
<matplotlib.text.Text at 0x10c798510>
# import
import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import norm
%matplotlib inline
# fonts
from astroML.plotting import setup_text_plots
setup_text_plots(fontsize=8, usetex=True)
#------------------------------------------------------------
# Generate the uniform samples
N = [2, 3, 10]
np.random.seed(42)
x = np.random.random((max(N), 1E6))
#------------------------------------------------------------
# Plot the results
fig = plt.figure(figsize=(10, 10))
fig.subplots_adjust(hspace=0.05)
for i in range(len(N)):
ax = fig.add_subplot(3, 1, i + 1)
# take the mean of the first N[i] samples
x_i = x[:N[i], :].mean(0)
# histogram the data
ax.hist(x_i, bins=np.linspace(0, 1, 101),
histtype='stepfilled', alpha=0.5, normed=True)
# plot the expected gaussian pdf
mu = 0.5
sigma = 1. / np.sqrt(12 * N[i])
dist = norm(mu, sigma)
x_pdf = np.linspace(-0.5, 1.5, 1000)
ax.plot(x_pdf, dist.pdf(x_pdf), '-k')
ax.set_xlim(0.0, 1.0)
ax.set_ylim(0.001, None)
ax.xaxis.set_major_locator(plt.MultipleLocator(0.2))
ax.yaxis.set_major_locator(plt.MaxNLocator(5))
ax.text(0.99, 0.95, r"$N = %i$" % N[i],
ha='right', va='top', transform=ax.transAxes,fontsize=20)
if i == len(N) - 1:
ax.xaxis.set_major_formatter(plt.FormatStrFormatter('%.4f'))
ax.set_xlabel(r'$x$',fontsize=20)
else:
ax.xaxis.set_major_formatter(plt.NullFormatter())
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
ax.set_ylabel('$p(x)$',fontsize=20)
#input
import numpy as np
from scipy.stats import cauchy
from matplotlib import pyplot as plt
%matplotlib inline
# fonts
from astroML.plotting import setup_text_plots
setup_text_plots(fontsize=8, usetex=True)
#------------------------------------------------------------
# Define the distribution parameters to be plotted
gamma_values = [0.5, 1.0, 2.0]
linestyles = ['-', '--', ':']
mu = 0
x = np.linspace(-10, 10, 1000)
#------------------------------------------------------------
# plot the distributions
fig, ax = plt.subplots(figsize=(10, 7.5))
for gamma, ls in zip(gamma_values, linestyles):
dist = cauchy(mu, gamma)
plt.plot(x, dist.pdf(x), ls=ls, color='black',
label=r'$\mu=%i,\ \gamma=%.1f$' % (mu, gamma))
plt.xlim(-4.5, 4.5)
plt.ylim(0, 0.65)
plt.xlabel('$x$',fontsize=20)
plt.ylabel(r'$p(x|\mu,\gamma)$',fontsize=20)
plt.title('Cauchy Distribution',fontsize=20)
plt.legend(fontsize=20)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
(array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]), <a list of 8 Text yticklabel objects>)
# import
import numpy as np
from matplotlib import pyplot as plt
from scipy.stats import norm
from scipy import stats
%matplotlib inline
# fonts
from astroML.plotting import setup_text_plots
setup_text_plots(fontsize=8, usetex=True)
#------------------------------------------------------------
# Generate the uniform samples
N = [2, 3, 10]
dist = stats.cauchy(0, 2.0) # mu = 0, gamma = 2
x = []
for i in range(max(N)):
x.append(dist.rvs(1e6))
x = np.array(x)
#x = dist.rvs(1e6) # ten random draws
#------------------------------------------------------------
# Plot the results
fig = plt.figure(figsize=(10, 10))
fig.subplots_adjust(hspace=0.05)
for i in range(len(N)):
ax = fig.add_subplot(3, 1, i + 1)
# take the mean of the first N[i] samples
x_i = x[:N[i], :].mean(0)
# histogram the data
ax.hist(x_i, bins=np.linspace(-10, 10, 251),
histtype='stepfilled', alpha=0.5, normed=True)
# plot the expected gaussian pdf
mu = 0.0
sigma = 1. / np.sqrt(12 * N[i])
dist = norm(mu, sigma)
x_pdf = np.linspace(-1.5, 1.5, 1000)
ax.plot(x_pdf, dist.pdf(x_pdf), '-k')
ax.set_xlim(-5.0, 5.0)
ax.set_ylim(0.001, None)
#ax.xaxis.set_major_locator(plt.MultipleLocator(0.2))
ax.yaxis.set_major_locator(plt.MaxNLocator(5))
ax.text(0.99, 0.95, r"$N = %i$" % N[i],
ha='right', va='top', transform=ax.transAxes,fontsize=20)
if i == len(N) - 1:
print 'hi'
else:
ax.xaxis.set_major_formatter(plt.NullFormatter())
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
ax.set_ylabel('$p(x)$',fontsize=20)
hi