\documentclass[10pt]{article}

% Manage page layout
\usepackage[margin=2.5cm, includefoot, footskip=30pt]{geometry}
\pagestyle{plain}
\setlength{\parindent}{0em}
\setlength{\parskip}{1em}
\renewcommand{\baselinestretch}{1}

\usepackage{blkarray}
\usepackage{multirow}
\usepackage{amsmath}
\usepackage{eurosym}
\usepackage{enumerate}

\title{\textbf{Week 7.} Sequential games with complete information III: Repeated games}
\date{}

\begin{document}
\maketitle

\subsection*{Exercise 1: Win-Stay Lose-Shift}

The strategy Win-Stay Lose-Shift (WSLS) cooperates in the first round. In all
subsequent rounds, it cooperates if either both players cooperated in the
previous round, or if no one did. Otherwise it defects.

\textbf{Show that for the infinitely repeated prisoner's dilemma with stage game
payoffs}

\begin{equation*}
    \begin{blockarray}{ccc}
        & C & D \\
        \begin{block}{c(cc)}
            C & (3, 3) & (0, 4) \\
            D & (4, 0) & (1, 1) \\
        \end{block}
    \end{blockarray}
\end{equation*}

\textbf{the strategy profile (WSLS, WSLS) is a subgame perfect equilibrium if
\(\delta \geq \frac{1}{2}\).}

[Hint: Similarly to the examples covered in class, to prove that the strategy
profile (WSLS, WSLS) is a subgame perfect equilibrium we need to check different
cases. For case one consider a history \(h_t\) according to which either both
players cooperated in the previous round, or both players defected. For case two
consider that one player defected.]

\subsection*{Exercise 2: Mini-Max I}

Consider the matching pennies games

\begin{equation*}
    \begin{blockarray}{ccc}
        & \text{Left} & \text{Right} \\
        \begin{block}{c(cc)}
            \text{Up} &    (0.8, 0.4) & (0.4, 0.8) \\
            \text{Down} & (0.4, 0.8) & (0.8, 0.4) \\
        \end{block}
    \end{blockarray}\qquad
    \end{equation*}

Show that in the definition of minimax, it is important to allow for mixed
strategies of the opponent.

\textbf{Specifically show that:}

\begin{align*}
    \min\limits_{s^{(2)}} \max\limits_{s^{(1)}} u^{(1)}(s^{(1)}, s^{(2)}) & = 0.8, \text{ but} \\
    \min\limits_{\sigma^{(2)}} \max\limits_{s^{(1)}} u^{(1)}(s^{(1)}, \sigma^{(2)}) & = 0.6.
\end{align*}

\subsection*{Bonus 1: Mini-Max II}

Show that the minimax payoff of a player can be lower than what this player
could get in a Nash equilibrium. Specifically, consider the game

\begin{equation*}
    \begin{blockarray}{cccc}
        & \text{Left} & \text{Right} \\
        \begin{block}{c(ccc)}
            \text{Up}  & (-2, 2)   & (1, -2)  \\
            \text{Medium} & (1, -2)   & (-2, 2)  \\
            \text{Down} & (0, 1) & (0, 1) \\
        \end{block}
    \end{blockarray}\qquad
\end{equation*}

\begin{itemize}
    \item \textbf{Show that} the Nash equilibria of this game are of the form
    \begin{align*}
        \sigma^{(1)} & = (0, 0, 1) \\
        \sigma^{(2)} & = (q, 1- q) \text{ with } q \in \left[\frac{1}{3}, \frac{2}{3}\right],\\
    \end{align*}
    and the resulting payoffs are \(\hat{u}^{(1)} = 0, \hat{u}^{(2)} = 1\).
    \item \textbf{Show that} player 2's minimax payoff \(\underline{u}^{(2)} =
    \min\limits_{\sigma^{(1)}} \max\limits_{s^{(2)}} u^{(2)}(\sigma^{(1)},
    s^{(2)}) = 0 < \hat{u}^{(2)}\).
\end{itemize}

Now that you have shown that the minimax payoff of a player can be lower than
their Nash equilibrium payoff, \textbf{conclude that in repeated games with a
sufficient large \(\delta\), players may be worse off in equilibrium than in the
one shot game.}

\subsection*{Bonus Exercise 2: Folk Theorem}

Consider the battle of the sexes

\begin{equation*}
    \begin{blockarray}{ccc}
        & a_1 & a_2 \\
        \begin{block}{c(cc)}
            a_1 & (3, 1) & (0, 0) \\
            a_2 & (0, 0) & (1, 3) \\
        \end{block}
    \end{blockarray}
\end{equation*}

\textbf{What is the set of feasible and individually rational payoffs?}

\underline{Bonus:} Construct a strategy \(\hat{\sigma}\) for the repeated battle of sexes
that for a sufficiently large \(\delta\) satisfies the following 2 conditions.

\begin{enumerate}[(i)]
    \item When both player adopt the strategy, they obtain a payoff of approximately
    \(\pi^{(1)} = \pi^{(2)} = 2.\)
    \item \((\hat{\sigma}, \hat{\sigma})\) is a subgame perfect equilibrium.
    You do not need to show this rigorously, but give a convincing argument.
\end{enumerate}

\end{document}


%!TEX root = ../main.tex

\documentclass[../main.tex]{subfiles}
\begin{document}
\subsection{Derivation of Changes in the Penalty Term}\label{app:derive_changes_penalty_term}
% Used in section 3 - MC Simulation
As part of the estimation of changes in skill prices, changes in the penalty term need to be estimated. These changes in the penalty term are a funtion of optimal task choices $\lambda_{i,\tau}^*, \; \tau \in \{t-1, t\}$. In this appendix, I show that changes in the penalty term result as a polynomial with penalty exponent of degree $\phi$.\\
From equation (\ref{eq:wage_change}) we have that wage changes come as a composition of three parts:
\begin{equation}
	\Delta w_{i,t} = \bar{\lambda}^*_{i,t} \Delta \tilde{\pi}_{t} + \bar{\lambda}^*_{i,t} \Delta \tilde{s}_{i,t} + \left[ \rho_{i}(\lambda_{i,t}^*) - \rho_{i}(\lambda_{i,t-1}^*) \right] \tag{\ref{eq:wage_change}}
\end{equation}
In the following I show that the part related to changes in the penalty term (i.e., $\left[ \rho_{i}(\lambda_{i,t}^*) - \rho_{i}(\lambda_{i,t-1}^*) \right]$) can be restated as a polynomial of optimal task choice parameters $\lambda_{i,\tau}^*, \; \tau \in \{t-1, t\}$ of degree $\phi$.
\begin{align}
	\intertext{Starting from}
	{} &  \rho_{i}(\lambda_{i,t}^*) - \rho_{i}(\lambda_{i,t-1}^*)  \label{eq:appen_penalty_term_1} \\
	\intertext{with}
	\rho_{i}(\lambda_{i,\tau}^*) &= \theta |b_i - \lambda_{i,\tau}^*|^\phi, \; \text{for} \; \tau \in \{t-1, t \} \nonumber \\
	\intertext{With regard to the absolute value function, two cases have to be considered:}
		|b_i - \lambda_{i,\tau}^*| &= \left\{
		\begin{array}{ll}
			b_i - \lambda_{i, \tau}^*, &\text{if} \; b_i - \lambda_{i, \tau}^* \geq 0 \\
			-(b_i - \lambda_{i, \tau}^*), &\text{if} \; b_i - \lambda_{i, \tau}^* < 0
		\end{array}
	\right. \nonumber \\
	\intertext{Consider first case: $b_i - \lambda_{i, \tau}^* \geq 0$. In this case equation \ref{eq:appen_penalty_term_1} can be written as follows:}
	\rho_{i}(\lambda_{i,t}^*) - \rho_{i}(\lambda_{i,t-1}^*) &= (b_i - \lambda_{i,t}^*)^\phi - (b_i - \lambda_{i,t-1}^*)^\phi \nonumber
	\intertext{Each of the two binomials on the right hand side can be rewritten as bivariate polynomials by application of the binomial expansion:}
	(b_i - \lambda_{i,\tau}^*)^\phi &= \sum^\phi_{k=0}\binom{\phi}{k} b_i^{\phi-k} (-\lambda_{i,\tau})^k, \; \text{for} \; \tau \in \{t-1, t\} \nonumber
\end{align}
It is straightforward to see that in the second case ($ b_i - \lambda_{i, \tau}^* < 0$) the result in analogous with opposite sign.\\
Thus, changes in the penalty term can be expressed as the differencee between two polynomials of $\lambda_{i,t-1}$ and $\lambda_{i,t}$, respectively. The degree of the resulting polynomial is equal to the penalty exponent $\phi$.\\
In the simulation study, it is assumed that $\phi = 2$. The changes in the penalty function in this particular case, therefore, result as follows:
\begin{align}
	\rho_{i}(\lambda_{i,t}^*) - \rho_{i}(\lambda_{i,t-1}^*) &= (b_i - \lambda_{i,t})^2 - (b_i - \lambda_{i,t-1})^2 \nonumber \\
	{} &= \lambda_{i,t-1}^{*2} + \lambda_{i,t}^{*2} + 2b_i(\lambda_{i,t-1}^* + \lambda_{i,t}^*) \tag{\ref{eq:changes_in_penalty}}
\end{align}

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{float}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}

\title{Mathematical Modelling in \LaTeX}
\author{Gurobi Optimization}

\begin{document}

\maketitle

This document provides an example of how \LaTeX\ can be used to write clean mathematical models. As an example, we consider the famous maximum flow / minimum cut problems. The problems consider a directed graph consisting of a set of nodes and a set of labeled arcs. The arc labels are non-negative values representing a notion of capacity for the arc. In the node set, there exists a source node $s$ and a terminal node $t$. The amount of flow into one of the intermediary nodes must equal the amount of flow out of the node, i.e., flow is conserved.

The maximum flow question asks: what is the maximum flow that can be transferred from the source to the sink. The minimum cut question asks: which is the subset of arcs, that once removed would disconnect the source node from the terminal node, which has the minimum sum of capacities. For example, removing arcs $(v_1, v_3)$ and $(v_2, v_4)$ from the network in \ref{fig:my_label} would mean there is no longer a path from $s$ to $t$ and the sum of the capacities of these arcs is $12 + 11 = 23$. It is reasonably straight forward to find a better cut, i.e., a subset of nodes with sum of capacities less than 23.

\input{picture}

A complete model is provided for the maximum flow problem, whereas the minimum cut problem is left as a challenge to the reader.

\section*{Notation}

\subsection*{Sets}

\begin{table}[H]
\centering
\begin{tabular}{p{1cm} p{1.5cm} p{9cm}}
\hline
\textbf{Index} & \textbf{Set} & \textbf{Description}\\
\hline
$i$ & $V$ & Set of all nodes ($s$ source and $t$ terminal)\\
$(i, j)$ & $A$ & Set of all arcs\\
\hline
\end{tabular}
\end{table}

\subsection*{Parameters}

\begin{table}[H]
\centering
\begin{tabular}{p{2cm} p{9cm}}
\hline
\textbf{Parameter} &  \textbf{Description}\\
\hline
$c_{i,j}$ & Capacity of arc $(i,j) \in A$\\
\hline
\end{tabular}
\end{table}

\section*{Maximum Flow}

\subsection*{Variables}

\begin{table}[H]
\centering
\begin{tabular}{p{2cm} p{1.5cm} p{9cm}}
\hline
\textbf{Variable} & \textbf{Type}& \textbf{Description}\\
\hline
$f_{i, j}$ & Cont & flow from $i$ to $j$ in arc $(i,j) \in A$\\
\hline
\end{tabular}
\end{table}

\subsection*{Model}

\begin{subequations}
\begin{alignat}3
\text{maximise} \ & \sum_{j \in V: (s,j) \in A} f_{s, j} \label{model-obj}\\
s.t. \ & f_{i, j} \leq c_{i,j} \quad && \forall (i, j) \in A \label{m1-c1}\\
& \sum_{i \in V: (i,j) \in A} f_{i, j} - \sum_{k \in V: (j, k) \in A} f_{j,k} = 0 \quad  && \forall j \in V \setminus \{s, t\} \label{m2-c2}
\end{alignat}
\end{subequations}

The objective \eqref{model-obj} is to maximise the sum of flow leaving the source node $s$. Constraints \eqref{m1-c1} ensure that the flow in each arc does not exceed the capacity of that arc. Constraints \eqref{m2-c2} are continuity constraints, which ensure that the flow into each of the nodes, excluding the source and sink, is equal to the flow out of that node.

\newpage

\section*{Minimum Cut}

This section is left for the reader to complete

\subsection*{Variables}

\begin{table}[H]
\centering
\begin{tabular}{p{2cm} p{1.5cm} p{9cm}}
\hline
\textbf{Variable} & \textbf{Type}& \textbf{Description}\\
\hline
$r_{i, j}$ &  & 1 if arc $(i,j) \in A$ is removed\\
$z_{i, j}$ &  & 1 if node $i \in V \setminus \{s, t\}$ connected to $s$, 0 otherwise\\
\hline
\end{tabular}
\end{table}

\subsection*{Model}

\begin{subequations}
\begin{alignat}3
\text{to do}
\end{alignat}
\end{subequations}

\end{document}


\chapter{Overview}
\label{chap:intro}

