\DocumentMetadata{
  lang = en-US,
  pdfstandard = ua-2,
  pdfversion = 2.0,
  testphase = {latest}
}

\documentclass[12pt]{article}

\tagpdfsetup{math/alt/use=true}

% -------------------------
% Course / assignment metadata
% Edit these once per assignment
% -------------------------
\newcommand{\coursecode}{EE 520}
\newcommand{\coursetitle}{Random Processes}
\newcommand{\quartername}{Fall 2026}
\newcommand{\assignmenttype}{Homework}
\newcommand{\assignmentnumber}{5}
\newcommand{\assignmenttitle}{Detection and learning a probability}
\newcommand{\duedate}{November 20, 2026}
\newcommand{\studentname}{}
\newcommand{\instructorname}{John Lipor}

% Derived metadata strings
\newcommand{\fullcoursename}{\coursecode: \coursetitle}
\newcommand{\assignmentname}{\assignmenttype\ \assignmentnumber}
\newcommand{\fullassignmentname}{\assignmentname\ifx\assignmenttitle\empty\else: \assignmenttitle\fi}

% -------------------------
% Packages
% -------------------------
\usepackage[english]{babel}
\usepackage[margin=1in]{geometry}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{microtype}
\usepackage{xcolor}
\usepackage{xparse}
\usepackage{etoolbox}
\usepackage{amsmath,amssymb,amsthm,amsfonts}
\usepackage{bbm}
\usepackage{graphicx}
\usepackage{algorithm}
\usepackage{algorithmic}
% \usepackage{enumitem}
\usepackage{titlesec}

% Links and PDF metadata
\usepackage[colorlinks=true,linkcolor=blue,urlcolor=blue,citecolor=blue]{hyperref}
\usepackage{bookmark}
\urlstyle{same}
\hypersetup{
  pdftitle={\coursecode\ \fullassignmentname},
  pdfauthor={\instructorname},
  pdfsubject={\fullcoursename\ assignment},
  pdfcreator={LaTeX},
  pdfproducer={LaTeX with hyperref},
  pdfdisplaydoctitle=true
}

% Improve math environment tagging
\usepackage{tagpdf}
\tagpdfsetup{activate-all,uncompress}

% Header command
\newcommand{\makehomeworkheader}{%
  \thispagestyle{plain}
  \begingroup
  \setlength{\fboxsep}{8pt}
  \noindent\fbox{%
    \begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}

      \noindent\textbf{\fullcoursename}\hfill \quartername\par

      \begin{center}
        {\LARGE \assignmentname\par}
        \ifx\assignmenttitle\empty\else
          {\large \assignmenttitle\par}
        \fi
        {\large Due: \duedate, 11:59 PM PT\par}
      \end{center}

      \noindent\textit{Student Name: \studentname}\hfill \textit{Instructor: \instructorname}\par
    \end{minipage}%
  }
  \endgroup
  \vspace{4mm}
}

% Section heading format used by \problem
\titleformat{\section}
  {\normalfont\normalsize\bfseries}
  {}
  {0pt}
  {}

