\selectlanguage{british}%

\chapter{An Alternative to Contextuality\label{chap:An-Alternative-to-Contextuality}}

\section{Rationale}

In principle, we have achieved what we set out to; we were able to
understand how BM explains PM, a test of contextuality. Having clarified
various concepts through the heavy machinery of BM, here we attempt
to further our understanding, without using BM. We will start with
refining the assumptions of the PM test. We will see how, when one
of the assumptions is refined, one version holds for QM. The other
is not even experimentally testable. Thereafter, we construct simple
toy models, one of which is unambiguously non-contextual (unlike BM,
where one must restrict the measurement process to make the same claim).
This is used to demonstrate the distinction between the two versions
of multiplicativity. We then generalize this toy model to produce
statistics consistent with QM, for an arbitrary, but discrete Hilbert
space. Finally, we generalize this to continuous variables (without
spins) and obtain a consistent one dimensional completion of QM. Its
relation with other known completions (BM \& RS) are also discussed.
This model aims to facilitate easy value assignments in case of the
phase space versions of determinism and contextuality tests. These
are hard to compute from BM and RS.

\section{Multiplicativity \label{sec:Multiplicativity}}

Let us recall two assumptions from \prettyref{sec:Peres-Mermin-Revisited},
that of non invasiveness and multiplicativity. Let $\hat{B}_{1},\hat{B}_{2},\dots,\hat{B}_{n}$
be a set of commuting observables and also let $m_{i}(\hat{*})$ represent
the value assigned to an operator. When $m$ occurs multiple times
in a given expression, then $i$ encodes the sequence of measurements.
If we assume non invasiveness, then multiplicativity has a clear meaning;
\begin{defn*}
In a Hilbert space $\mathcal{H}$, a map $m_{i}$ from $\mathcal{H}\otimes\mathcal{H}^{\dagger}$
$\to$ $\mathbb{R}$, is \emph{multiplicative} iff
\begin{equation}
m_{1}(f(\hat{B}_{1},\hat{B}_{2},\dots\hat{B}_{n}))=f(m_{1}(\hat{B}_{1}),m_{1}(\hat{B}_{2}),\dots m_{1}(\hat{B}_{n})).\label{eq:multiplicativity}
\end{equation}

\end{defn*}
Here we have generalized the notion from simply a product to any arbitrary
function. We have used $1$ as the subscript for each $m$, since
in this case, the sequence of a measurement is irrelevant. If one
relaxes the no-disturbance assumption, then the following also becomes
a possibility.
\begin{defn*}
In a Hilbert space $\mathcal{H}$, a map $m_{i}$ from $\mathcal{H}\otimes\mathcal{H}^{\dagger}$
$\to$ $\mathbb{R}$, is \emph{sequentially-multiplicative} iff
\begin{equation}
m_{1}(f(\hat{B}_{1},\hat{B}_{2},\dots,\hat{B}_{n}))=f(m_{k_{1}}(\hat{B}_{1}),m_{k_{2}}(\hat{B}_{2}),\dots m_{k_{n}}(\hat{B}_{n})),\label{eq:seqMultiplicativity}
\end{equation}
where $\mathbf{k}\equiv(k_{1},k_{2},\dots k_{n})\in\{(1,2,3,\dots n),(2,1,3,\dots n),$
+ all permutations$\}$.
\end{defn*}
We take a moment to understand the meaning of these statements more
carefully, in the context of QM. Consider the state $\left|\chi\right\rangle =\left|00\right\rangle $,
$\hat{B}_{1}\equiv\hat{\sigma}_{x}\otimes\hat{\sigma}_{y}$ and $\hat{B}_{2}\equiv\hat{\sigma}_{y}\otimes\hat{\sigma}_{x}$
while $\hat{C}\equiv f(\hat{B}_{1},\hat{B}_{2})=\hat{B}_{1}\hat{B}_{2}=\hat{\sigma}_{z}\otimes\hat{\sigma}_{z}$.
Measuring $\hat{B}_{1}$ yields $\pm1$ with equal probabilities,
and so does a measurement of $\hat{B}_{2}$. However, a measurement
of $\hat{C}$ is guaranteed to yield $+1$. QM can't explicitly contradict
\prettyref{eq:multiplicativity}, since it only yields probabilities.
Further, a measurement in QM leads to disturbance, unless ofcourse
we consider simultaneous eigenstates. Therefore, after making a measurement,
say $m_{1}(\hat{B}_{1})$, one can't obtain $m_{1}(\hat{B}_{2})$.
Even if we agree to start the measurement with the same state, we
can't make the hidden variables identical. However, \prettyref{eq:seqMultiplicativity}
is certainly testable in QM, for one can first measure $\hat{B}_{1}$
to obtain $m_{1}(\hat{B}_{1})$, then measure $\hat{B}_{2}$, to find
$m_{2}(\hat{B}_{2})$ and obtain $m_{1}(\hat{B}_{1})m_{2}(\hat{B}_{2})$.
Starting from the state $\left|\chi\right\rangle $ again, one can
measure $\hat{C}$ to get $m_{1}(\hat{C})$ which has a precise value
in this case. One may again claim that experimentally, there maybe
some hidden variables which can't be made identical and thus after
measuring the $B$s, it is impossible to obtain $m_{1}(\hat{C})$.
This difficulty is circumvented in this case, by the fact that regardless
of the value of the hidden variable, given $\left|\chi\right\rangle $
and $\hat{C}$, the measurement outcome is the certain. Thus, taking
the same state $\left|\chi\right\rangle $ is enough. One can check,
from all the possibilities, if $m_{1}(\hat{B}_{1})m_{2}(\hat{B}_{2})=m_{1}(\hat{C})$.
We tried looking for states and operators that would violate this
condition, but failed. Infact, as we will prove momentarily, \emph{QM
weakly enforces sequentially multiplicative}. However, since restoring
the hidden variables is not possible in general, \emph{QM doesn't
enforce multiplicativity}.

We worked out two proofs of weak sequential multiplicativity in QM.
The first was a restricted, brute force proof. The second we showed
holds in general, which we will discuss here.
\begin{prop*}
Let the system be in a state, s.t. measurement of $\hat{C}$ yields
repeatable results (same result each time). Then according to QM,
sequential multiplicativity (see \prettyref{eq:seqMultiplicativity})
holds, where $\hat{C}\equiv f(\hat{B}_{1},\hat{B}_{2},\dots\hat{B}_{n})$,
and $\hat{B}_{i}$ are as defined. \end{prop*}
\begin{proof}
Assume without loss of generality that $\hat{B}_{1},\hat{B}_{2},\dots\hat{B}_{n}$
are mutually compatible (commuting) and complete set of operators.
If say for instance the set is not complete, then one can add the
missing operators and label them as aforesaid. It follows that $\exists$
$\left|\mathbf{b}=\left(b_{1}^{(l_{1})},b_{2}^{(l_{2})},\dots b_{n}^{(l_{n})}\right)\right\rangle $
s.t. $\hat{B}_{i}\left|\mathbf{b}\right\rangle =b_{i}^{(l_{i})}\left|\mathbf{b}\right\rangle $,
where $l_{i}$ indexes the eigenvalues corresponding to $\hat{B}_{i}$
and that $\sum_{\mathbf{b}}\left|\mathbf{b}\right\rangle \left\langle \mathbf{b}\right|=\hat{\mathbb{I}}$.
Let the state of the system be given by $\left|\psi\right\rangle $
and it must be s.t. $\hat{C}\left|\psi\right\rangle =c\left|\psi\right\rangle $,
by assumption. For the statement to follow, one need only show that
$\left|\psi\right\rangle $ must be made of only those $\left|\mathbf{b}\right\rangle $s,
which satisfy $c=f(b_{1}^{(l_{1})},b_{2}^{(l_{2})},\dots b_{n}^{(l_{n})})$.
This is the crucial step. Proving this is so is trivial. We start
with $\hat{C}\left|\psi\right\rangle =c\left|\psi\right\rangle $
and take its inner product with $\left\langle \mathbf{b}\right|$
to get
\begin{eqnarray*}
\left\langle \mathbf{b}\right|\hat{C}\left|\psi\right\rangle  & = & c\left\langle \mathbf{b}|\psi\right\rangle ,\\
\left\langle \mathbf{b}\right|f(\hat{B}_{1},\hat{B}_{2},\dots\hat{B}_{n})\left|\psi\right\rangle  & = & c\left\langle \mathbf{b}|\psi\right\rangle ,\\
f(b_{1}^{(l_{1})},b_{2}^{(l_{2})},\dots b_{n}^{(l_{n})})\left\langle \mathbf{b}|\psi\right\rangle  & = & c\left\langle \mathbf{b}|\psi\right\rangle .
\end{eqnarray*}
Also, we have $\left|\psi\right\rangle =\sum_{\mathbf{b}}\left\langle \mathbf{b}|\psi\right\rangle \left|\mathbf{b}\right\rangle $,
from completeness. If we consider $\left|\mathbf{b}\right\rangle $s
for which $\left\langle \mathbf{b}|\psi\right\rangle \neq0$, then
we can conclude that indeed $c=f(b_{1}^{(l_{1})},b_{2}^{(l_{2})},\dots b_{n}^{(l_{n})})$.
However, when $\left\langle \mathbf{b}|\psi\right\rangle =0$, viz.
$\left|\mathbf{b}\right\rangle $s that are orthogonal to $\left|\psi\right\rangle $,
then nothing can be said. We can thus conclude that $\left|\psi\right\rangle $
is made only of those $\left|\mathbf{b}\right\rangle $s that satisfy
the required relation. That completes the proof.
\end{proof}
Note that we can't enforce sequential multiplicativity in general,
because of the hidden variable resetting objection that arises, which
was discussed with the example. However, in the PM case, where $\hat{R}_{i}$
and $\hat{C}_{j}$ are just $\pm\hat{\mathbb{I}}$, it follows that
all states are their eigenstates. Consequently, for these operators,
sequential multiplicativity must always hold. With the two notions
well defined, we now proceed with constructing two simple models,
which don't satisfy the non-invasive assumption.

\section{Simple Models}

We aim to distinguish the notion of contextuality and multiplicativity
by means of two simple models. These will not reproduce the statistics
as predicted by QM, but serve as examples and are generalized later.

\subsection{Contextual, Memory Model\label{sub:Contextual,-Memory-Model}}

The Memory Model presented here, is perhaps the simplest contextual
model. It is also non-multiplicative and invasive, viz. it doesn't
satisfy any of (1), (2) and (3), as listed in \prettyref{sec:Peres-Mermin-Revisited}.
The model is assumed to be sequentially multiplicative\footnote{infact according to QM, for row and column measurements, sequential
multiplicativity is a requirement}, and the assignments are made iteratively through the following algorithm.
We assume that the system has a matrix that can store values and has
a memory that can store the last 3 operators that were measured. Initially
assume that the matrix has all entries equal to $+1$. The algorithm
is that (i) upon measurement of an observable, yield the value as
saved in the matrix, (ii) append the observable in the 3 element memory
and (iii) update the matrix, once the context is known, to satisfy
the PM conditions on the rows and columns.

Let us take a quick example to understand how things are working.
Assume we start with measuring $\hat{A}_{33}$. The system will yield
$m_{1}(\hat{A}_{33})=1$, in accordance with the values stored initially
(see \prettyref{eq:memoryAssignments}). The memory at this stage
would read $\{*,*,\hat{A}_{33}\}$. Since the context is not yet known,
the matrix is left unchanged. Say the next operator measured is $\hat{A}_{23}$.
Then $m_{2}(\hat{A}_{23})=1$, and the memory would be updated to
$\{*,\hat{A}_{33},\hat{A}_{23}\}$. The context is now known, and
we update the matrix to ensure that $m_{1}(\hat{C})=m_{1}(A_{33})m_{2}(A_{23})m_{3}(A_{13})=-1$.
Since the first measurements yielded $+1$, we update the matrix so
that $m_{3}(A_{13})=-1$ to finally obtain the correct PM constraint.
This has been summarized by the following equations.

\begin{equation}
m_{1}(\hat{A}_{ij})=m_{2}(\hat{A}_{ij})\doteq\left[\begin{array}{ccc}
1 & 1 & 1\\
1 & 1 & 1\\
1 & 1 & 1
\end{array}\right],\,m_{3}(\hat{A}_{ij})\doteq\left[\begin{array}{ccc}
1 & 1 & -1\\
1 & 1 & 1\\
1 & 1 & 1
\end{array}\right].\label{eq:memoryAssignments}
\end{equation}
The reader can convince her(him)self that the assignments are indeed
consistent, regardless of which row/column is measured. There are
two remarks which need to be made. First, note this model is not multiplicative,
since $m_{1}(\hat{A}_{33})m_{1}(\hat{A}_{23})m_{1}(\hat{A}_{13})=1\neq m_{1}(\hat{C})=-1$,
by construction. Second observe that the value assigned to the observables,
depends explicitly on the context, thus the model is contextual.

\subsection{Non-Contextual, Toy Model \label{sub:Non-Contextual-Toy-Model}}

