\documentclass[12pt,a4paper,pdftex]{exam}

\usepackage[german]{babel}
\usepackage{pslatex}
\usepackage[mediumspace,mediumqspace,Gray]{SIunits}      % \ohm, \micro
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage[breaklinks=true,bookmarks=true,bookmarksopen=true,pdfpagemode=UseNone,pdfstartview=FitH,colorlinks=true,citecolor=blue]{hyperref}

%%%%% layout %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry}
\pagestyle{headandfoot}
\ifprintanswers
\newcommand{\stitle}{: L\"osungen}
\else
\newcommand{\stitle}{}
\fi
\header{{\bfseries\large Exercise 7\stitle}}{{\bfseries\large Statistics}}{{\bfseries\large 21. November, 2017}}
\firstpagefooter{Prof. Dr. Jan Benda}{Phone: 29 74573}{Email:
jan.benda@uni-tuebingen.de}
\runningfooter{}{\thepage}{}

\setlength{\baselineskip}{15pt}
\setlength{\parindent}{0.0cm}
\setlength{\parskip}{0.3cm}
\renewcommand{\baselinestretch}{1.15}

%%%%% listings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{listings}
\lstset{
  language=Matlab,
  basicstyle=\ttfamily\footnotesize,
  numbers=left,
  numberstyle=\tiny,
  title=\lstname,
  showstringspaces=false,
  commentstyle=\itshape\color{darkgray},
  breaklines=true,
  breakautoindent=true,
  columns=flexible,
  frame=single,
  xleftmargin=1em,
  xrightmargin=1em,
  aboveskip=10pt
}

%%%%% math stuff: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{bm} 
\usepackage{dsfont}
\newcommand{\naZ}{\mathds{N}}
\newcommand{\gaZ}{\mathds{Z}}
\newcommand{\raZ}{\mathds{Q}}
\newcommand{\reZ}{\mathds{R}}
\newcommand{\reZp}{\mathds{R^+}}
\newcommand{\reZpN}{\mathds{R^+_0}}
\newcommand{\koZ}{\mathds{C}}

%%%%% page breaks %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\continue}{\ifprintanswers%
\else
\vfill\hspace*{\fill}$\rightarrow$\newpage%
\fi}
\newcommand{\continuepage}{\ifprintanswers%
\newpage
\else
\vfill\hspace*{\fill}$\rightarrow$\newpage%
\fi}
\newcommand{\newsolutionpage}{\ifprintanswers%
\newpage%
\else
\fi}