% Problem headings
\newcounter{problem}
\NewDocumentCommand{\problem}{O{} O{}}{%
  \refstepcounter{problem}%
  \par\medskip
  \section*{\normalfont\normalsize\bfseries
    Problem \theproblem%
    \ifstrempty{#1}{}{\,: #1}%
    \ifstrempty{#2}{}{\normalfont\; [#2]}%
  }%
  \addcontentsline{toc}{section}{Problem \theproblem}%
}

% Use alpha labels for enumerate subproblems
\renewcommand{\labelenumi}{(\alph{enumi})}
\renewcommand{\theenumi}{\alph{enumi}}

% Solution command
\newcommand{\solution}{%
  \par\smallskip
  \noindent\textbf{Solution.}\enspace
}

% My math macros
% theorem type environments
\newtheorem{thm}{Theorem}
\newtheorem{prop}{Proposition}
\newtheorem{lemma}{Lemma}
\newtheorem{cor}{Corollary}
\newtheorem{defn}{Definition}
\newtheorem{assump}{Assumption}
\newtheorem{example}{Example}
\newtheorem{conjecture}{Conjecture}

% frequently used symbols
\newcommand{\bE}{\mathbb{E}}
\newcommand{\bP}{\mathbb{P}}
\newcommand{\bQ}{\mathbb{Q}}
\newcommand{\bR}{\mathbb{R}}
\newcommand{\bS}{\mathbb{S}}
\newcommand{\bN}{\mathbb{N}}
\newcommand{\bZ}{\mathbb{Z}}
\newcommand{\sC}{{\mathcal C}}
\newcommand{\sD}{{\mathcal D}}
\newcommand{\sE}{{\mathcal E}}
\newcommand{\sF}{{\mathcal F}}
\newcommand{\sL}{{\mathcal L}}
\newcommand{\sH}{{\mathcal H}}
\newcommand{\sN}{{\mathcal N}}
\newcommand{\sO}{{\mathcal O}}
\newcommand{\sP}{{\mathcal P}}
\newcommand{\sR}{{\mathcal R}}
\newcommand{\sS}{{\mathcal S}}
\newcommand{\sU}{{\mathcal U}}
\newcommand{\sX}{{\mathcal X}}
\newcommand{\sY}{{\mathcal Y}}
\newcommand{\sZ}{{\mathcal Z}}

% operators
\newcommand{\sign}{\mathop{\mathrm{sign}}}
\newcommand{\supp}{\mathop{\mathrm{supp}}}
\newcommand{\argmin}{\operatornamewithlimits{arg\,min}}
\newcommand{\argmax}{\operatornamewithlimits{arg\,max}}
\newcommand{\dist}{\operatorname{dist}}
\newcommand{\tr}{\operatorname{tr}}
\newcommand{\vecop}{\operatorname{vec}}
\newcommand{\st}{\operatorname{s.t.}}
\newcommand{\cut}{\setminus}
\newcommand{\ind}[1]{\mathbbm{1}\left\{#1\right\}}
\newcommand{\given}{\,|\,}

% grouping operators
\newcommand{\brac}[1]{\left[#1\right]}
\newcommand{\set}[1]{\left\{#1\right\}}
\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}
\newcommand{\paren}[1]{\left(#1\right)}
\newcommand{\norm}[1]{\left\|#1\right\|}
\newcommand{\ip}[2]{\left\langle #1,#2 \right\rangle}

% code commands
\newcommand{\matlab}{\textsc{Matlab}}
\newcommand{\python}{\textsc{Python}}


\begin{document}

\makehomeworkheader

\section*{Instructions}
Submit your work as a PDF through the course submission system. If you need an accommodation or an alternate format, contact the instructor.

\noindent \textbf{Note:} For all questions asked in this course, unless the question is simply asking about definitions, please justify all answers. Stating the correct answer without justification will earn no credit. For example, if you write down a string of equations, each successive equation should be accompanied by some justification such as ``by the additivity axiom'' or ``because the outcomes are equally likely'' or ``by the distributive property of set operations.'' You do not need to justify algebra.

% ============================================================
% Problem 1 -- NEW (photon counting; replaces old HW3 Laplacian signaling)
% REVISION 2026-11-04 (instructor-requested difficulty audit; off-spec): part
%   (e) added, two independent short gates = one long gate (total count carries
%   all the evidence; Poisson additivity by the binomial theorem). Parts (a)-(d)
%   and their numbers are unchanged (HW5.1 <-> S10 contract).
% ============================================================
\problem[Counting photons][5 pts each]

A lidar receiver counts photons in a gate. With no target present, the count is $Y \sim \text{Poisson}(\lambda_0 = 2)$ (dark counts plus background). With a target present, $Y \sim \text{Poisson}(\lambda_1 = 10)$.

\begin{enumerate}
  \item Write the likelihood ratio $L(y)$ and show it is monotone increasing in $y$. Conclude that both the ML and the MAP rules are \emph{threshold rules}: decide ``target'' if and only if $y \geq \gamma$.
  \item Find the ML threshold explicitly. With prior $\bP(\text{target}) = 0.1$, find the MAP threshold. Which direction did the prior move it, and why does that make sense?
  \item Compute $\bP(\text{false alarm})$ and $\bP(\text{miss})$ for the MAP rule (finite sums; give numbers).
  \item The gate is lengthened so that both means double ($\lambda_0 = 4$, $\lambda_1 = 20$). Recompute the error probabilities. In one sentence: what did the extra integration time buy, in the language of separating the likelihoods?
  \item Instead of one long gate, the receiver records two back-to-back gates of the original length. Their counts $Y_1, Y_2$ are independent given the hypothesis, each $\text{Poisson}(2)$ with no target and $\text{Poisson}(10)$ with one. Show that the MAP rule (prior $0.1$) depends on $(y_1, y_2)$ only through the total $y_1 + y_2$, and that it is the rule you found in part~(d). Then prove that, under either hypothesis, $Y_1 + Y_2$ is Poisson with twice the mean (sum the joint PMF over the line $y_1 + y_2 = k$ and use the binomial theorem), so the two receivers make exactly the same errors. In one sentence: where does the evidence in a photon count live?
\end{enumerate}

% ============================================================
% Problem 2 -- NEW (modeling-translation layer)
% ============================================================
\problem[Set up the test][5 pts]

A hydrophone either records ambient noise or noise plus a known weak tone. \emph{Without computing anything}, define the latent variable, the observation, the two likelihoods (state your modeling assumptions and what would justify them), the prior, and the decision rule you would use. Three of these five choices are \emph{modeling decisions}, not mathematics. Identify which three.

% ============================================================
% Problem 3 -- NEW (Thread C: bandits / learning a probability)
% REVISION 2026-11-04 (instructor-requested difficulty audit; off-spec): the
%   spec's (a) conjugacy derivation and (b) shrinkage form were S11's two
%   propositions proved verbatim in lecture (same defect the 2026-07-10 pass
%   removed from HW1 1(d)); conjugacy is now cited. New (a): posterior
%   predictive = posterior mean (rule of succession, 11/12). New (b):
%   Cov(X1,X2) = Var(theta), conditional vs marginal independence (S3 lesson,
%   continuous version). (c) unchanged (the HW5.3 <-> S11 template pair).
% ============================================================
\problem[Learning a click rate][5 pts each]

A relay link succeeds on each transmission independently with unknown probability $\theta$: given $\theta$, the outcomes $X_1, X_2, \dots$ (with $X_i = 1$ for a success) are independent $\text{Bernoulli}(\theta)$. Adopt the prior $\theta \sim \text{Beta}(a, b)$. By Session~11's conjugacy result, after $s$ successes in $n$ transmissions the posterior is $\text{Beta}(a + s,\ b + n - s)$; use it freely.

\begin{enumerate}
  \item Show that the probability that the next transmission succeeds is the posterior mean:
  \[
    \bP(X_{n+1} = 1 \given \text{data}) = \bE[\theta \given \text{data}] = \frac{a + s}{a + b + n}.
  \]
  Condition on $\theta$, and say exactly where the independence assumption enters. Then take the flat prior $\text{Beta}(1, 1)$ and a link that has succeeded on all $10$ of its first $10$ transmissions: what probability do you quote for the eleventh, and why is it not $1$?
  \item Before any data are seen, show that $\mathrm{Cov}(X_1, X_2) = \mathrm{Var}(\theta)$, so two transmissions are positively correlated even though they are independent given $\theta$. Under $\text{Beta}(1, 1)$, compare $\bP(X_2 = 1 \given X_1 = 1)$ with $\bP(X_2 = 1)$. In one sentence each: why must the transmissions be dependent for learning to be possible at all, and what kind of prior would make them independent?
  \item With prior $\text{Beta}(1, 1)$ and observations $(n = 10,\ s = 7)$, compute $\bP(\theta > 0.5 \mid \text{data})$ numerically (one line of \texttt{scipy}). Would you make a one-sentence recommendation to deploy the relay?
\end{enumerate}

% ============================================================
% Problem 4 -- NEW (Thread C + Simulation)
% REVISION 2026-11-04 (instructor-requested difficulty audit; off-spec): S11's
%   greedy lock-in proposition (2026-11-02) gave away the qualitative
%   prediction, so a quantitative one is added: evaluate the lock-in bound
%   (~0.003, vacuous), predict greedy's regret from P(worse link leads after
%   the forced pulls) = 0.321 (~32 vs ~34 observed), then split the lock-in
%   fraction by that event and explain escapes and traps. 15 -> 20 pts.
% ============================================================
\problem[Thompson sampling][20 pts]

Two relay links have unknown success rates $\theta_1 = 0.6$ and $\theta_2 = 0.65$ (unknown to the algorithm). At each of $2000$ rounds you must pick one link. Implement and compare three strategies:
\begin{enumerate}
  \item greedy on the empirical means (after $10$ forced pulls of each arm);
  \item $\varepsilon$-greedy with $\varepsilon = 0.1$;
  \item Thompson sampling with $\text{Beta}(1,1)$ priors on each arm.
\end{enumerate}

\noindent\textbf{Before running:} predict which strategy suffers most from ``premature commitment,'' and why. Then put a number on it for greedy:
\begin{itemize}
  \item Evaluate Session~11's greedy lock-in bound for this instance ($m = 10$ forced pulls per arm, $T = 2000$). Is it informative here?
  \item Compute the probability $p$ that, after the forced pulls, the worse link's empirical mean is strictly higher than the better link's (a finite double sum). Predict greedy's average regret at $T = 2000$ under the first-order story ``a run settles on the worse link if and only if that link leads after the forced pulls.''
\end{itemize}

\noindent\textbf{Turn in:} cumulative regret curves (averaged over $200$ runs), your predictions versus the outcome, and your code. For greedy, also report the fraction of runs that spend most of the last $500$ rounds on the worse link: overall, among the runs in which the worse link led after the forced pulls, and among the rest. In two sentences: how can a run escape the first-order story, and how can a run be trapped against it? In one sentence: where exactly in your Thompson-sampling code does ``posterior $\rightarrow$ action'' happen?

% ============================================================
% Problem 5 -- NEW (callback to HW3 Problem 5)
% ============================================================
\problem[Callback][5 pts]

\emph{(Callback to Homework 3, Problem 5.)} In the buy-a-measurement problem of Homework 3, replace the binary sensor with the photon counter of Problem~1 (priors and utilities unchanged; measurement cost $3$). Is the photon counter worth buying? (You may sum the relevant Poisson terms numerically.)

% ============================================================
% Problem 6 -- NEW (thread E; planted-error audit). Instance: Poisson(1) vs
% Poisson(6) with pi1=0.25, reserved S10. TWO planted errors (threshold
% direction; P_FA under the wrong hypothesis). Transcript typeset VERBATIM
% per spec -- do NOT correct, smooth, footnote, or annotate it.
% ============================================================
\problem[Audit the TA-bot][5 pts]

The assistant was asked: \emph{``An optical link's gate count is $\text{Poisson}(1)$ with no pulse present and $\text{Poisson}(6)$ with a pulse; the prior probability of a pulse is $0.25$. Find the MAP detection rule and its false-alarm probability.''} It answered:

\begingroup
\setlength{\fboxsep}{8pt}
\noindent\fbox{%
  \begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
    \noindent\textbf{TA-bot writes:} The likelihood ratio is $L(y) = e^{-5}\cdot 6^{y}$, increasing in $y$, so the MAP rule is a threshold rule. MAP declares ``pulse'' when $L(y) \geq \pi_1/\pi_0 = 0.25/0.75 = 1/3$, i.e.\ when $y \ln 6 - 5 \geq \ln(1/3)$, i.e.\ $y \geq (5 - \ln 3)/\ln 6 \approx 2.18$; since $y$ is an integer, the rule is: declare a pulse iff $y \geq 3$. The false-alarm probability is the probability the count clears the threshold when the pulse is present: $P_{\mathrm{FA}} = \bP(\text{Poisson}(6) \geq 3) = 1 - e^{-6}(1 + 6 + 18) \approx 0.938$.
  \end{minipage}%
}
\endgroup

\medskip
\noindent This solution contains \textbf{zero, one, or two errors}.
\begin{enumerate}
  \item Identify the first line that goes wrong, or certify the solution correct.
  \item Name the violated result or modeling rule in one sentence, citing the session. (Apply to each error you find.)
  \item Fix it: give the corrected detection rule and false-alarm probability, and state the Session-10 sanity check that flags each wrong line \emph{before} any recomputation. (Apply to each error you find.)
  \item One sentence: why is this a mistake a language model plausibly makes?
\end{enumerate}
\noindent\emph{Flagging a correct step as an error costs credit; certifying correctness is part of the skill.}

\end{document}
