\documentclass[a4paper,10pt]{article}

\usepackage{color}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{alltt}

\addtolength{\voffset}{-1em}
\addtolength{\textheight}{-2em}
\addtolength{\textwidth}{0.5em}
\setlength{\arraycolsep}{0.1em}

\begin{document}

\title{\bf\Large Guide to HOL4 interaction and basic proofs\vspace{-0.3em}}

\author{\normalsize Magnus O. Myreen}
\date{~\vspace{-2em}}

\maketitle

\newcommand{\etal}{{et al$.$}}
\newcommand{\eg}{{e$.$g$.$}}
\newcommand{\ie}{{i$.$e$.$}}

\newenvironment{code}{\begin{quote}\begin{alltt}\small}{\end{alltt}\end{quote}}
\newenvironment{enum}{\begin{enumerate}
\setlength{\itemsep}{-\parsep}
}{\end{enumerate}}

\newcommand{\conj}{\(\begin{array}{c}\texttt{\bf\scriptsize/\textbackslash}\\[0.18em]\end{array}\)}
\newcommand{\mysec}[1]{\section{\large #1}}
\newcommand{\mysubsec}[1]{\subsection{\normalsize#1}}

\mysec{Introduction}

This document gives readers, with no experience in using HOL4, the most
minimum knowledge needed to start using HOL4. The aim is to give a
concise description of the basics in a format usable as a beginners'
reference manual.
\begin{enum}
\item[~] Section \ref{interaction}:~~Interaction with HOL4 (via emacs or xemacs)
\item[~] Section \ref{search}:~~Searching for theorems and theories
\item[~] Section \ref{tactics}:~~Common proof tactics
\item[~] Section \ref{reading}:~~Further reading and general advice
\end{enum}
The text assumes that the reader has HOL4 installed from: {\tt\small http://hol.sf.net/}.

\mysec{Interaction with HOL4 (via emacs or xemacs)\label{interaction}}

Michael Norrish has written an emacs script which makes interaction
with HOL easy. To install the script add this line to your {\tt
.emacs} file:
\begin{code}
(load "<path>/HOL/tools/hol-mode")
\end{code}
Replace {\tt <path>} with the full path to your HOL4 installation. To make
emacs highlight the active region, also add the following line to your
{\tt .emacs} file:
\begin{code}
(transient-mark-mode 1)
\end{code}
Restart emacs to make these changes take effect.

\mysubsec{Starting a HOL4 session}

\begin{enum}
\item Start emacs.
\item Press {\tt C-x C-f} to open a file for your proof script.
\item Press {\tt C-x 3} to split the emacs window into two columns.
\item Press {\tt M-h h} to start HOL4.
\item Press {\tt C-x o} to move cursor to the other window (where we type).
\end{enum}
The HOL window should look something like this:
\begin{code}
-----------------------------------------------------------------
       HOL-4 [Kananaskis 5 (built Thu Feb 21 08:37:11 2008)]

       For introductory HOL help, type: help "hol";
-----------------------------------------------------------------

[loading theories and proof tools ............. ]
[closing file "/local/scratch/user/HOL/tools/end-init-boss.sml"]
-
\end{code}

\mysubsec{Copying input into HOL4 (Opening a theory)\label{copy}}

\begin{enum}
\item Select text by pressing {\tt C-space} at one end of the text.
\item Copy the region into HOL4 by pressing {\tt M-h M-r} at the other
  end. (You can also use drag the mouse to select text to be sent to
  HOL.)
\end{enum}
For example, pressing {\tt C-space} then {\tt M-h M-r} around the following line
\begin{code}
open arithmeticTheory listTheory;
\end{code}
makes HOL4 open the library theories for arithmetic (over natural numbers)
and lists. HOL4 prints a long list of definitions and already proved results.

\mysubsec{Starting a goal-oriented proof}

Most HOL4 proofs are constructed using an interactive \emph{goal
  stack} and then put together using the ML function {\tt prove}
(Section~\ref{prove1}, \ref{prove2}). To start the goal stack:
\begin{enum}
\item Write a goal, \eg{} {\tt `!n{.}~n < n + 1`}, (we write $\forall$ as {\tt !} in HOL4).
\item Move the cursor inside the back-quotes ({\tt `}).
\item Press {\tt M-h g} to push the goal onto the goal stack.
\end{enum}
The HOL4 window should look something like this:
\begin{code}
- > val it =
    Proof manager status: 1 proof.
    1. Incomplete:
         Initial goal:
         !n. n < n + 1

     : proofs
- > val it = () : unit
\end{code}

\mysubsec{Applying a tactic\label{apply}}

Make progress in a proof using \emph{proof tactics}.

\begin{enum}
\item Write the name of a tactic, \eg{} {\tt DECIDE\_TAC},~see Section~\ref{tactics} for more tactics
\item Press {\tt C-space} at one end of the text.
\item Move the cursor to the other end of the text.
\item Press {\tt M-h e} to apply the tactic.
\end{enum}
A tactic makes HOL4 update the current goal. The HOL4 window will either
display the new goal(s) or print:
\begin{code}
    Initial goal proved.
    |- !n. n < n + 1 : goalstack
\end{code}
You can undo the effect of the applied tactic by pressing {\tt M-h b}. Press {\tt M-h p} to view the current goal.

\mysubsec{Ending a goal-oriented proof}

One can pop goals off the goal stack by pressing {\tt M-h d}, which gives:
\begin{code}
- - OK..
> val it = There are currently no proofs. : proofs
\end{code}

\mysubsec{Saving the resulting theorem\label{prove1}}

One can use {\tt prove} to store the result of a proof (called a
\emph{theorem}), \eg{} the following stores the theorem $\forall n.\;n < n +
1$ in an ML variable {\tt LESS\_ADD\_1}:
\begin{code}
val LESS_ADD_1 = prove({`}` !n. n < n + 1 {`}`,DECIDE_TAC);
\end{code}
When the above line is copied into HOL4 (using {\tt C-space} then
{\tt M-h M-r}, as described in Section~\ref{copy}), HOL4 responds with:
\begin{code}
- > val LESS_ADD_1 = |- !n. n < n + 1 : thm
\end{code}

\mysubsec{Saving proofs based on multiple tactics\label{prove2}}

Suppose we have proved the goal {\tt `!n{.}~n <= n * n`} with the following tactics:
\begin{code}
Induct_on `n`                   (* comment: induction on n  *)

  DECIDE_TAC                    (* comment: solve base case *)

  ASM_SIMP_TAC bool_ss [MULT]   (* comment: simplify goal   *)
  DECIDE_TAC                    (* comment: solve step case *)
\end{code}
Tactics can be pieced together for {\tt prove} using {\tt THEN} and {\tt THENL}:
\begin{code}
val LESS_EQ_MULT = prove({`}` !n. n <= n * n {`}`,
  Induct_on `n` THENL [
    DECIDE_TAC,
    ASM_SIMP_TAC bool_ss [MULT] THEN
    DECIDE_TAC]);
\end{code}
Copy the above into HOL4 using {\tt C-space} then
{\tt M-h M-r}, as in Section~\ref{copy}.

\newcommand{\itemz}[2]{\texttt{#1}\; &-\;\; \textrm{#2}}
\newcommand{\itemy}[2]{#1&\quad\quad&#2\\}

\begin{figure}[t]
\hrule

\begin{displaymath}
\begin{array}{lllll}
\itemy{\itemz{\rule{0em}{1.5em}M-h h}{start HOL}}{\itemz{M-h g}{push goal onto goal stack}}
\itemy{\itemz{M-h M-r}{copy region into HOL}}{\itemz{M-h e}{apply tactic to goal}}
\itemy{\itemz{M-h C-t}{display types on/off}}{\itemz{M-h b}{move back in proof}}
\itemy{\itemz{M-h C-c}{interrupt HOL}}{\itemz{M-h p}{print current goal}}
\itemy{&}{\itemz{M-h d}{drop current goal}}
\end{array}
\end{displaymath}
\caption{Most important key bindings in the emacs HOL4 mode.}

\rule{0em}{1.5em}\hrule
\end{figure}

\mysubsec{Displaying types in HOL4}

HOL4 does not by default display types. Press {\tt M-h C-t} to switch
printing of type information on or off.

\mysubsec{Interrupting HOL4}

Press {\tt M-h C-c} to interrupt HOL4 --- useful when a tactic fails to terminate
(\eg{} {\tt\small METIS\_TAC} often fails to terminate when unsuccessfully applied).

\mysubsec{Making a definition\label{definition}}

Function can be defined using {\tt Define}, \eg{} square is defined as follows.
\begin{code}
val SQUARE_def = Define `SQUARE n = n * n`;
\end{code}

\noindent
Data-types are defined using {\tt Hol\_datatype}, \eg{} a binary tree
which holds values of type {\tt 'a} (a type variable) at the leaves:
\begin{code}
val _ = Hol_datatype `TREE = LEAF of 'a | BRANCH of TREE => TREE`;
\end{code}

\noindent
A valid tree is \eg{} {\tt BRANCH (LEAF 5) (BRANCH (LEAF 1) (LEAF 7))}
with type {\tt num TREE}, where {\tt  num} is the type name for a natural
number. We can define recursive functions, \eg{}
\begin{code}
val MAP_TREE_def = Define `
  (MAP_TREE f (LEAF n) = LEAF (f n)) \conj{}
  (MAP_TREE f (BRANCH u v) = BRANCH (MAP_TREE f u) (MAP_TREE f v))`;
\end{code}
{\tt  SQUARE\_def} and {\tt  MAP\_TREE\_def} are theorems containing the
above definitions. Theorems describing {\tt  TREE} can be retrieved by coping
the following into HOL4 (by pressing {\tt C-space} then {\tt M-h M-r}, Section~\ref{copy}).
\begin{code}
val TREE_11 = fetch "-" "TREE_11";
val TREE_distinct = fetch "-" "TREE_distinct";
\end{code}

\mysubsec{Making a theory}

Proofs and definitions are stored in files called scripts, \eg{} we
can store the definitions from above in a file called {\tt
  mytreeScript.sml}, which should begin with the lines
\begin{code}
open HolKernel boolLib bossLib Parse;
val _ = new_theory "mytree";
\end{code}
and end with the line
\begin{code}
val _ = export_theory();
\end{code}
Replace {\tt prove} by {\tt store\_thm} for results you
wish to export from the theory, \eg{}
\begin{code}
val LESS_ADD_1 = store_thm("LESS_ADD_1",{`}`!n.n<n+1{`}`,DECIDE_TAC);
\end{code}
Make sure your script only consists of ML definitions ({\tt \small val
  x = y}, {\tt \small fun g x = y}), open commands ({\tt \small
  open x y z}) and comments {\tt \small (* comment *)}.

The theory {\tt mytreeTheory} is created by executing {\tt Holmake} in
the directory where {\tt mytreeScript.sml} is stored. A
readable version of the theory is stored under {\tt mytreeTheory.sig}.

\newpage

\mysec{Searching for theorems and theories\label{search}}

\newcommand{\itemx}[2]{\texttt{\small #1} &\;\textrm{--}\;& \textrm{#2} \\}

HOL4 has a large collection of library theories. The most commonly used are:
\begin{displaymath}
\begin{array}{lcl}
\itemx{arithmeticTheory}{natural numbers, \eg{} {\tt\small 0, 1, 2, SUC 0, SUC 6}}
\itemx{listTheory}{lists, \eg{} {\tt\small [1;2;3] = 1::2::3::[], HD xs}}
\itemx{pred\_setTheory}{simple sets,\ \eg{} {\tt\small \{1;2;3\}, x IN s UNION t}}
\itemx{pairTheory}{pairs/tuples,\ \eg{} {\tt\small (1,x), (2,3,4,5), FST (x,y)}}
\itemx{wordsTheory}{n-bit words,\ \eg{} {\tt\small 0w:word32, 1w:'a word, x {!!}~1w}}
\end{array}
\end{displaymath}
Other standard theories include:
\begin{code}
arithmeticTheory  bagTheory  boolTheory  combinTheory  fcpTheory
finite_mapTheory  fixedPointTheory  floatTheory  integerTheory
limTheory  optionTheory  probTheory  ratTheory  realTheory
relationTheory  rich_listTheory  ringTheory  seqTheory
sortingTheory  state_transformerTheory  stringTheory  sumTheory
topologyTheory  transcTheory  whileTheory
\end{code}
The library theories are conveniently browsed using the following HTML reference page (created when HOL4 is compiled).
Replace {\tt <path>} with the path to your HOL4 installation.
\begin{code}
<path>/HOL/help/HOLindex.html
\end{code}

Once theories has been opened (see Section~\ref{copy}), one can search for theorems in the current
context using {\tt DB.match}, \eg{} with {\tt arithmeticTheory} opened,
\begin{code}
DB.match [] {`}`n DIV m <= k{`}`
\end{code}
prints a list of theorems containing $n\; \texttt{\small DIV} \;m \leq k$ for some $n,m,k$:
\begin{code}
[(("arithmetic", "DIV_LE_MONOTONE"),
  (|- !n x y. 0 < n \conj{} x <= y ==> x DIV n <= y DIV n, Thm)),
 (("arithmetic", "DIV_LE_X"),
  (|- !x y z. 0 < z ==> (y DIV z <= x = y < (x + 1) * z), Thm)),
 (("arithmetic", "DIV_LESS_EQ"),
  (|- !n. 0 < n ==> !k. k DIV n <= k, Thm))]
\end{code}
Try to write increasingly specific queries if the returned list is long, \eg{}
{\tt\small DB.match [] {`}`n DIV m{`}`} returns a list of length 32. Note that {\tt\small DB.match [] {`}`DIV{`}`}
does not work since {\tt\small DIV} is an infix operator, but {\tt\small DB.match [] {`}`\$DIV{`}`} works.

\mysec{Common proof tactics\label{tactics}}

Most HOL4 proofs are carried out by stating a goal and then applying
\emph{proof tactics} that reduce the goal.  This section describes
basic use of the most important proof tactics.  Press {\tt\small
  C-space} then {\tt\small M-h e} to apply a tactic
(Section~\ref{apply}).

\mysubsec{Automatic provers}

Simple goals can often be proved automatically by {\tt\small
  METIS\_TAC}, {\tt\small DECIDE\_TAC} or {\tt\small EVAL\_TAC}.
{\tt\small METIS\_TAC} is first-order prover which is good at general
problems, but requires the user to supply a list of relevant theorems,
\eg{} the following goal is proved by {\tt\small METIS\_TAC
  [MOD\_TIMES2,MOD\_MOD,MOD\_PLUS]}.
\begin{code}
!k. 0 < k ==> !m p n. (m MOD k * p + n) MOD k = (m * p + n) MOD k
\end{code}

\noindent
{\tt\small DECIDE\_TAC} handles linear arithmetic over natural
numbers, \eg{} {\tt\small DECIDE\_TAC} solves:
\begin{code}
!m n k. m < n \conj{} n < m+k \conj{} k <= 3 \conj{} ~(n = m+1) ==> (n = m+2)
\end{code}
{\tt\small EVAL\_TAC} is good at fully instantiated goals, \eg{} {\tt\small EVAL\_TAC} solves:
\begin{code}
0 < 5 \conj{} (HD [4;5;6;7] + 2**32 = 3500 DIV 7 + 4294966800)
\end{code}

\mysubsec{Proof set-up}

Goals that contain top-level universal quantifiers ({\tt !x.}),
implication ({\tt ==>}) or conjunction ({\tt \conj{}}) are often
taken apart using {\tt\small REPEAT STRIP\_TAC} or just {\tt\small
  STRIP\_TAC}, \eg{} the goal {\tt\small `!x{.}~(!z{.}~x < h z) ==> ?y{.}~f x = y`}
becomes the following. (Assumptions are written under the line.)
\begin{code}
    ?y. f x = y
    ------------------------------------
      !z. x < h z
\end{code}

\mysubsec{Existential quantifiers}

Goals that have a top-level existential quantifier can be given a
witness using {\tt \small Q.EXISTS\_TAC}, \eg{} {\tt \small
  Q.EXISTS\_TAC `1`} applied to goal {\tt \small ?n{.}~!k{.}~n * k = k}
produces goal {\tt \small !k{.}~1 * k = k}.

\mysubsec{Rewrites}

Most HOL4 proofs are based on rewriting using equality theorems, \eg{}
\begin{code}
ADD_0:            |- !n. n + 0 = n
LESS_MOD:         |- !n k. k < n ==> (k MOD n = k)
\end{code} {\tt \small ASM\_SIMP\_TAC} and {\tt \small FULL\_SIMP\_TAC} are two
commonly used rewriting tactics, \eg{} suppose the goal is the following:
\begin{code}
    5 + 0 + m = (m MOD 10) + (5 MOD 8)
    ------------------------------------
      0.  p = 2 + 0 + (m MOD 10)
      1.  m < 10
\end{code}
{\tt \small ASM\_SIMP\_TAC bool\_ss
  [ADD\_0,LESS\_MOD]} rewrites the
goal using the supplied theorems together with the current goal's
assumptions and some boolean simplifications {\tt \small bool\_ss}:
\begin{code}
    5 + m = m + (5 MOD 8)
    ------------------------------------
      0.  p = 2 + 0 + (m MOD 10)
      1.  m < 10
\end{code}
{\tt \small FULL\_SIMP\_TAC bool\_ss [ADD\_0,LESS\_MOD]}
does the same except that it also applies the rewrites to the
assumptions:
\begin{code}
    5 + m = m + (5 MOD 8)
    ------------------------------------
      0.  p = 2 + m
      1.  m < 10
\end{code}

{\tt \small bool\_ss} can be replaced by {\tt \small std\_ss}, which
is a stronger simplification set that would infer {\tt \small 5 < 8}
and hence simplify {\tt\small 5 MOD 8} as well. I recommend that the
interested reader also reads about {\tt\small AC}, {\tt\small Once}
and {\tt\small SRW\_TAC}.

\mysubsec{Induction}

Use the tactic {\tt\small Induct\_on `x`} to start an induction on {\tt\small x}.
Here {\tt x} can be any variable with a recursively defined type,
\eg{} a natural number, a list or a {\tt\small TREE} as defined in
Section~\ref{definition}.  One can start a complete (or strong)
induction over the natural number {\tt\small n} using {\tt\small
  completeInduct\_on `n`}.

\mysubsec{Case splits}

A goal can be split into cases using {\tt\small Cases\_on `x`}. The
goal is split according to the constructors of the type of {\tt\small
  x}, \eg{} for the following goal
\begin{code}
    !x. ~(x = []) ==> (x = HD x::TL x)
\end{code}
{\tt\small Cases\_on `x`} splits the goal into two:
\begin{code}
    ~(h::t = []) ==> (h::t = HD (h::t)::TL (h::t))

    ~([] = []) ==> ([] = HD []::TL [])
\end{code}
Case splits on boolean expressions are also useful, \eg{} {\tt\small Cases\_on `n < 5`}.

\mysubsec{Subproofs}

It is often useful to start a mini-proof inside a larger proof, \eg{} for the goal
\begin{code}
    foo n
    ------------------------------------
      0 < n
\end{code}
we might want to prove {\tt\small h n = g n} assuming {\tt\small 0 <
  n}.  We can start such a subproof by typing {\tt\small `h n = g n`
  by ALL\_TAC}.\footnote{You can also use the emacs binding {\tt\small
    M-h M-s} with the cursor inside the sub-goal.} The new goal stack:
\begin{code}
    foo n
    ------------------------------------
      0.  0 < n
      1.  h n = g n

    h n = g n
    ------------------------------------
      0 < n
\end{code}
If {\tt\small `h n = g n`} can be proved in one step, \eg{} using {\tt\small METIS\_TAC [MY\_LEMMA]}, then
apply {\tt\small `h n = g n` by METIS\_TAC [MY\_LEMMA]} instead of
{\tt\small `h n = g n` by ALL\_TAC}.  If the sub-goal requires
multiple steps the tactic after the \texttt{by} will need to be
parenthesised: {\tt\small`\textit{goal}` by ($\mathit{tac}_1$ THEN
  $\mathit{tac}_2$ ...)}

\mysubsec{Proof by contradiction}

Use {\tt\small CCONTR\_TAC} to add the negation of the goal to the
assumptions.  The task is then to prove that one of the assumptions of
the goal is false. One can \eg{} add more assumptions using
{\tt\small `...` by ALL\_TAC}, described above, until one assumption is the
negation of another assumption (and then apply {\tt\small METIS\_TAC []}).

\mysubsec{More tactics\label{html}}

An HTML reference of all tactics and proof tools is created when HOL4 is compiled.
Replace {\tt\small <path>} with the path to your HOL4 installation.
\begin{code}
<path>/HOL4/help/src/htmlsigs/idIndex.html
\end{code}
The reference provides an easy way to access both the implementations of
tactics as well as their documentation (where such exists).
The interested reader may want to look up the following:
\begin{code}
CONV_TAC  DISJ1_TAC  DISJ2_TAC  MATCH_MP_TAC  MP_TAC  PAT_ASSUM  Q
\end{code}

\mysec{Further reading and general advice\label{reading}}

General advice on using HOL4:
\begin{enum}
\item State definitions carefully with the subsequent proofs in mind.
\item Make proofs reusable by splitting them into multiple small lemmas.
\item Strive to make the most of library theories and rewriting.
\end{enum}
One can only learn HOL4 via examples, so try proving something.
Example problems and solutions are presented in the \emph{HOL
  Tutorial}, available under:
\begin{code}
http://hol.sf.net/documentation.html
\end{code}
The same page also contains:
\begin{enum}
\item[~]\emph{HOL Description} -- a description of the HOL4 system
\item[~]\emph{HOL Reference} -- a detailed descriptions of proof tactics and other tools
\item[~]\emph{HOL Logic} -- a presentation of the underlying logic
\end{enum}
For day-to-day look-ups, I find {\tt DB.match} (illustrated in
Section~\ref{search}) and the HTML reference (mentioned in
Section~\ref{html}) most helpful.

\end{document}


% Created 2019-09-05 jue 12:34
\documentclass[letterpaper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fixltx2e}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{float}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{marvosym}
\usepackage{wasysym}
\usepackage{amssymb}
\usepackage{hyperref}
\tolerance=1000
\usepackage{khpreamble}
\usepackage{subfigure}
\usepgfplotslibrary{groupplots}
\addtolength{\oddsidemargin}{-4mm}
\addtolength{\evensidemargin}{-4mm}
\addtolength{\textheight}{33mm}
\author{Kjartan Halvorsen}
\date{}
\title{}
\hypersetup{
  pdfkeywords={},
  pdfsubject={},
  pdfcreator={Emacs 25.3.50.2 (Org mode 8.2.10)}}
\begin{document}

\section*{Root locus, Bode- and Nyquist plots, relative stability}
\label{sec-1}
Kjartan Halvorsen, September 2019

\subsection*{Draw a root locus for PI-control of the reservoir}
\label{sec-1-1}
\begin{center}
  \begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

    \node[coordinate] (input) {};
    \node[sumnode, right of=input, node distance=16mm] (sum) {\tiny $\Sigma$};
    \node[block, right of=sum, node distance=25mm] (controller)  {$F(z) = K\frac{z-0.5}{z-1}$};
    \node[block, right of=controller, node distance=35mm] (plant)  {$G(z) = \frac{1}{z-1}$};
    \node[coordinate, right of=plant, node distance=30mm] (output) {};

    \draw[->] (input) -- node[above, pos=0.3] {$y_{ref}(k)$} (sum);
    \draw[->] (sum) -- node[above] {$e(k)$} (controller);
    \draw[->] (controller) -- node[above] {$u(k)$} (plant);
    \draw[->] (plant) -- node[coordinate] (measure) {}  node[above, near end] {$y(k)$} (output);
    \draw[->] (measure) -- ++(0,-14mm) -| node[pos=0.95, left] {$-$} (sum);
  \end{tikzpicture}
\end{center}

\includegraphics[width=0.36\linewidth]{../../figures/imaginary-plane-empty}
\includegraphics[width=0.36\linewidth]{../../figures/imaginary-plane-empty}\\

\subsection*{Frequency response primer: The product of two complex numbers}
\label{sec-1-2}
\begin{itemize}
\item At time \(k=2\), the discrete-time complex exponential signal \(u(k) = \mathrm{e}^{i\frac{\pi}{12}k}\) has the value \(u(2) = \mathrm{e}^{i\frac{\pi}{6}}\).
\item A pulse-transfer function \(H(z)\) is a mapping from the complex plane to the complex plane, that is \( H: \; \mathbb{C} \rightarrow \mathbb{C}\). Consider a specific pulse-transfer function \(H(z)\) that when evaluated at \(z=\mathrm{e}^{i\frac{\pi}{12}}\) has the value  \(H(\mathrm{e}^{i\frac{\pi}{12}}) = 2\mathrm{e}^{-i\frac{\pi}{3}}\).
\end{itemize}

\textbf{Determine the magnitude, the argument and the imaginary part of the product \(H(\mathrm{e}^{i\frac{\pi}{12}})u(2)\). Then mark the first few values of the sequence \(H(\mathrm{e}^{i\frac{\pi}{12}})u(k)\).}

\includegraphics[width=0.34\linewidth]{../../figures/imaginary-plane-two-points}

\subsection*{Bode plot and Nyquist plots}
\label{sec-1-3}
Sketch the Nyquist plot corresponding to the Bode plot! Then mark the phase margin and amplitude margins!
\begin{center}
\includegraphics[width=0.95\linewidth]{../../figures/bode-nyquist-exc-1}
\end{center}

\begin{center}
\includegraphics[width=0.95\linewidth]{../../figures/bode-nyquist-exc-2}
\end{center}
% Emacs 25.3.50.2 (Org mode 8.2.10)
\end{document}


\chapter{Warm-up: CCω}
\label{chap:bidir-ccw}

\margintoc

% In this chapter, we give an alternate presentation of \kl{CCω}, as defined in
% \cref{fig:ccw-typing}. Most of the ideas are abstract over the notion of \kl{conversion}
% that is considered,
% so either \kl(conv){declarative} or \kl(conv){algorithmic} conversion can be used without
% much impact.

\section{Turning \kl(tit){CCω} Bidirectional}
\label{sec:bidir-ccw}

\subsection{McBride’s discipline}
\label{sec:bidir-mcbride}

\AP To design our bidirectional type system, we follow a discipline exposed by McBride
\sidecite[1em]{McBride2018,McBride2019}.
The central point is to distinguish in a judgement between three \intro{modes}:
the \reintro{subject},
whose well-formation is under examination, \reintro{inputs},
whose well-formation is a condition for the judgement to be meaningful,
and \reintro{outputs}, whose well-formation should be a consequence of the judgement.
By \intro{well-formed}, which we use indistinctly for contexts, terms and types,
we mean:
\begin{itemize}
  \item $\vdash \Gamma$ in the case of a context $\Gamma$,
  \item $\Gamma \vdash T \ty \uni$ in the case of a type $T$,
  \item the existence of some $T$ such that $\Gamma \vdash t \ty T$ in the case of a term $t$.
\end{itemize}
For the last two, this is relative to an implicit context $\Gamma$.
We also use \reintro{well-typed} for a term, with the same meaning as \kl{well-formed}.

\AP In the case of \intro{inference} $\intro*\inferty{\Gamma}{t}{T}$, the subject is $t$,
$\Gamma$ is an input and $T$ is an output.
On the contrary, in \intro{checking} $\intro*\checkty{\Gamma}{t}{T}$, $t$ is still the subject
and $\Gamma$ is an input, but this time $T$ is an input as well.
This means that one should consider whether $\inferty{\Gamma}{t}{T}$
only in cases where $\vdash \Gamma$ is already known,
and if the judgement is derivable it should be possible to conclude that
not only $t$, but also $T$ are well-formed.

In order to enforce this property globally, all inference rules should locally
preserve it as an invariant.%
\sidenote{The motto – slightly adapted from \textcite{McBride2018} – is:
  \textit{A rule is a server for its conclusion and a client for its premises.
  Servers receive promises about inputs and make promises about outputs, clients make
  promises about inputs and receive promises about outputs.}}
More precisely, information flows in a clockwise manner. First, we can assume that inputs
to the conclusion are well-formed, as inputs to the whole rule. Next, we move to the
premises. Here the constraint is reversed: we should ensure that inputs to a premise are
well-formed, but can assume that its outputs and subjects are. We might need to use
the well-formation of subjects or outputs of previous ones for that.
Finally, information goes to the conclusion again, and now not only the subject but also
the output should be well-formed if all those of the premises are.

\begin{figure*}
  \begin{mathpar}
  % \inferrule[(1)]
  %   {\phantom{\Gamma \vdash t \pity{\P} \P x : A. B}}
  %   {\Gamma \vdash \tikzmarkin{s1} t\ u \tikzmarkend{s1} \ity \phantom{\subs{B}{x}{u}}} \and
  \inferrule[(1)]
    {\tikzmarkin[ch]{p2}\Gamma \tikzmarkend{p2} \vdash
      \tikzmarkin[uc]{p23} t \pity{\P} \P x : A. B \tikzmarkend{p23}\\
      \tikzmarkin[ch]{p21}\Gamma\tikzmarkend{p21} \vdash
      \tikzmarkin[uc]{p22} u \cty A\tikzmarkend{p22}}
    {\tikzmarkin[ch]{c2}\Gamma\tikzmarkend{c2} \vdash
      \tikzmarkin[uc]{c21}t\ u \ity \subs{B}{x}{u}\tikzmarkend{c21}} \and
  \inferrule[(2)]
  {\tikzmarkin[ch]{p3}\Gamma \vdash t \pity{\P} \P x : A. B \tikzmarkend{p3}\\
    \tikzmarkin[ch]{p31}\Gamma\tikzmarkend{p31} \vdash
    \tikzmarkin[uc]{p32} u \cty A\tikzmarkend{p32}}
  {\tikzmarkin[ch]{c3}\Gamma\tikzmarkend{c3} \vdash
    \tikzmarkin[uc]{c31}t\ u \ity \subs{B}{x}{u}\tikzmarkend{c31}} \and
  \inferrule[(3)]
  {\tikzmarkin[ch]{p4}\Gamma \vdash t \pity{\P} \P x : A. B \tikzmarkend{p4}\\
    \tikzmarkin[ch]{p41}\Gamma\tikzmarkend{p41} \vdash
    \tikzmarkin[uc]{p42} u \tikzmarkend{p42} \cty \tikzmarkin[ch]{p43}A\tikzmarkend{p43}}
  {\tikzmarkin[ch]{c4}\Gamma\tikzmarkend{c4} \vdash
    \tikzmarkin[uc]{c41}t\ u \ity \subs{B}{x}{u}\tikzmarkend{c41}} \and
  \inferrule[(4)]
  {\tikzmarkin[ch]{p5}\Gamma \vdash t \pity{\P} \P x : A. B \tikzmarkend{p5}\\
    \tikzmarkin[ch]{p51}\Gamma \vdash u \cty A\tikzmarkend{p51}}
  {\tikzmarkin[ch]{c5}\Gamma\tikzmarkend{c5} \vdash
    \tikzmarkin[uc]{c51}t\ u \ity \subs{B}{x}{u}\tikzmarkend{c51}} \and
  \inferrule[(5)]
  {\tikzmarkin[ch]{p6}\Gamma \vdash t \pity{\P} \P x : A. B \tikzmarkend{p6}\\
    \tikzmarkin[ch]{p61}\Gamma \vdash u \cty A\tikzmarkend{p61}}
  {\tikzmarkin[ch]{c6}\Gamma \vdash t\ u \ity \subs{B}{x}{u}\tikzmarkend{c6}}
  \end{mathpar}
  \caption{An illustration of McBride’s discipline (well-formed objects are in blue, those not known to be so are in red)}
  \label{fig:bidir-mcbride}
\end{figure*}

As an illustration, an example of a rule that respects this discipline – that
for application – is given in \cref{fig:bidir-mcbride}.
Let us ignore for an instant the exact meaning of the judgement $\mathord{\pity{\P}}$
which we introduce soon, and whose modes are the same as inference.
Instead, focus on well-formation: objects known to be well-formed
are on a blue background, those which are not are on a red one.
First, $\Gamma$ is well-formed, as an input to the conclusion (1). Thus
we can thus move to the first premise, since its only input is $\Gamma$.
From that premise holding, we learn that $t$ and $\P x : A.\ B$ are well-formed (2).
Therefore, $A$ is in particular well-formed, and we can move to the second premise
whose two inputs are now known to be well-formed (3). From it, we learn
that $u$ is well-formed (4). Now we can deduce that $t\ u$ is well-formed. But this is
not enough: since $\subs{B}{x}{u}$ is an output of the conclusion,
we must ensure it is well-formed too. Fortunately,
it is, since $\P x : A.\ B$ is, so $B$ is too,%
\sidenote{In the extended context $\Gamma, x : A$.}
and so $\subs{B}{x}{u}$ is as well, since $\Gamma \vdash u \cty A$.
Thus, we can move back to the conclusion (5), which ends our roundtrip.

A somewhat similar discipline has appeared independently in
\sidetextcite[][Section 4]{Dunfield2021}, where it is called ”Pfenning’s recipe”.
The main criterion is \emph{mode-correctness}, which demands
an information flow similar to McBride’s, but is coarser, as it does not
consider well-formation of the objects, only their knowledge. For instance,
in the case of $\l x : A.\ t$, that criterion allows to directly extend a context
with $A$ to infer a type for $t$, because it is known,
but McBride’s discipline forbids it, because $A$’s well-formation is not established.
Another related condition is also used in \sidetextcite{Bauer2020}.
The authors introduce the notions of a \emph{(weakly) presuppositive type theory}
\cite[Def.~5.6]{Bauer2020} and of \emph{well-presented} premise-family and rule-boundary
\cite[Def.~6.16 and 6.17]{Bauer2020}, using what they call the \intro{boundary} of a judgement
as the analogue of our \kl{inputs} and \kl{outputs}.
Due to their setting being undirected, this is however more restrictive,
because they are not able to distinguish inputs from outputs and thus cannot relax the
condition to only demand inputs to be well-formed but not outputs.

\AP Because of our dependently typed setting, we actually need to introduce a third judgement,
beyond the already mentioned \kl{inference} and \kl{checking}:
\intro{constrained inference}, written
$\intro*\pinferty{h}{\Gamma}{t}{T}$, where $h$ is either $\Pi$ or $\uni$.%
\sidenote{These are the only type formers in \kl{CCω}, but
in \kl{PCUIC}, $h$ can also be \eg an inductive type.}
Constrained inference is a judgement%
\sidenote{Or, rather, a family of judgements indexed by $h$.}
with the exact same \kl{modes} as \kl{inference},
but where the type output is not completely free.
Rather, as the name suggests, a constraint is imposed on it, namely that its head constructor can only be the corresponding element of $h$.
This is needed to handle the behaviour absent in simple types that some terms might not have a desired type “on the nose”. Take for instance the first premise
$\Gamma \vdash t \ty \P x : A .\ B$ of \ruleref{rule:cic-app}.
What bidirectional judgement should replace it?
It would be too much to ask $t$ to directly infer a Π-type, as some reduction might be needed to uncover this $\P$. Checking also cannot be used, because the domain and codomain of the tentative Π-type are not known at that point: they should be inferred from $t$.

Finally, this mode distinction also applies to computation-related judgements,
although those have no subject.
Instead, what is under scrutiny is the “computational content” of the rule.
For conversion $\Gamma \vdash T \conv T' : \uni$,
both $T$ and $T'$ are inputs and thus should be known to be well-formed beforehand.
For reduction $T \red T'$, on the contrary, $T$ is an input,
but $T'$ is an output. Hence, only $T$ needs to be well-formed \textit{a priori},
and we rely on the \kl{subject reduction} property to ensure
that the output $T'$ also is.

\subsection{The typing rules}

To transform the rules of \kl{CCω} as given in \cref{fig:ccw-typing},
start by recalling that we wish to obtain a complete bidirectional type system.
Therefore, any term should infer a type, and thus
all rules where the subject of the conclusion starts with a term former
should give rise to a rule with \kl{inference} as a conclusion.
It thus remains to choose the judgements for the premises,
which amounts to determining their \kl{modes}.
If a term in a premise appears as input in the conclusion or output of a previous premise, then it can be considered an input, otherwise it must be an output. Moreover, if a type output is unconstrained, then \kl{inference} can be used, otherwise we must resort to
\kl{constrained inference}. This transformation leads to the rules of \cref{fig:ccw-bidir-infer}.

\begin{figure}[ht]
  \ContinuedFloat*
  \begin{mathpar}
    \inferdef{Var}
      {(x : T) \in \Gamma}
      {\inferty{\Gamma}{x}{T}}
    \label{rule:bd-var} \and
    \inferdef{Univ}
      { }
      {\inferty{\Gamma}{\uni[i]}{\uni[i+1]}}
    \label{rule:bd-univ} \and
    \inferdef{ΠTy}
      {\pinferty{\uni}{\Gamma}{A}{\uni[i]} \\
        \pinferty{\uni}{\Gamma, x : A}{B}{\uni[j]}}
      {\inferty{\Gamma}{\P x : A .\ B}{\uni[\umax{i}{j}]}}
    \label{rule:bd-prod} \and
    \inferdef{Abs}
      {\pinferty{\uni}{\Gamma}{A}{\uni[i]} \\ \inferty{\Gamma, x : A}{t}{B}}
      {\inferty{\Gamma}{\l x : A .\ t}{\P x : A .\ B}}
    \label{rule:bd-abs} \and
    \inferdef{App}
      {\pinferty{\Pi}{\Gamma}{t}{\P x : A .\ B} \\ \checkty{\Gamma}{u}{A}}
      {\inferty{\Gamma}{t\ u}{\subs{B}{x}{u}}}
    \label{rule:bd-app} \\
  \end{mathpar}
  \caption{Rules for inference in bidirectional \kl{CCω}}
  \label{fig:ccw-bidir-infer}
\end{figure}

In anticipation, we set the typing rules for \kl{CCω} so that this transformation would be
direct. This particularly applies to the undirected \ruleref{rule:cic-abs},
recalled opposite.
Indeed, there are at least two other ways to write it, which do not lead to a valid
bidirectional presentation.
\marginnote{
  \normalsize
  \begin{mathpar}
  \inferrule*[vcenter,right=Abs]
  {\Gamma \vdash A \ty \uni \\ \Gamma, x : A \vdash t \ty B}
  {\Gamma \vdash \l x : A.\ t \ty \P x : A.\ B}
  \end{mathpar}
}
The first, which is the usual one in \kl{Pure Type Systems} (\kl{PTS})
\sidecite{Barendregt1991},
is to have $\Gamma \vdash \P x : A.\ B \ty \uni$ as a premise
instead of $\Gamma \vdash A \ty \uni$. In the setting of a general \kl{PTS},
this is needed, because not every Π-type is well-formed,
even if the domain and codomain are.%
\sidenote{\kl{PTS} where this is true are called \intro{full}.}
However, this premise is problematic in the bidirectional setting. Indeed, $B$ can only be
inferred as a type for the body of the abstraction $t$. But to infer a type for $t$, the
context $\Gamma, x : A$ needs to be well-formed, which is not known if this premise is
the first one.
This issue has been identified by \sidetextcite{Pollack1992}, who remarked that the
bidirectional structure we present here is only equivalent to the undirected one
in semi-full \kl{PTS} – a slight generalization of the full ones.
In a full \kl{PTS}, the opposite path of simply removing the first premise altogether
can also be taken, relying on \kl{validity} to ensure that $\vdash \Gamma, x \ty A$ and thus
$\Gamma \vdash A : \uni$. But again, in a bidirectional setting,
this does not respect McBride’s discipline.

The main difference between the bidirectional and undirected rules is that we dropped
hypotheses of context well-formation in Rules~\nameref{rule:bd-univ} and
\nameref{rule:bd-var}. Indeed, since the context is always supposed to be well-formed
as an input to the conclusion, it is not useful to re-check it. This is also in line with implementations, where the context is not checked at leaves of a derivation tree, with performance issues in mind. The well-formation invariants then ensure that any derivation starting with the (well-formed) empty context will only ever encounter well-formed contexts.

With the rules for term formers taken care of,
we are left with the single \ruleref{rule:cic-conv}.
\begin{marginfigure}
  \ContinuedFloat
  \begin{mathpar}
    \inferdef{Check}
      {\inferty{\Gamma}{t}{T'} \\ T' \conv T}
      {\checkty{\Gamma}{t}{T}}
    \label{rule:bd-check} \and
    \inferdef{UnivInf}
      {\inferty{\Gamma}{t}{T} \\ T \fred \uni[i]}
      {\pinferty{\uni}{\Gamma}{t}{\uni[i]}}
      \label{rule:bd-pinf-univ} \and
    \inferdef{ΠInf}
      {\inferty{\Gamma}{t}{T} \\ T \fred \P x : A. B}
      {\pinferty{\P}{\Gamma}{t}{\P x : A . B}}
      \label{rule:bd-pinf-prod}
  \end{mathpar}
  \caption{Computation rules for bidirectional \kl{CCω}}
  \label{fig:bidir-ccw-other}
  \end{marginfigure}
There are two different possible adaptations of this rule, depending on
\kl{modes} for computation.
In the case of \kl{checking}, the target type is an input, so
it can be compared to the inferred one using \kl{conversion}.
But in the case of \kl{constrained inference} it is unknown, and so
we must resort to \kl{reduction} to obtain it from the inferred one.
Using \kl{conversion} would not respect modes, since it has two inputs.
This eventually leads to the decomposition of \ruleref{rule:cic-conv} into
\ruleref{rule:bd-check} in the first case, while
\nameref{rule:bd-pinf-univ} and \nameref{rule:bd-pinf-prod} correspond to the second case.
Note that while the way conversion and reduction can be used in derivations have changed,
those relations themselves remain untouched,
we only refined them by giving them an explicit mode.
We also do not need to choose one or the other notion of \kl{conversion} yet. Instead, we can
stay abstract, only listing the properties we need from it in order to establish the
equivalence.

\subsection{Constrained inference in disguise}

This need to split the conversion rule into a reduction and conversion sub-routines depending on the mode is of course known to the implementors of proof assistants \sidecite{Abel2011}.
It explains in part the ubiquity of \kl(red){weak-head} reduction
in the dependently typed setting.
Indeed, it is exactly the minimal reduction strategy that is needed to expose the
head constructor of a type, and thus to implement constrained inference.

Still, reduction is only a means to determine whether a certain term fits into
a certain kind of types. In the setting of \kl{CCω}, this is basically the only way to do.
However, as soon as conversion is extended or modified,
% for instance with unification
% \sidecite{Asperti2012}, coercions \sidecite{Asperti2012,Sozeau2007}
% or graduality \sidecite{LennonBertrand2022},
reduction is often not enough any more.
Putting constrained inference forward explains some ideas that recurrently appear in
such settings:  they are not ad-hoc workarounds,
but are based on the need to account for constrained inference.

We already mentioned \sidetextcite{Pollack1992}, where $\Gamma \vdash t \ty T$ is used
for inference, and a judgement written $\Gamma \vdash t \mathrel{:\geq} T$ –
denoting type inference followed by reduction –
is used to effectively inline the two hypothesis of our constrained inference rules.
Checking is also inlined.
Similarly, \sidetextcite{Abel2008} use a judgement written $\Delta \vdash V \delta \Uparrow \operatorname{Set} \rightsquigarrow i$, where a type $V$ is checked to be well-formed, but with its exact level $i$ free. This corresponds very closely to our use of $\pity{\uni}$.
\sidetextcite{Gratzer2019} similarly use a judgement $\Xi \vdash T \Leftarrow type$,
but they do not bother inferring the level as they never have any need for it.

But the main area where constrained inference repeatedly becomes apparent is that of
elaboration. For instance,
\sidetextcite{Saibi1997} describes an elaboration mechanism inserting coercions between types.
This happens primarily during checking, when both types are known.
However, \citeauthor{Saibi1997} introduces two special classes to handle the need
to cast a term to a sort or a function type without more information,
exactly in the places where we resort to constrained inference instead of checking.
More recently, \sidetextcite{Sozeau2007} describes a system where conversion is augmented
to handle subset types.
As in \textcite{Pollack1992}, $\Gamma \vdash t \ty T$ is used for inference,
and the other judgements are inlined.
Once again, reduction is not enough to perform constrained inference, this time
because type constructors can be hidden in subsets:
an inhabitant of a type such as $\{f : \Nat \to \Nat \mid f\ \z = \z \}$
should be usable as a function of type $\Nat \to \Nat$.
An erasure procedure is therefore required on top of reduction to remove subsets in the places where we use constrained inference.

Analogous ideas can also be found in \kl{Matita}'s elaboration algorithm, as described in
\sidetextcite{Asperti2012}.
Indeed, the presence of unification meta-variables on top of coercions makes it
even clearer that a specific treatment of what we identified as constrained inference is
required.
In the case of $\pity{\P}$, they have two rules to apply a function,
one where its inferred type reduces to a Π-type, corresponding to \ruleref{rule:bd-pinf-prod},
and another one to handle the case when the inferred type instead reduces to a meta-variable.
As \citeauthor{Saibi1997} and \citeauthor{Sozeau2007}, they also
need to handle coercions for terms in function position. However, their solution is different:
they introduce new meta-variables for the domain and codomain,
and rely on unification, which is available in their setting, to find values for those.
They also need to introduce a special judgement they call
\intro{type-level enforcing}, which corresponds to our $\pity{\uni}$ judgement.
The solution they take for Π-types is not viable there, as one would need a kind of universe
meta-variable. Instead, they rely on backtracking to test multiple possible universe choices.

Finally, in \arefpart{gradual}, somewhat akin to the use of meta-variables in
\textcite{Asperti2012}, there are two rules per constrained inference judgement.
One when the head constructor is the desired one – as for \kl{CCω} –,
and a second one to handle the wildcard $\?$, characteristic of gradual type systems.

\section{Properties of the Bidirectional System}
\label{sec:bidir-prop}

Let us now state and sketch proofs of the main properties of the bidirectional system.
The first two relate it to the
undirected one: it is both \kl(bidir){correct} – terms typeable in the bidirectional system are typeable in the undirected system – and \kl(bidir){complete} – all terms typeable in the undirected system are also typeable in the bidirectional system.
Next, we investigate \kl{uniqueness of types}, and its relation to the choice of a strategy for reduction.
Finally, we expose how \kl{strengthening} can be shown for undirected \kl{CCω}
by proving it on the bidirectional side.

\subsection{Correctness}

A bidirectional derivation can be seen as a refinement of an undirected derivation.
Indeed, the bidirectional structure can be erased to obtain an undirected derivation,
replacing each bidirectional rule with the corresponding undirected rule.
As bidirectional rules lack some premises of the undirected ones, missing some sub-derivations
must be retrieved by relying on the well-formation invariants going with McBride’s discipline.
Thus, we get the following correctness theorem – note how the
discipline manifests as well-formation hypothesis on inputs.

\begin{minipage}{\textwidth}
\begin{theorem}[\intro{Correctness} of bidirectional typing for \kl{CCω}]
  \label{thm:corr-ccomega}
  If $\Gamma$ is well-formed and $\inferty{\Gamma}{t}{T}$ or $\pinferty{h}{\Gamma}{t}{T}$,
  then $\Gamma \vdash t \ty T$.
  If both $\Gamma$ and $T$ are well-formed and
  $\checkty{\Gamma}{t}{T}$, then $\Gamma \vdash t \ty T$.
\end{theorem}
\end{minipage}

\begin{minipage}{\textwidth}
\begin{proof}
  By mutual induction on the bidirectional typing derivation.

  Each rule of the bidirectional system can be replaced by the corresponding rule of the
  undirected system, with all three rules \nameref{rule:bd-check}, \nameref{rule:bd-pinf-univ} and \nameref{rule:bd-pinf-prod} replaced by
  \nameref{rule:cic-conv}. In all cases, the induction hypothesis can be used on sub-derivations of the bidirectional judgement, because context extensions and checking are
  done with types that are known to be well-formed,%
  \sidenote{This is the point where following McBride’s discipline is crucial!}
  by induction hypothesis on previous premises and possibly \kl{validity}.

  Some sub-derivations of the undirected rules that have no counterpart
  in the bidirectional ones are however missing.
  In Rules \nameref{rule:cic-univ} and \nameref{rule:cic-var},
  the hypothesis that $\Gamma$ is well-formed is enough to get the required premise.
  For \ruleref{rule:bd-check},
  the well-formation hypothesis on the type is needed to get the typing premise of
  \nameref{rule:cic-conv-unty}.
  As for Rules \nameref{rule:bd-pinf-univ} and \nameref{rule:bd-pinf-prod},
  that typing premise is obtained by combining the induction hypothesis,
  validity and \kl{subject reduction}.

  Alternatively, the appeal to validity could be removed by
  strengthening the theorem to incorporate the well-formation of outputs on top of that of
  the subject. Here we follow the proofs in \kl{MetaCoq}, which establishes
  meta-theoretical properties of the undirected system first – including validity –,
  so we can exploit these.
\end{proof}
\end{minipage}

\subsection{Completeness}

Contrarily to correctness, which keeps the structure of a derivation,
completeness is of a different nature.
Because in bidirectional derivations the computation rules are much less liberal than in
undirected ones, the structure of derivations must be altered.
The crux of the proof is thus to ensure that all uses of \ruleref{rule:cic-conv}
can be permuted down through the other rules,
in order to concentrate them in the places where they are authorized in the bidirectional
derivation.
In a way, composing completeness with correctness gives a kind of normalization procedure
on undirected derivations, which produces a canonical one by pushing conversion
down as much as possible.

The proof mainly relies on the following lemma,
which can be seen as a strong form of \kl{injectivity of type constructors} –
the version of \cref{prop:prod-inj} is a direct consequence.

\begin{lemma}[Conversion implies reduction for type constructors]
  \label{lem:conv-red-tycons}
  If $T \conv \uni[i]$, then $T \red \uni[i]$.

  If $T \conv \P x : A.\ B$, then there exist $A'$ and $B'$ such that:
  \begin{itemize}
    \item $T \red \P x : A'.\ B'$
    \item $A' \conv A$
    \item $B' \conv B$
  \end{itemize}
\end{lemma}

\begin{minipage}{\textwidth}
\begin{proof}
  Let us spell out the proof on Π-types – the case of $\uni$ is similar, but easier.

  For \kl{algorithmic conversion}, by definition there must exist $T'$ and $T''$
  such that $T \red T'$, $\P x : A.\ B \red T''$, $T' \alpheq T''$.
  But there can be no \kl(red){top-level} reduction step in $\P x : A.\ B \red T''$,
  so actually $T''$ is some $\P x : A''.\ B''$ and $A \red A''$, $B \red B''$.
  Similarly, $T'$ must be some $\P x : A'.\ B'$
  such that $A' \alpheq A''$ and $B' \alpheq B''$.
  Combining these, we obtain that $A' \conv A$ and $B' \conv B$, as expected.

  For \kl{declarative conversion}, we can go through the
  equivalence with algorithmic conversion – and thus use \kl{confluence} under
  the hood.
\end{proof}
\end{minipage}

\begin{theorem}[\intro{Completeness} of bidirectional typing for \kl{CCω}]
  \label{thm:compl-ccomega}
  If $\Gamma \vdash t \ty T$, then there exists $T'$ such that $\inferty{\Gamma}{t}{T'}$
  and $T' \conv T$.
\end{theorem}

\begin{proof}
  By induction on the undirected typing derivation.

  Rules \nameref{rule:cic-var} and \nameref{rule:cic-univ} are base cases,
  and can be simply replaced by the corresponding bidirectional rules.
  In the case of \ruleref{rule:cic-conv}, the property is a direct consequence of the induction hypothesis, together with transitivity of conversion:
  we simply conflate two conversions together.

  As for \ruleref{rule:cic-prod}, the induction hypothesis on the domain $A$
  gives the existence of $T_A$
  such that $\inferty{\Gamma}{A}{T_A}$ and $T_A \conv \uni[i]$. Using
  \cref{lem:conv-red-tycons}, we can derive $\pinferty{\uni}{\Gamma}{A}{\uni[i]}$.
  Applying a similar reasoning on the codomain and combining both is enough to conclude.

  In \ruleref{rule:cic-abs}, we do the same reasoning again on the type annotation.
  Combined with the induction hypothesis on the body $t$,
  we get $\inferty{\Gamma}{\l x : A.\ t}{\P x : A .\ B'}$ for some $B'$ such that $B \conv B'$, and thus $\P x : A . B \conv \P x : A . B'$ as desired.

  We are finally left with \ruleref{rule:cic-app}.
  Again, the key is \cref{lem:conv-red-tycons}, which can be combined with the induction
  hypothesis on the function $f$ to get $\pinferty{\P}{\Gamma}{f}{\P x : A' .\ B'}$
  for some $A'$ and $B'$ such that $A \conv A'$ and $B \conv B'$,
  where $\P x : A. B$ is the type of $f$ in the undirected derivation.
  The induction hypothesis on the argument $u$ gives
  $\inferty{\Gamma}{u}{A''}$ with $A'' \conv A$. Thus, by transitivity of conversion
  $\checkty{\Gamma}{u}{A'}$, and we can apply \ruleref{rule:bd-app} to conclude.
\end{proof}

Interestingly, the proof of correctness relies on \kl{subject reduction}, which itself
needs \kl{injectivity of type constructors} and transitivity of conversion.
Similarly, completeness relies both on the injectivity as given by \cref{lem:conv-red-tycons},
and transitivity of conversion. Be it for algorithmic or declarative conversion, one at
least of those is not directly provable – we need \kl{confluence}.
We already hit this same tension between injectivity and transitivity
with \kl{subject reduction}, and
must draw the same conclusion: there is no free lunch!

\Cref{thm:compl-ccomega} is quite specific to our \kl{Church-style} design.
Instead, an important portion of the research on bidirectional typing in the context
of dependent types adopts a \kl{Curry-style} approach.
This is the case of \eg \sidetextcite{Coquand1996}, the type system of \kl{Agda} as described
by \sidetextcite{Norell2007}, and most of the work by Abel
\sidecite{Abel2007,Abel2008,Abel2011,Abel2017},
and McBride \sidecite{McBride2016,McBride2018,McBride2019}.
In such systems, λ-abstractions can only be checked against a given type, but cannot infer one,
which implies that only terms with no redexes are typeable.
\textcite{Norell2007} argues that explicit
redexes are uncommon in real-life programs, so that being unable to type them is not a strong
limitation in practice. Another solution, taken by \textcite{McBride2022}, is to add
type annotations in order regain the ability to check non-normal terms,
at the cost of inserting annotations at the right place.
In all cases, however, the fact that all terms well-typed in the declarative system infer
a type is irremediably lost. Weaker forms of completeness should still hold for such systems,
typically one where all terms check against their type, but are not ensured to infer.
See for instance \sidetextcite[][Theorem~7.3]{Gratzer2019} for one restricted to normal forms
– and thus not taking the role of annotations into account.

\begin{marginfigure}
  \begin{mathpar}
    \inferrule
    {T \hred \P x : A' . B \\ \pinferty{\uni}{\Gamma}{A}{\uni[i]} \\
    A \conv A' \\ \checkty{\Gamma, x : A}{t}{B}}
    {\checkty{\Gamma}{\l x : A.\ t}{T}}
  \end{mathpar}
\end{marginfigure}

In a setting with \kl{Church-style} abstraction,
if one wishes to give the possibility for seemingly untyped abstraction,
another mechanism has to be resorted to, typically elaboration using meta-variables.
This is described in \eg \textcite{Asperti2012},
which combines a rule similar to \ruleref{rule:bd-abs}
– where the type of an abstraction is inferred – with another one,
similar to the \kl{Curry-style} one – where abstraction is checked, see opposite.
While such a rule would make a system as that we have just described “over-complete”,
it is a useful addition to enable the propagation of checking information
upwards in the derivation, which is crucial in elaboration phases, even in \kl{Church-style}.

\subsection{Uniqueness}

All the bidirectional judgements of \cref{fig:ccw-bidir-infer} are syntax-directed,
in the sense that there is always at most one rule that applies to derive a certain typing judgement, given a fixed \kl{subject}.
But there is still some indeterminacy.
Indeed, in rules involving reduction no strategy is fixed, thus two different reducts can be used with the same rule, resulting in different inferred types.
However, inferred types are still related:

\begin{theorem}[Uniqueness of inferred type up to joinability]
  \label{thm:unique-inf}
  If $\Gamma$ is well-formed,
  $\inferty{\Gamma}{t}{T}$ and $\inferty{\Gamma}{t}{T'}$ then $T$ and $T'$ both reduce to a
  common $T''$, \eg $T \red T''$ and $T \red T''$.
  In particular, $T \conv T'$.
\end{theorem}

\begin{proof}
  By mutual induction on the first derivation, together with the same property for
  constrained inference.

  The main idea is to use \kl{confluence} to relate different reduction paths in Rules
  \nameref{rule:bd-pinf-prod} and \nameref{rule:bd-pinf-univ}. For the other rules,
  the conclusion is direct from the induction hypotheses.
\end{proof}

Combining this with \kl(bidir){correctness} and \kl(bidir){completeness},
we get \kl{uniqueness of types} for the undirected system.

\begin{theorem}[Uniqueness of types]
  \label{thm:unique-undir}
  If $\Gamma \vdash t \ty T$ and $\Gamma \vdash t \ty S$ then $T \conv S$.
\end{theorem}

\begin{minipage}{\textwidth}
\begin{proof}
  Since $\Gamma \vdash t \ty T$, by correctness
  there exists some $T'$ such that $\inferty{\Gamma}{t}{T'}$ and moreover $T' \conv T$.
  Similarly, there exists some $S'$ such that $\inferty{\Gamma}{t}{S'}$
  and moreover $S' \conv S$.
  But by uniqueness $T' \conv S'$, and thus $T \conv S$.
\end{proof}
\end{minipage}

\begin{marginfigure}
\begin{mathpar}
  \inferdef{UnivWhInf}
    {\inferty{\Gamma}{t}{T} \\ T \hred \uni[i]}
    {\pinferty{\uni}{\Gamma}{t}{\uni[i]}}
    \label{rule:bd-pinf-wh-univ} \and
  \inferdef{ΠWhInf}
    {\inferty{\Gamma}{t}{T} \\ T \hred \P x : A. B}
    {\pinferty{\P}{\Gamma}{t}{\P x : A . B}}
    \label{rule:bd-pinf-wh-prod}
\end{mathpar}
\caption{Constrained inference with a weak-head strategy}
\label{fig:wh-pinf}
\end{marginfigure}

In order to completely eliminate indeterminacy, a reduction strategy can be fixed.
This amounts to replacing \kl{full reduction} with \kl{weak-head reduction},
\eg to replace the two reduction rules in \cref{fig:bidir-ccw-other} by those of
\cref{fig:wh-pinf}.
This is still \kl(bidir){correct} and \kl(bidir){complete}.
Correctness follows exactly the same proof as
\cref{thm:corr-ccomega}. As for completeness, the main point is to show an analogous
to \cref{lem:conv-red-tycons} for weak-head reduction.

\begin{theorem}[Reduction strategy]
  \label{thm:red-strat}

  If Rules \nameref{rule:bd-pinf-univ} and \nameref{rule:bd-pinf-prod} are replaced by
  \nameref{rule:bd-pinf-wh-univ} and \nameref{rule:bd-pinf-wh-prod},
  then given a well-formed context $\Gamma$ and a term $t$ there is at most one $T$
  such that $\inferty{\Gamma}{t}{T}$, and at most one $T'$ such that
  $\pinferty{h}{\Gamma}{t}{T'}$.
\end{theorem}

\begin{proof}
  Once again, by mutual induction.

  For inference, given a fixed term $t$ there is always at most one rule which applies to
  derive $\inferty{\Gamma}{t}{T}$, since there is exactly one rule per term former.
  Combining this with the uniqueness of types inferred in the premises by induction
  hypothesis is enough to conclude.

  For the constrained inference judgement, once again there is only one rule that applies.
  Since weak-head reduction is deterministic – given $T$, there is at most one $T'$ such that
  $T \hored T'$ –, there is at most one weak-head normal form $\uni$ or $\P x : A.\ B$ for
  a type. Hence, the type obtained by constrained inference is unique.
\end{proof}

\subsection{Strengthening}

Reasoning on the bidirectional derivation makes proofs easier,
while \kl(bidir){correctness} and \kl(bidir){completeness} ensure the results
can be carried to the undirected system.
One way to understand this is that the canonical derivation obtained by combining
correctness and completeness is more structured, and thus more amenable to proofs.

An example of this is the \kl{strengthening} property, a consequence of
\kl{conditional stability under renaming}. We explained in \cref{sec:tech-properties}
why proving these in the undirected system is not straightforward: the issue is that
computation is too unconstrained, so that derivations might make use of needless variables.
Bidirectional typing, however, does not have this defect, since no type is ever "invented".
Rather, they are obtained either by reduction of previously inferred types, or as inputs.
This means that types in a bidirectional derivation never mention useless variables, and thus
that the following holds.

\begin{minipage}{\textwidth}
\begin{theorem}[Conditional stability under renaming – bidirectional]
  \label{thm:strong-stab-renaming-bidir}
  Whenever we have
  \begin{itemize}
    \item $\inferty{x_1 : A_1 \dots x_n : A_n}{t}{T}$
    \item for all $i$ such that $x_i$ appears in $t$,
      there is a variable $y_i$ such that $(y_i : \multisubs{A_i}{x_1 \into y_1 \dots x_n \into y_n}) \in \Delta$
  \end{itemize}
  it also holds that $\inferty{\Delta}{\multisubs{t}{x_1 \into y_1 \dots x_n \into y_n}}{\multisubs{T}{x_1 \into y_1 \dots x_n \into y_n}}$.
\end{theorem}
\end{minipage}

\begin{proof}
  By a direct induction on the typing derivation.

  Note that we do not even need $\Delta$ to be well-formed.
\end{proof}

And as a special case, \kl{strengthening} follows.

\begin{theorem}[Strengthening – bidirectional]
  \label{thm:strengthening-bidir}
  Whenever $\inferty{\Gamma, x : A}{t}{T}$ and $x$ does not appear in $t$,
  $\inferty{\Gamma}{t}{T}$ is derivable.
\end{theorem}

From those, \kl{conditional stability under renaming} and \kl{strengthening} for
the undirected system can be obtained without any difficulty.


\section{An information theoretic approach}

% The problem with increasing complexity
% - Training and test loss as a function of complexity - use trees
% We seek to minimize generalization error
% Most common strategy: cross validation on different input settings
% Goal: Estimate generalization error analytically, by adjusting training error
% Specifically: Estimate generalization error for trees analytically
% Quite generally: 2cov(y_i pred_i)
% So the goal is to estimate cov
% For likelihoods: Akaike - explain Taylor approx?
% Is generalized for differentiable (in parameters) loss functions NIC (Murata)
% Does not apply directly to trees, due to the non-standard binary splitting procedure
% However, conditioned on topology. Only leaf-parameters are learned (satisfies differentiability etc.) - local optimism as NIC
% Big result -

\begin{frame}{Revisit the supervised learning problem}
	The goal is to find $f$ that minimises \textit{generalization error}:
	\begin{align*}
	\hat{f} = \arg\min_f E_{\hat{\theta},\features^0\response^0}\left[l(\response^0,f(\features^0;\hat{\theta}))\right]
	\end{align*}
	where $(\features^0\response^0)$ are unseen in the training-phase, and therefore independent of $\hat{\theta}$ trained from $(\features,\response)$.

	\begin{columns}[T]
		\begin{column}{0.35\textwidth}
		\includegraphics<2->[height=4.5cm,width=4.5cm]{figures/loss_vs_complexity.pdf}
		\end{column}
		\begin{column}{0.6\textwidth}
			\visible<3->{
			\begin{myblock}{}{bg=yellow!05,fg=black}{bg=yellow!20, fg=black}
				\begin{itemize}
					\item<1-> Optimism of the training loss: \small$$C(\hat{\theta}) = E\left[l(\response^0,f(\features^0;\hat{\theta})) - l(\response,f(\features;\hat{\theta}))\right]$$
					\item<1-> Often \small$C(\hat{\theta})\approx \frac{2}{n}\sum_{i=1}^n\Cov(y_i,\pred_i)$
					\item<1-> Useful to talk about \textit{asymptotic loss} \small$$E\left[l(\response,f(\features;\theta_0))\right],~\lim_{n\to\infty}\hat{\theta}\stackrel{P}{\to}\theta_0$$
				\end{itemize}
			\end{myblock}}
		\end{column}
	\end{columns}

%	The goal is to find $f$ that minimizes generalization error:
%
%	where XXX is data unseen in the training-phase.
%
%	Training and test error on as a function of complexity
%
%		Optimism definition
%
%	Then add asymptotic one
%
%	The general result with cov

\end{frame}

\begin{frame}{But the ensemble complexity is unknown...}

	\begin{myblock}{The main idea:}{bg=green!05,fg=black}{bg=green!20, fg=black}
		\begin{itemize}
			\item<1-> Estimate $C(\hat{\theta})$ for trees analytically!
		\end{itemize}
	\end{myblock}

	\begin{myblock}{And hope that we may...}{bg=yellow!05,fg=black}{bg=yellow!20, fg=black}
		\begin{enumerate}
			\item<2-> Adaptively control the complexity of each tree
			\item<3-> Automatically stop the boosting procedure
			%\item<4-> In a highly efficient manner
		\end{enumerate}
	\end{myblock}
	\begin{columns}[T]
	\begin{column}{0.35\textwidth}
		% tree loss vs depth
		\includegraphics<2->[height=4cm,width=4.5cm]{figures/loss_vs_treedepth1.pdf}
	\end{column}
	\begin{column}{0.35\textwidth}
		% no split at all
		\includegraphics<3>[height=4cm,width=4.5cm]{figures/loss_vs_treedepth2.pdf}
	\end{column}
	\end{columns}

\end{frame}

\begin{frame}{Information criteria: Akaike and beyond...}
	\begin{myblock}{The poor researcher has no processing power...}{bg=blue!5,fg=black}{bg=blue!10, fg=black}
		\begin{itemize}
			\item<1-> An analytic, not a data-driven approach, is needed!
		\end{itemize}
	\end{myblock}
	\visible<2->{\begin{myblock}{A brief history of (some) information criteria}{bg=yellow!05,fg=black}{bg=yellow!20, fg=black}
		\begin{itemize}
			\item<2-> \cite{akaike1974new} AIC: $C=p$ for NLL. Assumptions on true model
			\item<2-> \cite{takeuchi1976distribution} TIC: $C = \tr(QH^{-1})$ also for NLL, but no assumption on the true model
			\item<2-> \cite{murata1994network} NIC: $C = \tr(QH^{-1})$ also for differentiable loss
		\end{itemize}
		\begin{align*}
	H&=\E \left[ \nabla_{\mathbf{\theta}_0}^2 \loss(\response,f(\features;\mathbf{\theta}_0)) \right]\\
	Q &= \E\left[ \left( \nabla_{\theta_0}\loss(\response,f(\features;\mathbf{\theta}_0)) \right)\left( \nabla_{\theta_0}\loss(\response,f(\features;\mathbf{\theta}_0)) \right)^\intercal  \right]
	\end{align*}
	\end{myblock}
}
	%Yellow block local optimism conditioning?
\end{frame}

\begin{frame}{The local optimism}

				\begin{myblock}{But can we use this for trees?}{bg=blue!5,fg=black}{bg=blue!10, fg=black}
				\begin{itemize}
					\item<2-> Conditionally on known tree-topology / regions $R$
					\item<2-> We define the local optimism for a leaf node $t$
					\begin{align*}
						\mathcal{C}(t|q) &=
						\E_{y,\hat{w}_t} \left[ \frac{\partial^2}{\partial \hat{w}_t^2}\tilde l(\response,\hat{w}_t)\right]
						\Var_{\hat{w}_t}[\hat{w}_{t}]
					\end{align*}
					$q$ is the tree-topology, $\hat{w}_t$ is the prediction in region $t$
				\end{itemize}
			\end{myblock}

		\visible<3->{
	\begin{myblock}{Correspondingly for internal nodes...}{bg=yellow!05,fg=black}{bg=yellow!20, fg=black}
		\begin{itemize}
			\item<3-> At some stage in the tree building process every node will have been a leaf node.
			\item<3-> Define this to be the local optimism $\mathcal{C}(t|q)$ for the internal node $t$ in the fully grown tree.
		\end{itemize}
	\end{myblock}
	}

\end{frame}

\begin{frame}{The local optimism: Illustration}
	\centering\resizebox{7cm}{4cm}{
		\begin{tikzpicture}
\tikzstyle{custbox} = [circle, align=left, minimum width=0.5cm, minimum height=0.5cm,text centered, draw=black, fill=gray!10]

\node (r) at (0,0) [custbox]{1};
\node[right=0.2cm of r] (optimismr) {$\mathcal{C}(1|q_1)$};

\node (rl) at (-1.5,-1.5) [custbox]{2};
\node[left=0.2cm of rl] (optimismrl) {$\mathcal{C}(2|q_2)$};

\node (rll) at (-2.5,-3) [custbox]{4}; \node[below left=0.2cm and 0cm of rll] (opt4) {$\mathcal{C}(4|q_4)$};
\node (rlr) at (-0.5,-3) [custbox]{5}; \node[below left=0.2cm and 0cm of rlr] (opt5) {$\mathcal{C}(5|q_5)$};

\node (rr) at (1.5,-1.5) [custbox]{3};
\node[right=0.2cm of rr] (optimismrr) {$\mathcal{C}(3|q_3)$};

\node (rrl) at (0.5,-3) [custbox]{6};
\node[below right=1cm and 1cm of rrl] (optimismrrl) {$\mathcal{C}(6|q_6)$};

\node (rrll) at (0,-4.5) [custbox]{8}; \node[below left=0.2cm and 0cm of rrll] (opt8) {$\mathcal{C}(8|q_8)$};
\node (rrlr) at (1,-4.5) [custbox]{9}; \node[below right=0.2cm and 0cm of rrlr] (opt9) {$\mathcal{C}(9|q_9)$};
\node (rrr) at (2.5,-3) [custbox]{7}; \node[below right=0.2cm and 0cm of rrr] (opt7) {$\mathcal{C}(7|q_7)$};

%Arrows
\draw[->,black] (r) -- (rl);
\draw[->,black] (r) -- (rr);
\draw[->,black] (rl) -- (rll);
\draw[->,black] (rl) -- (rlr);
\draw[->,black] (rr) -- (rrl);
\draw[->,black] (rr) -- (rrr);
\draw[->,black] (rrl) -- (rrll);
\draw[->,black] (rrl) -- (rrlr);
%optimism
\draw[-,orange] (r) -- (optimismr);
\draw[-,orange] (rr) -- (optimismrr);
\draw[-,orange] (rrl) -- (optimismrrl);
\draw[-,orange] (rl) -- (optimismrl);
\draw[-,orange] (rll) -- (opt4);
\draw[-,orange] (rlr) -- (opt5);
\draw[-,orange] (rrr) -- (opt7);
\draw[-,orange] (rrll) -- (opt8);
\draw[-,orange] (rrlr) -- (opt9);

\end{tikzpicture}
}

The optimism induced from fitting the predictions $\mathbf{w}$ in the leaf-nodes conditioned on the final topology is given as
\begin{align*}
	\hat{C}(\hat{\mathbf{w}}|q) = \sum_{t\in\{4,5,8,9,7\} } \mathcal{C}(t|q)\pi_t,~\pi_t=P(q(\mathbf{x})=t)
\end{align*}

\end{frame}

\begin{frame}{The tree-learning procedure learns the future map $q$}
	\begin{myblock}{The steps - first consider only one feature}{bg=blue!5,fg=black}{bg=blue!10, fg=black}
	\begin{itemize}
		\item<1-> Relate the distance between the training and asymptotic loss to a gamma distribution
		\item<1-> Fit the gamma using knowledge of its shape and expectation
	\end{itemize}
	\end{myblock}
\visible<2->{
	\begin{myblock}{The steps - consider $m\geq 1$ features}{bg=yellow!05,fg=black}{bg=yellow!20, fg=black}
		\begin{itemize}
			\item<2-> Under $H_0$: no feature is relevant
			\item<2-> Distance between training and asymptotic loss as expected maximum of multiple realizations of the gamma RV
		\end{itemize}
	\end{myblock}
}
\begin{itemize}
	\item<2-> Warning: several approximations!
\end{itemize}
\end{frame}

\begin{frame}{Gives the following result:}
	\begin{align*}
	\hat{C}_m(\hat{\mathbf{w}}, q)
	=
	2 \sum_{t\not\in \mathcal{L}} \E \left[
	Q\left(
	d(t), \frac{ \mathcal{C}(L(t)|q)\pi_{L(t)} + \mathcal{C}(R(t)|q)\pi_{R(t)} }{2}, m
	\right)
	\right]
	\end{align*}
	\begin{itemize}
		\item $\mathcal{L}$ is the set of leaf nodes
		\item $Q(\alpha,\beta,m)$ is the maximum of $m$ gamma random variables with shape $\alpha$ and scale $\beta$
		\item $L(t)$ and $R(t)$ returns the index of left and right child-nodes respectively
		\item 	\small\begin{align*}
		d(t) =
		\begin{cases}
		3 & \text{if } L(t) \in \setleafnodes \text{ and } R(t) \in \setleafnodes\\
		\frac{5}{2} & \text{if } L(t) \in \setleafnodes \text{ and } R(t) \not\in \setleafnodes\\
		\frac{5}{2} & \text{if } L(t) \not\in \setleafnodes \text{ and } R(t) \in \setleafnodes\\
		2 & \text{if } L(t) \not\in \setleafnodes \text{ and } R(t) \not\in \setleafnodes.
		\end{cases}
		\end{align*}
	\end{itemize}
\end{frame}

\begin{frame}{Visualization of the result}
	\centering\resizebox{11cm}{4.5cm}{
	\begin{tikzpicture}
	\tikzstyle{custbox} = [circle, align=left, minimum width=0.5cm, minimum height=0.5cm,text centered, draw=black, fill=gray!10]

	\node (r) at (0,0) [custbox]{1};
	\node[right=0.2cm of r] (optimismr) {$2E\left[Q\left(2,\frac{\mathcal{C}(2|q)\pi_2+\mathcal{C}(3|q)\pi_3}{2}, m\right)\right]$};

	\node (rl) at (-1.5,-1.5) [custbox]{2};
	\node[left=0.2cm of rl] (optimismrl) {$2E\left[Q\left(3,\frac{\mathcal{C}(4|q)\pi_4+\mathcal{C}(5|q)\pi_5}{2}, m\right)\right]$};

	\node (rll) at (-2.5,-3) [custbox]{4}; %\node[below=0.2cm of rll] {$\hat{w}_1$};
	\node (rlr) at (-0.5,-3) [custbox]{5}; %\node[below=0.2cm of rlr] {$\hat{w}_2$};

	\node (rr) at (1.5,-1.5) [custbox]{3};
	\node[right=0.2cm of rr] (optimismrr) {$2E\left[Q\left(\frac{5}{2},\frac{\mathcal{C}(6|q)\pi_6+\mathcal{C}(7|q)\pi_7}{2}, m\right)\right]$};

	\node (rrl) at (0.5,-3) [custbox]{6};
	\node[below right=1cm and 1cm of rrl] (optimismrrl) {$2E\left[Q\left(3,\frac{\mathcal{C}(8|q)\pi_8+\mathcal{C}(9|q)\pi_9}{2}, m\right)\right]$};

	\node (rrll) at (0,-4.5) [custbox]{8};% \node[below=0.2cm of rrll] {$\hat{w}_3$};
	\node (rrlr) at (1,-4.5) [custbox]{9};% \node[below=0.2cm of rrlr] {$\hat{w}_4$};
	\node (rrr) at (2.5,-3) [custbox]{7}; %\node[below=0.2cm of rrr] {$\hat{w}_5$};

	%Arrows
	\draw[->,black] (r) -- (rl);
	\draw[->,black] (r) -- (rr);
	\draw[->,black] (rl) -- (rll);
	\draw[->,black] (rl) -- (rlr);
	\draw[->,black] (rr) -- (rrl);
	\draw[->,black] (rr) -- (rrr);
	\draw[->,black] (rrl) -- (rrll);
	\draw[->,black] (rrl) -- (rrlr);
	%optimism
	\draw[-,orange] (r) -- (optimismr);
	\draw[-,orange] (rr) -- (optimismrr);
	\draw[-,orange] (rrl) -- (optimismrrl);
	\draw[-,orange] (rl) -- (optimismrl);

	\end{tikzpicture}
}
	\begin{itemize}
	\item Sum up the node-contributions to obtain the optimism estimate
	\end{itemize}
\end{frame}

\begin{frame}{Estimation}
	Quantities must admit evaluation and efficient computation:
	\begin{itemize}
		\item<2-> Conditioned on $q$, $\hat{\mathbf{w}}$ are M-estimators.
		\item<2-> 	\begin{align*}
		\lim_{n\to\infty}
		\mathcal{C}(t|q)\pi_t
		&=
		\E_{y,\hat{w}_t} \left[ \frac{\partial^2}{\partial \hat{w}_t^2}l(\response,\hat{w}_t)\right]
		\Var_{\hat{w}_t}[\hat{w}_{t}]\pi_t\\
		%,~t\in\setleafnodes,~q(\features)=t\\
		&=
		\frac{\sum_{i\in I_t} \left(g_i+h_i\hat{w}_t\right)^2}{n\sum_{i\in I_t}h_i},
		\text{ $I_t$ is indexes of obs in leaf $t$}
		\end{align*}
		\item<3-> Assume features independent, then $E[Q]$ is the expected $m-$th order statistic
		\item<3-> Estimate asymptotically using the gamma-quantile function: $$E[Q]\sim z\left(\frac{m}{m+1}\right)$$
	\end{itemize}
\end{frame}

\begin{frame}{Some sanity checks: Simulation experiments}

	100 Datasets and 100 trees, trained on 1 and 100 features.
		\begin{figure}[h]
		\centering
		\begin{subfigure}{.43\textwidth}
			\centering
			\includegraphics[scale=0.4]{figures/loss_vs_depth_one_feature.pdf}
			\caption[]{One (relevant) feature}
			\label{fig: loss vs tree depth one feature}
		\end{subfigure}
		\begin{subfigure}{.53\textwidth}
			\centering
			\includegraphics[scale=0.4]{figures/loss_vs_depth_dim_correct.pdf}
			\caption[]{100 features of which 99 are noise}
			\label{fig: loss vs tree depth multiple feature}
		\end{subfigure}
	\caption{Average $\pm 2 SD$ for training (black) and test (orange) MSE loss, together with estimated optimism (blue: 1 feature, green: 100 features)}
		%\caption{The average $\pm 2 SD$ for training (black) and test (orange) MSE loss, and estimated optimism, after training on 100 simulated datasets, each containing 10000 realizations from a linear model $y\sim N(x,1)$ where $x\sim U(0,2)$. In Figure (a), training is done on only one feature (the relevant $x$), and the quantity \eqref{eq:expected tree optimism one feature} (blue) is the optimism added to the training loss. Figure (b) results from the same data and training, but with 99 added Gaussian noise features. Resulting is that the optimism from Figure (a) is pushed downwards, and in need of the correction given in \eqref{eq:optimism full m} (green). }
	\end{figure}
	\begin{itemize}
		\item<2-> Not crazy!
	\end{itemize}
\end{frame}


\documentclass[color=usenames,dvipsnames]{beamer}\usepackage[]{graphicx}\usepackage[]{color}
% maxwidth is the original width if it is less than linewidth
% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0, 0, 0}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.69,0.494,0}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.749,0.012,0.012}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.514,0.506,0.514}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0,0.341,0.682}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0,0,0}{\textbf{#1}}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.004,0.004,0.506}{#1}}%
\let\hlipl\hlkwb

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}
%\documentclass[color=usenames,dvipsnames,handout]{beamer}

%\usepackage[roman]{../../lab1}
\usepackage[sans]{../../lab1}

\hypersetup{pdftex,pdfstartview=FitV}

%% New command for inline code that isn't to be evaluated
\definecolor{inlinecolor}{rgb}{0.878, 0.918, 0.933}
\newcommand{\inr}[1]{\colorbox{inlinecolor}{\texttt{#1}}}

\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}

%\setlength\fboxsep{0pt}

\begin{frame}[plain]
  \huge
  \centering \par
  {\color{RoyalBlue}{Lab 11 -- ANCOVA}} \\
  \vspace{1cm}
  \Large
%  October 29 \& 30, 2018 \\
  FANR 6750 \\
  \vfill
  \large
  Richard Chandler and Bob Cooper
\end{frame}

\section{Regression}

\begin{frame}
  \frametitle{ANCOVA overview}
  {%\bf
    Scenario}
  \begin{itemize}
    \item We are interested in doing a one-way ANOVA
    \item However, we need to account for variation associated with a
      continuous predictor variable
  \end{itemize}
  \pause
  \vfill
  {%\bf
    Additive model}
  \[
    y_{ij} = \mu + \alpha_i + \beta(x_{ij} - \bar{x}) + \varepsilon_{ij}
  \]
  \pause
  \vfill
%  \centering
%  \bf
  ANCOVA can be thought of as a hybrid between ANOVA and regression \\
  \pause
  \vfill
%  \centering
%  \bf
  ANOVA, regression, and ANCOVA are linear models \\
\end{frame}

\begin{frame}[fragile]
  \frametitle{The Diet Data}
%  \small
  Import the data and view the levels of the factor
\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{dietData} \hlkwb{<-} \hlkwd{read.csv}\hlstd{(}\hlstr{"dietData.csv"}\hlstd{)}
\hlkwd{levels}\hlstd{(dietData}\hlopt{$}\hlstd{diet)}
\end{alltt}
\begin{verbatim}
## [1] "Control" "High"    "Low"     "Med"
\end{verbatim}
\end{kframe}
\end{knitrout}
\pause
\vspace{0.5cm}
{\large Reorder the levels of the factor, just for convenience}
\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{levels}\hlstd{(dietData}\hlopt{$}\hlstd{diet)} \hlkwb{<-} \hlkwd{list}\hlstd{(}\hlkwc{Control}\hlstd{=}\hlstr{"Control"}\hlstd{,} \hlkwc{Low}\hlstd{=}\hlstr{"Low"}\hlstd{,}
                              \hlkwc{Med}\hlstd{=}\hlstr{"Med"}\hlstd{,} \hlkwc{High}\hlstd{=}\hlstr{"High"}\hlstd{)}
\hlkwd{levels}\hlstd{(dietData}\hlopt{$}\hlstd{diet)}
\end{alltt}
\begin{verbatim}
## [1] "Control" "Low"     "Med"     "High"
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{The diet data}
%<<scatter,echo=FALSE,fig.show="hide">>=
%plot(weight ~ age, dietData)
%@
%<<bp,echo=FALSE,fig.show=FALSE>>=
%boxplot(weight ~ diet, dietData, ylab="Weight")
%@
  \begin{columns}
    \begin{column}{0.5\textwidth}
      \tiny %\scriptsize
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot}\hlstd{(weight} \hlopt{~} \hlstd{age, dietData)}
\end{alltt}
\end{kframe}
\end{knitrout}
    \includegraphics[width=\textwidth]{figure/plot-wt-1}
    \end{column}
    \begin{column}{0.5\textwidth}
      \tiny %\scriptsize
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{boxplot}\hlstd{(weight} \hlopt{~} \hlstd{diet, dietData,} \hlkwc{ylab}\hlstd{=}\hlstr{"Weight"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
    \includegraphics[width=\textwidth]{figure/bp-1}
    \end{column}
  \end{columns}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Simple linear regression using {\tt lm}}
%  \scriptsize

\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{fm1} \hlkwb{<-} \hlkwd{lm}\hlstd{(weight} \hlopt{~} \hlstd{age, dietData)}
\hlkwd{summary}\hlstd{(fm1)}
\end{alltt}
\begin{verbatim}
##
## Call:
## lm(formula = weight ~ age, data = dietData)
##
## Residuals:
##     Min      1Q  Median      3Q     Max
## -5.6906 -1.2625  0.0522  1.0233  6.1680
##
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept) 21.32523    0.80685  26.430  < 2e-16 ***
## age          0.51807    0.06742   7.685 2.07e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.171 on 58 degrees of freedom
## Multiple R-squared:  0.5045,	Adjusted R-squared:  0.496
## F-statistic: 59.05 on 1 and 58 DF,  p-value: 2.072e-10
\end{verbatim}
\end{kframe}
\end{knitrout}
\pause
\footnotesize
{The two estimates correspond to the intercept and slope parameters}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Regression line and confidence interval}
  {%\bf %\footnotesize
    Regression lines and CIs can be created using
    \inr{predict} \\}
  \begin{enumerate}[\bf (1)]
    \item Create a new {\tt data.frame} containing a sequence of
        values of the predictor variable {\it age}
    \item Predict {\it weight} using these values of {\it age}
    \item Put predictions and data together for easy plotting
  \end{enumerate}
  \pause
  \vfill
  \small
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{age} \hlkwb{<-} \hlstd{dietData}\hlopt{$}\hlstd{age}
\hlstd{predData1} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}\hlkwc{age}\hlstd{=}\hlkwd{seq}\hlstd{(}\hlkwd{min}\hlstd{(age),} \hlkwd{max}\hlstd{(age),} \hlkwc{length}\hlstd{=}\hlnum{50}\hlstd{))}
\hlstd{pred1} \hlkwb{<-} \hlkwd{predict}\hlstd{(fm1,} \hlkwc{newdata}\hlstd{=predData1,} \hlkwc{se.fit}\hlstd{=}\hlnum{TRUE}\hlstd{,}
              \hlkwc{interval}\hlstd{=}\hlstr{"confidence"}\hlstd{)}
\hlstd{predictions1} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(pred1}\hlopt{$}\hlstd{fit, predData1)}
\end{alltt}
\end{kframe}
\end{knitrout}
\pause
\vfill
% {\bf Plot raw data and the regression results}
There's nothing special about \inr{length=50}, but in general, the longer the length of the sequence, the smoother the lines will look.
\end{frame}

\begin{frame}[fragile]
  \frametitle{Regression line and confidence interval}
  \scriptsize
\begin{knitrout}\scriptsize
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot}\hlstd{(weight} \hlopt{~} \hlstd{age,} \hlkwc{data}\hlstd{=dietData)}                      \hlcom{# raw data}
\hlkwd{lines}\hlstd{(fit} \hlopt{~} \hlstd{age,} \hlkwc{data}\hlstd{=predictions1,} \hlkwc{lwd}\hlstd{=}\hlnum{2}\hlstd{)}             \hlcom{# fitted line}
\hlkwd{lines}\hlstd{(lwr} \hlopt{~} \hlstd{age,} \hlkwc{data}\hlstd{=predictions1,} \hlkwc{lwd}\hlstd{=}\hlnum{2}\hlstd{,} \hlkwc{col}\hlstd{=}\hlstr{"gray"}\hlstd{)} \hlcom{# lower CI}
\hlkwd{lines}\hlstd{(upr} \hlopt{~} \hlstd{age,} \hlkwc{data}\hlstd{=predictions1,} \hlkwc{lwd}\hlstd{=}\hlnum{2}\hlstd{,} \hlkwc{col}\hlstd{=}\hlstr{"gray"}\hlstd{)} \hlcom{# upper CI}
\end{alltt}
\end{kframe}
\end{knitrout}
  \vspace{-9mm}
  \begin{center}
    \includegraphics[width=0.65\textwidth]{figure/plot-fm1-1}
  \end{center}
\end{frame}

\section{One-way ANOVA}

\begin{frame}[fragile]
  \frametitle{One-way ANOVA using {\tt lm}}
{%\bf
  Change the \inr{contrasts} option so that the estimates will
  correspond to the additive model, and then fit the ANOVA
}
\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{options}\hlstd{(}\hlkwc{contrasts}\hlstd{=}\hlkwd{c}\hlstd{(}\hlstr{"contr.sum"}\hlstd{,} \hlstr{"contr.poly"}\hlstd{))}
\hlstd{fm2} \hlkwb{<-} \hlkwd{lm}\hlstd{(weight} \hlopt{~} \hlstd{diet, dietData)}
\hlkwd{summary.aov}\hlstd{(fm2)}
\end{alltt}
\begin{verbatim}
##             Df Sum Sq Mean Sq F value Pr(>F)
## diet         3   54.6  18.216   2.053  0.117
## Residuals   56  496.9   8.873
\end{verbatim}
\end{kframe}
\end{knitrout}
\pause
\vfill
{%\bf
  The \inr{aov} function gives identical results}
\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{summary}\hlstd{(}\hlkwd{aov}\hlstd{(weight} \hlopt{~} \hlstd{diet, dietData))}
\end{alltt}
\begin{verbatim}
##             Df Sum Sq Mean Sq F value Pr(>F)
## diet         3   54.6  18.216   2.053  0.117
## Residuals   56  496.9   8.873
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

% See Venables and Ripley (2002, pg 145)

\begin{frame}[fragile]
  \frametitle{An alternative summary}
  \scriptsize
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{summary}\hlstd{(fm2)}
\end{alltt}
\begin{verbatim}
##
## Call:
## lm(formula = weight ~ diet, data = dietData)
##
## Residuals:
##     Min      1Q  Median      3Q     Max
## -7.6371 -1.9253 -0.0366  1.9770  5.4576
##
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept) 27.13962    0.38456  70.573   <2e-16 ***
## diet1       -1.02179    0.66608  -1.534    0.131
## diet2       -0.56593    0.66608  -0.850    0.399
## diet3        0.08027    0.66608   0.121    0.905
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.979 on 56 degrees of freedom
## Multiple R-squared:  0.09908,	Adjusted R-squared:  0.05082
## F-statistic: 2.053 on 3 and 56 DF,  p-value: 0.1169
\end{verbatim}
\end{kframe}
\end{knitrout}
\pause
\vfill
 {%\bf
   Because we changed the \inr{contrast} option to {\tt
     contr.sum}, the intercept is the grand mean ($\mu$) and the other
   estimates are the effect sizes ($\alpha_i$) \\}
%     contr.sum}, The intercept is the mean weight for the reference level
%   (Control) when age=0. The other estimates indicate the
%   difference from the reference level. \\}
 \pause
 \vfill
 The effect size for the last level of diet ({\tt diet4}) isn't shown because it is not a unique parameter (i.e., it is a function of the other parameters: $\alpha_4 = -\alpha_1 - \alpha_2 - \alpha_3$). \\

\end{frame}

%\begin{comment}
\begin{frame}[fragile]
  \frametitle{One-way ANOVA}
  {%\centering
    \small %\bf
    The \inr{predict} function can also be used to obtain
    group means, SEs, and CIs from a one-way ANOVA \\}
  \footnotesize %\scriptsize
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{predData2} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}\hlkwc{diet}\hlstd{=}\hlkwd{levels}\hlstd{(dietData}\hlopt{$}\hlstd{diet))}
\hlstd{pred2} \hlkwb{<-} \hlkwd{predict}\hlstd{(fm2,} \hlkwc{newdata}\hlstd{=predData2,}
                 \hlkwc{se.fit}\hlstd{=}\hlnum{TRUE}\hlstd{,} \hlkwc{interval}\hlstd{=}\hlstr{"confidence"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\pause
\vfill
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{predictions2} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(pred2}\hlopt{$}\hlstd{fit,} \hlkwc{SE}\hlstd{=pred2}\hlopt{$}\hlstd{se, predData2)}
\hlstd{predictions2}
\end{alltt}
\begin{verbatim}
##        fit      lwr      upr        SE    diet
## 1 26.11783 24.57710 27.65856 0.7691199 Control
## 2 26.57368 25.03295 28.11442 0.7691199     Low
## 3 27.21988 25.67915 28.76062 0.7691199     Med
## 4 28.64707 27.10634 30.18780 0.7691199    High
\end{verbatim}
\end{kframe}
\end{knitrout}

\end{frame}

\begin{frame}
  \frametitle{One-way ANOVA}
  \vspace{-0.5cm}
  \begin{center}
    \includegraphics[width=0.8\textwidth]{figure/barplot1-1}
  \end{center}
\end{frame}
%\end{comment}

\section{ANCOVA}

\begin{frame}[fragile]
  \frametitle{ANCOVA preliminaries}
  \small
  { Additive model}
  \[
    y_{ij} = \mu + \alpha_i + \beta(x_{ij} - \bar{x}) + \varepsilon_{ij}
  \]
  \pause
  \vfill
  {Make sure the {\tt contrasts} are set as before}
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{options}\hlstd{(}\hlkwc{contrasts}\hlstd{=}\hlkwd{c}\hlstd{(}\hlstr{"contr.sum"}\hlstd{,} \hlstr{"contr.poly"}\hlstd{))}
\end{alltt}
\end{kframe}
\end{knitrout}
  \pause
  \vfill
  {Centering the covariate isn't required, but doing so allow the
    intercept to be interpretted as the grand mean}
%  \small
  \footnotesize
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{dietData}\hlopt{$}\hlstd{ageCentered} \hlkwb{<-} \hlstd{dietData}\hlopt{$}\hlstd{age} \hlopt{-} \hlkwd{mean}\hlstd{(dietData}\hlopt{$}\hlstd{age)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{ANCOVA}
%  \pause
{%\scriptsize
  \footnotesize
  %\bf
  Put the covariate before the treatment
  variable in the formula. \\}
%\scriptsize %\tiny
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{fm3} \hlkwb{<-} \hlkwd{lm}\hlstd{(weight} \hlopt{~} \hlstd{ageCentered} \hlopt{+} \hlstd{diet, dietData)}
\end{alltt}
\end{kframe}
\end{knitrout}
\pause
%\vfill
\begin{knitrout}\tiny
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{summary}\hlstd{(fm3)}
\end{alltt}
\begin{verbatim}
##
## Call:
## lm(formula = weight ~ ageCentered + diet, data = dietData)
##
## Residuals:
##     Min      1Q  Median      3Q     Max
## -3.8214 -1.2213 -0.2519  1.2161  4.9185
##
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  27.1396     0.2406 112.787  < 2e-16 ***
## ageCentered   0.5573     0.0594   9.382  5.2e-13 ***
## diet1        -1.7446     0.4238  -4.116  0.00013 ***
## diet2        -0.3758     0.4173  -0.901  0.37171
## diet3         0.7819     0.4234   1.847  0.07020 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.864 on 55 degrees of freedom
## Multiple R-squared:  0.6536,	Adjusted R-squared:  0.6284
## F-statistic: 25.94 on 4 and 55 DF,  p-value: 4.147e-12
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{The ANOVA table}
  {%\bf
    The null hypothesis of no diet
    effect is rejected, even though it was not rejected before.}
  \small
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{summary.aov}\hlstd{(fm3)}
\end{alltt}
\begin{verbatim}
##             Df Sum Sq Mean Sq F value   Pr(>F)
## ageCentered  1 278.25  278.25  80.095 2.54e-12 ***
## diet         3  82.22   27.41   7.889 0.000182 ***
## Residuals   55 191.07    3.47
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Predict weight}
  {%\bf
    Create predictions of {\tt weight} over a sequences of {\tt
      ages}, for every level of {\tt diet}}
  \small
\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{ageC} \hlkwb{<-} \hlstd{dietData}\hlopt{$}\hlstd{ageCentered}
\hlstd{predData3} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}
    \hlkwc{diet}\hlstd{=}\hlkwd{rep}\hlstd{(}\hlkwd{c}\hlstd{(}\hlstr{"Control"}\hlstd{,} \hlstr{"Low"}\hlstd{,} \hlstr{"Med"}\hlstd{,} \hlstr{"High"}\hlstd{),} \hlkwc{each}\hlstd{=}\hlnum{20}\hlstd{),}
    \hlkwc{ageCentered}\hlstd{=}\hlkwd{rep}\hlstd{(}\hlkwd{seq}\hlstd{(}\hlkwd{min}\hlstd{(ageC),} \hlkwd{max}\hlstd{(ageC),}
                          \hlkwc{length}\hlstd{=}\hlnum{20}\hlstd{),}
                      \hlkwc{times}\hlstd{=}\hlnum{4}\hlstd{))}
\hlstd{pred3} \hlkwb{<-} \hlkwd{predict}\hlstd{(fm3,} \hlkwc{newdata}\hlstd{=predData3,} \hlkwc{se.fit}\hlstd{=}\hlnum{TRUE}\hlstd{,}
                 \hlkwc{interval}\hlstd{=}\hlstr{"confidence"}\hlstd{)}
\hlstd{predictions3} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(pred3}\hlopt{$}\hlstd{fit, predData3)}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Plot the regression lines}
\footnotesize %\small

\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{colrs} \hlkwb{<-} \hlkwd{c}\hlstd{(}\hlstr{"black"}\hlstd{,} \hlstr{"royalblue"}\hlstd{,} \hlstr{"orange"}\hlstd{,} \hlstr{"darkcyan"}\hlstd{)}
\hlkwd{plot}\hlstd{(weight} \hlopt{~} \hlstd{ageCentered, dietData,} \hlkwc{cex}\hlstd{=}\hlnum{1.2}\hlstd{,}
     \hlkwc{pch}\hlstd{=}\hlkwd{rep}\hlstd{(}\hlnum{15}\hlopt{:}\hlnum{18}\hlstd{,} \hlkwc{each}\hlstd{=}\hlnum{15}\hlstd{),}
     \hlkwc{col}\hlstd{=}\hlkwd{rep}\hlstd{(colrs,} \hlkwc{each}\hlstd{=}\hlnum{15}\hlstd{))}
\hlkwd{lines}\hlstd{(fit} \hlopt{~} \hlstd{ageCentered, predictions3,} \hlkwc{subset}\hlstd{=diet}\hlopt{==}\hlstr{"Control"}\hlstd{,}
      \hlkwc{col}\hlstd{=colrs[}\hlnum{1}\hlstd{],} \hlkwc{lwd}\hlstd{=}\hlnum{2}\hlstd{)}
\hlkwd{lines}\hlstd{(fit} \hlopt{~} \hlstd{ageCentered, predictions3,} \hlkwc{subset}\hlstd{=diet}\hlopt{==}\hlstr{"Low"}\hlstd{,} \hlkwc{lty}\hlstd{=}\hlnum{1}\hlstd{,}
      \hlkwc{col}\hlstd{=colrs[}\hlnum{2}\hlstd{],} \hlkwc{lwd}\hlstd{=}\hlnum{2}\hlstd{)}
\hlkwd{lines}\hlstd{(fit} \hlopt{~} \hlstd{ageCentered, predictions3,} \hlkwc{subset}\hlstd{=diet}\hlopt{==}\hlstr{"Med"}\hlstd{,} \hlkwc{lty}\hlstd{=}\hlnum{1}\hlstd{,}
      \hlkwc{col}\hlstd{=colrs[}\hlnum{3}\hlstd{],} \hlkwc{lwd}\hlstd{=}\hlnum{2}\hlstd{)}
\hlkwd{lines}\hlstd{(fit} \hlopt{~} \hlstd{ageCentered, predictions3,} \hlkwc{subset}\hlstd{=diet}\hlopt{==}\hlstr{"High"}\hlstd{,} \hlkwc{lty}\hlstd{=}\hlnum{1}\hlstd{,}
      \hlkwc{col}\hlstd{=colrs[}\hlnum{4}\hlstd{],} \hlkwc{lwd}\hlstd{=}\hlnum{2}\hlstd{)}
\hlkwd{legend}\hlstd{(}\hlnum{4}\hlstd{,} \hlnum{23}\hlstd{,} \hlkwd{c}\hlstd{(}\hlstr{"High"}\hlstd{,} \hlstr{"Med"}\hlstd{,} \hlstr{"Low"}\hlstd{,} \hlstr{"Control"}\hlstd{),} \hlkwc{pch}\hlstd{=}\hlnum{18}\hlopt{:}\hlnum{15}\hlstd{,}
       \hlkwc{title}\hlstd{=}\hlstr{"Diet"}\hlstd{,} \hlkwc{lwd}\hlstd{=}\hlnum{2}\hlstd{,} \hlkwc{col}\hlstd{=}\hlkwd{rev}\hlstd{(colrs))}
\end{alltt}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}
  \frametitle{Plot the regression lines}
  \vspace{-0.5cm}
  \begin{center}
    \only<1 | handout:0>{\includegraphics[width=0.8\textwidth]{figure/scatplot0-1}}
    \only<2>{\includegraphics[width=0.8\textwidth]{figure/scatplot1-1}}
  \end{center}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Multiple comparisons}
%{\bf Use {\tt multcomp} package}
\scriptsize
\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.878, 0.918, 0.933}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{## install.packages("multcomp")}
\hlkwd{library}\hlstd{(multcomp)}
\hlkwd{summary}\hlstd{(}\hlkwd{glht}\hlstd{(fm3,} \hlkwc{linfct}\hlstd{=}\hlkwd{mcp}\hlstd{(}\hlkwc{diet}\hlstd{=}\hlstr{"Tukey"}\hlstd{)))}
\end{alltt}
\begin{verbatim}
##
## 	 Simultaneous Tests for General Linear Hypotheses
##
## Multiple Comparisons of Means: Tukey Contrasts
##
##
## Fit: lm(formula = weight ~ ageCentered + diet, data = dietData)
##
## Linear Hypotheses:
##                     Estimate Std. Error t value Pr(>|t|)
## Low - Control == 0    1.3688     0.6875   1.991  0.20389
## Med - Control == 0    2.5265     0.6973   3.623  0.00336 **
## High - Control == 0   3.0830     0.6832   4.513  < 0.001 ***
## Med - Low == 0        1.1577     0.6828   1.696  0.33583
## High - Low == 0       1.7143     0.6817   2.515  0.06861 .
## High - Med == 0       0.5566     0.6869   0.810  0.84931
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## (Adjusted p values reported -- single-step method)
\end{verbatim}
\end{kframe}
\end{knitrout}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Assignment}
  {\bf \large Complete the following and upload your \R~script to ELC
    before lab next week \\}
%  \pause
  \vfill
  \begin{enumerate}[\bf \color{PineGreen} (1)]
%    \item Run a new analysis using {\tt lm} to test for an interaction
%      of diet and age
%    \item Is the interaction significant? Report the $F$-value and
%      $P$-value used for this test.
    \item Fit an ANCOVA model to the data in {\tt treeData.csv}, which
      represent the height of trees following a fertilizer
      experiment. The covariate is pH.
    \item Use: {\tt options(contrasts=c("contr.sum", "contr.poly"))}
      so that your estimates correspond to the additive model from the
      lecture notes
    \item Interpret each of the estimates from {\tt lm}. What is the
      null hypothesis associated with each $p$-value?
    \item Plot the data and the regression lines. Use different colors
      or symbols to distinguish the treatment groups.
    \item Which fertilizer treatments are significantly different?
  \end{enumerate}

\end{frame}

\end{document}