\documentclass[12pt]{article}
% Exercise environment
\newcounter{ex}
\newenvironment{ex}[1][]{\begin{trivlist}\refstepcounter{ex}
\item[\hskip \labelsep {\bfseries Exercise}\hskip \labelsep {\bfseries \theex. #1}]}{\end{trivlist}}
% These packages help with equations, math symbols, etc.
\usepackage{amsmath,amssymb}
% This EXCELLENT resource is your guide to writing equations in LaTeX!
% ftp://ftp.ams.org/pub/tex/doc/amsmath/short-math-guide.pdf
% The following gives us better margins and spacing
\usepackage[margin=1in]{geometry}
\setlength{\topskip}{0in} % No blank space on first page
\setlength{\parindent}{0in} % 0 = no paragraph indentation
\setlength{\parskip}{0.5em} % space between paragraphs
%% remove ligatures (e.g. ``ff") to allow text copying from PDF
\usepackage{microtype}
\DisableLigatures{encoding = *, family = * }
% In case you want to hyperlink anything...
\usepackage[colorlinks=true, urlcolor=blue, anchorcolor=black, linkcolor=black, citecolor=black]{hyperref}
% For more compact numbered lists...
\usepackage{enumitem}
\setlist{noitemsep, topsep=-0.5em}
%% DRAFT watermark across each page:
%\usepackage[printwatermark]{xwatermark}
%\usepackage{xcolor}
%\newwatermark[allpages,color=gray!30,angle=55,scale=6,xpos=0,ypos=0]{DRAFT}
% If you want to include an image
\usepackage{graphicx}
% For fancy code box formatting
\usepackage{listings,xcolor}
\lstset{basicstyle=\footnotesize\ttfamily,breaklines=true}
%multiple columsn
\usepackage{multicol}
\begin{document}
\textbf{\large MATH 420/620 HW \#1 \hfill Due: 12pm on WEDS, 13 Sep 2017} % Replace Due date with your name.
\vspace{1pt}
\hrule
~\\ % this just forces a blank line.
\textbf{Instructions:} A printed copy of your homework is due at \textbf{the start of class}. Supplementary electronic files (e.g. R scripts or wxMaxima files) should be emailed to the instructor prior to class with file name format LASTNAME-HWX.EXT (send multiple files in a single ZIP file).
\begin{ex}[(15 pts)] List three probability distributions (besides the Normal distribution) commonly used in your major field of study, and for each of these describe
\begin{enumerate}
\item[(a)] the physical/real-world processes that lead to each distribution, or otherwise justify their widespread use; \\
\item[(b)] any useful associations between those real-world processes, the parameters of the distribution, and the mean and/or variance (or similar quantities) of that distribution; and \\
\item[(c)] select plausible parameter values (one set for each distribution) and plot the histogram of a large sample and the corresponding density/mass function.
\end{enumerate}
\end{ex}
\begin{ex}[(15 pts) Neutral Allele's under Wright-Fisher.] The Wright-Fisher model is a simplified model of how allele frequencies in a population of $N$ individuals changes from generation to generation. It assumes that, in one time step of the model (i.e., one generation time), the $N$ individuals are replaced by the next generation of $N$ individuals. Assume there are two different types of individuals (e.g., a \textit{normal} type \textbf{A} and a \textit{mutant} type \textbf{B}) and further assume that the mutant type has the same (\textit{neutral}) fitness (i.e., neither type is more or less likely than the other to contribute offspring to the next generation). In this case, the rule for populating the next generation is to randomly sample from the parent population with replacement, which yields a binomially distributed number of type \textbf{B} individuals with probability $n/N$, where $n$ is the number of \textbf{B} individuals in the parent population.
\begin{enumerate}
\item[(a)] Write down the probability of the \textbf{B} type going extinct in the next generation given that $n$ of the $N$ individuals in the parent population are type \textbf{B}.\\
%\item[]\textbf{Solution:}
\item[(b)] How does population size impact extinction probabilities? Plot this probability (y axis) for the following population sizes (x axis) assuming 10\% of the population are mutants, and discuss: $N\in\{10,20,30,40,\ldots,1000\}$.
%\item[]\textbf{Solution:}
\end{enumerate}
\end{ex}
\begin{ex}[(10pts) Mixture Distributions.] ~\\ \begin{enumerate}
\item[(a)] The R code below generates a random sample from a mixture of two Normal distributions. Modify it generate mixture of three (or more) Normals. \\
\item[(b)] The Negative Binomial distribution with rate $r$ and probability $p$ can be viewed as a \textit{compound} distribution (aka a \textit{continuous mixture} distribution) where each observation is drawn from a Poisson whose rate parameter $\lambda$ is not constant, but instead is sampled from a Gamma distribution, which -- to quote the Wikipedia page \url{https://en.wikipedia.org/wiki/Negative_binomial_distribution} as of 5pm on 9/8/2017 -- is parameterized by ``shape = $r$ and scale $\theta = p/(1 - p)$ or correspondingly rate $\beta = (1 - p)/p$". The R code below compares samples from these two distributions using the above parameterization, but there's a problem! \textbf{Correct the code, and explain the source of the error.}
\end{enumerate}
\end{ex}
\begin{lstlisting}[language=R]
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Exercise 3a
rnormix <- function(n, mean1, mean2, sd1, sd2, p1, p2) {
# n = sample size, ps=c(p1, p2) are the mixing probabilities
# means = c(mean1,mean2) and sds = c(sd1,sd2) the distributions.
ps=c(p1,p2)/(p1+p2) # This ensures these sum to 1!
means=c(mean1,mean2)
sds=c(sd1,sd2)
# First pick which distribution each observation comes from...
indx = sample(c(1,2),n,replace=TRUE,prob=ps)
# next give the corresponding vector of means and sds to rnorm()...
return(rnorm(n, mean=means[indx], sd=sds[indx]))
# see ?rnorm for details.
}
# Example:
fakedata = rnormix(5000, mean1=10, mean2=20, sd1=2, sd2=1, p1=0.2, p2=0.8)
hist(fakedata,50)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Exercise 3b
rgampois <- function (n, r, prob) {
# use a different lambda for each observation
# parameterized using scale=p/(1-p) as described on wikipedia
rpois(n, lambda = rgamma(n, shape=r, scale=prob/(1-prob)))
}
r=2
p=0.3
xnegbin <- rnbinom(5000, size = r, prob = p)
xgampois <- rgampois(5000, r, p)
x11() # try quartz() if this doesn't work on your mac!
par(mfrow=c(1,2)) # Two subplots, arranged in 1 row, 2 columns
hist(xnegbin,breaks = 0:max(xnegbin,xgampois))
hist(xgampois,breaks = 0:max(xnegbin,xgampois))
\end{lstlisting}
\end{document}