\documentclass[letterpaper,11pt]{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[hmargin=1.25in,vmargin=1in]{geometry}
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{lmodern}
\usepackage{microtype}

\title{Coursework 1: STAT 570}
\author{Philip Pham}
\date{\today}

\begin{document}
\maketitle

\begin{enumerate}
\item The data we analyze are from a 1970s study that investigated insurance
  redlining on $n = 47$ zipcodes. Information on who was being refused
  homeowners is not available so instead we take as response the number of FAIR
  plan policies written and renewed in Chicago by zip code over the period
  December 1977 to May 1978. The FAIR plan was offered by the city of Chicago as
  a default policy to homeowners who had been rejected by the voluntary market.
  The data we will analyze are named \texttt{chredlin} and are in the
  \texttt{faraway} package. The variable \texttt{involact} are the number of new
  FAIR plan policies and renewals per 100 housing units.

  We will consider five covariates for modeling the response: racial composition
  in percent minority (\texttt{race} $x_{i1}$), fires per 100 housing units
  (\texttt{fire} $x_{i2}$), theft per 1000 population (\texttt{theft} $x_{i3}$),
  percent of housing units built before 1939 (\texttt{age} $x_{i4}$), log
  median family income in thousands of dollars (\texttt{lincome} $x_{i5}$`),
  $i = 1,\ldots,47$.

  We will examine the model with the main effects due to race, fire, theft, age
  and $\log(\mathrm{income})$.

  We let $Y_i$ represent \texttt{involact}, and
  $x_i = \left(x_{i1}, x_{i2}, \ldots, x_{i5}\right)$, the covariates, for
  individual $i$, $i = 1,2,\ldots,47$. We fit the model
  \begin{equation}
    y_i = \beta_0 + \sum_{j=1}^5x_{ij}\beta_j + \epsilon_i
    \label{eqn:p1_model}
  \end{equation}
  for $i=1,\ldots,n$ using least squares.

  \begin{enumerate}
  \item Provide informative plots to illustrate what we might expect to learn
    from the model in Equation \ref{eqn:p1_model}.
    \label{part:p1a}

    \begin{description}
    \item[Solution:] See Figure \ref{fig:p1_pair_plots} and the corresponding
      code in
      \href{https://nbviewer.jupyter.org/github/ppham27/stat570/blob/master/hw1/chredlin\_explore.ipynb}{\texttt{chredlin\_explore.ipynb}}.

      \texttt{fire}, \texttt{race}, and \texttt{age} appear to be positively
      correlated with \texttt{involact}. \texttt{income} appears to be
      negatively correlated.

      Zipcodes in the northern \texttt{side} of Chicago have a lower minority
      population and higher income. \texttt{involact} is smaller in these
      northern zipcodes, too.
    \end{description}
  \item Give interpretations of the parameters $\beta_j$, $j = 1,\ldots,5$.
    \label{part:p1b}
    \begin{table}
      \centering
      \input{p1_model_parameters.tex}
      \caption{The result of fitting the model described in Equation
        \ref{eqn:p1_model}. The procedure for obtaining the estimates and test
        statistics is described in Part \ref{part:p1_model}.}
      \label{tab:p1_model_parameters}
    \end{table}

    \begin{description}
    \item[Solution:] Fitting such a model, we get the estimates in Table
      \ref{tab:p1_model_parameters} for $\beta_j$.

      The percent of minorities (\texttt{race}) and frequency of fires
      (\texttt{fire}) are positively correlated with the number of FAIR plan
      policies. \texttt{involact} is the number of FAIR plans per 100 housing
      units. Thus, every percent increase in racial minorities means about 1
      FAIR plan, and for every fire per 100 housing units, there are 3 FAIR
      plans.

      \texttt{age} seems to have postive effect on \texttt{involact}, while
      \texttt{theft} has a negative effect.

      \texttt{log\_income} doesn't seem to tell us anything new: it's correlated
      with other covariates, and its effect is mainly due to chance.
    \end{description}

  \item Reproduce every number in the handout using matrix and arithmetic
    operations.
    \label{part:p1_model}

    \begin{description}
    \item[Solution:] Let us assume that
      $\epsilon_i \sim \mathcal{N}\left(0, \sigma^2\right)$. The log-likelihood
      of this model is
      \begin{align}
        \sum_{i=1}^n \log\mathbb{P}\left(y_i \mid x_i, \beta, \sigma^2\right)
        &= -\frac{n}{2}\log\left(2\pi\sigma^2\right)
          - \frac{1}{2\sigma^2}\sum_{i=1}^n \left(y_i - x_i^\intercal\beta\right)^2
          \nonumber \\
        &= -\frac{n}{2}\log\left(2\pi\sigma^2\right) - \frac{1}{2\sigma^2}\left\lVert
          y - X\beta
          \right\rVert_2^2,
          \label{eqn:p1_log_likelihood}
      \end{align}
      where we $0$-index $\beta$ and the columns of $X$, so each row of $X$ is
      $x_i = \left(1, x_{i1}, x_{i2},\ldots,x_{i5}\right)$.

      \subsection*{Estimating $\hat{\beta}$}

      To maximize Equation \ref{eqn:p1_log_likelihood}, we choose $\hat{\beta}$
      such that $X\hat{\beta}$ is the projection of $y$ onto the hyperplane
      spanned by the columns of $X$. Thus, we must have that
      $X^\intercal\left(y - X\hat{\beta}\right) = 0$ since the residuals will
      orthogonal to the columns of $X$ if $X\hat{\beta}$ is the projection that
      minimizes the squared error. Solving for $\hat{\beta}$, we have that
      \begin{equation}
        \hat{\beta} = \left(X^\intercal X\right)^{-1} X^\intercal y.
        \label{eqn:p1_beta_hat}
      \end{equation}
      The results of apply Equation \ref{eqn:p1_beta_hat} can be seen in the
      first column of Table \ref{tab:p1_model_parameters}.

      \subsection*{Estimating $\hat{\sigma}^2$}

      Let us derive an unbiased estimator for residual standard error. Consider
      the residual random vector.

      \begin{equation}
        R = y - X\hat{\beta}
      \end{equation}

      As stated earlier, the residuals are orthogonal to hyperplane spanned by
      the columns of $X$, so they must lie in some orthonormal hyperplane of
      $N - p$ vectors, where $p = \dim(\beta)$. Thus, residuals are $y$
      projected down to this space.

      Let $w_1,\ldots,w_{n-p}$ be an orthonormal basis of this space. Let $W$ be
      matrix with these basis vectors as the columns.

      We have that
      \begin{align}
        R &= y - X\hat{\beta} \nonumber\\
          &= W\left(W^\intercal y\right) \nonumber\\
          &= W\left(W^\intercal\left(X\beta + \sigma\epsilon\right)\right) \nonumber\\
          &= W\left(W^\intercal X\right)\beta + \sigma W\left(W^\intercal\epsilon\right) \nonumber\\
          &= \sigma W\left(W^\intercal\epsilon\right).
      \end{align}

      Now, $W^\intercal\epsilon \sim \mathcal{N}\left(0, I_{n-p}\right)$. To see
      this, note that the $i$th entry is
      $\sum_{j=1}^n w_{ij}\epsilon_j \sim \mathcal{N}\left(0, 1\right)$, and for
      $i \neq i^\prime$,
      \begin{align*}
        \operatorname{Cov}\left(
        \left(W^\intercal\epsilon\right)_i, \left(W^\intercal\epsilon\right)_{i^\prime}\right)
        &=
          \mathbb{E}\left[
          \left(\sum_{j=1}w_{ij}\epsilon_j\right)\left(\sum_{k=1}w_{i^\prime k}\epsilon_k\right)
          \right] \\
        &= \sum_{j=1}^n\mathbb{E}\left[w_{ij}w_{i^\prime j} \epsilon_j^2\right] +
          2\sum_{j=1}^{n-1}\sum_{k=j+1}^n \mathbb{E}\left[w_{ij}w_{i^\prime k} \epsilon_j\epsilon_k\right] \\
        &= w_i^\intercal w_{i^\prime} + 2\sum_{j=1}^{n-1}\sum_{k=j+1}^n w_{ij}w_{i^\prime k}
          \mathbb{E}\left[\epsilon_j\epsilon_k\right] \\
        &= 0,
      \end{align*}
      where the first term disappears by since the two vectors are orthonormal,
      and the second term disappears because of independence of the errors.

      Thus, we have that

      \begin{equation}
        R^\intercal R
        = \sigma^2 \left(W^\intercal\epsilon\right)^\intercal W^\intercal W \left(W^\intercal\epsilon\right)
        = \sigma^2 \left(W^\intercal\epsilon\right)^\intercal\left(W^\intercal\epsilon\right)
        \sim \sigma^2 \chi^2_{n-p}.
        \label{eqn:p1_residual_distribution}
      \end{equation}

      Finally, we have that

      \begin{equation*}
        \mathbb{E}\left[R^\intercal R\right] = \sigma^2\left(n - p\right)
        \Rightarrow
        \mathbb{E}\left[\frac{\sum_{i=1}^n \left(y - X\hat{\beta}\right)^2}{n-p}\right] = \sigma^2.
      \end{equation*}

      Our consistent estimator is

      \begin{equation}
        \hat{\sigma}^2 = \frac{\sum_{i=1}^n \left(y - X\hat{\beta}\right)^2}{n-p}.
        \label{eqn:p1_sample_variance}
      \end{equation}

      Applying Equation \ref{eqn:p1_sample_variance}, we obtain
      \boxed{\hat{\sigma} = \input{p1_residual_standard_error.txt}\unskip.}

      \subsection*{Hypothesis Testing}

      We can rewrite $y$ as $y = X\beta + \sigma \epsilon$, where each element
      of $\epsilon$ is drawn from $\mathcal{N}\left(0, 1\right)$. Substituting,
      we have that
      \begin{align}
        \hat{\beta}
        &= \left(X^\intercal X\right)^{-1}X^\intercal\left(X\beta + \sigma\epsilon\right) \nonumber\\
        &= \beta + \sigma\left(X^\intercal X\right)^{-1}X^\intercal \epsilon.
          \label{eqn:p1_beta_hat_distribution}
      \end{align}

      Thus, $\hat{\beta}_j \sim \mathcal{N}\left(\beta_j, \sigma^2\left(X^\intercal X\right)^{-1}_{jj}\right)$.

      This gives us that
      \begin{equation*}
        \frac{\hat{\beta}_j - \beta_j}{\sqrt{\sigma^2\left(X^\intercal X\right)^{-1}_{jj}}} \sim
        \mathcal{N}\left(0, 1\right).
      \end{equation*}

      From Equations \ref{eqn:p1_residual_distribution} and \ref{eqn:p1_sample_variance},
      \begin{equation}
        (n - p)\frac{\hat{\sigma}^2}{\sigma^2} \sim \chi^2_{n-p}.
      \end{equation}

      $\hat{\beta}$ and $\hat{\sigma}^2$ are independent by
      \href{https://en.wikipedia.org/wiki/Basu\%27s_theorem}{Basu's theorem}:
      $\hat{\sigma}^2$ is an ancillary statistic that does not depend on the
      model parameters, $\beta$. Thus, we have that
      \begin{equation}
        \left.
          \frac{\hat{\beta}_j - \beta_j}{\sqrt{\sigma^2\left(X^\intercal X\right)^{-1}_{jj}}}
          \middle/
          \sqrt{\frac{(n - p)\frac{\hat{\sigma}^2}{\sigma^2}}{n-p}}
        \right.
        = \frac{\hat{\beta}_j - \beta_j}{\sqrt{\hat{\sigma}^2\left(X^\intercal X\right)^{-1}_{jj}}}
        \sim t_{n-p}.
        \label{eqn:p1_beta_hat_j_distribution}
      \end{equation}

      That is, we have $t$ distribution with $n - p$ degrees of freedom. The
      denominator of Equation \ref{eqn:p1_beta_hat_j_distribution} gives the
      second column of Table \ref{tab:p1_model_parameters}.

      For each $\beta_j$, our null hypothesis is $H_0: \beta_j = 0$. Thus, our
      $t$-test statistic is obtain from substituting $\beta_j$ into Equation
      \ref{eqn:p1_beta_hat_j_distribution},
      \begin{equation*}
        \hat{t}_j = \frac{\hat{\beta}_j}{\sqrt{\hat{\sigma}^2\left(X^\intercal X\right)^{-1}_{jj}}},
      \end{equation*}
      which gives us the third column of Table \ref{tab:p1_model_parameters}.

      The fourth column is the probability of obtaining evidence that
      contradicts the null hypothesis at least as much. Let $F^{-1}_{t_{n-p}}$
      be the inverse cumulative distribution function. The $p$-value is
      \begin{equation*}
        \mathbb{P}\left(
          \left\lvert T_{n - p}\right\rvert \geq
          \left\lvert \hat{t}_j\right\rvert
          \mid
          \hat{t}_j
        \right) =
        2\left(1 - F^{-1}_{n - p}\left(\left\lvert\hat{t}_j\right\rvert\right)\right).
      \end{equation*}

      These calculations are carried out in
      \href{https://nbviewer.jupyter.org/github/ppham27/stat570/blob/master/hw1/chredlin\_model.ipynb}{\texttt{chredlin\_explore.ipynb}}.
    \end{description}

  \item What assumptions are valid for:
    \begin{enumerate}
    \item An unbiased estimate of $\beta_j$, $j = 1,\ldots,5$.
      \label{part:p1di}

      \begin{description}
      \item[Solution:] From Equation \ref{eqn:p1_beta_hat_distribution}, we have
        that
        \begin{equation}
          \mathbb{E}\left[\hat{\beta}\right]
          =
          \beta + \left(X^\intercal X\right)^{-1}X^\intercal \mathbb{E}\left[\epsilon\right]
        \end{equation}
        since expectation is a linear operator. In our previous calcuations, we
        assumed that the $\epsilon_i$ were independent and normally distributed.

        It's sufficient, however, that
        $\boxed{\mathbb{E}\left[\epsilon\right] = \mathbf{0}.}$ Then, we'll have
        \begin{equation*}
          \operatorname{bias}\left(\hat{\beta}\right) =
          \mathbb{E}\left[\hat{\beta}\right] - \beta
          = \beta - \beta = 0.
        \end{equation*}
      \end{description}
    \item An accurate estimate of the standard error of $\hat{\beta}_j$,
      $j = 1,\ldots,5$.

      \begin{description}
      \item[Solution:] From Equation \ref{eqn:p1_beta_hat_distribution}, we can
        estimate the standard error exactly if $\sigma^2$ is known. For
        $\hat{\beta}_j$, we get $\sigma\sqrt{\left(X^\intercal X\right)_{jj}^{-1}}$.

        When $\sigma^2$ is unknown, but our errors are still independent and
        normally distributed, we apply Equation
        \ref{eqn:p1_beta_hat_j_distribution}. Since $\hat{\beta}_j$ has Student's
        $t$-distribution, we can estimate the standard error for $\hat{\beta}_j$
        with $\sqrt{\hat{\sigma}^2\left(X^\intercal X\right)_{jj}^{-1}}$.

        If our errors are not normally distributed, our estimate is only
        accurate if the number of observations is large, and our errors have a
        distribution that converges to a normal distribution.
      \end{description}
    \item Accurate coverage probabilities for $100\left(1 - \alpha\right)\%$
      confidence intervals of the form
      \begin{equation}
        \hat{\beta}_j \pm \sqrt{\hat{\sigma}_j^2}z_{1-\alpha/2},
        \label{eqn:p1_confidence_interval_normal}
      \end{equation}
      where $z_{1-\alpha/2}$ represents the $\left(1-\alpha/2\right)$ quantile
      of an $\mathcal{N}\left(0, 1\right)$ random variable, and
      $\hat{\sigma}_j^2 = \hat{\sigma}^2\left(X^\intercal X\right)_{jj}^{-1}$.

      \begin{description}
      \item[Solution:] Firstly, the assumptions from the previous part must hold
        for $\hat{\sigma}_j^2$ to be meaningful.

        From Equation \ref{eqn:p1_beta_hat_j_distribution}, $\hat{\sigma}_j^2$
        has Student's $t$-distribution, so the normal approximation for the the
        confidence interval (Equation \ref{eqn:p1_confidence_interval_normal})
        only holds when $n$ is large.
      \end{description}

    \item Accurate coverage probabilities for $100\left(1 - \alpha\right)\%$
      confidence intervals of the form

      \begin{equation}
        \hat{\beta}_j \pm \sqrt{\hat{\sigma}_j^2}t_{n-p}\left(1-\alpha/2\right),
        \label{eqn:p1_confidence_interval_t}
      \end{equation}
      where $p = \dim\left(\beta\right)$ and $t_{n-p}\left(1-\alpha/2\right)$
      represents the $\left(1-\alpha/2\right)$ quantile of standard Student's
      $t$ random variable with $n - p$ degrees of freedom.

      \begin{description}
      \item[Solution:] Equation \ref{eqn:p1_beta_hat_j_distribution} shows that
        this is exactly the correct distribution when the $\epsilon_i$ are
        independent and identically distributed as normal random variables with
        mean zero.

        It may still prove to be an accurate confidence interval if the errors
        have distributions that are well-approximated by the normal
        distribution and the number of observations is large.
      \end{description}
    \item An accurate prediction for an \emph{observed} outcome at $x_0$.

      \begin{description}
      \item[Solution:] Suppose we were to observe
        $\left(x_0, y_0 = x_0^\intercal\beta + \epsilon_0\right)$. Let our
        prediction be $\hat{y}_0 = x_0^\intercal\hat{\beta}$. If the conditions
        in Part \ref{part:p1di} are satisfied, the error has mean zero, and our
        estimate for $\hat{\beta}$ is unbiased, so
        \begin{equation*}
          \mathbb{E}\left[y_0\right]
          = x_0^\intercal\beta
          = \mathbb{E}\left[\hat{y}_0\right].
        \end{equation*}

        We want to compare our prediction with $\hat{y}_0$ with some
        hypothetical observed response $y_0$. We'll call our prediction
        accurate within $\delta > 0$ if
        \begin{equation*}
          \hat{y}_0 - \delta \leq y_0 \leq \hat{y}_0 + \delta.
        \end{equation*}

        We want the probability of this event to be high, so we'll say accurate
        within $\delta$ at confidence level $1 - \alpha$ if
        \begin{equation*}
          \mathbb{P}\left(\hat{y}_0 - \delta \leq y_0 \leq \hat{y}_0 + \delta\right)
          = \mathbb{P}\left(-\delta \leq y_0 - \hat{y}_0 \leq \delta\right)
          \geq 1 - \alpha.
        \end{equation*}
        Our prediction is accurate if for small $\alpha$, we have small
        $\delta$.

        If we assume normality, we can calculate the minimum $\delta$ for a
        specific $\alpha$, which we'll denote $\delta_\alpha$.

        Since $\hat{\beta}$ satisfies
        $\left(X^\intercal X\right)\hat{\beta} = X^\intercal y$, we have that
        the intercept estimate is
        \begin{equation}
          \hat{\beta}_0 = \bar{y} - \sum_{j=1}^p \hat{\beta}_j \bar{X}_{:,j}.
        \end{equation}

        Consider trying to predict $\hat{y} = x^\intercal\hat{\beta}$ for some
        $x$. We have that
        \begin{equation}
          \hat{y} = \bar{y} + \sum_{j=1}^p \left(x_i - \bar{X}_{:,j}\right)\hat{\beta}_j,
        \end{equation}
        so the variance of the prediction increases with values far from data.

        Let $\bar{X}$ be the vector of column-wise means of $X$. Since
        $\bar{\epsilon}$ is an ancillary statistic, this can also be written as

        \begin{equation}
          \hat{y} \mid x \sim \mathcal{N}\left(
            x^\intercal \beta,
            \sigma^2 \left(
              \frac{1}{n} +
              \left(x - \bar{X}\right)^\intercal
              \left(X^\intercal X\right)^{-1}
              \left(x - \bar{X}\right)
            \right)
          \right).
        \end{equation}

        Using the same method as in deriving Equation
        \ref{eqn:p1_beta_hat_j_distribution}, if we replace $\beta$ with
        $\hat{\beta}$ and $\sigma^2$ with $\hat{\sigma}^2$, we have
        \begin{equation}
          \frac{\hat{y} - x^\intercal\hat{\beta}}{
            \sqrt{\hat{\sigma}^2\left(\frac{1}{n} +
              \left(x - \bar{X}\right)^\intercal
              \left(X^\intercal X\right)^{-1}
              \left(x - \bar{X}\right)\right)}}
          \sim t_{n-p}.
          \label{eqn:p1_response_confidence_interval}
        \end{equation}

        Noting that
        $y_0 \sim \mathcal{N}\left(x_0^\intercal\beta, \sigma^2\right)$, we can
        apply Equation \ref{eqn:p1_response_confidence_interval} to
        $\left(x_0, y_0\right)$, which gives us
        \begin{equation*}
          \boxed{
            \delta_\alpha
            =
            t_{n-p}\left(1 - \alpha/2\right)
            \sqrt{\hat{\sigma}^2\left(
                1 + \frac{1}{n} +
                \left(x_0 - \bar{X}\right)^\intercal
                \left(X^\intercal X\right)^{-1}
                \left(x_0 - \bar{X}\right)
              \right).
            }
          }
        \end{equation*}

        Thus, our predictions will always have standard error of at least
        $\sigma$, but they will be more accurate when $x_0$ is close to
        $\bar{X}$.
      \end{description}
    \end{enumerate}
  \item Summarize the relationship between $y$, and $x_1$, $x_2$, $x_3$, $x_4$,
    $x_5$, fitting any other models that you see fit to.

    \begin{description}
    \item[Solution:] The relationship between $y$ and the covariates was
      described in Parts \ref{part:p1a} and \ref{part:p1b}.

      Particularly, we see \texttt{income} does not explain much about
      \texttt{involact} due to multicollinearity: it is correlated with
      \texttt{race} and \texttt{fire}.

      Removing \texttt{log\_income} from the model gives us the model parameters
      in Table \ref{tab:p1_model_parameters_custom}. The residual standard error
      for this model was \input{p1_residual_standard_error_custom.txt}\unskip,
      which is actually ever so slightly smaller than the model that includes
      income.

      \begin{table}
        \centering
        \input{p1_model_parameters_custom.tex}
        \caption{The result of fitting a model without considering income.}
        \label{tab:p1_model_parameters_custom}
      \end{table}

      I tried adding an indicator for \texttt{side} but it suffers from the same
      issue as \texttt{income}: its effect is already explained by the other
      covariates.
    \end{description}
  \end{enumerate}

  \pagebreak
\item Consider the following distributions:
  \begin{description}
  \item[Poisson:] \begin{equation}
      p\left(y \mid \mu\right) = \frac{\exp\left(-\mu\right)\mu^y}{y!},
      \label{eqn:p2_poisson}
    \end{equation}
    for $y = 0,1,2,\ldots$.

  \item[Gamma:] \begin{equation}
      p\left(y \mid \alpha, \beta\right) =
      \frac{\beta^\alpha}{\Gamma(\alpha)}y^{\alpha - 1}\exp(-\beta y)
      \label{eqn:p2_gamma}
    \end{equation}
    for $y > 0$ and with $\alpha$ known.

  \item[Inverse Gaussian:]
    \begin{equation*}
      p\left(y \mid \mu, \delta\right) =
      \left(\frac{\delta}{2\pi y^3}\right)^{1/2}
      \exp\left[
        \frac{-\delta\left(y-\mu\right)^2}{2\mu^2y}
      \right]
      \label{eqn:p2_inverse_gaussian}
    \end{equation*}
    for $y > 0$ and $\delta$ known.

  \end{description}

  A distribution is said to be a member of the one parameter exponential family
  of distributions if it can be written as
  \begin{equation}
    p\left(y \mid \eta_1,\eta_2\right) = h(y)\exp\left[
      \eta_1y + \eta_2T_2(y) - A\left(\eta_1,\eta_2\right)
    \right],
    \label{eqn:p2_exponential}
  \end{equation}
  where $\eta_2$ is known.

  \begin{enumerate}
  \item Show that each of the above distributions is a member of the exponential
    family and identify $\eta_1$, $\eta_2$, $T_2(y)$,
    $A\left(\eta_1,\eta_2\right)$, and $h(y)$.
    \label{part:p2a}
    \begin{description}
    \item[Solution:] For each distribution, we can do some algebra.

      \begin{description}
      \item[Poisson:] We can rewrite Equation \ref{eqn:p2_poisson} as Equation
        \ref{eqn:p2_exponential}, where
        \begin{align*}
          \eta_1 &= \log\mu \\
          \eta_2 &= 0 \\
          T_2(y) &= 0 \\
          A\left(\eta_1,\eta_2\right) &= \exp(\eta_1) \\
          h(y) &= \frac{1}{y!}.
        \end{align*}
      \item[Gamma:] We can rewrite Equation \ref{eqn:p2_gamma} as Equation
        \ref{eqn:p2_exponential}, where
        \begin{align*}
          \eta_1 &= -\beta \\
          \eta_2 &= \alpha - 1 \\
          T_2(y) &= \log(y) \\
          A\left(\eta_1,\eta_2\right)
                 &=-\left(\eta_2 + 1\right)\log\left(-\eta_1\right) +
                   \log\Gamma\left(\eta_2 + 1\right)\\
          h(y) &= 1.
        \end{align*}
      \item[Inverse Gaussian:] We can rewrite Equation \ref{eqn:p2_inverse_gaussian} as
        Equation \ref{eqn:p2_exponential}, where
        \begin{align*}
          \eta_1 &= -\frac{\delta}{2\mu^2} \\
          \eta_2 &= -\frac{\delta}{2} \\
          T(y) &= \frac{1}{y} \\
          A\left(\eta_1,\eta_2\right)
                 &= -2\sqrt{\eta_1\eta_2} -
                   \frac{1}{2}\log\left(-2\eta_2\right) \\
          h(y) &= \frac{1}{\sqrt{2\pi y^3}}.
        \end{align*}
      \end{description}
    \end{description}
  \item Identify $\mathbb{E}\left[Y \mid \theta\right]$ and
    $\operatorname{Var}\left(Y \mid \theta\right)$.
    \begin{description}
    \item[Solution:] We can derive a general formula for computing the mean and
      variance from Equation \ref{eqn:p2_exponential}.

      The log-likelihood function is
      \begin{equation}
        l\left(
          \eta_1, \eta_2
        \right) =
        \log h(y) + \eta_1y + \eta_2 T_2(y) - A\left(\eta_1,\eta_2\right).
      \end{equation}

      If $\eta_2$ is known, the score function is
      \begin{equation}
        S\left(\eta_1, \eta_2\right) = \frac{\partial l\left(\eta_1,\eta_2\right)}{\partial \eta_1}
        =
        y - \frac{\partial A\left(\eta_1,\eta_2\right)}{\partial \eta_1}.
      \end{equation}

      The expectation of the score is $0$, so
      \begin{equation}
        \boxed{\mathbb{E}\left[y \mid \eta_1, \eta_2\right] =
        \frac{\partial A\left(\eta_1,\eta_2\right)}{\partial \eta_1}.}
        \label{eqn:p2_score_mean}
      \end{equation}

      The variance of the score is Fisher information, so
      \begin{align}
        \mathcal{I}\left(\eta_1, \eta_2\right)
        &=
        \operatorname{Var}\left(
        S\left(\eta_1, \eta_2\right)
          \right) \nonumber\\
        &= \mathbb{E}\left[
          \left(y -
          \frac{\partial A\left(\eta_1,\eta_2\right)}{\partial \eta_1}
          \right)^2
          \mid
          \eta_1, \eta_2
          \right]\nonumber\\
        &= \operatorname{Var}\left(y \mid \eta_1, \eta_2\right)
          \label{eqn:p2_fisher_variance}
      \end{align}
      by Equation \ref{eqn:p2_score_mean}
      and using that the mean of the score function is $0$.

      An alternative definition of the Fisher information is the expected value
      of the observed information:
      \begin{equation}
        \mathcal{I}\left(\eta_1, \eta_2\right) =
        - \frac{\partial^2 l\left(\eta_1,\eta_2\right)}{\partial\eta_1^2}
        = \frac{\partial^2 A\left(\eta_1,\eta_2\right)}{\partial\eta_1^2}.
        \label{eqn:p2_fisher_expectation}
      \end{equation}

      Combining Equations \ref{eqn:p2_fisher_variance} and
      \ref{eqn:p2_fisher_expectation}, we obtain
      \begin{equation}
        \boxed{
          \operatorname{Var}\left(y \mid \eta_1, \eta_2\right)
          =
          \frac{\partial^2 A\left(\eta_1,\eta_2\right)}{\partial\eta_1^2}.
        }
        \label{eqn:p2_score_variance}
      \end{equation}

      We can now apply Equations \ref{eqn:p2_score_mean} and
      \ref{eqn:p2_score_variance} to our the results from Part \ref{part:p2a}.
      \begin{description}
      \item[Poisson:]
        \begin{align*}
          \mathbb{E}\left[y \mid \eta_1, \eta_2\right]
          &= \exp\left(\eta_1\right) = \mu \\
          \operatorname{Var}\left(y \mid \eta_1, \eta_2\right)
          &= \exp\left(\eta_1\right) = \mu.
        \end{align*}
      \item[Gamma:]
        \begin{align*}
          \mathbb{E}\left[y \mid \eta_1, \eta_2\right]
          &= -\frac{\eta_2 + 1}{\eta_1} = \frac{\alpha}{\beta} \\
          \operatorname{Var}\left(y \mid \eta_1, \eta_2\right)
          &= \frac{\eta_2 + 1}{\eta_1^2} = \frac{\alpha}{\beta^2}.
        \end{align*}
      \item[Inverse Gaussian:]
        \begin{align*}
          \mathbb{E}\left[y \mid \eta_1, \eta_2\right]
          &= \sqrt{\frac{\eta_2}{\eta_1}} = \mu \\
          \operatorname{Var}\left(y \mid \eta_1, \eta_2\right)
          &= \frac{1}{2}\sqrt{\frac{\eta_2}{\eta_1^3}} = \frac{\mu^3}{\delta}.
        \end{align*}
      \end{description}
    \end{description}

  \item The canonical link function is such that $g(\mu) = \eta_1$. Determine the
    canonical link for each distribution.
    \begin{description}
    \item[Solution:] We can use the results from Part \ref{part:p2a}.
      \begin{description}
      \item[Poisson:] $\displaystyle g(\mu) = \exp(\mu)$.
      \item[Gamma:] $\displaystyle g(\mu) = -\frac{\alpha}{\mu} \propto \mu^{-1}$,
        where $\alpha$ is known.
      \item[Inverse Gaussian:]
        $\displaystyle g(\mu) = -\frac{\delta}{2\mu^2} \propto \mu^{-2}$, where $\delta$ is
        known.
      \end{description}
    \end{description}
  \end{enumerate}
\end{enumerate}

\begin{figure}
  \centering
  \includegraphics[width=\textwidth]{p1_pair_plots.pdf}
  \caption{The empirical univariate and joint distributions for the
    \texttt{chredlin} dataset.}
  \label{fig:p1_pair_plots}
\end{figure}

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\setlength\parindent{0pt}
\usepackage{multirow}
\usepackage{graphicx}
\usepackage{siunitx}
\usepackage{float}
\usepackage{derivative}
\usepackage{amsmath,amssymb}

\title{Assignment 2 description and solutions \\ CTA200H}
\author{Lechung Xing - 1004705170 }
\date{May 8th 2020}

\begin{document}

\maketitle

\section{Question 1}
\subsection{Methods}
In order to plot the complex number z on a 2D plane, I separated z and c(the fixed point $x + iy$) into Real and Imaginary parts corresponding to x and y (axis) components, respectively. I found the expression \(Z_{n+1}= [(ReZ_n)^2-(ImZ_n)^2+x] + 2i[(ReZ_n)(ImZ_n) + y]\) for the sequence \(Z_{n+1} = (Z_n)^2 + c, Z_0 = 0\) as n increases. To test the divergence, I selected some c points with iteration steps varying from $10 to 50$ and computed the \(|z|^2\)for each step. The iteration terminated if \(|z|^2\) is larger than 2, which indicated the sequence is diverging under the initial choice of c. I created a n-dim array to partition the domain of c into a \(Matrix_{n\times{n}}\) so that I can collect the diverging c points and marked them with colours to distinguish from the bounded ones. I attempted to demonstrate the rate of divergence for the diverging c points according to the relative difference between their  \(|z|^2\) value (i.e. the magnitude of the entries in \(Matrix_{n\times{n}} = np.abs(z)\)).
\begin{figure}[H]
    \centering
    \includegraphics[scale=0.7]{diverge(1,1).png}
    \includegraphics[scale=0.7]{2colours.png}
    \includegraphics[scale=0.5]{colorbar.png}
    \caption{a) The set of c points confined within a 4 by 4 square are plotted on the complex plane for $c = 1 + 1i$. b) The shaded region represents the c points that generate diverging sequence \(z_{i+1}=(z_i)^2 + c\). The yellow region shows the c points that yield bounded sequences. c)The rate of divergence with color bar generated from n-dim matrix entries. The color resembles the $3rd$ axis apart from x and y axis. }
    \label{fig:Q1}
\end{figure}

\section{Discussion}
In Fig. 1a, since $c = 1 + 1i$ yields diverging sequence from the $8th$ iteration step, the blue dots are far away from each other as expected. In Fig. 1b, the bounded c points are bounded by an open ball of radius 2 centered at the origin. The n-dim array of \(|z|^2\) contains different entries, unlike the homogeneous color as shown in Fig. 1c.

\section{Question 2}
\subsection{Methods}
I implemented the initial condition: $N = 1000, S(t) + I(t) + R(t) = N for any t\in[0, 200]$ to solve the ODE by using the $Scipy.odeint$ package. The parameters: $\beta and \gamma$ determine the slope of the first derivative of S, I, R. I expect S to decrease while I increases in the beginning followed by the delayed increase in R. To take into account of the death rate arising from the infected population, I added $D(t)*I(t)$ to $dI/dt$ and subtracted the death term from $dR/dt$ to satisfy the initial condition.

\begin{figure}[H]
    \centering
    \includegraphics[scale=0.31]{3curvesSIR.png}
    \includegraphics[scale=0.8]{4curvesSIR (1).png}
    \caption{a) The S(t), I(t), R(t) curves plotted for \(t=200days\), representing the number of vulnerable(not yet affected) individuals, infected individuals and recovered individuals, respectively. The first graph has parameters \(\beta=9\times10^6, \gamma=0.01\), second one with \(\beta=3\times10^6, \gamma=0.05\) and the last one with \(\beta=9\times10^6, \gamma=0.003\). The initial condition is \(S(0)=999, I(0)=1 and R(0)=0\) b)Same as a) with a death term included \(\beta=3\times10^6, \gamma=0.005, death2 = 0.01\). }
    \label{fig:Q2}
\end{figure}

\section{Discussion}
Among the 3 figures in Fig. 2a, the one the middle best represents the disease spreading model with a reasonable infected curve and a flattened out recovered curve. Unlike the first and the third plot, the decaying tails of $I(t)$ and $R(t)$ expanded beyond the 200 days time interval which is too slow for most common diseases. In Fig. 2b, the death rate exceeds the recovered rate near $t = 180days$. Vulnerable but not infected individuals vanished after $t=75days$, they become either recovered or dead as shown in this new model.

\end{document}


%!TEX root = ../thesis.tex
% ******************************* Thesis Appendix A ****************************
\chapter{Tables}\label{chap:AppendixOne}

\section{Sequential Probability Ratio Test Tables of A and B as functions of the parameters  \texorpdfstring{$\alpha$} \text{ and} \texorpdfstring{$\beta$}.}

\begin{table}[!ht]
\centering
\caption{A table of values of A=$\frac{1 - \beta}{\alpha}$, where $\alpha$ is the significance and $1 - \beta$  is the power of the Sequential Probability Ratio Test (SPRT)}
\begin{tabular}{| c |cccccccccc|}
\toprule
\diagbox{$\beta$}{$\alpha$} & \makecell{0.05}& \makecell{0.1} & \makecell{0.15} & \makecell{0.2}& \makecell{0.25}& \makecell{0.3} & \makecell{0.35}& \makecell{0.4}& \makecell{0.45}& \makecell{0.5}  \\
\midrule
0.05 & 19.00 & 18.00 & 17.00 & 16.00 & 15.00 & 14.00 & 13.00 & 12.00 & 11.00 & 10.00 \\
0.1 & 9.50 & 9.00 & 8.50 & 8.00 & 7.50 & 7.00 & 6.50 & 6.00 & 5.50 & 5.00 \\
0.15 & 6.33 & 6.00 & 5.67 & 5.33 & 5.00 & 4.67 & 4.33 & 4.00 & 3.67 & 3.33 \\
0.2 & 4.75 & 4.50 & 4.25 & 4.00 & 3.75 & 3.50 & 3.25 & 3.00 & 2.75 & 2.50 \\
0.25 & 3.80 & 3.60 & 3.40 & 3.20 & 3.00 & 2.80 & 2.60 & 2.40 & 2.20 & 2.00 \\
0.3 & 3.17 & 3.00 & 2.83 & 2.67 & 2.50 & 2.33 & 2.17 & 2.00 & 1.83 & 1.67 \\
0.35 & 2.71 & 2.57 & 2.43 & 2.29 & 2.14 & 2.00 & 1.86 & 1.71 & 1.57 & 1.43 \\
0.4 & 2.37 & 2.25 & 2.12 & 2.00 & 1.88 & 1.75 & 1.62 & 1.50 & 1.38 & 1.25 \\
0.45 & 2.11 & 2.00 & 1.89 & 1.78 & 1.67 & 1.56 & 1.44 & 1.33 & 1.22 & 1.11 \\
0.5 & 1.90 & 1.80 & 1.70 & 1.60 & 1.50 & 1.40 & 1.30 & 1.20 & 1.10 & 1.00 \\
\bottomrule
\end{tabular}
\label{table:SPRTA}
\end{table}

\begin{table}[!ht]
\centering
\caption{A table of values of B=$\frac{\beta}{1-\alpha}$, where $\alpha$ is the significance and $1 - \beta$ is the power of the Sequential Probability Ratio Test (SPRT)}
\begin{tabular}{| c |cccccccccc|}
\toprule
\diagbox{$\beta$}{$\alpha$} & \makecell{0.05}& \makecell{0.1} & \makecell{0.15} & \makecell{0.2}& \makecell{0.25}& \makecell{0.3} & \makecell{0.35}& \makecell{0.4}& \makecell{0.45}& \makecell{0.5}  \\
\midrule
0.05 & 0.05 & 0.11 & 0.16 & 0.21 & 0.26 & 0.32 & 0.37 & 0.42 & 0.47 & 0.53 \\
0.1 & 0.06 & 0.11 & 0.17 & 0.22 & 0.28 & 0.33 & 0.39 & 0.44 & 0.50 & 0.56 \\
0.15 & 0.06 & 0.12 & 0.18 & 0.24 & 0.29 & 0.35 & 0.41 & 0.47 & 0.53 & 0.59 \\
0.2 & 0.06 & 0.12 & 0.19 & 0.25 & 0.31 & 0.37 & 0.44 & 0.50 & 0.56 & 0.62 \\
0.25 & 0.07 & 0.13 & 0.20 & 0.27 & 0.33 & 0.40 & 0.47 & 0.53 & 0.60 & 0.67 \\
0.3 & 0.07 & 0.14 & 0.21 & 0.29 & 0.36 & 0.43 & 0.50 & 0.57 & 0.64 & 0.71 \\
0.35 & 0.08 & 0.15 & 0.23 & 0.31 & 0.38 & 0.46 & 0.54 & 0.62 & 0.69 & 0.77 \\
0.4 & 0.08 & 0.17 & 0.25 & 0.33 & 0.42 & 0.50 & 0.58 & 0.67 & 0.75 & 0.83 \\
0.45 & 0.09 & 0.18 & 0.27 & 0.36 & 0.45 & 0.55 & 0.64 & 0.73 & 0.82 & 0.91 \\
0.5 & 0.10 & 0.20 & 0.30 & 0.40 & 0.50 & 0.60 & 0.70 & 0.80 & 0.90 & 1.00 \\
\bottomrule
\end{tabular}
\label{table:SPRTB}
\end{table}


\documentclass{memoir}
\usepackage{notestemplate}

%\logo{./resources/pdf/logo.pdf}
%\institute{Rice University}
%\faculty{Faculty of Whatever Sciences}
%\department{Department of Mathematics}
%\title{Class Notes}
%\subtitle{Based on MATH xxx}
%\author{\textit{Author}\\Gabriel \textsc{Gress}}
%\supervisor{Linus \textsc{Torvalds}}
%\context{Well, I was bored...}
%\date{\today}

\begin{document}

% \maketitle

% Notes taken on 01/27/21

\begin{defn}[Norm of Integral Domain]
	Let \(R\) be an integral domain. Any function \(N:R\to \Z^{+}\cup \left\{ 0 \right\} \) with \(N(0)\) is called a \textbf{norm} on the integral domain \(R\). If \(N(a) > 0\) for \(a\neq 0\) then \(N\) is a \textbf{positive norm}
\end{defn}
This is a pretty loose construction, and an integral domain can have many norms on it.\\

\begin{defn}[Euclidean Function]
	An integral domain \(R\) is a \textbf{Euclidean Domain} if there exists a norm called the \textbf{Euclidean function} \(N:R\setminus\left\{ 0 \right\} \to \N\) that satisfies \(\forall a,b \in R\setminus \left\{ 0 \right\} \):
	\begin{align*}
		N(ab) \geq \textrm{max}\left\{ N(a),N(b) \right\}\\
		\exists q,r \in R \text{ such that }a = qb + r \text{ and }\left[ r = 0 \text{ or }N(r)<N(b) \right]
	\end{align*}
	We call the element \(q\) the \textbf{quotient} and the element \(r\) the  \textbf{remainder}.
\end{defn}
The existence of a Euclidean function is integral to constructing a Euclidean algorithm to perform division of elements \(a,b \in R\). We can perform successive divisions to get
\begin{align*}
	a = q_0b + r_0\\
	b = q_1r_0 + r_1\\
	r_0 = q_2r_1 + r_2\\
	\vdots\\
	r_{n-1}= q_nr_{n-1}+r_n\\
	r_{n-1} = q_{n+1}r_n
\end{align*}
where \(r_n\) is the last nonzero remainder. This \(r_n\) always exists as the norms form a decreasing sequence of nonnegative integers. However, these elements are not necessarily unique.

\begin{prop}
	Every ideal in a Euclidean Domain is principal. That is, if \(I\triangleleft R\) is a nontrivial ideal in \(R\), then \(I = (d)\) for some nonzero element of \(I\) with minimum norm.
\end{prop}
This also makes it convenient to show an integral domain is not a Euclidean Domain by simply finding a non-principal ideal. Moreover, it motivates the notion of greatest common divisors from \(\Z\) into comumutative rings.

\begin{defn}[Greatest Common Divisor]
	Let \(R\) be a commutative ring and let \(a,b \in R\) with \(b\neq 0\). Then \(a\) is said to be a \textbf{multiple} of \(b\) if there exists an element \(x \in R\) with \(a=bx\). In this case, we say \(b\) \textbf{divides} \(a\) (or is a \textbf{divisor} of \(a \)) written \(b\mid a\).\\

	A \textbf{greatest common divisor} of \(a,b\) is a nonzero element \(d\) such that
	\begin{align*}
		d \mid a, \; d \mid b \\
		d'\mid a \; d'\mid b \implies d'\mid d
	\end{align*}
	We will denote a greatest common divisor by \(\textrm{gcd}(a,b)\), or sometimes simply \((a,b)\) if it is clear from context.\\

	If \(gcd(a,b) = 1_R\), then we say that \(a\) and \(b\) are \textbf{relatively prime}.
\end{defn}
We can easily extend this to finite sequences of elements \((a_1,a_2,\ldots,a_n)\).\\

Recall that in a ring \(b\mid a \iff a \in (b) \iff (a) \subset (b)\). Hence, we can discuss greatest common factors in terms of ideals. That is, if \(I = (a,b)\) is the ideal of \(R\) generated by \(a,b\), then \(d = \textrm{gcd}(a,b)\) if \(I\subset (d)\) and if \(I\subset (d') \implies (d)\subset (d')\). Thus, it is the unique smallest principal ideal containing \(a\) and \(b\). However, it may not exist in all rings.

\begin{prop}[Sufficient Conditions for Existence]
	If \(a,b \in R\) are nonzero elements in a commutative ring such that \(I = (a,b) = (d)\), then \(d\) is the greatest common divisor of \(a,b\).
\end{prop}
Obviously this is a sufficient and not a necessary condition. But it also clarifies why \((a,b)\) is used both for ideals and greatest common divisors. Any integral domain that satisfies the above condition for all ideals of two elements is called a \textbf{Bezout Domain}.

\begin{prop}
	Let \(R\) be an integral domain. If two elements \(d,d' \in R\) generate the same principal ideal, i.e. \((d) = (d')\), then \(d' = ud\) for some unit \(u \in R\). In particular, this tells us that greatest common divisors are unique up to units.
\end{prop}

\begin{thm}
	Let \(R\) be a Euclidean Domain and let \(a,b \in R\) be nonzero. Let \(d = r_n\) be the last nonzero remainder in the Euclidean Algorithm for \(a,b\) described earlier. Then \(d = \textrm{gcd}(a,b)\) and \((d) = (a,b)\). That is, \(d\) can be written as an \textbf{\(R\)-linear combination} of \(a,b\):
	\begin{align*}
		d = ax+by
	\end{align*}
	for some \(x,y \in R\).
\end{thm}
Notice that \(x,y\) are not unique in this case. One can show that that if \(x_0,y_0\) are solutions to
\begin{align*}
	ax+by=N
\end{align*}
then any other solutions are of the form
\begin{align*}
	x = x_0 + m \frac{b}{(a,b)}\\
	y = y_0 - m \frac{a}{(a,b)}
\end{align*}
for \(m \in \Z\). This is really strong as it gives a complete solution of the first order Diophantine equation provided we have one solution. Our work here essentially tells us that \(ax+by = N\) is solvable in integers \(x,y\) if and only if \(\textrm{gcd}(a,b)\mid N\).

\begin{proof}

\end{proof}

Finally, we discuss a definition that is useful to determine whether an integral domain is a Euclidean Domain.
\begin{defn}[Universal Side Divisor]
	Let \(R\) be an integral domain, and define \(\tilde{R}= R^{\times }\cup \left\{ 0 \right\} \). We say an element \(u \in R-\tilde{R}\) is a \textbf{universal side divisor} if for every \(x \in R\) there is a \(z \in \tilde{R}\) such that
	\begin{align*}
		u\mid x-z
	\end{align*}
	That is, every \(x\) can be written
	\begin{align*}
		x = qu + z
	\end{align*}
	where \(z\) is either zero or a unit.
\end{defn}

\begin{prop}
	Let \(R\) be an integral domain that is not a field. If \(R\) is a Euclidean Domain, then there exist universal side divisors in \(R\).
\end{prop}
It is often simpler to show that an integral domain can't have universal side divisors by assuming one exists of minimal norm, finding candidates, then showing they fail to satisfy the necessary properties.

\begin{hw}
	Let \(F = \Q(\sqrt{D} )\) be a quadratic field with quadratic integer ring \(\mathcal{O}\) and field norm \(N\).
	\begin{itemize}
		\item Suppose \(D \in \left\{ -1,-2,-3,-7,-11 \right\} \). Prove that \(\mathcal{O}\) is a Euclidean Domain with respect to \(N\). %Hint: Modify the proof for Z[i] in the text. For D = -3,-7, -11, prove that every element of F differs from an element in O by an element whose norm is at most (1+|D|)^2 / (16|D|), which is less than 1 for these values. Plotting the points of O might be helpful
		\item Suppose that \(D \in \left\{ -43,-67,-163 \right\} \). Prove that \(\mathcal{O}\) is not a Euclidean Domain with respect to any norm. %Apply same proof as D = -19 from text
	\end{itemize}
	These numbers are specially chosen because they are the only negative values of \(D\) that makes every ideal in \(\mathcal{O}\) principal.
\end{hw}

\subsection{Principal Ideal Domains}
\label{sub:principal_ideal_domains}

\begin{defn}[Princiapl Ideal Domain]
	A \textbf{Principal Ideal Domain} is an integral domain in which every ideal is principal.
\end{defn}
We have already shown that every Euclidean Domain is a Principal Ideal Domain. The converse does not hold. The biggest difference from a practicality angle is that while PIDs have gcds, there is no algorithm to compute them.

\begin{prop}
	Let \(R\) be a Principal Ideal Domain and let \(a,b \in R\) be nonzero. Let \(d\) be a generator for the principal ideal generated by \(a,b\). Then \(d = \textrm{gcd}(a,b)\) and can be written as an \(R\)-linear combination
	\begin{align*}
		d = ax+by
	\end{align*}
	for \(x,y \in R\). Finally, \(d\) is unique up to multiplication by a unit.
\end{prop}
Recall that maximal ideals are always prime ideals but the converse is not true in general. Fortunately, PIDs have enough structure so this holds.

\begin{prop}
	Every nonzero prime ideal in a Principal Ideal Domain is a maximal ideal.
\end{prop}
\begin{proof}% Try yourself

\end{proof}

\begin{cor}
	If \(R\) is any commutative ring such that \(R[x]\) is a PID, then \(R\) is necessarily a field.
\end{cor}
\begin{proof}

\end{proof}

We construct some definitions that help us distinguish PIDs and EDs.

\begin{defn}[Dedeking-Hasse Norm]
	Define \(N\) to be a \textbf{Dedekind-Hasse norm} if \(N\) is a positive norm and for every \(a,b \in R\) nonzero either \(a \in (b)\) or there exists \(s,t \in R\) with \(0 < N(sa-tb) < N(b)\) (that is, a nonzero element in the ideal \((a,b)\) with norm smaller than \(b\)).
\end{defn}
This is a weakening of the Euclidean condition. \(R\) is an ED with respect to a positive norm \(N\) if it is always possble to satisfy the above condition with \(s=1\).

\begin{prop}
	The integral domain \(R\) is a PID if and only if \(R\) has a Dedeking-Hasse norm.
\end{prop}

\begin{exmp}
	%Show that in Z[1+sqrt(-19) / 2] N()=a^2+ab+5b%2 is a Dedekind-Hasse norm
\end{exmp}

\subsection{Unique Factorization Domain}
\label{sub:unique_factorization_domain}

Unique Factorization Domains capture the idea that some rings admit a proper factorization on elements.

\begin{defn}[Reducibility and Primes]
	Let \(R\) be an integral domain
	\begin{itemize}
		\item Suppose \(r \in R\) is nonzero and not a unit. Then \(r\) is called \textbf{irreducible in \(R\)} if for all \(a,b \in R\), \(r = ab\) implies that either \(a\) or \(b\) is a unit. Otherwise, we say \(r\) is \textbf{reducible}.
		\item Let \(p \in R\) be nonzero. We say it is \textbf{prime in \(R\)} if the ideal \((p)\) is a prime ideal. An equivalent statement is that \(p\) is not a unit and if \(p\mid ab\), then \(p\mid a\) or \(p\mid b\).
		\item If \(a=ub\) for \(a,b \in R\) and \(u \in R\) a unit, then we say \(a\) and \(b\) are \textbf{associates}.
	\end{itemize}
\end{defn}

\begin{prop}
	In an integral domain, a prime element is always irreducible.
\end{prop}
The converse does not hold in general. However, in a PID, the converse does hold.

\begin{proof}% Try yourself, also try showing that in a PID it does hold

\end{proof}
This is also a useful tool to show a ring is not a PID.

\begin{defn}[Proper Factorization]
	Let \(a \in R\) be a nonzero nonunit. A \textbf{proper factorization} of \(a\) is a finite product \(a=p_1p_2\ldots p_n\), where \(p_i\) are not units of \(R\). If this exists, we say \(\left\{ p_i \right\} \) are \textbf{proper factors} of \(a\).
\end{defn}
Of course, an irreducible element has no proper factorizations.

\begin{defn}[Unique Factorization Domain]
	An integral domain \(R\) is a \textbf{unique factorization domain} if every nonzero, non-unit element has a proper factorization
	\begin{align*}
		r = p_1p_2\ldots p_n
	\end{align*}
	where \(\left\{ p_i \right\} \) are irreducible elements and unique up to associates and reordering.
\end{defn}
It turns out that primes are equivalent to irreducibles in a UFD as well.

\begin{prop}
	In a Unique Factorization Domain, a nonzero element is a prime if and only if it is an irreducible.
\end{prop}
We will also see that UFDs admit a greatest common divisor via its factorization

\begin{prop}
	Let \(a,b \in R\) be nonzero elements of a UFD \(R\) and suppose
	\begin{align*}
		a = up_1^{e_1}p_2^{e_2}\ldots p_n^{e_n}\\
		b= v p_1^{f_1}p_2^{f_2}\ldots p_n^{f_n}
	\end{align*}
	are prime factorizations with \(u,v\) units, primes \(p_1,p_2,\ldots,p_n\) distinct, and exponents \(e_i,f_i \geq 0\). Then the element
	\begin{align*}
		d = p_1^{\textrm{min}(e_1,f_1)}p_2^{\textrm{min}(e_2,f_2)}\ldots p_n^{\textrm{min}(e_n,f_n)}
	\end{align*}
	is a greatest common divisor of \(a\) and \(b\).
\end{prop}

\begin{hw}
	Let \(R\) be a UFD.
	\begin{enumerate}[(a).]
		\item Let \(b\) and \(a_1,\ldots,a_s\) be nonzero elements of \(R\). For \(d \in R\), show that
			\begin{align*}
				bd = gcd(ba_1,\ldots,ba_s) \iff d = gcd(a_1,\ldots,a_s)
			\end{align*}
		\item Let \(f(x) \in R[x]\) where \(f(x) = bf_1(x)\) for \(f_1(x)\) \textbf{primitive} (i.e. \(gcd(\text{coefficients of }f_1(x)) = 1_R\) ). Show that
			\begin{align*}
				b = gcd(\left\{ \text{coefficients of }f(x) \right\} ).
			\end{align*}
	\end{enumerate}
\end{hw}

This leads us to the full description of the structure of these domains.

\begin{thm}
	Every Principal Ideal Domain is a Unique Factorization Domain. Hence, every Euclidean Domain is a Unique Factorization Domain.
\end{thm}
\begin{proof}

\end{proof}

This forms a strict classification hierarchy by
\begin{align*}
	\textbf{Euclidean Domains} \subset \textbf{Principal Ideal Domains} \subset \textbf{Unique Factorization Domains} \subset \textbf{Integral Domains} \subset \textbf{commutative rings}
\end{align*}

\begin{cor}
	The integers \(\Z\) are a UFD.
\end{cor}

\begin{cor}
	Let \(R\) be a PID. Then there exists a multiplicative Dedekind-Hausse norm on \(R\).
\end{cor}

\end{document}

Abstract Algebra - Introductory/Algebra II/Notes/source/Lecture2 - ED_UFD_PID.tex


\providecommand{\main}{../..}
\documentclass[\main/thesis.tex]{subfiles}
\begin{document}

\section{Digit: the basic building block}\label{digit}

As the fundamental building block of numerals, we will devise a suitable
representation for digits in this section.

\subsection{Fin}

To represent a digit, we use a datatype conventionally called \textit{Fin}
which can be indexed to have an exact number of inhabitants.

\begin{lstlisting}
data Fin : ℕ → Set where
    zero : {n : ℕ} → Fin (suc n)
    suc  : {n : ℕ} (i : Fin n) → Fin (suc n)
\end{lstlisting}

The definition of {\lstinline|Fin|} looks the same as {\lstinline|ℕ|} on the term
level, but different on the type level. The index of a {\lstinline|Fin|} increases
with every {\lstinline|suc|}, and there can only be at most {\lstinline|n|} of
them before reaching {\lstinline|Fin (suc n)|}. In other words, {\lstinline|Fin n|}
has exactly \textit{n} inhabitants.

\subsection{Definition of \lstinline|Digit|}

{\lstinline|Digit|} is simply just a synonym for {\lstinline|Fin|}, indexed by
the number of digits {\lstinline|d|} of a system.
Since the same digit may represent different values in different numeral systems,
it is essential to make the context clear.

\begin{lstlisting}
Digit : ℕ → Set
Digit d = Fin d
\end{lstlisting}

Ordinary binary digits for example can thus be represented as:

\begin{lstlisting}
Binary : Set
Binary = Digit 2

零 : Binary
零 = zero

一 : Binary
一 = suc zero
\end{lstlisting}

\subsection{Digit Assignment}

\begin{center}
    \begin{adjustbox}{max width=\textwidth}
        \begin{tikzpicture}
            % the frame
            \path[clip] (-1, -1) rectangle (11, 2);
            % the spine
            \draw[ultra thick] (0,0) -- (1,0);
            \draw[ultra thick] (9,0) -- (10,0);
            % the body

            \foreach \i in {1,...,8} {
                \draw[ultra thick, fill=white] ({\i+0.05}, -0.2) rectangle ({\i+0.95}, +0.2);
            };

            % ticks
            \draw (0,0.2) -- (0,-0.2) node[below] {$0$};

            % labels
            \draw[->, ultra thick] (1.5,1) -- (1.5,0.5)
                node at (1.5, 1.3) {$o$};
            \draw[->, ultra thick] (8.5,1) -- (8.5,0.5)
                node at (8.5, 1.3) {$o+d$};
            \node at (5, -0.7) {{\lstinline|ℕ|}};
        \end{tikzpicture}
    \end{adjustbox}
\end{center}

Digits are assigned to {\lstinline|ℕ|} together with the offset {\lstinline|o|}
of a system, ranging from $ o $ to $ d + o $.

\begin{lstlisting}
Digit-toℕ : ∀ {d} → Digit d → ℕ → ℕ
Digit-toℕ x o = toℕ x + o
\end{lstlisting}
\footnote{
    {\lstinline|toℕ : ∀ {n} → Fin n → ℕ|}
    \newline\hspace*{4em} converts from {\lstinline|Fin n|} to {\lstinline|ℕ|}.
}

However, not all natural numbers can be converted to digits.
The value has to be in a certain range, between $ o $ and $ d + o $.
Values less than $ o $ are increased to $ o $.
Values greater than $ d + o $ are prohibited by the supplied upper-bound.

\begin{lstlisting}
Digit-fromℕ : ∀ {d}
    → (n o : ℕ)
    → (upper-bound : d + o ≥ n)
    → Digit (suc d)
Digit-fromℕ {d} n o upper-bound with n ∸ o ≤? d
Digit-fromℕ {d} n o upper-bound | yes p = fromℕ≤ (s≤s p)
Digit-fromℕ {d} n o upper-bound | no ¬p = contradiction p ¬p
    where   p : n ∸ o ≤ d
            p = start
                    n ∸ o
                ≤⟨ ∸n-mono o upper-bound ⟩
                    (d + o) ∸ o
                ≈⟨ m+n∸n≡m d o ⟩
                    d
                □
\end{lstlisting}
\footnote{
    {\lstinline|fromℕ≤ : ∀ {m n} → m < n → Fin n|}
    \newline\hspace*{4em} converts from {\lstinline|ℕ|} to {\lstinline|Fin n|} given the number is small enough.
}

\subsubsection{Properties of \lstinline|Digit|}

\begin{center}
    \begin{tikzpicture}
        \matrix (m) [matrix of nodes,row sep=6em,column sep=8em,minimum width=4em]
            {
                {\lstinline|ℕ|} & {\lstinline|Digit d|} \\
            };
        \path[-stealth]
            ($(m-1-1.east)+(0,+0.1)$)
                edge node [above] {{\lstinline|Digit-fromℕ|}}
                ($(m-1-2.west)+(0,+0.1)$)
            ($(m-1-2.west)+(0,-0.1)$)
                edge node [below] {{\lstinline|Digit-toℕ|}}
                ($(m-1-1.east)+(0,-0.1)$)
            ;
    \end{tikzpicture}
\end{center}

{\lstinline|Digit-fromℕ-toℕ|} states that the value of a natural number should
remain the same, after converting back and forth between {\lstinline|Digit|} and
{\lstinline|ℕ|}.

\begin{lstlisting}
Digit-fromℕ-toℕ : ∀ {d o}
    → (n : ℕ)
    → (lower-bound :     o ≤ n)
    → (upper-bound : d + o ≥ n)
    → Digit-toℕ (Digit-fromℕ {d} n o upper-bound) o ≡ n
Digit-fromℕ-toℕ {d} {o} n lb ub with n ∸ o ≤? d
Digit-fromℕ-toℕ {d} {o} n lb ub | yes q =
    begin
        toℕ (fromℕ≤ (s≤s q)) + o
    ≡⟨ cong (λ x → x + o) (toℕ-fromℕ≤ (s≤s q)) ⟩
        n ∸ o + o
    ≡⟨ m∸n+n≡m lb ⟩
        n
    ∎
Digit-fromℕ-toℕ {d} {o} n lb ub | no ¬q = contradiction q ¬q
    where   q : n ∸ o ≤ d
            q = +n-mono-inverse o (
                start
                    n ∸ o + o
                ≈⟨ m∸n+n≡m lb ⟩
                    n
                ≤⟨ ub ⟩
                    d + o
                □)
\end{lstlisting}
\footnote{
    {\lstinline|toℕ-fromℕ≤ : ∀ {m n} (m<n : m < n) → toℕ (fromℕ≤ m<n) ≡ m|}
    \newline\hspace*{4em} states that a number should remain the same after converting back and forth.
}

Digits have a upper-bound and a lower-bound after evaluated to \lstinline|ℕ|.

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
Digit-upper-bound : ∀ {d} → (o : ℕ) → (x : Digit d) → Digit-toℕ x o < d + o
Digit-upper-bound {d} o x = +n-mono o (bounded x)

Digit-lower-bound : ∀ {d} → (o : ℕ) → (x : Digit d) → Digit-toℕ x o ≥ o
Digit-lower-bound {d} o x = m≤n+m o (toℕ x)
\end{lstlisting}
\footnote{
    {\lstinline|bounded : ∀ {n} (i : Fin n) → toℕ i < n|}
    \newline\hspace*{4em} a property about the upper-bound of a {\lstinline|Fin n|}.
}

\subsection{Functions on \lstinline|Digit|}

\subsubsection{Increment}

To increment a digit, the digit must not be \textit{the greatest}.

\begin{lstlisting}
digit+1 : ∀ {d}
    → (x : Digit d)
    → (¬greatest : ¬ (Greatest x))
    → Digit d
digit+1 x ¬greatest =
    fromℕ≤ {suc (toℕ x)} (≤∧≢⇒< (bounded x) ¬greatest)
\end{lstlisting}
%
where {\lstinline|≤∧≢⇒< (bounded x) ¬greatest : suc (toℕ x) < d|}.

\begin{center}
    \begin{tikzpicture}
        \matrix (m) [matrix of nodes,row sep=6em,column sep=8em,minimum width=4em]
            {
                {\lstinline|Digit d|} & {\lstinline|Digit d|} \\
                {\lstinline|ℕ|} & {\lstinline|ℕ|} \\
            };
      \path[-stealth]
            (m-1-1)
                edge node [left] {{\lstinline|Digit-toℕ|}} (m-2-1)
                edge node [above] {{\lstinline|digit+1|}} (m-1-2)
            (m-2-1.east|-m-2-2)
                edge node [below] {{\lstinline|suc|}} (m-2-2)
            (m-1-2)
                edge node [right] {{\lstinline|Digit-toℕ|}} (m-2-2);
                % edge [dashed,-] (m-2-1);
    \end{tikzpicture}
\end{center}

A digit taking these two routes should result in the same {{\lstinline|ℕ|}}.

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
digit+1-toℕ : ∀ {d o}
    → (x : Digit d)
    → (¬greatest : ¬ (Greatest x))
    → Digit-toℕ (digit+1 x ¬greatest) o ≡ suc (Digit-toℕ x o)
digit+1-toℕ {d} {o} x ¬greatest =
    begin
        Digit-toℕ (digit+1 x ¬greatest) o
    ≡⟨ cong (λ w → w + o) (toℕ-fromℕ≤ (≤∧≢⇒< (bounded x) ¬greatest)) ⟩
        suc (Digit-toℕ x o)
    ∎
\end{lstlisting}

\subsubsection{Increase and then Subtract}

Increases a digit and then subtract it by $ n $.
This function is useful for implementing \textit{carrying}.
When the digit to increase is already the greatest,
it has to be subtracted from an amount (usually the base) after the increment.

\begin{lstlisting}
digit+1-n : ∀ {d}
    → (x : Digit d)
    → Greatest x
    → (n : ℕ)
    → n > 0
    → Digit d
digit+1-n x greatest n n>0 =
    fromℕ≤ (digit+1-n-lemma x greatest n n>0)
\end{lstlisting}

\begin{center}
    \begin{tikzpicture}
        \matrix (m) [matrix of nodes,row sep=4em,column sep=6em,minimum width=2em]
            {
                {\lstinline|Digit d|} & & {\lstinline|Digit d|} \\
                {\lstinline|ℕ|} & {\lstinline|ℕ|} & {\lstinline|ℕ|} \\
            };
      \path[-stealth]
            (m-1-1)
                edge node [left] {{\lstinline|Digit-toℕ|}} (m-2-1)
                edge node [above] {{\lstinline|digit+1-n|}} (m-1-3)
            (m-2-1)
                edge node [below] {{\lstinline|suc|}} (m-2-2)
            (m-2-2)
                edge node [below] {{\lstinline|∸ n|}} (m-2-3)
            (m-1-3)
                edge node [right] {{\lstinline|Digit-toℕ|}} (m-2-3);
    \end{tikzpicture}
\end{center}

A digit taking these two routes should result in the same {{\lstinline|ℕ|}}.

\begin{lstlisting}[basicstyle=\ttfamily\scriptsize]
digit+1-n-toℕ : ∀ {d o}
    → (x : Digit d)
    → (greatest : Greatest x)
    → (n : ℕ)
    → (n>0 : n > 0)
    → n ≤ d
    → Digit-toℕ (digit+1-n x greatest n n>0) o ≡ suc (Digit-toℕ x o) ∸ n
digit+1-n-toℕ {zero}  {o} () greatest n n>0 n≤d
digit+1-n-toℕ {suc d} {o} x greatest n n>0  n≤d =
    begin
        toℕ (digit+1-n x greatest n n>0) + o
    ≡⟨ cong (λ w → w + o) (toℕ-fromℕ≤ (digit+1-n-lemma x greatest n n>0)) ⟩
        suc (toℕ x) ∸ n + o
    ≡⟨ +-comm (suc (toℕ x) ∸ n) o ⟩
        o + (suc (toℕ x) ∸ n)
    ≡⟨ sym (+-∸-assoc o {suc (toℕ x)} {n} (
        start
            n
        ≤⟨ n≤d ⟩
            suc d
        ≈⟨ sym greatest ⟩
            suc (toℕ x)
        □)
    ⟩
        (o + suc (toℕ x)) ∸ n
    ≡⟨ cong (λ w → w ∸ n) (+-comm o (suc (toℕ x))) ⟩
        suc (toℕ x) + o ∸ n
    ∎)
\end{lstlisting}

\subsection{Special Digits}

\subsubsection{The Greatest Digit}

The greatest digit of a system is constructed by converting the index
{\lstinline|d|} to {\lstinline|Fin|}.

\begin{lstlisting}
greatest-digit : ∀ d → Digit (suc d)
greatest-digit d = fromℕ d
\end{lstlisting}
\footnote{
    {\lstinline|fromℕ : ∀ {n} → Fin n → ℕ|}
    \newline\hspace*{4em} construct the greatest possible {\lstinline|Fin n|}
    when given an index {\lstinline|n|}.
}

\paragraph{predicates}

We can see whether a digit is the greatest by converting it to {\lstinline|ℕ|}.
This predicate also comes with a decidable version.

\begin{lstlisting}
Greatest : ∀ {d} (x : Digit d) → Set
Greatest {d} x = suc (toℕ x) ≡ d

Greatest? : ∀ {d} (x : Digit d) → Dec (Greatest x)
Greatest? {d} x = suc (toℕ x) ≟ d
\end{lstlisting}

\paragraph{properties}

Converting from the greatest digit to {\lstinline|ℕ|} should result in
{\lstinline|d + o|}.

\begin{lstlisting}
greatest-digit-toℕ : ∀ {d o}
    → (x : Digit (suc d))
    → Greatest x
    → Digit-toℕ x o ≡ d + o
greatest-digit-toℕ {d} {o} x greatest = cancel-suc (
    begin
        suc (Digit-toℕ x o)
    ≡⟨ refl ⟩
        suc (toℕ x) + o
    ≡⟨ cong (λ w → w + o) greatest ⟩
        suc d + o
    ∎)
\end{lstlisting}

A digit is the greatest if and only if it is greater than or equal to all other
digits. This proposition is proven by induction on both of the compared digits.

\begin{lstlisting}
greatest-of-all : ∀ {d} (o : ℕ) → (x y : Digit d)
    → Greatest x
    → Digit-toℕ x o ≥ Digit-toℕ y o
greatest-of-all o zero    zero     refl     = ≤-refl
greatest-of-all o zero    (suc ()) refl
greatest-of-all o (suc x) zero     greatest
    = +n-mono o {zero} {suc (toℕ x)} z≤n
greatest-of-all o (suc x) (suc y)  greatest
    = s≤s (greatest-of-all o x y (cancel-suc greatest))
\end{lstlisting}

\subsubsection{The Carry}

A carry is a digit that is transferred to a more significant digit to compensate
the ``loss'' of the original digit.

The carry is defined as the greater of these two values:

\begin{itemize}
    \item the least digit of a system
    \item the digit that is assigned to $ 1 $
\end{itemize}

We define the carry as the greater number of $ 1 $ and $ o $.
In case that the least digit (which is determined by $o$) is assigned to $ 0 $,
rendering the carry useless.

\begin{lstlisting}
carry : ℕ → ℕ
carry o = 1 ⊔ o
\end{lstlisting}

The corresponding numeral of the carry is constructed by converting
{\lstinline|carry o|} to {\lstinline|Digit|}.

\begin{lstlisting}
carry-digit : ∀ d o → 2 ≤ suc d + o → Digit (suc d)
carry-digit d o proper =
    Digit-fromℕ
        (carry o)
        o
        (carry-upper-bound {d} proper)
\end{lstlisting}

\paragraph{properties}

The value of the carry should remain the same after convertions.

\begin{lstlisting}
carry-digit-toℕ : ∀ d o
    → (proper : 2 ≤ suc (d + o))
    → Digit-toℕ (carry-digit d o proper) o ≡ carry o
carry-digit-toℕ d o proper
    = Digit-fromℕ-toℕ
        (carry o)
        (m≤n⊔m o 1)
        (carry-upper-bound {d} proper)
\end{lstlisting}

The carry also have an upper-bound and a lower-bound,
similar to that of {\lstinline|Digit|}.

\begin{lstlisting}
carry-lower-bound : ∀ {o} → carry o ≥ o
carry-lower-bound {o} = m≤n⊔m o 1

carry-upper-bound : ∀ {d o} → 2 ≤ suc d + o → carry o ≤ d + o
carry-upper-bound {d} {zero}  proper = ≤-pred proper
carry-upper-bound {d} {suc o} proper = n≤m+n d (suc o)
\end{lstlisting}

\end{document}


\section{Function extensionality}
\label{chap:funext}

\subsection{Equivalent forms of function extensionality}
\begin{defn}
  The \define{axiom of function extensionality} asserts that for any type family $B$ over $A$, and any two dependent functions $f,g:\prd{x:A}B(x)$, the canonical map
  \begin{equation*}
    \mathsf{htpy\usc{}eq}:(f=g)\to (f\htpy g)
  \end{equation*}
  is an equivalence.
\end{defn}

In other words, the axiom of function extensionality asserts that any an identification of any two dependent functions is equivalent to an identification of all their values. By \cref{thm:id_fundamental} there are three equivalent ways of asserting function extensionality. In the following theorem we state one further equivalent condition.

\begin{thm}\label{thm:funext_wkfunext}
The axiom of function extensionality is equivalent to any of the following statements:\index{function extensionality}
\begin{enumerate}
\item
  The principle of \define{homotopy induction}\index{homotopy induction}:
  for any type family $B$ over $A$, any dependent function $f:\prd{x:A}B(x)$, and any family of types $P(g,H)$ indexed by $g:\prd{x:A}B(x)$ and $H:f\htpy g$, the evaluation function
\begin{equation*}
\Big(\prd{g:\prd{x:A}B(x)}{H:f\htpy g}P(g,H)\Big)\to P(f,\mathsf{htpy\usc{}refl}_f)
\end{equation*}
given by $s\mapsto s(f,\mathsf{htpy\usc{}refl}_f)$ has a section.
\item For any type family $B$ over $A$ and any dependent function $f:\prd{x:A}B(x)$, the total space
  \begin{equation*}
    \sm{g:\prd{x:A}B(x)}f\htpy g
  \end{equation*}
  is contractible.
\item The \define{weak function extensionality principle}\index{weak function extensionality} holds: For every type family $B$ over $A$ one has\index{contractible!weak function extensionality}
\begin{equation*}
\Big(\prd{x:A}\iscontr(B(x))\Big)\to\iscontr\Big(\prd{x:A}B(x)\Big).
\end{equation*}
\end{enumerate}
\end{thm}

\begin{proof}
The fact that function extensionality is equivalent to (i) and (ii) follows directly from \cref{thm:id_fundamental}.

To show that function extensionality implies weak function extensionality, suppose that each $B(a)$ is contractible with center of contraction $c(a)$ and contraction $C_a:\prd{y:B(a)}c(a)=y$. Then we take $c\defeq \lam{a}c(a)$ to be the center of contraction of $\prd{x:A}B(x)$. To construct the contraction we have to define a term of type
\begin{equation*}
\prd{f:\prd{x:A}B(x)}c=f.
\end{equation*}
Let $f:\prd{x:A}B(x)$. By function extensionality we have a map $(c\htpy f)\to (c=f)$, so it suffices to construct a term of type $c\htpy f$. Here we take $\lam{a}C_a(f(a))$. This completes the proof that function extensionality implies weak function extensionality.

To prove function extensionality from weak function extensionality, observe that it suffices by \cref{thm:id_fundamental} to show that
\begin{equation*}
\sm{g:\prd{x:A}B(x)}f\htpy g
\end{equation*}
is contractible.

Since the type $\sm{b:B(x)}f(x)=b$ is contractible for each $x:X$, it follows by our assumption of weak function extensionality that the type $\prd{x:A}\sm{b:B(x)}f(x)=b$ is contractible. By \cref{ex:contr_retr} it therefore suffices to show that
\begin{equation*}
\sm{g:\prd{x:A}B(x)}f\htpy g
\end{equation*}
is a retract of the type $\prd{x:A}\sm{b:B(x)}f(x)=b$. We have the functions
\begin{align*}
i & \defeq \ind{\Sigma}(\lam{g}{H}{x}\pairr{g(x),H(x)}) \\
r & \defeq \lam{p}\pairr{\lam{x}\proj 1(p(x)),\lam{x}\proj 2(p(x))}.
\end{align*}
It remains to show that $r\circ i\htpy \idfunc$. This homotopy is constructed by $\Sigma$-induction. Let $g:\prd{x:A}B(x)$ and let $H:f\htpy g$. Then we have
\begin{align*}
r(i(g,H)) & \jdeq r(\lam{x}\pairr{g(x),H(x)}) \\
& \jdeq \pairr{\lam{x}g(x),\lam{x}H(x)} \\
& \jdeq \pairr{g,H}.
\end{align*}
In other words, the homotopy $r\circ i\htpy \idfunc$ is given by $\ind{\Sigma}(\lam{g}{H}\refl{(\pairr{g,H})})$.
\end{proof}

From now on we will assume that the function extensionality axiom holds.

\begin{defn}
  Consider a type family $B$ over $A$ and let $f,g:\prd{x:A}B(x)$. We define
  \begin{equation*}
    \mathsf{eq\usc{}htpy}: (f\htpy g) \to (f = g)
  \end{equation*}
  to be the inverse of $\mathsf{htpy\usc{}eq}$.
\end{defn}

As a first application of the function extensionality axiom we generalize the weak function extensionality axiom to $k$-types.

\begin{thm}\label{thm:trunc_pi}
Assume function extensionality. Then for any type family $B$ over $A$ one has\index{truncated}
\begin{equation*}
\Big(\prd{x:A}\istrunc{k}(B(x))\Big)\to \istrunc{k}\Big(\prd{x:A}B(x)\Big).
\end{equation*}
\end{thm}

\begin{proof}
The theorem is proven by induction on $k\geq -2$. The base case is just the weak function extensionality principle, which was shown to follow from function extensionality in \cref{thm:funext_wkfunext}.

For the inductive hypothesis, assume that the $k$-types are closed under dependent function types. Assume that $B$ is a family of $(k+1)$-types. By function extensionality, the type $f=g$ is equivalent to $f\htpy g$ for any two dependent functions $f,g:\prd{x:A}B(x)$. Now observe that $f\htpy g$ is a dependent product of $k$-types, and therefore it is an $k$-type by our inductive hypotheses. Therefore, it follows by \cref{thm:ktype_eqv} that $f=g$ is an $k$-type, and hence that $\prd{x:A}B(x)$ is an $(k+1)$-type.
\end{proof}

\begin{cor}\label{cor:funtype_trunc}
Suppose $B$ is a $k$-type. Then $A\to B$ is also a $k$-type, for any type $A$.
\end{cor}

\subsection{The type theoretic principle of choice}

The \define{type theoretic principle of choice}\index{type theoretic choice|textbf} asserts that \emph{$\Pi$ distributes over $\Sigma$}. More precisely, it asserts that the canonical map
\begin{equation*}
  \mathsf{choice}:\Big(\prd{x:A}\sm{y:B(x)}C(x,y)\Big)\to \Big(\sm{f:\prd{x:A}B(x)}\prd{x:A}C(x,f(x))\Big)
\end{equation*}
given by $\lam{h}(\proj 1(h(x)),\proj 2(h(x)))$, is an equivalence. In order to see this as a principle of choice, one can view the left hand side as the type of functions $h$ that pick for every $x:A$ a term $y:B(x)$ equipped with a term of type $C(x,y)$. The function $\mathsf{choice}$ then constructs a dependent function $f:\prd{x:A}B(x)$ equipped with a term of type $\prd{x:A}C(x,f(x))$. In this section we show that the map $\mathsf{choice}$ is an equivalence, and we use this to characterize the identity of any dependent function type $\prd{x:A}B(x)$ in terms of any characterization of the identity types of the individual types $B(x)$.

\begin{thm}\label{thm:choice}
Consider a family of types $C(x,y)$ indexed by $x:A$ and $y:B(x)$. Then the map
\begin{equation*}
  \mathsf{choice}:\Big(\prd{x:A}\sm{y:B(x)}C(x,y)\Big)\to \Big(\sm{f:\prd{x:A}B(x)}\prd{x:A}C(x,f(x))\Big)
\end{equation*}
given by $\lam{h}(\proj 1(h(x)),\proj 2(h(x)))$ is an equivalence.
\end{thm}

\begin{proof}
  We define the map
  \begin{equation*}
    \mathsf{choice}^{-1}:\Big(\sm{f:\prd{x:A}B(x)}\prd{x:A}C(x,f(x))\Big)\to\Big(\prd{x:A}\sm{y:B(x)}C(x,y)\Big)
  \end{equation*}
  by $\lam{(f,g)}{x}(f(x),g(x))$. Then we have to construct homotopies
  \begin{equation*}
    \mathsf{choice}\circ\mathsf{choice}^{-1}\htpy\idfunc,\qquad\text{and}\qquad
    \mathsf{choice}^{-1}\circ\mathsf{choice}\htpy\idfunc.
  \end{equation*}
  For the first homotopy it suffices to construct an identification
  \begin{equation*}
    \mathsf{choice}(\mathsf{choice}^{-1}(f,g))=(f,g)
  \end{equation*}
  for any $f:\prd{x:A}B(x)$ and any $g:\prd{x:A}C(x,f(x))$. We compute the left-hand side as follows:
  \begin{align*}
    \mathsf{choice}(\mathsf{choice}^{-1}(f,g))
    & \jdeq \mathsf{choice}(\lam{x}(f(x),g(x))) \\
    & \jdeq (\lam{x}f(x),\lam{x}g(x)).
  \end{align*}
  By the $\eta$-rule it follows that $f\jdeq \lam{x}f(x)$ and $g\jdeq\lam{x}g(x)$. Therefore we have the identification
  \begin{equation*}
    \refl{(f,g)}:\mathsf{choice}(\mathsf{choice}^{-1}(f,g))=(f,g).
  \end{equation*}
  This completes the construction of the first homotopy.

  For the second homotopy we have to construct an identification
  \begin{equation*}
    \mathsf{choice}^{-1}(\mathsf{choice}(h))=h
  \end{equation*}
  for any $h:\prd{x:A}\sm{y:B(x)}C(x,y)$. We compute the left-hand side as follows:
  \begin{align*}
    \mathsf{choice}^{-1}(\mathsf{choice}(h))
    & \jdeq \mathsf{choice}^{-1}(\lam{x}\proj 1(h(x)),(\lam{x}\proj 2(h(x)))) \\
    & \jdeq \lam{x}(\proj 1(h(x)),\proj 2(h(x)))
  \end{align*}
  However, it is \emph{not} the case that $(\proj 1(h(x)),\proj 2(h(x)))\jdeq h(x)$ for any $h:\prd{x:A}\sm{y:B(x)}C(x,y)$. Nevertheless, we have the identification
  \begin{equation*}
    \mathsf{eq\usc{}pair}(\refl{},\refl{}):(\proj 1(h(x)),\proj 2(h(x)))= h(x).
  \end{equation*}
  Therefore we obtain the required homotopy by function extensionality:
  \begin{equation*}
    \lam{h}\mathsf{eq\usc{}htpy}(\lam{x}\mathsf{eq\usc{}pair}(\refl{\proj 1(h(x))},\refl{\proj 2(h(x))}):\mathsf{choice}^{-1}\circ\mathsf{choice}\htpy\idfunc.\qedhere
  \end{equation*}
\end{proof}

\begin{cor}
For type $A$ and any type family $C$ over $B$, the map
\begin{equation*}
\Big(\sm{f:A\to B} \prd{x:A}C(f(x))\Big)\to\Big(A\to\sm{y:B}C(x)\Big)
\end{equation*}
given by $\lam{(f,g)}{x}(f(x),g(x))$ is an equivalence.
\end{cor}

\subsection{Universal properties}
The function extensionality principle allows us to prove \emph{universal properties}: characterizations of all maps out of (or into) a given type. Universal properties characterize a type up to equivalence. In the following theorem we prove the universal property of dependent pair types.

\begin{thm}
Let $B$ be a type family over $A$, and let $X$ be a type. Then the map
\begin{equation*}
\mathsf{ev\usc{}pair}:\Big(\Big(\sm{x:A}B(x)\Big)\to X\Big)\to \Big(\prd{x:A}(B(x)\to X)\Big)
\end{equation*}
given by $f\mapsto\lam{a}{b}f(a,b)$ is an equivalence.
\end{thm}

\begin{proof}
The map in the converse direction is simply
\begin{equation*}
\ind{\Sigma} : \Big(\prd{x:A}(B(x)\to X)\Big)\to \Big(\Big(\sm{x:A}B(x)\Big)\to X\Big).
\end{equation*}
By the computation rules for $\Sigma$-types we have
\begin{equation*}
\lam{f}\refl{f}:\mathsf{ev\usc{}pair}\circ\ind{\Sigma}\htpy\idfunc
\end{equation*}

To show that $\ind{\Sigma}\circ\mathsf{ev\usc{}pair}\htpy\idfunc$ we will also apply function extensionality. Thus, it suffices to show that $\ind{\Sigma}(\lam{x}{y}f((x,y)))=f$. We apply function extensionality again, so it suffices to show that
\begin{equation*}
\prd{t:\sm{x:A}B(x)}\ind{\Sigma}\big(\lam{x}{y}f((x,y))\big)(t)=f(t).
\end{equation*}
We obtain this homotopy by another application of $\Sigma$-induction.
\end{proof}

\begin{cor}\label{cor:times_up_out}
Let $A$, $B$, and $X$ be types. Then the map
\begin{equation*}
\mathsf{ev\usc{}pair}: (A\times B \to X)\to (A\to (B\to X))
\end{equation*}
given by $f\mapsto\lam{a}{b}f((a,b))$ is an equivalence.
\end{cor}

The universal property of identity types is sometimes called the \emph{type theoretical Yoneda lemma}: families of maps out of the identity type are uniquely determined by their action on the reflexivity identification.

\begin{thm}\label{thm:yoneda}
Let $B$ be a type family over $A$, and let $a:A$. Then the map
\begin{equation*}
\mathsf{ev\usc{}refl}:\Big(\prd{x:A} (a=x)\to B(x)\Big)\to B(a)
\end{equation*}
given by $\lam{f} f(a,\refl{a})$ is an equivalence.
\end{thm}

\begin{proof}
The inverse $\varphi$ is defined by path induction, taking $b:B(a)$ to the function $f$ satisfying $f(a,\refl{a})\jdeq b$. It is immediate that $\mathsf{ev\usc{}refl}\circ\varphi\htpy \idfunc$.

To see that $\varphi\circ \mathsf{ev\usc{}refl}\htpy\idfunc$, let $f:\prd{x:A}(a=x)\to B(x)$. To show that $\varphi(f(a,\refl{a}))=f$ we use function extensionality (twice), so it suffices to show that
\begin{equation*}
\prd{x:A}{p:a=x} \varphi(f(a,\refl{a}),x,p)=f(x,p).
\end{equation*}
This follows by path induction on $p$, since $\varphi(f(a,\refl{a}),a,\refl{a})\jdeq f(a,\refl{a})$.
\end{proof}

\subsection{Composing with equivalences}

We show in this section that a map $f:A\to B$ is an equivalence if and only if for any type $X$ the precomposition map
\begin{equation*}
\blank\circ f: (B\to X)\to (A\to X)
\end{equation*}
is an equivalence. Moreover, we will show in \cref{ex:equiv_precomp} that the `dependent version' of this statement also holds: a map $f:A\to B$ is an equivalence if and only if for any type family $P$ over $B$, the precomposition map
\begin{equation*}
\blank\circ f: \Big(\prd{y:B}P(y)\Big)\to\Big(\prd{x:A}P(f(x))\Big)
\end{equation*}
is an equivalence.

\begin{thm}\label{ex:equiv_precomp}
For any map $f:A\to B$, the following are equivalent:
\begin{enumerate}
\item $f$ is an equivalence.
\item For any type family $P$ over $B$ the map
\begin{equation*}
\Big(\prd{y:B}P(y)\Big)\to\Big(\prd{x:A}P(f(x))\Big)
\end{equation*}
given by $h\mapsto h\circ f$ is an equivalence.
\item For any type $X$ the map
\begin{equation*}
(B\to X)\to (A\to X)
\end{equation*}
given by $g\mapsto g\circ f$ is an equivalence.
\end{enumerate}
\end{thm}

\begin{proof}
To show that (i) implies (ii), we first recall from \cref{lem:coherently-invertible} that any equivalence is also coherently invertible. Therefore $f$ comes equipped with
\begin{align*}
g & : B \to A\\
G & : f\circ g \htpy \idfunc[B] \\
H & : g\circ f \htpy \idfunc[A] \\
K & : G\cdot f \htpy f\cdot H.
\end{align*}
Then we define the inverse of $\blank\circ f$ to be the map
\begin{equation*}
\varphi:\Big(\prd{x:A}P(f(x))\Big)\to\Big(\prd{y:B}P(y)\Big)
\end{equation*}
given by $h\mapsto \lam{y}\mathsf{tr}_P(G(y),h(g(y)))$.

To see that $\varphi$ is a section of $\blank\circ f$, let $h:\prd{x:A}P(f(x))$. By function extensionality it suffices to construct a homotopy $\varphi(h)\circ f\htpy h$. In other words, we have to show that
\begin{equation*}
\mathsf{tr}_P(G(f(x)),h(g(f(x)))=h(x)
\end{equation*}
for any $x:A$. Now we use the additional homotopy $K$ from our assumption that $f$ is a half-adjoint equivalence. Since we have $K(x):G(f(x))=\ap{f}{H(x)}$ it suffices to show that
\begin{equation*}
\mathsf{tr}_P(\ap{f}{H(x)},hgf(x))=h(x).
\end{equation*}
A simple path-induction argument yields that
\begin{equation*}
\mathsf{tr}_P(\ap{f}{p})\htpy \mathsf{tr}_{P\circ f}(p)
\end{equation*}
for any path $p:x=y$ in $A$, so it suffices to construct an identification
\begin{equation*}
\mathsf{tr}_{P\circ f}(H(x),hgf(x))=h(x).
\end{equation*}
We have such an identification by $\apd{h}{H(x)}$.

To see that $\varphi$ is a retraction of $\blank\circ f$, let $h:\prd{y:B}P(y)$. By function extensionality it suffices to construct a homotopy $\varphi(h\circ f)\htpy h$. In other words, we have to show that
\begin{equation*}
\mathsf{tr}_P(G(y),hfg(y))=h(y)
\end{equation*}
for any $y:B$. We have such an identification by $\apd{h}{G(y)}$. This completes the proof that (i) implies (ii).

Note that (iii) is an immediate consequence of (ii), since we can just choose $P$ to be the constant family $X$.

It remains to show that (iii) implies (i). Suppose that
\begin{equation*}
\blank\circ f:(B\to X)\to (A\to X)
\end{equation*}
is an equivalence for every type $X$. Then its fibers are contractible by \cref{thm:contr_equiv}. In particular, choosing $X\jdeq A$ we see that the fiber
\begin{equation*}
\fib{\blank\circ f}{\idfunc[A]}\jdeq \sm{h:B\to A}h\circ f=\idfunc[A]
\end{equation*}
is contractible. Thus we obtain a function $h:B\to A$ and a homotopy $H:h\circ f\htpy\idfunc[A]$ showing that $h$ is a retraction of $f$. We will show that $h$ is also a section of $f$. To see this, we use that the fiber
\begin{equation*}
\fib{\blank\circ f}{f}\jdeq \sm{i:B\to B} i\circ f=f
\end{equation*}
is contractible (choosing $X\jdeq B$).
Of course we have $(\idfunc[B],\refl{f})$ in this fiber. However we claim that there also is an identification $p:(f\circ h)\circ f=f$, showing that $(f\circ h,p)$ is in this fiber, because
\begin{align*}
(f\circ h)\circ f & \jdeq f\circ (h\circ f) \\
& = f\circ \idfunc[A] \\
& \jdeq f
\end{align*}
Now we conclude by the contractibility of the fiber that $(\idfunc[B],\refl{f})=(f\circ h,p)$. In particular we obtain that $\idfunc[B]=f\circ h$, showing that $h$ is a section of $f$.
\end{proof}

\begin{exercises}
\item Show that the functions
\begin{align*}
\mathsf{htpy\usc{}inv} & : (f \htpy g) \to (g \htpy f) \\
\mathsf{htpy\usc{}concat}(H) & : (g \htpy h) \to (f \htpy h) \\
\mathsf{htpy\usc{}concat}'(K) & : (f \htpy g) \to (f \htpy h)
\end{align*}
are equivalences for every $f,g,h : \prd{x:A}B(x)$. Here, $\mathsf{htpy\usc{}concat}'(K)$ is the function defined by $H\mapsto \ct{H}{K}$.
\item \label{ex:isprop_istrunc}
\begin{subexenum}
\item Show that for any type $A$ the type $\iscontr(A)$ is a proposition. %There's an easy proof using double singleton induction. This is a nice application of weak funext.
\item Show that for any type $A$ and any $k\geq-2$, the type $\istrunc{k}(A)$ is a proposition.
\end{subexenum}
\item \label{lem:postcomp_equiv}
Let $f:X\to Y$ be a map. Show that the following are equivalent:
\begin{enumerate}
\item $f$ is an equivalence.\index{equivalence!post-composition|textit}
\item The map $f\circ\blank : X^A\to Y^A$ is an equivalence for every type $A$.
\end{enumerate}
\item \label{ex:isprop_isequiv}Let $f:A\to B$ be a function.
\begin{subexenum}
\item Show that if $f$ is an equivalence, then the type $\sm{g:B\to A}f\circ g\htpy \idfunc$ of sections of $f$ is contractible.
\item Show that if $f$ is an equivalence, then the type $\sm{h:B\to A}h\circ f\htpy \idfunc$ of retractions of $f$ is contractible.
\item Show that $\isequiv(f)$ is a proposition.
\item Use \cref{ex:prop_equiv,ex:isprop_istrunc} to show that $\isequiv(f)\eqvsym \iscontr(f)$.
\end{subexenum}
Conclude that $\eqv{A}{B}$ is a subtype of $A\to B$, and in particular that the map $\proj 1 : (\eqv{A}{B})\to (A\to B)$ is an embedding.
\item \label{ex:prop_equiv}
\begin{subexenum}
\item \label{ex:equiv-bi-implication}Let $P$ and $Q$ be propositions. Show that
\begin{equation*}
\eqv{(P\leftrightarrow Q)}{(\eqv{P}{Q})}.
\end{equation*}
\item Show that $P$ is a proposition if and only if $P\to P$ is contractible.
\end{subexenum}
\item Show that $\mathsf{path\usc{}split}(f)$ and $\mathsf{half\usc{}adj}(f)$ are propositions for any map $f:A\to B$. Conclude that we have equivalences
  \begin{equation*}
    \isequiv(f) \eqvsym \mathsf{path\usc{}split}(f) \eqvsym \mathsf{half\usc{}adj}(f).
  \end{equation*}
%\item Let $B$ and $C$ be type families over $A$, suppose that $p:\id{a}{a'}$ in $A$, and consider two functions $f:B(a)\to C(a)$ and $g:B(a')\to C(a')$.
%\begin{subexenum}
%\item Show that the square
%\begin{equation*}
%\begin{tikzcd}
%B(a) \arrow[r,"f"] \arrow[d,swap,"\mathsf{tr}_B(p)"] & C(a) \arrow[d,"\mathsf{tr}_C(p)"] \\
%B(a') \arrow[r,swap,"g"] & C(a')
%\end{tikzcd}
%\end{equation*}
%commutes for every homotopy $H:\mathsf{tr}_{B(x)\to C(x)}(p,f)\htpy g$. In other words, construct a function of type
%\begin{equation*}
%\Big(\mathsf{tr}_{B(x)\to C(x)}(p,f)\htpy g\Big)\to \Big(\mathsf{tr}_C(p)\circ f\htpy g\circ \mathsf{tr}_B(p)\Big)
%\end{equation*}
%\item Show that this map is an equivalence.
%\end{subexenum}
\item \label{ex:idfunc_autohtpy}Construct for any type $A$ an equivalence
\begin{equation*}
\eqv{\mathsf{is\usc{}invertible}(\idfunc[A])}{\Big(\idfunc[A]\htpy\idfunc[A]\Big)}.
\end{equation*}
Note: We will use this fact in \cref{ex:is_invertible_id_S1} to show that there
are types for which $\mathsf{is\usc{}invertible}(\idfunc[A])\not\eqvsym\isequiv(\idfunc[A])$.
\item
\begin{subexenum}
\item Show that the type
\begin{equation*}
\prd{t:\emptyt}P(t)
\end{equation*}
is contractible for any $P:\emptyt\to \UU$.
\item Show that for any type $X$ the following are equivalent:
  \begin{enumerate}
  \item the unique map $\emptyt \to X$ is an equivalence.
  \item The type $Y^X$ is contractible for any type $Y$.
  \end{enumerate}
\end{subexenum}
\item Consider two types $A$ and $B$.
\begin{subexenum}
\item Show that the map
\begin{equation*}
  \mathsf{ev\usc{}inl\usc{}inr}: \Big(\prd{t:A+B}P(t)\Big) \to
  \Big(\prd{x:A}P(\inl(x))\Big)\times\Big(\prd{y:B}P(\inr(y))\Big)
\end{equation*}
given by $f\mapsto (f\circ\inl,f\circ\inr)$ is an equivalence.
\item Show that the following are equivalent for any type $X$ equipped with maps $i:A\to X$ and $j:B\to X$:
  \begin{enumerate}
  \item The map $\mathsf{ind\usc{}coprod}(i,j) :A+B\to X$ is an equivalence.
  \item For any type $Y$, the map
    \begin{equation*}
      \lam{f}(f\circ i,f\circ j):(X\to Y)\to (A\to Y)\times (B \to Y)
    \end{equation*}
    is an equivalence.
  \end{enumerate}
\end{subexenum}
\item
\begin{subexenum}
\item Show that the map
\begin{equation*}
\Big(\prd{t:\unit}P(t)\Big)\to P(\ttt)
\end{equation*}
given by $\lam{f}f(\ttt)$ is an equivalence.
\item Consider a type $X$ equipped with a point $x:X$. Show that the following are equivalent:
\begin{enumerate}
\item The map $\mathsf{ind\usc{}unit}(x):\unit\to X$ is an equivalence (i.e., $X$ is contractible).
\item For any type $Y$ the map
\begin{equation*}
\lam{f}f(x) : (X\to Y)\to Y
\end{equation*}
is an equivalence.
\end{enumerate}
\end{subexenum}
\item \label{ex:sec_retr}Consider a commuting triangle
\begin{equation*}
\begin{tikzcd}[column sep=tiny]
A \arrow[rr,"h"] \arrow[dr,swap,"f"] & & B \arrow[dl,"g"] \\
& X
\end{tikzcd}
\end{equation*}
with $H:f\htpy g\circ h$.
\begin{subexenum}
\item Show that if $h$ has a section, then $\mathsf{sec}(g)$ is a retract of $\mathsf{sec}(f)$.
\item Show that if $g$ has a retraction, then $\mathsf{retr}(h)$ is a retract of $\mathsf{sec}(f)$.
\end{subexenum}
\item \label{ex:equiv_pi}Let $e_i:\eqv{A_i}{B_i}$ be an equivalence for every $i:I$. Show that the map
\begin{equation*}
\lam{f}{i}e_i\circ f:\Big(\prd{i:I}A_i\Big)\to\Big(\prd{i:I}B_i\Big)
\end{equation*}
is an equivalence.
\item \label{ex:triangle_fib}Consider a diagram of the form
\begin{equation*}
\begin{tikzcd}[column sep=tiny]
A \arrow[dr,swap,"f"] & & B \arrow[dl,"g"] \\
& X
\end{tikzcd}
\end{equation*}
\begin{subexenum}
\item Show that the type $\sm{h:A\to B} f\htpy g\circ h$ is equivalent to the type of families of maps
\begin{equation*}
\prd{x:X}\fib{f}{x}\to\fib{g}{x}.
\end{equation*}
Note: In \cref{thm:pb_fibequiv_complete} we will characterize the type of families of equivalences $\prd{x:X}\fib{f}{x}\simeq\fib{g}{x}$.
\item Show that the type $\sm{h:\eqv{A}{B}} f\htpy g\circ h$ is equivalent to the type of families of equivalences
\begin{equation*}
\prd{x:X}\fib{f}{x}\eqvsym\fib{g}{x}.
\end{equation*}
\end{subexenum}
\item \label{ex:sq_fib}Consider a diagram of the form
\begin{equation*}
\begin{tikzcd}
A \arrow[d,swap,"f"] & B \arrow[d,"g"] \\
X \arrow[r,swap,"h"] & Y.
\end{tikzcd}
\end{equation*}
Show that the type $\sm{i:A\to B}h\circ f\htpy g\circ i$ is equivalent to the type of families of maps
\begin{equation*}
\prd{x:X}\fib{f}{x}\to\fib{g}{h(x)}.
\end{equation*}
%\item Show that the type $\sm{i:\eqv{A}{B}}h\circ f\htpy g\circ i$ is equivalent to the type of families of equivalences
%\begin{equation*}
%\prd{x:X}\fib{f}{x}\eqvsym\fib{g}{h(x)}.
%\end{equation*}
\item \label{ex:iso_equiv}Let $A$ and $B$ be sets. Show that type type $\eqv{A}{B}$ of equivalences from $A$ to $B$ is equivalent to the type $A\cong B$ of \define{isomorphisms} from $A$ to $B$, i.e., the type of quadruples $(f,g,H,K)$ consisting of
\begin{align*}
f & : A\to B \\
g & : B\to A \\
H & : f\circ g = \idfunc[B] \\
K & : g\circ f = \idfunc[A].
\end{align*}
\item \label{ex:pi_sec}Let $B$ be a type family over $A$, and consider the maps
  \begin{align*}
    \proj 1 & : \sm{x:A} B(x)\to A \\
    \proj1 \circ \blank & : \Big(\sm{x:A} B(x)\Big)^A \to A^A.
  \end{align*}
  Construct equivalences
  \begin{equation*}
    \Big(\prd{x:A}B(x)\Big) \eqvsym \mathsf{sec}(\proj 1) \eqvsym \fib{\proj 1 \circ\blank}{\idfunc[A]}.
  \end{equation*}
\item Suppose that $A:I\to \UU$ is a type family over a set $I$ with decidable equality. Show that
  \begin{equation*}
    \Big(\prd{i:I}\iscontr(A_i)\Big)\leftrightarrow \iscontr\Big(\prd{i:I}A_i\Big).
  \end{equation*}
\item Construct equivalences
  \begin{align*}
    \mathsf{Fin}(n^m) & \simeq (\mathsf{Fin}(m)\to\mathsf{Fin}(n)) \\
    \mathsf{Fin}(n!) & \simeq (\mathsf{Fin}(n)\simeq\mathsf{Fin}(n)).
  \end{align*}
\end{exercises}


% Copyright 2018 Markus J. Pflaum, licensed under GNU FDL v1.3
% main author:
%   Markus J. Pflaum
%
\section{Unbounded linear operators}
\label{sec:unbounded-linear-operators}
%
\para
In this section let $\banachV,\banachW$ always denote Banach spaces over the field
$\fldK =\R$ or $\fldK=\C$. The symbols $\hilbertH$, $\hilbertH_1$, ... will always stand for Hilbert spaces over $\fldK$.

\begin{definition}
  By an \emph{unbounded $\fldK$-linear operator} or shortly by an
  \emph{unbounded operator} from $\banachV$ to $\banachW$ we understand a linear map
  $A: \Dom (A) \to \banachW$ defined on a $\fldK$-linear subspace $\Dom (A)\subset \banachV$.
  As usual, $\Dom (A)$ is called the \emph{domain} of the operator $A$.
  The space of unbounded $\fldK$-linear operators from $V$ to $W$ will be
  denoted $\linOps_\fldK (V,W)$ or just  $\linOps (V,W)$.
\end{definition}

\begin{remark}
  In this work, the term ``unbounded'' is meant in the sense of
  ``not necessarily bounded''. Sometimes we just say
  \emph{linear operator} or even only \emph{operator} instead of
  ``unbounded linear operator''.
\end{remark}

\para Observe that besides the domain $\Dom (A)$ of an unbounded operator
  $A \in \linOps (\banachV,\banachW)$ the
  \emph{kernel}
  \[\Ker(A)=\big\{ v \in \banachV \bigmid Av = 0 \big\}\subset\banachV \ , \]
  the \emph{image}
  \[\Img(A)=\big\{ w \in \banachW \bigmid \exists v\in \Dom (A): w = Av \big\}
  \subset \banachW \ , \]
  and the \emph{graph}
  \[\Graph(A)=\big\{ (v,w) \in \Dom (A) \times \banachW \bigmid
    w = Av \big\} \subset \banachV \times \banachW \]
  of $A$  are all linear subspaces. We will frequently make use of this.

\begin{definition}
  An unbounded operator $A\in \linOps (\banachV,\banachW)$ is called
  \emph{densely defined} if $\Dom (A)$ is dense in $\banachV$,
  and \emph{closed} if the graph $\Gr (A)$
  is closed in $\banachV \times \banachW$.
  The operator $A \in \linOps (V,W)$ is called \emph{closable} if the closure
  $\closure{\Gr(A)}$ is the graph of an unbounded operator
  from $\banachV$ to $\banachW$.

  An operator $A \in \linOps (V,W)$ is called an \emph{extension} of
  $B \in \linOps (V,W)$ if $\Gr (B) \subset \Gr (A)$. One writes in this
  situation $B \subset A$.
\end{definition}


\documentclass{article}
\usepackage{fullpage}
\usepackage{parskip}
\usepackage{booktabs}
\usepackage{amsmath}
\pagenumbering{gobble}

\begin{document}

\section{Select your strategies}

$$A=
\begin{pmatrix}
2 & -2\\
-1 & 1
\end{pmatrix}
\qquad
B=
\begin{pmatrix}
-2 & 2\\
1 & -1
\end{pmatrix}
$$

\begin{enumerate}
\item Choose your strategies (as the column player) against the mixed strategy.
\item Wait for me to generate random strategies according to the random distribution.
\end{enumerate}
\vspace{1cm}

\begin{center}
\begin{tabular}{c|c|c|c|c|c|c}
\toprule
& \multicolumn{2}{c|}{$\sigma_1 = (.2,.8)$} & \multicolumn{2}{|c|}{$\sigma_1 = (.9,.1)$} & \multicolumn{2}{|c}{$\sigma_1=(1/3,2/3)$}\\
Round&$s_2$&$\sum u_2$&$s_2$&$\sum u_2$&$s_2$&$\sum u_2$\\
\midrule
&&&&&&\\
1&&&&&&\\
&&&&&&\\
&&&&&&\\
2&&&&&&\\
&&&&&&\\
&&&&&&\\
3&&&&&&\\
&&&&&&\\
&&&&&&\\
4&&&&&&\\
&&&&&&\\
&&&&&&\\
5&&&&&&\\
&&&&&&\\
&&&&&&\\
6&&&&&&\\
&&&&&&\\
\bottomrule
\end{tabular}
\end{center}

\end{document}