\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 \"Ubung 6\stitle}}{{\bfseries\large Statistik}}{{\bfseries\large 27. Oktober, 2015}}
\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}{--- Zusatzaufgabe ---\ \mbox{}}
\newcommand{\code}[1]{\texttt{#1}}


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

\input{instructions}


\begin{questions}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\question \qt{Homogeneous Poisson process}
We use the Poisson process to generate spike trains on which we can test and imrpove some 
standard analysis functions.

A homogeneous Poisson process of rate $\lambda$ (measured in Hertz) is a point process
where the probability of an event is independent of time $t$ and independent of previous events.
The probability $P$ of an event within a bin of width $\Delta t$ is 
\[ P = \lambda \cdot \Delta t \]
for sufficiently small $\Delta t$.
\begin{parts}
  
  \part Write a function that generates $n$ homogeneous Poisson spike trains of a given duration $T_{max}$
  with rate $\lambda$.
  \begin{solution}
    \lstinputlisting{hompoissonspikes.m}
  \end{solution}
  
  \part Using this function, generate a few trials and display them in a raster plot.
  \begin{solution}
    \lstinputlisting{../code/spikeraster.m}
    \begin{lstlisting}
      spikes = hompoissonspikes( 10, 100.0, 0.5 );
      spikeraster( spikes )
    \end{lstlisting}
    \mbox{}\\[-3ex]
    \colorbox{white}{\includegraphics[width=0.7\textwidth]{poissonraster100hz}}
  \end{solution}
  
  \part Write a function that extracts a single vector of interspike intervals
  from the spike times returned by the first function.
  \begin{solution}
    \lstinputlisting{../code/isis.m}
  \end{solution}
  
  \part Write a function that plots the interspike-interval histogram
  from a vector of interspike intervals. The function should also
  compute the mean, the standard deviation, and the CV of the intervals
  and display the values in the plot.
  \begin{solution}
    \lstinputlisting{../code/isihist.m}
  \end{solution}
  
  \part Compute histograms for Poisson spike trains with rate
  $\lambda=100$\,Hz. Play around with $T_{max}$ and $n$ and the bin width
  (start with 1\,ms) of the histogram.
  How many
  interspike intervals do you approximately need to get a ``nice''
  histogram? How long do you need to record from the neuron?
  \begin{solution}
    About 5000 intervals for 25 bins. This corresponds to a $5000 / 100\,\hertz = 50\,\second$ recording
    of a neuron firing with 100\,\hertz.
  \end{solution}
  
  \part Compare the histogram with the true distribution of intervals $T$ of the Poisson process
  \[ p(T) = \lambda e^{-\lambda T} \]
  for various rates $\lambda$.
  \begin{solution}
    \lstinputlisting{hompoissonisih.m}
    \colorbox{white}{\includegraphics[width=0.48\textwidth]{poissonisih100hz}}
    \colorbox{white}{\includegraphics[width=0.48\textwidth]{poissonisih20hz}}
  \end{solution}
  
  \part What happens if you make the bin width of the histogram smaller than $\Delta t$ 
  used for generating the Poisson spikes?
  \begin{solution}
    The bins between the discretization have zero entries. Therefore
    the other ones become higher than they should be.
  \end{solution}
  
  \part Plot the mean interspike interval, the corresponding standard deviation, and the CV
  as a function of the rate $\lambda$ of the Poisson process.
  Compare the ../code with the theoretical expectations for the dependence on $\lambda$.
  \begin{solution}
    \lstinputlisting{hompoissonisistats.m}
    \colorbox{white}{\includegraphics[width=0.98\textwidth]{poissonisistats}}
  \end{solution}
  
  \part Write a function that computes serial correlations for the interspike intervals
  for a range of lags.
  The serial correlations $\rho_k$ at lag $k$ are defined as
  \[ \rho_k = \frac{\langle (T_{i+k} - \langle T \rangle)(T_i - \langle T \rangle) \rangle}{\langle (T_i - \langle T \rangle)^2\rangle} = \frac{{\rm cov}(T_{i+k}, T_i)}{{\rm var}(T_i)} \]
  Use this function to show that interspike intervals of Poisson spikes are independent.
  \begin{solution}
    \lstinputlisting{../code/isiserialcorr.m}
    \colorbox{white}{\includegraphics[width=0.98\textwidth]{poissonserial100hz}}
  \end{solution}
  
  \part Write a function that generates from spike times 
  a histogram of spike counts in a count window of given duration $W$.
  The function should also plot the Poisson distribution
  \[ P(k) = \frac{(\lambda W)^ke^{\lambda W}}{k!} \]
  for the rate $\lambda$ determined from the spike trains.
  \begin{solution}
    \lstinputlisting{../code/counthist.m}
    \colorbox{white}{\includegraphics[width=0.48\textwidth]{poissoncounthistdist100hz10ms}}
    \colorbox{white}{\includegraphics[width=0.48\textwidth]{poissoncounthistdist100hz100ms}}
  \end{solution}
  
  \part Write a function that computes mean count, variance of count and the corresponding Fano factor
  for a range of count window durations. The function should generate tow plots: one plotting
  the count variance against the mean, the other one the Fano factor as a function of the window duration.
  \begin{solution}
    \lstinputlisting{../code/fano.m}
    \colorbox{white}{\includegraphics[width=0.98\textwidth]{poissonfano100hz}}
  \end{solution}
  
\end{parts}

\end{questions}

\end{document}