From b8b18597916f682e1861bda4f3f2e245d951e755 Mon Sep 17 00:00:00 2001 From: Nathan TeBlunthuis Date: Fri, 15 May 2020 08:47:16 -0700 Subject: [PATCH] add example homework assignment using latex --- .../latex_homework_assignment/HW1.Rnw | 228 +++++++++++++++++ .../latex_homework_assignment/HW1Code.R | 34 +++ .../latex_homework_assignment/Makefile | 31 +++ .../knitr_data.RData | Bin 0 -> 63 bytes .../latex_homework_assignment/mako-mem.sty | 232 ++++++++++++++++++ .../refs-processed.bib | 0 6 files changed, 525 insertions(+) create mode 100644 knitr_examples/latex_homework_assignment/HW1.Rnw create mode 100644 knitr_examples/latex_homework_assignment/HW1Code.R create mode 100644 knitr_examples/latex_homework_assignment/Makefile create mode 100644 knitr_examples/latex_homework_assignment/knitr_data.RData create mode 100644 knitr_examples/latex_homework_assignment/mako-mem.sty create mode 100644 knitr_examples/latex_homework_assignment/refs-processed.bib diff --git a/knitr_examples/latex_homework_assignment/HW1.Rnw b/knitr_examples/latex_homework_assignment/HW1.Rnw new file mode 100644 index 0000000..357a12e --- /dev/null +++ b/knitr_examples/latex_homework_assignment/HW1.Rnw @@ -0,0 +1,228 @@ +% based on mako's mod +\documentclass[12pt]{memoir} + +% based on kieran healy's memoir modifications +\usepackage{mako-mem} +\chapterstyle{article-3} +\pagestyle{memo} +\usepackage{wrapfig} +\usepackage{ucs} +\usepackage[utf8x]{inputenc} + +<>= +knit_hooks$set(document = function(x) { + sub('\\usepackage[]{color}', +'\\usepackage[usenames,dvipsnames]{color}', x, fixed = TRUE) +}) + +bold <- function(x) {paste('{\\textbf{',x,'}}', sep ='')} +gray <- function(x) {paste('{\\textcolor{gray}{',x,'}}', sep ='')} +wrapify <- function (x) {paste("{", x, "}", sep="")} + +load("knitr_data.RData") +attach(r) +f <- function (x) {formatC(x, format="d", big.mark=',')} +format.percent <- function(x) {paste(f(x*100),"\\%",sep='')} +format.day.ordinal <- function(x) { + day <- format(x,format="%d") + daylast <- substr(day,nchar(day),nchar(day)) + dayfirst <- substr(day,1,1) + if(dayfirst == '0') + day = daylast + + if( daylast == "1") + day <- paste0(day,"st") + else if(daylast == "2") + day <- paste0(day,"nd") + else if (daylast == "3") + day <- paste0(day,"rd") + else + day <- paste0(day,"th") + + return(day) +} +format.month <- function(x){ + return( format(x,format='%B %Y')) +} +format.date <- function(x) +{ + return(paste(format(x,format = '%B'),format.day.ordinal(x),format(x,format='%Y'),sep=' ')) +} +@ + +\usepackage[T1]{fontenc} +\usepackage{textcomp} +\usepackage[garamond]{mathdesign} + +\usepackage[letterpaper,left=1.65in,right=1.65in,top=1.3in,bottom=1.2in]{geometry} + +% packages i use in essentially every document +\usepackage{graphicx} +\newsubfloat{figure} +\usepackage{enumerate} + +% packages i use in many documents but leave off by default +\usepackage{amsmath, amsthm, amssymb} +% \usepackage{dcolumn} +% \usepackage{endfloat} + +% import and customize urls +\usepackage[breaklinks]{hyperref} +\hypersetup{colorlinks=true, linkcolor=Black, citecolor=Black, filecolor=Blue, + urlcolor=Blue, unicode=true} + +% add bibliographic stuff +\usepackage[round]{natbib} +\def\citepos#1{\citeauthor{#1}'s (\citeyear{#1})} +\def\citespos#1{\citeauthor{#1}' (\citeyear{#1})} + +% memoir function to take out of the space out of the whitespace lists +\firmlists + +\everymath{\displaymode} +\begin{document} + +\setlength{\parskip}{4.5pt} +\baselineskip 16pt + +\title{STAT 544 HW1} +\author{Nathan TeBlunthuis\\ + \href{mailto:nathante@uw.edu}{nathante@uw.edu}} + +\maketitle + +\subsubsection{1.a} +\[u^{STOP}(s) = + \begin{cases} + s & \quad s <= S \\ + 0 & \quad \mathrm{otherwise} + \end{cases} +\] +This is given in the problem statement. + + \subsubsection{1.b} +\newcommand{\ugoon}[1]{\overline{u}^{*GO~ON}(#1)} +\newcommand{\ubarstar}[1]{\overline{u}^*(#1)} +\newcommand{\ustop}[1]{u^{STOP}(#1)} +\newcommand{\ustareq}[1]{max \left \{\ugoon{#1}, \ustop{#1} \right\}} +\[ + u^*(s) = \ustareq{s} +\] +\[ + \ugoon{s} = \sum_{x = 1}^{R}{\ubarstar{s+x}p(x)} = \frac{1}{R}\sum_{x=1}^{R}\ubarstar{s+x} +\] +\[\ugoon{S} = \frac{1}{R}\sum_{x=1}^{R}\ubarstar{S+x} = \frac{1}{R}\sum_{x=1}^R0 = 0\] + +Since \(S + x > S\) and \(u(s) = 0\) for all \(s > S\). + +Therefore \[\ubarstar{S} = \ustareq{S} = max\left\{0,S\right\} = S\] + +\subsubsection{1.c} +Code below: +<<<1.c,echo=TRUE, fig.height=4,fig.width=6,fig.cap="The expected utility of following the optimal strategy is stricly greater than the value of stopping until it is possible that \\(S < s\\) after the next roll. The expected utility is greater when the odds of having an additional safe roll are greater." >>= +R <- 6; +S <- 20; + +# use memoization to speed up dynamic programming +u.star <- function(s){ + memo <<- array(dim=S+R) + u.star.step(s) +} + +u.star.step <- function(s){ + if(is.na(memo[[s+1]])){ + memo[[s+1]] <<- u.star.comp(s) + } + memo[[s+1]] +} + +u.star.comp <- function(s){ + if (s > S) return(0); + if (s == S) return(s); + go.on <- sum(sapply(1:R,function(x) u.star.step(s+x)))/R; + max(s, go.on) +} + +library(ggplot2) +df <- data.frame(x=0:S,y=sapply(0:S,u.star)) +p <- ggplot(df,aes(x=x,y=y)) +p <- p + geom_line(aes(linetype='max expected')) +p <- p + geom_point() +p <- p + scale_y_continuous('utility',limits=c(0,S)) +p <- p + scale_x_continuous('s') + theme_minimal() +p <- p + geom_abline(aes(linetype='always stop', slope=1,intercept=0)) +p <- p + scale_linetype_manual(name='',values=c("always stop"='dashed', + "max expected"='solid')) +print(p) +@ + +\clearpage + +\subsubsection{1.d} + +\newcommand{\sumxr}[1]{\frac{1}{R}\sum_{x=1}^R{#1}} +\newcommand{\sumxrest}[1]{\frac{1}{R}\sum_{x=1}^{R - (S -s)}{#1}} + +\[\mathrm{Let}~ s < S - R ~\mathrm{then}~ s + x \le S ~\mathrm{for}~ x \in \{1,...R\}. \] + +\[\mathrm{So}~ \ubarstar{s+x} = \mathrm{max}\{s + x, \sumxr{\ubarstar{s + x}}\}\] + +\[\mathrm{Since~we~know}~ \(s + x > s\), \(\ubarstar{s} = \sumxr{\ubarstar{s+x}} = \ugoon{s}\]. + +\[\mathrm{So~if}~ \(s < S - R\), \(\pi^*(s) = GO~ON\]. + +We can see that \(\ubarstar{s} \ge s\) since our decisions are \(GO~ON\) which implies that \(\ubarstar{s} \ge u^{STOP}(s)\) and \(STOP\) which yields \(\ubarstar{s}={s}\). + +\subsubsection{2.a} +\newcommand{\piof}[1]{_{\pi({#1})}} +\newcommand{\pistarof}[1]{_{\pi^*({#1})}} + +Prove that if \(X_1,...X_n\) is \mathit{n}-exchangeable, then \(X_1,...X_m\) is m-exchangeable for any \(m < n\). + +\[P(X_1,...X_n) = P(X\piof{1},...X\piof{2}) ~\mbox{for all}~ \pi \in \mathbb{S}_n\] + +Choose a permutation \[\pi \in \mathbb{S}_n ~\mbox{such that for all}~ i > m ~\mbox{,}~ \pi(i) = i\]. Then \[P(X\piof{1},...X\piof{m} | X\piof{m+1},...X\piof{n}) = P(X\piof{1},...X\piof{m} | X_{m+1},...X_n) = \frac{P(X\piof{1},...X\piof{m})}{P(X_{m+1},...X_n)}\]. + +And, since \(X_1,...X_n\) is exchangeable, \[P(X\piof{1},...X\piof{m} | X\piof{m+1},...X\piof{n}) = P(X_1,...X_m | X_{m+1},...X_n) \quad = \quad \frac{P(X_1,...X_m)}{P(X_{m+1},...X_n)}\] +Therefore +\[ P(X_1,...X_m) = P(X\piof{1},...X\piof{m})\] + +Which means that \(X_{1},...X_{m}\) is exchangeable. +\subsubsection{2.b} + +To show that if \(X_{1},...X_{n}\) is exchangeable, then \(P(X_1) = P(X_m)\) for all \(m \le n\), \[\mbox{choose a permutation}~\pi\) ~\mbox{such that}~ \pi_1 = 1,~ \pi_2 = m\] + +\[\mbox{Note that}~X\piof{1},...X\piof{n}~\mbox{is exchangeable. Therefore}~ X\piof{1},X\piof{2} \mbox{is exchangeable, by 2.a.} \] + +So we have \[P(X_1,X_m) = P(X_m,X_1) \rightarrow P(X_1|X_m) = P(X_m|X_1) \rightarrow \frac{P(X_1)}{P(X_m)}=\frac{P(X_m)}{P(X_1)}}\] +\[\rightarrow P(X_1)P(X_1)=P(X_m)P(X_m) \] +So since \(P(X_1),P(X_m)>0\), \[P(X_1) = P(X_m)\] + +\subsubsection{2.c} +\newcommand{\sn}{X_1+...X_n} +\newcommand{\sN}{X_1+...X_N} +\newcommand{\spin}{X_{\pi(1)}+...X_{\pi(n)}} +\newcommand{\sumn}[1]{\sum_{N_1=n_1}^{n_1+N-n}{#1}} +\newcommand{\crhs}{\sumn{P[X_1+ ... X_N = N_1] {N_1 \choose n_1} {N-N_1 \choose n-n_1} / {N \choose n}}} + +To show that \[P[\sn=n_1]=\crhs\] + +See that there are \(n \choose n_1\) ways that \(\sn=n_1\). + +By exchangeability, for any permutation \(\pi\) of \(N\) such that \(\spin=n_1\). + +\[P(\sn=n_1)=\({n \choose n_1}\)p(\spin)\] + +The number of permutations \(\pi\) of \(N\) such that \(\spin=n_1\) depends on \(\sN\). We know \(\sN \ge n_1\) otherwise \(\sn < \sN\). Also \(\sN \le n_1+ N - n\) since if \(\sN > n_1 + N - n \) then \(N - (\sN) \le n - n_1\) which would mean that there are more zeros in the sequence \(X_1,...X_n\) than in the sequence \(X_1,...X_N\). +Therefore + +\[P(\sn=n_1) = \sumn{P(\sn=n_1 | \sN=N_1)P(\sN=N_1)}\] + +There \(N \choose n\) ways to pick a permutation of N giving the sequence \(X_1,...X_n\). Of these, there are \(N_1 \choose n_1\) ways of choosing the 1's and \({N-N_1 \choose n-n_1}\) ways of choosing the 0s such that \(\sn=n_1\). Therefore +\[P(\sn=n_1 | \sN=N_1) = {N_1 \choose n_1} {N-N_1 \choose n-n_1} / {N \choose n}\] + +and we have \[P[\sn=n_1]=\crhs\]. + +\end{document} + + diff --git a/knitr_examples/latex_homework_assignment/HW1Code.R b/knitr_examples/latex_homework_assignment/HW1Code.R new file mode 100644 index 0000000..0d3a97e --- /dev/null +++ b/knitr_examples/latex_homework_assignment/HW1Code.R @@ -0,0 +1,34 @@ +R <- 6; +S <- 20; + +# use memoization to speed up dynamic programming +u.star <- function(s){ + memo <<- array(dim=S+R) + u.star.step(s) +} + +u.star.step <- function(s){ + if(is.na(memo[[s+1]])){ + memo[[s+1]] <<- u.star.comp(s) + } + memo[[s+1]] +} + +u.star.comp <- function(s){ + if (s > S) return(0); + if (s == S) return(s); + go.on <- sum(sapply(1:R,function(x) u.star.step(s+x)))/R; + max(s, go.on) +} + +library(ggplot2) +df <- data.frame(x=0:S,y=sapply(0:S,u.star)) +p <- ggplot(df,aes(x=x,y=y)) +p <- p + geom_line(aes(linetype='max expected')) +p <- p + geom_point() +p <- p + scale_y_continuous('utility',limits=c(0,S)) +p <- p + scale_x_continuous('s') + theme_minimal() +p <- p + geom_abline(aes(linetype='always stop', slope=1,intercept=0)) +p <- p + scale_linetype_manual(name='',values=c("always stop"='dashed', + "max expected"='solid')) +print(p) diff --git a/knitr_examples/latex_homework_assignment/Makefile b/knitr_examples/latex_homework_assignment/Makefile new file mode 100644 index 0000000..d52e21b --- /dev/null +++ b/knitr_examples/latex_homework_assignment/Makefile @@ -0,0 +1,31 @@ +#!/usr/bin/make + +all: $(patsubst %.Rnw,%.pdf,$(wildcard *.Rnw)) +pdf: all + +refs-processed.bib: + perl -p -e 's/©//' refs.bib > refs-processed.bib + perl -0pe 's/,\s+(file|abstract|note) = \{.*?\}(,\n|\n)/\2/sg' refs-processed.bib |sponge refs-processed.bib + recode -d u8..ltex < refs-processed.bib | sponge refs-processed.bib + +%.tex: %.Rnw + Rscript -e "library(knitr); knit('$<')" + +%.pdf: %.tex refs-processed.bib + latexmk -f -pdf -quiet $< + +clean: + latexmk -f -pdf -quiet -c *.tex + rm -f *.tex + rm -f *.tmp + rm -f vc + rm -f refs-processed.bib + +viewpdf: all + evince *.pdf + +spell: + aspell -c -t --tex-check-comments -b text.tex + +.PHONY: clean all +.PRECIOUS: %.tex all diff --git a/knitr_examples/latex_homework_assignment/knitr_data.RData b/knitr_examples/latex_homework_assignment/knitr_data.RData new file mode 100644 index 0000000000000000000000000000000000000000..9618f00f58af5f2fe052749a7adef12e8945f7a7 GIT binary patch literal 63 zcmb2|=3oE=X6~X+gGXHtk`fXUk`mIA(ioK3*u>Nn4xTu0=m6)U?uHfvW{CzBiT}z! Pc>Xg;d*1)z0MrKnT@w`C literal 0 HcmV?d00001 diff --git a/knitr_examples/latex_homework_assignment/mako-mem.sty b/knitr_examples/latex_homework_assignment/mako-mem.sty new file mode 100644 index 0000000..5f80e40 --- /dev/null +++ b/knitr_examples/latex_homework_assignment/mako-mem.sty @@ -0,0 +1,232 @@ +% Some article styles and page layout tweaks for the LaTeX Memoir class. +% +% Copyright 2009 Benjamin Mako Hill +% Copyright 2008-2009 Kieran Healy + +% Distributed as free software under the GNU GPL v3 + +% This file is heavily based on one by Kieran Healy +% available here: http://github.com/kjhealy/latex-custom-kjh/ + +\usepackage{lastpage} + +% blank footnote +% Use \symbolfootnote[0]{Footnote text} for a blank footnote. +% Useful for initial acknowledgment note. +\long\def\symbolfootnote[#1]#2{\begingroup% +\def\thefootnote{\fnsymbol{footnote}}\footnote[#1]{#2}\endgroup} + +% put a period after the section numbers +\setsecnumformat{\csname the#1\endcsname.\enspace} + +% >> article-1 << +\makechapterstyle{article-1}{ + \renewcommand{\rmdefault}{ugm} + \renewcommand{\sfdefault}{phv} + + \setsecheadstyle{\large\scshape} + \setsubsecheadstyle{\normalsize\itshape} + \renewcommand{\printchaptername}{} + \renewcommand{\chapternamenum}{} + \renewcommand{\chapnumfont}{\chaptitlefont} + \renewcommand{\printchapternum}{\chapnumfont \thechapter\space} + \renewcommand{\afterchapternum}{} + \renewcommand{\printchaptername}{\secheadstyle} + \renewcommand{\cftchapterfont}{\normalfont} + \renewcommand{\cftchapterpagefont}{\normalfont\scshape} + \renewcommand{\cftchapterpresnum}{\scshape} + \captiontitlefont{\small} + + % turn off chapter numbering + \counterwithout{section}{chapter} + \counterwithout{figure}{chapter} + \counterwithout{table}{chapter} + + % reduce skip after section heading + \setaftersecskip{1.2ex} + + \pretitle{\newline\centering \LARGE\scshape \MakeLowercase } + \posttitle{\par\vskip 1em} + \predate{\footnotesize \centering} + \postdate{\par\vskip 1em} + + % 'abstract' title, bigger skip from title + \renewcommand{\abstractname}{} + \abstractrunin + +% set name of bibliography to 'references' +\renewcommand{\bibname}{References} +} + +% >> article-2 << +\makechapterstyle{article-2}{ + \renewcommand{\rmdefault}{ugm} + \renewcommand{\sfdefault}{phv} + + \setsecheadstyle{\large\scshape} + \setsubsecheadstyle{\normalsize\itshape} + \setaftersubsubsecskip{-1em} + \setsubsubsecheadstyle{\bfseries} + \renewcommand{\printchaptername}{} + \renewcommand{\chapternamenum}{} + \renewcommand{\chapnumfont}{\chaptitlefont} + \renewcommand{\printchapternum}{\chapnumfont \thechapter\space} + \renewcommand{\afterchapternum}{} + \renewcommand{\printchaptername}{\secheadstyle} + \renewcommand{\cftchapterfont}{\normalfont} + \renewcommand{\cftchapterpagefont}{\normalfont\scshape} + \renewcommand{\cftchapterpresnum}{\scshape} + \captiontitlefont{\small} + + % turn off chapter numbering + \counterwithout{section}{chapter} + \counterwithout{figure}{chapter} + \counterwithout{table}{chapter} + + % supress chapter numbers + \maxsecnumdepth{chapter} + \setsecnumdepth{chapter} + + % for numbered sections and subsections: + % (a) comment out the above stanza; (b) uncomment the one below + % \maxsecnumdepth{subsection} + % \setsecnumdepth{subsection} + + % reduce skip after section heading + \setaftersecskip{1.7ex} + + % Title flush left + \pretitle{\flushleft\LARGE \itshape} + \posttitle{\par\vskip 0.5em} + \preauthor{\flushleft \large \lineskip 1em} + \postauthor{\par\lineskip 1em} + \predate{\flushleft\footnotesize\vspace{0.65em}} + \postdate{\par\vskip 1em} + + % 'abstract' title, bigger skip from title + \renewcommand{\abstractname}{Abstract:} + \renewcommand{\abstractnamefont}{\normalfont\small\bfseries} + \renewcommand{\abstracttextfont}{\normalfont\small} + \setlength{\absparindent}{0em} + \setlength{\abstitleskip}{-1.5em} + \abstractrunin + + % set name of bibliography to 'references' + \renewcommand{\bibname}{References} +} + + +% >> article-3 << +\makechapterstyle{article-3}{ + \renewcommand{\rmdefault}{ugm} + \renewcommand{\sfdefault}{phv} + + \setsecheadstyle{\large\sffamily\bfseries\MakeUppercase} + \setsubsecheadstyle{\normalsize\itshape} + \setaftersubsubsecskip{-1em} + \setsubsubsecheadstyle{\small\bfseries} + \renewcommand{\printchaptername}{} + \renewcommand{\chapternamenum}{} + \renewcommand{\chapnumfont}{\chaptitlefont} + \renewcommand{\printchapternum}{\chapnumfont \thechapter\space} + \renewcommand{\afterchapternum}{} + \renewcommand{\printchaptername}{\secheadstyle} + \renewcommand{\cftchapterfont}{\normalfont} + \renewcommand{\cftchapterpagefont}{\normalfont\scshape} + \renewcommand{\cftchapterpresnum}{\scshape} + \captiontitlefont{\small} + + % turn off chapter numbering + \counterwithout{section}{chapter} + \counterwithout{figure}{chapter} + \counterwithout{table}{chapter} + + % supress chapter numbers + \maxsecnumdepth{chapter} + \setsecnumdepth{chapter} + + % reduce skip after section heading + \setaftersecskip{1pt} + \setbeforesecskip{-1em} + + % 'abstract' title, bigger skip from title + % \renewcommand{\maketitle}{\{\preauthor \theauthor\} \hfill \thetitle} + \renewcommand{\maketitle}{ + {\Large\sffamily\bfseries\MakeUppercase\thetitle} \hfill + {\Large\sffamily\MakeUppercase\theauthor} + \vskip 0.7em} + \renewcommand{\abstractname}{\normalfont\scriptsize\noindent} + \renewcommand{\abstracttextfont}{\normalfont\scriptsize} + \abstractrunin + + % set name of bibliography to 'references' + \renewcommand{\bibname}{References} + + \parindent 0pt + +} + +%%% Custom styles for headers and footers +%%% Basic +\makepagestyle{mako-mem} +%\makeevenfoot{mako-mem}{\thepage}{}{} +%\makeoddfoot{mako-mem}{}{}{\thepage} +%\makeheadrule{mako-mem}{\textwidth}{\normalrulethickness} +\newcommand{\@makomarks}{% + \let\@mkboth\markboth + \def\chaptermark##1{% + \markboth{% + \ifnum \c@secnumdepth >\m@ne + \if@mainmatter + \thechapter. \ % + \fi + \fi + ##1}{}} + \def\sectionmark##1{% + \markright{##1}} +} +\makepsmarks{mako-mem}{\@makomarks} +\makepsmarks{mako-mem}{} +\makeevenhead{mako-mem}{}{}{\scshape\thepage} +\makeoddhead{mako-mem}{}{}{\scshape\thepage} + +%%% version control info in footers; requires vc package +% Make the style for vc-git revision control headers and footers +\makepagestyle{mako-mem-git} +\newcommand{\@gitmarks}{% + \let\@mkboth\markboth + \def\chaptermark##1{% + \markboth{% + \ifnum \c@secnumdepth >\m@ne + \if@mainmatter + \thechapter. \ % + \fi + \fi + ##1}{}} + \def\sectionmark##1{% + \markright{##1}} +} +\makepsmarks{mako-mem-git}{\@gitmarks} +\makeevenhead{mako-mem-git}{}{}{\scshape\thepage} +\makeoddhead{mako-mem-git}{}{}{\scshape\thepage} +\makeevenfoot{mako-mem-git}{}{\texttt{\footnotesize{\textcolor{Blue}{git revision \VCRevision\ on \VCDateTEX}}}}{} +\makeoddfoot{mako-mem-git}{}{\texttt{\footnotesize \textcolor{Blue}{git revision \VCRevision\ on \VCDateTEX}}}{} + +%% Create a command to make a note at the top of the first page describing the +%% publication status of the paper. +\newcommand{\published}[1]{% + \gdef\puB{#1}} + \newcommand{\puB}{} + \renewcommand{\maketitlehooka}{% + \par\noindent\footnotesize \puB} + +\makepagestyle{memo} +\makeevenhead{memo}{}{}{} +\makeoddhead{memo}{}{}{} + +\makeevenfoot{memo}{}{\scshape \thepage/\pageref{LastPage}}{} +\makeoddfoot{memo}{}{\scshape \thepage/\pageref{LastPage}}{} + + +\endinput + diff --git a/knitr_examples/latex_homework_assignment/refs-processed.bib b/knitr_examples/latex_homework_assignment/refs-processed.bib new file mode 100644 index 0000000..e69de29