updated plots of the data analysis chapters

This commit is contained in:
2020-01-14 23:38:16 +01:00
parent 75aa46d71c
commit 9769d5e94f
18 changed files with 226 additions and 272 deletions

View File

@@ -1,24 +1,22 @@
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.gridspec as gridspec
from plotstyle import *
plt.xkcd()
fig = plt.figure( figsize=(6,6.8) )
rng = np.random.RandomState(4637281)
lmarg=0.1
rmarg=0.1
ax = fig.add_axes([lmarg, 0.75, 1.0-rmarg, 0.25])
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.xaxis.set_ticks_position('bottom')
ax.get_yaxis().set_visible(False)
fig = plt.figure(figsize=cm_size(figure_width, 2.8*figure_height))
spec = gridspec.GridSpec(nrows=4, ncols=1, height_ratios=[4, 4, 1, 3], hspace=0.2,
**adjust_fs(fig, left=4.0))
ax = fig.add_subplot(spec[0, 0])
ax.set_xlim(0.0, np.pi)
ax.set_xticks(np.arange(0.125*np.pi, 1.*np.pi, 0.125*np.pi))
ax.set_xticklabels([])
ax.set_ylim(0.0, 3.5)
ax.yaxis.set_major_locator(plt.NullLocator())
ax.text(-0.2, 0.5*3.5, 'Activity', rotation='vertical', va='center')
ax.annotate('Tuning curve',
xy=(0.42*np.pi, 2.5), xycoords='data',
@@ -31,55 +29,49 @@ ax.annotate('',
arrowprops=dict(arrowstyle="->", relpos=(0.5,0.5),
connectionstyle="angle3,angleA=80,angleB=90") )
ax.text(0.52*np.pi, 0.7, 'preferred\norientation')
ax.plot([0, 0], [0.0, 3.5], 'k', zorder=10, clip_on=False)
xx = np.arange(0.0, 2.0*np.pi, 0.01)
pp = 0.5*np.pi
yy = np.exp(np.cos(2.0*(xx+pp)))
ax.fill_between(xx, yy+0.25*yy, yy-0.25*yy, color=cm.autumn(0.3, 1), alpha=0.5)
ax.plot(xx, yy, color=cm.autumn(0.0, 1))
ax.fill_between(xx, yy+0.25*yy, yy-0.25*yy, **fsBa)
ax.plot(xx, yy, **lsB)
ax = fig.add_axes([lmarg, 0.34, 1.0-rmarg, 0.38])
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.xaxis.set_ticks_position('bottom')
ax.get_yaxis().set_visible(False)
ax = fig.add_subplot(spec[1, 0])
ax.set_xlim(0.0, np.pi)
ax.set_xticks(np.arange(0.125*np.pi, 1.*np.pi, 0.125*np.pi))
ax.set_xticklabels([])
ax.set_ylim(-1.5, 3.0)
ax.text(0.5*np.pi, -1.8, 'Orientation', ha='center')
ax.set_ylim(0.0, 3.0)
ax.yaxis.set_major_locator(plt.NullLocator())
ax.text(-0.2, 0.5*3.5, 'Activity', rotation='vertical', va='center')
ax.plot([0, 0], [0.0, 3.0], 'k', zorder=10, clip_on=False)
xx = np.arange(0.0, 1.0*np.pi, 0.01)
prefphases = np.arange(0.125*np.pi, 1.*np.pi, 0.125*np.pi)
responses = []
xresponse = 0.475*np.pi
for pp in prefphases :
for pp, ls, ps in zip(prefphases, [lsE, lsC, lsD, lsB, lsD, lsC, lsE],
[psE, psC, psD, psB, psD, psC, psE]) :
yy = np.exp(np.cos(2.0*(xx+pp)))
ax.plot(xx, yy, color=cm.autumn(2.0*np.abs(pp/np.pi-0.5), 1))
#ax.plot(xx, yy, color=cm.autumn(2.0*np.abs(pp/np.pi-0.5), 1))
ax.plot(xx, yy, **ls)
y = np.exp(np.cos(2.0*(xresponse+pp)))
responses.append(y + rng.randn()*0.25*y)
ax.plot(xresponse, y, '.', markersize=20, color=cm.autumn(2.0*np.abs(pp/np.pi-0.5), 1))
r=0.3
y=-0.8
ax.plot([pp-0.5*r*np.cos(pp), pp+0.5*r*np.cos(pp)], [y-r*np.sin(pp), y+r*np.sin(pp)], 'k', lw=6)
ax.plot(xresponse, y, **ps)
responses = np.array(responses)
ax = fig.add_axes([lmarg, 0.05, 1.0-rmarg, 0.22])
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.xaxis.set_ticks_position('bottom')
ax.get_yaxis().set_visible(False)
ax = fig.add_subplot(spec[2, 0])
ax.show_spines('')
r = 0.3
ax.set_ylim(-1.1*r, 1.1*r)
for pp in prefphases:
ax.plot([pp-0.5*r*np.cos(pp), pp+0.5*r*np.cos(pp)], [-r*np.sin(pp), r*np.sin(pp)],
colors['black'], lw=6, clip_on=False)
ax = fig.add_subplot(spec[3, 0])
ax.set_xlim(0.0, np.pi)
ax.set_xticks(np.arange(0.125*np.pi, 1.*np.pi, 0.125*np.pi))
ax.set_xticklabels([])
ax.set_ylim(-1600, 0)
ax.yaxis.set_major_locator(plt.NullLocator())
ax.set_xlabel('Orientation')
ax.text(-0.2, -800, 'Log-Likelihood', rotation='vertical', va='center')
ax.plot([0, 0], [-1600, 0], 'k', zorder=10, clip_on=False)
phases = np.linspace(0.0, 1.1*np.pi, 100)
probs = np.zeros((len(responses), len(phases)))
for k, (pp, r) in enumerate(zip(prefphases, responses)) :
@@ -95,7 +87,6 @@ ax.annotate('',
arrowprops=dict(arrowstyle="->", relpos=(0.5,0.5),
connectionstyle="angle3,angleA=80,angleB=90") )
ax.text(maxp+0.05, -1100, 'most likely\norientation\ngiven the responses')
ax.plot(phases, loglikelihood, '-b')
ax.plot(phases, loglikelihood, **lsA)
plt.savefig('mlecoding.pdf')
#plt.show();

View File

@@ -1,8 +1,11 @@
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from plotstyle import *
plt.xkcd()
fig = plt.figure( figsize=(6,5) )
fig = plt.figure(figsize=cm_size(figure_width, 1.8*figure_height))
spec = gridspec.GridSpec(nrows=2, ncols=2, hspace=0.6,
**adjust_fs(fig, left=5.5))
# the data:
n = 40
@@ -11,21 +14,17 @@ sigma = 0.5
rmu = 2.0
xd = rng.randn(n)*sigma+rmu
# and possible pdfs:
x = np.arange( 0.0, 4.0, 0.01 )
x = np.arange(0.0, 4.0, 0.01)
mus = [1.5, 2.0, 2.5]
g=np.zeros((len(x), len(mus)))
for k, mu in enumerate(mus) :
g[:,k] = np.exp(-0.5*((x-mu)/sigma)**2.0)/np.sqrt(2.0*np.pi)/sigma
# plot it:
ax = fig.add_subplot( 2, 1, 1 )
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax = fig.add_subplot(spec[0, :])
ax.set_xlim(0.5, 3.5)
ax.set_ylim(-0.02, 0.85)
ax.set_xticks( np.arange(0, 5))
ax.set_yticks( np.arange(0, 0.9, 0.2))
ax.set_xticks(np.arange(0, 5))
ax.set_yticks(np.arange(0, 0.9, 0.2))
ax.set_xlabel('x')
ax.set_ylabel('Probability density')
s = 1
@@ -36,14 +35,14 @@ for mu in mus :
ax.annotate('', xy=(mu, 0.02), xycoords='data',
xytext=(mu, 0.75), textcoords='data',
arrowprops=dict(arrowstyle="->", relpos=(0.5,0.5),
connectionstyle=cs), zorder=1 )
connectionstyle=cs), zorder=1)
if mu > rmu :
ax.text(mu-0.1, 0.04, '?', zorder=1, ha='right')
else :
ax.text(mu+0.1, 0.04, '?', zorder=1)
for k in range(len(mus)) :
ax.plot(x, g[:,k], zorder=5)
ax.scatter(xd, 0.05*rng.rand(len(xd))+0.2, s=30, zorder=10)
for k, ls in enumerate([lsCm, lsBm, lsDm]) :
ax.plot(x, g[:,k], zorder=5, **ls)
ax.plot(xd, 0.05*rng.rand(len(xd))+0.2, zorder=10, **psAm)
# likelihood:
thetas=np.arange(1.5, 2.6, 0.01)
@@ -52,48 +51,38 @@ for i, theta in enumerate(thetas) :
ps[:,i]=np.exp(-0.5*((xd-theta)/sigma)**2.0)/np.sqrt(2.0*np.pi)/sigma
p=np.prod(ps,axis=0)
# plot it:
ax = fig.add_subplot( 2, 2, 3 )
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax = fig.add_subplot(spec[1, 0])
ax.set_xlabel(r'Parameter $\theta$')
ax.set_ylabel('Likelihood')
ax.set_xticks( np.arange(1.6, 2.5, 0.4))
ax.set_xticks(np.arange(1.6, 2.5, 0.4))
ax.annotate('Maximum',
xy=(2.0, 5.5e-11), xycoords='data',
xytext=(1.0, 1.1), textcoords='axes fraction', ha='right',
arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5),
connectionstyle="angle3,angleA=10,angleB=70") )
connectionstyle="angle3,angleA=10,angleB=70"))
ax.annotate('',
xy=(2.0, 0), xycoords='data',
xytext=(2.0, 5e-11), textcoords='data',
arrowprops=dict(arrowstyle="->", relpos=(0.5,0.5),
connectionstyle="angle3,angleA=90,angleB=80") )
ax.plot(thetas,p)
connectionstyle="angle3,angleA=90,angleB=80"))
ax.plot(thetas, p, **lsAm)
ax = fig.add_subplot( 2, 2, 4 )
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax = fig.add_subplot(spec[1, 1])
ax.set_xlabel(r'Parameter $\theta$')
ax.set_ylabel('Log-Likelihood')
ax.set_ylim(-50,-20)
ax.set_xticks( np.arange(1.6, 2.5, 0.4))
ax.set_yticks( np.arange(-50, -19, 10.0))
ax.set_xticks(np.arange(1.6, 2.5, 0.4))
ax.set_yticks(np.arange(-50, -19, 10.0))
ax.annotate('Maximum',
xy=(2.0, -23), xycoords='data',
xytext=(1.0, 1.1), textcoords='axes fraction', ha='right',
arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5),
connectionstyle="angle3,angleA=10,angleB=70") )
connectionstyle="angle3,angleA=10,angleB=70"))
ax.annotate('',
xy=(2.0, -50), xycoords='data',
xytext=(2.0, -26), textcoords='data',
arrowprops=dict(arrowstyle="->", relpos=(0.5,0.5),
connectionstyle="angle3,angleA=80,angleB=100") )
ax.plot(thetas,np.log(p))
connectionstyle="angle3,angleA=80,angleB=100"))
ax.plot(thetas,np.log(p), **lsAm)
plt.tight_layout();
plt.savefig('mlemean.pdf')
#plt.show();

