% Options for packages loaded elsewhere
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
\PassOptionsToPackage{dvipsnames,svgnames,x11names}{xcolor}
%
\documentclass[
  letterpaper,
  DIV=11,
  numbers=noendperiod]{scrartcl}
\usepackage{xcolor}
\usepackage{amsmath,amssymb}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\usepackage{iftex}
\ifPDFTeX
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math} % this also loads fontspec
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
\usepackage{lmodern}
\ifPDFTeX\else
  % xetex/luatex font selection
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
% Make \paragraph and \subparagraph free-standing
\makeatletter
\ifx\paragraph\undefined\else
  \let\oldparagraph\paragraph
  \renewcommand{\paragraph}{
    \@ifstar
      \xxxParagraphStar
      \xxxParagraphNoStar
  }
  \newcommand{\xxxParagraphStar}[1]{\oldparagraph*{#1}\mbox{}}
  \newcommand{\xxxParagraphNoStar}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
  \let\oldsubparagraph\subparagraph
  \renewcommand{\subparagraph}{
    \@ifstar
      \xxxSubParagraphStar
      \xxxSubParagraphNoStar
  }
  \newcommand{\xxxSubParagraphStar}[1]{\oldsubparagraph*{#1}\mbox{}}
  \newcommand{\xxxSubParagraphNoStar}[1]{\oldsubparagraph{#1}\mbox{}}
\fi
\makeatother

\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{241,243,245}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.40,0.45,0.13}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\BuiltInTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\ExtensionTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.28,0.35,0.67}{#1}}
\newcommand{\ImportTok}[1]{\textcolor[rgb]{0.00,0.46,0.62}{#1}}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.07,0.07,0.07}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}

\usepackage{longtable,booktabs,array}
\newcounter{none} % for unnumbered tables
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx}
\makeatletter
\newsavebox\pandoc@box
\newcommand*\pandocbounded[1]{% scales image to fit in text height/width
  \sbox\pandoc@box{#1}%
  \Gscale@div\@tempa{\textheight}{\dimexpr\ht\pandoc@box+\dp\pandoc@box\relax}%
  \Gscale@div\@tempb{\linewidth}{\wd\pandoc@box}%
  \ifdim\@tempb\p@<\@tempa\p@\let\@tempa\@tempb\fi% select the smaller of both
  \ifdim\@tempa\p@<\p@\scalebox{\@tempa}{\usebox\pandoc@box}%
  \else\usebox{\pandoc@box}%
  \fi%
}
% Set default figure placement to htbp
\def\fps@figure{htbp}
\makeatother





\setlength{\emergencystretch}{3em} % prevent overfull lines

\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}



 


\KOMAoption{captions}{tableheading}
\makeatletter
\@ifpackageloaded{caption}{}{\usepackage{caption}}
\AtBeginDocument{%
\ifdefined\contentsname
  \renewcommand*\contentsname{Table of contents}
\else
  \newcommand\contentsname{Table of contents}
\fi
\ifdefined\listfigurename
  \renewcommand*\listfigurename{List of Figures}
\else
  \newcommand\listfigurename{List of Figures}
\fi
\ifdefined\listtablename
  \renewcommand*\listtablename{List of Tables}
\else
  \newcommand\listtablename{List of Tables}
\fi
\ifdefined\figurename
  \renewcommand*\figurename{Figure}
\else
  \newcommand\figurename{Figure}
\fi
\ifdefined\tablename
  \renewcommand*\tablename{Table}
\else
  \newcommand\tablename{Table}
\fi
}
\@ifpackageloaded{float}{}{\usepackage{float}}
\floatstyle{ruled}
\@ifundefined{c@chapter}{\newfloat{codelisting}{h}{lop}}{\newfloat{codelisting}{h}{lop}[chapter]}
\floatname{codelisting}{Listing}
\newcommand*\listoflistings{\listof{codelisting}{List of Listings}}
\makeatother
\makeatletter
\makeatother
\makeatletter
\@ifpackageloaded{caption}{}{\usepackage{caption}}
\@ifpackageloaded{subcaption}{}{\usepackage{subcaption}}
\makeatother
\usepackage{bookmark}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same}
\hypersetup{
  pdftitle={Problem Set 5},
  colorlinks=true,
  linkcolor={blue},
  filecolor={Maroon},
  citecolor={Blue},
  urlcolor={Blue},
  pdfcreator={LaTeX via pandoc}}


\title{Problem Set 5}
\author{}
\date{}
\begin{document}
\maketitle


\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  \textbf{Question Format and Score: Crossover Design}. Three weeks ago
  you took your midterm exam. Unbeknownst to you, the exam was also
  serving as a crossover experiment. There were four versions (A, B, C,
  D) where versions A/C shared one structure and versions B/D shared
  another. The treatment of interest was question \emph{format}: whether
  a question was presented as a single compound sentence or as separate
  sentences/bullet points. Questions 21 and 23 each had a compound and a
  separated version; the two structures (A/C vs B/D) ensured that if Q21
  was compound then Q23 was separated, and vice versa.

  Exam versions were randomly assigned to students in blocks by session
  (morning or afternoon).

  \begin{enumerate}
  \def\labelenumii{\alph{enumii}.}
  \tightlist
  \item
    What are the experimental units in this study? What are the
    measurement units?
  \item
    What is the treatment? How many levels does it have?
  \item
    Why is this a crossover design rather than a simple completely
    randomized design? What is the advantage of each student receiving
    both formats?
  \item
    What is the blocking factor, and why was blocking used?
  \end{enumerate}
\end{enumerate}

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\setcounter{enumi}{1}
\item
  \textbf{Coffee and Reaction Time: Crossover Design}. A researcher
  wants to know whether drinking coffee before a cognitive test improves
  reaction time. She recruits 20 subjects and uses a crossover design:
  each subject takes the cognitive test twice, once after drinking
  coffee and once after drinking decaf (a placebo). The order
  (coffee-first vs decaf-first) is randomly assigned, with a one-week
  washout period between sessions.

  \begin{enumerate}
  \def\labelenumii{\alph{enumii}.}
  \tightlist
  \item
    What is the primary advantage of this crossover design compared to a
    completely randomized design that assigns 10 subjects to coffee and
    10 to decaf?
  \item
    The researcher is concerned about a \emph{carryover effect}:
    caffeine from the first session might still affect performance in
    the second session even after a week. Explain why it is a threat to
    this crossover design.
  \item
    If carryover effects are present and asymmetric (i.e., the carryover
    from coffee to decaf is different from the carryover from decaf to
    coffee), explain why the crossover estimator \(\bar{Z}\) (the
    average of the observed differences) from the previous problem would
    be biased.
  \item
    One way to test for carryover is to compare the \emph{total}
    response (session 1 + session 2) between the two sequence groups.
    Explain the logic behind this test: under no carryover, why should
    the sequence groups have the same expected total?
  \end{enumerate}
\end{enumerate}

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\setcounter{enumi}{2}
\item
  \textbf{Coffee and Reaction Time: Analysis}. The researcher from the
  previous problem ran her crossover experiment and collected data. You
  can generate a synthetic version of her dataset with the following
  code.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(tidyverse)}
\FunctionTok{set.seed}\NormalTok{(}\DecValTok{40}\NormalTok{)}
\NormalTok{n }\OtherTok{\textless{}{-}} \DecValTok{20}
\NormalTok{coffee }\OtherTok{\textless{}{-}} \FunctionTok{tibble}\NormalTok{(}
    \AttributeTok{subject  =} \DecValTok{1}\SpecialCharTok{:}\NormalTok{n,}
    \AttributeTok{sequence =} \FunctionTok{rep}\NormalTok{(}\FunctionTok{c}\NormalTok{(}\StringTok{"coffee\_first"}\NormalTok{, }\StringTok{"decaf\_first"}\NormalTok{), }\AttributeTok{each =}\NormalTok{ n }\SpecialCharTok{/} \DecValTok{2}\NormalTok{),}
    \AttributeTok{subject\_ability =} \FunctionTok{rnorm}\NormalTok{(n, }\AttributeTok{mean =} \DecValTok{250}\NormalTok{, }\AttributeTok{sd =} \DecValTok{30}\NormalTok{)}
\NormalTok{) }\SpecialCharTok{|\textgreater{}}
    \FunctionTok{mutate}\NormalTok{(}
        \AttributeTok{period1 =} \FunctionTok{case\_when}\NormalTok{(}
\NormalTok{            sequence }\SpecialCharTok{==} \StringTok{"coffee\_first"} \SpecialCharTok{\textasciitilde{}}\NormalTok{ subject\_ability }\SpecialCharTok{{-}} \DecValTok{8} \SpecialCharTok{+} \FunctionTok{rnorm}\NormalTok{(n, }\DecValTok{0}\NormalTok{, }\DecValTok{10}\NormalTok{),}
\NormalTok{            sequence }\SpecialCharTok{==} \StringTok{"decaf\_first"}  \SpecialCharTok{\textasciitilde{}}\NormalTok{ subject\_ability }\SpecialCharTok{+} \FunctionTok{rnorm}\NormalTok{(n, }\DecValTok{0}\NormalTok{, }\DecValTok{10}\NormalTok{)}
\NormalTok{        ),}
        \AttributeTok{period2 =} \FunctionTok{case\_when}\NormalTok{(}
\NormalTok{            sequence }\SpecialCharTok{==} \StringTok{"coffee\_first"} \SpecialCharTok{\textasciitilde{}}\NormalTok{ subject\_ability }\SpecialCharTok{+} \DecValTok{5} \SpecialCharTok{+} \FunctionTok{rnorm}\NormalTok{(n, }\DecValTok{0}\NormalTok{, }\DecValTok{10}\NormalTok{),}
\NormalTok{            sequence }\SpecialCharTok{==} \StringTok{"decaf\_first"}  \SpecialCharTok{\textasciitilde{}}\NormalTok{ subject\_ability }\SpecialCharTok{{-}} \DecValTok{8} \SpecialCharTok{+} \DecValTok{5} \SpecialCharTok{+} \FunctionTok{rnorm}\NormalTok{(n, }\DecValTok{0}\NormalTok{, }\DecValTok{10}\NormalTok{)}
\NormalTok{        )}
\NormalTok{    ) }\SpecialCharTok{|\textgreater{}}
    \FunctionTok{select}\NormalTok{(subject, sequence, period1, period2)}
\end{Highlighting}
\end{Shaded}

  Here, \texttt{period1} and \texttt{period2} are reaction times (in
  milliseconds) on the cognitive test in session 1 and session 2
  respectively. We'll denote these \(Y_{i1}\) and \(Y_{i2}\) for subject
  \(i\). Lower is better. The data-generating process includes a true
  treatment effect of coffee (coffee lowers reaction time by 8 ms) and a
  period effect (reaction time increases by 5 ms in period 2, perhaps
  due to fatigue or reduced novelty). There is no carryover effect.

  \begin{enumerate}
  \def\labelenumii{\alph{enumii}.}
  \item
    For each subject, compute \(Z_i\), the within-subject difference in
    reaction time (decaf \(-\) coffee). Be careful: which period
    corresponds to the coffee score depends on the subject's sequence.
    Add this column to the data frame and compute \(\bar{Z}\).
  \item
    Conduct a randomization test of the sharp null hypothesis that
    coffee has no effect on any subject's reaction time.

    \begin{enumerate}
    \def\labelenumiii{\roman{enumiii}.}
    \tightlist
    \item
      State the null hypothesis in terms of potential outcomes.
    \item
      Simulate 1,000 test statistics under the null by re-randomizing
      the sequence assignment (a complete randomization of 10 to each
      sequence).
    \item
      Plot the null distribution with the observed statistic and report
      the two-sided p-value.
    \end{enumerate}
  \item
    Reshape the data into long format (one row per subject per period)
    and fit a linear model with fixed effects for subject:

    \[Y_{ij} = \beta_0 + \beta_1 \text{treatment}_{ij} + \beta_2 \text{period}_{ij} + \alpha_i + \varepsilon_{ij}\]

    where \(\alpha_i\) is a fixed effect for subject \(i\). Report and
    interpret \(\hat{\beta}_1\). Compare this with the result from the
    randomization test.
  \item
    Now fit a simpler model that omits the period effect:
    \(Y_{ij} = \beta_0 + \beta_1 \text{treatment}_{ij} + \alpha_i + \varepsilon_{ij}\).
    How does the estimate of the treatment effect change? Explain why
    the crossover design protects the treatment effect estimate from
    period effects even when period is not included in the model.
  \end{enumerate}
\end{enumerate}

\newpage{}

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\setcounter{enumi}{3}
\item
  \textbf{Sequential Probability Ratio Test: Tinkering with Parameters}.
  The SPRT is a powerful tool, but its performance depends on the choice
  of parameters. In this exercise, we will explore how changing the
  parameters \(p_0\), \(p_1\), \(\alpha\), and \(\beta\) affects the
  behavior of the test that was shown in class\footnote{See the slides
    for Sequential Analysis I for code to simulate the performance of
    the SPRT.}.

  \begin{enumerate}
  \def\labelenumii{\alph{enumii}.}
  \item
    Using the same simulation of a single run that was shown in class,
    lower both the error rates (\(\alpha\) and \(\beta\)) to represent a
    more stringent test. Plot the same simulated run with the new
    thresholds. How does the decision process change with the new
    thresholds? Does it take more or fewer samples to reach a decision?
  \item
    Choose a fixed value for \(p_0\) (e.g., 0.5) and vary \(p_1\) (e.g.,
    0.6, 0.7, 0.8). For each value of \(p_1\), use a full simulation to
    calculate the expected number of samples needed to reach a decision
    under both hypotheses.
  \item
    Now, fix \(p_1\) (e.g., 0.7) and vary \(p_0\) (e.g., 0.5, 0.4, 0.3).
    Again, calculate the expected number of samples needed for each
    scenario.
  \item
    Summarize your findings. How do the choices of \(p_0\) and \(p_1\)
    affect the efficiency of the SPRT? What trade-offs do you observe
    when adjusting the error rates \(\alpha\) and \(\beta\)?
  \end{enumerate}
\end{enumerate}

\newpage{}

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\setcounter{enumi}{4}
\item
  \textbf{Adaptive Assignment: Implementing UCB}. A pharmaceutical
  company is running a clinical trial comparing two pain relief drugs.
  Each patient reports a pain relief score on a continuous scale from 0
  to 1 (higher is better). The company wants to use an adaptive
  assignment strategy that balances learning which drug is better with
  assigning more patients to the better-performing drug.

  The following code generates potential outcomes for \(N = 30\)
  subjects under both treatments.

\begin{Shaded}
\begin{Highlighting}[]
\FunctionTok{library}\NormalTok{(tidyverse)}
\FunctionTok{set.seed}\NormalTok{(}\DecValTok{42}\NormalTok{)}
\NormalTok{N }\OtherTok{\textless{}{-}} \DecValTok{30}
\NormalTok{po }\OtherTok{\textless{}{-}} \FunctionTok{tibble}\NormalTok{(}
  \AttributeTok{subject =} \DecValTok{1}\SpecialCharTok{:}\NormalTok{N,}
  \AttributeTok{Y\_A =} \FunctionTok{rbeta}\NormalTok{(N, }\DecValTok{4}\NormalTok{, }\DecValTok{6}\NormalTok{),}
  \AttributeTok{Y\_B =} \FunctionTok{rbeta}\NormalTok{(N, }\DecValTok{5}\NormalTok{, }\DecValTok{5}\NormalTok{)}
\NormalTok{)}
\end{Highlighting}
\end{Shaded}

  Recall the UCB algorithm with Hoeffding's Inequality. The upper
  confidence bound for arm \(j\) at time \(t\) is:

  \[
  U_j = \hat{\mu}_j + \sqrt{\frac{\ln(t)}{2\, n_j}}
  \]

  where \(\hat{\mu}_j\) is the sample mean of responses observed so far
  for arm \(j\), \(n_j\) is the number of subjects assigned to arm \(j\)
  so far, and \(t\) is the index of the current subject. At each step,
  the algorithm assigns the next subject to the arm with the highest
  \(U_j\).

  \begin{enumerate}
  \def\labelenumii{\alph{enumii}.}
  \tightlist
  \item
    Write code to implement the UCB algorithm on these potential
    outcomes. Initialize by assigning the first subject to arm A and the
    second to arm B, then use the UCB rule for subjects 3 through
    30\footnote{Start off by simply writing the code to calculate
      \(U_j\) after observing the first two subjects, and then assign
      the third subject accordingly. Next, wrap that logic in a loop to
      handle all subjects up to 30.}. Store the results in a data frame
    with columns for the subject index, assignment, and observed
    response.
  \end{enumerate}

  \begin{enumerate}
  \def\labelenumii{\alph{enumii}.}
  \setcounter{enumii}{1}
  \item
    Create a plot of the cumulative fraction of subjects assigned to arm
    B (\(n_B / t\)) as a function of the subject index \(t\). Describe
    what you see: when is the algorithm exploring (assigning roughly
    evenly) and when is it exploiting (favoring one arm)?
  \item
    For a single time step of your choosing between \(t = 5\) and
    \(t = 10\), report the values of \(\hat{\mu}_A\), \(\hat{\mu}_B\),
    the addition to the mean (aka the Hoeffding bonus), and the
    resulting \(U_j\). Verify that the algorithm's assignment matches
    the arm with the higher \(U_j\). What role does the bonus term play
    when one arm has been sampled much less than the other?
  \item
    Compute the \emph{total regret} of the UCB algorithm on these 30
    subjects and compare it with the expected total regret of a balanced
    completely randomized design that assigns 15 subjects to each
    arm\footnote{To compute the expected total regret of a balanced CR
      design, you can simulate many random assignments of 15 subjects to
      each arm, compute the total regret for each simulation, and then
      take the average.}. The per-subject regret is
    \(\max(Y_i(A), Y_i(B)) - Y_i(d_i)\), where \(d_i\) is the arm
    actually assigned. Does the UCB algorithm achieve lower total regret
    than what we would expect had we used a balanced CR design?
  \end{enumerate}
\end{enumerate}




\end{document}