MARSS stands for Multivariate Auto-Regressive(1) State-Space. The MARSS package is an \R package for estimating the parameters of linear MARSS models with Gaussian errors\index{MARSS model}.  This class of model is extremely important in the study of linear stochastic dynamical systems, and these models are important in many different fields, including economics, engineering, genetics, physics and ecology (Appendix \ref{chap:SSreferences}).  The model class has different names in different fields, for example in some fields they are termed dynamic linear models (DLMs) or vector autoregressive (VAR) state-space models.  The MARSS package allows you to easily fit time-varying constrained and unconstrained MARSS models with or without covariates to multivariate time-series data via maximum-likelihood using primarily an EM algorithm\footnote{Fitting via the BFGS algorithm is also provided using \R's optim function, but this is not the focus of the package.}.

A full MARSS model, with Gaussian errors, takes the form:
\begin{subequations}\label{eqn:marss}
\begin{gather}
\xx_t = \BB_t\xx_{t-1} + \uu_t + \CC_t\cc_t + \GG_t\ww_t, \text{ where } \ww_t \sim \MVN(0,\QQ_t) \label{eqn:marssx}\\
\yy_t = \ZZ_t\xx_t + \aa_t + \DD_t\dd_t + \HH_t\vv_t, \text{ where } \vv_t \sim \MVN(0,\RR_t) \label{eqn:marssy}\\
\xx_1 \sim \MVN(\pipi,\LAM) \text{ or } \xx_0 \sim \MVN(\pipi,\LAM)\label{eqn:marssx1}
\end{gather}
\end{subequations}
The $\xx$ equation is termed the state process and the $\yy$ equation is termed the observation process.  Data enter the model as the $\yy$; that is the $\yy$ is treated as the data although there may be missing data.  The $\cc_t$ and $\dd_t$ are inputs (aka, exogenous variables, covariates or indicator variables).  The $\GG_t$ and $\HH_t$ are also typically inputs (fixed values with no missing values).

The bolded terms are matrices with the following definitions:
 \begin{description}
	\item[$\xx$] is a $m \times T$ matrix of states.  Each $\xx_t$ is a realization of the random variable $\XX_t$ at time $t$.
	\item[$\ww$] is a $m \times T$ matrix of the process errors.  The process errors at time $t$ are multivariate normal with mean 0 and covariance matrix $\QQ_t$.
	\item[$\yy$] is a $n \times T$ matrix of the observations.   Some observations may be missing.
	\item[$\vv$] is a $n \times T$ column vector of the non-process errors.  The observation erros at time $t$ are multivariate normal with mean 0 and covariance matrix $\RR_t$.
	\item[$\BB_t$ and $\ZZ_t$] are parameters and are $m \times m$ and $n \times m$ matrices.
	\item[$\uu_t$ and $\aa_t$] are parameters and are $m \times 1$ and $n \times 1$ column vectors.
	\item[$\QQ_t$ and $\RR_t$] are parameters and are $g \times g$ (typically $m \times m$) and $h \times h$ (typically $n \times n$) variance-covariance matrices.
	\item[$\pipi$] is either a parameter or a fixed prior. It is a $m \times 1$ matrix\index{prior}.
	\item[$\LAM$] is either a parameter or a fixed prior. It is a $m \times m$ variance-covariance matrix.
	\item[$\CC_t$ and $\DD_t$] are parameters and are $m \times p$ and $n \times q$ matrices.
	\item[$\cc$ and $\dd$] are inputs (no missing values) and are $p \times T$ and $q \times T$ matrices.
  \item[$\GG_t$ and $\HH_t$] are inputs (no missing values) and are $m \times g$ and $n \times h$ matrices.
\end{description}

In some fields, the $\uu$ and $\aa$ terms are routinely set to 0 or the model is written in such a way that they are incorporated into $\BB$ or $\ZZ$.  However, in other fields, the $\uu$ and $\aa$ terms are the main objects of interest, and the model is written to explicitly show them.  We include them throughout our discussion, but they can be set to zero if desired.

AR(p) models can be written in the above form by properly defining the $\xx$ vector and setting some of the $\RR$ variances to zero; see Chapter \ref{chap:ARMA}. Although the model appears to only include i.i.d. errors ($\vv_t$ and $\ww_t$), in practice, AR(p) errors can be included by moving the error terms into the state model.  Similarly, the model appears to have independent process ($\vv_t$) and observation ($\ww_t$) errors, however, in practice, these can be modeled as identical or correlated by using one of the state processes to model the errors with the $\BB$ matrix set appropriately for AR or white noise---although one may have to fix many of the parameters associated with the errors to have an identifiable model.  Study the application chapters and textbooks on MARSS models (Appendix \ref{chap:SSreferences}) for examples of how a wide variety of autoregressive models can be written in MARSS form.

\section{What does the MARSS package do?}
Written in an unconstrained form\footnote{meaning all the elements in a parameter matrices are allowed to be different and none constrained to be equal or related.}, a MARSS model can be written out as follows. Two state processes ($\xx$) and three observation processes ($\yy$) are used here as an example.
\begin{gather*}
\begin{bmatrix}x_1\\ x_2\end{bmatrix}_t
= \begin{bmatrix}b_{11}&b_{12}\\b_{21}&b_{22}\end{bmatrix}
\begin{bmatrix}x_1\\x_2\end{bmatrix}_{t-1}
+ \begin{bmatrix}w_1\\ w_2\end{bmatrix}_t,\quad
\begin{bmatrix}w_1\\ w_2\end{bmatrix}_t \sim \MVN\begin{pmatrix}\begin{bmatrix}u_1\\u_2\end{bmatrix},\begin{bmatrix}q_{11}&q_{12}\\q_{21}&q_{22}\end{bmatrix} \end{pmatrix}  \\
\\
\begin{bmatrix}y_1\\ y_2\\ y_3\end{bmatrix}_t
= \begin{bmatrix}z_{11}&z_{12}\\ z_{21}&z_{22}\\ z_{31}&z_{32}\end{bmatrix}
\begin{bmatrix}x_1\\ x_2\end{bmatrix}_t
+ \begin{bmatrix}v_1\\ v_2\\ v_3\end{bmatrix}_t,
\begin{bmatrix}v_1\\ v_2\\ v_3\end{bmatrix}_t \sim \MVN\begin{pmatrix}\begin{bmatrix}a_1\\ a_2\\ a_3\end{bmatrix},
 \begin{bmatrix}r_{11}&r_{12}&r_{13}\\r_{21}&r_{22}&r_{23}\\r_{31}&r_{32}&r_{33}\end{bmatrix} \end{pmatrix}  \\
\\
\begin{bmatrix}x_1\\ x_2\end{bmatrix}_0 \sim \MVN\begin{pmatrix}\begin{bmatrix}\pi_1\\ \pi_2\end{bmatrix},\begin{bmatrix}\nu_{11}&\nu_{12}\\ \nu_{21}&\nu_{22}\end{bmatrix} \end{pmatrix} \quad
or\quad
\begin{bmatrix}x_1\\ x_2\end{bmatrix}_1 \sim \MVN\begin{pmatrix}\begin{bmatrix}\pi_1\\ \pi_2\end{bmatrix},\begin{bmatrix}\nu_{11}&\nu_{12}\\ \nu_{21}&\nu_{22}\end{bmatrix} \end{pmatrix}
\end{gather*}

However not all parameter elements can be estimated simultaneously. Constraints are required in order to specify a model with a unique solution. The MARSS package allows you to specify constraints by fixing elements in a parameter matrix or specifying that some elements are estimated---and have a linear relationship to other elements. Here is an example of a MARSS model with fixed and estimated parameter elements:
\begin{gather*}
\begin{bmatrix}x_1\\ x_2\end{bmatrix}_t
= \begin{bmatrix}a&0\\0&a\end{bmatrix}
\begin{bmatrix}x_1\\x_2\end{bmatrix}_{t-1}
+ \begin{bmatrix}w_1\\ w_2\end{bmatrix}_t,\quad
\begin{bmatrix}w_1\\ w_2\end{bmatrix}_t \sim \MVN\begin{pmatrix}\begin{bmatrix}0.1\\u\end{bmatrix},\begin{bmatrix}q_{11}&q_{12}\\q_{12}&q_{22}\end{bmatrix} \end{pmatrix}  \\
\\
\begin{bmatrix}y_1\\ y_2\\ y_3\end{bmatrix}_t
= \begin{bmatrix}d&d\\ c& c\\ 1+2d+3c&2+3d\end{bmatrix}
\begin{bmatrix}x_1\\ x_2\end{bmatrix}_t
+ \begin{bmatrix}v_1\\ v_2\\ v_3\end{bmatrix}_t,\quad
\begin{bmatrix}v_1\\ v_2\\ v_3\end{bmatrix}_t \sim \MVN\begin{pmatrix}\begin{bmatrix}a_1\\ a_2\\ 0\end{bmatrix},
 \begin{bmatrix}r&0&0\\0&r&0\\0&0&r\end{bmatrix} \end{pmatrix}  \\
\\
\begin{bmatrix}x_1\\ x_2\end{bmatrix}_0 \sim \MVN\begin{pmatrix}\begin{bmatrix}\pi\\ \pi\end{bmatrix},\begin{bmatrix}1&0\\ 0&1\end{bmatrix} \end{pmatrix}
\end{gather*}
Notice that some elements are fixed (in this case to 0, but could be any fixed number), some elements are shared (have the same value), and some elements are linear combinations of other estimated values ($c$, $1+2d+3c$ and $2+3d$ are linear combinations of $c$ and $d$).

The MARSS package fits models via maximum likelihood. The MARSS package is unusual among packages for fitting MARSS models in that fitting is performed via a constrained EM algorithm \citep{Holmes2010} based on a vectorized form of Equation \ref{eqn:marss} (See Chapter \ref{chap:algorithms} for the vectorized form used in the algorithm).  Although fitting via the BFGS algorithm is also provided using \verb@method="BFGS"@ and the optim functionin R, the examples in this guide use the EM algorithm primarily because it gives robust estimation for datasets replete with missing values and for high-dimensional models with various constraints. However, there are many models/datasets where BFGS is faster and we typically try both for problems.  The EM algorithm is also often used to provide initial conditions for the BFGS algorithm (or an MCMC routine) in order to improve the performance of those algorithms.  In addition to the main model fitting function, the MARSS package  supplies functions for bootstrap and approximate confidence intervals, parametric and non-parametric bootstrapping, model selection (AIC and bootstrap AIC), simulation, and bootstrap bias correction.

\section{What does MARSS output and how do I get the output?}\index{MARSS model}
MARSS models are used in many different ways and different users will want different types of output.  Some users will want the parameter estimates while others want the smoothed states and others want to use MARSS to interpolate missing values and want the expected values of missing data.

The best way to find out how to get output is to type \verb@?print.MARSS@ at the command line after installing MARSS.  The print help page discusses how to get parameter estimates in different forms, the smoothed and filtered states, all the Kalman filter and smoother output, all the expectations of y (missing data), confidence intervals and bias estimates for the parameters,
and standard errors of the states.  If you are looking only for Kalman filter and smoother output, see the relevant section in Chapter \ref{chap:algorithms} and see the help page for the \verb@MARSSkf()@ function (type \verb@?MARSSkf@at the \R command line).

You might also want to look at the \verb@tidy@ and \verb@glance@ functions which will summarize commonly needed output from a MARSS model fit.  These functions work as they do in the \verb@broom@ R package.

\section{How to get started (quickly)}

If you already work with models in the form of Equation \ref{eqn:marss}, you can immediately fit your model with the MARSS package.  Install the MARSS package and then type \texttt{library(MARSS)} at the command line to load the package.  Look at the \href{https://CRAN.R-project.org/package=MARSS/vignettes/Quick_Start.pdf}{Quick Start Guide} and then skim through Chapter \ref{chap:Examples}.  Appendix \ref{chap:modelspec} also has many examples of how to specify different forms for your parameter matrices.

\section{Getting your data in right format}
Your data need to be a matrix, not dataframe, with time across the columns ($n \times T$ matrix). Note a univariate or multivariate ts (time-series) object can also be used and this will be converted to a $n \times T$ matrix. The MARSS functions assume discrete time steps and you will need a column for each time step.  Replace any missing time steps with NA.  Write your model down on paper and identify which parameters correspond to which parameter matrices in Equation \ref{eqn:marss}.  Call the \texttt{MARSS()} function (Chapter \ref{chap:MARSS}) using your data and using the \texttt{model} argument to specify the structure of each parameter.

\subsection{Getting a ts object into the right form}
A R ts object (time series object) stores information about the time steps of the data and often seasonal information (the quarter or month).  You can pass in your data as a ts object and MARSS will convert this to matrix form.  However if you have your data in ts form, then you may be using year and season (quarter, month) as covariates to estimate trend and seasonality.  Here is how to get your ts into the form that MARSS wants with a matrix of covariates for season.

\emph{Univariate example}.  This converts a univariate ts object with year and quarter into a matrix with a row for the response (here called Temp), year, and quarter.
\begin{Schunk}
\begin{Sinput}
z = ts(rnorm(10), frequency = 4, start = c(1959, 2))
dat = data.frame(Yr = floor(time(z) + .Machine$double.eps),
      Qtr = cycle(z), Temp=z)
dat = t(dat)
\end{Sinput}
\end{Schunk}
When you call MARSS, \verb@dat["Temp",]@ is the data. \verb@dat[c("Yr","Qtr"),]@ are your covariates.

\emph{Multivariate example}.  In this example, we have two temperature readings and a salinity reading. The data are monthly.

\begin{Schunk}
\begin{Sinput}
z <- ts(matrix(rnorm(300), 100, 3), start = c(1961, 1),
     frequency = 12, names=c("Temp1","Temp2","Sal"))
dat = data.frame(Yr = floor(time(z) + .Machine$double.eps),
     Month = cycle(z), z)
\end{Sinput}
\end{Schunk}
When you call MARSS, \verb@dat[c("Temp1","Temp2"),]@ are the data and your covariates are \verb@dat[c("Yr","Month","Sal"),]@.

See the chapters that discuss seasonality for examples of how to model seasonality.  The brute force method of treating month or quarter as a factor requires estimation of more parameters than necessary in many cases.

\section{Important notes about the algorithms}
 \textit{Specification of a properly constrained model with a unique solution is the responsibility of the user because MARSS has no way to tell if you have specified an insufficiently constrained model---with correspondingly an infinite number of solutions.}

Specifying a properly constrained model with a unique solution is imperative.  How do you know if the model is properly constrained?  If you are using a MARSS model form that is widely used, then you can probably assume that it is properly constrained. If you go to papers where someone developed the model or method, the issue of constraints necessary to ensure ``identifiability'' will likely be addressed if it is an issue.  Are you fitting novel MARSS models? Then you will need to do some study on identifiability in this class of models using textbooks (Appendix \ref{chap:SSreferences}).  Often textbooks do not address identifiability explicitly.  Rather it is addressed implicitly by only showing a model constructed in such a way that it is identifiable.  In our work, if we suspect identification problems, we will often first do a Bayesian analysis with flat priors and look for oddities in the posteriors, such as ridges, plateaus or bimodality.

All the EM code in the MARSS package is currently in native \R.  Thus the model fitting is slow.  The classic Kalman filter/smoother algorithm, as shown in \citet[p. 331-335]{ShumwayStoffer2006}, is based on the original smoother presented in \citet{Rauch1963}. This Kalman filter is provided in function \verb@MARSSkfss@, but the default Kalman filter and smoother used in the MARSS package is based on the algorithm in \citet{KohnAnsley1989} and papers by Koopman et al. This Kalman filter and smoother is provided in the KFAS package (Helske 2012).  Table 2 in \citet{Koopman1993} indicates that the classic algorithm is 40-100 times slower than the algorithm given in \citet{KohnAnsley1989}, \citet{Koopman1993}, and \citet{Koopmanetal1998}. The MARSS package function \verb@MARSSkfas@ provides a translator between the model objects in MARSS and those in KFAS so that the KFAS functions can be used.  \verb@MARSSkfas@ also includes a lag-one covariance smoother algorithm as this is not output by the KFAS functions, and it provides proper formulation of the priors so that one can use the KFAS functions when the prior on the states is set at $t=0$ instead of $t=1$. Simply off-setting your data to start at t=2 and sending that value to $t_{init}=1$ in the KFAS Kalman filter would not be mathematically correct!

EM algorithms will quickly get in the vicinity of the maximum likelihood, but the final approach to the maximum is generally slow relative to quasi-Newton methods.  On the flip side, EM algorithms are quite robust to initial conditions choices and can be extremely fast at getting close to the MLE values for high-dimensional models.  The MARSS package also allows one to use the BFGS method to fit MARSS models, thus one can use an EM algorithm to ``get close'' and then the BFGS algorithm to polish off the estimate. Restricted maximum-likelihood algorithms\index{estimation!REML} are also available for AR(1) state-space models, both univariate \citep{Staplesetal2004} and multivariate \citep{HinrichsenHolmes2009}.  REML can give parameter estimates with lower variance than plain maximum-likelihood algorithms.  However, the algorithms for REML when there are missing values are not currently available (although that will probably change in the near future).  Another maximum-likelihood method is data-cloning which adapts MCMC algorithms used in Bayesian analysis for maximum-likelihood estimation \citep{Leleetal2007}.

Missing values\index{missing values} are seamlessly accommodated with the MARSS package.  Simply specify missing data with NAs.  The likelihood computations are exact and will deal appropriately with missing values.  However, no innovations\footnote{referring to the non-parametric bootstrap developed by Stoffer and Wall (1991).} bootstrapping can be done if there are missing values.  Instead parametric bootstrapping must be used.

You should be aware that maximum-likelihood estimates of variance in MARSS models are fundamentally biased, regardless of the algorithm used.  This bias is more severe when one or the other of $\RR$ or $\QQ$ is very small, and the bias does not go to zero as sample size goes to infinity.  The bias arises because variance is constrained to be positive.  Thus if $\RR$ or $\QQ$ is essentially zero, the mean estimate will not be zero and thus the estimate  will be biased high while the corresponding bias of the other variance will be biased low.  You can generate unbiased variance estimates using a bootstrap estimate of the bias.  The function \texttt{MARSSparamCIs()}\index{functions!MARSSparamCIs} will do this.  However be aware that adding an {\it estimated} bias to a parameter estimate will lead to an increase in the variance of your parameter estimate.  The amount of variance added will depend on sample size.

You should also be aware that mis-specification of the prior on the initial states ($\pipi$ and $\LAM$) can have catastrophic effects on your parameter estimates if your prior conflicts with the distribution of the initial states implied by the MARSS model\index{prior!troubleshooting}.  These effects can be very difficult to detect because the model will appear to be well-fitted.  Unless you have a good idea of what the parameters should be, you might not realize that your prior conflicts.

The most common problems we have found with priors on $\xx_0$ are the following.  Problem 1) The correlation structure in $\LAM$ (whether the prior is diffuse or not) does not match the correlation structure in $\xx_0$ implied by your model.  For example, you specify a diagonal $\LAM$ (independent states), but the implied distribution has correlations. Problem 2) The correlation structure in $\LAM$ does not match the structure in $\xx_0$ implied by constraints you placed on $\pipi$.  For example, you specify that all values in $\pipi$ are shared, yet you specify that $\LAM$ is diagonal (independent).