View File

@@ -2,9 +2,10 @@ import numpy as np
import scipy.stats as st
import scipy.optimize as opt
import matplotlib.pyplot as plt
from plotstyle import *
plt.xkcd()
fig = plt.figure( figsize=(6,3) )
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.subplots_adjust(**adjust_fs(fig, right=1.0))
# the data:
n = 100
@@ -23,27 +24,23 @@ a = st.gamma.fit(xd, 5.0)
yf = st.gamma.pdf(xx, *a)
# plot it:
ax = fig.add_subplot( 1, 2, 1 )
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax.set_xlim(0, 10.0)
ax.set_ylim(0.0, 0.42)
ax.set_xticks( np.arange(0, 11, 2))
ax.set_yticks( np.arange(0, 0.42, 0.1))
ax.set_xlabel('x')
ax.set_ylabel('Probability density')
ax.plot(xx, yy, '-', lw=5, color='#ff0000', label='pdf')
ax.plot(xx, yf, '-', lw=2, color='#ffcc00', label='mle')
ax1.set_xlim(0, 10.0)
ax1.set_ylim(0.0, 0.42)
ax1.set_xticks(np.arange(0, 11, 2))
ax1.set_yticks(np.arange(0, 0.42, 0.1))
ax1.set_xlabel('x')
ax1.set_ylabel('Probability density')
ax1.plot(xx, yy, label='pdf', **lsB)
ax1.plot(xx, yf, label='mle', **lsCm)
kernel = st.gaussian_kde(xd)
x = kernel(xd)
x /= np.max(x)
ax.scatter(xd, 0.05*x*(rng.rand(len(xd))-0.5)+0.05, s=30, zorder=10)
ax.legend(loc='upper right', frameon=False)
sigma = 0.07
ax1.plot(xd, sigma*x*(rng.rand(len(xd))-0.5)+sigma, zorder=10, **psAm)
ax1.legend(loc='upper right')
# histogram:
h,b = np.histogram(xd, np.arange(0, 8.5, 1), density=True)
h,b = np.histogram(xd, np.arange(0, 8.4, 0.5), density=True)
# fit histogram:
def gammapdf(x, n, l, s) :
@@ -52,22 +49,15 @@ popt, pcov = opt.curve_fit(gammapdf, b[:-1]+0.5*(b[1]-b[0]), h)
yc = st.gamma.pdf(xx, *popt)
# plot it:
ax = fig.add_subplot( 1, 2, 2 )
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
ax.set_xlim(0, 10.0)
ax.set_xticks( np.arange(0, 11, 2))
ax.set_xlabel('x')
ax.set_ylim(0.0, 0.42)
ax.set_yticks( np.arange(0, 0.42, 0.1))
ax.set_ylabel('Probability density')
ax.plot(xx, yy, '-', lw=5, color='#ff0000', label='pdf')
ax.plot(xx, yc, '-', lw=2, color='#ffcc00', label='fit')
ax.bar(b[:-1], h, np.diff(b))
ax.legend(loc='upper right', frameon=False)
ax2.set_xlim(0, 10.0)
ax2.set_xticks(np.arange(0, 11, 2))
ax2.set_xlabel('x')
ax2.set_ylim(0.0, 0.42)
ax2.set_yticks(np.arange(0, 0.42, 0.1))
ax2.set_ylabel('Probability density')
ax2.plot(xx, yy, label='pdf', **lsB)
ax2.plot(xx, yc, label='fit', **lsCm)
ax2.bar(b[:-1], h, np.diff(b), **fsA)
ax2.legend(loc='upper right')
plt.tight_layout();
plt.savefig('mlepdf.pdf')
#plt.show();