We now discuss another simple model, which is non-contextual and still
consistent with QM. It is also non-multiplicative and invasive, viz.
assumption (1) holds, but (2) and (3) don't, as listed in \prettyref{sec:Peres-Mermin-Revisited}.
Reference to the PM square will be made, and it has been reproduced,
\[
\hat{A}_{ij}\doteq\left[\begin{array}{ccc}
\hat{\mathbb{I}}\otimes\hat{\sigma}_{x} & \hat{\sigma}_{x}\otimes\hat{\mathbb{I}} & \hat{\sigma}_{x}\otimes\hat{\sigma}_{x}\\
\hat{\sigma}_{y}\otimes\hat{\mathbb{I}} & \hat{\mathbb{I}}\otimes\hat{\sigma}_{y} & \hat{\sigma}_{y}\otimes\hat{\sigma}_{y}\\
\hat{\sigma}_{y}\otimes\hat{\sigma}_{x} & \hat{\sigma}_{x}\otimes\hat{\sigma}_{y} & \hat{\sigma}_{z}\otimes\hat{\sigma}_{z}
\end{array}\right],
\]
for convenience. The assignments are made by a three step process.\\
\quad{}1. Initial State: Choose an appropriate initial state $\left|\psi\right\rangle $
(say $\left|00\right\rangle $).\\
\quad{}2. Hidden Variable (HV): Toss a coin and assign $c=+1$ for
heads, else assign $c=-1$.\\
\quad{}3. Predictions/Assignments: For an operator $\hat{p}'\in\{\hat{A}_{ij},\hat{R}_{i},\hat{C}_{j}\,(\forall\,i,j)\}$
check if $\exists$ a $\lambda$, s.t. $\hat{p}'\left|\psi\right\rangle =\lambda\left|\psi\right\rangle $.
If $\exists$ a $\lambda$, then assign $\lambda$ as the value. Else,
assign $c$.\\
So far the model has only predicted the outcomes of measurements.
If however, a measurement is made on the system, then although we
know the result from the predictions, we must update the state $\left|\psi\right\rangle $
of the system, depending on which observable is measured and arrive
at new predictions, using the aforesaid steps. The following final
step fills precisely this gap. \\
\quad{}4. Update: Say $\hat{p}$ was observed. If $\hat{p}$ is s.t.
$\hat{p}\left|\psi\right\rangle =\lambda\left|\psi\right\rangle $,
then leave the state unchanged. Else, find $\left|p_{\pm}\right\rangle $
(eigenkets of $\hat{p}$), s.t. $\hat{p}\left|p_{\pm}\right\rangle =\pm\left|p_{\pm}\right\rangle $
and update the state $\left|\psi\right\rangle \to\left|p_{c}\right\rangle $.

Let us explicitly apply the aforesaid algorithm, to the state $\left|\psi\right\rangle =\left|00\right\rangle $.
Say we obtained tails, and thus $c=-1$. To arrive at the assignments,
note that $\left|00\right\rangle $ is an eigenket of only $\hat{R}_{i},\hat{C}_{j}$
and $\hat{A}_{33}=\hat{\sigma}_{z}\otimes\hat{\sigma}_{z}$. Thus,
in the first iteration, all these should be assigned their respective
eigenvalues. The remaining operators must be assigned $c$ (see \prettyref{eq:toyModel}).
Two remarks are in order. First, this model is \emph{non}-multiplicative,
for $m_{1}(\hat{C}_{3})=-1\neq m_{1}(\hat{A}_{13})m_{1}(\hat{A}_{23})m_{1}(\hat{A}_{33})=1$.
Second, we must impose sequential multiplicativity as a consistency
check of the model, which in particular entails that $m_{1}(\hat{C}_{3})=m_{1}(\hat{A}_{33})m_{2}(\hat{A}_{23})m_{3}(\hat{A}_{13})$.
To illustrate this, we must choose to measure $\hat{A}_{33}$. According
to step 4, since $\left|00\right\rangle $ is an eigenstate of $\hat{A}_{33}$,
the final state remains $\left|00\right\rangle $. %
\begin{comment}
{\center {\footnotesize asb}}
\end{comment}
For the next iteration, $i=2$, say we again yield $c=-1$. Since
$\left|\psi\right\rangle $ is also unchanged, the assignment remains
invariant. For the final step, we choose to measure $\hat{p}=\hat{A}_{23}(=\hat{\sigma}_{y}\otimes\hat{\sigma}_{y})$,
to proceed with sequentially measuring $\hat{C}_{3}$. To simplify
calculations, we note
\[
\left|00\right\rangle =\frac{(\left|\tilde{+}\tilde{-}\right\rangle +\left|\tilde{-}\tilde{+}\right\rangle )/\sqrt{2}+(\left|\tilde{+}\tilde{+}\right\rangle +\left|\tilde{-}\tilde{-}\right\rangle )/\sqrt{2}}{\sqrt{2}},
\]
where $\left|\tilde{\pm}\right\rangle =\left|0\right\rangle \pm i\left|1\right\rangle $
(eigenkets of $\hat{\sigma}_{y}$). Since $\left|00\right\rangle $
is manifestly not an eigenket of $\hat{p}$, we must find $\left|p_{-}\right\rangle $,
since $c=-1$. It is immediate that $\left|p_{-}\right\rangle =\left(\left|\tilde{+}\tilde{-}\right\rangle +\left|\tilde{-}\tilde{+}\right\rangle \right)/\sqrt{2}=\left(\left|00\right\rangle +\left|11\right\rangle \right)/\sqrt{2}$,
which becomes the final state. \begin{landscape}
\begin{table}
\begin{equation}
\begin{array}{c|ccc}
\text{Iteration} & i=1 & i=2 & i=3\\
\left|\psi_{\text{init}}\right\rangle  & \left|00\right\rangle  & \left|00\right\rangle  & \frac{\left|00\right\rangle +\left|11\right\rangle }{\sqrt{2}}\\
\text{HV/Toss} & c=-1 & c=-1 & c=+1\\
\\
\text{Predictions} & m_{1}(\hat{A}_{ij})\doteq\left[\begin{array}{ccc}
-1 & -1 & -1\\
-1 & -1 & -1\\
-1 & -1 & +1
\end{array}\right] & m_{2}(\hat{A}_{ij})\doteq\left[\begin{array}{ccc}
-1 & -1 & -1\\
-1 & -1 & -1\\
-1 & -1 & +1
\end{array}\right] & m_{3}(\hat{A}_{ij})\doteq\left[\begin{array}{ccc}
+1 & +1 & +1\\
+1 & +1 & -1\\
+1 & +1 & +1
\end{array}\right]\\
\text{(Assignments)}\\
 & m_{1}(\hat{R}_{i}),m_{1}(\hat{C}_{j})=+1\,(j\neq3) & m_{2}(\hat{R}_{i}),m_{2}(\hat{C}_{j})=+1\,(j\neq3) & m_{3}(\hat{R}_{i}),m_{3}(\hat{C}_{j})=+1\,(j\neq3)\\
 & m_{1}(\hat{C}_{3})=-1 & m_{2}(\hat{C}_{3})=-1 & m_{3}(\hat{C}_{3})=-1\\
\text{Operator}\\
\text{Measured} & \hat{A}_{13}=\hat{\sigma}_{z}\otimes\hat{\sigma}_{z};m_{1}(\hat{A}_{13})=+1 & \quad\quad\hat{A}_{23}=\hat{\sigma}_{y}\otimes\hat{\sigma}_{y};m_{2}(\hat{A}_{23})=-1\quad\quad & \hat{A}_{33}=\hat{\sigma}_{x}\otimes\hat{\sigma}_{x};m_{3}(\hat{A}_{33})=+1\\
\\
\left|\psi_{\text{final}}\right\rangle  & \left|00\right\rangle  & \frac{\left|00\right\rangle +\left|11\right\rangle }{\sqrt{2}} & \frac{\left|00\right\rangle +\left|11\right\rangle }{\sqrt{2}}\\
\\
\\
\end{array}\label{eq:toyModel}
\end{equation}
\end{table}
\end{landscape}For the final iteration, $i=3$, say we yield $c=1$.
So far, we have $m_{1}(\hat{A}_{33})=1$ and $m_{2}(\hat{A}_{23})=-1$.
We must obtain $m_{3}(\hat{A}_{13})=1$, independent of the value
of $c$, to be consistent. Let's check that. According to step 3,
since $\hat{\sigma}_{x}\otimes\hat{\sigma}_{x}\left(\left|00\right\rangle +\left|11\right\rangle \right)/\sqrt{2}=1\left(\left|00\right\rangle +\left|11\right\rangle \right)/\sqrt{2}$,
$m_{3}(\hat{A}_{13})=1$ indeed. As a remark, it maybe emphasised
that the $m_{2}(\hat{A}_{33})=m_{3}(\hat{A}_{33})$ and $m_{2}(\hat{A}_{23})=m_{3}(\hat{A}_{23})$,
which essentially expresses compatibility of these observables, viz.
measurement of $\hat{A}_{13}$ doesn't affect the result one would
obtain by measuring operators compatible to it (granted they have
been measured once before).

