This repository has been archived on 2021-05-17. You can view files and clone it, but cannot push or open issues or pull requests.
scientificComputing/linearalgebra/exercises/exercises01.tex

192 lines
7.1 KiB
TeX

\documentclass[12pt,a4paper,pdftex]{exam}
\usepackage[english]{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}{: Solutions}
\else
\newcommand{\stitle}{}
\fi
\header{{\bfseries\large Exercise\stitle}}{{\bfseries\large PCA}}{{\bfseries\large January 7th, 2019}}
\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{Covariance and correlation coefficient\vspace{-3ex}}
\begin{parts}
\part Generate two vectors $x$ and $z$ with $n=1000$ Gausian distributed random numbers.
\part Compute $y$ as a linear combination of $x$ and $z$ according to
\[ y = r \cdot x + \sqrt{1-r^2}\cdot z \]
where $r$ is a parameter $-1 \le r \le 1$.
What does $r$ do?
\part Plot a scatter plot of $y$ versus $x$ for about 10 different values of $r$.
What do you observe?
\part Also compute the covariance matrix and the correlation
coefficient matrix between $x$ and $y$ (functions \texttt{cov} and
\texttt{corrcoef}). How do these matrices look like for different
values of $r$? How do the values of the matrices change if you generate
$x$ and $z$ with larger variances?
\part Do the same analysis (scatter plot, covariance, and correlation coefficient)
for \[ y = x^2 + 0.5 \cdot z \]
Are $x$ and $y$ really independent?
\end{parts}
\question \qt{Principal component analysis in 2D\vspace{-3ex}}
\begin{parts}
\part Generate pairs $(x,y)$ of Gaussian distributed random numbers such
that all $x$ values have zero mean, half of the $y$ values have mean $+d$
and the other half mean $-d$, with $d \ge0$.
\part Plot scatter plots of the pairs $(x,y)$ for $d=0$, 1, 2, 3, 4 and 5.
Also plot a histogram of the $x$ values.
\part Apply PCA on the data and plot a histogram of the data projected onto
the PCA axis with the largest eigenvalue.
What do you observe?
\end{parts}
\question \qt{Principal component analysis in 3D\vspace{-3ex}}
\begin{parts}
\part Generate triplets $(x,y,z)$ of Gaussian distributed random numbers such
that all $x$ values have zero mean, half of the $y$ and $z$ values have mean $+d$
and the other half mean $-d$, with $d \ge0$.
\part Plot 3D scatter plots of the pairs $(x,y)$ for $d=0$, 1, 2, 3, 4 and 5.
Also plot a histogram of the $x$ values.
\part Apply PCA on the data and plot a histogram of the data projected onto
the PCA axis with the largest eigenvalue.
What do you observe?
\end{parts}
\continue
\question \qt{Spike sorting}
Extracellular recordings often pick up action potentials originating
from more than a single neuron. In case the waveforms of the action
potentials differ between the neurons one could assign each action
potential to the neuron it originated from. This process is called
``spike sorting''. Here we explore this methods on a simulated
recording that contains action potentials from two different
neurons.
\begin{parts}
\part Load the data from the file \texttt{extdata.mat}. This file
contains the voltage trace of the recording (\texttt{voltage}),
the corresponding time vector in seconds (\texttt{time}), and a
vector containing the times of the peaks of detected action
potentials (\texttt{spiketimes}). Further, and in contrast to real
data, the waveforms of the actionpotentials of the two neurons
(\texttt{waveform1} and \texttt{waveform2}) and the corresponding
time vector (\texttt{waveformt}) are also contained in the file.
\part Plot the voltage trace and mark the peaks of the detected
action potentials. Zoom into the plot and look whether you can
differentiate between two different waveforms of action
potentials. How do they differ?
\part Cut out the waveform of each action potential (5\,ms before
and after the peak). Plot all these snippets in a single
plot. Can you differentiate the two actionpotential waveforms?
\part Apply PCA on the waveform snippets, that is compute the
eigenvalues and eigenvectors of their covariance matrix, and plot
the sorted eigenvalues (the ``eigenvalue spectrum''). How many
eigenvalues are clearly larger than zero?
\part Plot the two eigenvectors (``features'') with the two
largest eigenvalues.
\part Project the waveform snippets onto these two eigenvectors
and display them with a scatter plot. What do you observe? Can you
separate two ``clouds'' of data points (``clusters'')?
\part Think about a very simply way how to separate the two
clusters. Generate a vector whose elements label the action
potentials, e.g. that contains '1' for all snippets belonging to
the one cluster and '2' for the waveforms of the other
cluster. Use this vector to mark the two clusters in the previous
plot with two different colors.
\part Plot the waveform snippets of each cluster together with the
true waveform obtained from the data file. Do they match?
\part Mark the action potentials in the recording according to
their cluster identity.
\part Compute interspike-interval histograms of all the (unsorted)
action potentials, and of each of the two neurons. What do they
tell you?
\end{parts}
\end{questions}
\end{document}