View File

@@ -1,9 +1,14 @@
import numpy as np
import scipy.stats as st
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from plotstyle import *
plt.xkcd()
fig = plt.figure(figsize=(6, 3))
fig = plt.figure()
spec = gridspec.GridSpec(nrows=1, ncols=2, wspace=0.3,
**adjust_fs(fig, left=5.5))
spec1 = gridspec.GridSpecFromSubplotSpec(1, 2, spec[0, 0], width_ratios=[3, 1], wspace=0.0)
spec2 = gridspec.GridSpecFromSubplotSpec(1, 2, spec[0, 1], width_ratios=[3, 1], wspace=0.0)
# the line:
slope = 2.0
@@ -20,71 +25,44 @@ 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 = fig.add_subplot(spec1[0, 0])
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.plot(x, y, label='data', zorder=10, **psAm)
ax.plot(xx, yy, label='original', zorder=5, **lsB)
ax.plot(xx, yf, label='fit', zorder=7, **lsCm)
ax.legend(loc='upper left', bbox_to_anchor=(0.0, 1.15))
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 = fig.add_subplot(spec1[0, 1])
ax.show_spines('l')
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.hist(y, bins, orientation='horizontal', zorder=10, **fsA)
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 = fig.add_subplot(spec2[0, 0])
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.plot(x, y - slopef*x, label='residuals', zorder=10, **psAm)
#ax.legend(loc='upper left', bbox_to_anchor=(0.0, 1.0))
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 = fig.add_subplot(spec2[0, 1])
ax.show_spines('l')
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)
ax.hist(r, bins, orientation='horizontal', zorder=10, **fsA)
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)
ax.plot(1.0+gy*29.0, gx, zorder=5, **lsBm)
plt.savefig('mlepropline.pdf')
#plt.show();