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

\newcommand{\exercisetopic}{Point processes}
\newcommand{\exercisenum}{11}
\newcommand{\exercisedate}{January 19th, 2021}

\input{../../exercisesheader}

\firstpagefooter{Prof. Dr. Jan Benda}{}{jan.benda@uni-tuebingen.de}

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

\input{../../exercisestitle}

\begin{questions}

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
  \question \qt{Read in chapter 10 on ``Spike-train analysis'' sections 10.1 -- 10.5!}\vspace{-3ex}

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
  \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 the 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}
        % not so good:
        clear all
        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;
        clear x;
      \end{lstlisting}
      The files contain cell arrays with 20 trials of spike
      trains. Each spike train is a vector containing the times of
      spikes.
      \begin{lstlisting}
        % access first trial:
        trial = poissonspikes{1};
        % access second spike of first trial:
        trial(2);
      \end{lstlisting}
    \end{solution}

    \newsolutionpage    
    \part Write a function that displays 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. When appropriate, the function should use milliseconds 
    for the time axis instead of seconds.

    Use this function to plot the first second of the spike rasters of
    the three neurons. Carefully look at the raster plots. How do the
    spike rasters differ between the three neurons?
    \begin{solution}
      \lstinputlisting{rasterplot.m}
      \lstinputlisting{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 all trials of spike times.
    \begin{solution}
      \lstinputlisting{isis.m}
    \end{solution}
    
    \part Write a function that computes an estimate of the
    probability density of interspike intervals from a vector of
    interspike intervals. The function should automatically choose a
    good bin width for the histogram.  

    Write another function that plots the probability density of
    interspike intervals given a vector of interspike intervals as
    argument. The function should use the first function for computing
    the probability density. 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. How do the ISI
    histograms differ between the three neurons?
    \begin{solution}
      \lstinputlisting{isihist.m}
      \lstinputlisting{plotisihist.m}
      \lstinputlisting{plotisihs.m}
      \mbox{}\\[-3ex]
      \colorbox{white}{\includegraphics[width=1\textwidth]{isihist}}
    \end{solution}

    \part Compare the interspike interval histogram of the Poisson
    spike train to the expected exponential distribution
    \[ p(T) = \lambda e^{-\lambda T} \; .\] of a Poisson process with
    firing rate $\lambda$. Estimate the firing rate from the inverse
    of the mean interspike interval.
    \begin{solution}
      \lstinputlisting{plotpoissonisih.m}
      \mbox{}\\[-3ex]
      \colorbox{white}{\includegraphics{poissonisihist}}
    \end{solution}
    
    \continue
    % XXX Add return map!!! XXX
    \part Write a function that computes the serial correlations of
    interspike intervals for lags up to \code{maxlag}. The serial
    correlations $\rho_k$ for lag $k$ of the interspike intervals
    $T_i$ are the correlation coefficients between interspike
    intervals $T_i$ and the intervals $T_{i+k}$ that are shifted by
    lag $k$:
    \[ \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)} = {\rm corr}(T_{i+k}, T_i) \]

    Write another function that plots the serial correlations.

    Use the functions to compare the serial correlations of the
    interspike intervals of the three neurons. What do the serial
    correlations mean? How do they relate to what you see in the
    raster plots? Do the serial correlations of the Poisson spike
    train match the expectations?
    \begin{solution}
      \lstinputlisting{isiserialcorr.m}
      \lstinputlisting{plotisiserialcorr.m}
      \lstinputlisting{plotserialcorr.m}
      \colorbox{white}{\includegraphics[width=1\textwidth]{serialcorr}}
    \end{solution}
    
  \end{parts}

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
  \question \qt{Statistics of spike counts}
  Now let's have a look at the statistics of the spike counts.
  \begin{parts}
    \part \label{counts} Write a function that counts with the number
    of spikes in windows of a given width $W$. The spikes are passed
    to the function as a cell array containing vectors of spike times.
    The function returns a single vector with all the spike counts.
    \begin{solution}
      \lstinputlisting{counts.m}
    \end{solution}

    \part Generate a properly normalized histogram of spike counts for
    the data of the three types of neurons. Use 100\,ms for the window
    width and the function from (\ref{counts}) for computing the spike
    counts.

    In addition, compare the distributions with the Poisson
    distribution expected for a Poisson spike train:
    \[ P(k) = \frac{(\lambda W)^ke^{\lambda W}}{k!} \; , \] where
    $\lambda$ is the rate of the spike train that you should estimate
    from the data.
    \begin{solution}
      \newsolutionpage
      \lstinputlisting{spikecountshists.m}
      \colorbox{white}{\includegraphics[width=1\textwidth]{spikecountshists}}
    \end{solution}

    \newsolutionpage
    \part Write a function that computes for a range of window widths
    the mean, the variance and the Fano factor of the corresponding
    spike counts.  The function should generate two plots. One showing
    the spike count variance in dependence on the mean spike count
    (the mean spike count increases for larger window widths). The
    other plot showing the Fano factor as a function of window width.
    \begin{solution}
      \lstinputlisting{fanoplot.m}
      \newsolutionpage
      \lstinputlisting{fanoplots.m}
      \colorbox{white}{\includegraphics[width=1\textwidth]{fanoplotspoisson}}
      \colorbox{white}{\includegraphics[width=1\textwidth]{fanoplotspifou}}
      \colorbox{white}{\includegraphics[width=1\textwidth]{fanoplotslifadapt}}
    \end{solution}

  \end{parts}
  
\end{questions}

\end{document}