\documentclass[12pt,a4paper,addpoints]{exam}
\setlength{\rightpointsmargin}{1.8cm}
%\extrawidth{2cm}
\usepackage[utf8]{inputenc}\usepackage[pdftex]{graphicx}\usepackage{hyperref}
\usepackage{amsmath}\usepackage{amsfonts}\usepackage{amssymb}\usepackage{amsthm}
\usepackage{parskip}\usepackage{bbding}
\usepackage{xcolor}
\usepackage{listings}
\lstset{ 
  language=R,                     % the language of the code
  basicstyle=\scriptsize\ttfamily, 	% the size of the fonts that are used for the code
  numbers=none,                   % where to put the line-numbers
  backgroundcolor=\color{white},  % choose the background color.
  showspaces=false,               % show spaces adding underscores
  showstringspaces=false,         % underline spaces within strings
  showtabs=false,                 % show tabs within strings
  frame=single,                   % adds a frame around the code
  rulecolor=\color{black},        % if not set, the frame-color may be changed on line-breaks within not-black text
  tabsize=2,                      % sets default tabsize to 2 spaces
  breaklines=true,                % sets automatic line breaking
  breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
  keywordstyle=\color{blue!70!black},      % keyword style
  commentstyle=\color{green!50!black},   % comment style
  stringstyle=\color{orange!70!black}      % string literal style
}