While this model serves as a simple counter-example to the usual `QM
is contextual' conclusion one draws the PM situation, the model fails
to yield the appropriate statistics. For instance, if we consider
simply the state $\sqrt{2}\left|\psi\right\rangle =\cos\theta\left|++\right\rangle +\sin\theta\left|--\right\rangle $,
then it follows that a measurement of $\hat{A}_{11}=\hat{\mathbb{I}}\otimes\hat{\sigma}_{x}$,
would yield $\pm1$ with equal probability according to the toy model,
whereas it (the probabilities) should depend on $\theta$ according
to QM.\footnote{This was pointed out by Prof. Arvind.}

\section{Generic Models}

In this section, we present arguably, the simplest HV theories, one
of which is for spin like systems (discrete Hilbert space) while the
other is for phase space (continuous Hilbert Space) for spin-less
particles. These models are essentially non-contextual completions
of QM, which facilitate easy computation of value assignment to operators.

\subsection{Discretely C-ingle Theory \label{sub:Discretely-C-ingle-Theory}}

The state of the system is $\left|\chi\right\rangle $, defined on
a discrete Hilbert space (spin like) and we wish to assign a value
to an arbitrary operator $\hat{A}=\sum_{a}a\left|a\right\rangle \left\langle a\right|$,
which has eigenvalues $\{a_{\text{min}}=a_{1}\le a_{2}\dots\le a_{n}=a_{\text{max}}\}$.
This theory has the following postulates: \\
1. Initial HV: Pick a $c\in[0,1]$, from a uniform random distribution.\\
2. Assignment/Prediction: The value assigned to $\hat{A}$ is given
by finding the smallest $a$ s.t.
\[
c\le\sum_{a'=a_{\text{min}}}^{a}\left|\left\langle a'|\chi\right\rangle \right|^{2}.
\]
 A measurement of $\hat{A}$, would yield $a$.\\
3. Update: After measuring an operator, the state must be updated
(collapsed) in accordance with the rules of QM.

To see how this works, we restrict ourselves to a single spin case.
Say $\left|\chi\right\rangle =\cos\theta\left|0\right\rangle +\sin\theta\left|1\right\rangle $,
and $\hat{A}=\hat{\sigma}_{z}=\left|0\right\rangle \left\langle 0\right|-\left|1\right\rangle \left\langle 1\right|$.
Now, according to the postulates of this theory, $\hat{A}$ will be
assigned $+1$, if $c\le\cos^{2}\theta$, else $\hat{A}$ will be
assigned $-1$. It follows then, from $c$ being uniformly random
in $[0,1]$, that the statistics agree with predictions of QM. The
reader can convince him(her)self that the said scheme works in general,
specifically for the PM situation.

We can clearly see that the assignment is non-contextual, since given
an operator and a state (+ the hidden variable), the value is uniquely
assigned. The assignment is non-multiplicative, because this scheme
when applied to the PM situation, becomes effectively the same as
the toy model (barring the statistics). We have already seen explicitly
that the toy-model non-multiplicative. The theory is ofcourse invasive,
for the state is updated after each measurement.

\subsection{Continuously C-ingle Theory | Preliminary}

The state of the system is $\left|\psi\right\rangle $, defined for
a single spin-less particle, and we wish to assign a value to an arbitrary
operator
\[
\hat{A}=\int_{a_{\text{min}}}^{a_{\text{max}}}daa\left|a\right\rangle \left\langle a\right|.
\]
This theory has the following postulates: \\
1. Initial HV: Pick a $c\in[0,1]$, from a uniform random distribution.\\
2. Assignment/Prediction: The value assigned to $\hat{A}$ is given
by an $a$ that satisfies
\[
c=\int_{a_{\text{min}}}^{a}\left|\psi(a')\right|^{2}da',
\]
where $\psi(a')=\left\langle a'|\psi\right\rangle $. A measurement
of $\hat{A}$, would yield $a$.\\
3. Update: After measuring an operator, the state must be updated
(collapsed) in accordance with the rules of QM.

The continuous variable version has some exclusive interesting features.
First, note that for $\hat{A}=\hat{q}$ (the position), one can predict
the trajectory of a particle. This is quite intuitive to observe graphically.
Say $c$ had a value as shown in the graph (see \prettyref{fig:Illustration-of-cingle})
\begin{figure}
\centering{}\includegraphics[width=0.95\columnwidth]{Chapter4/Figs/Vector/cingle}\caption{Illustration of the underlying principle of the continuously c-ingle
model\label{fig:Illustration-of-cingle}}
\end{figure}
and the initial state is given by a Gaussian. Now the cumulative of
this can be quickly constructed, and where the $c$ line intersects
the graph, that will yield the position of the particle. At some later
time if the Gaussian shifts (assume it had some momentum), then for
the same value of $c$, the particle's location would've moved with
the Gaussian as expected. Infact this is a general feature and can
be done for all observables, including $p$. %
\begin{comment}
Reasoning similar to the previous subsection entails that this theory
is also non-multiplicative and non-contextual.
\end{comment}
The theory is explicitly non-contextual, since values are uniquely
assigned to operators, given $\psi$ and $c$. However, one needs
more analysis to extend this to multiple particles. Once that is accomplished,
one must show that measuring the observables using the Hamiltonian
approach, would yield results consistent with those obtained from
the postulates of the theory. To show then that the theory is non-contextual,
one would be required to show that all concievable measurement schemes
would produce the same result, else, like BM, this theory wouldn't
be able to predict the values of operators with only the state and
hidden variable information. %
\begin{comment}
{[}TODO: write this section properly; the main idea is that to be
consistent with the method of measuring arbitrary observables, by
position measurements, one can't assign values in the aforesaid way.
This way, you end up with two assignments for the same observable
and have to accept eventually, that the result you get would depend
on the experiment that is performed and then there's no meaning one
can assign to an observable having a value; thus if I assign a value
to position, then effectively all my remaining freedom is lost. Thus,
one would have to restrict the theory in some, which is ok for spins,
black box the other degree of freedom, but here one can't do this.
{]}
\end{comment}
Once extended appropriately, this theory would effectively overcome
all the challenges we had for testing the continuous variable extensions
of the GHZ and contextuality tests. Predictions of observables will
be straight forward. To obtain values from BM, one had to do a detailed
analysis, for RS, evaluating values of observables other than $q$
and $p$ (even $q+p$ is hard) was hard.

We had this hunch however, that the trajectory so obtained, must be
identical to Bohmian trajectories. This infact turns out to be so.
Let us take a moment to prove this, for using the same method, one
can evalute a trajectory for the momentum also. This would be distinct
from the momentum in BM. In this case, a measurement of $\hat{p}$
yields $p$ (the assigned value), unlike in BM.
\begin{prop*}
$\dot{q}=\nabla S/m$ in a continuously c-ingle theory, for a single
particle, with one degree of freedom.\end{prop*}
\begin{proof}
Let $f$ quantify some property of a particle. To point to a particle,
we can either use the variables $c,t$ or $x,t$, since we know how
$x$ and $c$ are related. Thus, one can write $f(c,t)$ or $f(x,t)$.\footnote{If you think that the two functions should be different, then the
notation is confusing you. $f(x,t)=f(t,x)$ should help resolve. The
position of the arguments is not important, this is not like a computer
function. $f(x,t)$ is the statement that $f$ is a function of $x,t$.
That's it.} Now we have
\[
\left[\frac{\partial f}{\partial t}\right]_{c}=\left[\frac{\partial f}{\partial t}\right]_{t}.\left[\frac{\partial q}{\partial t}\right]_{c}+\left[\frac{\partial f}{\partial t}\right]_{q},
\]
where we used $f(x,t)$ on the RHS. Note also that $\dot{q}$ refers
to the velocity of a given particle, thus it must equal $\left[\partial q/\partial t\right]{}_{c}$.
Now for $f=\int_{-\infty}^{q}\left|\psi(q')\right|^{2}dq'=c$, the
LHS disappears. Consequently, we have
\[
\left[\frac{\partial q}{\partial t}\right]_{c}=-\left[\frac{\partial f}{\partial t}\right]_{q}/\left[\frac{\partial f}{\partial q}\right]_{t},
\]
which is in a form which can be evaluated directly from the relation
given. $\left[\partial f/\partial q\right]_{t}=\left|\psi(q,t)\right|^{2}=R^{2}$,
if $\psi=Re^{iS/\hbar}$. It is easy to show that the probability
current is given by $\nabla S/m$. Using $\left[\partial\left|\psi(q,t)\right|/\partial t\right]_{q}=-\nabla.(R^{2}\nabla S/m)$,
which is effectively the probability conservation statement, derived
from Schrodinger's equation, written in polar form, it follows that
$-\left[\partial f/\partial t\right]_{q}=R^{2}\nabla S$. Thus we
have $\dot{q}=\nabla S$ as claimed.
\end{proof}
An equation similarly for $\dot{p}$ can be obtained and an appropriate
dynamics can perhaps then be constructed. It maybe mentioned as a
remark that, quite independent of it's motivation, this scheme can
be used to compute Bohmian trajectories. It is much more efficient,
as is apparent from a comparison of the resources required between
computing an integral and the steps listed in \prettyref{sec:Bohm's-Theory,-Bohmian}.

\section{The Verdict: Contextuality vs. Non-Multiplicativity}

We have already constructed an explicit non-contextual model, which
is consistent with QM. This model we knew had to be non-multiplicative.
We will see how non-multiplicativity gives rise to what one might
confuse to mean contextuality.

The approach is to take some compatible observables, and to construct
a `super-operator', a measurement of which can yield the values of
all of these compatible observables in a single shot. We would see
then, that the in-principle measurement outcome of observing these
compatible operators, would not be consistent. Now this one might
treat as contextuality, but according to the definition in \prettyref{sec:Peres-Mermin-Revisited},
we note that this is non-multiplicativity, as per \prettyref{sec:Multiplicativity}.

Let us construct an explicit situation and make more precise statements.
Borrowing the notation from \prettyref{sec:Multiplicativity}, imagine
\[
\hat{B}_{1}=\hat{\sigma}_{z}\otimes\hat{\mathbb{I}}=\left|00\right\rangle \left\langle 00\right|+\left|01\right\rangle \left\langle 01\right|-\left[\left|10\right\rangle \left\langle 10\right|+\left|11\right\rangle \left\langle 11\right|\right],
\]
\[
\hat{B}_{2}=\hat{\mathbb{I}}\otimes\hat{\sigma}_{z}=\left|10\right\rangle \left\langle 10\right|+\left|11\right\rangle \left\langle 11\right|-\left[\left|00\right\rangle \left\langle 00\right|+\left|01\right\rangle \left\langle 01\right|\right],
\]
while we define
\[
\hat{C}=f(\{\hat{B}_{i}\})=0.\left|00\right\rangle \left\langle 00\right|+1.\left|01\right\rangle \left\langle 01\right|+2.\left|10\right\rangle \left\langle 10\right|+3.\left|11\right\rangle \left\langle 11\right|.
\]
$\hat{C}$ maybe viewed as a function of $\hat{B}_{1}$, $\hat{B}_{2}$
and other operators $\hat{B}_{i}$ which are constructed to obtain
a maximally commuting set. A measurement of $\hat{C}$, will collapse
the state into one of the states which are simultaneous eignkets of
$B_{1}$ and $B_{2}$. Consequently, from the observed value of $\hat{C}$,
one can deduce the values of $\hat{B}_{1}$ and $\hat{B}_{2}$. Now
consider $\sqrt{2}\left|\chi\right\rangle =\left|10\right\rangle +\left|01\right\rangle $,
for which $m_{1}(\hat{B}_{1})=1$, and $m_{1}(\hat{B}_{2})=1$, using
the discretely c-ingle theory (\prettyref{sub:Discretely-C-ingle-Theory}),
with $c<0.5$. However, $m_{1}(\hat{C})=1$, from which one can deduce
that $B_{1}$ was $+1$, while $B_{2}$ was $-1$. This property itself,
one may be tempted call contextuality, viz. the value of $B_{1}$
depends on whether it is measured alone or with the remaining $\{B_{i}\}$.
However, it must be noted that $B_{1}$ has a well defined value,
and so does $\hat{C}$. Thus by our accepted definition, there's no
contextuality. It is just that $m_{1}(\hat{C})\neq f(m_{1}(\hat{B}_{1}),m_{1}(\hat{B}_{2}),\dots)$,
viz. the theory is non-multiplicative. Note that after measuring $\hat{C}$
however, $m_{2}(\hat{B}_{1})=+1$ and $m_{2}(\hat{B}_{2})=-1$ (for
any value of $c$) consistent with those deduced by measuring $\hat{C}$.

\section{Denouement}

