 \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 6\stitle}}{{\bfseries\large Statistics}}{{\bfseries\large November 14th, 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}}


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

\input{instructions}

\ifprintanswers%
\else

\begin{itemize}
\item Convince yourself that each single line of your code really does
  what it should do!  Test it with small examples directly in the
  command line.
\item Always try to break down your solution into small and meaningful
  functions. As soon something similar is computed more than once you
  should definitely put it into a function.
\item Initially test computationally expensive \code{for} loops, vectors,
  matrices, etc. with small numbers of repetitions and/or
  sizes. Once it is working use large repetitions and/or sizes for
  getting a good statistics.
\item Use the help functions of \code{matlab} (\code{help command} or
  \code{doc command}) and the internet to figure out how specific
  \code{matlab} functions are used and what features they offer.  In
  addition, the internet offers a lot of material and suggestions for
  any question you have regarding your code !
\item Please upload your solution to the exercises to ILIAS as a zip-archive with the name
  ``probabilities\_\{last name\}\_\{first name\}.zip''.
\end{itemize}

\fi


\begin{questions}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\question \textbf{Read chapter 4 of the script on ``programming style''!}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\question \qt{Probabilities of a die I}
The computer can roll dice with more than 6 faces!
\begin{parts}
  \part Simulate 10000 times rolling a die with eight faces by
  generating integer random numbers $x_i = 1, 2, \ldots 8$ .

  \part Compute the probability $P(5)$ of getting a five by counting the number of fives
  occurring in the data set.

  Does the result fit to your expectation?

  Check the probabilities $P(x_i)$ of the other numbers.

  Is the die a fair die?

  \part Store the computed probabilities $P(x_i)$ in a vector and use
  the \code{bar()} function for plotting the probabilities as a
  function of the corresponding face values.

  \part Compute a normalized histogram of the face values by means of
  the \code{hist()} and \code{bar()} functions.

  \part \extra Simulate a loaded die with the six showing up
  three-times as often as the other numbers.

  Compute a normalized histogram of the face values from rolling the loaded die 10000 times.
\end{parts}
\begin{solution}
  \lstinputlisting{rollthedie.m}
  \lstinputlisting{diehist.m}
  \lstinputlisting{die1.m}
  \includegraphics[width=1\textwidth]{die1}
\end{solution}


\continue
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\question \qt{Probabilities of a die II}
Now we analyze several dice at once.
\begin{parts}
  \part Simulate 20 dice, each of which is rolled 100 times 
  (each die is simulated with the same random number generator).
  \part Compute for this data set for each die a normalized histogram.
  \part Calculate the mean and the standard deviation for each face
  value averaged over the dice.
  \part Visualize the result in a bar plot with error bars
  (\code{bar()} and \code{errorbar()} functions).
\end{parts}
\begin{solution}
  \lstinputlisting{die2.m}
  \includegraphics[width=0.5\textwidth]{die2}
\end{solution}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\question \qt{Histogram of the normal distribution}
\vspace{-3ex}
\begin{parts}
  \part Generate a data set $X = (x_1, x_2, ... x_n)$ of
  $n=10000$ normally distributed random numbers with mean $\mu=0$ and
  standard deviation $\sigma=1$ (\code{randn()} function).

  \part Compute from this data set the probability $P(0\le x<0.5)$.

  \part What happens to the probability of drawing a number from a
  specific range (z.B. $P(0\le x<a)$), if this range gets smaller and
  smaller, i.e. $a \to 0$?

  Write a script that illustrates this by plotting $P(0\le x<a)$
  as a function of $a$ (use $0 \le a \le 4$).

  \part \label{manualpdf} Compute and plot the probability density of
  the data set (the normalized histogram). First, define the positions
  of the bins (width of 0.5) in a vector. Count in a \code{for} loop
  for each bin die number of data values falling into the
  bin. Finally, normalize the resulting histogram and plot it using
  the \code{bar()} function.

  \part \label{gaussianpdf} Draw into the same 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} \]
  for a comparison.

  \part Plot the probability density as in (\ref{manualpdf}) and
  (\ref{gaussianpdf}), but this time by means of the \code{hist()} and
  \code{bar()} functions.
\end{parts}
\begin{solution}
  \lstinputlisting{normhist.m}
  \includegraphics[width=1\textwidth]{normhist}
\end{solution}


\end{questions}

\end{document}