% Maths shortcuts:
\newcommand{\bx}{\ensuremath{\mathbf{x}}} \newcommand{\bX}{\ensuremath{\mathbf{X}}}
\newcommand{\ba}{\ensuremath{\mathbf{a}}} \newcommand{\bA}{\ensuremath{\mathbf{A}}}
\newcommand{\by}{\ensuremath{\mathbf{y}}} \newcommand{\bY}{\ensuremath{\mathbf{Y}}}
\newcommand{\bs}{\ensuremath{\mathbf{s}}} \newcommand{\bS}{\ensuremath{\mathbf{S}}}
\newcommand{\bt}{\ensuremath{\mathbf{t}}} \newcommand{\bT}{\ensuremath{\mathbf{T}}}
\newcommand{\bz}{\ensuremath{\mathbf{z}}} \newcommand{\bZ}{\ensuremath{\mathbf{Z}}}
\newcommand{\bO}{\ensuremath{\mathbf{0}}} \newcommand{\bmu}{\ensuremath{\boldsymbol\mu}}
\newcommand{\btheta}{\ensuremath{\boldsymbol\theta}}
\newcommand{\binfty}{\ensuremath{\boldsymbol\infty}}
\newcommand{\balign}{\begin{aligned}} \newcommand{\ealign}{\end{aligned}}
\newcommand{\cdc}{,\ldots ,} \newcommand{\pdp}{+\cdots +}
\newcommand{\dnpms}{\ensuremath{\sim N_p(\bmu,\Sigma)}}
\newcommand{\ahalf}{\ensuremath{\frac{1}{2}}} \newcommand{\shalf}{\ensuremath{\tfrac{1}{2}}}
\newcommand{\mattwoone}[2]{\ensuremath{\begin{bmatrix}#1 \\ #2\end{bmatrix}}}
\newcommand{\matonetwo}[2]{\ensuremath{\begin{bmatrix}#1 & #2\end{bmatrix}}}
\newcommand{\matthreeone}[3]{\ensuremath{\begin{bmatrix}#1 \\ #2 \\ #3\end{bmatrix}}}
\newcommand{\matthreetwo}[6]{\ensuremath{\begin{bmatrix}#1 & #2 \\ #3 & #4 \\ #5 & #6\end{bmatrix}}}
\newcommand{\mattwothree}[6]{\ensuremath{\begin{bmatrix}#1 & #2 & #3 \\ #4 & #5 & #6\end{bmatrix}}}
\newcommand{\matthreeonedots}[2]{\ensuremath{\begin{bmatrix}#1 \\ \vdots \\ #2\end{bmatrix}}}
\newcommand{\matfourone}[4]{\ensuremath{\begin{bmatrix}#1 \\ #2 \\ #3 \\ #4\end{bmatrix}}}
\newcommand{\matfouronedots}[3]{\ensuremath{\begin{bmatrix}#1 \\ #2 \\ \vdots \\ #3\end{bmatrix}}}
\newcommand{\mattwotwo}[4]{\ensuremath{\begin{bmatrix}#1&#2 \\ #3&#4\end{bmatrix}}}
\newcommand{\matthreethree}[9]{\ensuremath{\begin{bmatrix}#1&#2&#3 \\ #4&#5&#6 \\ #7&#8&#9\end{bmatrix}}}
\newcommand{\matthreethreedots}[4]{\ensuremath{\begin{bmatrix}#1&\cdots &#2 \\ \vdots &\ddots &\vdots \\ #3&\cdots &#4\end{bmatrix}}}
\newcommand{\matfourfourdots}[9]{\ensuremath{\begin{bmatrix}#1&#2&\cdots &#3 \\ #4&#5&\cdots &#6 \\ \vdots &\vdots &\ddots &\vdots \\ #7&#8&\cdots &#9\end{bmatrix}}}
\newcommand{\partderiv}[2]{\ensuremath{\frac{\partial #1}{\partial #2}}}
\newcommand{\etr}{\text{etr}} \newcommand{\vc}{\text{vec\,}} \newcommand{\where}{\text{where }}
% General exam setting shortcuts
\newcounter{DoubleMarginPoints}
\newcounter{HalfMarks}
\newcommand{\tickm}{\ \mbox{\CheckmarkBold}\stepcounter{HalfMarks}\stepcounter{HalfMarks}}
\newcommand{\halfm}{\ \mbox{\makebox[-0.2em][l]{\CheckmarkBold} \raisebox{2.6ex}{\rotatebox{180}{\mbox{\CheckmarkBold}}}}\stepcounter{HalfMarks}}
\newlength{\figwidth} \setlength{\figwidth}{100mm}
\newcommand{\ques}[1][]{\question[#1]\hfill\vspace{12pt}}
\renewcommand{\solutiontitle}{}
\newcommand{\bsol}{\vspace{-16pt}\begin{EnvFullwidth}\begin{solution}}
\newcommand{\esol}{\end{solution}\end{EnvFullwidth}\vspace{-7pt}}
\newcommand{\dps}{\droppoints}

% Subject and lecturer particulars:
\newcommand{\lecturer}{Mr~S.~van~der~Merwe} % Change
\newcommand{\subjectcode}{STSM~3714} % Change
\newcommand{\testid}{Test 3} % Change
\newcommand{\testdate}{29 May 2018} % Change
\newcommand{\timeallowed}{120 Minutes} % Change

\hypersetup{pdfinfo={Title={UFS Test},Subject={\subjectcode},Author={\lecturer},Keywords={Test, UFS} } }

\begin{document}
\firstpagefooter{}{Page \thepage\ of \numpages}{} \runningfooter{}{Page \thepage\ of \numpages}{}
\pointsdroppedatright \bracketedpoints

\printanswers % Comment or uncomment to change between question paper and memo


% Don't change this section unless you have to, rather change the particulars above.
\hbadness=10000 \begin{center}\fbox{\parbox{14cm}{\begin{center}
\textbf{UNIVERSITEIT VAN DIE VRYSTAAT}\\ \textbf{UNIVERSITY OF THE FREE STATE}\par
\fbox{\fbox{\Large{\textbf{\subjectcode}}}}\par
\textbf{WISKUNDIGE STATISTIEK \& AKTUARIËLE WETENSKAP/}\\
\textbf{MATHEMATICAL STATISTICS \& ACTUARIAL SCIENCE}\par\medskip
\textbf{\huge\testid\ ---\ \testdate}\par
\begin{solution}\hfill \begin{Huge}MEMORANDUM\end{Huge}\hfill \break \hfill \end{solution}\end{center}\hbadness=1000
\textbf{TYD/TIME:} \timeallowed\hfill PUNTE/MARKS: \numpoints\ }}\end{center}\vspace{0.5cm}
\emph{INSTRUCTIONS:}\begin{itemize} 
\setlength{\parskip}{2pt}
\item Answer all questions in a single Word document but convert the final version to .pdf before submitting (backup the Word document and all working files separately).
\item Label questions clearly, as it is done on this question paper. Also provide your details in the document header.
\item Show all code, calculations, formulas and reasoning.
\item Use a significance level of 5\% for all hypothesis tests in Question 1.
\item No USB drives or data transfer devices may be used during the course of the test.
\item No attempt at communication of any kind is allowed with other students. No email or messaging service on any device may be accessed.
\end{itemize}\vspace{0.5cm}
\qformat{\hfill\Large\underline{\textbf{Question~\thequestion}}\hfill}

% Test starts here, change everything after this.
\begin{questions}

\ques[] \label{Warpbreaks}

For this question, load the warpbreaks data from R [command: `data(warpbreaks)']. The data compares two types of wool by comparing the number of breaks under different loads. Thus, the goal is to find which wool type breaks less.

\begin{parts}

\part[4]
Test whether the breaks could follow an approximate Normal distribution if you consider ONLY the data for wool B under low tension. \dps
\bsol
\begin{lstlisting}
data(warpbreaks)
attach(warpbreaks)
names(warpbreaks)
shapiro.test(breaks[(wool=='B') & (tension=='L')])
\end{lstlisting}
\tickm
\esol
\hspace{1mm}
\bsol
\fbox{\parbox{\figwidth}{\texttt{
Shapiro-Wilk normality test \\
data:  breaks[(wool == "B") \& (tension == "L")] \\
W = 0.94899, p-value = 0.679 \\
}}}
\tickm

Since the p-value $> 0.05$ we fail to reject $H_0\tickm$ and continue to assume an approximate Normal distribution for wool B under low tension.\tickm
\esol

\part[5]
Test whether wool A and wool B could have the same variance, considering all loads. \dps
\bsol
$$H_0:\ \sigma^2_A=\sigma^2_B\ vs\ H_1:\ \sigma^2_A\ne \sigma^2_B\ \tickm$$
\begin{lstlisting}
var.test(breaks~wool)
\end{lstlisting}
\tickm

\fbox{\parbox{\figwidth}{\texttt{
        F test to compare two variances\\
data:  breaks by wool\\
F = 2.9046, num df = 26, denom df = 26, p-value = 0.008461\\
alternative hypothesis: true ratio of variances is not equal to 1\\
95 percent confidence interval:\\
 1.323695 6.373561\\
sample estimates:\\
ratio of variances \\
          2.904591 \\
}}}
\tickm

Since the p-value $< 0.05$ we reject $H_0\tickm$ and conclude that the variance in breaking of the two types of wool is probably not the same.\tickm
\esol

\part[5]
Test whether wool A and wool B could have the same mean, considering all loads, and taking the result of the previous question into account. \dps
\bsol
$$H_0:\ \mu^2_A=\mu^2_B\ vs\ H_1:\ \mu^2_A\ne \mu^2_B\ \tickm$$
\begin{lstlisting}
t.test(breaks~wool)
\end{lstlisting}
\tickm
\esol
\hspace{1mm}
\bsol
\fbox{\parbox{\figwidth}{\texttt{
        Welch Two Sample t-test\\
data:  breaks by wool\\
t = 1.6335, df = 42.006, p-value = 0.1098\\
alternative hypothesis: true difference in means is not equal to 0\\
95 percent confidence interval:\\
 -1.360096 12.915652\\
sample estimates:\\
mean in group A mean in group B \\
       31.03704 \ \  \  \  25.25926 \\
}}}
\tickm

Since the p-value $> 0.05$ we fail to reject $H_0\tickm$ and continue to assume the number of breaks are the same on average.\tickm
\esol

\end{parts}
\droptotalpoints

\ques[] \label{Rayleigh}

At an archery tournament you are asked to model the distances between 9 arrows on a target and the bullseye of the target. Assuming Normal variation in all dimensions you know you can model the distances using a Rayleigh distribution, with density $f(x|\tau)=\tau x e^{-0.5\tau x^2}$, and CDF $F(x|\tau)=1-e^{-0.5\tau x^2}$. $\tau$ is the precision parameter and represents the archer's skill. Potentially this approach could be used to more accurately differentiate archers, but for now you only wish to model one sample given in `rayleighsample.csv'. Distances are in inches.

\begin{parts}
\part[4]
Derive the log likelihood and show (in detail) that the ML estimator for $\tau$ is $\frac{2n}{\sum X_i^2}$.\dps
\bsol
$$\begin{aligned}
\ell&=\sum_{i=1}^n \ln f(x_i|\tau) \\
&= \sum_{i=1}^n \left( \ln\tau + \ln x_i - 0.5\tau x_i^2 \right)\tickm \\
&= n\ln\tau + \sum_{i=1}^n \ln x_i - 0.5\tau \sum_{i=1}^n x_i^2 \tickm \\
\therefore \frac{d\ell}{d\tau}&=\frac{n}{\tau} - 0.5\sum_{i=1}^n x_i^2 \tickm\\
\therefore \hat{\tau}&= \frac{2n}{\sum X_i^2}\tickm \qquad [\text{By setting derivative to zero.}]
\end{aligned}$$
\esol

\part[4]
Draw a histogram of the given sample and overlay the ML estimated density. You may use relative frequencies, or match the scale another way.\dps
\bsol
\begin{lstlisting}
d <- read.csv(file.choose(),row.names=1)
sam <- d$x
class(sam)
(n <- length(sam))
tauest <- 2*n/sum(sam^2)
hist(sam,6,freq=FALSE,col='blue',density=10,angle=30,main='Rayleigh distribution fit',xlab='Distance',ylab='Relative Frequency',xlim=c(0,1.2*max(sam)))
curve(tauest*x*exp(-0.5*tauest*(x^2)),0,1.2*max(sam),add=TRUE,lwd=2,col='purple')
\end{lstlisting}
\tickm

\includegraphics[width=\figwidth]{STSM3714Test3of2018graph1.pdf}
\tickm\tickm\tickm
\esol

\part[7]
Derive a generalised likelihood ratio test for the hypothesis $H_0:\ \tau=\tau_0$. \hspace{12mm}
[Hint: $Q=\left(\tau\sum_{i=1}^n X_i^2\right) \sim \chi^2_{2n}$] \dps
\bsol
$$\begin{aligned}
\ln\lambda &= \ell(\tau_0) - \ell(\hat{\tau}) \tickm \\
&= \left[ n\ln\tau_0 + \sum_{i=1}^n \ln x_i - 0.5\tau_0 \sum_{i=1}^n x_i^2 \right] - \left[ n\ln\hat{\tau} + \sum_{i=1}^n \ln x_i - 0.5\hat{\tau} \sum_{i=1}^n x_i^2 \right] \\
&=n\left[\ln \tau_0 - \ln \hat{\tau} + 1\right] - 0.5\tau_0 \sum_{i=1}^n x_i^2 \tickm  \\
\therefore \lambda &= n^{-n}e^n (0.5Q)^n \exp (-0.5Q),\ Q=\left(\tau_0\sum_{i=1}^n X_i^2\right) \tickm\tickm\tickm\\
\therefore \lambda \leq c &\Rightarrow \text{ Reject }H_0 \text{ when } Q\leq k_1 \text{ or }Q\geq k_2\ ,\ k_1<k_2 \tickm \\
&\text{Under }H_0,\ Q\sim \chi^2_{2n} \\
&\therefore \text{ Reject }H_0 \text{ when } \sum_{i=1}^n X_i^2\leq \frac{1}{\tau_0}\chi^2_{2n; \frac{\alpha}{2}} \text{ or } \sum_{i=1}^n X_i^2 \geq \frac{1}{\tau_0}\chi^2_{2n; 1-\frac{\alpha}{2}}\tickm
\end{aligned}$$
\esol

\part[6]
Use the pivotal quantity $Q=\left(\tau\sum_{i=1}^n X_i^2\right) \sim \chi^2_{2n}$ to construct a 90\% symmetric interval for $\tau$ based on the observed sample, then use it to test $H_0:\ \tau=0.5$. \dps
\bsol
$$\begin{aligned}
&P\left[\frac{\chi^2_{2n; \frac{\alpha}{2}}}{\sum_{i=1}^n X_i^2} \leq \tau \leq \frac{\chi^2_{2n; 1-\frac{\alpha}{2}}}{\sum_{i=1}^n X_i^2}\right]=1-\alpha\\
\Rightarrow &P\left[0.6269 \leq \tau \leq 1.0576\right]=0.9 \tickm\tickm\tickm
\end{aligned}$$

\begin{lstlisting}
(SS  <- sum(sam^2))
qchisq(c(0.05,0.95),2*n)/SS
\end{lstlisting}
\tickm

Since the interval does not contain 0.5\tickm, we reject $H_0$ and conclude that $\tau\ne 0.5\tickm$ with 90\% confidence.
\esol

\part[5]
Create a function that calculates the Anderson-Darling statistic for a sample assumed to come from a Rayleigh distribution. It should automatically calculate and use the ML estimator. Show that it works by applying it to your sample. It must give a result of about 0.25.\dps
\bsol
\begin{lstlisting}
ADstat.rayleigh <- function(sam) {
n <- length(sam)
ML <- 2*n/sum(sam^2)
x <- 1 - exp(-ML*(sort(sam)^2)/2)
A <- -n - sum(((2*(1:n)-1)/n)*(log(x)+log(1-rev(x))))
return(A)
}
(basestat <- ADstat.rayleigh(sam))
\end{lstlisting}
\tickm\tickm\tickm\tickm

Result checked\tickm
\esol

\part[4]
Test the null hypothesis that the sample originates from a Rayleigh model using the parametric bootstrap approach. Use 9999 random samples generated with the `rrayleigh' function supplied by the lecturer.\dps
\bsol
\begin{lstlisting}
n <- length(sam)
acc <- 9999
stats <- rep(0,acc)
for (i in 1:acc) {
	newx <- rrayleigh(n,tauest)
	stats[i] <- ADstat.rayleigh(newx)
}
(pvalue <- mean(stats>basestat))
\end{lstlisting}
\tickm
\tickm
\tickm

The p-value should be close to 90\%.\tickm
\esol
\end{parts}

\droptotalpoints

\bsol
\vspace{1cm}
\setcounter{DoubleMarginPoints}{0}
\addtocounter{DoubleMarginPoints}{\numpoints}
\addtocounter{DoubleMarginPoints}{\numpoints}
Total half marks on memo = \theHalfMarks\ vs. \theDoubleMarginPoints\ = Double total margin points (=\numpoints).
\esol

\end{questions}

%\input{Tables.tex}

\end{document}