We have already learnt that the proof of `contextuality', infact requires
three assumptions, (1) multiplicativity, (2) non-contextuality and
(3) non-invasiveness. Here we were able to construct an explicit non-contextual
theory (for spins) which is non-multiplactive, but invasive and completely
consistent with QM. Succinctly stated, it satisfies (2) but neither
(3) nor (1), serving as a counter-example to the claim that non-contextual
hidden variable theories can't be consistent with QM. We also showed
how the theory might be misconstrued to be contextual and provided
a clarification. This is of interest because the contextuality arising
in BM from the measurement schemes, has been a source of confusion
about the said notion, arising in the PM situation.

\begin{comment}

\section{First Section of the Third Chapter}

And now I begin my third chapter here . . . And now to cite some more
people \cite{prime-number-theorem,texbook,SFPT,latex}

\subsection{First Subsection in the First Section . . .}

and some more

\subsection{Second Subsection in the First Section . . . }

and some more . . .

\subsubsection{First subsub section in the second subsection . . . }

and some more in the first subsub section otherwise it all looks the
same doesn\textquoteright t it? well we can add some text to it .
. .

\subsection{Third Subsection in the First Section . . . }

and some more text . . .

\subsubsection{First subsub section in the third subsection . . . }

and some more in the first subsub section otherwise it all looks the
same doesn\textquoteright t it? well we can add some text to it and
some more and some more and some more and some more

\section{Second section with a Table}

Oh I have a table, which I can to refer (See \ref{tab:My-first-table}).

\begin{table}[H]
\hfill{}%
\begin{tabular}{|c|c|c|}
\hline
\textbf{1} & \textbf{2} & \textbf{3}\tabularnewline
\hline
\hline
4 & 5 & 6\tabularnewline
\hline
7 & 8 & 9\tabularnewline
\hline
\end{tabular}\hfill{}

\caption{\label{tab:My-first-table}My first table (I know, it is a really
intuitive name) }
\end{table}
\end{comment}
\selectlanguage{english}%


\documentclass{article}

%----------------------------------------------------------------------------------------

\usepackage{listings} % Required for inserting code snippets
\usepackage{geometry}
\geometry{margin=0.7in}
\usepackage[usenames,dvipsnames]{color} % Required for specifying custom colors and referring to colors by name
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{tikz}
\usepackage{enumerate}
\usepackage[T1]{fontenc}

\delimitershortfall-1sp
\newcommand\abs[1]{\left|#1\right|}

\definecolor{DarkGreen}{rgb}{0.0,0.4,0.0} % Comment color
\definecolor{highlight}{RGB}{255,251,204} % Code highlight color

%----------------------------------------------------------------------------------------

\begin{document}

%----------------------------------------------------------------------------------------

\section*{Problem 1}
\begin{flushleft}
Assume that $f(x) \leq g(x) \leq h(x)$ for all x in some open interval containing a, except perhaps at a itself. If $\displaystyle \lim_{x \to a} f(x) = \lim_{x \to a} h(x) = L$, then $\lim_{x \to a} g(x)$ exists and equals L, also. Justify each step in the following proof of the squeeze theorem.
\begin{enumerate}[a)]
\item Given $\epsilon > 0$ show that there exists a $\delta_1 > 0$ such that if $0 < \abs{x-a} < \delta_1$, then $\abs{f(x) - L} < \epsilon$.
\item Show that if $0 < \abs{x-a} < \delta_1$, then $L - \epsilon < f(x)$.
\item Show that there exists a $\delta_2>0$ such that if $0 < \abs{x-a} < \delta_2$, then $h(x) < L + \epsilon$
\item Let $\delta=min{\delta_1, \delta_2}$. Show: If $0 <  \abs{x-a} < \delta$, then $L-\epsilon< g(x) < L+\epsilon$
\item Complete the proof by showing that if $0<\abs{x-a}<\delta$, then $\abs{g(x)-L} < \epsilon$.
\end{enumerate}

\newpage

\section*{Problem 2}
\begin{enumerate}[a)]
\item \begin{enumerate}[a)] \item Suppose that $f(xx) \leq 0$ for all $x$ (except perhaps at $x=a$). Show: if $\displaystyle \lim_{x \to a} f(x)=L$, then $L\leq0$. Hint: Assume instead that $L > 0$. Let $\epsilon=\frac{L}{2}$ and derive a contradiction \item State and prove the analogue for $f(x) \geq 0$. \end{enumerate}
\item Assume that $g(x) \leq h(x)$ for all $x$ (except perhaps at $a$). If $\displaystyle \lim_{x \to a} g(x)=M$
and $\displaystyle \lim_{x \to a} h(x) = N$, prove that $M \leq N$. (Hint: Let $f(x) = g(x) - h(x)$ and then use problem 2.2.9). \end{enumerate}

\newpage

\section*{Problem 3}
Prove (from the $\epsilon-\delta$ definition) that $\displaystyle \lim_{x \to 3} \sqrt{3-x}$ does not exist, but $\displaystyle \lim_{x \to 3^-} \sqrt{3-x}$ = 0.

\newpage

\section*{Problem 4}
Consider the set of numbers ${a_n: n \in \mathbb{N}}$ where each $a_n$ is determined via this recursive definition:\\

\begin{center}
$a_1=2$ and $a_n=2-\frac{1}{a_{n-1}}$ for $n \geq 2$.\\
\end{center}
For example,
\begin{center}
$a_1=2$, $a_2=2-\frac{1}{a_1}=2-\frac{1}{2}=\frac{3}{2}$, $a_3=2-\frac{1}{\frac{3}{2}}=\frac{4}{3}$, etc. \\
\end{center}
Use induction to prove that for all $n \in \mathbb{N}$ we have $a_n = \frac{n+1}{n}$.
\end{flushleft}

%----------------------------------------------------------------------------------------

\end{document}


\documentclass[12pt,fleqn]{article}

\title{A method for smoothing Impulse Response Functions for
  discrete-time linear models}

\author{Gray Calhoun\thanks{%
    Economics Department, Iowa State University;
    \protect\url{gcalhoun@iastate.edu},
    web: \protect\url{http://gray.clhn.org}.
    Helle Bunzel, Seth Pruitt, and participants in the ISU Statistical
    Graphics Working Group gave very helpful feedback on early
    versions of this paper.}}

\input{tex/setup}
\input{tex/shorthand}

\begin{document}
\maketitle
\begin{abstract}\noindent%
  We show how to construct continuous and differentiable Impulse
  Response Functions for discrete-time Vector Autoregressions and
  Vector Error-Correction Models. Current methods produce piecewise
  linear functions that introduce visual distortions, especially when
  many response functions are plotted in the same graph to represent
  uncertainty or partial identification. We also show how to plot the
  cumulative response to a shock and incorporate moving average
  dynamics.
\end{abstract}

\tableofcontents
\newpage

\section{Introduction}
\label{S1}

Impulse Response Functions (IRFs) are widely used in macroeconomics
to represent the dynamic effect over time of an unanticipated
shock. If $y_t$ is a stationary sequence of random vectors, an IRF
is a graph of the change in the conditional expectation of $y_{t+s}$
in response to an unanticipated shock to $y_t$, plotted as a function
of $s$. The conditional expectation may be implied by a theoretical
model or a statistical model, such as a VAR, in which case additional
assumptions may be necessary to identify the shocks of economic
interest. In either of these cases, IRFs can provide a parsimonious and
interpretable summary of the key features of the model.

Producing an IRF requires calculating the conditional expectation
$\E_t y_{t+s}$ under different counterfactual scenarios, with and
without the shock of interest. Since most macroeconomic models are
defined and estimated in discrete time (both $t$ and $s$ are integers)
the conditional expectations used to produce IRFs are calculated in
discrete time as well. (See Chapter 1 of \citealp{Ham:94}, for
example.) But in most applications, it is desirable to graph the IRFs
as curves and connect the points between the integer values of $s$;
this is especially important when more than one IRF is plotted on
the same graph --- whether to represent dynamics under different
assumptions (see \citealp{BeM:98}, and \citealp{StW:01}, for
representative examples), to represent statistical uncertainty
(\citealp{Kil:98}, and \citealp{SiZ:99}) or to represent regions that
are partially identified (\citealp{Uhl:05}, and
\citealp{InK:13}). Currently, this is done by linear interpolation
between the integer values of $\E_t y_{t+s}$ but this practice has
some drawbacks. Linear interpolation introduces misleading visual
distortions and can remove potentially interesting dynamics from the
model.

In this paper, we show how to construct continuous and smooth IRFs
for vector ARMA and other linear time series models. These IRFs are
defined for all values of $t$ and $s$, even noninteger values, and
ensure that the interpolation obeys the same dynamics as the rest of
the model. The resulting graph removes the distortions introduced by
current methods.  Section~\ref{S2} presents these results,
Section~\ref{S3} provides some numeric and empirical examples, and
Section~\ref{S4} concludes.

\section{Method for smoothing IRFs}
\label{S2}

This section presents the main results of the paper: smooth IRFs for
finite-order linear models. Section~\ref{S2.1} presents results for
the AR(1) to motivate our approach. Section~\ref{S2.2} extends this
result to finite-order VARs and Section~\ref{S2.3} extends the
result further to VARMA and VECM models and to cumulative responses.
The rest of this subsection introduces some notation and defines the
IRFs formally.

Let $\{y_t\}$ be a stationary sequence of random vectors in $\RR^k$,
let $\Fs_t$ represent the information set available in period $t$,
\begin{equation*}
  \Fs_t = \sigma(y_t, y_{t-1}, y_{t-2},\dots),
\end{equation*}
and let the function $m_s$ represent the conditional expectation of
$y_{t+s}$ given $y_t$, so
\begin{equation*}
  m_s(x) = \E(y_{t+s} \mid y_t = x)
\end{equation*}
for each $s \geq 0$. Define $m_s = 0$ for $s < 0$ and let
$\E y_t = \mu$. For a shock of interest $u$, with $u \in \RR^k$, the
IRF corresponding to $u$ is the function
\begin{equation}
  \label{eq:16}
  \Psi_{u}(s) = \E(m_s(y_t + u) - m_s(y_t)).
\end{equation}
The shock, $u$, in these equations should be viewed as the ``reduced
form'' version of the shock --- an unanticipated change in the current
value of $y_t$. In a Structural VAR application, $u$ will typically
be related to a more fundamental shock of economic interest $\delta$
through the linear transformation $u = \Gamma \delta$, for some known
or estimated matrix $\Gamma$.

If $y_t$ is nonstationary $\E_t$ may depend on the value of $t$. In that
case, define
\begin{equation*}
  m_{ts}(x_0,\dots,x_p)
  = \E(y_{t+s} \mid y_t = x_0,\dots, y_{t-p} = x_p)
\end{equation*}
and let
\begin{equation*}
  \Psi_{tu}(s) =
  m_{ts}(\E y_t + u, \E y_{t-1}, \dots, \E y_{t-p}) - m_{ts}(\E y_t, \E y_{t-1}, \dots,\E y_{t-p}).
\end{equation*}
Note that $\Psi_{tu}$ can be independent of $t$ even if $m_{ts}$ is
not.

\subsection{AR(1) motivation}
\label{S2.1}

\begin{figure}[t]
  \centering
  \includegraphics[width=6.5in]{graphs/motivation.pdf}
  \caption{IRF for simple AR(1) example. Here $y_t = 0.2 y_{t-1} +
    \vep_t$ --- the vertical axis shows $\Psi_1(s)$. The left panel
    plots only integer values of $s$, the middle panel uses linear
    interpolation between the integers, and the right panel plots the
    function $0.2^s$ directly.}
  \label{fig:1}
\end{figure}

For motivation, start with the simplest univariate case and assume
that $y_t$ is the AR(1)
\begin{equation}
  \label{eq:1}
  y_t = a y_{t-1} + \vep_t
\end{equation}
where $\vep_t$ is a martingale difference sequence and
$\lvert a \rvert < 1$.  For discrete-time models, it is easy to define
the IRF recursively and $\Psi_h(s) = a^s u$. When $a > 0$, this
function is well-defined for all positive real $s$, and can be used
directly to interpolate between the integer points. But typically
representing these dynamics has been achieved by linear interpolation,
so
\begin{align}
  \label{eq:2}
  \Psi_u(s)
  &= (s - \lfloor s\rfloor) \Psi_u(\lfloor s\rfloor+1)
    + (1 - s + \lfloor s\rfloor) \Psi_u(\lfloor s\rfloor) \\
  \label{eq:3}
  &= (s - \lfloor s\rfloor) a^{\lfloor s \rfloor + 1} u
    + (1 - s + \lfloor s\rfloor) a^{\lfloor s \rfloor} u
\end{align}
for noninteger $s$, where $\lfloor s\rfloor$ is the largest integer
less than or equal to $s$.

Figure~\ref{fig:1} plots $\Psi_u(s)$ for $u = 1$ and $a = 0.2$ using
three approaches: first only for the integer values of $s$, second
using linear interpolation between the integer values defined
by~\eqref{eq:3}, and finally using $a^s u$ directly for all
$s \geq 0$.\footnote{%
  All of the graphs in this paper were produced using R. \citep{R}} %
While one could debate which of the functions represents the `true'
impact of a shock at a fractional point in time and whether the
interpolated points should be interpreted, the dynamics and rate of
decay of the AR model are much more clearly represented by the right
panel that graphs $0.2^s$ directly.

This should not be surprising. We can see that $\Psi_u(s) = a^s u$
satisfies the recurrence relation implied by the lag structure of the
original AR process for any positive real value of $s$:
\begin{equation}
  \label{eq:4}
  \Psi_u(s) = a^s u = a \times a^{s-1} u = a \, \Psi_u(s-1).
\end{equation}
Letting $\Phi(L)$ define the lag polynomial of the AR(1) defined by
Equation~\eqref{eq:1}, (so the autoregressive model is defined as
$\Phi(L)y_t = \vep_t$ for all $t$) we can express~\eqref{eq:4} in a
form that will be easier to extend to more complicated DGPs,
\begin{equation}
  \label{eq:5}
  \Phi(L) \Psi_u(s) = (1 - a L) \Psi_{u}(s) = 0
\end{equation}
for all $s \in \RR^+$ with the implicit definition
$L \Psi_u(s) = \Psi_u(s-1)$. When $\Psi_u(s)$ is constructed by linear
interpolation, i.e.~\eqref{eq:3}, the recursive
relationships~\eqref{eq:4} and~\eqref{eq:5} only hold for integer
values of $s$.

\begin{figure}[t]
  \centering
  \includegraphics[width=6.5in]{graphs/motivation2.pdf}
  \caption{IRF for simple AR(1) example. Here $y_t = -0.2 y_{t-1} +
    \vep_t$ --- the vertical axis shows $\Psi_s(1)$. The left panel
    plots only integer values of $s$, the middle panel uses linear
    interpolation between the integers, and the right panel plots the
    function $|a|^s \cos(\pi s)$ directly.}
  \label{fig:2}
\end{figure}

The case $a \in (-1, 0)$ is slightly more complicated.  The previous
solution, $a^s u$, is well-defined and real-valued only for integer
values of $s$, so it can no longer be used directly for interpolation
between the noninteger points. Instead, consider the definition
\begin{equation}
  \label{eq:6}
  \Psi_u(s) = |a|^s \cos(\pi s) \cdot u.
\end{equation}
For integer values of $s$, $\cos(\pi s) = 1$ so~\eqref{eq:6} is
exactly equal to $a^s u$. But~\eqref{eq:6} remains real-valued and
well defined for noninteger $s$ as well. Moreover,~\eqref{eq:6}
satisfies the recurrence relation for the AR(1) model for all values
of $a \in (-1,1)$
\begin{equation*}
  \Psi_u(s) = |a|^s \cos(\pi s) \cdot u = - |a| \times |a|^{s-1} \cos(\pi (s-1)) \cdot u = a \, \Psi_u(s-1)
\end{equation*}
implying that
\begin{equation*}
  \Phi(L) \Psi_u(s) = (1 - a L) \, \Psi_u(s) = 0
\end{equation*}
as before. Figure~\ref{fig:2} plots the IRFs for $a = -0.2$ and
$u = 1$ with no interpolation, linear interpolation, and the
definition~\eqref{eq:6}. Just as before, the smooth version more
accurately reflects the system's dynamics after a shock.

This choice of $\Psi_u(s)$ was not made at random. Any complex
number $a + bi$ can be written in polar form,
\[
a + bi = R [\cos(\theta) - i \sin(\theta)]
\]
where $R = |a^2 + b^2|^{1/2}$ and $\theta$ satisfies
$\cos(\theta) = a/R$ and $\sin(\theta) = b/R$. Real powers of $a + bi$
can be expressed as\footnote{%
  See \citet{Ham:94} for background on complex numbers.} %
\[
(a + bi)^s = R^s [\cos(\theta s) - i \sin(\theta s)].
\]
For the AR example above, $a < 0$ and $b = 0$ so $\theta = \pi$.
Then $a^s = |a|^s \cos(\pi s)$ as we originally claimed.

This example also suggests a more general method for interpolating
between integer values of $s$ for any VAR; solve recurrence relation
using standard tools (as described in \citealp{Ham:94}, for example)
and use the solution as the IRF for real-valued $s$. We pursue that
approach in the next section.

\subsection{General Approach for VAR(\textit{p})}
\label{S2.2}

The AR(1) model in the previous section can be extended to general
VARs using the ``canonical'' representation.\footnote{%
  See \citet{Ham:94} or \citet{HaS:13} for a textbook treatment of
  much of this material. Our presentation borrows heavily from
  \citet{Ham:94}.} %
Define $y_t$ to be the VAR($p$),
\[
y_t = \sum_{j=1}^p A_j y_{t-j} + \vep_t.
\]
(We assume that $y_t$ has mean zero to simplify the presentation
without loss of generality.) To represent this relationship as a
VAR(1), form the vector $(y_t',\dots,y_{t-p+1}')'$ and observe
that
\begin{equation}
\label{eq:7}
\underbrace{\begin{pmatrix*}[l]
  y_t \\ y_{t-1} \\ y_{t-2} \\ \vdots \\ y_{t-p+1}
\end{pmatrix*}}_{Z_t}
=
\underbrace{\begin{pmatrix*}[l]
  A_1 & A_2 & \cdots & A_{p-1} & A_p \\
  I_k & 0   & \cdots & 0 & 0 \\
  0  & I_k  & \cdots & 0 & 0 \\
  \vdots & \vdots & \ddots & \vdots & \vdots \\
  0 & 0 & \cdots & I_k & 0
\end{pmatrix*}}_{F}
\underbrace{\begin{pmatrix*}[l]
  y_{t-1} \\ y_{t-2} \\ \vdots \\ y_{t-p+1} \\ y_{t-p}
\end{pmatrix*}}_{Z_{t-1}}
+
\underbrace{\begin{pmatrix*}[l]
  \vep_{t} \\ 0 \\ 0 \\ \vdots \\ 0
\end{pmatrix*}}_{U_t}.
\end{equation}
So this relationship is equivalent to $Z_t = F Z_{t-1} + U_t$. The
conditional expectation has a convenient form that is trivial to
calculate for integer values of $s$,
\begin{equation*}
  m_s(x) = e_{1p} F^s x