%%%%% new commands %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\qt}[1]{\textbf{#1}\\}
\newcommand{\pref}[1]{(\ref{#1})}
\newcommand{\extra}{--- bonus question ---\ \mbox{}}
\newcommand{\code}[1]{\texttt{#1}}

\graphicspath{{../../pointprocesses/exercises/}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}

\input{instructions}


\begin{questions}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\question \qt{Probabilities of a normal distribution}
Which fraction of a normally distributed data set is contained in ranges
that are symmetric around the mean?
\begin{parts}
  \part Generate a data set $X = (x_1, x_2, ... x_n)$ of
  $n=10000$ normally distributed numbers with mean $\mu=0$ and
  standard deviation $\sigma=1$ (\code{randn() Funktion}).
  \part Estimate and plot the probability density of this data set (normalized histogram).
  For a comparison plot the normal distribution
  \[ p_g(x) = \frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2} \]
  into the same plot.

  \part \label{onesigma} How many data values are at maximum one standard deviation 
  away from the mean?\\
  That is, how many data values $x_i$ have the value $-\sigma < x_i < +\sigma$?\\
  What is the probability $P_{\pm\sigma}$ to get a value in this range? 

  \part \label{probintegral} Compute the probability of 
  $-\sigma < x_i < +\sigma$ by numerically integrating over the
  probability density of the normal distribution
  \[ P_{\pm\sigma}=\int_{x=\mu-\sigma}^{x=\mu+\sigma} p_g(x) \, dx \; .\]
  First check whether
  \[ \int_{-\infty}^{+\infty} p_g(x) \, dx = 1 \; . \]
  Why is this the case?

  \part What fraction of the data is contained in the intervals $\pm 2\sigma$
  and $\pm 3\sigma$? 

  Compare the results with the corresponding integrals over the normal distribution.

  \part \label{givenfraction} Find out which intervals, that are
  symmetric with respect to the mean, contain 50\,\%, 90\,\%, 95\,\% and 99\,\%
  of the data by means of numeric integration of the normal
  distribution.

  \part \extra Modify the code of questions \pref{onesigma} -- \pref{givenfraction} such
  that it works for data sets with arbitrary mean and arbitrary standard deviation.\\
  Check your code with different sets of random numbers.\\
  How do you generate random numbers of a given mean and standard
  deviation using the \code{randn()} function?
\end{parts}
\begin{solution}
  \lstinputlisting{normprobs.m}
\end{solution}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\question \qt{Central limit theorem} 
According to the central limit theorem the sum of independent and
identically distributed (i.i.d.)  random variables converges toward a
normal distribution, although the distribution of the randmon
variables might not be normally distributed.

With the following questions we want to illustrate the central limit theorem.
\begin{parts}
  \part Before you continue reading, try to figure out yourself what
  the central limit theorem means and what you would need to do for
  illustrating this theorem.

  \part Draw 10000 random numbers that are uniformly distributed between 0 and 1
  (\code{rand} function).

  \part Plot their probability density (normalized histogram).

  \part Draw another set of 10000 uniformly distributed random numbers
  and add them to the first set of numbers.

  \part Plot the probability density of the summed up random numbers.

  \part Repeat steps (d) and (e) many times.

  \part Compare in a plot the probability density of the summed up
  numbers with the normal distribution
  \[ p_g(x) =
  \frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{1}{2}\left(\frac{x-\mu}{\sigma}\right)^2}\]
  with mean $\mu$ and standard deviation $\sigma$ of the summed up random numbers.

  \part How do the mean and the standard deviation change with the
  number of summed up data sets?

  \part \extra Check the central limit theorem in the same way using
  exponentially distributed random numbers (\code{rande} function).
\end{parts}
\begin{solution}
  \lstinputlisting{centrallimit.m}
  \includegraphics[width=0.5\textwidth]{centrallimit-hist01}
  \includegraphics[width=0.5\textwidth]{centrallimit-hist02}
  \includegraphics[width=0.5\textwidth]{centrallimit-hist03}
  \includegraphics[width=0.5\textwidth]{centrallimit-hist05}
  \includegraphics[width=0.5\textwidth]{centrallimit-samples}
\end{solution}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  \question \qt{Statistics of interspike intervals}
  Download the files \code{poisson.mat},
  \code{pifou.mat}, and \code{lifadapt.mat} from Ilias.  
  Each of these files contains several trials of spike trains
  of a specific type of neuron. The spike times are measured in seconds.

  We want to compare the statistics of the interspike intervals of the
  three neurons.
  \begin{parts}
    \part Load the spike trains from the three files.
    Make sure that data are assigned to different variables. 

    What is the type of the data? How can you access individual spike trains?
    How do you access single spike times?
    \begin{solution}
      \begin{lstlisting}
        clear all
        % not so good:
        load poisson.mat
        whos
        poissonspikes = spikes;
        load pifou.mat;
        pifouspikes = spikes;
        load lifadapt.mat;
        lifadaptspikes = spikes;
        clear spikes;
        % better:
        clear all
        x = load('poisson.mat');
        poissonspikes = x.spikes;
        x = load('pifou.mat');
        pifouspikes = x.spikes;
        x = load('lifadapt.mat');
        lifadaptspikes = x.spikes;
      \end{lstlisting}
    \end{solution}
    
    \part Write a function that illustrated the spike times of the
    first $t_{max}$ seconds in a raster plot. Each spike train is one
    row in the raster plot. Each spike is a vertical line at the time
    of the spike. Use this function to plot the first second of the
    spike rasters of the three neurons.
    \begin{solution}
      \lstinputlisting{../../pointprocesses/code/spikeraster.m}
      \lstinputlisting{../../pointprocesses/code/plotspikeraster.m}
      \mbox{}\\[-3ex]
      \colorbox{white}{\includegraphics[width=1\textwidth]{spikeraster}}
    \end{solution}
  
    \part Write a function that returns a single vector containing the
    interspike intervals of aall trials of spike times.
    \begin{solution}
      \lstinputlisting{../../pointprocesses/code/isis.m}
    \end{solution}
    
    \part Write a function that computes and plots an estimate of the
    probability density of interspike intervals from a vector of
    interspike intervals. The interspike intervals are given in
    seconds, but the plot should mark the interspike intervals in
    milliseconds. In addition, the function should compute the mean,
    the standard deviation and the coefficient of variation
    and display them in the plot as well.

    Use this and the previous functions to compare the 
    interspike interval statistics of the three neurons.
    \begin{solution}
      \lstinputlisting{../../pointprocesses/code/isihist.m}
      \lstinputlisting{../../pointprocesses/code/plotisih.m}
      \mbox{}\\[-3ex]
      \colorbox{white}{\includegraphics[width=1\textwidth]{isihist}}
    \end{solution}
  \end{parts}

\end{questions}

\end{document}