Unfortunately, using a diffuse prior does not help with these two problems because  the diffuse prior still has a correlation structure and can still conflict with the implied correlation in $\xx_0$.  One way to get around these problems is to set $\LAM$=0 (a $m \times m$ matrix of zeros) and estimate $\pipi \equiv \xx_0$ only.  Now $\pipi$ is a fixed but unknown (estimated) parameter, not the mean of a distribution.  In this case, $\LAM$ does not exist in your model and there is no conflict with the model.
Be aware however that estimating $\pipi$ as a parameter is not always robust. If you specify that $\LAM$=0 and specify that $\pipi$ corresponds to $\xx_0$, but your model ``explodes'' when run backwards in time, you cannot estimate $\pipi$ because you cannot get a good estimate of $\xx_0$.  Sometimes this can be avoided by specifying that $\pipi$ corresponds to $\xx_1$ so that it can be constrained by the data $\yy_1$.

In summary, if the implied correlation structure of your initial states is independent (diagonal variance-covariance matrix), you should generally be ok with a diagonal and high variance prior or with treating the initial states as parameters (with $\LAM=0$).  But if your initial states have an implied correlation structure that is not independent, then proceed with caution. `With caution' means that you should assume you have problems and test how your model fits with simulated data.

There is a large class of models in the statistical finance literature that have the form
\begin{equation*}
\begin{gathered}
\xx_{t+1} = \BB\xx_t + \GAM\et_t\\
\yy_t = \ZZ\xx_t  + \et_t\\
\end{gathered}
\end{equation*}
For example, ARMA(p,q) models can be written in this form.  The MARSS model framework in this package will not allow you to write models in that form.  You can put the $\et_t$ into the $\xx_t$ vector and set $\RR=0$ to make models of this form using the MARSS form, but the EM algorithm in the MARSS package won't let you estimate parameters because the parameters will drop out of the full likelihood being maximized in the algorithm.  You can try using BFGS by passing in the \texttt{method} argument to the \verb@MARSS()@ call.

\section{Troubleshooting}
\index{troubleshooting}Numerical errors due to ill-conditioned matrices are not uncommon when fitting MARSS models\index{errors!ill-conditioned}\index{troubleshooting!ill-conditioning}\index{likelihood!troubleshooting}. The Kalman and EM algorithms need inverses of matrices. If those matrices become ill-conditioned, for example all elements are close to the same value, then the algorithm becomes unstable.  Warning messages will be printed if the algorithms are becoming unstable\index{troubleshooting!numerical instability} and you can set \verb@control$trace=1@, to see details of where the algorithm is becoming unstable.  Whenever possible, you should avoid using shared $\pipi$ values in your model\footnote{An example of a $\pipi$ with shared values is $\pipi=\bigl[\begin{smallmatrix} a\\a\\a \end{smallmatrix} \bigr]$.}.  The way our algorithm deals with $\LAM$ tends to make this case unstable, especially if $\RR$ is not diagonal.  In general, estimation of a non-diagonal $\RR$ is more difficult, more prone to ill-conditioning, and more data-hungry.

You may also see non-convergence warnings, especially if your MLE model turns out to be degenerate\index{errors!degenerate}\index{troubleshooting!degenerate}\index{troubleshooting!non-convergence}.  This means that one of the elements on the diagonal of your $\QQ$ or $\RR$ matrix are going to zero (are degenerate).  It will take the EM algorithm forever to get to zero.  BFGS will have the same problem, although it will often get a bit closer to the degenerate solution.  If you are using \verb@method="kem"@, MARSS will warn you if it looks like the solution is degenerate. If you use \verb@control=list(allow.degen=TRUE)@, the EM algorithm will attempt to set the degenerate variances to zero (instead of trying to get to zero using an infinite number of iterations).  However, if one of the variances is going to zero, first think about why this is happening.  This is typically caused by one of three problems:  1) you made a mistake in inputting your data, e.g. used -99 as the missing value in your data but did not replace these with NAs before passing to MARSS, 2) your data are not sufficient to estimate multiple variances or 3) your data are inconsistent with the model you are trying fit.

The algorithms in the MARSS package are designed for cases where the $\QQ$ and $\RR$ diagonals are all non-minuscule.  For example, the EM update equation for $\uu$ will grind to a halt (not update $\uu$) if $\QQ$ is tiny (like 1E-7).  Conversely, the BFGS equations are likely to miss the maximum-likelihood when $\RR$ is tiny because then the likelihood surface becomes hyper-sensitive to $\pipi$.   The solution is to use the degenerate likelihood function for the likelihood calculation and the EM update equations.  MARSS will implement this automatically when $\QQ$ or $\RR$ diagonal elements are set to zero and will try setting $\QQ$ and $\RR$ terms to zero automatically if \verb@control$allow.degen=TRUE@.

One odd case can occur when $\RR$ goes to zero (a matrix of zeros), but you are estimating $\pipi$.  If \verb@model$tinitx=1@, then $\pipi=\xx_1^0$ and $\yy_1-\ZZ\xx_1^0$ can go to 0 as well as $\var(\yy_1-\ZZ\xx_1^0)$ by driving $\RR$  to zero. But as this happens, the log-likelihood associated with $\yy_1$ will go (correctly) to infinity and thus the log-likelihood goes to infinity.  But if you set $\RR=0$, the log-likelihood will be finite.  The reason is that $\RR \approx 0$ and $\RR=0$ specify different likelihoods associated with $\yy_1-\ZZ\xx_1^0$.  With $\RR=0$, $\yy_1-\ZZ\xx_1^0$ does not have a distribution; it is just a fixed value.  So there is no likelihood to go to infinity.  If some elements of the diagonal of $\RR$ are going to zero, you should be suspect of the parameter estimates.  Sometimes the structure of your data, e.g. one data value followed by a long string of missing values, is causing an odd spike in the likelihood at  $\RR \approx 0$.  Try manually setting $\RR$ equal to zero to get the correct log-likelihood\footnote{The likelihood returned when $\RR \approx 0$ is not incorrect.  It is just not the likelihood that you probably want.  You want the likelihood where the $\RR$ term is dropped because it is zero.}.

\section{Other related packages}
Packages that will do Kalman filtering and smoothing are many, but packages that estimate the parameters in a MARSS model, especially constrained MARSS models, are much less common.  The following are those with which we are familiar, however there are certainly more packages for estimating MARSS models in engineering and economics of which we are unfamiliar.  The MARSS package is unusual in that it uses an EM algorithm for maximizing the likelihood as opposed to a Newton-esque method (e.g. BFGS). The package is also unusual in that it allows you to specify the initial conditions at $t=0$ or $t=1$, allows degenerate models (with some of the diagonal elements of $\RR$ or $\QQ$ equal to zero). Lastly, model specification in the MARSS package  has a one-to-one relationship between the model list in MARSS and the model as you would write it on paper as a matrix equation.  This makes the learning curve a bit less steep.  However, the MARSS package has not been optimized for speed and probably will be really slow if you have time-series data with a lot of time points.

\begin{description}
	\item[atsar] \href{https://asts-es.github.io/atsar/}{atsar} is an \R package we wrote for fitting MARSS models using STAN.  It allows fast and flexible fitting of MARSS models in a Bayesian framework.  Our book from our time-series class has example applications \href{https://atsa-es.github.io/atsa-labs/}{Applied Time-Series Analysis for Fisheries and Environmental Sciences}.
  \item[stats] The \verb@stats@ package (part of base R) has functions for fitting univariate structural time series models (MARSS models with a univariate y).  Read the help file at \verb@?StructTS@. The Kalman filter and smoother functions are described here: \verb@?KalmanLike@.
	\item[DLM] \href{https://cran.r-project.org/package=dlm}{DLM} is an \R package for fitting MARSS models.  Our impression is that it is mainly Bayesian focused but it does allow MLE estimation via the \verb@optim()@ function.  It has a book, Dynamic Linear Models with \R  by Petris et al., which has many examples of how to write MARSS models for different applications.
	\item[sspir] \href{https://cran.r-project.org/package=sspir}{sspir} an \R package for fitting ARSS (univariate) models with Gaussian, Poisson and binomial error distributions.
	\item[dse] \href{https://cran.r-project.org/package=dse}{dse} (Dynamic Systems Estimation) is an \R package for multivariate Gaussian state-space models with a focus on ARMA models.
	\item[SsfPack] \href{http://www.ssfpack.com/}{SsfPack} is a package for Ox/Splus that fits constrained multivariate Gaussian state-space models using mainly (it seems) the BFGS algorithm but the newer versions support other types of maximization.  SsfPack is very flexible and written in C to be fast.  It has been used extensively on statistical finance problems and is optimized for dealing with large (financial) data sets.  It is used and documented in Time Series Analysis by State Space Methods by Durbin and Koopman, An Introduction to State Space Time Series Analysis by Commandeur and Koopman, and Statistical Algorithms for Models in State Space Form: SsfPack 3.0, by Koopman, Shephard, and Doornik.
	\item[Brodgar] The Brodgar software was developed by Alain Zuur to do (among many other things) dynamic factor analysis, which involves a special type of MARSS model.  The methods and many example analyses are given in Analyzing Ecological Data by Zuur, Ieno and Smith.  This is the one package that we are aware of that also uses an EM algorithm for parameter estimation.
	\item[eViews] eViews is a commercial economics software that will estimate at least some types of MARSS models.
	\item[KFAS] The \href{https://cran.r-project.org/package=KFAS}{KFAS} \R package provides a fast Kalman filter and smoother.  Examples in the package show how to estimate MARSS models using the KFAS functions and \R's \verb@optim()@ function.   The MARSS package uses the filter and smoother functions from the KFAS package.
		\item[S+FinMetrics] \href{http://faculty.washington.edu/ezivot/MFTS2ndEditionFinMetrics.htm}{S+FinMetrics} is a S-plus module for fitting MAR models, which are called vector autoregressive (VAR) models in the economics and finance literature.  It has some support for state-space VAR models, though we haven't used it so are not sure which parameters it allows you to estimate.  It was developed by Andrew Bruce, Doug Martin, Jiahui Wang, and Eric Zivot, and it has a book associated with it: Modeling Financial Time Series with S-plus by Eric Zivot and Jiahui Wang.
		\item[kftrack] The \href{https://github.com/positioning/kalmanfilter/wiki}{kftrack} \R package provides a suite of functions specialized for fitting MARSS models to animal tracking data.
\end{description}


\section{Uncertainty principle}
The uncertainty principle for observables $A$ and $B$ are
\begin{align}
    \sigma_A \sigma_B \ge \frac{1}{2}
    |\bra{\psi} [A,B] \ket{\psi}|
\end{align}
for example,
\begin{align}
    \sigma_x \sigma_p \ge \frac{\hbar}{2}.
\end{align}

Don't pretend you have to rush through the material and not get very basic
things like the uncertainty principle.

Here's something I know you understand for fact.

\section{Time-energy uncertainty}
Energy is an operator in quantum mechanics.
Time is not an operator in QM.
It is a parameter in the Schrodinger equation.
Nothing tells me the time operator.
It's special in QM.

But we shouldn't.
Time and energy satisfy an uncertainty just like position and momentum.
The proof and interoperation is different,
but it's true.

Take any observable $A$.
Compute expectation value $\bar{A}$.
Let's see how the average changes over time.
\begin{align}
    \frac{d}{dt}\bar{A} &= \frac{d}{dt}\bra{\psi} A\ket{\psi}\\
    &= \underbrace{\frac{d}{dt} \bra{\psi}}_{+\frac{i}{\hbar}\bra{\psi} H}
    A \ket{\psi}
    + \bra{\psi} A
    \underbrace{\frac{d}{dt}\ket{\psi}}_{-\frac{i}{\hbar}H\ket{\psi}}
\end{align}
and then we get the beautiful result
\begin{align}
    \frac{d}{dt}\bar{A} &=
    -\frac{i}{\hbar} \bra{\psi} [A,H] \ket{\psi}
\end{align}
which leads to
\begin{align}
    \left|\frac{d\bar{A}}{dt}\right| &=
    \frac{1}{\hbar}
    |\bra{\psi} [A,H] \ket{\psi} |\\
    &\le \frac{2}{\hbar}\sigma_A \sigma_H
\end{align}
by the uncertainty principle,
just half the commutator.

What we do is this
There is an interpretation for the formula.
I'm going to write it like this.
\begin{align}
    \boxed{
        \sigma_H \frac{\sigma_A}{\left|\frac{d\bar{A}}{dt}\right|}
        \ge \frac{\hbar}{2}.
    }
\end{align}
This $\sigma_H$ of course is the uncertainty of the energy.
In a particular state,
there is some spread in the energy I get.

What about this $\frac{\sigma_A}{\left|\frac{d\bar{A}}{dt}\right|}$?
It's the uncertainty of $A$ divided by how fast the average of $A$ changes.
This thing has units of time.
The interpretation is that it is the time it takes for $\bar{A}$
to move enough to go outside the uncertainty.
This is how long it takes for the average value of $A$ to change appreciably.
It is the time it takes for $\bar{A}$ to change by $\sigam_A$.

Let me draw a picture.
I can measure $A$,
there is an average $\bar{A}$ and there is a spread $\sigma_A$.
Over time,
the central value would change,
and it's going to take time for the central value to change more than
$\sigma_A$.
So this is the time it takes for $A$ to change such that it matters.

If you're close to an eigenstate of energy,
this is going to be small,
and this is going to be large.
The expectation is going to change not by much.
If I'm in an eigenstate,
in fact I don't change at all.
On the other hand,
if it's a superposition of many energy eigenstates,
thne the wave function chages fast,
and this thing is small,
and hte expectation value moves wildly.
There is an uncertainty relation between energy and time,
but time is just the time it takes for stuff to change,
where stuff is whatever $A$ you put here.

\begin{question}
    Eigenstates are stable?
\end{question}
They are completely stable.
If you substitute it into the Schrodinger equation,
the ket is just going to evolve by phase,
but the phase doesn't matter anyway.
The universe is not in an eigenstate of the Hamiltonian of the universe,
because it it was,
how can we be moving around?

Example.
Have you heard of quarks and gluons?
It's a complicated theory.
Not so much to state.
But to solve.
There is a state where 3 quarks live together,
they have spin that point up called $\Delta^+$.
This doesn't last a long time.
Very quick,
it splits up into a neutron $n$ and a pion $\pi^+$.
Let's try to understand what this means in QM language.

We started off with a certain state
$\ket{\Delta^+}$.
It's not an energy eigenstate of the QCD Hamiltonian,
so it will hange,
but change in many ways.
When this evolves in time,
this is going to go into a state
\begin{align}
    U(t)\ket{\Delta^+} &=
    \alpha\ket{\Delta^+} + \beta \ket{n \pi^+} + \cdots
\end{align}
so it's not an eigenstate of QCD.
You can detect the neutron,
and you can measure the energy and momenta,
and you can make a plot.
Because of conservation of energy,
this state should have the same energy as the original state.

What I find is something like this of prob vs energy $E$.
I get a certain distribution,
a distribution with some mean at 1232 MeV
and a spread of 300 MeV.
What I say is this.

It takes time for this state $\ket{\Delta^+}$
to change appreciably.
How long does it take to change appreciably?
This number here 300 MeV.
Knowing the uncertainty of the energy,
and knowing the right hand side,
I should know this $\sigma_A/|d\bar{A}/dt|$,
the time it takes to appreciably change,
otherwise known as the lifetime,
sometimes written as $\tau$.

When you see the newspaper saying the Higgs boson has a lifetime whatever,
no one timed it,
they just observed it decayed into $\Delta^+$ and $\Delta^-$.
They say a plot like this.
It didn't look pretty,
there are error bars,
and there's this bump here with two data points.
No one believed it.

Then people looked at other decay modes.
They do some plots,
and notice this other peak.
It must be a coincidence,
Calculated the probability.
They say they discovered the Higgs and celebrated.

The regular uncertainty that is not time,
it's typically saturated,
you get the equality for the ground state.
There's a similar experience here.
For ground state things,
the lowest state that 3 quarks can have for example,
is going to saturate the inequality.

It becomes a definition almost.
What they really mean when they say the Higgs has some lifetime,
it's really saying the width is something.

In some experiments,
you can measure two things independently,
lifetime and width.
The relation is an inequality.
In some situations,
there is saturated.
But I can construct situations where the lifetime is much bigger than this
inequality suggests,
but I have to think about it.
In a harmonic oscillator,
the uncertain is minimum and bigger for higher states.
One can construct a state with much bigger uncertainty than the inequality
bound.

You learn in Griffiths to calculate the hydrogen atom levels.
I can't believe we're out of time.
You solve the hydrogen atom.
What you did is htis.
You ge the Hamiltonian with Coulomb interaciton.
Nothing else.
Then you find the eigenstates.
They are there the rest of the time.
But of course they don't.
You start iwth excited states,
get cofeee
and measuredecays.
To include not onlu the Coulomb interaction etween electorn and neuclus,
but also the quantised electromagnetic field.
If you do that,
those states you found were not eigenstates of the whole thing anymore,
but they're darn close to being eigenstates.
All those eigenstates you calculated in Griffiths are really close,
because the unceratinty is so small.
Those lines in the spectra you see are not infinitely thin.
There is a little spread.
This width is related to the lifetime.
When you excite the hydrogen atom,
let it decay,
send it through a prism,
they're not going to be infinitely thin,
some are going to be bright and some are not.

can you guess a relation between the brightess and the width?
If the line is very sharp,
this time must be large,
so you get very few photons iwth htat energy.
So thin lines are going to be dim.

On the other harnd, if $\sigma_H$ is going to be big fat line,
it's going to decay slow.

As an undergrad,
you learn about eigenstates and they never change,
but suddenly people say they jump.
There's no reason for that.
Well it's beccause they missed out the rest of the Hamiotnian,
but it's good to teach as an introduction.


\section{Infinite Limits and Limits at Infinity}\label{sec:InfLimits}
We occasionally want to know what happens to some quantity
when a variable gets very large or ``goes to infinity''.

\begin{example}{Limit at Infinity}{LimitInfinity}
What happens to the function $\ds \cos(1/x)$ as $x$ goes to infinity? It
seems clear that as $x$ gets larger and larger, $1/x$ gets closer and
closer to zero, so $\cos(1/x)$ should be getting closer and closer to
$\cos(0)=1$.
\end{example}

As with ordinary limits, this concept of ``limit at infinity'' can be
made precise. Roughly, we want $\ds \lim_{x\to \infty}f(x)=L$ to mean that
we can make $f(x)$ as close as we want to $L$ by making $x$ large
enough.

\begin{definition}{Limit at Infinity (Formal Definition)}{LimitAtInfinity}
If $f$ is a function, we say that $\ds \lim_{x\to
  \infty}f(x)=L$ if for every $\epsilon>0$ there is an $N > 0$ so that
  whenever $x>N$, $|f(x)-L|<\epsilon$.
We may similarly define $\ds \lim_{x\to-\infty}f(x)=L$.
\end{definition}

We include this definition for completeness, but we will not explore it in
detail. Suffice it to say that such limits behave in much the same way
that ordinary limits do; in particular there is a direct analog of
Theorem~\ref{thm:PropertiesLimits}.

\begin{example}{Limit at Infinity}{LimitInfinity2}
Compute $\ds\lim_{x\to \infty}{2x^2-3x+7\over x^2+47x+1}$.
\end{example}

\begin{solution}
As $x$ goes to infinity both the numerator and denominator go to
infinity. We divide the numerator and
denominator by $\ds x^2$:
$$\lim_{x\to \infty}{2x^2-3x+7\over x^2+47x+1}=
\lim_{x\to \infty}{2-\ds{3\over x}+\ds{7\over x^2}\over
1+\ds{47\over x}+\ds{1\over x^2}}.$$
Now as $x$ approaches infinity, all the quotients with some power of
$x$ in the denominator approach zero, leaving 2 in the numerator and 1
in the denominator, so the limit again is 2.
\end{solution}

In the previous example, we \ifont{divided by the highest power of $x$ that occurs in the denominator} in order to evaluate the limit.
We illustrate another technique similar to this.

\begin{example}{Limit at Infinity}{LimitInfinity3}
Compute the following limit:
$$\lim_{x\to\infty}\frac{2x^2+3}{5x^2+x}.$$
\vspace{-0.5cm}
\end{example}

\begin{solution}
As $x$ becomes large, both the numerator and denominator become large, so it isn't clear what happens to their ratio. The highest power of $x$ in the denominator is $x^2$, therefore we will divide every term in both the numerator and denominator by $x^2$ as follows:
$$\lim_{x\to\infty}\frac{2x^2+3}{5x^2+x}=\lim_{x\to\infty}\frac{2+3/x^2}{5+1/x}.$$
Most of the limit rules from last lecture also apply to infinite limits, so we can write this as:
$$=\frac{\ds{\lim_{x\to\infty}2+3\lim_{x\to\infty}\frac{1}{x^2}}}{\ds{\lim_{x\to\infty}5+\lim_{x\to\infty}\frac{1}{x}}}=\frac{2+3(0)}{5+0}=\frac{2}{5}.$$
Note that we used the theorem above to get that $\ds{\lim_{x\to\infty}\frac{1}{x}=0}$ and $\ds{\lim_{x\to\infty}\frac{1}{x^2}=0}$.

A shortcut technique is to analyze only the \ifont{leading terms} of the numerator and denominator. A leading term is a term that has the highest power of $x$. If there are multiple terms with the same exponent, you must include all of them.\\
\ifont{Top:} The leading term is $2x^2$.\\
\ifont{Bottom:} The leading term is $5x^2$.\\
Now only looking at leading terms and ignoring the other terms we get:
$$\lim_{x\to\infty}\frac{2x^2+3}{5x^2+x}=\lim_{x\to\infty}\frac{2x^2}{5x^2}=\frac{2}{5}.$$
\end{solution}

We next look at limits whose value is infinity (or minus infinity).

\begin{definition}{Infinite Limit (Useable Definition)}{Infinite Limit}
In general, we will write
$$\lim_{x\to a}f(x)=\infty$$
if we can make the value of $f(x)$ arbitrarily large by taking $x$ to be sufficiently close to $a$ (on either side of $a$) but not equal to $a$.
Similarly, we will write
$$\lim_{x\to a}f(x)=-\infty$$
if we can make the value of $f(x)$ arbitrarily large and \blue{negative} by taking $x$ to be sufficiently close to $a$ (on either side of $a$) but not equal to $a$.
\end{definition}

This definition can be modified for one-sided limits as well as limits with $x\to a$ replaced by $x\to\infty$ or $x\to-\infty$.

\begin{example}{Limit at Infinity}{LimitInfinity4}
Compute the following limit: $\ds\lim_{x\to\infty}(x^3-x).$
\end{example}

\begin{solution}
One might be tempted to write:
$$\lim_{x\to\infty}x^3-\lim_{x\to\infty}x=\infty-\infty,$$
however, we do not know what $\infty-\infty$ is, as $\infty$ is not a real number and so cannot be treated like one.
We instead write:
$$\lim_{x\to\infty}(x^3-x)=\lim_{x\to\infty}x(x^2-1).$$
As $x$ becomes arbitrarily large, then both $x$ and $x^2-1$ become arbitrarily large, and hence their product $x(x^2-1)$ will also become arbitrarily large. Thus we see that
$$\lim_{x\to\infty}(x^3-x)=\infty.$$
\end{solution}

\begin{example}{Limit at Infinity and Basic Functions}{LimitInfinity5}
We can easily evaluate the following limits by observation:
\[ \begin{array}{ll}
1.~\ds\lim_{x\to \infty} \frac{6}{\sqrt{x^3}}=0 & \qquad 2.~\ds\lim_{x\to -\infty} x-x^2=-\infty \\
\\
3.~\ds\lim_{x\to \infty}x^3+x=\infty            & \qquad 4.~\ds\lim_{x\to \infty} \cos(x)=\mbox{DNE} \\
\\
5.~\ds\lim_{x\to \infty} e^x=\infty             & \qquad 6.~\ds\lim_{x\to -\infty} e^x=0 \\
\\
7.~\ds\lim_{x\to 0^+} \ln x=-\infty             & \qquad 8.~\ds\lim_{x\to 0} \cos(1/x)=\mbox{DNE} \\
\end{array} \]
\end{example}

Often, the shorthand notation $\ds\frac{1}{0^+}=+\infty$ and $\ds\frac{1}{0^-}=-\infty$ is used to represent the following two limits respectively:
$$\lim_{x\to 0^+}\frac{1}{x}=+\infty\qquad\mbox{and}\qquad\lim_{x\to 0^-}\frac{1}{x}=-\infty.$$
Using the above convention we can compute the following limits.

\begin{example}{Limit at Infinity and Basic Functions}{LimitInfinity6}
Compute $\ds{\lim_{x\to 0^+} e^{1/x}}$, $\ds{\lim_{x\to 0^-} e^{1/x}}$ and $\ds{\lim_{x\to 0} e^{1/x}}$.
\end{example}

\begin{solution}
We have:
$$\lim_{x\to 0^+} e^{\frac{1}{x}}=e^{\frac{1}{0^+}}=e^{+\infty}=\infty.$$
$$\lim_{x\to 0^-} e^{\frac{1}{x}}=e^{\frac{1}{0^-}}=e^{-\infty}=0.$$
Thus, as left-hand limit $\neq$ right-hand limit,
$$\lim_{x\to 0} e^{\frac{1}{x}}=\mbox{DNE}.$$
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Subsections to include
\input{3-limits/3-5-1-vertical-asymptotes}
\input{3-limits/3-5-2-horizontal-asymptotes}
\input{3-limits/3-5-3-slant-asymptotes}
\input{3-limits/3-5-4-end-behaviour-growth-rate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for \ref{sec:InfLimits}}

\begin{enumialphparenastyle}

%%%%%%%%%%
\begin{ex}
Compute the following limits.
\begin{multicols}{3}
\begin{enumerate}
	\item	$\ds\lim_{x\to \infty} \sqrt{x^2+x}-\sqrt{x^2-x}$
	\item	$\ds\lim_{x\to\infty} {e^x + e^{-x}\over e^x -e^{-x}}$
	\item	$\ds\lim_{t\to1^+}{(1/t)-1\over t^2-2t+1}$
	\item	$\ds\lim_{t\to\infty}{t+5-2/t-1/t^3\over 3t+12-1/t^2}$
	\item	$\ds\lim_{y\to\infty}{\sqrt{y+1}+\sqrt{y-1}\over y}$
	\item	$\ds\lim_{x\to 0^+}{3+x^{-1/2}+x^{-1}\over 2+4x^{-1/2}}$
	\item	$\ds\lim_{x\to\infty}{x+x^{1/2}+x^{1/3}\over x^{2/3}+x^{1/4}}$
	\item	$\ds\lim_{t\to\infty}{1-\sqrt{t\over t+1}\over 2-\sqrt{4t+1\over t+2}}$
	\item	$\ds\lim_{t\to\infty}{1-{t\over t-1}\over 1-\sqrt{t\over t-1}}$
	\item	$\ds\lim_{x\to-\infty}{x+x^{-1}\over 1+\sqrt{1-x}}$
	\item	$\ds\lim_{x\to1^+}{\sqrt{x}\over x-1}$
	\item	$\ds\lim_{x\to\infty}{x^{-1}+x^{-1/2}\over x+x^{-1/2}}$
	\item	$\ds\lim_{x\to\infty}{x+x^{-2}\over 2x+x^{-2}}$
	\item	$\ds\lim_{x\to\infty}{5+x^{-1}\over 1+2x^{-1}}$
	\item	$\ds\lim_{x\to\infty}{4x\over\sqrt{2x^2+1}}$
	\item	$\ds\lim_{x\to\infty}{(x+5)\left({1\over 2x}+{1\over x+2}\right)}$
	\item	$\ds\lim_{x\to0^+}{(x+5)\left({1\over 2x}+{1\over x+2}\right)}$
	\item	$\ds\lim_{x\to2}{x^3-6x-2\over x^3-4x}$
\end{enumerate}
\end{multicols}
\begin{sol}
\begin{multicols}{3}
\begin{enumerate}
	\item	$1$
	\item	$1$
	\item	$-\infty$
	\item	$1/3$
	\item	$0$
	\item	$\infty$
	\item	$\infty$
	\item	$2/7$
	\item	$2$
	\item	$-\infty$
	\item	$\infty$
	\item	$0$
	\item	$1/2$
	\item	$5$
	\item	$\ds 2\sqrt2$
	\item	$3/2$
	\item	$\infty$
	\item	does not exist
\end{enumerate}
\end{multicols}
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
The function $\ds f(x) = {x\over\sqrt{x^2+1}}$ has two horizontal asymptotes.  Find them and give a rough sketch of $f$ with its horizontal asymptotes.
\begin{sol}
$y=1$ and $y=-1$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Find the vertical asymptotes of $\ds f(x)=\frac{\ln x}{x-2}$.
\begin{sol}
	$x=0$ and $x=2$.
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
Suppose that a falling object reaches velocity $v(t)=50(1-e^{-t/5})$ at time $t$, where distance is measured in $m$ and time $s$. What is the object's terminal velocity, i.e. the value of $v(t)$ as $t$ goes to infinity?
\end{ex}

%%%%%%%%%%
\begin{ex}
Find the slant asymptote of $f(x)=\dfrac{x^2+x+6}{x-3}$.
\begin{sol}
	$y=x+4$
\end{sol}
\end{ex}

%%%%%%%%%%
\begin{ex}
	Compute the following limits.
	\begin{enumerate}
		\item	$\ds\lim_{x\to -\infty}(2x^3-x)$
		\item	$\ds\lim_{x\to \infty}\tan^{-1}(e^x)$
		\item	$\ds\lim_{x\to -\infty}\tan^{-1}(e^x)$
		\item	$\ds\lim_{x\to \infty}\dfrac{e^x+x^4}{x^3+5\ln x}$
		\item	$\ds\lim_{x\to \infty}\dfrac{2^x+5(3^x)}{3(2^x)-3^x}$
		\item	$\ds\lim_{x\to -\infty}\dfrac{2^x+5(3^x)}{3(2^x)-3^x}$
		\item	$\ds\lim_{x\to 0^{+}}\sqrt{x}\ln x$ [Hint: Let $t=1/x$]
	\end{enumerate}
	\begin{sol}
		\begin{enumerate}
			\item	$-\infty$
			\item	$\pi/2$
			\item	0
			\item	$\infty$
			\item	$-5$
			\item	1/3
			\item	0
		\end{enumerate}
	\end{sol}
\end{ex}

\end{enumialphparenastyle}


\documentclass[../main.tex]{subfiles}
\begin{document}
\subsection{Proofs of correctness}
Our optimisation problem described previously can now be restated as follows for mathematical clarity.\\
\noindent\Problem \textit{Find a maximal member $B$ of $\mathcal{I}$ of maximum weight.}
\begin{note}
Let $B_G$ be a base of a matroid generated by the greedy algorithm.
\end{note}
\begin{thm}\cite{ox_book}
If $(E,\mathcal{I})$ is a matroid $M,$ then $B_G$ is a solution to the optimization problem.
\end{thm}
\begin{proof}
If $r(M) = r,$ then $B_G = \{e_1,e_2, ..., e_r\}$ is a basis of $M.$ Let $B$ be another basis of $M$, $B = \{f_1, f_2, ..., f_r\}$
where $\omega(f_1) \geq \omega(f_2) \geq ... \geq \omega(f_r).$ We claim that $\omega(e_j) \geq \omega(f_f)$ $ \forall j$ , then it follows that $\omega(B_G) \geq \omega(B)$ for any other basis in $\mathcal{B}.$
\end{proof}

\begin{lem}\cite{ox_book}
If $1 \leq j \leq r,$ then $\omega(e_j) \geq \omega(f_j).$
\end{lem}
\begin{proof}
Suppose (seeking a contradiction) that $k$ is the least integer for which $\omega(e_k) < \omega(f_k).$ Take $I_1 = \{e_1, e_2, ..., e_{k-1}\}$ and $I_2 = \{f_1, f_2, ..., f_{k}\}.$ Since $|I_2| = |I_1|+1$  $(I3)$ implies $I_1 \cup \{f_t\} \in \mathcal{I}$ for some $f_t \in I_2 \setminus I_1.$ But this means that $\omega(f_t) \geq \omega(f_k) > \omega(e_k)$ and hence the Greedy algorithm would have chosen $f_t$ over $e_k$, which gives us our contradiction.
\end{proof}
\end{document}


\RequirePackage[l2tabu, orthodox]{nag}
\documentclass[a4paper]{article}
\usepackage[a4paper]{geometry}
\usepackage[colorlinks=false, pdfborder={0 0 0}]{hyperref}
\usepackage{amsmath,esint,bm,siunitx,cleveref}
\title{Green's Identities}
\author{Naitree Zhu}
\date{Last modified: \today}
\begin{document}
\maketitle
In mathematics, Green's identities\footnote{More information:\url{http://en.wikipedia.org/wiki/Green's_identities}} are a set of three identities in vector calculus. They are named after the mathematician George Green, who discovered Green's theorem.
\part{Green's first identity}
This identity is derived from the divergence theorem applied to the vector field $\boldsymbol{F}=\psi\nabla\varphi$: Let $\varphi$ and $\psi$ be scalar functions defined on some region \textit{U} in $R^{3}$, and suppose that $\varphi$ is twice continuously differentiable, and $\psi$ is once continuously differentiable. Then
\begin{equation}
\int_U\left(\psi\nabla^{2}\varphi+\nabla\varphi\cdot\nabla\psi\right)\mathrm{d}V=\oint_{\partial U} \psi\nabla\varphi\cdot\mathrm{d}\boldsymbol{S}
\end{equation}

This theorem is essentially the higher dimensional equivalent of integration by parts:
\begin{equation}
\int_\Omega\nabla u\cdot\boldsymbol{v}\mathrm{d}\Omega=\int_\Gamma u\left(\boldsymbol{v}\cdot\hat{\nu}\right)\mathrm{d}\Gamma-\int_\Omega u\nabla\cdot\boldsymbol{v}\mathrm{d}\Omega
\end{equation}
with $\psi$ and $\nabla\varphi$ replacing u and v.
\section*{Proof}
Note that Green's first identity above is a special case of the more general identity derived from the divergence theorem by substituting $\psi\bm{F}$ for $\bm{F}$:
\begin{equation}
\int_U\left(\psi\nabla\cdot\bm{F}+\bm{F}\cdot\nabla\psi\right)\mathrm{d}V=\oint_{\partial U}\psi\bm{F}\cdot\mathrm{d}\bm{S}
\end{equation}
\part{Green's second identity}
If $\varphi$ and $\psi$ are both twice continuously differentiable on \textit{U} in $R^{3}$, and $\epsilon$ is once continuously differentiable, we can choose $\bm{F}=\psi\epsilon\nabla\varphi-\varphi\epsilon\nabla\psi$ and obtain:
\begin{equation}
\int_U \left[\psi\nabla\cdot\left(\epsilon\nabla\varphi\right)-\varphi\nabla\cdot\left(\epsilon\nabla\psi\right)\right]\mathrm{d}V=\oint_{\partial U} \epsilon\left(\psi\frac{\partial\varphi}{\partial n}-\varphi\frac{\partial\psi}{\partial n}\right)\mathrm{d}S
\end{equation}
For the special case of $\epsilon=1$ all across \textit{U} in $R_{3}$ then:
\begin{equation}
\int_U \left(\psi\nabla^{2}\varphi-\varphi\nabla^{2}\psi\right)\mathrm{d}V=\oint_{\partial U}\left(\psi\frac{\partial \varphi}{\partial n}-\varphi\frac{\partial\psi}{\partial n}\right)\mathrm{d}S
\end{equation}
$\frac{\partial \varphi}{\partial n}$ is the directional derivative of $\varphi$ in the direction of the outward pointing normal $\hat{n}$ to the surface element $\mathrm{d}\bm{S}$, i.e.
\begin{equation}
\frac{\partial \varphi}{\partial n}=\nabla\varphi\cdot\hat{n}
\end{equation}
\section*{Proof}
By substituting $\epsilon\nabla\varphi$ for $\nabla\varphi$ in Green's first identity eq.~(1), we can get
\begin{equation}
\int_U \left[\psi\nabla\cdot\left(\epsilon\nabla\varphi\right)+\epsilon\nabla\varphi\cdot\nabla\psi\right]\mathrm{d}V=\oint_{\partial U} \epsilon\psi\frac{\partial\varphi}{\partial n}\mathrm{d}S
\end{equation}
Swap $\psi$ and $\varphi$ in the equation above, then
\begin{equation}
\int_U \left[\varphi\nabla\cdot\left(\epsilon\nabla\psi\right)+\epsilon\nabla\psi\cdot\nabla\varphi\right]\mathrm{d}V=\oint_{\partial U} \epsilon\varphi\frac{\partial\psi}{\partial n}\mathrm{d}S
\end{equation}
Finally, $(7)-(8)$ and it will lead to Green's second identity eq.~(4).
\part{Green's third identity}
Green's third identity derives from the second identity by choosing $\varphi=G$, where \textit{G} is a Green's function of the Laplace operator. This means that:
\begin{equation}
\nabla^{2}G(\bm{x},\bm{\eta})=\delta(\bm{x}-\bm{\eta})
\end{equation}
For example in $R^{3}$, a solution has the form:
\begin{equation}
G(\bm{x},\bm{\eta})=\frac{-1}{4\pi\lVert \bm{x}-\bm{\eta} \rVert}
\end{equation}
Apply it into eq.~(5), and it leads to Green's third identity.

Green's third identity states that if $\psi$ is a function that is twice continuously differentiable on \textit{U}, then
\begin{equation}
\int_U \left[G\left(\bm{x},\bm{\eta}\right)\nabla^{2}\psi\left(\bm{x}\right)\right]\mathrm{d}V-\psi\left(\bm{\eta}\right)=\oint_{\partial U}\left[G\left(\bm{x},\bm{\eta}\right)\frac{\partial \psi\left(\bm{x}\right)}{\partial n}-\psi\left(\bm{x}\right)\frac{\partial G\left(\bm{x},\bm{\eta}\right)}{\partial n}\right]\mathrm{d}S
\end{equation}
A simplification arises if $\psi$ is itself a harmonic function, i.e. a solution to the Laplace equation. So the identity above simplifies to:
\begin{equation}
\psi\left(\eta\right)=\oint_{\partial U}\left[\psi\left(\bm{x}\right)\frac{\partial G\left(\bm{x},\bm{\eta}\right)}{\partial n}-G\left(\bm{x},\bm{\eta}\right)\frac{\partial \psi\left(\bm{\eta}\right)}{\partial n}\right]\mathrm{d}S
\end{equation}
The second term in the integral above can be eliminated if we choose \textit{G} to be the Green's function that vanishes on the boundary of region \textit{U} (Dirichlet boundary condition):
\begin{equation}
\psi\left(\eta\right)=\oint_{\partial U}\psi\left(\bm{x}\right)\frac{\partial G\left(\bm{x},\bm{\eta}\right)}{\partial n}\mathrm{d}S
\end{equation}
This form is used to construct solutions to Dirichlet boundary condition problems. To find solutions for Neumann boundary condition problems, the Green's function with vanishing normal gradient on the boundary is used instead.

It can be further verified that the above identity also applies when $\psi$ is a solution to the Helmholtz equation or wave equation and \textit{G} is the appropriate Green's function. In such a context, this identity is the mathematical expression of the Huygens Principle.
\part{Green's vector identity}
See Wikipedia. A little complicated and I don't see a need in the near future to know it.
\end{document}


\subsection*{Introduction}

For this exercise, we analyze a time series containing the British coal mining disasters under the time period 1851--1962. The difference between this exercise and the one in the course literature, is that we have a continuous time series as well as more than 1 \textbf{breakpoint}. A breakpoint is thus the year at which the intensity of the disasters change. We will use $d-1$ breakpoints, where $d$ then corresponds to the number of intervals we will use. In order to get a feel for the time series we will analyze, a figure containing a histogram of the disasters is found in figure \ref{fig:disasters}.

\begin{figure}[H]
  \centering
    \includegraphics[scale=0.24]{./Figures/disasters.png}
  \caption[An Electron]{Histogram plot of the British coal mine disasters in the time period 1851--1962.}
  \label{fig:disasters}
\end{figure}

As is seen in figure \ref{fig:disasters} there is a change in the disaster intensity at the turn of the century, perhaps due to some legislation regarding work safety or some technological advancement. \\ \\ To begin the exercise, we define the vector $\boldsymbol{t}$ to be the vector containing all of the breakpoints $t_i, \: i = 2,\dots d$ aswell as the start and end point $t_1 = 1851$ and $t_{d+1}=1963$. We wish to model the disasters using an inhomogenous Poisson process with an intensity $\lambda_i$ for each of the intervals $[t_i,t_{i+1}), \: i = 1,\dots,d$. Where all of the $\lambda_i$'s are collected in a vector $\boldsymbol{\lambda}$. \\ We will denote the time series containing the year at which disaster struck as $\boldsymbol{\tau} = (\tau_1,\dots,\tau_n)$ for $n = 191$, where the subscript denotes accident $i$. \\ We then define the number of accidents under the interval $[t_i,t_{i+1})$ to be
\[n_i(\boldsymbol{\tau}) = \sum^n_{j = 1}\mathbbm{1}\{[t_i,t_{i+1})\}\cdot \tau_j \]
We set a $\Gamma(2,\theta)$ prior on the intensities, $\lambda_i$, and a $\Gamma(2,\beta)$ hyperprior on $\theta$. Where $\beta$ is a hyperparameter that needs to be specified. Furthermore, we put the prior
\[ f(\boldsymbol{t}) \propto\left\{
	\begin{array}{l}
		\prod^d_{i = 1}(t_{i+1} - t_i), \quad \text{for } t_1 < t_2 < \dots < t_{d+1} \\
		0, \quad \text{else}
	\end{array}
\right. \]
 This prior prevents the breakpoints from being located to closely. All of these prior assumptions then imply that
\[f(\boldsymbol{\tau} | \boldsymbol{\lambda}, \boldsymbol{t}) \propto \prod^d_{i = 1}\lambda_i^{n_i(\boldsymbol{\tau})}\cdot\exp \left \{ - \sum^d_{i = 1}\lambda_i(t_{i+1} - t_i) \right \} \]
In order to sample from the posterior $f(\theta,\boldsymbol{t},\boldsymbol{\lambda} | \boldsymbol{\tau})$ we will construct a hybrid \textbf{M}arkov \textbf{C}hain \textbf{M}onte \textbf{C}arlo algorithm, where the hybrid comes from the fact that we will need to sample $\boldsymbol{t}$ using a Metropolis--Hastings step whereas the other components can be updated using a Gibbs sampler. \\ \\ There are several ways to choose the proposal distribution for the Metropolis--Hastings, we chose to use the \textit{Random walk proposal}, which means that we will update one breakpoint at a time and for each breakpoint $t_i$ generate a candidate $t^*_i$ according to
\[t^*_i = t_i + \epsilon, \quad \epsilon \sim \text{Unif}(-R,R) \]
Where $R = \rho(t_{i+1} - t_{i-1})$ and $\rho$ is a tuning parameter.

%Basic idea of MCMC (Markov Chain Monte Carlo): To sample from a density f we construct a
%Markov chain having f as stationary distribution. A law of
%large numbers for Markov chains guarantees convergence. \\

%MCMC is widely used for sampling, due to its simplicity when dealing with complex distributions or distributions of high dimensions. However, the price is that the samples will be statistically dependant. It can be used to simulate a probabiltiy distribution $\pi(x)$ that is known only up to a normalizing constant, which is especially important in Bayesian inference with $\pi$ as the posterior distribution.


\gotosection{2}{1}
\subsection{The main algorithm: row reduction}

\begin{exercise}{2}
  \begin{enumerate}
    \item $[A|\Vect{b}] = \xmat{0&3&-1&0\\-2&1&2&0\\1&0&-5&0}
      \rightarrow \rops{R_1: R_3\\R_3: R_1}\xmat{1&0&-5&0\\-2&1&2&0\\0&3&-1&0}
      \rightarrow \rops{R_2: 2R_1+R_2}\\ \xmat{1&0&-5&0\\0&1&-8&0\\0&3&-1&0}
      \rightarrow \rops{R_3: -3R_2+R_3}\xmat{1&0&-5&0\\0&1&-8&0\\0&0&23&0}
      \rightarrow \rops{R_3: 1/23R_3}\\\xmat{1&0&-5&0\\0&1&-8&0\\0&0&1&0}
      \rightarrow \rops{R_1: R_1+5R_3\\R_2: R_2+8R_3}\xmat{1&0&0&0\\0&1&0&0\\0&0&1&0}
      = [\widetilde{A}|\widetilde{b}]$
    \item $[A|\Vect{b}] = \xmat{2&3&-1&1\\0&-2&1&2\\1&0&-2&-1}
      \rightarrow \rops{R_1: R_3\\R_3: R_1}\xmat{1&0&-2&-1\\0&-2&1&2\\2&3&-1&1}
      \rightarrow \rops{R_3: -2R_1 + R_3}\\\xmat{1&0&-2&-1\\0&-2&1&2\\0&3&3&3}
      \rightarrow \rops{R_2: -1/2R_2}\xmat{1&0&-2&-1\\0&1&-1/2&-1\\0&3&3&3}
      \rightarrow \rops{R_3: -3R_2+R_3}\\ \xmat{1&0&-2&-1\\0&1&-1/2&-1\\0&0&9/2&6}
      \rightarrow \rops{R_3: 2/9R_3}\xmat{1&0&-2&-1\\0&1&-1/2&-1\\0&0&1&4/3}
      \rightarrow \rops{R_1: R_1 + 2R_3\\R_2: R_2 + 1/2R3} \\
                  \xmat{1&0&0&5/3\\ 0&1&0&-1/3\\ 0&0&1&4/3}
      = [\widetilde{A}|\widetilde{b}]$
  \end{enumerate}
\end{exercise}

\begin{exercise}{3}
  \begin{enumerate}
    \item $A = \xmat{1&2&3 \\ 4&5&6} \rightarrow
      \rops{R_2: -4R_1 + R_2} \xmat{1&2&3 \\ 0&-3&-6} \rightarrow
      \rops{R_2: -1/3R_2} \xmat{1&2&3 \\ 0&1&2} \rightarrow
      \rops{R_1: R_1 - 2R_2} \xmat{1&0&-1 \\ 0&1&2} = \widetilde{A}$

    \item $A = \xmat{1&-1&1 \\ -1&0&2 \\ -1&1&1} \rightarrow
      \rops{R_2: R_1 + R_2\\ R_3: R_1 + R_3} \xmat{1&-1&1 \\ 0&-1&3 \\ 0&0&1} \rightarrow
      \rops{R_2: -R_2} \xmat{1&-1&1 \\ 0&1&-3 \\ 0&0&1} \rightarrow
      \rops{R_1: R_1 + R_2} \xmat{1&0&-2 \\ 0&1&-3 \\ 0&0&1} \rightarrow
      \rops{R_1: R_1 + 2R_3\\ R_2: R_2 + 3R_3} \xmat{1&0&0 \\ 0&1&0 \\ 0&0&1}
      = \widetilde{A}$

    \item $A = \xmat{1&2&3&5 \\ 2&3&0&-1 \\ 0&1&2&3} \rightarrow
        \rops{R_2: -2R_1 + R_2} \xmat{1&2&3&5 \\ 0&-1&-6&-11 \\ 0&1&2&3} \rightarrow
        \rops{R_2: R_3 \\ R_3: R_2} \\ \xmat{1&2&3&5 \\ 0&1&2&3 \\ 0&-1&-6&-11} \rightarrow
        \rops{R_1: R_1 - R_2 \\ R_3: R_2 + R_3}
          \xmat{1&0&-1&-1 \\ 0&1&2&3 \\ 0&0&-4&-8} \rightarrow
        \rops{R_3: -1/4R_3} \\ \xmat{1&0&-1&-1 \\ 0&1&2&3 \\ 0&0&1&2} \rightarrow
        \rops{R_1: R_1 + R_3 \\ R_2: R_2 - 2R_3}
          \xmat{1&0&0&1 \\ 0&1&0&-1 \\ 0&0&1&2} = \widetilde{A}$

    \item $A = \xmat{1&3&-1&4 \\ 1&2&1&2 \\3&7&1&9} \rightarrow
      \rops{R_2: -1R_1 + R_2\\ R_3: -3R_1 + R_3}
        \xmat{1&3&-1&4 \\ 0&-1&2&-2 \\ 0&-2&4&-3} \rightarrow
      \rops{R_2: -R_2} \\ \xmat{1&3&-1&4 \\ 0&1&-2&2 \\ 0&-2&4&-3} \rightarrow
      \rops{R_1: R_1 - 3R_2\\ R_3: 2R_2 + R_3}
        \xmat{1&0&5&-2 \\ 0&1&-2&2 \\ 0&0&0&1} = \widetilde{A}$

    \item $A = \xmat{1&1&1&1 \\ 2&-3&3&3 \\ 1&-4&2&2} \rightarrow
      \rops{R_2: -2R_1 + R_2\\ R_3: -R_1 + R_3}
        \xmat{1&1&1&1 \\ 0&-5&1&1 \\ 0&-5&1&1} \rightarrow
      \rops{R_2: -1/5R_2} \\
        \xmat{1&1&1&1 \\ 0&1&-1/5&-1/5 \\ 0&-5&1&1} \rightarrow
      \rops{R_1: R_1 - R_2\\ R_3: 5R_2 + R_3}
        \xmat{1&0&6/5&6/5 \\ 0&1&-1/5&-1/5 \\ 0&0&-1&-1} \rightarrow
      \rops{R_3: -R_3} \\
        \xmat{1&0&6/5&6/5 \\ 0&1&-1/5&-1/5 \\ 0&0&1&1} \rightarrow
      \rops{R_1: R_1 - 6/5R_3\\ R_2: R_2 + 1/5R_3}
        \xmat{1&0&0&0 \\ 0&1&0&0 \\ 0&0&1&1} = \widetilde{A}$
  \end{enumerate}
\end{exercise}

\begin{exercise}{5}
  \begin{enumerate}
    \item Multiplying a row by a nonzero number $c$.

        Such operation can be undone by multiplying the result row with $1/c$, since $\forall x \in \mathbb{R}$, $(1/c)cx = ((1/c)c)x = x$, namely $1/c$ is $c$'s inverse with $1$ being the identity providing that $c \neq 1$, and multiplication over real numbers is associative. Since $1/c /neq = 0$, this undoing is also a row operation.

    \item Adding a multiple $c$ of a row $r_0$ onto another row $r_x$.

        Such operation can be undone by adding the result row with $-cr_0$, since $\forall c \in \mathbb{R}$, $-cr_0 + cr_0 + r_x = (-cr_0 + cr_0) + r_x = (-c+c)r_0 + r_x = r_x$, namely $-c$ is $c$'s inverse with $0$ being the identity, addition over vectors of real numbers is associative, and scaling of vectors of real numbers is distributive. Since $-cr_0$ is a multiple of $r_0$, this undoing is also a row operation.

    \item Exchanging two rows.

    Exchanging them back. This is also a row reduction.
  \end{enumerate}

  Therefore, any row operation can be undone by another row reduction. \rQED
\end{exercise}

\begin{exercise}{8}
  Let the number of rows of $A$ be $n$. Since each row has at most one pivot, $\widetilde{A}$ has at most $n$ pivots.

  Supposing that $\widetilde{A}$ has $n$ pivots. Since each column has at most one pivot, the last column of $\widetilde{A}$ has a pivot. Since each row must have exactly one pivot, the last row of $\widetilde{A}$ has a pivot. Since the pivotal $1$ of a lower row is always to the right of the pivotal $1$ of a higher row, and the $n-1$-th rows have $n-1$ pivots, the last column of the last row of $\widetilde{A}$ must be the pivot. Since the $n-1 \times n-1$ matrix of the upper left part of $\widetilde{A}$ has the rest $n-1$ pivots, with all those requirements still applied, the rightmost column's bottommost row is a pivot. Inductively, one can prove that all pivots lie on the orthogonal of $\widetilde{A}$. Since in every column that contains a pivotal 1, all other entries are 0, $\widetilde{A}$'s rest entries must be 0. Therefore, $\widetilde{A}$ is an identity matrix.

  Supposing that $\widetilde{A}$ has less than $n$ pivots. Since in every row, the first nonzero entry is $1$, there must be some rows consisting only 0, otherwise there will be $n$ pivots. Then, since any rows consisting entirely of $0$'s are at the bottom, such $\widetilde{A}$'s bottom row must be all $0$. Therefore, $\widetilde{A}$ is either the identity or the last row is a row of zeros. \rQED
\end{exercise}


\section{Relations}

\begin{definition}
  A \textbf{relation} on a set $A$ is a subset $C$ of the cartesian product $A \times A$.

  For a relation $C$ on $A$, we use the notation $xCy$ to mean $(x, y) \in C$, or ``$x$ is in the relation $C$ to $y$.''
\end{definition}

\begin{definition}
  An \textbf{equivalence relation} on a set $A$ is a relation $C$ on $A$ having the following 3 properties: (we use $\sim$ to denote the equivalence relation)
  \begin{enumerate}
    \item Reflexivity: $xCx \quad \forall x \in A$ ($x \sim x$)
    \item Symmetry: If $xCy$ then $yCx$ ($x \sim y \implies y \sim x$)
    \item Transitivity: If $xCy$ and $yCz$ then $xCz$ ($x \sim y \land y \sim z \implies x \sim z$)
  \end{enumerate}
\end{definition}

\begin{definition}
  We call a subset of $E$ of $A$ the \textbf{equivalence class} determined by $x$ as the equation
  \begin{equation}
    E = \pbrac{
      y \mid y \sim x
    }
  \end{equation}
\end{definition}

\begin{definition}
  A \textbf{partition} of a set $A$ is a collection of disjoint nonempty subsets of $A$ whose union is all of $A$.
\end{definition}

\begin{definition}
  A relation $C$ on a set $A$ is called an \textbf{order relation} if it has the following properties:
  \begin{enumerate}
    \item Comparability: For every $x, y \in A$ for which $x \neq y$, either $xCy$ or $yCx$
    \item Nonreflexitivity: For no $x \in A$ does $xCx$ hold
    \item Transtivity: If $xCy$ and $yCz$ then $xCz$
  \end{enumerate}
\end{definition}

\begin{definition}
  if $X$ is a set and $<$ is an order relation on $X$, and if $a<b$, we use the notation $(a, b)$ to denote the set
  \begin{equation}
    \pbrac{
      x \mid a < x < b
    };
  \end{equation}
  it is called an \textbf{open interval} in $X$. If this set if empty, we call
  \begin{itemize}
    \item $a$ the \textbf{immediate predecessor} of $b$
    \item $b$ the \textbf{immediate successor} of $a$
  \end{itemize}
\end{definition}

\begin{definition}
  Suppose $A, B$ are two sets with order relations $<_A$ and $<_B$ respectively. We say that $A, B$ have the same \textbf{order type} if there is a bijective correspondence between them that preserves order; that is, if there exists a bijective function $f : A \to B$ such that
  \begin{equation}
    a_1 <_A a_2 \implies f(a_1) <_B f(a_2)
  \end{equation}
\end{definition}

\begin{definition}
  An ordered set $A$ has the \textbf{least upper bound property} if every
  nonempty subset $A_0$ of $A$ that is bounded above has a least upper bound.
  Analogously, the set $A$ is said to have the \textbf{greatest lower bound
  property} if every nonempty subset $A_0$ of $A$ that is bounded below has a
  greatest lower bound.
\end{definition}

\section*{Exercises}

\bx{
  Check equivalence relation:
  \begin{itemize}
    \item Reflexivity is obvious
    \item Equality is symmetric, so the relation is too
    \item Equality is transitive, so the relation is too
  \end{itemize}

  This looks like a bunch of parabolas of the form $y = x^2 + C$ on the plane, see figure \ref{chap1:sec3:p1:fig:1}

  \begin{figure}[H]
    \centering
    \def\domainSize{2}
    \begin{tikzpicture}
      \begin{axis}[
        axis y line = middle,
        axis x line = middle,
      ]

      \addplot[
        color=red,
        ultra thick,
        domain=-\domainSize:\domainSize,
        samples=100
      ]{
        x^2
      };

      \addplot[
        color=blue,
        ultra thick,
        domain=-\domainSize:\domainSize,
        samples=100
      ]{
        x^2 + 0.25
      };

      \addplot[
        color=green,
        ultra thick,
        domain=-\domainSize:\domainSize,
        samples=100
      ]{
        x^2 + 0.5
      };

      \addplot[
        color=black,
        ultra thick,
        domain=-\domainSize:\domainSize,
        samples=100
      ]{
        x^2 + 0.75
      };

      \end{axis}
    \end{tikzpicture}
    \caption{Plotting the partition defined by the equivalence relation}
    \label{chap1:sec3:p1:fig:1}
  \end{figure}
}

\bx{
  Reflexivity will still hold in $A_0$, since $A_0 \in A$, and $C$ applies
  to any element $x \in A$.
  Symmetry still holds, since $x, y \in A$, and transitivity also holds since
  $x, y, z \in A$. The idea is that $A_0$'s elements are contained in $A$, so
  all the equivalence relation properies still hold.
}

\bx{
  We are assuming $\exists b$ such that $aCb$. If there is no such $b$, then we do not have $aCa$.
}
\bx{
  \item \ea{
    \item Let us check the properties
    \begin{itemize}
      \item Reflexive: $f(a) = f(a)$ is trivial
      \item Symmetric: If we have $f(a) = f(b)$, then $f(b) = f(a)$
      \item Transitive: Equality is transitive, so this also holds
    \end{itemize}

    \item $A^\ast$ is a partition of $B$, so a bijective correspondence exists.
  }
}

\bx{
  \ea{
    \item $S'$ is an equivalence relation because we can imagine partitions of
    $y - x = z$ for $z \in \mathbb{Z}$.
    In $S$, we notice that $y - x = 1$, and $1 \in \mathbb{Z}$, so we know every
    relation in $S$ is also in $S'$, therefore $S \subset S'$.
    See \ref{chap1:sec3:p5:fig:1} for how to visualize these equivalence
    classes.
    \begin{figure}[H]
      \centering
      \def\domainSize{2}
      \begin{tikzpicture}
        \begin{axis}[
          axis y line = middle,
          axis x line = middle,
        ]

        \addplot[
          color=green,
          ultra thick,
          domain=-\domainSize:\domainSize,
          samples=100
        ]{
          x - 0.2
        };

        \addplot[
          color=blue,
          ultra thick,
          domain=-\domainSize:\domainSize,
          samples=100
        ]{
          x - 0.1
        };

        \addplot[
          color=red,
          ultra thick,
          domain=-\domainSize:\domainSize,
          samples=100
        ]{
          x
        };

        \addplot[
          color=blue,
          ultra thick,
          domain=-\domainSize:\domainSize,
          samples=100
        ]{
          x + 0.1
        };

        \addplot[
          color=green,
          ultra thick,
          domain=-\domainSize:\domainSize,
          samples=100
        ]{
          x + 0.2
        };

        \addplot[
          color=black,
          ultra thick,
          domain=-\domainSize:\domainSize,
          samples=100
        ]{
          x + 0.3
        };

        \end{axis}
      \end{tikzpicture}
      \caption{Plotting the partition defined by the equivalence relation}
      \label{chap1:sec3:p5:fig:1}
    \end{figure}

    \item If their intersection is empty, then this is trivially true.

    Otherwise, if we have some nonempty intersection, since we know any elements
    in this intersection are also part of some equivalence relation, all the
    equivalence relation properties apply, so this intersection is also an
    equivalence relation on $A$.

    \item \TODO: I'm pretty confused about this question. Wouldn't the intersection of all equivalence relations that contain $S$ just end up with $S$?
  }
}

\bx{
  Showing it is an order relation
  \begin{itemize}
    \item Comparability: Per the rule, we will always have $xCy$ or $yCx$.
    \item Nonreflexitivity: We have a tiebreaker rule that prevents $xCx$.
    \item Transitivity: Intuitively, there is an ordering at the highest level
    with $y-x^2$ value. If there are ties there, we check with $x$ values for
    ordering.
  \end{itemize}

  Yeah sorry I didn't feel like doing the order relation formally, but hopefully
  the geometric solution can help explain more. There's just a lot of casework
  and mundane proof so I didn't feel like writing it out.

  Geometrically, this is like the partition in \ref{chap1:sec3:p1:fig:1}, except
  parabolas that are higher up are ordered ``higher''. Within a parabola, the
  values on the left are less than the values on the right.
}

\bx{
  A restriction is a subset of the larger set, so all the order relation properties will still hold.
}

\bx{
  I assume the author wants us to check the $x^2 < y^2$, if $x^2 = y^2$, then $x < y$.

  \begin{enumerate}
    \item Comparability: If $x^2 \neq y^2$, either $x^2 < y^2$ or $x^2 > y^2$.
    If $x^2 = y^2$, then it must be the case that $x, y \neq 0$, and one of $x,
    y$ is negative and the other positive, so we either have $x > y$ or $x < y$.

    \item Nonreflexitivity: $xCx$ means $x^2 = x^2$, so we would check $x < \text{ or } > x$, but this is not possible since $x = x$.
    \item Transitivity: If $xCy$ then $x^2 < y^2$ or $x < y$, and if we have
    $yCz$ then $y^2 < z^2$ or $y < z$. In all cases, we can conclude $x^2 < z^2$
    or $x < z$. I'm being lazy with casework here.
  \end{enumerate}
}

\bx{
  We want to check that a dictionary order relation is an order relation.

  \begin{enumerate}
    \item Comparability: For any two $(a_1, b_1), (a_2, b_2)$, we have 2 cases:
      \begin{enumerate}
        \item $a_1 <_A a_2$ or $a_2 <_A a_1$ since $<_A$ is an order relation on $A$.
        Then we know $(a_1, b_1) < (a_2, b_2)$ or $(a_1, b_1) > (a_2, b_2)$.
        \item $a_1 = a_2$. Then we use the same argument with $b_1, b_2$, that
        either $b_1 <_B b_2$ or $b_2 <_B b_1$, which then shows the
        corresponding $<$ and $>$ on the tuple.
      \end{enumerate}

    \item Nonreflexitivity: if we have some $(a, b)$, we know by $<_A$ that $a
    <_A a$ does not hold, so the overall order relation is not possible.

    \item Transitivity: If we have $(a_1, b_1) < (a_2, b_2) < (a_3, b_3)$, then we have 2 cases for the first tuple, and 2 cases for the second tuple.
    \begin{enumerate}
      \item $a_1 <_A a_2 <_A a_3$: then we can use the transitive property of $<_A$
      \item $a_1 <_A a_2 = a_3$: we can see that $a_1 <_A a_3$
      \item $a_1 = a_2 <_A a_3$:  we can see that $a_1 <_A a_3$
      \item $a_1 = a_2 = a_3$: then we must have $b_1 <_B b_2 <_B b_3$, so we can use the transitive property of $<_B$
    \end{enumerate}
  \end{enumerate}
}

\bx{
  \begin{itemize}
    \item One way to see that this is an order preserving function is that the derivative is always positive between $(-1, 1)$,
    \begin{equation*}
      f'(x) = \frac{
        x^2 + 1
      }{
        \pa{1-x^2}^2
      },
    \end{equation*}
    which means the function is monotonically increasing, and thus will preserve the order, since monotonically increasing functions have the property that
    \begin{equation*}
      a < b \implies f(a) < f(b).
    \end{equation*}

    \item This is just an algebra exercise...pretty easy to verify
  \end{itemize}
}

\bx{
  \AFSOC there is more than one immediate successor to some $a$, call them $b$ and $c$. Then by order set properties, we know that either $b < c$ or $c < b$.
  In either case, we end up finding that $b$ or $c$ cannot be immediate successors, since for example, if $b < c$, we have that $(a, c)$ is not empty.

  The argument for immediate predecessor is symmetric to this argument.

  To show there can only be one smallest element, we can \AFSOC there is more
  than one. If we call these $a, a'$, we know from ordering properties that WLOG
  $a < a'$, then $a'$ is not the smallest element, so this is a contradiction.

  The argument for the largest element is symmetric.
}

\bx{
  \begin{enumerate}[label=(\roman**)]
    \item Every element has an immediate predecessor. For some $(x, y)$, the
    immediate predecessor is $(x, y+1)$.
    There is no smallest element, since you can always find a smaller element,
    i.e. for any $(x, y)$, $(x-1, y) < (x, y)$.

    \item The immediate predecessor for some $(x, y)$ is $(x+1, y+1)$. There is
    no smallest element, since you can always find $(x-1, y) < (x, y)$ for any
    $(x, y)$.

    \item The immediate predecessor for some $(x, y)$ is $(x-1, y+1)$. There is
    no smallest element, since for any $(x, y)$, you have $(x-1, y) < (x, y)$.
  \end{enumerate}

  Not rigorous, but geometrically, the first ordering is like a zigzag on the
  plane, the second one is like $y = x + C$, and the third is $y = -x + C$, so
  these orderings are all different.
}

\bx{
  Suppose $A$ has the least upper bound property, meaning every nonempty subset
  $A_0$ of $A$ is bounded above by some least upper bound.
  \AFSOC $A$ does not have the greatest lower bound property, that is $\exists
  A_1 \subset A$ such that $A_1$ does not have a greatest lower bound.
  If this is the case, then consider the lower bound $a$ for this set $A_1$, and
  consider the set $A_2$, which we define as
  \begin{equation*}
    A_2 = \pbrac{a' \mid a' \geq a}
  \end{equation*}
  Notice that $A_2$ cannot be empty, or else $a$ is the only, and therefore greatest lower bound for $A_1$.
  Now, from the definition of $A_2$, we can see that $a$ is an upper bound for
  $A_2$. However, we now claim that there is no least upper bound for $A_2$.
  Because if there were, call it some $a_2$, then $a_2$ would be the greatest
  lower bound for $A_1$, since $a_2$ is larger than all lower bounds of $A_1$.
  This is a contradiction, since we assumed that $A_1$ does not have a greatest lower bond.
  Therefore, we must conclude that every subset of $A$ has a greatest lower bound.
}

\bx{
  \ea{
    \item If $C$ is symmetric, then $(a, b) \in C \implies (b, a) \in C$, which means $D \subset C$.

    If $C = D$, then $(a, b) \in C \implies (b, a) \in D = C$, so therefore $C$ is symmetric.

    \item We will check the order relation properties for $D$
    \begin{enumerate}
      \item Comparability: For any $(b, a) \in D$, we know $(a, b) \in C$, so we
      know either $b < a$ or $a < b$.
      \item Nonreflexitivity: $(b, b) \in D$ would imply $(b, b) \in C$, but $C$
      is an order relation so this is not possible.
      \item Transitivity: $(c, b) \in D, (b, a) \in D$. We know that $(a, b),
      (b, c) \in C$, so we know $(a, c) \in C$, so therefore $(c, a) \in D$,
      which proves transitivity.
    \end{enumerate}

    \item The other direction of the argument is symmetric.
  }
}

\bx{
  \ea{
    \item Let us show that
    \begin{itemize}
      \item 1 is the least upper bound for $[0, 1]$. We can see this because if
      you pick any smaller of an upper bound $u$, $u < 1$ and thus is not an
      upper bound for the set.

      \item 1 is the least upper bound for $[0, 1)$. Suppose we have some other
      least upper bound $u$ such that $x \in [0, 1), x \leq u$, but $u < 1$.
      Then consider $u' = u + \epsilon/2$, where $\epsilon = \frac{1-u}{2}$.
      Then $u'$ is $<1$ but $u' > u$ and is also an upper bound, which means $u$
      was not the least upper bound. Therefore we have reached a contradiction
      and conclude that 1 is the least upper bound.
    \end{itemize}

    \item $[0, 1] \times [0, 1]$ with dictionary ordering has least upper bound property.
    This is because for any subset, if we look at the first coordinate, it is in
    $[0, 1]$, which we showed has the least upper bound property, so call this
    upper bound $u_1$.
    Similarly, for the second coordinate it is also in $[0, 1]$, so we have a
    least upper bound $u_2$ for this coordinate.
    Then we have $(u_1, u_2)$ is a least upper bound for any subset in $[0, 1] \times [0, 1]$.

    This argument holds for $[0, 1] \times [0, 1)$ and $[0, 1) \times [0, 1]$.
  }
}


\documentclass[final]{siamart171218}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{bm}

\setlength{\oddsidemargin}{0.65in}
\setlength{\evensidemargin}{0.65in}

\title{Sketch of the ``alternating SQP'' method for fitting Poisson
  topic models}

\author{Peter Carbonetto\thanks{Dept. of Human Genetics and the Research Computing Center, University of Chicago, Chicago, IL}}

\begin{document}

\maketitle

\section{Problem statement}

Given an $n \times p$ matrix of counts $X$ with entries $x_{ij} \geq
0$, the aim is to fit a Poisson model of the counts,
\begin{align}
p(x) &= \prod_{i=1}^n \prod_{j=1}^p p(x_{ij}) \nonumber \\
     &= \prod_{i=1}^n \prod_{j=1}^p \mathrm{Poisson}(x_{ij}; \lambda_{ij}),
\label{eq:poisson-likelihood}
\end{align}
in which the Poisson rates are given by $\lambda_{ij} = \sum_{k=1}^K
l_{ik} f_{jk}$. The model is determined by a $p \times K$ matrix $F$
with entries $f_{ik} \geq 0$ (the ``factors'') and an $n \times K$
matrix $L$ with entries $l_{ik} \geq 0$ (the ``loadings''). Fitting
$F$ and $L$ is equivalent to non-negative matrix factorization with
the ``beta-divergence'' cost function \cite{lee-2001}. It can also be
used to recover a maximum-likelihood estimate for the latent Dirichlet
allocation (LDA) model \cite{blei-2003}. So fitting this model is
useful for a wide range of applications.

The log-likelihood for the Poisson model is
\begin{equation}
\log p(x \,|\, F, L) \propto
  \sum_{i=1}^n \sum_{j=1}^p x_{ij} \log
  ({\textstyle \sum_{k=1}^K l_{ik} f_{jk}})
    - \sum_{i=1}^n \sum_{j=1}^p \sum_{k=1}^K l_{ik} f_{jk},
\label{eq:poisson-log-likelihood}
\end{equation}
where the constant of proportionality is obtained from factorial terms
in the Poisson densities. Our specific aim is to find a $F$ and $L$
that maximizes the log-likelihood \eqref{eq:poisson-log-likelihood};
that is, we would like to solve
\begin{equation}
\begin{array}{ll}
\mbox{minimize} & \ell(F,L) \equiv -\log p(x \,|\, F, L) \\
\mbox{subject to} & F \geq 0, L \geq 0.
\end{array}
\label{eq:problem}
\end{equation}
In the next section, we derive an efficient approach to doing this.

\section{Block co-ordinate descent strategy}

Our strategy for solving \eqref{eq:problem} is to alternate between
solving for $F$ with $L$ fixed, and solving for $L$ with $F$
fixed. When solving for $F$ with $L$ fixed (and vice versa), the
problem naturally decomposes into a collection of much smaller
subproblems that are much more tractable to solve. All the subproblems
are of the following form:
\begin{equation}
\begin{array}{ll}
\mbox{minimize} & \phi(y; B, w) \\
\mbox{subject to} & \mbox{$y_k \geq 0$ for all $k = 1, \ldots, K$},
\end{array}
\label{eq:subproblem}
\end{equation}
in which the objective function is defined as
\begin{equation}
\phi(y; B, w) =
    - \sum_{i=1}^n w_i \log\big({\textstyle \sum_{k=1}^K b_{ik} y_k}\big)
    + \sum_{i=1}^n \sum_{k=1}^K b_{ik} y_k.
\label{eq:subproblem-objective}
\end{equation}
To see the connection between subproblem \eqref{eq:subproblem} and the
original optimization problem \eqref{eq:problem}, observe that the
negative log-likelihood can be recovered as
\begin{equation}
-\log p(x \,|\, F, L) = \sum_{i=1}^n \phi(l_i; F, x_i),
\end{equation}
where $x_i$ is the $i$th row of $X$ and $l_i$ is the $i$th row of $L$.
Alternatively, it can be recovered as
\begin{equation}
-\log p(x \,|\, F, L) = \sum_{j=1}^p \phi(f_j; L, x_j),
\end{equation}
in which $x_j$ is the $j$th column of $X$, and $f_j$ is the $j$th row
of $F$. Therefore, when $F$ is fixed, each row of $L$ can be
separately optimized by solving a problem of the form
\eqref{eq:subproblem}, and when $L$ is fixed, each row of $F$ can be
separately optimized by solving a problem of the form
\eqref{eq:subproblem}.

Initially this may seem like a sensible strategy, but directly
optimizing \eqref{eq:subproblem} turns out to be difficult to do for
numerical reasons: in practice, the entries of $B$ can be very large
or very small, resulting in solutions $y$ in which all the entries are
either very large or very small. This makes it difficult to devise an
algorithm that will work well for all possible input matrices $B$.

I propose to solve for $y$ indirectly by instead solving
\begin{equation}
\begin{array}{ll}
\mbox{minimize}   & f(t; P, u) \\
\mbox{subject to} & \mbox{$t_k \geq 0$ for all $k = 1, \ldots, K$},
\label{eq:subproblem-modified}
\end{array}
\end{equation}
in which the new objective function is
\begin{equation}
f(t; P, u) =
    - \sum_{i=1}^n u_i \log\big({\textstyle \sum_{k=1}^K p_{ik} t_k}\big)
    + \sum_{k=1}^K t_k,
\end{equation}
where I've defined
\begin{align*}
u_i    &= \frac{w_i}{\sum_{i'=1}^n w_{i'}} \\
p_{ik} &= b_{ik} \times \frac{\sum_{i'=1}^n w_{i'}}{\sum_{i'=1}^n b_{i'k}}.
\end{align*}
After finding the solution $t^{\star}$ to
\eqref{eq:subproblem-modified}, the solution $y^{\star}$ to
\eqref{eq:subproblem} is recovered as
\begin{equation}
y_k^{\star} = t_k^{\star} \times
  \frac{\sum_{i=1}^n w_i}{\sum_{i=1}^n b_{ik}}.
\end{equation}
The main advantage of solving \eqref{eq:subproblem-modified} is that
the solution is numerically well behaved; in particular, the entries
of the solution $t^{\star}$ sum to 1 \cite{kim-2019}. A
straightforward way to appreciate this result is to compare the
complementary slackness condition $\sum_{k=1}^K g_k x_k = 0$ for the
original and modified subproblems, where $g_k$ denotes the partial
derivative of the objective (either $\phi$ or $f$) with respect to the
$k$th co-ordinate (either $y_k$ or $t_k$). Further, since problem
\eqref{eq:subproblem-modified} is obtained by a simple linear
transformation of the variables, any iterate that leads to an
improvement in the solution to the modified subproblem will produce an
improvement in the solution to the original subproblem, and vice
versa.

\section{Karush-Kuhn-Tucker conditions}

To assess the quality of a solution, here I derive the first-order
Karush-Kuhn-Tucker (KKT) conditions for \eqref{eq:problem}. Written
in matrix notation, they are
\begin{align}
\nabla_F \ell^{\star}(F,L,\Omega,\Gamma) &= 0 \label{eq:kkt-1} \\
\nabla_L \ell^{\star}(F,L,\Omega,\Gamma) &= 0 \label{eq:kkt-2} \\
\Omega \odot F &= 0 \label{eq:kkt-3} \\
\Gamma \odot L &= 0 \label{eq:kkt-4}.
\end{align}
Here, I have introduced matrices of Lagrange multipliers $\Omega \geq
0, \Gamma \geq 0$ associated with the non-negativity constraints $F
\geq 0, L \geq 0$, $A \odot B$ is the Hadamard (elementwise) product
of matrices $A$ and $B$, and $\ell^{\star}(F, L, \Omega, \Gamma)$ is the
Lagrangian function:
\begin{equation}
\phi(F, L, \Omega, \Gamma) = \ell(F,L)
- \sum_{i=1}^n \sum_{k=1}^K \gamma_{ik} l_{ik}
- \sum_{j=1}^m \sum_{k=1}^K \omega_{jk} f_{jk}.
\end{equation}
Applying conditions (\ref{eq:kkt-1}, \ref{eq:kkt-2}), we obtain
\begin{align}
\Omega &= (1 - A)^TL \\
\Gamma &= (1 - A)F,
\end{align}
in which $A$ is defined to be the $n \times m$ matrix with entries
$a_{ij} = x_{ij} / \lambda_{ij}$. Next, applying the complementary
conditions (\ref{eq:kkt-3}, \ref{eq:kkt-4}) results in the following
expressions for the residuals of the complementary conditions:
\begin{align}
r_F &= F \odot (1 - A)^TL \\
r_L &= L \odot (1 - A)F.
\end{align}
These residuals should vanish near a minimizer of \eqref{eq:problem}.

\bibliographystyle{siamplain}
\bibliography{altsqp}

\end{document}