\end{equation*}
where $e_{jp}$ is the $k p \times k$ selection matrix
\begin{equation*}
  e_{jp} = \begin{pmatrix*}[l]
    0_{k \times (j-1)p} & I_k & 0_{k \times (k - j)p}
  \end{pmatrix*}.
\end{equation*}
Consequently, for integer values of $s$, the IRF has the form
\begin{equation*}
  \Psi_u(s) = e_{1p} F^s e_{1p}' u.
\end{equation*}
This section will argue that a variation of
this definition remains meaningful for noninteger values of $s$.

To show this, notice that, since $F$ is square, it has the Jordan
decomposition
\begin{equation}
  \label{eq:8}
  F = M J M^{-1}
\end{equation}

where $J$ is block diagonal of the form
\[
J = \begin{pmatrix*}[l]
  J_1 & \cdots & 0 \\
  \vdots & \ddots & \vdots \\
  0 & \cdots & J_q
\end{pmatrix*}
\]
and $q$ is the number of unique eigenvalues of $J_{\ell}$. Each
$J_\ell$ is an $n_\ell \times n_\ell$ matrix of the form
\[
J_\ell =
\begin{pmatrix*}[l]
  \lambda_\ell & 1 & 0 & \cdots & 0 & 0 \\
  0 & \lambda_\ell & 1 & \cdots & 0 & 0 \\
  0 & 0 & \lambda_\ell & \cdots & 0 & 0 \\
  \vdots & \vdots & \vdots & \ddots & \vdots & \vdots \\
  0 & 0 & 0 & \cdots & \lambda_\ell & 1 \\
  0 & 0 & 0 & \cdots & 0 & \lambda_\ell
\end{pmatrix*}
\]
where $\lambda_1,\dots,\lambda_q$ are the distinct eigenvalues of $F$
and $n_\ell$ is the number of times the $\ell$th eigenvalue is
repeated.  If all of the eigenvalues of $F$ are
distinct,~\eqref{eq:8} is just the eigenvalue decomposition of $F$.

This representation is convenient because, for positive integer values
of $s$, we have
\[
F^s = M J^s M^{-1}
\]
and
\begin{align*}
J^s =
\begin{pmatrix*}[l]
  J_1^s  & \cdots & 0      \\
  \vdots & \ddots & \vdots \\
  0      & \cdots & J_q^s
\end{pmatrix*}, &&
J_\ell^s &=
\begin{pmatrix*}[l]
  h_\ell(s,0) & h_\ell(s,1) & h_\ell(s,2) & \cdots & h_\ell(s,n_\ell)   \\
  0           & h_\ell(s,0) & h_\ell(s,1) & \cdots & h_\ell(s,n_\ell-1) \\
  0           & 0           & h_\ell(s,0) & \cdots & h_\ell(s,n_\ell-2) \\
  \vdots      & \vdots      & \vdots      & \ddots & \vdots             \\
  0           & 0           & 0           & \cdots & h_\ell(s,0)        \\
\end{pmatrix*},
\end{align*}
  where $h_\ell(s,j)$ is defined as
\begin{equation*}
h_\ell(s,j) =
\begin{cases}
  |\lambda_\ell|^s \big[\cos(\theta_\ell s) + i \sin(\theta_\ell s)\big] & j = 0 \\
  \frac{s!}{j!(s-j)!} |\lambda_\ell|^s \big[\cos(\theta_\ell s) + i \sin(\theta_\ell s)\big] & \text{if } s \geq j > 0 \\
  0 & \text{otherwise.}
\end{cases}
\end{equation*}
and $\theta_\ell$ satisfies
$\cos(\theta_\ell) = \Re(\lambda_\ell)/|\lambda_\ell|$ and
$\sin(\theta_\ell) = \Im(\lambda_\ell)/|\lambda_\ell|$ as before.

This representation allows us to write
\begin{multline}
  \label{eq:9}
  F^s =
  \sum_{j = 1}^q \sum_{l=0}^{\min(n_\ell, \lfloor s\rfloor)} \tfrac{s(s-1)\cdots(s-l+1)}{l (l - 1) \cdots 1} |\lambda_j|^{s-l} \\
  \times\Big\{\big[B_{jl} \cos(\theta_j s) + C_{jl} \sin(\theta_j s)\big] + i\big[D_{jl} \cos(\theta_j s) + E_{jl} \sin(\theta_j s)\big]\Big\}
\end{multline}
where the coefficient matrices $B_{jl}$, $C_{jl}$, $D_{jl}$, and
$E_{jl}$ are chosen to match $F^0, F^1, F^2,\dots$. For integer
values of $s$, the imaginary components of $M J^s M^{-1}$ exactly
cancel, so we can use the real part of~\eqref{eq:9} alone, giving
\begin{equation}
  \label{eq:10}
  F^s =
  \sum_{j = 1}^q \sum_{l=0}^{\min(n_\ell, \lfloor s \rfloor)} \tfrac{s(s-1)\cdots(s-l+1)}{l (l - 1) \cdots 1} |\lambda_j|^{s-l} \big[B_{jl} \cos(\theta_j s) + C_{jl} \sin(\theta_j s)\big]
\end{equation}
to produce $F^1, F^2, F^3,\dots$. Our proposal, in a nutshell, is to
use the definition given by~\eqref{eq:10} for all of the reals rather
than just the integers, exactly as we did in the motivating AR(1)
examples.

Equation~\eqref{eq:9} has another practical implication. Although
$F^s$ has real and imaginary components for real $s$, we are only
interested in its real component that generates the IRFs for integer
values of $s$. Rather than explicitly calculating the matrices
$B_{jl}$ and $C_{jl}$ to use~\eqref{eq:10}, we can calculate the
complex valued $F^s$ and simply drop its imaginary component. This
calculation is directly available in many programming languages and
can be implemented as $M J^s M^{-1}$ using the Jordan decomposition if
it is not.

We conclude this section with a formal statement of the result.

\begin{proposition}[Impulse Response Functions for VARs]
  \label{P1}
  Let $F$ be the coefficient matrix of the canonical VAR(1)
  representation of an arbitrary real-valued VAR($p$) $y_t$. Define
  $\Psi_{u}(s) = e_{1p} \Re(F^s) e_{1p}' u$ for all
  $s \in [0,+\infty)$, where $u \in \RR^k$ is a shock of theoretical
  interest. Then $\Psi_{u}(s)$ satisfies~\eqref{eq:16} for all
  integer values of $s$ and satisfies the recurrence relation implied
  by the original VAR for all positive, real values of $s$,
  \begin{equation}
    \label{eq:11}
    \Psi_u(s) = \sum_{j=1}^q A_j \Psi_u(s-j).
  \end{equation}
\end{proposition}
\begin{proof}
  The fact that $\Psi_u(s)$ meets the definition of the IRF on
  integers is discussed in the text and follows from simple
  algebra. Observe that the series $a_s = \Re(F^s) e_{1p}'u$ satisfies
  $a_s = F a_{s-1}$ since $F$ is real and
  \[
    F a_{s-1} = F \Re(F^{s-1}) e_{1p}'u
    = \Re(F^s) e_{1p}'u
    = a_s.
  \]
  The conclusion of this proposition is a direct consequence.
\end{proof}

\subsection[Finite-order linear models]{Extensions to other linear models}
\label{S2.3}

This section considers three extensions to the previous section.
First, we show how to calculate cumulative response functions for
VAR($p$)s. Second, we show how to calculate IRFs for Vector Error
Correction Models (VECM). And, third, we show how to calculate IRFs
for VARMA($p$, $q$) models. These results all rely on augmenting the
series to create a new canonical VAR(1) and can easily be combined
and extended further to other recursive linear models.

First, suppose that $y_t$ has a VAR($p$) representation as before.
but we want to derive the effect of the shock $u$ on $S_t$, the
cumulative sum of the $y_t$s,
\[
  S_t = \sum_{s=0}^t y_s.
\]
Since $S_t - y_t = S_{t-1}$, we can extend the canonical
representation for $y_t$ by embedding this relationship as the first
element of the vector $Z_t$ in~\eqref{eq:7}. Equation~\eqref{eq:7}
becomes
\begin{equation*}
  \begin{pmatrix*}[l]
    S_t - y_t \\ y_t \\ y_{t-1} \\ \vdots \\ y_{t-p+1}
  \end{pmatrix*}
  =
  \begin{pmatrix*}[l]
    I      & 0      & \cdots & 0       & 0      \\
    0      & A_1    & \cdots & A_{p-1} & A_p    \\
    0      & I      & \cdots & 0       & 0      \\
    \vdots & \vdots & \ddots & \vdots  & \vdots \\
    0      & 0      & \cdots & I       & 0
  \end{pmatrix*}
  \begin{pmatrix*}[l]
    S_{t-1}  \\ y_{t-1} \\ \vdots \\ y_{t-p+1} \\ y_{t-p}
  \end{pmatrix*}
  +
  \begin{pmatrix*}[l]
    0 \\ \vep_t \\ 0 \\ \vdots \\ 0
  \end{pmatrix*}
\end{equation*}
and can be rewritten as
\begin{equation*}
  \begin{pmatrix*}[l]
    S_t \\ y_t \\ y_{t-1} \\ \vdots \\ y_{t-p+1}
  \end{pmatrix*}
  =
  \underbrace{\begin{pmatrix*}[l]
    I      & A_1    & \cdots & A_{p-1} & A_p    \\
    0      & A_1    & \cdots & A_{p-1} & A_p    \\
    0      & I      & \cdots & 0       & 0      \\
    \vdots & \vdots & \ddots & \vdots  & \vdots \\
    0      & 0      & \cdots & I       & 0
  \end{pmatrix*}}_F
  \begin{pmatrix*}[l]
    S_{t-1}  \\ y_{t-1} \\ y_{t-2} \\ \vdots \\ y_{t-p}
  \end{pmatrix*}
  +
  \begin{pmatrix*}[l]
    \vep_t \\ \vep_t \\ 0 \\ \vdots \\ 0
  \end{pmatrix*}.
\end{equation*}
This representation is now a VAR(1) that can be handled exactly as in
Proposition~\ref{P1} and $\Psi_u(s)$ can be defined as
$e_{1,p+1} \Re(F^s) (e_{1,p+1} + e_{2,p+1})' u$

The VECM model has similar behavior. Suppose now that we have the
relationship
\begin{equation*}
  \Delta y_t = B y_{t-1} + A_1 \Delta y_{t-1} + \cdots + A_p \Delta
  y_{t-p} + \vep_t.
\end{equation*}
This can also be put in a canonical form using almost identical
arguments as before,
\begin{equation*}
  \begin{pmatrix*}[l]
    y_t \\ \Delta y_t \\ \Delta y_{t-1} \\ \vdots \\ \Delta y_{t-p+1}
  \end{pmatrix*}
  =
  \underbrace{\begin{pmatrix*}[l]
    (I + B) & A_1    & \cdots & A_{p-1} & A_p    \\
    B       & A_1    & \cdots & A_{p-1} & A_p    \\
    0       & I      & \cdots & 0       & 0      \\
    \vdots  & \vdots & \ddots & \vdots  & \vdots \\
    0       & 0      & \cdots & I       & 0
  \end{pmatrix*}}_F
  \begin{pmatrix*}[l]
    y_{t-1} \\ \Delta y_{t-1} \\ \vdots \\ \Delta y_{t-p+1} \\ \Delta y_{t-p}
  \end{pmatrix*}
  +
  \begin{pmatrix*}[l]
    \vep_t \\ \vep_t \\ 0 \\ \vdots \\ 0
  \end{pmatrix*}
\end{equation*}
and $\Psi_u(s)$ can be defined as
$e_{1,p+1} \Re(F^s) (e_{1,p+1} + e_{2,p+1})' u$ again.

VARMA($p$, $q$)s can also be handled similarly. If $y_t$ satisfies
\begin{equation*}
  y_t = \sum_{j=1}^p A_j y_{t-j} + \sum_{i=1}^q B_i \vep_{t-i} + \vep_t
\end{equation*}
this can be written as a canonical VAR(1) using the equation
\begin{equation*}
\begin{pmatrix*}[l]
  y_t \\ y_{t-1} \\ \vdots \\ y_{t-p+1} \\
  x_t \\ x_{t-1} \\ \vdots \\ x_{t-q+1}
