diff --git a/header.tex b/header.tex index 4231e0c..080215b 100644 --- a/header.tex +++ b/header.tex @@ -216,21 +216,30 @@ %%%%% code/matlab commands: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \usepackage{textcomp} +% typeset code inline: \newcommand{\varcode}[1]{\setlength{\fboxsep}{0.5ex}\colorbox{codeback}{\texttt{#1\protect\rule[-0.1ex]{0pt}{1.6ex}}}} -\newcommand{\code}[2][]{\varcode{#2}\ifthenelse{\equal{#1}{}}{\protect\sindex[mcode]{#2}}{\protect\sindex[mcode]{#1}}} - +% type set code and add it to the python index: \newcommand{\pcode}[2][]{\varcode{#2}\ifthenelse{\equal{#1}{}}{\protect\sindex[pcode]{#2}}{\protect\sindex[pcode]{#1}}} +% type set code and add it to the matlab index: \newcommand{\mcode}[2][]{\varcode{#2}\ifthenelse{\equal{#1}{}}{\protect\sindex[mcode]{#2}}{\protect\sindex[mcode]{#1}}} +% XXX typeset code and put it into matlab index: +% THIS SHOULD actually take both the matlab and the python code! +\newcommand{\code}[2][]{\varcode{#2}\ifthenelse{\equal{#1}{}}{\protect\sindex[mcode]{#2}}{\protect\sindex[mcode]{#1}}} + +% the name of the python language: \newcommand{\python}{Python} +% the name of the matlab language: \newcommand{\matlab}{\texorpdfstring{MATLAB$^{\copyright}$}{MATLAB}} -\newcommand{\pythonfun}[2][]{(\tr{\python-function}{\python-Funktion} \setlength{\fboxsep}{0.5ex}\colorbox{codeback}{\texttt{#2}})\ifthenelse{\equal{#1}{}}{\protect\sindex[pcode]{#2}}{\protect\sindex[pcode]{#1}}} +% typeset '(python-function #1)' and add the function to the python index: +\newcommand{\pythonfun}[1]{(\tr{\python-function}{\python-Funktion} \varcode{#1})\protect\sindex[pcode]{#1}} -\newcommand{\matlabfun}[2][]{(\tr{\matlab-function}{\matlab-Funktion} \setlength{\fboxsep}{0.5ex}\colorbox{codeback}{\texttt{#2}})\ifthenelse{\equal{#1}{}}{\protect\sindex[mcode]{#2}}{\protect\sindex[mcode]{#1}}} +% typeset '(matlab-function #1)' and add the function to the matlab index: +\newcommand{\matlabfun}[1]{(\tr{\matlab-function}{\matlab-Funktion} \varcode{#1})\protect\sindex[mcode]{#1}} %%%%% shortquote and widequote commands: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/statistics/lecture/displayunivariatedata.py b/statistics/lecture/displayunivariatedata.py index ac62b88..546d4ff 100644 --- a/statistics/lecture/displayunivariatedata.py +++ b/statistics/lecture/displayunivariatedata.py @@ -1,54 +1,111 @@ import numpy as np import matplotlib.pyplot as plt -rng = np.random.RandomState(981) -x = rng.randn(40, 10) + 4.0 +#rng = np.random.RandomState(981) +#data = rng.randn(40, 1) + 4.0 +rng = np.random.RandomState(1981) +data = rng.gamma(1.0, 1.5, 40) + 1.0 +data = data[data<7.5] +xpos = 0.08 +ypos = 0.15 +width = 0.65 +height = 0.8 plt.xkcd() fig = plt.figure( figsize=(6,3.4) ) -ax = fig.add_subplot(1, 1, 1) + +ax = fig.add_axes([xpos, ypos, width, height]) 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_xlabel('Experiment') +ax.set_xticklabels([]) +ax.set_xlim(0.0, 4.8) ax.set_ylabel('x') ax.set_ylim( 0.0, 8.0) -ax.scatter(0.5+rng.rand(len(x[:, 5])), x[:, 5], s=50) -ax.bar([2.0], [np.mean(x[:, 5])], 1.0, yerr=[np.std(x[:, 5])], - ecolor='k', capsize=0, error_kw={'elinewidth':5}) - -## ax.annotate('Median', -## xy=(3.9, 0.0), xycoords='data', -## xytext=(3.5, -2.7), textcoords='data', ha='right', -## arrowprops=dict(arrowstyle="->", relpos=(0.8,1.0), -## connectionstyle="angle3,angleA=-110,angleB=60") ) -## ax.annotate('1. quartile', -## xy=(5.8, -0.9), xycoords='data', -## xytext=(5.5, -3.4), textcoords='data', ha='right', -## arrowprops=dict(arrowstyle="->", relpos=(0.9,1.0), -## connectionstyle="angle3,angleA=30,angleB=70") ) -## ax.annotate('3. quartile', -## xy=(6.1, 1.1), xycoords='data', -## xytext=(6.5, 3.0), textcoords='data', ha='left', -## arrowprops=dict(arrowstyle="->", relpos=(0.0,0.0), -## connectionstyle="angle3,angleA=30,angleB=70") ) -## ax.annotate('minimum', -## xy=(6.1, -1.9), xycoords='data', -## xytext=(7.2, -3.3), textcoords='data', ha='left', -## arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5), -## connectionstyle="angle3,angleA=10,angleB=100") ) -## ax.annotate('maximum', -## xy=(5.9, 2.7), xycoords='data', -## xytext=(4.9, 3.5), textcoords='data', ha='right', -## arrowprops=dict(arrowstyle="->", relpos=(1.0,0.5), -## connectionstyle="angle3,angleA=0,angleB=120") ) -#ax.boxplot( x[:, 5], positions=[4.0], whis=100.0 ) -#ax.boxplot( x[:, 5], positions=[4.0], widths=[1.0], whis=100.0, manage_xticks=False ) -ax.boxplot( x[:, 5], positions=[4.0], widths=[1.0], whis=100.0 ) -ax.set_xlim(0.0, 5.0) -ax.set_xticks([1, 3, 5], ['a', 'b', 'c']) -plt.tight_layout() +barwidth = 0.8 + +scatterpos = 1.0 +barpos = 2.5 +boxpos = 4.0 + +ax.set_xticks([scatterpos, barpos, boxpos]) +ax.set_xticklabels(['(1) data', '(2) bar\n plot', '(3) box-\nwhisker']) + +ax.scatter(scatterpos-0.5*barwidth+rng.rand(len(data)), data, s=50) + +barmean = np.mean(data) +barstd = np.std(data) +ew = 0.2 +ax.bar([barpos-0.5*barwidth], [barmean], barwidth, color='#FFCC00') +eargs = {'color': 'k', 'lw': 2} +ax.plot([barpos, barpos], [barmean-barstd, barmean+barstd], **eargs) +ax.plot([barpos-0.5*ew, barpos+0.5*ew], [barmean-barstd, barmean-barstd], **eargs) +ax.plot([barpos-0.5*ew, barpos+0.5*ew], [barmean+barstd, barmean+barstd], **eargs) +ax.annotate('mean', + xy=(barpos-0.4*barwidth, 2.7), xycoords='data', + xytext=(barpos-1*barwidth, 5.5), textcoords='data', ha='left', + arrowprops=dict(arrowstyle="->", relpos=(1.0,0.5), + connectionstyle="angle3,angleA=0,angleB=120") ) +ax.annotate('mean plus\nstd. dev.', + xy=(barpos+0.05*barwidth, 4.2), xycoords='data', + xytext=(barpos-1*barwidth, 7.0), textcoords='data', ha='left', + arrowprops=dict(arrowstyle="->", relpos=(0.5,0.0), + connectionstyle="angle3,angleA=-60,angleB=80") ) + +ax = fig.add_axes([xpos, ypos, width, height], axis_bgcolor='none') +ax.spines['right'].set_visible(False) +ax.spines['top'].set_visible(False) +ax.spines['left'].set_visible(False) +ax.spines['bottom'].set_visible(False) +ax.xaxis.set_ticks_position('none') +ax.yaxis.set_ticks_position('none') +ax.set_xticklabels([]) +ax.set_yticklabels([]) +wh = ax.boxplot( data, positions=[boxpos], widths=[barwidth], whis=100.0, patch_artist=True) +wh['medians'][0].set_linewidth(4) +wh['whiskers'][0].set_linewidth(2) +wh['whiskers'][1].set_linewidth(2) +wh['whiskers'][0].set_linestyle('-') +wh['whiskers'][1].set_linestyle('-') +whiskercolor = 'k' +wh['whiskers'][0].set_color(whiskercolor) +wh['whiskers'][1].set_color(whiskercolor) +wh['caps'][0].set_color(whiskercolor) +wh['caps'][1].set_color(whiskercolor) +wh['boxes'][0].set_facecolor('#99ff00') +ax.set_xlim(0.0, 4.8) +ax.set_ylim( 0.0, 8.0) +ax.annotate('maximum', + xy=(boxpos, 6.5), xycoords='data', + xytext=(boxpos-1*barwidth, 7.6), textcoords='data', ha='left', + arrowprops=dict(arrowstyle="->", relpos=(1.0,0.5), + connectionstyle="angle3,angleA=0,angleB=120") ) +ax.annotate('3. quartile', + xy=(boxpos-0.3*barwidth, 3.7), xycoords='data', + xytext=(boxpos-1.3*barwidth, 5.5), textcoords='data', ha='left', + arrowprops=dict(arrowstyle="->", relpos=(0.4,0.0), + connectionstyle="angle3,angleA=0,angleB=120") ) +ax.annotate('median', + xy=(boxpos+0.6*barwidth, 2.2), xycoords='data', + xytext=(boxpos+0.1*barwidth, 4.2), textcoords='data', ha='left', + arrowprops=dict(arrowstyle="->", relpos=(0.8,0.0), + connectionstyle="angle3,angleA=-60,angleB=20") ) + +ax = fig.add_axes([xpos+width+0.03, ypos, 0.98-(xpos+width+0.03), height], axis_bgcolor='none') +ax.spines['right'].set_visible(False) +ax.spines['top'].set_visible(False) +ax.xaxis.set_ticks_position('bottom') +ax.yaxis.set_ticks_position('left') +ax.set_yticklabels([]) +ax.set_ylim( 0.0, 8.0) +ax.set_xticks(np.arange(0.0, 0.4, 0.1)) +ax.set_xlabel('(4) p(x)') +bw = 0.75 +bins = np.arange(0, 8.0+bw, bw) +h, b = np.histogram(data, bins) +ax.barh(b[:-1], h/bw/np.sum(h), bw, color='#CC0000') + plt.savefig('displayunivariatedata.pdf') #plt.show() diff --git a/statistics/lecture/median.py b/statistics/lecture/median.py index 9bc8bef..8204688 100644 --- a/statistics/lecture/median.py +++ b/statistics/lecture/median.py @@ -7,7 +7,7 @@ x = np.arange( -3.0, 3.0, 0.01 ) g = np.exp(-0.5*x*x)/np.sqrt(2.0*np.pi) plt.xkcd() -fig = plt.figure( figsize=(6,3) ) +fig = plt.figure( figsize=(6, 2.8) ) ax = fig.add_subplot(1, 2, 1) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) @@ -72,6 +72,7 @@ ax.plot(x, g, 'b', lw=4) ax.plot([m, m], [0.0, 0.38], 'k', lw=2 ) #ax.plot([gm, gm], [0.0, 0.42], 'k', lw=2 ) -plt.tight_layout() +#plt.tight_layout() +plt.subplots_adjust(left=0.1, right=0.98, bottom=0.15, top=0.98, wspace=0.4, hspace=0.0) fig.savefig( 'median.pdf' ) #plt.show() diff --git a/statistics/lecture/pdfprobabilities.py b/statistics/lecture/pdfprobabilities.py index 26c8e22..10c4482 100644 --- a/statistics/lecture/pdfprobabilities.py +++ b/statistics/lecture/pdfprobabilities.py @@ -30,6 +30,7 @@ ax.annotate('$P(0x1)&(xx1)&(x