\documentclass[addpoints,10pt]{exam}
\usepackage{url}
\usepackage{color}
\usepackage{hyperref}
\usepackage{graphicx}

\pagestyle{headandfoot}
\runningheadrule
\firstpageheadrule

\firstpageheader{Scientific Computing}{Homogeneous Poisson process}{Oct 27, 2014}
%\runningheader{Homework 01}{Page \thepage\ of \numpages}{23. October 2014}
\firstpagefooter{}{}{}
\runningfooter{}{}{}
\pointsinmargin
\bracketedpoints

%\printanswers
\shadedsolutions

\usepackage[mediumspace,mediumqspace,Gray]{SIunits}      % \ohm, \micro

%%%%% listings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{listings}
\lstset{
 basicstyle=\ttfamily,
 numbers=left,
 showstringspaces=false,
 language=Matlab,
 breaklines=true,
 breakautoindent=true,
 columns=flexible,
 frame=single,
 captionpos=t,
 xleftmargin=2em,
 xrightmargin=1em,
 aboveskip=10pt,
 %title=\lstname,
 title={\protect\filename@parse{\lstname}\protect\filename@base.\protect\filename@ext}
 }


\begin{document}

\sffamily
%%%%%%%%%%%%%% Questions %%%%%%%%%%%%%%%%%%%%%%%%%

\begin{questions}
  \question \textbf{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{simulations/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{simulations/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{simulations/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 simulations 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{simulations/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{simulations/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{simulations/fano.m}
      \colorbox{white}{\includegraphics[width=0.98\textwidth]{poissonfano100hz}}
    \end{solution}

  \end{parts}
  
\end{questions}


\end{document}