\end{pmatrix*}
=
\underbrace{\begin{pmatrix*}[l]
  A_1    & \cdots & A_{p-1} & A_p    & B_1    & \cdots & B_{q-1} & B_q    \\
  I_k    & \cdots & 0       & 0      & 0      & \cdots & 0       & 0      \\
  \vdots & \ddots & \vdots  & \vdots & \vdots &        & \vdots  & \vdots \\
  0      & \cdots & I_k     & 0      & 0      & \cdots & 0       & 0      \\
  0      & \cdots & 0       & 0      & 0      & \cdots & 0       & 0      \\
  0      & \cdots & 0       & 0      & I_k    & \cdots & 0       & 0      \\
  \vdots &        & \vdots  & \vdots & \vdots & \ddots & \vdots  & 0      \\
  0      & \cdots & 0       & 0      & 0      & \cdots & I_k     & 0      \\
\end{pmatrix*}}_{F}
\begin{pmatrix*}[l]
  y_{t-1} \\ \vdots \\ y_{t-p+1} \\ y_{t-p} \\
  x_{t-1} \\ \vdots \\ x_{t-q+1} \\ x_{t-q}
\end{pmatrix*}
+
\begin{pmatrix*}[l]
  \vep_{t} \\ 0 \\ \vdots \\ 0 \\ \vep_t \\ 0 \\ \vdots \\ 0
\end{pmatrix*}.
\end{equation*}
Now $\Psi_u(s) = e_{1,p+q} \Re(F^s) (e_{1,p+q} + e_{p+1,p+q})' u$.

Other extensions are obviously also possible and these results can
be applied to state space models as well. See~\citet{HaS:13} for
additional examples and discussion.

\section{Examples}
\label{S3}
In this section, we present two examples. The first is a numerical
example that demonstrates how these methods scale when they are used
to graph many curves simultaneously. The second is an application to
Uhlig's~(2005) partial identification strategy for monetary policy
shocks.
\nocite{Uhl:05}

\subsection{Numerical example}
\label{S3.1}

For a simple numerical example, consider the two-variable VAR(2)
\begin{equation}
  \label{eq:12}
  \begin{pmatrix*}[l]
    y_{1t} \\ y_{2t}
  \end{pmatrix*}
  =
  \begin{pmatrix*}[r]
    - 0.50 & 0.01 \\ 0.30 & 0.10
  \end{pmatrix*}
  \begin{pmatrix*}[l]
    y_{1,t-1} \\ y_{2,t-1}
  \end{pmatrix*}
  +
  \begin{pmatrix*}[r]
    -0.20 & 0.10 \\ -0.10 & 0.00
  \end{pmatrix*}
  \begin{pmatrix*}[l]
    y_{1,t-2} \\ y_{2,t-2}
  \end{pmatrix*}
  +
  \begin{pmatrix*}[l]
    \vep_{1t} \\ \vep_{2t}
  \end{pmatrix*}
\end{equation}
and assume that $(\vep_{1t},\vep_{2t}) \sim N(0,I)$ represents the
shocks of interest.

The IRF for an $\vep_1$-shock is defined as described above,
\begin{equation*}
  \Psi_1(s) = e_{1,2} \Re(F^s) e_{1,2}'
  \begin{pmatrix*}[l]
    1 \\ 0
  \end{pmatrix*}
\end{equation*}
with
\begin{equation*}
F = \begin{pmatrix*}[r]
    - 0.50 & 0.01 & -0.20 & 0.10 \\
      0.30 & 0.10 & -0.10 & 0.00 \\
      1.00 & 0.00 & 0.00 & 0.00 \\
      0.00 & 1.00 & 0.00 & 0.00
  \end{pmatrix*}
\end{equation*}
Similarly, the IRF for an $\vep_2$-shock is given by
\begin{equation*}
  \Psi_2(s) = e_{1,2} \Re(F^s) e_{1,2}'
  \begin{pmatrix*}[l]
    0 \\ 1
  \end{pmatrix*}.
\end{equation*}

We plot the IRFs in the first two columns Figure~\ref{fig:3}. The
first column plots the standard IRFs, using linear interpolation
between integer-valued time periods, and the second plots our proposed
smooth plots. Although the general impressions from both graphs are
similar, there are important differences. First, peaks and troughs are
often underestimated by the standard IRFs and their timing is
frequently misidentified. This is especially apparent in the first
peak in the second row, which falls exactly on period $t+1$ using
linear interpolation but approximately $t+0.5$ using ours.  Second,
even the sign of the IRF can be misidentified, as we see with the
immediate response of $y_{1}$ to a shock in $\vep_{2}$.

However, it would be reasonable for researchers to want to emphasize
the value of the IRF at the integer time-periods, since those do not
depend on any method of interpolation. The third column of
Figure~\ref{fig:3} demonstrates one method of doing this: plot the
individual points of the IRF defined on the integers over a light
version of the curve.

\begin{figure}[t]
  \centering
  \includegraphics[width=6.5in]{graphs/numeric.pdf}
  \caption{Impulse Response Functions from Section~\ref{S3.1} example;
    $y_t$ is generated by Equation~\eqref{eq:12}. The left column
    plots standard IRFs, the middle column plots our new method, and
    the right column plots our new approach, emphasizing the values at
    integer time periods.}
  \label{fig:3}
\end{figure}

The weaknesses of using linear interpolation become more apparent when
we graph multiple perturbations of the IRFs in the same panel, which
is a common way of representing uncertainty or set-identified
responses. To demonstrate this phenomenon, we generated 150
perturbations of the IRFs by adding independent $N(0, 0.15)$ noise
terms to each element of the VAR coefficients in~\eqref{eq:12}, then
calculating and plotting the IRFs as before. (The graphs use alpha
blending, a form of partial transparency, to make the individual
curves more visible.)

\begin{figure}[t]
  \centering
  \includegraphics[width=6.5in]{graphs/numeric2.pdf}
  \caption{Impulse Response Functions from Section~\ref{S3.1} example;
    $y_t$ is generated by Equation~\eqref{eq:12} with independent
    $N(0,0.15)$ random variable added to each coefficient. These
    graphs plot the IRFs from 150 independent draws of the
    coefficient matrices. The left column plots standard IRFs, the
    middle column plots our new method, and the right column plots our
    new approach with an emphasis on the integer time periods..}
  \label{fig:4}
\end{figure}

These graphs are shown in Figure~\ref{fig:4}. The same issues apparent
in Figure~\ref{fig:3} are present here as well. But there are other
problems as well. In the first curve, for example, the discrete IRF
shows substantial negative correlation between the period 2 and period
3 estimated response and the period 3 and 4 response to an
$\vep_1$-shock. But the smoothed graph makes it clear that this is
driven by the timing and size of the first peak. When that peak is
near period 2, the curve has time to fall noticeably before period 2,
but when the peak is closer to period 3, the curve is still rising for
that interval. The actual dynamics implied by the different curves are
very similar. Similar but less dramatic distortions appear in the
other panels as well. In the third row, for example, the discrete
IRF shows that about half of the parameter values have an initial
increase in response to a $y_{20}$ shock and half have an initial
decrease, but the smooth curves show that virtually all of them have
an immediate decrease, but that many start to increase very soon. The
exact location of the peak that falls between periods 1 and 2
determines most of the initial dynamics, but this is impossible to see
in the discrete curve.

These graphs reveal that our method of constructing IRFs is more
resilient to perturbation than linear interpolation: curves generated
by close coefficient values have a more similar appearance. This
property is particularly useful when graphing many similar curves to
represent statistical uncertainty. Moreover, as the right columns in
Figures~\ref{fig:3} and~\ref{fig:4} illustrate, it is still possible
to emphasize the integer-valued time periods while using our method of
interpolation.

\subsection{Empirical analysis of the effect of monetary policy}
\label{S3.2}

This section demonstrates our proposed method for constructing IRFs
in a widely-studied empirical setting. We conduct a brief analysis of
the effect of monetary policy on the real economy using Uhlig's~(2005)
sign-restriction partial identification strategy.

First, a quick review. In Structural VAR models, the shocks of
theoretical interest --- a monetary policy shock in this section ---
can simultaneously affect all of the variables in the model. Without
additional assumptions beyond those necessary to estimate the
coefficients of the VAR, we can not estimate all of the unknown
parameters necessary to identify the shock.\footnote{%
  \citet{Kil:13} gives a recent review of the SVAR literature.} %
I.e., if $y_t$ is a VAR($p$),
\begin{equation}
  \label{eq:13}
  y_t = \alpha + \sum_{i=1}^p A_i y_{t-p} + \vep_t,
\end{equation}
with $\vep_t \sim (0, \Sigma)$ a martingale difference sequence, then
$\vep_t$ is related to the sequence of theoretical shocks $\delta_t$
through
\begin{equation*}
  \vep_t = \Gamma \delta_t
\end{equation*}
and $\delta_t \sim (0, I)$ is also martingale difference sequence. The
variance covariance matrix, $\Sigma$, can be consistently estimated,
but $\Gamma$ will in general have more unique elements than $\Sigma$.
The IRF corresponding to a vector of theoretical shocks $\delta$ is
given by
\begin{equation}
  \label{eq:14}
  \Psi_\delta(s) = e_{1p} \Re(F^s) e_{1p}' \Gamma \delta
\end{equation}
so additional restrictions on $\Gamma$ are necessary to estimate
$\Psi_\delta$.

\citet{Uhl:05} proposes using a minimal set of assumptions to identify
\emph{potential} monetary policy shocks. Letting the first element of
$\delta$ represent a positive monetary policy shock, Uhlig's approach
is to generate candidate shocks randomly and calculate $\Psi_\delta$
corresponding to that specific shock. IRFs that match some a priori
plausible criteria are kept as potential responses to monetary policy
shocks and the resulting set of functions partially identifies the
effect of a shock. Formally, for given estimates $\hat F$ and
$\hat \Sigma$, this procedure amounts to generating many candidate
shocks $u^* = u / \| u \|_2$ where $u \sim N(0,I)$ and calculating the
candidate IRFs as
\begin{equation}
  \label{eq:15}
  \hat \Psi_{u^*}(s) =  e_{1p} \Re(\hat F^s) e_{1p}' \hat\Sigma^{1/2} u^*,
\end{equation}
then discarding $\hat\Psi_{u^*}(s)$ that do not meet a set of criteria of
that define and constrain the monetary policy shock.

In this paper, we use our proposed method for graphing IRFs to plot
the candidate responses to a monetary policy shock using Uhlig's sign
restrictions. We fit a 6-lag VAR using the same six variables as
Uhlig: real GDP, the GDP deflator, the federal funds rate, borrowed
reserves, nonborrowed reserves, and a commodity price
index.\footnote{%
  As in Uhlig, we fit the model in levels and without an explicit time
  trend.} %
We also impose the same restrictions as Uhlig:
\begin{itemize}[noitemsep]
\item the Federal Funds rate does not increase for five months after a
  monetary policy shock,
\item the price level does not decrease for five months after a
  monetary policy shock, and
\item nonborrowed reserves does not decrease after a monetary policy
  shock.
\end{itemize}
Since our interest here is in the method of displaying IRFs, we use
\citeauthor{BeM:98}'s original dataset, which covers January 1965
through December 1997.  Most of the variables are available
monthly. Since GDP and the GDP deflator are only quarterly, their
values are interpolated as described by \citet{BGW:97} and
\citet{BeM:98} and we fit a VAR on the monthly data.

Figure~\ref{fig:5} presents our graphs.\footnote{%
  As in our earlier example, these graphs use partial transparency,
  alpha blending, to show as many of the individual curves as
  possible.} %
For this exercise, we only present the estimate of the identified set
that is associated with the OLS point estimates and ignore the
additional uncertainty in estimating the coefficients. The main
results are essentially already known: the immediate effect of a
sign-identified monetary policy shock on GDP is ambiguous in the very
short run but lowers GDP relative to trend after about two years. And
the effects on the other variables are consistent with the
identification strategy and with the previous literature. But our
approach to smoothing the IRFS allows each of the individual candidate
shocks to be studied as well. Many of the curves have a minimum
between 1/2 to 2 years, but there is considerable uncertainty over the
exact date of the minimum.  The dynamics expressed by the outer
envelope of the curves, which is the quantity plotted in many VAR
applications, is not particularly representative of any of the
individual IRFs.

\begin{figure}[t]
  \centering
  \includegraphics[width=6.5in]{graphs/empirics}
  \caption{%
    Empirical response of each variable to a sign-identified negative
    monetary policy shock; see description in Section~\ref{S3.2}. The
    horizontal axis is the number of years since the initial
    shock. These figures graph 500 candidate shocks.}
  \label{fig:5}
\end{figure}

\section{Conclusion}
\label{S4}

Vector Autoregressive models do not just have implications for the
period-to-period dynamics of a stochastic process, they also have
implications about the very short-run dynamics within periods. In this
paper, we propose that researchers graph those intra-period dynamics
when plotting the IRFs for linear models and we give a simple method
to do so, based on the model's canonical VAR(1) representation. Even
when researchers do not want to assign any economic importance to
these ultra short-run dynamics, plotting them in the IRFs minimizes
visual distortions that can arise from discretizing the dynamics,
especially when several IRFs are plotted over each other to
represent uncertainty or partial identification.

\clearpage
\addcontentsline{toc}{section}{References}
\bibliography{latex_misc/references}

\clearpage
\appendix
\section{Appendix: Changes to this paper from previous versions}
\input{CHANGELOG}
\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% TeX-command-extra-options: "-shell-escape"
%%% End:


