Worked on statistics chapter
This commit is contained in:
parent
f44ae51c68
commit
cb7924ffd5
@ -166,11 +166,6 @@
|
||||
\newcommand{\matlab}{MATLAB}
|
||||
\newcommand{\matlabfun}[1]{(\tr{\matlab{}-function}{\matlab-Funktion} \setlength{\fboxsep}{0.5ex}\colorbox{blue!10}{\texttt{#1}})}
|
||||
|
||||
%%%%% definition environment: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage{ifthen}
|
||||
\newenvironment{definition}[1][]{\medskip\noindent\textbf{Definition}\ifthenelse{\equal{#1}{}}{}{ #1}:\newline}%
|
||||
{\medskip}
|
||||
|
||||
%%%%% exercises environment: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% usage:
|
||||
%
|
||||
@ -186,6 +181,7 @@
|
||||
% the listing
|
||||
%
|
||||
% Innerhalb der exercise Umgebung ist enumerate umdefiniert, um (a), (b), (c), .. zu erzeugen.
|
||||
\usepackage{ifthen}
|
||||
\usepackage{framed}
|
||||
\newcounter{maxexercise}
|
||||
\setcounter{maxexercise}{10000} % show listings up to exercise maxexercise
|
||||
|
@ -1,10 +1,10 @@
|
||||
% check whether the median returned by mymedian
|
||||
% really separates a vector into two halfs
|
||||
for i = 1:140 % loop over different length
|
||||
for k = 1:10 % try several times
|
||||
a = randn( i, 1 ); % generate some data
|
||||
m = mymedian( a ); % compute median
|
||||
if length( a(a>m) ) ~= length( a(a<m) ) % check
|
||||
for i = 1:140 % loop over different length
|
||||
for k = 1:10 % try several times
|
||||
a = randn( i, 1 ); % generate some data
|
||||
m = mymedian( a ); % compute median
|
||||
if length( a(a>m) ) ~= length( a(a<m) ) % check
|
||||
disp( 'error!' )
|
||||
end
|
||||
end
|
||||
|
@ -9,7 +9,7 @@ bins = np.arange(0.5, 7, 1.0)
|
||||
|
||||
plt.xkcd()
|
||||
|
||||
fig = plt.figure( figsize=(6,4) )
|
||||
fig = plt.figure( figsize=(6,3) )
|
||||
ax = fig.add_subplot( 1, 2, 1 )
|
||||
ax.spines['right'].set_visible(False)
|
||||
ax.spines['top'].set_visible(False)
|
||||
|
@ -1,32 +1,77 @@
|
||||
import numpy as np
|
||||
import scipy.stats as st
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# normal distribution:
|
||||
x = np.arange( -4.0, 4.0, 0.01 )
|
||||
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,4) )
|
||||
ax = fig.add_subplot( 1, 1, 1 )
|
||||
fig = plt.figure( figsize=(6,3) )
|
||||
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_xlabel( 'x' )
|
||||
ax.set_ylabel( 'Probability density p(x)' )
|
||||
ax.set_ylabel( 'Prob. density p(x)' )
|
||||
ax.set_ylim( 0.0, 0.46 )
|
||||
ax.set_yticks( np.arange( 0.0, 0.45, 0.1 ) )
|
||||
ax.text(-1.0, 0.1, '50%', ha='center' )
|
||||
ax.text(+1.0, 0.1, '50%', ha='center' )
|
||||
ax.annotate('Median',
|
||||
ax.text(-1.0, 0.06, '50%', ha='center' )
|
||||
ax.text(+1.0, 0.06, '50%', ha='center' )
|
||||
ax.annotate('Median\n= mean',
|
||||
xy=(0.1, 0.3), xycoords='data',
|
||||
xytext=(1.6, 0.35), textcoords='data', ha='left',
|
||||
arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5),
|
||||
xytext=(1.2, 0.35), textcoords='data', ha='left',
|
||||
arrowprops=dict(arrowstyle="->", relpos=(0.0,0.2),
|
||||
connectionstyle="angle3,angleA=10,angleB=40") )
|
||||
ax.annotate('Mode',
|
||||
xy=(-0.1, 0.4), xycoords='data',
|
||||
xytext=(-2.5, 0.43), textcoords='data', ha='left',
|
||||
arrowprops=dict(arrowstyle="->", relpos=(0.0,0.2),
|
||||
connectionstyle="angle3,angleA=10,angleB=120") )
|
||||
ax.fill_between( x[x<0], 0.0, g[x<0], color='#ffcc00' )
|
||||
ax.fill_between( x[x>0], 0.0, g[x>0], color='#99ff00' )
|
||||
ax.plot(x,g, 'b', lw=4)
|
||||
ax.plot(x, g, 'b', lw=4)
|
||||
ax.plot([0.0, 0.0], [0.0, 0.45], 'k', lw=2 )
|
||||
|
||||
# normal distribution:
|
||||
x = np.arange( 0.0, 6.0, 0.01 )
|
||||
shape = 2.0
|
||||
g = st.gamma.pdf(x, shape)
|
||||
m = st.gamma.median(shape)
|
||||
gm = st.gamma.mean(shape)
|
||||
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_xlabel( 'x' )
|
||||
ax.set_ylabel( 'Prob. density p(x)' )
|
||||
ax.set_ylim( 0.0, 0.46 )
|
||||
ax.set_yticks( np.arange( 0.0, 0.45, 0.1 ) )
|
||||
ax.text(m-0.8, 0.06, '50%', ha='center' )
|
||||
ax.text(m+1.2, 0.06, '50%', ha='center' )
|
||||
ax.annotate('Median',
|
||||
xy=(m+0.1, 0.2), xycoords='data',
|
||||
xytext=(m+1.6, 0.25), textcoords='data', ha='left',
|
||||
arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5),
|
||||
connectionstyle="angle3,angleA=30,angleB=70") )
|
||||
ax.annotate('Mean',
|
||||
xy=(gm, 0.01), xycoords='data',
|
||||
xytext=(gm+1.8, 0.15), textcoords='data', ha='left',
|
||||
arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5),
|
||||
connectionstyle="angle3,angleA=0,angleB=90") )
|
||||
ax.annotate('Mode',
|
||||
xy=(1.0, 0.38), xycoords='data',
|
||||
xytext=(1.8, 0.42), textcoords='data', ha='left',
|
||||
arrowprops=dict(arrowstyle="->", relpos=(0.0,0.5),
|
||||
connectionstyle="angle3,angleA=0,angleB=70") )
|
||||
ax.fill_between( x[x<m], 0.0, g[x<m], color='#ffcc00' )
|
||||
ax.fill_between( x[x>m], 0.0, g[x>m], color='#99ff00' )
|
||||
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()
|
||||
fig.savefig( 'median.pdf' )
|
||||
#plt.show()
|
||||
|
@ -9,7 +9,7 @@ r = rng.randn( 100 )
|
||||
|
||||
plt.xkcd()
|
||||
|
||||
fig = plt.figure( figsize=(6,4) )
|
||||
fig = plt.figure( figsize=(6,3) )
|
||||
ax = fig.add_subplot( 1, 2, 1 )
|
||||
ax.spines['right'].set_visible(False)
|
||||
ax.spines['top'].set_visible(False)
|
||||
@ -31,7 +31,7 @@ ax.xaxis.set_ticks_position('bottom')
|
||||
ax.set_xlabel( 'x' )
|
||||
ax.set_xlim(-3.2, 3.2)
|
||||
ax.set_xticks( np.arange( -3.0, 3.1, 1.0 ) )
|
||||
ax.set_ylabel( 'Probability density p(x)' )
|
||||
ax.set_ylabel( 'Probab. density p(x)' )
|
||||
ax.set_ylim(0.0, 0.44)
|
||||
ax.set_yticks( np.arange( 0.0, 0.41, 0.1 ) )
|
||||
ax.plot(x, g, '-b', lw=2, zorder=-1)
|
||||
|
@ -33,24 +33,30 @@
|
||||
\end{itemize}
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\section{\tr{Median, quartile, etc.}{Median, Quartil, etc.}}
|
||||
\section{\tr{Mode, median, quartile, etc.}{Modus, Median, Quartil, etc.}}
|
||||
|
||||
\begin{figure}[t]
|
||||
\includegraphics[width=1\textwidth]{median}
|
||||
\caption{\label{medianfig} Median.}
|
||||
\caption{\label{medianfig} Median, Mittelwert und Modus einer
|
||||
Wahrscheinlichkeitsverteilung. Links: Bei der symmetrischen,
|
||||
unimodalen Normalverteilung sind Median, Mittelwert und Modus
|
||||
identisch. Rechts: bei unsymmetrischen Verteilungen sind die drei
|
||||
Gr\"o{\ss}en nicht mehr identisch. Der Mittelwert wird am
|
||||
st\"arksten von einem starken Schw\"anz der Verteilung
|
||||
herausgezogen. Der Median ist dagegen robuster, aber trotzdem
|
||||
nicht unbedingt identsich mit dem Modus.}
|
||||
\end{figure}
|
||||
|
||||
\begin{definition}[\tr{median}{Median}]
|
||||
\tr{Half of the observations $X=(x_1, x_2, \ldots, x_n)$ are
|
||||
larger than the median and half of them are smaller than the
|
||||
median.} {Der Median teilt eine Liste von Messwerten so in zwei
|
||||
H\"alften, dass die eine H\"alfte der Daten nicht gr\"o{\ss}er
|
||||
und die andere H\"alfte nicht kleiner als der Median ist.}
|
||||
\end{definition}
|
||||
Der Modus ist der h\"aufigste Wert, d.h. die Position des Maximums
|
||||
einer Wahrscheinlichkeitsverteilung.
|
||||
|
||||
Der Median teilt eine Liste von Messwerten so in zwei H\"alften, dass
|
||||
die eine H\"alfte der Daten nicht gr\"o{\ss}er und die andere H\"alfte
|
||||
nicht kleiner als der Median ist (\figref{medianfig}).
|
||||
|
||||
\begin{exercise}{mymedian.m}{}
|
||||
\tr{Write a function that computes the median of a vector.}
|
||||
{Schreibe eine Funktion, die den Median eines Vektors zur\"uckgibt.}
|
||||
\tr{Write a function \code{mymedian} that computes the median of a vector.}
|
||||
{Schreibe eine Funktion \code{mymedian}, die den Median eines Vektors zur\"uckgibt.}
|
||||
\end{exercise}
|
||||
|
||||
\matlab{} stellt die Funktion \code{median()} zur Berechnung des Medians bereit.
|
||||
@ -65,6 +71,12 @@
|
||||
insbesondere verschieden lange Datenvektoren testen.}
|
||||
\end{exercise}
|
||||
|
||||
Eine Wahrscheinlichkeitsverteilung kann weiter durch die Position
|
||||
ihrere Quartile charakterisiert werden. Zwischen den Quartilen liegen
|
||||
jeweils 25\,\% der Daten (\figref{quartilefig}). Perzentile erlauben
|
||||
eine feinere Einteilung. Das 3. Quartil ist das 75. Perzentil, da
|
||||
75\,\% der Daten unterhalb des 3. Quartils liegen.
|
||||
|
||||
\begin{figure}[t]
|
||||
\includegraphics[width=1\textwidth]{quartile}
|
||||
\caption{\label{quartilefig} Median und Quartile einer Normalverteilung.}
|
||||
@ -81,6 +93,30 @@
|
||||
% {Schreibe eine Funktion, die das erste, zweite und dritte Quartil als Vektor zur\"uckgibt.}
|
||||
% \end{exercise}
|
||||
|
||||
\begin{figure}[t]
|
||||
\includegraphics[width=1\textwidth]{boxwhisker}
|
||||
\caption{\label{boxwhiskerfig} Box-whisker plots illustrate distributions.}
|
||||
\end{figure}
|
||||
|
||||
Box-Whisker Plots sind eine h\"aufig verwendete Darstellung um die
|
||||
Verteilung unimodaler Daten zu visualisieren und vergleichbar zu
|
||||
machen mit anderen Daten. Dabei wird um den Median eine Box vom 1. zum
|
||||
3. Quartil gezeichnet. Die Whiskers deuten den minimalen und den
|
||||
maximalen Datenwert an (\figref{boxwhiskerfig}).
|
||||
|
||||
\begin{exercise}{boxwhisker.m}{}
|
||||
\tr{Generate eine $40 \times 10$ matrix of random numbers and
|
||||
illustrate their distribution in a box-whicker plot
|
||||
(\code{boxplot()} function). How to interpret the plot?}
|
||||
{Erzeuge ein $40 \times 10$ Matrix
|
||||
von Zufallszahlen und illustriere ihre Verteilungen in einem
|
||||
Box-Whisker Plot (\code{boxplot()} Funktion, lies die Hilfe!). Wie ist der
|
||||
Box-Whisker Plot zu interpretieren? Was hat es mit den Ausreissern auf sich?
|
||||
Wie kann man erreichen, dass die Whisker den kleinsten und den gr\"o{\ss}ten
|
||||
Datenwert anzeigen? Warum sind die unterschiedlichen Box-Whiskers nicht alle gleich,
|
||||
obwohl sie aus der selben Verteilung gezogen worden sind?}
|
||||
\end{exercise}
|
||||
|
||||
\section{\tr{Histogram}{Histogramm}}
|
||||
|
||||
Histogramme z\"ahlen die H\"aufigkeit $n_i$ des Auftretens von
|
||||
@ -115,18 +151,34 @@ Wahrscheinlichkeitsverteilung der Messwerte abzusch\"atzen.
|
||||
mit der theoretischen Verteilung $P=1/6$ vergleichbar.}}
|
||||
\end{figure}
|
||||
|
||||
Bei ganzzahligen Messdaten (z.B. die Augenzahl eines W\"urfels)
|
||||
kann f\"ur jede auftretende Zahl eine Klasse definiert werden.
|
||||
Damit die H\"ohe der Histogrammbalken unabh\"angig von der Anzahl der Messwerte wird,
|
||||
normiert man das Histogram auf die Anzahl der Messwerte.
|
||||
Die H\"ohe der Histogrammbalken gibt dann die Wahrscheinlichkeit $P(x_i)$
|
||||
des Auftretens der Gr\"o{\ss}e $x_i$ in der $i$-ten Klasse an
|
||||
Bei ganzzahligen Messdaten (z.B. die Augenzahl eines W\"urfels oder
|
||||
die Anzahl von Aktionspotentialen in einem bestimmten Zeitfenster)
|
||||
kann f\"ur jede auftretende Zahl eine Klasse definiert werden. Damit
|
||||
die H\"ohe der Histogrammbalken unabh\"angig von der Anzahl der
|
||||
Messwerte wird, normiert man das Histogram auf die Anzahl der
|
||||
Messwerte (\figref{diehistogramsfig}). Die H\"ohe der
|
||||
Histogrammbalken gibt dann die Wahrscheinlichkeit $P(x_i)$ des
|
||||
Auftretens der Gr\"o{\ss}e $x_i$ in der $i$-ten Klasse an
|
||||
\[ P_i = \frac{n_i}{N} = \frac{n_i}{\sum_{i=1}^M n_i} \; . \]
|
||||
|
||||
|
||||
\section{\tr{Probability density function}{Wahrscheinlichkeitsdichte}}
|
||||
|
||||
Meistens haben wir es jedoch mit reellen Messgr\"o{\ss}en zu tun.
|
||||
Meistens haben wir es jedoch mit reellen Messgr\"o{\ss}en zu tun
|
||||
(z.B. Gewicht von Tigern, L\"ange von Interspikeintervallen). Es
|
||||
macht keinen Sinn dem Auftreten jeder einzelnen reelen Zahl eine
|
||||
Wahrscheinlichkeit zuzuordnen, denn die Wahrscheinlichkeit genau den
|
||||
Wert einer bestimmten reelen Zahl, z.B. 1.23456789, zu messen ist
|
||||
gleich Null, da es unabz\"ahlbar viele reelle Zahlen gibt.
|
||||
|
||||
Sinnvoller ist es dagegen, nach der Wahrscheinlichkeit zu fragen, eine
|
||||
Zahl aus einem bestimmten Bereich zu erhalten, z.B. die
|
||||
Wahrscheinlichkeit $P(1.2<x<1.3)$, dass die Zahl $x$ einen Wert
|
||||
zwischen 1.2 und 1.3 hat.
|
||||
|
||||
%Der Grenzwert zu einem immer kleineren
|
||||
%Bereich f\"uhrt uns dann zum Begriff der Wahrscheinlichkeitsdichte
|
||||
%\[ p(x) = \lim_{\Delta x \to 0}P(x_0<x<x_0+\Delta x) = P(x_0) + dP/dx \cdot \Delta x \]
|
||||
|
||||
\begin{exercise}{gaussianbins.m}{}
|
||||
\tr{Draw 100 random data from a Gaussian distribution and plot
|
||||
@ -182,24 +234,6 @@ spricht von einer Wahrscheinlichkeitsdichte.
|
||||
\tr{Why?}{Warum?}
|
||||
\end{exercise}
|
||||
|
||||
\begin{exercise}{boxwhisker.m}{}
|
||||
\tr{Generate eine $40 \times 10$ matrix of random numbers and
|
||||
illustrate their distribution in a box-whicker plot
|
||||
(\code{boxplot()} function). How to interpret the plot?}
|
||||
{Erzeuge ein $40 \times 10$ Matrix
|
||||
von Zufallszahlen und illustriere ihre Verteilungen in einem
|
||||
Box-Whisker Plot (\code{boxplot()} Funktion, lies die Hilfe!). Wie ist der
|
||||
Box-Whisker Plot zu interpretieren? Was hat es mit den Ausreissern auf sich?
|
||||
Wie kann man erreichen, dass die Whisker den kleinsten und den gr\"o{\ss}ten
|
||||
Datenwert anzeigen? Warum sind die unterschiedlichen Box-Whiskers nicht alle gleich,
|
||||
obwohl sie aus der selben Verteilung gezogen worden sind?}
|
||||
\end{exercise}
|
||||
|
||||
\begin{figure}[t]
|
||||
\includegraphics[width=1\textwidth]{boxwhisker}
|
||||
\caption{\label{boxwhiskerfig} Box-whisker plots illustrate distributions.}
|
||||
\end{figure}
|
||||
|
||||
|
||||
\section{\tr{Correlations}{Korrelationen}}
|
||||
|
||||
|
Reference in New Issue
Block a user