91 lines
2.8 KiB
Python
91 lines
2.8 KiB
Python
import numpy as np
|
|
import scipy.stats as st
|
|
import matplotlib.pyplot as plt
|
|
|
|
plt.xkcd()
|
|
fig = plt.figure(figsize=(6, 3))
|
|
|
|
# the line:
|
|
slope = 2.0
|
|
xx = np.arange(0.0, 4.1, 0.1)
|
|
yy = slope*xx
|
|
# the data:
|
|
n = 40
|
|
rng = np.random.RandomState(5218)
|
|
sigma = 1.5
|
|
x = 4.0*rng.rand(n)
|
|
y = slope*x+rng.randn(n)*sigma
|
|
# fit:
|
|
slopef = np.sum(x*y)/np.sum(x*x)
|
|
yf = slopef*xx
|
|
|
|
# plot it:
|
|
ax = fig.add_axes([0.09, 0.02, 0.33, 0.9])
|
|
ax.spines['left'].set_position('zero')
|
|
ax.spines['bottom'].set_position('zero')
|
|
ax.spines['right'].set_visible(False)
|
|
ax.spines['top'].set_visible(False)
|
|
ax.get_xaxis().set_tick_params(direction='inout', length=10, width=2)
|
|
ax.get_yaxis().set_tick_params(direction='inout', length=10, width=2)
|
|
ax.yaxis.set_ticks_position('left')
|
|
ax.xaxis.set_ticks_position('bottom')
|
|
ax.set_xticks(np.arange(0.0, 4.1))
|
|
ax.set_xlim(0.0, 4.2)
|
|
ax.set_ylim(-4.0, 12.0)
|
|
ax.set_xlabel('x')
|
|
ax.set_ylabel('y')
|
|
ax.scatter(x, y, label='data', s=40, zorder=10)
|
|
ax.plot(xx, yy, 'r', lw=5.0, color='#ff0000', label='original', zorder=5)
|
|
ax.plot(xx, yf, '--', lw=1.0, color='#ffcc00', label='fit', zorder=7)
|
|
ax.legend(loc='upper left', bbox_to_anchor=(0.0, 1.15), frameon=False)
|
|
|
|
ax = fig.add_axes([0.42, 0.02, 0.07, 0.9])
|
|
ax.spines['left'].set_position('zero')
|
|
ax.spines['right'].set_visible(False)
|
|
ax.spines['top'].set_visible(False)
|
|
ax.spines['bottom'].set_visible(False)
|
|
ax.get_yaxis().set_tick_params(direction='inout', length=10, width=2)
|
|
ax.yaxis.set_ticks_position('left')
|
|
ax.set_xticks([])
|
|
ax.set_ylim(-4.0, 12.0)
|
|
ax.set_yticks([])
|
|
bins = np.arange(-4.0, 12.1, 0.75)
|
|
ax.hist(y, bins, orientation='horizontal', zorder=10)
|
|
|
|
ax = fig.add_axes([0.6, 0.02, 0.33, 0.9])
|
|
ax.spines['left'].set_position('zero')
|
|
ax.spines['bottom'].set_position('zero')
|
|
ax.spines['right'].set_visible(False)
|
|
ax.spines['top'].set_visible(False)
|
|
ax.get_xaxis().set_tick_params(direction='inout', length=10, width=2)
|
|
ax.get_yaxis().set_tick_params(direction='inout', length=10, width=2)
|
|
ax.yaxis.set_ticks_position('left')
|
|
ax.xaxis.set_ticks_position('bottom')
|
|
ax.set_xticks(np.arange(0.0, 4.1))
|
|
ax.set_xlim(0.0, 4.2)
|
|
ax.set_ylim(-4.0, 12.0)
|
|
ax.set_xlabel('x')
|
|
ax.set_ylabel('y - mx')
|
|
ax.scatter(x, y - slopef*x, label='residuals', s=40, zorder=10)
|
|
#ax.legend(loc='upper left', bbox_to_anchor=(0.0, 1.0), frameon=False)
|
|
|
|
ax = fig.add_axes([0.93, 0.02, 0.07, 0.9])
|
|
ax.spines['left'].set_position('zero')
|
|
ax.spines['right'].set_visible(False)
|
|
ax.spines['top'].set_visible(False)
|
|
ax.spines['bottom'].set_visible(False)
|
|
ax.get_yaxis().set_tick_params(direction='inout', length=10, width=2)
|
|
ax.yaxis.set_ticks_position('left')
|
|
ax.set_xlim(0.0, 11.0)
|
|
ax.set_xticks([])
|
|
ax.set_ylim(-4.0, 12.0)
|
|
ax.set_yticks([])
|
|
r = y - slopef*x
|
|
ax.hist(r, bins, orientation='horizontal', zorder=10)
|
|
gx = np.arange(-4.0, 12.1, 0.1)
|
|
gy = st.norm.pdf(gx, np.mean(r), np.std(r))
|
|
ax.plot(1.0+gy*29.0, gx, 'r', lw=2, zorder=5)
|
|
|
|
plt.savefig('mlepropline.pdf')
|
|
#plt.show();
|