% Created 2022-01-16 Sun 11:18
% Intended LaTeX compiler: pdflatex
\documentclass[presentation]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{xypic}
\usepackage{tabularx}
\xyoption{pdf}
\usetheme{default}
\newcommand\cat[1]{\ensuremath{\mathcal{#1}}}
\newcommand\hm[2]{\hom(#1,#2)}
\newcommand\nat[2]{{#1}\overset{\cdot}{\rightarrow}{#2}}
\newcommand\tup[1]{\langle #1\rangle}
\newcommand\id[1]{\mathrm{id}_{#1}}
\author{jens.jensen@stfc.ac.uk \\0000-0003-4714-184X\\CC-BY 4.0}
\date{March 27, 2022}
\title{Functional Programming 3}
\hypersetup{
 pdfauthor={jens.jensen@stfc.ac.uk},
 pdftitle={Functional Programming 3},
 pdfkeywords={functional, monad, programming theory},
 pdfsubject={functional programming},
 pdfcreator={Emacs 27.1 (Org mode 9.3) then by hand},
 pdflang={English}}
\begin{document}

\maketitle
\begin{frame}{Outline of Talks}

  \begin{itemize}
    \item Previous talks (talks 1 and 2):
    \begin{itemize}
\item Introibo
\item Pure Functional Programming Principles
    \item Mapping

    \end{itemize}

  \item This talk (talk 3):
    \begin{itemize}
    \item Labels and naming
    \item Lists (or rather cons cells)
    \end{itemize}

  \item Advanced(ish) Topics
\item Impure Functional? Side Effects
\item Category Theory
\item Categories and Functions
\item Categories and Computation

  \end{itemize}
Still written in the author's spare time!

\medskip

Very much a personal perspective, and not following any particular textbook.  Using \emph{meditations} and \emph{exercises} -- solutions to all exercises given during the talks.

\end{frame}

\section{Introibo}
\label{sec:org0edb596}

\begin{frame}{Summary of Talk 2 -- Mapping}
  One of the core functional programming patterns is to recurse through a list with \texttt{cdr} (or \texttt{rest}), gathering results of a function being called on the elements.

  \medskip
  Lisp's powerful mapping functions implement this fundamental pattern very concisely:
  \begin{tabularx}{\linewidth}{|l|X|X|X|}
    \hline function sees: & list of single value results & nconc'ed list of results & nothing returned \\
    \hline\hline first elt & \texttt{mapcar} & \texttt{mapcan} & \texttt{mapc} \\
    \hline whole sublist & \texttt{maplist} & \texttt{mapcon} & \texttt{mapl} \\
    \hline
  \end{tabularx}

  \medskip
  \begin{itemize}
  \item Sequence functions (lists, vectors, strings)
    \begin{itemize}
    \item \texttt{map} acts on \emph{sequences}, returning a sequence (of possibly different type) or \texttt{nil} (side effect call)
    \item  \texttt{reduce} implements the pattern we used in \texttt{fact}
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}{Summary of Talk 2 -- Functional Lisp}
  We saw how core features of Lisp support functional programming:
  \begin{itemize}
  \item \texttt{nil} is very powerful:
    \begin{itemize}
    \item It evaluates to false
      \begin{itemize}
      \item In python and Perl, the empty list evaluates to false
      \item But in Lisp, false is an empty list (see \texttt{iota1} below)
      \end{itemize}
    \item \texttt{(car nil)} $\Rightarrow$ \texttt{nil} (and the same for \texttt{first}) makes code cleaner, shorter
      \begin{itemize}
      \item E.g.\ \texttt{(endp (cddr w))} says ``\texttt{w} has 0-2 elements''
      \end{itemize}
    \end{itemize}
  \item Using \texttt{and} and \texttt{or} as control flow
    \begin{itemize}
    \item \texttt{and} returns the first \texttt{nil} it finds, or last expr
    \item \texttt{or} returns its first non-\texttt{nil}
  \item E.g.\ \texttt{iota1} (helper for \texttt{iota}):
    \begin{itemize}
    \item \texttt{(defun iota1 (k1) (and (>= k1 1) (cons k1 (iota1 (1- k1)))))}
    \item \texttt{(when (>= k1 1) ...)} will also return \texttt{nil} for \texttt{k1}$<1$  but the \texttt{if} version is better style
    \end{itemize}
    \end{itemize}
  \item Using convenient \emph{symbols} where Inferior Languages™ use strings, enums, etc (more on symbols in this talk and later talks)
  \end{itemize}

\end{frame}

\begin{frame}{Summary of Talk 2 -- Answers}
  Answers to meditations and exercises:
  \begin{itemize}
  \item \texttt{and}, \texttt{or}, \texttt{if} etc \emph{are not functions} so cannot be used with \texttt{funcall} and mapping functions
  \item How to do \texttt{(search m "JanFebMar...")} better
    \begin{itemize}
    \item Answer later in this talk
    \end{itemize}
  \item Why do we need to copy a list before sorting it?
    \begin{itemize}
    \item Because \texttt{sort} destroys its argument
    \item It is an ``\texttt{n}'' function though not named so
    \item The \texttt{mapcar}s in the Schwarzian transform created temporary lists
    \end{itemize}
  \item Similarly if we know the list will be altered, we may have to create it as \texttt{(list 1 2 3 4)} rather than \texttt{'(1 2 3 4)}
    \begin{itemize}
    \item Lisp may be justified in assuming \texttt{'(1 2 3 4)} constant
    \item Though ELisp doesn't!
    \end{itemize}
  \item \texttt{labels} and naming
    \begin{itemize}
    \item Answer on next slide
    \end{itemize}

  \end{itemize}
\end{frame}

\begin{frame}[fragile]{To \texttt{labels} or not to \texttt{labels}?}
\begin{verbatim}
(defun iota (k)
  "Generate a list of integers from 1 to k"
  (labels ((iota1 (k1)
                  (and (>= k1 1)
                    (cons k1 (iota1 (1- k1))))))
    (nreverse (iota1 k))))
\end{verbatim}
or
\begin{verbatim}
(defun iota-1 (k1)
  "Helper function for iota - generate integers from k1 to 1"
  (and (>= k1 1)
      (cons k1 (iota1 (1- k1)))))
(defun iota (k)
  "Generate a list of integers from 1 to k"
  (nreverse (iota-1 k)))
\end{verbatim}

\end{frame}
\begin{frame}{To \texttt{labels} or not to \texttt{labels}?}
  \begin{itemize}
  \item Standalone function
    \begin{itemize}
    \item Easier to write/debug -- and read
    \item Can be unit tested (e.g.\ with \texttt{rt} in CL)
    \item Can be traced (in CL) or inspected, as it has a persistent name
    \item Sometimes the helper needs to be defined before its parent (next slide)
    \item Safety: the helper cannot access its parent's lexical scope
    \end{itemize}
  \item \texttt{labels}
    \begin{itemize}
    \item Safety: Can never be called from \texttt{labels}' lexical scope
    \item Convenience: Helper is in parent's lexical scope
    \item Fewer standalone functions cluttering our code
    \item Mutual recursion (adv.\ top.\ section 3)
    \end{itemize}
  \end{itemize}
  The point is that helper functions, if their parameter types are guaranteed, can optimise those checks away (in principle), regardless of whether they are standalone functions or declared with \texttt{labels} (or \texttt{flet}).

  \medskip
  One approach is to develop as a standalone function, but for smaller functions later embed as \texttt{labels}.
\end{frame}
\begin{frame}[fragile]{Interdependency}
  Lisp is traditionally generous with letting us define functions as we need them:
\begin{verbatim}
(defun foo (k)
  (if (oddp k) (bar k) (+ k 5)))
\end{verbatim}
This function will cheerfully run as long as we keep passing it even numbers, even though \texttt{bar} is not defined.  However, some CLs such as SBCL will warn if a function is not defined at read/compile time (SBCL compiles on read).

\medskip
In turn, we can \emph{stub} \texttt{bar} for now, if it is called rarely (EL version):
\begin{verbatim}
(defun bar (n)
  "FIXME: bar stub"
  (string-to-number
    (read-string (format "bar(%d) = ?" n))))
\end{verbatim}
  Once we replace \texttt{bar} with its production version, \texttt{foo} automagically calls that instead.
\end{frame}

\begin{frame}[fragile]{Lists -- Assumed Background}
  We assume you know your cons cells:
  \begin{itemize}
  \item The cons cell has a \texttt{car} and \texttt{cdr}
    \begin{itemize}
    \item \texttt{(cons 'x 'y)} $\Rightarrow$ \texttt{(x . y)}
    \item \texttt{(car '(x . y))} $\Rightarrow$ \texttt{x}
    \item \texttt{(cdr '(x . y))} $\Rightarrow$ \texttt{y}
    \end{itemize}
  \item A normal list has the \texttt{car}s pointing to the elements
    \begin{itemize}
    \item The \texttt{cdr}s point to the next cons cell
    \item In particular, the last cell's \texttt{cdr} is \texttt{nil}
    \end{itemize}
  \item The \emph{dot} tells the \emph{Reader} the \texttt{cdr} follows:
    \begin{itemize}
    \item \texttt{'(2 . nil)} $\Rightarrow$ \texttt{(2)}
    \item \texttt{'(2 . (a b))} $\Rightarrow$ \texttt{(2 a b)}
    \item The \emph{Printer} will always choose the most concise (and re-readable) representation
    \end{itemize}
  \item A list whose final \texttt{cdr} is not \texttt{nil} is an \emph{improper} list
    \begin{itemize}
    \item These are not allowed in iterations (mapping, recursion) as we almost always test whether the \texttt{cdr} is \texttt{nil} to stop
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{More Standard Types with Lists: lookup}
  Lisp has powerful types built in, that Inferior Languages™ need to pull in from (usually standard) libraries.

  \medskip
While Lisp has hash tables for key/value lookup, in simple cases lists will do perfectly well and have a long history.  The \emph{alist} (short for association list) is a standard type in Lisp, yet constructed from standard conses:
\begin{verbatim}
(let ((dict '((a . 1) (b . 2) (c . 3))))
  (list (assoc 'b dict) (rassoc 3 dict) (assoc 'f dict)))
((b . 2) (c . 3) nil)
\end{verbatim}
You have to do \texttt{(cdr (assoc ...))} to get the value, or a \texttt{(car (rassoc ...))} for the reverse lookup; both return the full cons cell.  Meditation: why is this the better choice (next slide)?

If you are not using \texttt{rassoc}, the key/value does not have to be a dotted pair.

\begin{verbatim}
(let ((dict '((a 1 2) (b 2 y) (c . 3))))
  (assoc 'b dict))
(b 2 y)
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{More Standard Types with Lists: lookup}
  ... answering the meditations from the previous slide:

1. We may want to know what the key was:
\begin{verbatim}
(let ((dict '((1 . a) (2 . b) (3 . c))))
  (assoc-if #'evenp dict))
(2 . b)
\end{verbatim}
\medskip
2. or we may wish to modify the value after the lookup:
\begin{verbatim}
(let* ((dict '((1 . a) (2 . b) (3 . c)))
       (val (assoc-if #'evenp dict)))
  (when val (rplacd val 'z))
  dict)
((1 . a) (2 . z) (3 . c))
\end{verbatim}
\medskip
3. or distinguish looking up value \texttt{nil} vs entry doesn't exist:
\begin{verbatim}
(let ((dict '((1 . a) (2 . nil) (3 . fred))))
  (list (assoc 2 dict) (assoc 'biff dict)))
((2) nil)           ; (2) is (2 . nil)
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{More Standard Types with Lists: lookup}
For modifying the alist, this would also have worked and is a more ``modern'' version of achieving the same goal:
\begin{verbatim}
(let* ((dict '((1 . a) (2 . b) (3 . c))))
  (setf (cdr (assoc-if #'evenp dict)) 'z)
  dict)
((1 . a) (2 . z) (3 . c))
\end{verbatim}
(note it will change only the \emph{first} entry with an even key) but has the obvious disadvantage that it will explode if the lookup fails (ie.\ it doesn't ``autovivify'' in Perl-speak).

\medskip
In contrast, hashes \emph{do} autovivify:
\begin{verbatim}
(let ((y (make-hash-table :test #'equal))) ; empty
  (setf (gethash "mimsy" y) 'blop)
  (gethash "mimsy" y))
blop
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{More Standard Types with Lists: lookup}
\texttt{(acons key val dict)} is short for \texttt{(cons (cons key val) dict)}.

\medskip
  To add-or-update an entry, we can simply \texttt{acons} the new entry in front (it will always match before the entry it supersedes).

  \medskip
  This add-to-front is a useful feature of alists:
\begin{verbatim}
(flet ((lookup-a (g)
                 "Find entry associated with 'a"
                 (cdr (assoc 'a g))))
  (let ((dict '((a . 1) (b . 2) (c . 3))))
    (list (lookup-a dict)
          (lookup-a (acons 'a 7 dict))
          (lookup-a dict))))
(1 7 1)
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{More Standard Types with Lists: lookup}
There is even a multiplayer version of \texttt{acons} called \texttt{pairlis}:

\begin{verbatim}
(flet ((lookup-a (g)
                 "Find entry associated with 'a"
                 (cdr (assoc 'a g))))
  (let ((dict '((a . 1) (b . 2) (c . 3))))
    (list (lookup-a dict)
          (lookup-a (pairlis '(c a) '(12 13) dict))
          (lookup-a dict))))
(1 13 1)
\end{verbatim}

\texttt{pairlis} takes a list of (distinct) keys (which may shadow those in the alist as above) and a list of the corresponding values and returns the alist with those added to the front.
\end{frame}

\begin{frame}[fragile]{More Standard Types with Lists: lookup}
This is potentially useful/interesting because Emacs uses alists -- for example, several alists help select the major mode:
\begin{verbatim}
(length auto-mode-alist)
266
(push (cons "\\.R$" 'text-mode) auto-mode-alist)
(find-file "foo.R") ; opens in text mode
(pop auto-mode-alist)
(find-file "bar.R") ; back to R mode
\end{verbatim}
This makes it possible to temporarily override a lookup, although this code is not functional like on the previous slide, as we need to override a system variable (in Advanced Topics section 6, we shall look at a more functional approach)

\medskip
Exercise: how would we ``pop'' if the cell is not the first?  If the key is \texttt{b} and the alist is \texttt{((a . 1) (b . 2) (b . 3))}, we want to remove the \texttt{b} $\Rightarrow$ \texttt{2} lookup so we get \texttt{b} $\Rightarrow$ \texttt{3}.
You may (must) modify the alist.  There is more than one solution.
\end{frame}

\begin{frame}[fragile]{More Standard Types with Lists: sets}
Lists can also be used as \emph{trees} and \emph{sets}.  We show the latter (though these slides contain lots of examples of trees):
\begin{verbatim}
(let ((y (list 1 2 3 4 3 1 5 2)))
  (delete-duplicates y))
(4 3 1 5 2)
\end{verbatim}
(as ever, delete- is the destructive version, remove- creates a fresh list with the result)
\begin{verbatim}
(let ((x '(1 2 3 4)) (y '(2 4 6 8)))
  (intersection x y))
(4 2)
(let ((x '(1 2 3 4)) (y '(2 4 6 8)))
  (union x y))
(8 6 1 2 3 4)
(let ((x '(1 2 3 4)) (y '(2 4 6 8)))
  (set-difference x y))
(1 3)
\end{verbatim}
These have $n$ versions if altering their arguments is allowed.
\end{frame}

\begin{frame}[fragile]{More Standard Types with Lists: sets and lookup}
Depending on what your set/dict elements are, you may need to specify the test function:
\begin{verbatim}
(let ((dict '(("abc" . 1) ("def" . 2))))
  (assoc "abc" dict))
("abc" . 1)
\end{verbatim}
In EL, the default test function for \texttt{assoc} is \texttt{equal} but a test function can be passed in as a third argument.  In CL, the situation is different as it uses \texttt{eql} by default:
\begin{verbatim}
(let ((dict '(("abc" . 1) ("def" . 2))))
  (assoc "abc" dict))
NIL
(eql "abc" "abc")
NIL
(let ((dict '(("abc" . 1) ("def" . 2))))
  (assoc "abc" dict :test #'equal))
("abc" . 1)
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{More Standard Types with Lists: sets and lookup}
Caution: because the default test function for the set functions is not \texttt{equal} even in EL:
\begin{verbatim}
(remove-duplicates '("abc" "def" "abc" "def" "ghi"))
("abc" "def" "abc" "def" "ghi")
(remove-duplicates '("abc" "def" "abc" "def" "ghi")
  :test #'equal)
("abc" "def" "ghi")
\end{verbatim}
\texttt{remove-duplicates} was defined in the \texttt{cl} package, so it implements the interface of its CL equivalent (including the \texttt{:test} keyword).  This is unlike \texttt{assoc} which is from the core EL, so is different from the CL version.

\medskip
Exercise: write \texttt{remove-duplicates} using (1) recursion and (2) mapping.  It should take an equality test function as an optional parameter (extra credit: use the \texttt{:test} keyword)
\end{frame}

\begin{frame}[fragile]{More Standard Types with Lists: sets and lookup}
Using alists, we can revisit last talk's exercise that used
\begin{verbatim}
(search m "JanFebMarAprMayJunJulAugSepOctNovDec")
\end{verbatim}
and replace the lookup with an alist:
\begin{verbatim}
(defun month-number (m)
  (cdr (assoc m '(("Jan" . 1) ("Feb" . 2) ...))))
\end{verbatim}
but in CL, the \texttt{assoc} needs a \texttt{:test \#'equal}.

\medskip
This implementation has additional advantages:
\begin{itemize}
\item It does not return valid results for nonsense strings like \texttt{"anF"}
\item It needs no extra calculations (\texttt{(1+ (floor ... 3))})
\end{itemize}
\end{frame}

\begin{frame}[fragile]{Digression: which is faster?}
Quick test macro (meditation: why does it have to be a macro?) (code in the reference uses \texttt{gensym}s) (and it's EL only)
\begin{verbatim}
(defmacro exec-time (expr)
  `(let ((t1 (current-time)))
     (cons ,expr
           (let ((t2 (current-time)))
             (time-subtract t2 t1)))))
exec-time
(exec-time (+ 2 3))
(5 0 0 1 272000)
\end{verbatim}
It returns its result followed by timing data (which we could tidy up)

\medskip
Full code available at \url{https://github.com/jjensenral/functional/blob/main/code/month.el}
\end{frame}

\begin{frame}{Digression: which is faster?}
  Results of 10,000 tests (run on the author's old slow laptop):

  \medskip
\begin{tabular}{|l|c|c|c|}
  \hline
  Months are & Lookup & Time (ms) & Rank \\
  \hline\hline
  Strings & \texttt{search} & 966 & 6 \\
  Strings & \texttt{assoc} & 68 & 5 \\
  Strings & \texttt{gethash} & 66 & 4 \\
  Strings & \texttt{position} & 28 & 2 \\
  Symbols & \texttt{assoc} & 65 & 3 \\
  Symbols & \texttt{position} & 18 & 1 \\
  \hline
\end{tabular}

\medskip
We can see the search method (from Talk 2) has the worst performance, and symbols generally do better than strings, as we would expect -- symbols are atoms, strings are sequences.
\end{frame}

\begin{frame}[fragile]{More Standard Types with Lists: queues}
  Lists can \texttt{push} and \texttt{pop} at the front but not naturally at the back.  How would we implement a queue (FIFO)?  Could we implement a dequeue?

  \medskip
  Lists can be used to implement an efficient FIFO:
  \resizebox{\linewidth}{!}{
    \includegraphics{queue.pdf}
  }

  \medskip
  \begin{itemize}
  \item Meditation: why is the \texttt{car} of the top cons pointing to the tail and not the \texttt{cdr}?
  \item Exercise: construct such a FIFO from any list
  \item Exercise: How do we implement enqueue/dequeue?
  \item Exercise: Can we implement a dequeue (push/pop at both ends)?
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Non-standard Types with Lists}
1. Improper lists: \texttt{(a b . c)}

2. Shared list structure:
\begin{verbatim}
(let* ((base '((a . 2) (b . 3)))
       (l1 (acons 'c 3 base))
       (l2 (acons 'd 4 base)))
  (list l1 l2))
(((c . 3) (a . 2) (b . 3)) ((d . 4) (a . 2) (b . 3)))
\end{verbatim}

\medskip
3. Infinite lists:
\begin{verbatim}
(let ((w (list 1 2 3 4)))
  (rplacd (last w) w)
  w)
(1 2 3 4 1 2 3 4 1 2 . #5)
\end{verbatim}

Emacs saves the day by abbreviating the output (cf.\ Adv Top.~6, 10).  You can even ask the \emph{Reader} to create the list:
\begin{verbatim}
'#1=(a b c d . #1#)
(a b c d a b c d a b . #5)
\end{verbatim}
\end{frame}

\begin{frame}[fragile]{Naming and Abstractions}
To write clean production code, it may be useful to introduce abstractions.  As a simple example, let us return to the \emph{pair} using a \texttt{cons}, like we had in our \emph{alists} earlier:
\begin{verbatim}
(defun make-pair (x y) (cons x y))
(defun pair-first (p) (car x))
(defun pair-second (p) (cdr x))
\end{verbatim}
The naming used here is consistent with Lisp \emph{structures} except for the absence of keywords in the lambda list (see next slide).
\end{frame}
\begin{frame}[fragile]{Naming and Abstractions}
Using structures instead, we get more bang for the buck:
\begin{verbatim}
(defstruct pair first second)
pair
(make-pair :first 'a :second 'b)
#s(pair a b)
(pair-first #s(pair a b))
a
(copy-pair #s(pair a b))
#s(pair a b)
(make-pair :second 'bar :first 'foo)
#s(pair foo bar)
\end{verbatim}
\emph{Structures} automatically define a \emph{constructor} and the \texttt{\#s} \emph{reader macro}, a \emph{copier}, and \emph{accessors} for every slot.

Notice the use of \emph{keywords} in the constructor.
\end{frame}
\begin{frame}[fragile]{Digression: Compatibility notes: Lambda Lists}
EL does not support full CL lambda lists, so this is CL:
\begin{verbatim}
(defun make-pair (&key first second) (cons first second))
(make-pair :second 2 :first 1)
(1 . 2)
\end{verbatim}
In EL we could fudge this with
\begin{verbatim}
(defun make-pair (key1 x key2 y)
  (unless (and (eq key1 :first) (eq key2 :second))
    (error "Invalid keywords"))
  (cons x y))
(make-pair :first 1 :second 2)
(1 . 2)
\end{verbatim}
though it would still rely on \texttt{:first} being first; fixing this is left as an exercise to the reader/listener (what if there were 3 keywords?).

\medskip
EL supports \texttt{\&optional} (but default is always nil, and there is no supplied-p) and \texttt{\&rest}.
\end{frame}

\begin{frame}[fragile]{Naming and Abstractions}
We can then write auxiliary functions which do not need to know how the pair was implemented (this one is using the struct but it could have worked with the cons (keywords version)):
\begin{verbatim}
(defun zip-pairs (list1 list2)
  (if (and list1 list2)
      (cons (make-pair :first (first list1)
                       :second (first list2))
            (zip-pairs (rest list1) (rest list2)))
    nil))
zip-pairs
(zip-pairs '(a b c) '(1 2 3))
(#s(pair a 1) #s(pair b 2) #s(pair c 3))
\end{verbatim}
This is like python's ``zip'' (not to be confused with CMUCL's python which predates python).  Of course in CL, one would not bother with such trivialities, but merely write \texttt{(mapcar \#'cons list1 list2)} or \texttt{(pairlis list1 list2 nil)}.
\end{frame}
\begin{frame}[fragile]{Naming and Abstractions -- Summary}

Arguably the pair is a quite natural fit to \texttt{cons}; but when implementing \emph{trees} using native Lisp types, it may help to have an abstraction, because there is more than one way to do it and the choice may depend on how the tree is used.

\begin{itemize}

\item The \emph{disadvantage} of the abstraction is the extra function call
  \begin{itemize}
  \item Although CL at least can \emph{inline} functions, or optimise the extra call away.
  \item And also writing more code; inlining doesn't help with that...
  \item Though CL's built in generator might, theoretically (\texttt{\#.} reader macro)
  \item Possibly less optimisation across functions than inside one?
  \end{itemize}
\item Norvig's PAIP is a good place to look for Useful Code using clean abstractions (in CL, not EL);
\item Abstractions can also introduce extra \emph{type checks} during development, or \emph{stubs} for complicated stuff;
\item While EL is more limited, CL has a very powerful type system which can implement checks (adv.\ top.\ sec.~7)
\end{itemize}

\end{frame}

\begin{frame}{Summary - the Lisp superpowertools}
  Summarising the Lisp functional programming Super Power Power Tools we have met (plus one from an earlier talk):
  \begin{itemize}
  \item \emph{Recursion}, the elegant engine of functional programming;
  \item \texttt{list} and list tools, designed for the first/rest paradigm;
  \item \texttt{cond} and friends for dividing and conquering;
  \item Mapping functions (and \texttt{reduce}) implement functional patterns concisely (including recursion, without recursing)
  \item Sequence functions including \texttt{reduce}
  \item Functions: \texttt{funcall}, \texttt{lambda}, \texttt{apply}; multivalued in CL
  \item \texttt{lambda} lists in Lisp are much more powerful than other functional languages -- more powerful than other languages;
  \item \texttt{flet} and \texttt{labels} for defining local functions;
  \item \emph{macros}, because Lisp macros are superpowertools for everything
  \item \emph{stubs}, and \texttt{trace} (the latter CL only)
  \end{itemize}
\end{frame}

\section{Advanced(ish) Functional Programming}
\begin{frame}{Topics for Future Talks}
\label{sec:org95ddb74}

\begin{enumerate}
\item Lambda (anonymous (unnamed) functions) and \emph{currying}
\item List comprehension
\item Functions -- mutually recursive, higher order
\item Symbols
\item Tail recursion
\item Scope and extent (Lisp)
\item Types and type inference
\item Branch-on-pattern-matching and guards
\item Memoisation
\item Lazy evaluation types
\item Pipes style composition
\begin{itemize}
\item  \(h(g(f(x)))\equiv\texttt{(h (g (f x)))}\equiv{}x\vert{f}\vert{g}\vert{h}\)
\end{itemize}
\item Monads: theoretical framework for types and computation
\item Applied monads: Maybe, Arrays
\item Bonus section for survivors of MonadLand: Lisp Hacking
\end{enumerate}

\end{frame}

\end{document}