\documentclass{article}
\usepackage{graphicx}
\usepackage{titletoc}
\usepackage{titlesec}
\usepackage{geometry}
\usepackage{fontspec, xunicode, xltxtra}
\usepackage{float}
\usepackage{cite}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{titletoc}

\geometry{left=3cm,right=3cm,top=3cm,bottom=3cm}
\DeclareMathOperator*{\argmin}{argmin}
\DeclareMathOperator*{\argmax}{argmax}
\DeclareMathOperator*{\logit}{logit}
\DeclareMathOperator*{\var}{var}
\DeclareMathOperator*{\cov}{cov}
\DeclareMathOperator*{\expec}{E}
\DeclareMathOperator*{\deriv}{d}
\DeclareMathOperator*{\const}{constant}

\begin{document}
\title{\textsf{Homework 4 for Bayesian Data Analysis}}
\author{Fan JIN\quad (2015011506)}
\maketitle

\section*{Question 5.10a}
{
    $$p(\mu, \tau | y) \propto p(\mu, \tau) p(y | \mu, \tau) = p(\mu, \tau) \cdot \prod_{j=1}^{J} {N(\bar{y}_{.j} | \mu, \sigma_j^2 + \tau^2)}$$
    $$\propto \tau^{-1} \cdot \prod_{j=1}^{J} {(\sigma_j^2 + \tau^2)^{-1/2} \exp{ \left( - \frac{(\bar{y}_{.j} - \mu)^2}{2(\sigma_j^2 + \tau^2)} \right) }},$$
    where $\sigma_j^2 = \sigma^2 / n_j$ is the variance of the $j$-th group.

    As $\tau \rightarrow 0$, the posterior pdf $p(\mu, \tau | y)$ is dominated by $\tau^{-1}$, which is not integrable. Therefore, the posterior distribution is improper.
}

\section*{Question 5.10b}
{
    $$p(\mu, \tau | y) \propto p(\mu, \tau) p(y | \mu, \tau) = p(\mu, \tau) \cdot \prod_{j=1}^{J} {N(\bar{y}_{.j} | \mu, \sigma_j^2 + \tau^2)}$$
    $$= \prod_{j=1}^{J} {(\sigma_j^2 + \tau^2)^{-1/2} \exp{ \left( - \frac{(\bar{y}_{.j} - \mu)^2}{2(\sigma_j^2 + \tau^2)} \right) }}$$
    $$= \left[ \prod_{j=1}^{J} {(\sigma_j^2 + \tau^2)} \right]^{-1/2} \cdot \exp{\left[ -\frac{1}{2} \sum_{j=1}^{J} {\frac{(\bar{y}_{.j} - \mu)^2}{\sigma_j^2 + \tau^2}} \right]}$$
    $$= \left[ \prod_{j=1}^{J} {(\sigma_j^2 + \tau^2)} \right]^{-1/2} \cdot \exp{\left[ -\frac{1}{2} {\left( A(\mu-B)^2 + C \right)} \right]}$$
    $$= \left[ \prod_{j=1}^{J} {(\sigma_j^2 + \tau^2)} \right]^{-1/2} \cdot \exp{\left[ -\frac{A}{2} (\mu-B)^2 \right]} \cdot \exp{(-\frac{C}{2})},$$
    where $$A = \sum_{j=1}^{J} {\frac{1}{(\sigma_j^2 + \tau^2)}},$$
    $$C = \left[ \sum_{j=1}^{J} {\frac{\bar{y}_{.j}^2}{(\sigma_j^2 + \tau^2)}} \right] - A^{-1} \left[ \sum_{j=1}^{J} {\frac{\bar{y}_{.j}}{(\sigma_j^2 + \tau^2)}} \right]^2 .$$

    It follows that
    $$\int_{-\infty}^{\infty} {p(\mu, \tau | y) \deriv{\mu}} \propto \left[ \prod_{j=1}^{J} {(\sigma_j^2 + \tau^2)} \right]^{-1/2} \cdot A^{-1/2} \cdot \exp{(-\frac{C}{2})}.$$

    As $\tau \rightarrow 0$, the integral above goes to a finite constant. As $\tau \rightarrow \infty$, we have
    $$A \rightarrow J \tau^{-2},$$
    $$C \rightarrow \tau^{-2} \cdot \left[ \sum_{j=1}^{J}{\bar{y}_{.j}^2} - \frac{1}{J} (\sum_{j=1}^{J}{\bar{y}_{.j}})^2 \right],$$
    and the integral above is therefore dominated by
    $$\tau^{-J} \cdot J^{-1/2} \tau \cdot \exp{\left(-\frac{D}{2} \tau^{-2}\right)} \propto \tau^{-(J-1)} \exp{\left(-\frac{D}{2} \tau^{-2}\right)} \rightarrow \tau^{-(J-1)},$$
    where $$D = \sum_{j=1}^{J}{\bar{y}_{.j}^2} - \frac{1}{J} (\sum_{j=1}^{J}{\bar{y}_{.j}})^2 > 0.$$
    Since $\tau^{-(J-1)}$ is integrable with respect to $\tau$ if $J > 2$, the posterior distribution is proper:
    $$\int_{0}^{\infty} { \left\{ \int_{-\infty}^{\infty} {p(\mu, \tau | y) \deriv{\mu}} \right\} \deriv{\tau}} < \infty.$$
}

\section*{Question 5.10c}
{
    We don't have enough information to infer if we only have two groups in the hierarchical model, as the hyperparameters $(\mu, \tau)$ is estimated precisely when $J$ is large. So it is an alternative to use a model other than hierarchical model, e.g., to assume the two schools are independent of each other, and assign independent prior distribution for each school (i.e. treat the two schools separately), as shown below:
    $$y_{1j} \sim^{\mathrm{i.i.d.}} N(\theta_1, \sigma^2), \quad y_{2j} \sim^{\mathrm{i.i.d.}} N(\theta_2, \sigma^2),$$
    where $\sigma^2$ is known, and $$\theta_1 \sim N(\mu_1, \tau_1^2), \quad \theta_2 \sim N(\mu_2, \tau_2^2)$$ with hyperparameters $(\mu_1, \mu_2, \tau_1, \tau_2)$. Thus, $p(\mu_1, \tau_1 | \bar{y}_{1j})$ and $p(\mu_2, \tau_2 | \bar{y}_{2j})$ can be obtained separately.

}

\section*{Question 5.12}
{
    First, we have
    $$\theta_j | \mu, \tau, y \sim N( \frac{\frac{1}{\sigma_j^2} \bar{y}_{.j} + \frac{1}{\tau^2} \mu}{\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}}, \frac{1}{\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}}).$$
    It follows that $$\expec{(\theta_j | \mu, \tau, y)} = \frac{\frac{1}{\sigma_j^2} \bar{y}_{.j} + \frac{1}{\tau^2} \mu}{\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}},$$
    $$\var{(\theta_j | \mu, \tau, y)} = \frac{1}{\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}}.$$

    Second, we have
    $$\mu | \tau, y \sim N( \frac{\sum_{j=1}^{J}{\frac{1}{\sigma_j^2+\tau^2} \bar{y}_{.j}}}{\sum_{j=1}^{J}{\frac{1}{\sigma_j^2+\tau^2}}}, \left( \sum_{j=1}^{j}{\frac{1}{\sigma_j^2+\tau^2}} \right)^{-1} ).$$
    It follows that $$\expec{(\mu | \tau, y)} = \frac{\sum_{j=1}^{J}{\frac{1}{\sigma_j^2+\tau^2} \bar{y}_{.j}}}{\sum_{j=1}^{J}{\frac{1}{\sigma_j^2+\tau^2}}},$$
    $$\var{(\mu | \tau, y)} = (\sum_{j=1}^{j}{\frac{1}{\sigma_j^2+\tau^2}})^{-1}.$$

    Therefore, the posterior expectation and variance of $\theta_j$ conditional on $\tau$ and $y$ are:
    $$\expec{(\theta_j | \tau, y)} = \expec_{\mu | \tau, y}{\left[ \expec{(\theta_j | \mu, \tau, y)} \right]} $$
    $$= \expec_{\mu | \tau, y}{\left[ \frac{\frac{1}{\sigma_j^2} \bar{y}_{.j} + \frac{1}{\tau^2} \mu}{\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}} \right]}$$
    $$= \frac{\frac{1}{\sigma_j^2} \bar{y}_{.j} + \frac{1}{\tau^2} \expec{(\mu | \tau, y)}}{\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}}$$
    $$= \frac{\frac{1}{\sigma_j^2} \bar{y}_{.j} + \frac{1}{\tau^2} \frac{\sum_{j=1}^{J}{\frac{1}{\sigma_j^2+\tau^2} \bar{y}_{.j}}}{\sum_{j=1}^{J}{\frac{1}{\sigma_j^2+\tau^2}}}}{\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}},$$
    and
    $$\var{(\theta_j | \tau, y)} = \expec_{\mu | \tau, y}{\left[ \var{(\theta_j | \mu, \tau, y)} \right]} + \var_{\mu | \tau, y}{\left[ \expec{(\theta_j | \mu, \tau, y)} \right]}$$
    $$= \frac{1}{\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}} + \var_{\mu | \tau, y}{\left[ \frac{\frac{1}{\sigma_j^2} \bar{y}_{.j} + \frac{1}{\tau^2} \mu}{\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}} \right]}$$
    $$= \frac{1}{\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}} + \left( \frac{\frac{1}{\tau^2}}{\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}} \right)^2 \var{(\mu | \tau, y)}$$
    $$= \frac{1}{\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}} + \left( \frac{\frac{1}{\tau^2}}{\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}} \right)^2 (\sum_{j=1}^{j}{\frac{1}{\sigma_j^2+\tau^2}})^{-1}$$

}

\section*{Question 5.15a}
{
    Build the hierarchical model below:
    $$y_j | \theta_j, \sigma_j \sim N(\theta_j, \sigma_j^2),$$
    where $$y_j = \logit{(y_{1j} / n_{1j})} - \logit{(y_{0j} / n_{0j})}$$ is the log-odds (observed data), and $$\sigma_j^2 = (y_{1j})^{-1} + (n_{1j} - y_{1j})^{-1} + (y_{0j})^{-1} + (n_{0j} - y_{0j})^{-1}$$ is the approximated sampling variance (assumed known). And the model paramters $$\theta_j \sim^{\mathrm{i.i.d.}} N(\mu, \tau^2)$$ with hyperparameters $(\mu, \tau)$.

    Assign a noninformative hyperprior distribution $p(\mu, \tau) \propto 1$. It follows from Question 5.10b that
    $$p(\tau | y) = \int_{-\infty}^{\infty} {p(\mu, \tau | y) \deriv{\mu}} \propto \left[ \prod_{j=1}^{J} {(\sigma_j^2 + \tau^2)} \right]^{-1/2} \cdot A^{-1/2} \cdot \exp{(-\frac{C}{2})},$$ where $A$, $C$ are defined in my answer to Question 5.10b:
    $$A = \sum_{j=1}^{J} {\frac{1}{(\sigma_j^2 + \tau^2)}},$$
    $$C = \left[ \sum_{j=1}^{J} {\frac{\bar{y}_{.j}^2}{(\sigma_j^2 + \tau^2)}} \right] - A^{-1} \left[ \sum_{j=1}^{J} {\frac{\bar{y}_{.j}}{(\sigma_j^2 + \tau^2)}} \right]^2 .$$

    Although the expression is a bit complicated for analysis, it can be easily calculated numerically. Below is the output in R:
    \begin{figure}[H]
        \centering
        \includegraphics[width = 0.8\linewidth]{tau.posterior.png}
        \caption{Posterior density of $\tau$}
    \end{figure}

}

\section*{Question 5.15b}
{
    Based on what we obtained in Question 5.12 about the posterior mean and variance of $\theta_j$ conditional on $\tau$, we get the plots below.
    \begin{figure}[H]
        \centering
        \includegraphics[width = 1.0\linewidth]{theta.posterior.means.png}
        \caption{Posterior means of $\theta_j$}
    \end{figure}
    \begin{figure}[H]
        \centering
        \includegraphics[width = 1.0\linewidth]{theta.posterior.vars.png}
        \caption{Posterior variances of $\theta_j$}
    \end{figure}

}

\section*{Question 5.15c}
{
    First, we obtained the posterior joint distribution for hyperparameters in Question 5.10b:
    $$p(\mu, \tau | y) \propto \left[ \prod_{j=1}^{J} {(\sigma_j^2 + \tau^2)} \right]^{-1/2} \cdot \exp{\left[ -\frac{A}{2} (\mu-B)^2 \right]} \cdot \exp{(-\frac{C}{2})},$$
    where $$A = \sum_{j=1}^{J} {\frac{1}{(\sigma_j^2 + \tau^2)}},$$
    $$C = \left[ \sum_{j=1}^{J} {\frac{\bar{y}_{.j}^2}{(\sigma_j^2 + \tau^2)}} \right] - A^{-1} \left[ \sum_{j=1}^{J} {\frac{\bar{y}_{.j}}{(\sigma_j^2 + \tau^2)}} \right]^2 .$$

    Second, we have
    $$p(\theta_j | \mu, \tau, y) \propto (V_j)^{-1/2} \exp{(-\frac{(\theta_j - \hat{\theta}_j)^2}{2V_j})}, $$
    where $$\hat{\theta}_j = \frac{\frac{1}{\sigma_j^2} \bar{y}_{.j} + \frac{1}{\tau^2} \mu}{\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}}, $$
    $$V_j = \left( \frac{1}{\sigma_j^2} + \frac{1}{\tau^2} \right)^{-1}.$$

    It follows that
    $$p(\theta_j | y) = \int_{0}^{\infty} {\int_{-\infty}^{\infty} {p(\mu, \tau | y) \cdot p(\theta_j | \mu, \tau, y) \deriv{\mu}} \deriv{\tau}}.$$ The median of $\theta_j | y$ is obtained as well.
}

\section*{Question 5.15d}
{
    First, draw $\tau$ from $p(\tau | y)$. Then draw $\mu$ from $p(\mu | \tau, y)$. Finally, draw $\theta_j$ from $p(\theta_j | \mu, \tau, y)$.

    \begin{figure}[H]
        \centering
        \includegraphics[width = 1.0\linewidth]{theta.simulations.png}
        \caption{Simulations of $\theta_j$s}
    \end{figure}
}

\section*{Question 5.15e}
{
    I failed to simulate $y_{1j}$ and $y_{0j}$ from the $\theta_j$s simulated in Question 5.15d, because we cannot obtain the likelihood $$p(y_j | \theta_j, \sigma_j^2) = N(\theta_j, \sigma_j^2)$$ with $\sigma_j^2$ unknown for the hypothetical new study, although $\theta_j$ has been simulated in Question 5.15d.
}

\section*{Source Code in R:}
{
    Raw file: http://39.106.23.58/files/BayesianHW4.7z

    \begin{lstlisting}[language=R]
        plot.increment = 0.0005
        dat = read.csv("data.csv", header=T)

        logit <- function(x) {
          return (log(x / (1 - x)))
        }

        log.odds = logit(dat$treated.deaths / dat$treated.total) - logit(dat$control.deaths / dat$control.total)
        std.err = sqrt((dat$treated.deaths)^(-1) +
                         (dat$treated.total - dat$treated.deaths)^(-1) +
                         (dat$control.deaths)^(-1) +
                         (dat$control.total - dat$control.deaths)^(-1))

        dat = cbind(dat, log.odds)
        dat = cbind(dat, std.err)

        J = nrow(dat)

        # Question 5.15a

        tau.posterior <- function(tau, dat) {
          J = nrow(dat)

          A = 0
          for (j in 1:J) {
            A = A + 1 / (dat$std.err[j]^2 + tau^2)
          }

          S1 = 0
          for (j in 1:J) {
            S1 = S1 + (dat$log.odds[j]) / (dat$std.err[j]^2 + tau^2)
          }

          S2 = 0
          for (j in 1:J) {
            S2 = S2 + (dat$log.odds[j]^2) / (dat$std.err[j]^2 + tau^2)
          }

          C = S2 - S1^2 / A

          P = 0
          for (j in 1:J) {
            P = P + (-1/2) * log(dat$std.err[j]^2 + tau^2)
          }
          P = exp(P)

          return (P * A^(-1/2) * exp(-C/2))

        }

        taus = seq(0 + plot.increment, 1, by = plot.increment)
        taus.posterior = unlist(lapply(taus, tau.posterior, dat))
        taus.posterior = taus.posterior / sum(taus.posterior) / plot.increment # rescale to 1

        png("tau.posterior.png", width = 800, height = 600)
        plot(taus, taus.posterior)
        dev.off()

        # Question 5.15b

        theta.posterior.mean <- function(tau, dat) {
          J = nrow(dat)

          S0 = 0
          for (j in 1:J) {
            S0 = S0 + 1 / (dat$std.err[j]^2 + tau^2)
          }

          S1 = 0
          for (j in 1:J) {
            S1 = S1 + (dat$log.odds[j]) / (dat$std.err[j]^2 + tau^2)
          }

          ret = 1:12
          for (j in 1:J) {
            A = dat$log.odds[j] / dat$std.err[j]^2 + 1 / tau^2 * S1 / S0
            B = 1 / dat$std.err[j]^2 + 1 / tau^2
            ret[j] = A / B
          }

          return (ret)
        }

        theta.posterior.var <- function(tau, dat) {
          J = nrow(dat)

          S0 = 0
          for (j in 1:J) {
            S0 = S0 + 1 / (dat$std.err[j]^2 + tau^2)
          }

          ret = 1:12
          for (j in 1:J) {
            A = 1 / tau^2
            B = 1 / dat$std.err[j]^2 + 1 / tau^2
            ret[j] = 1 / B + (A / B)^2 / S0
          }

          return (ret)
        }

        theta.posterior.means = lapply(taus, theta.posterior.mean, dat)
        theta.posterior.means = matrix(unlist(theta.posterior.means), ncol=J, byrow=T)

        theta.posterior.vars = lapply(taus, theta.posterior.var, dat)
        theta.posterior.vars = matrix(unlist(theta.posterior.vars), ncol=J, byrow=T)

        png("theta.posterior.means.png", width = 800, height = 600)
        matplot(taus, theta.posterior.means, type="l", lty=1, main="Posterior means of theta")
        dev.off()

        png("theta.posterior.vars.png", width = 800, height = 600)
        matplot(taus, theta.posterior.vars, type="l", lty=1, main="Posterior variances of theta")
        dev.off()

        # Question 5.15c

        index.MLE = which.max(taus.posterior) # use MLE of tau for a crude estimate of theta_j
        tau.MLE = taus[index.MLE]

        # Question 5.15d

        p.mu.tau.c.dat <- function(mu, tau, dat) {
          J = nrow(dat)

          A = 0
          for (j in 1:J) {
            A = A + 1 / (dat$std.err[j]^2 + tau^2)
          }

          S1 = 0
          for (j in 1:J) {
            S1 = S1 + (dat$log.odds[j]) / (dat$std.err[j]^2 + tau^2)
          }

          S2 = 0
          for (j in 1:J) {
            S2 = S2 + (dat$log.odds[j]^2) / (dat$std.err[j]^2 + tau^2)
          }

          C = S2 - S1^2 / A

          S3 = 0
          for (j in 1:J) {
            S3 = S3 + (dat$log.odds[j]) / (dat$std.err[j]^2 + tau^2)
          }

          B = S3 / A

          P = 0
          for (j in 1:J) {
            P = P + (-1/2) * log(dat$std.err[j]^2 + tau^2)
          }
          P = exp(P)

          return (P * exp(-A/2 * (mu - B)^2) * exp(-C/2))
        }

        p.theta.c.mu.tau.dat <- function(j, theta.j, mu, tau, dat) {
          J = nrow(dat)

          A = 1 / dat$std.err[j]^2
          B = 1 / tau^2

          theta.j.hat = (A * dat$log.odds[j] + B * mu) / (A + B)
          V.j = 1 / (A + B)

          return (V.j^(-1/2) * exp(-1/2 * (theta.j - theta.j.hat)^2 / V.j))
        }

        # Question 5.15d

        T = 1000
        samples = matrix(NA, T, J)

        for (k in 1:1000) {
          U = runif(1)
          temp = 1:length(taus)
          temp[1] = taus.posterior[1]
          V = 0
          for (j in 2:length(taus)) {
            temp[j] = temp[j - 1] + taus.posterior[j]
            if (temp[j] > U) {
              tau.sampled = taus[j] # sample tau
              break;
            }
          }

          J = nrow(dat)
          A = 0
          for (j in 1:J) {
            A = A + 1 / (dat$std.err[j]^2 + tau.sampled^2)
          }
          S3 = 0
          for (j in 1:J) {
            S3 = S3 + (dat$log.odds[j]) / (dat$std.err[j]^2 + tau.sampled^2)
          }
          B = S3 / A
          mu.sampled = rnorm(1, mean = B, sd = 1 / sqrt(A)) # sample mu

          J = nrow(dat)
          for (j in 1:J) {
            C = 1 / dat$std.err[j]^2
            D = 1 / tau.sampled^2

            theta.j.hat = (C * dat$log.odds[j] + D * mu.sampled) / (C + D)
            V.j = 1 / (C + D)
            theta.j.sampled = rnorm(1, theta.j.hat, sqrt(V.j)) # sample theta_j

            samples[k, j] = theta.j.sampled
          }
        }

        png("theta.simulations.png", width = 800, height = 600)
        par(mfrow=c(4,6))
        for (j in 1:J) {
          hist(samples[, j], main="Simulations of theta", pch = j)
        }
        dev.off()

    \end{lstlisting}
}

\clearpage
\end{document}


\section{Ensembles}

For a given macrostate $(N, V, E)$, a statistical system, at any time t, may be in any one of an extremely large number of distinct microstates. As time passes, the system transitions between microstates, with the result that, over a long enough time period the behaviour of the systems is ``averaged'' over the collection of microstates which have been visited by the system.
A useful approach is to consider, at a single instant of time, a large (infinite) number of ``copies'' of the same system, existing in all possible microstates that satisfy the macroscopic conditions. Then, we can expect the average behaviour of any system for this collection (or ensemble) to be identical with the time-averaged behaviour of the system. This forms the basis of the so–called ensemble theory.

In the previous section, we considered an isolated system where we could keep track of the dynamics of every particle and use that to calculate the values of extensive, macroscopic properties of the system. An important aspect of this was the conservation of energy for the system. We refer to such systems as a \emph{micro-canonical ensemble} --- the system is isolated with no heat flux and no change in the number of particles, hence the internal energy of the system is constant and it is described entirely by the Hamiltonian dynamics

We also saw (a couple of sections earlier) that we can equate the entropy of a system with the accessible volume of the phase space of that system. This was part of what motivated us to study the microscopic dynamics of the system using molecular dynamics.  We'd now like to consider some slightly more realistic cases where, for example, we may want to allow for multiple systems in contact.

We'll start by considering the simplest case: two (isolated) systems in contact, without any exchange of energy. If the state of system 1 corresponds to the region of phase space $\Gamma^1$ and similarly, the state of system 2 to $\Gamma^2$ then the state of the composite system $1\cup 2$ corresponds to the phase space regions given by the Cartesian product $\Gamma^{1\cup 2}=\Gamma^1\times\Gamma^2$ and the volume of this accessible volume of phase space is given by $|\Gamma^{1\cup 2}|=|\Gamma^1||\Gamma^2|$. From this, it's easy to see that the entropy of the composite system is given by
\begin{eqnarray*}
	S &=& k_B\ln|\Gamma^{1\cup 2}| = k_B\ln(|\Gamma^1||\Gamma^2|)\\
		&=& k_B\ln|\Gamma^1| + k_B\ln|\Gamma^2| = S^1 + S^2,
\end{eqnarray*}
which is fortunate, since entropy is an extensive variable and we therefore expect it to be additive.
In this example the two sub-system were completely isolated from each other; the dynamics of one system had no influence on the dynamics of the other. This condition of dynamic independence corresponds to the independence of the observables that pertain to these sub-systems.

Now, let's look at how the entropy of a composite system changes if we allow for the exchange of energy between the two sub-systems. This has the effect of increasing the accessible volume of the phase space, since exchange of energy means that there are more possible configurations for the overall system.

Without energy exchange, the volume of the accessible phase space to the total system is given by
$$
	|\Gamma_0| = |\Gamma^1||\Gamma^2|.
$$
Once we allow for the exchange of energy, this becomes
$$
	|\Gamma| = \sum_{E^1}|\Gamma^1(E^1)||\Gamma^2(E_\text{tot}-E^1)|.
$$
That is, we must now consider all possible configurations where the total energy of the composite system is partitioned between the two sub-systems. This volume is bounded below by $|\Gamma_0|$ since the expression for $|\Gamma_0|$ is just one of the terms in the sum. At first glance, it may look like this increase in the volume of the accessible phase space is enormous (we have added many more possible configurations), however, the volume of the accessible phase space for the composite system corresponds almost entirely to the states where $E^1=E^1_\text{eqm}$ and $E^2=E^2_\text{eqm}$. As a consequence, for large enough $N$, the difference between $\Gamma$ and $|\Gamma^1(E^1_\text{eqm})|||\Gamma^2(E^2_\text{eqm})|$ is negligible. It's not too hard to show (see section 3.7 of \emph{Statistical Mechanics in a Nutshell}) that the contribution to the accessible phase space volume due to the exchange of energy between the two systems is of order $\sqrt{N}$ compared with the total system size $N$ --- small enough to be negligible when $N$ is large.

\subsection{The canonical ensemble}

Rather than considering a perfectly isolated system, for which energy is conserved, a more realistic experimental situation may be to consider a system S in thermal contact with some much larger reservoir R. This has the effect of holding the total system at constant temperature. In such a situation we want to be able to calculate the average value $\langle A\rangle$ of an observable $A$ for the system S; we are not interested in the state of the reservoir R, except to the extent that it helps us determine the state of S.

If, for the composite system S$\cup$R, we write $x_S,x_R$ for the state, te=hen the average value of the observable $A$ is given by
$$
	\langle A\rangle = \frac{1}{|\Gamma|}\int_{\Gamma}dx_Sdx_RA(x_s),
$$
where $\Gamma$ is the region of the phase space for the composite system, when it has total internal energy of $E$.

In order to make explicit the parts of the total phase space that is accessible to the composite system, wewrite the above expression as the Cartesian product of the phase space for the system and the reservoir, i.e.
$$
	\langle A\rangle =\frac{1}{|\Gamma|}\int dx_SA(x_S)\times\int dx_R\delta(H^R(x_R)-(E-H^S(x_S))).
$$
The delta function in the last term is zero, except when $x_S$ and $x_R$ in the two sub-systems take values such that $H^S+H^R=E$. That is the delta function defines the accessible phase space volume when the two sub-systems can exchange internal energy between them, but the total internal energy of the composite system is conserved.

Recalling the fundamental postulate of statistical mechanics ($S=k_B\ln|\Gamma|$), we rewrite the last expression to replace the phase space volume with the corresponding expression for entropy:
$$
	\int dx_R\delta(H^R(x_R)-(E-H^S(x_S))) \simeq \exp\left(\frac{1}{k_B}S^R(E-H^S)\right).
$$

Since $H^S$ is much less than $E$ it makes sense to now expand the exponential in a Taylor series about $E$:
$$
	\exp\left(\frac{1}{k_B}S^R(E-H^S)\right)\simeq \exp\left(\frac{1}{k_B}S^R(E)\right)\exp\left(\frac{-1}{k_B}\left.\frac{\partial S^R}{\partial E}\right\|_E H^S(x_S)\right)\ldots
$$

Earlier in the course, we identified $\frac{\partial S}{\partial E}$ with $\frac{1}{T}$, and, for a canonical ensemble, $T$ is constant (that's the point of the reservoir) so we write the expected value of the observable as
$$
	\langle A\rangle \simeq \frac{1}{\Gamma}\int dx_sA(x_S)\times\exp\left(\frac{1}{k_B}s^R(E)\right)\exp\left(\frac{-H^S(x_S)}{k_BT}\right).
$$

It remains to make the normalisation precise. When we do this, the factors of $S\exp\left(\frac{1}{k_B}S^R(E)\right)$ cancel from the integral and its normalisation and we get
\begin{equation}
	\langle A\rangle = \frac{1}{Z}\int dx_SA(x_S)\exp\left(\frac{-H^S(x_S)}{k_BT}\right),
	\label{eq:z1}
\end{equation}
where $Z$ is the known as the \emph{partition function} and is given by
\begin{equation}
	Z = \int dx_S\exp\left(\frac{-H^S(x_S)}{k_BT}\right).
	\label{eq:z2}
\end{equation}

One way to think about equations \ref{eq:z1} and \ref{eq:z2} is that we no longer need to keep track of which parts of phase space are accessible. Instead we can integrate over the entire phase space and each region is weighted appropriately, according to $\exp\frac{-H}{k_BT}$ --- the so-called \emph{Boltzmann factor}, a probability density in the phase space.

Similar to the case of the micro-canonical ensemble, for the canonical ensemble, the contributions to $A$ are dominated by the part of phase space that corresponds to when the system's internal energy is at the equilibrium value.

You should read through, and understand, section 3.12 of \emph{Statistical Mechanics in a Nutshell} and sections 4.1--4.3 of \emph{Statistical Mechanics Made Simple} for some extra details and explanation.

\subsection{Other ensembles}
We can follow a similar approach to what we did for the canonical ensemble and generalise to other types of ensembles. If $f_i$ is some variable that we want to hold fixed and $X_i$ is the conjugate extensive variable, then putting the system in contact with a reservoir with which it can exchange $X_i$ gives
\begin{equation*}
	\langle A\rangle = \frac{1}{Z}\int dx_SA(x_S)\exp\left(\frac{f_iX^S_i(x_S)}{k_BT}\right),
\end{equation*}
where we have used the relation $\left.\frac{\partial S^R}{\partial X_i^R}\right\|_E=-\frac{f_i}{T}$.

Calculating the partition function proceeds similarly too, (see SMiaN, section 3.13) and gives
\begin{equation*}
	Z \simeq \exp\left(\frac{T^S(X_i^*)+f_iX_i^*}{k_BT}\right),
\end{equation*}
where $X_i^*$ is the value of $X_i$ which maximises the value of the exponential.

\subsubsection{The grand-canonical ensemble}
Rather than holding $N$ the number of particles fixed we can allow it to vary and instead fix $\mu$ the chemical potential. The corresponding ensemble for this case is the so-called \emph{grand-canonical ensemble}. In this case, the expected value of an observable $A$ is given by
$$
	\langle A\rangle = \frac{1}{Z}\sum_{N=1}^{\infty}\int\mathrm{d}xA(x)\exp\left(-\frac{H_N(x)-\mu N}{k_B T}\right)
$$
and the corresponding partition function is given by
$$
	Z=\exp\left(-\frac{E-TS-\mu N}{k_B T}\right).
$$

\subsubsection{The $P-T$ ensemble}
The \emph{$p-T$ ensemble} is one specific example of a generalised ensemble. In this case the pressure and temperature are fixed, while the internal energy and volume (their conjugate variables) are allowed to fluctuate.
Using the generalised formula above, and dropping the subscripts that we were previously using to denote the system, the $p-T$ ensemble is given by
 \begin{equation*}
	\langle A\rangle = \frac{1}{Z}\int dxA(x)\exp\left(-\frac{E(x)+pV(x)}{k_BT}\right),
\end{equation*}
while the partition function is given by
$$
	\ln Z = -\frac{E-TS+pV}{k_BT}.
$$
The quantity on the top of the fraction is the \emph{Gibbs free energy}. (See section 3.14 of SMiaN)

Another important ensemble is the so-called \emph{grand ensemble} in which the number of particles in the system is also allowed to fluctuate due to, for example, chemical reactions, while the \emph{chemical potential} $\mu$ is held fixed. In this case the expression for the expected value of an observable is
$$
	\langle A \rangle = \frac{1}{Z} \sum_{N=1}^\infty \int dxA(x)\exp\left(-\frac{H_N(x)-\mu N}{k_BT}\right),
$$
while the corresponding partition function is given by
$$
	\ln Z = -\frac{E-TS-\mu N}{k_BT}.
$$
(See section 3.15 of SMiaN and section 4.4 of SMMS for more discussion of the grand canonical ensemble.)

\subsection{Information theory and the Gibbs Formula for entropy}
To finish off this section, we'll look at one last application of entropy; not because it is particularly useful to physical systems in statistical mechanics, but because it gives a result that forms the basis of information theory.

We'll return to a considering a generalised ensemble, like the one we looked at a couple of sections earlier. However, in this case we'll assume that the phase space is discretized and that the index $n$ runs over all of the microstates of the system. If we consider an intensive variable $f$ and its corresponding extensive variable $X$ then the expression for the expected value of any observable $A$ is
$$
	\langle A \rangle = \frac{1}{Z}\sum_n A_n\exp\left(\frac{fX_n}{k_BT}\right),
$$
while the partition function $Z$ is given by
$$
	Z =\sum_n\exp\left(\frac{fX_n}{k_BT}\right).
$$

The partition function is related to the thermodynamic potentials (see the previous section on generalised ensembles and SMiaN sections 3.12 and 3.13) via
\begin{equation}
	\ln Z = \frac{TS+f\langle X\rangle}{k_BT}.
	\label{eq:gibbZ}
\end{equation}

Now, for any individual microstate $n$ the probability is therefore given by
$$
	p_n = \frac{1}{Z}\exp\left(\frac{fX_n}{k_BT}\right).
$$
Taking the log of both sides of this expression gives
$$
	\ln p_n = \frac{fX_n}{k_BT} - ln Z,
$$
which after substituting \ref{eq:gibbZ} for $\ln Z$ gives
$$
	\ln p_n = \frac{1}{k_BT}(fX_n -TS -f\langle X\rangle).
$$

Now we can calculate the expected value of both side of the above equation
$$
	\langle \ln p_n \rangle = \sum_n p_n\ln p_n = \frac{1}{k_BT}(f\langle X\rangle -TS -\langle X \rangle) = \frac{-S}{kB}.
$$

After rearranging this for $S$ (and making explicit the sum for the expected value of $p_n$) we arrive at the \emph{Gibbs formula for entropy}:
$$
	S = -k_B\sum_np_n\ln p_n.
$$

Although this is elegant, it's generally useless in the context of physical systems since the number of microstates it would be necessary to sum over is far to large to be computationally practical and, in any case, we generally don't know the probability distribution $p_n$ for each of the microstates. The value of this expression lies in it's application to other systems, particularly information theory, where it can be used to quantify amount of information in, for example a digital signal, in which case the $p_n$ represent the probability of receiving the $n$-th possible value in the list of signals.

\subsection{Calculating things with ensembles}
Let $\rho(q,p,t)$ be the probability density function that gives the probability that a system will be in states $(q,p)$ at time $t$. Now, consider an ensemble of systems characterised by $\rho$, for any choices of $(q,p)$. If $\rho(q,p,t) = \rho(H(q,p))$ --- i.e. $\rho$ is autonomous --- then $\frac{\partial \rho}{\partial t}=0$ and we say that the system is stationary.

The canonical ensemble average $\langle f\rangle$ of some physical quantity $f(q,p)$ is
$$
	 \langle f\rangle = \frac{\int f(q,p)\rho(q,p)\mathrm{d}q\mathrm{d}p}{\int\rho{q,p}\mathrm{d}q\mathrm{d}p}.
$$
For fixed $N$, as we saw above, the natural choice for the probability density function is
$$
\rho(q,p) \propto \exp\left(-\frac{H(q,p)}{k_BT}\right)
$$
and if we normalise this, so that $\int \rho =1,$ we have
$$
	\rho(q,p) = \frac{1}{Z}\exp\left(-H(q,p)/k_BT\right).
$$
For $N$ particles, in 3-D the normalisation factor is
$$
	Z(T,N,V) = \frac{1}{h^{3N}}\int\mathrm{d}^{3N}q\mathrm{d}^{3N}p\exp(-H(q,p)/k_BT),
$$
the so-called partition function.

We often write the integral as an infinite sum, both for ease of notation, and because it makes sense for discrete systems like the QM systems we will meet soon. I.e. $Z=\sum_n\exp(-H_n/k_BT)$.

Most quantities of interest for an ensemble can be calculated two ways; as a sum over states and as derivates of the partition function. For example:

\subsubsection{Internal energy}
The average value of the internal energy of an ensemble can be found by summing over each energy state $E_n$ of the system, weighted by the corresponding probability $p_n$. (In the following, we're going to use the notation $\beta =1/k_bT$. The quantity $\beta$ is referred to as the inverse temperature.)

\begin{align*}
	\langle E \rangle = \sum_n E_n p_n &= \frac{\sum_n E_n\exp(-\beta E_n)}{Z}\\
	&= -\frac{\partial Z}{\partial \beta}\frac{1}{Z} = \frac{\partial}{\partial \beta} \ln(Z).
\end{align*}

\subsubsection{Specific heat}
Let $c_v$ be the specific heat per particle in a system with constant volume. This means that when we add a unit of heat to the system, the temperature increases by $\frac{\partial\langle E\rangle}{\partial T}$ units. The total specific heat of the system, with $N$ particles, is given by
\begin{align*}
	Nc_v = \frac{\partial \langle E \rangle}{\partial T} &=  \frac{\partial \langle E \rangle}{\partial \beta} \frac{\mathrm(d)}{\mathrm{d}T}\\
	&=\frac{-1}{k_bT^2} \frac{\partial^2 Z}{\partial \beta^2} \mbox{ since } \beta = 1/k_BT \mbox{ and } \langle E \rangle = \frac{-\partial Z}{\partial \beta}.
\end{align*}

We can expand the second to last expression in the equality above as a sum. This gives
\begin{align*}
	Nc_v &= \frac{-1}{k_bT^2} \frac{\partial}{\partial \beta} \frac{\sum_n E_n \exp(-\beta E_n)}{\sum_n\exp(-\beta E_n)}\\
	&= \frac{-1}{k_BT^2}\left[\frac{\sum_n E_n\exp(-\beta E_n)}{Z}+ \frac{(\sum_n E_n\exp(-\beta E_n))^2}{Z^2}\right]\\
	&= \frac{1}{k_BT}(\langle E \rangle^2 - \langle E^2\rangle).
\end{align*}
That is, the specific heat of the system is given by the RMS fluctuation in the internal energy of the system at constant temperature.

\subsubsection{Entropy}
We have
$$
	S = -k_B\sum_n p_n\ln(p_n).
$$
(This is the Gibbs formula for entropy that we saw a couple of pages ago.) Using $p_n = \frac{\sum_n \exp(-\beta E_n)}{Z}$, along with the properties of logs, one gets (do the calculation yourself --- it's not a tough one)
$$
	S = \frac{\langle E \rangle}{T} +k_B\ln(Z).
$$
Rearranging gives
$$
	\langle E \rangle -ST = -1/\beta \ln(Z) = A(T,V,N).
$$

The quantity $A(T,V,N)$ is the so-called Helmholtz free energy. It has the property that $S = \left.\frac{-\partial A}{\partial T}\right\|_{N,V}$.

\subsubsection{Simple harmonic oscillator}
A classical simple harmonic oscillator has $H(q,p) =p^2/2m + m\omega^2q^2/2$ where $\omega$ is the angular frequency of the oscillator. As an exercise, calculate the following for an ensemble consisting of a single oscillator.
\begin{itemize}
	\item The partition function $Z$;
	\item The Helmholtz free energy $A_{\omega}(T)$;
	\item The average internal energy $\langle E \rangle_{\omega}(T)$.
\end{itemize}

\subsection{Recommended reading}
Most of the notes in this section follow closely the second half of chapter 3 in \emph{Statistical Mechanics in a Nutshell}; specifically, section 3.6--3.18. Chapter four of \emph{Statistical Mechanics made Simple} covers the same material in sections 4.0--4.4. It is gives some intuitive and succinct explanations, but I find it to be of more use \emph{after} you've already looked at SMiaN. Also useful, and with a slightly different presentation (perhaps with more traditional notation), is \emph{Entropy, Order Parameters, and Complexity}. Here the content is spread around a bit over chapters three through six. Much of the useful content, including some relevant to early sections of these notes, is in sections 3.1, 3.5, 6.1, 6.2, 6.3, and 5.3.


\chapter{Backgroud}

As the proposed architecture and algorithm extend existing work on Probabilistic Graphical Models, a substantial amount of background is required culminating with the new approach being derived. A robust understanding of these concepts is required for this project to implement and design appropriate evaluations for the proposed architecture.

\section{Generative Models}\label{S:Generative-Models}

This project works with neural network based generative models.
Generative models are a powerful way to model data. With generative models, we aim to learn a model that can both create the training data, represent it, and reconstruct it using the representation the model has learnt to create. Generative models can map input data from raw values to higher level features. Hinton~\cite{hinton:32723:vv}, gave a compelling argument for higher level features in the context of generative models. \begin{quote} ``Consider, for example, a set of images of a dog. Latent variables such as the position, size, shape and color of the dog are a good way of explaining the complicated, higher-order correlations between the individual pixel intensities, and some of these latent variables are very good predictors of the class label.''\end{quote}

Generative models model a distribution over a collection of binary variables $X$, where $X$ is comprised of variables which can be observed or unobserved. The observed variables are referred to as \texttt{visible} variables or units ($v$). Conversely, unobserved variables correspond to the hidden units of the neural network ($h$). With these terms defined, the joint distribution that generative models model can be expressed as $P(X)$ where $X$ is comprised of $h,v$. Collections of these units, are often referred to as `patterns` or `vectors` in that they are represented by a vector or pattern of bits. For instance in the context of an image, a visible pattern is the pixels of the image flattened into a one dimensional vector.

\section{Directed PGMs}

The new approach proposed in this project uses both the two classes of Probabilistic Graphical Models, Directed and Undirected. A Directed PGM, is a notation for factorising over a collection of stochastic variables.
Given $X$, the variables in the generative model, and $parent_i$ the parent unit of $x_i$, the distribution over $X$ in a directed PGM is defined by the following factorisation:
\begin{equation}\label{eq:sbn-factorisation}
P(X) = \prod_i P(x_i | parent_i)
\end{equation}
Connections between units in the Directed PGM express causation~\cite{Pearl:1988:PRI:52121}. They provide an expressive way to represent a collection of related, stochastic variables. See figure \ref{F:PGM-example} for the minimal example, where variable $A$ is dependent on $B$. As a result this network is often referred to as a Belief Network or Bayesian Network. A Belief Network's causal dependencies are expressed as a conditional probability table also know as `factor'. For example in figure \ref{F:PGM-example} the probabilities of $A$ being in a given state are dependent on $B$. The joint distribution over $A$ and $B$, as in equation \ref{eq:sbn-factorisation}, can be expressed as:
$$P(A,B) = P(A|B)P(B)$$
\begin{wrapfigure}{r}{0.2\textwidth}
\begin{center}
  \includegraphics[width = 0.1\textwidth]{Assets/PGM_Example_1.png}
\caption{Minimal Directed PGM, showing an observed variable `A` and it's hidden cause `B`.}
\label{F:PGM-example}
\end{center}
\end{wrapfigure}

\subsection{Explaining Away in Directed PGMs}\label{S:Explaining-Away}

A common task in generative models is given a parent unit (a cause) is in some state, inferring the state of child variable. In directed PGMs this is trivial to calculate. The opposite task is also desirable~\cite{salakhutdinov2009learning}, inferring the state of causes given the effect of that cause. It becomes problematic in Directed PGMs as the causal relationships give rise to the effect of `Explaining Away'. The canonical example that exemplifies `Explaining Away', \texttt{Burglar, Earthquake, Alarm Problem}~\cite{Barber:2012:BRM:2207809} is illustrated in figure~\ref{F:Explaining-Away}.
\begin{figure}[h]
\begin{center}
  \includegraphics[width = 0.4\textwidth]{Assets/Explaining_Away.png}
\caption{The famous \texttt{Burglar, Earthquake, Alarm} network showing a minimal case of explaining away.}
\label{F:Explaining-Away}
\end{center}
\end{figure}
Knowledge of the state of the \texttt{alarm} makes \texttt{burglar} and \texttt{earthquake} dependent. The \texttt{alarm} is the observable variable ($v$) and the \texttt{burglar} and \texttt{earthquake} are the hidden `causes' ($h$). For example if the \texttt{alarm} is true, and we see news of earthquake in the area, our belief that we have been burgled decreases. Expressed (again exemplifying equation \ref{eq:sbn-factorisation}) as a joint probability over $A$, $B$ and $E$ where these are the states of \texttt{alarm},\texttt{burglar} and \texttt{earthquake} respectively:
$$
P(A,B,E) = P(A|B,E)P(B)P(E)
$$

\subsection{Directed PGMs in Neural Networks: The Sigmoid Belief Network}

A Belief network can be expressed as a neural network, where conditional probabilities are parameterised as weights. This network is called a Sigmoid Belief Network (SBN) as the probability of a variable $x_i$ being 1 is dependent on a ancestor variable $parent_i$. The weighted sum into $x_i$, $\phi_i$ passed through the sigmoid function ($ \sigma(x)=1/(1+e^{-x})$). This is equivalent to a Perceptron using a sigmoid activation function which ensures that the output is a valid probability (between 0 and 1).
SBNs take a naive approach to causes, where each hidden unit represents a single, simple cause. Formally, $\phi_i$ is a weighted sum of the activations of parent nodes:
$$ \phi_i = \sum_{j \in parent_i} W_{ij}x_j$$
and
$$
P(x_i = 1 | parent_i) = \sigma(\phi_i)
$$

\section{Undirected PGMs:}

Undirected PGMs do not represent causation, instead merely capturing a dependency between two units. These pairwise dependencies change the structure of the factorisation a factor $\Phi$ between each pair of variables $x_i,x_j$ resulting in the factorisation:
$$
P(X) = \frac{1}{Z} \prod_i \Phi(x_i, x_j)
$$
The introduction of the normalisation $Z$ (often referred to as the partition function) adds nontrivial complexity to performing inference in Undirected PGMs~\cite{Salakhutdinov08learningand}, a sum over all $2^N$ configurations of $x$ is required.

Undirected PGMs do not capture causal relationships meaning computing the state of a variable given another is no longer hampered by the effect of explaining away. However, their reccurent structure, while expressive introduces an intractability in practice.

\subsection{Undirected PGMs in Neural Networks: The Boltzmann Machine}
A Undirected PGM expressed as nueral network is referred as Boltzmann Machine, where connections encode dependancies with an associated weight. We see this where $W_{ij}$ is the weight between variables $x_i$ and $x_j$ the factor $\Phi$ is expressed as:
$$
\Phi(x_i, x_j) = e^{x_ix_jW_{ij}}
$$
The Boltzmann machine has been proposed in various forms from different domains throughout the years. For instance it was presented in a non-stochastic context of the Hopfield network in~\cite{Hopfield01041982}. Hinton and Sejnowski~\cite{geoffreye.hintonterrencej.sejnowski1983} also proposed the Boltzmann machine. An example Boltzmann Machine is shown in figure \ref{F:Boltzmann-Machine}, we see that the Boltzmann Machine can be recurrent, expressing complex dependencies between variables. This recurrence makes inferring the state of a subset variables based on knowledge of another subset non-trivial as the size of the network grows.
\begin{figure}[h]
\begin{center}
  \includegraphics[width = 0.4\textwidth]{Assets/Boltzmann_Machine.png}
\caption{A Boltzmann Machine, the blue shaded nodes representing the observed variables, and the non-shaded nodes the latent variables.}
\label{F:Boltzmann-Machine}
\end{center}
\end{figure}

\subsection{Restricted Boltzmann Machines}

A Boltzmann Machine's architecture can be altered to alleviate inference shortcomings. The restriction, originally proposed by \cite{Smolensky:1986vy}, and then later revitalised with a training algorithm that operates on the deeper architecture of the DBN~\cite{geoffreye.hintonterrencej.sejnowski1983}. The restriction requires the network to be a two layer bipartite network, each layer corresponding to the observed (visible) and latent (hidden) units. Connections are forbidden between the layer of hidden units and the layer of visible units respectively. An example Restricted Boltzmann Machine architecture is shown in figure~\ref{F:Restricted-Boltzmann-Machine} (with the biases omitted for simplicity.). The collection of hidden units, forming a layer are referred to as the \texttt{hidden layer}. The collection of visible units are referred to as the \texttt{visible layer}.

\begin{figure}[h]
\begin{center}
  \includegraphics[width = 0.6\textwidth]{Assets/RBM_Example.png}
\caption{An example Restricted Boltzmann Machine with four hidden units, and five visible units. Note that the edges between units are not directed --- representing a dependency not a cause. This means that the weights are symmetric.}
\label{F:Restricted-Boltzmann-Machine}
\end{center}
\end{figure}

The probability of the RBM (again ignore biases) being in a given configuration is the joint probability of $h$ and $v$:
$$ P(h,v) = \frac{1}{Z} \prod_{j,i} e^{h_jv_iW_{ji}} $$
Taking logs this becomes:
$$ \log P(h,v) = \log \sum_{j,i} h_jv_i  W_{ji}  - \log Z $$
$ Z $ is the partition function, which normalises the probability of the joint. Calculating this would require summing over all $2^N$ configurations of $h$ and $v$, which is intractable for practical numbers of units. For instance a 28 by 28 image corresponds to 784 visible units, and for say 10 hidden units, this would amount to $ 2^{784} * 2^{10} $ possible configurations. We opt to work in terms of $P^\star$ which is the non-normalised probability of the joint over $h \text{ and } v$.
% So we arrive at:

\begin{equation}\label{eq:LogPJoint}
   \log P^\star(h, v) = \log \sum_{i,j} {h_j v_i W_{ji}}
\end{equation}

\section{Sampling in Generative Models}

\subsection{Why sampling is important}

Sampling is used when the distribution we want to work with is intractable to calculate analytically, which happens to be the case in the model proposed in this report. As mentioned in \ref{S:Generative-Models}, the power of generative models is their ability to represent, reconstruct and be trained on data. These tasks all require sampling from configurations/distributions of the hidden and visible units, often conditioned one or the other. There are two cases:
\begin{itemize}
  \item Sampling from $P(v|h)$, which is known as running the generative model, where the model \emph{generates} data based on a hidden representation.
  \item Sampling from $P(h|v)$, which is known as \emph{inverting} a generative model (This is also reffered to as \emph{inference}). The process is called `inverting' because instead of the model generating the data (sampling from $P(v|h)$) we instead try to infer a representation given data $P(h|v)$. It is the process of reasoning about what we do not know, given that of which we do.
\end{itemize}

Sampling from $P(v|h)$ and $P(h|v)$ is required to train generative models, as often the gradient to be climbed/descended involves calculating a probability over all the units in the generative model. Training a network based generative model involves calculating a weight update, which in turn requires inferring a hidden representation given a training item. The converse, sampling from a $P(v|h)$ is also required~\cite{dempster1977maximum}.

\subsection{The sampling technique: Gibbs Sampling}

Gibbs sampling is a special case of Markov Chain Monte Carlo~\cite{hastings70}, a technique for drawing samples from a complex distribution. Sampling from the probability mass (or `joint distribution`) of a generative model is a common use case for Gibbs sampling~\cite{Pearl:1988:PRI:52121}.

Gibbs sampling explores the desired probability distribution, taking samples of that distribution's state. One must leave iterations of `exploration' (of the probability mass) between drawing of a sample to ensure that the samples are independent~\cite{casella1992explaining}. The process of taking a step between states is referred to as a \texttt{Gibbs iteration} or a \texttt{Gibbs Step}. Formally the algorithm is described in algorithm~\ref{Alg:Gibbs-Sampling}.

\begin{algorithm}[!ht]
 \KwData{A vector $x$ indexed by $j$.}
 \KwResult{Gibbs sampling algorithm}
 Let $ x_{\smallsetminus} j$ be all components that make up $x$ vector except $x_j$\;
 initialization, begin with $x$, we are going to get a sample $x'$\;
  \For{$k$ many iterations}{
   \For{each component in $x$, $x_j$}{
     Draw a sample, $x_j'$ from $P(x_j| x_{\smallsetminus} j)$\;
     Update the current value of $x_j$ in $x$ with $x_j'$\;
   }
  }
 \caption{The Gibbs Sampling Algorithm}\label{Alg:Gibbs-Sampling}
\end{algorithm}

\subsubsection{Mixing Time of the Gibbs Sampler}

MCMC methods aim to approximate a distribution, by exploring likely states. As we often start this process from a random state, it's important that enough Gibbs steps are taken before a sample is drawn. This is because the random state may not be close to any part of the true distribution we want to sample from, so by running the chain for many iterations we increase the likelihood of samples being from the desired distribution.

This process of waiting for enough steps to before drawing samples is referred to as the \texttt{Mixing Time}. Gibbs sampling is used for performing inference in RBMs~\cite{Hinton:2006:FLA:1161603.1161605} and as result also in the ORBM. The \texttt{mixing time}, that is how many Gibbs iterations are needed to reach a satisfactory sample is an important part issue in the ORBM, in that one Gibbs step was sufficient in practice for training and using an RBM. The new generative model is not so fortunate.

\subsection{Sampling in a Sigmoid Belief Network}

Sampling from $ P(v|h) $ (known as Ancestral Sampling in a SBN) is extremely efficient in a SBN, it does not need Gibbs sampling. It can be expressed as
\begin{equation}\label{eq:SBN-v-given-h}
P(v|h) = \sum_i v_i \log \sigma_i(h) + (1-v_i)log(1-\sigma_i(h))
\end{equation}
As this process is calculated by propagating probabilities from the root hidden causes, it is known in a SBN as Ancestral Sampling.

Conversly, sampling from $P(h|v)$ in an SBN is intractable. Performing inference in a Sigmoid Belief network would allow source separation, as each hidden unit could represent a simple cause. The SBN would be shown an input, and could extract representations of the seperate causes that likely gave rise to the input. There exist algorithms for performing inference in Sigmoid Belief Networks. For instance, the Belief Propagation algorithm proposed by Judea Pearl~\cite{Pearl1982} operates on this encoding, calculating the probabilities of a given network state (i.e. the state of all the variables). As well as constraining the architecture to be a Directed Acyclic Graph, Belief Propagation is intractable to use as the number of variables grow~\cite{elidan2007using}. As we want to work with cases with upwards of 100 visible and hidden nodes, these algorithms break down.

Despite the Sigmoid Belief Network being expressive and providing a succinct encoding of conditional probabilities, Gibbs sampling is intractable for SBNs of practical size~\cite{Jensen95blockinggibbs}. The issue being that the Gibbs chain takes too long to mix~\cite{neal1992:connectionist}, which arises from the the `explaining away effect`~\cite{Hinton:2006:FLA:1161603.1161605}. Calling back to the example in section \ref{S:Explaining-Away}, instead of a single \texttt{burglar} or \texttt{earthquake} becoming dependent given the \texttt{alarm}, there is more in the realm of hundreds of burlgars and earthquakes all suddenly dependent.

Being able to efficiently sample from $P(h|v)$ is also required for training generative models~\cite{elidan2007using} making Sigmoid Belief Networks impractical for not only inference, but also training.

\subsection{Gibbs Sampling in a Boltzmann Machine}

Performing Gibbs sampling appears trivial in a Boltzmann Machine, in that to find the probability of a given unit being active, a weighted input to that node is passed through a sigmoid function. However, in practice the recurrent nature of Boltzmann Machines makes sampling intractable, as updating a node will change the probabilities of those connected. This requires a long Gibbs chain to sample from, intractably long in practice.
%However, it was shown that given unlimited training time Boltzmann Machines could be trained, out performing the state of the art models of the time \todocite{This}.

Recall that $ x_{\smallsetminus} j$ be all components that make up $x$ vector except $x_j$ and that a Boltzmann Machine has symmetric weights ($ W_{ji} = W_{ij} $),
$$
P(x_j = 1, x_{\smallsetminus}j) = \frac{1}{1 + e^{-\sum_i w_{ji}x_i}}
$$
That is, Gibbs sampling in a Boltzmann Machine amounts to use the Sigmoid function of the weighted inputs~\cite{neal1992:connectionist}.

\subsection{Gibbs Sampling in RBMs}\label{S:Gibbs-Sampling-RBM}

A big payoff for the restriction in an RBM is inverting the model becomes tractable, as the latent variables no longer become dependant given the observed ones. This is illustrated in figure~\ref{F:Restricted-Boltzmann-Machine} the hidden unit $h_1$ is not dependent on $h_2$, regardless if we know anything about the visible units. Firstly, this is the opposite of a SBN, where knowledge of the visible units makes the hidden units dependent. Secondly, by removing the recurrence present in Boltzmann Machines, it reduces the expressiveness of the RBM network. This does make the RBM useable in practice, as the Gibbs sampling process can stop after one Gibbs step\cite{fischer2014training}.

In order to describe Gibbs sampling in the new architecture proposed, it must first be explained for a standard RBM --- The process of Gibbs sampling is as follows:
  \begin{itemize}
    \item One must sample from $P(h|v)$ giving a hidden state $h'$
    \item Using this hidden state, a visible state is then generated, $v'$, by sampling from $P(v'|h')$. This process of generating a hidden pattern, and subsequent visible pattern is referred to as a Gibbs step.
    \item This chain of Gibbs steps between sampling from $P(h|v)$ and $P(v|h)$ can then be repeated as desired, the longer the chain the closer the samples will be to the true joint distribution that the model has learnt. For training an RBM Hinton\cite{Hinton:2006:FLA:1161603.1161605} showed that one step is often enough in practice, as one step is enough to infer a direction to adjust the weights in.
  \end{itemize}
  The process of updating the hidden, then visible layers forms what is referred to as the \texttt{Gibbs Chain} and is visualised at layer level in Figure \ref{F:Gibbs_Chain}.
  \begin{figure}[h]
    \begin{center}
      \includegraphics[width=0.8\textwidth]{Assets/RBM-Gibbs-Chain.png}
    \end{center}
    \caption{A figure illustrating a Gibbs chain where left to right indicates a Gibbs iteration. Note this is \emph{not} a PGM.}
    \label{F:Gibbs_Chain}
  \end{figure}

  \subsubsection{The Gibbs update}\label{S:Gibbs-Update}

    Denote the Gibbs sampler's probability of setting a variable, $x_j$ to 1 as:
    \begin{equation}\label{eq-p1-gibbs-full}
    P_{Gibbs}(h_j = 1 | v, h_{k != j})
    \end{equation}
    We can refer to the proability in equation \ref{eq-p1-gibbs-full} as $p_1$ and the converse $P_{Gibbs}(h_j = 0 | v, h_{k != j})$ can be referred to as $p_0$.
    Then, by the product rule of probabilities:
    $$
    \begin{aligned}
    \frac{p_1}{p_0} &= \frac{P^\star(h,v \text{ where } h_j = 1)}{P^\star(h,v \text{ where } h_j = 0)}\\
    &= \frac{p_1}{1 - p_1} \\
    % \text{Rearranging to give p_1 leads to} \\
    p_1 &= \frac{1}{1 + \frac{P^\star(h,v \text{ where } h_j = 0)}{P^\star(h,v \text{ where } h_j = 1)} }
    &= \frac{1}{1 + e^{-\psi_j}}
  \end{aligned}
    $$
  To update a hidden unit $h_j$
  we find $ P(h_j = 1 | v) $ where $v$ is an input pattern. In the context of an image, $ v $ would be the pixel values where each pixel corresponds to a visible unit, $v_i$.
  The probability of a given hidden unit activating is:
    \begin{equation}\label{eq:Hid-Gibbs-Update}
    P(h_j = 1 | v) = \sigma(\psi_j)
    \end{equation}
    Where $\psi_j$ is the weighted sum into the $jth$ hidden unit and $\sigma()$ is the Sigmoid function, or it also known as the Logistic function $\sigma(x)=1/(1+e^{-x})$. Figure \ref{F:PSI} illustrates $\psi_j$ for an example RBM.
    \begin{figure}[h]
    \begin{center}
      \includegraphics[width = 0.8\textwidth]{Assets/PSI_and_PHI.png}
    \caption{A diagram showing $\psi_j$, the weighted sum into the $jth$ hidden unit. Note that $W_{0j}$ is the hidden bias, represented as a unit that is always on with a weight into each hidden unit.}
    \label{F:PSI}
    \end{center}
    \end{figure}
    As the weights are symmetric, sampling from the visible layer, given a hidden state is similar. That is $P(v_i = 1 | h)$, where $h$ is the entire hidden vector is given by:
    \begin{equation}\label{eq:Vis-Gibbs-Update}
     P(v_i = 1 | h) = \sigma(\phi_{i})
    \end{equation}
    Where $\phi_i$ is the weighted sum into the $ith$ visible unit, which is: $ \phi_i = \sum(W_{ji}h_{j}) + W_{0i} $. Both $\phi_j$ and $\psi_i$ can be expressed in alternative, but useful way:
    \begin{equation}
    \phi_j = \log P^\star(v,h | v_i = 1) - \log P^\star(v,h | v_i = 0)
    \end{equation}
    \begin{equation}\label{psi-gibbs-update-rbm}
    \psi_i = \log P^\star(h,v | h_j = 1) - \log P^\star(h,v | h_j = 0)
    \end{equation}

\subsubsection{Reconstructions: visualising what the RBM has learnt}\label{SS:RBM-Reconstructions}

RBMs create an internal representation given an input by sampling from $P(h|v)$. They can also generate a faux input given an internal representation (a $h$). Performing one Gibbs iteration, that is, sampling from the hidden units given a `clamped' input $ P(h|v) $ and then taking the generated hidden state and generating a faux input (sampling from $P(v|h_{sampled})$) results in a \texttt{reconstruction}. Clamped input is where the visible units are set to be an input pattern. The model tries to reconstruct the input based on the internal representation it has learnt to model.

\subsubsection{RBM Fantasies: The Free-Phase of a Generative model}\label{S:Dreams}

In the same way that a Generative model uses reconstructions to try and recreate the supplied input vector, performing many Gibbs iterations with no input pattern clamped and taking a sample is referred to as free-phase sampling of the model. It allows the samples to explore the probability mass that has been built by the model during training. Sampling from these wanderings creates what are referred to as `fantasies` or `dreams`.


\subsection{Countermeasures}

\begin{frame}[t]
\begin{center}\Large Algebraic Security (1/3)\end{center}

\begin{columns}
    \setlength{\leftmargini}{0.25cm}
    \setlength{\leftmarginii}{0.5cm}
    \column{0.1\linewidth}{}
    \column{0.35\linewidth}{
        {\large Security Model:}
        \vspace{0.25cm}
        \begin{enumerate}
            \item<1-> \alert{random} bits allowed \\
                \begin{itemize}
                    \item as in classic masking
                    \item model~\alert{unpredictability}
                    \item in WB impl. as {\bf pseudorandom}
                \end{itemize}

            \item<2-> {\bf Goal:} \\
                  any $f \in span\{v_i\}$ is \alert{unpredictable}

            \item<3-> {\bf isolated} from obfuscation problems
        \end{enumerate}
    }
    \column{0.5\linewidth}{
        \includegraphics[height=7cm]{MaskedCircuitWB.png}
    }
\end{columns}
\end{frame}

\newcommand\bias{\varepsilon}
\newcommand\adv{\mathcal{A}}
\newcommand\Advan{\mathsf{Adv}}

\begin{frame}[t]
\begin{center}\Large Algebraic Security (2/3)\end{center}

\begin{columns}
    \setlength{\leftmargini}{0.25cm}
    \setlength{\leftmarginii}{0.5cm}
    \column{0.1\linewidth}{}
    \column{0.35\linewidth}{
        {\large Adversary:}
        \vspace{0.25cm}
        \begin{enumerate}
            \item<1-> chooses plaintext/key pairs

            \item<2-> chooses $f \in span\{v_i\}$
            \item<3-> tries to {\bf predict} values of this function \\
                  (i.e. before random bits are sampled)

            %\item<4-> succeeds,\\
            %      if \alert{only} $f$ matches
        \end{enumerate}
    }
    \column{0.5\linewidth}{
        \includegraphics[height=7cm]{MaskedCircuitWB.png}
    }
\end{columns}
\end{frame}

\begin{frame}
\begin{center}\Large Algebraic Security (3/3)\end{center}

\CenterBlock{10cm}{
\begin{block}{Proposition}

Let ${\color{blue}F} = \{f(x,\cdot,\cdot) ~\mid~ f(x,r_e,r_c) \in span\{v_i\},~ x \in \mathbb{F}_2^N\}.$

Let ${\color{red}e} = -\log_2{\big(1/2 + \max_{f \in {\color{blue}F}}{bias(f)}\big)}$.

Then for any adversary $\adv$ choosing $Q$ inputs
$$
    \Advan[\adv] \le min(2^{Q-|r_c|}, 2^{-{\color{red}e}Q}).
$$
\end{block}
}

\pause

\CenterBlock{10cm}{
\begin{block}{Corollary}
Let ${\color{green!40!black}k}$ be a positive integer. Then for any adversary $\adv$
$$
    \Advan[\adv] \le 2^{-{\color{green!40!black}k}} \text{~if~}
    e > 0 \text{~and~} |r_c| \ge k\cdot(1+\frac{1}{{\color{red}e}}).
$$
\end{block}
}

\pause

\center{\bf Information-theoretic security!}
\end{frame}

\begin{frame}[t]
    \Title{Minimalist Quadratic Masking Scheme}
    \begin{columns}
    \setlength{\leftmargini}{0.5cm}
    \column{0.1\linewidth}{}
    \column{0.3\textwidth}{
        \only<1>{
            {\Large Masking scheme}
            \vspace{0.25cm}
            \begin{itemize}
                \item \alert{quadratic} decoder:\\
                      $(a,b,c) \mapsto ab\oplus c$
                \item set of {\bf gadgets} \\
                \item provably secure {\bf composition}
            \end{itemize}
        }
        \only<2->{
            {\Large Security}
            \vspace{0.25cm}
            \begin{enumerate}
                \item algorithm to verify \\
                      that bias $\ne 1/2$
                \item max. degree on $r$: 4
            \end{enumerate}
            \onslide<3->{

            \vspace{0.3cm}

            ~~~~$\Rightarrow$ bias $\le 7/16$

            \vspace{0.3cm}

            ~~~~for 80-bit security \\
            ~~~~we need $|r_c| \ge 940$
            }
        }
    }
    \column{0.5\textwidth}{
        \input{figures/mqms.tex}
    }
    \end{columns}
\end{frame}

\begin{frame}
\Title{Proof-of-concept masked AES-128}

\CenterBlock{10cm}{
\vspace{0.5cm}
\begin{enumerate}
    \item MQMS + 1-st order Boolean masking
    \item 31,783 $\to$ 2,588,743 gates expansion (x81)
    \item 16 Mb code / 1 Kb RAM / 0.05s per block on a laptop
    \item (unoptimized)
\end{enumerate}
}

\vspace{0.5cm}

\center{\Large
    \href{https://github.com/cryptolu/whitebox}{github.com/cryptolu/whitebox}
}

\end{frame}


If you haven't downloaded and unzipped \href{https://libaoj.in/courses/2021f/MATH3341/zip/Math.3341.zip}{\texttt{Math.3341.zip}}. Download and unzip it under \verb|H:| (H Drive if you are working on the Remote Lab). Change the current working directory by typing \verb|cd H:\Math.3341\Math.3341.Lab.05| in the Command Window, and type \verb|edit lab_05_script| in the Command Window to edit \verb|lab_05_script.m|.

%---------------------------------------------
\section{Formatting Numerical Values}
%---------------------------------------------
\begin{enumerate}[(a)]
    \item Define a variable \verb|x|, of which the value is $e^{\pi}$.
    \item Define a cell array \verb|formatOptions|, of which the entries are listed as follows:
        \begin{enumerate}[(1)]
            \item \verb|rat|
            \item \verb|longeng|
            \item \verb|longg|
            \item \verb|longe|
            \item \verb|long|
            \item \verb|shorteng|
            \item \verb|shortg|
            \item \verb|shorte|
            \item \verb|short|
        \end{enumerate}
    \item Use a for-loop to output \verb|x| in the above formats (do \textsc{not} change the order).
\end{enumerate}
%---------------------------------------------
\section{Formatting Data using \lstinline[style=MATLAB]{fprintf}}
%---------------------------------------------
\begin{enumerate}[(a)]
    \item Define \verb|x| to be column vector ranging from $0$ to $2 \pi$ with $25$ entries, and define \verb|y1|, \verb|y2|, \verb|y3| as follows
        $$
        y_1 = \sin(x/2), \quad
        y_2 = \sin(x), \quad
        y_3 = \sin(2x).
        $$ \item Concatenate column vectors \verb|x|, \verb|y1|, \verb|y2|, \verb|y3|, and store the new 2-D array to \verb|data|.
    \item Print out the heading in the Command Window using \verb|fprintf|, where the heading of the output is \verb|x|, \verb|sin(x/2)|, \verb|sin(x)|, \verb|sin(2x)|, whose widths are $9$. The heading should be left-justified.
    \item Then use a for-loop to loop over each row of \verb|data|: use \verb|fprintf| to print out the numerical values, which have width $9$ with $6$ decimal places, in the Command Window. All numerical values should be left-justified.
\end{enumerate}

%---------------------------------------------
\section{Formatting Data for \LaTeX{}}
%---------------------------------------------
This part we will format \verb|data| (defined above) for \LaTeX{}.
\begin{enumerate}[(a)]
    \item Set the output filename to \verb|sin.tex|, and the permission to \verb|w| (write mode) in \verb|fopen| and store the file handle to the variable \verb|fileHandle|.
    \item Use \verb|fprintf| to print out the setup for \verb|table| and \verb|tabular| environments. The output should be as follows
        \begin{lstlisting}[style=TeX]
\begin{table}[!hbtp]
\centering
\caption{Sine functions}
\label{tab:sin}
\begin{tabular}{lcrr}
\toprule
\midrule
\bottomrule
\end{tabular}
\end{table}
        \end{lstlisting}
    \item Print out the heading of the data, whose column width is $11$ between \verb|\toprule| and \verb|\midrule|. The expected output is as follows:
        \begin{lstlisting}[style=TeX]
        $x$ & $\sin(x/2)$ &   $\sin(x)$ &  $\sin(2x)$ \\
        \end{lstlisting}
    \item Print out the numerical values of each row in \verb|data| between \verb|\midrule| and \verb|\bottomrule| using a for-loop. Each number has width $9$ and $6$ decimal places. Also each number should be enclosed by a pair of \verb|$| and seperated by \verb|&|. The expected output for one of the rows should be as follows
        \begin{lstlisting}[style=TeX]
$ 0.000000$ & $ 0.000000$ & $ 0.000000$ & $ 0.000000$ \\
        \end{lstlisting}
    \item Print the content of \verb|sin.tex| by calling \verb|type('sin.tex')|.
\end{enumerate}
%---------------------------------------------
\section{Plotting Multiple Functions using for-loop}
%---------------------------------------------
\begin{enumerate}[(a)]
    \item Define a cell array \verb|styles|. The elements are plotting styles, i.e.,
        \begin{enumerate}[(1)]
            \item solid line with circle as the marker;
            \item dashdot line with diamond as the marker;
            \item dashed line with triangle (up) as the marker.
        \end{enumerate}
    \item Define another cell array \verb|y|, of which the entries are \verb|y1|, \verb|y2|, and \verb|y3|.
    \item Then use a for-loop to plot each entries of \verb|y| versus \verb|x| with in the same figure window the above styles (in the same order).
    \item Set legend, labels, grid, and title. Change the range of $x$-axis to $[0, 2\pi]$, and that of $y$-axis to $[-1, 1]$. Set the following properties as you did in last lab. The expected result is shown in Figure \ref{fig:sin}.
    \begin{itemize}
        \item \verb|XTick| to \verb|[0, pi / 2, pi, 3 * pi / 2, 2 * pi]|;
        \item \verb|XTickLabel| to \verb|{'0', '$\pi/2$', '$\pi$', '$3 \pi/2$', '$2\pi$'}|;
        \item \verb|GridLineStyle| to \verb|'--'|;
        \item \verb|Box| to \verb|'on'|;
        \item \verb|BoxStyle| to \verb|'full'|.
    \end{itemize}
    \item Then save the plot using the following lines of commands:
        \begin{lstlisting}[style=MATLAB]
name = 'lab_05_plot';
fig = figure(1);                         % Set figure i as current figure window
set(fig, 'PaperPositionMode', 'auto');   % Set paper position mode to 'auto'
pos = get(fig, 'PaperPosition');         % Get figure window paper position
set(fig, 'PaperSize', [pos(3) pos(4)]);  % Set figure paper size
print(fig, '-dpdf', name);               % Save figure
        \end{lstlisting}
\end{enumerate}
%---------------------------------------------
Type \verb|diary('lab_05_output.txt')| in the Command Window, run the script file \verb|lab_05_script.m|, and type \verb|diary off| in the Command Window. Upload \verb|lab_05_output.txt|, \verb|sin.tex|, and \verb|lab_05_script.m| to the folder \verb|src| on Overleaf.

On Overleaf, open \verb|body.tex| under the folder \verb|LaTeX|. In the last section of the report, you will reproduce Section \ref{sec:bol} using \LaTeX{}. You may find the following helpful:

\begin{itemize}
    \item You may use enviroments such as  \verb|align|, \verb|figure|, and \verb|table|.
    \item You may use \verb|\includegraphics[width=amount unit]{/path/to/figure.pdf}| to specify the width of a figure. In our case, the width of the figure is \verb|0.75\textwidth|.
    \item For special characters, you may look them up in \href{https://libaoj.in/files/LaTeX.Mathematical.Symbols.pdf}{\LaTeX{}.Mathematics.Symbols.pdf}.
    \item You may use \verb|\input{/path/to/sin.tex}| to include the table you got from MATLAB.
\end{itemize}

Recompile and submit the PDF file generated by Overleaf to WyoCourses.
%---------------------------------------------

\newpage
\section{Basics of \LaTeX{}}
\label{sec:bol}
\subsection{Sine functions}
For given $x \in [0, 2\pi]$ with step size $\pi/12$, we can obtain the evaluations of \eqref{eq:y1}, \eqref{eq:y2}, \eqref{eq:y3} at $x$ (see Table \ref{tab:sin}), and the corresponding plot (see Figure \ref{fig:sin}).
\begin{align}
y_1 & = \sin(x/2) \label{eq:y1} \\
y_2 & = \sin(x)   \label{eq:y2} \\
y_3 & = \sin(2x)  \label{eq:y3}
\end{align}
\input{../Math.3341.Lab.05.ans/sin.tex}
\begin{figure}[!hbtp]
    \centering
    \includegraphics[width=0.75\textwidth]{../Math.3341.Lab.05.ans/lab_05_plot.pdf}
    \caption{Sine functions}
    \label{fig:sin}
\end{figure}


\documentclass[a4paper,12pt]{report}

\usepackage{amsmath,amsfonts,mathtools}
\usepackage{amssymb}
\usepackage{hyperref}

\begin{document}
\title{PHY293 Abridged}
\author{Aman Bhargava}
\date{September 2019}
\maketitle

\tableofcontents

\section{Introduction}
Prof. Grisouard has put up some pretty solid
\href{https://github.com/PHY293-Grisouard/Chapters-2019}{notes} written in jupyter notebooks.
Those offer a great comprehensive guide for the course contents. The goal here is to give
a very concise overview of the things you need to know (NTK) to answer exam questions. Unlike
some of our other courses, you don't need to be very intimately familiar with the derivations
of everything in order to solve the problems (though it certainly doesn't hurt). Think of this
as a really good cheat sheet.

\chapter{Simple Harmonic Oscillators (SHO's)}
\section{Set Up}
Pretty much the same as covered last year.
$$F_{restorative} = -kx$$
$$F_{net} = ma$$
$$ma + kx = 0$$
We let $\omega_0^2 = k/m$ and rename $a$ to $\ddot{x}$
\section{ODE and Solution}
\paragraph{ODE:}
$$\ddot{x} + \omega_0^2 x = 0$$
\paragraph{Solution:}
$$x(t) = a cos(\omega_0 t + \theta)$$
Solve for 2 unknowns (usually $a$ and $\theta$) based on two initial conditions.

\chapter{Dampened Harmonic Oscillators (DHO's)}
\section{Set Up}
We have all the same forces from last time plus the force of friction.

$$F_{friction} = F_f = -bv$$

\section{ODE and Solutions}
\paragraph{ODE}
We let $\gamma = b/m$ so that:
$$\ddot{x} + \gamma \dot{x} + \omega_0^2 x = 0$$

\subsection{General Solution}

And for underdampened and over dampened, the overall solution looks like:
$$x(t) = ae^{rt}$$
Where $a, r \in \mathbb{C}$, remembering that $$e^{i \theta} = i sin(\theta) + cos(\theta)$$

This is actually really cool, because the existence of a complex component in $r$ is what
enables oscillation.

If we populate the initial ODE with the derivatives of our $x(t)$, we get:
$$ar^2e^{rt} + \gamma are^{rt} + \omega_0^2 ae^rt = 0$$
$$r^2 + \gamma r + \omega_0^2 = 0$$

This results in a simple quadratic for which you can solve for $r$ using the quadratic formula.

$$x = \frac{b \pm \sqrt{b^2 - 4ac}}{2a}$$
$$r = \frac{\gamma \pm \sqrt{\gamma^2 - 4w_0^2}}{2}$$

There will be up to 2 $r$ values. Since the solution space is a 2-D vector space, any
linear combination of vectors (i.e. values of $r$) is still a valid vector.

You might be wondering how $a$ figures into this whole thing. That's just based on initial
conditions, really.

\subsection{Regieme 1: Underdampened}
If the dampening is weak enough, there will still be \textbf{some} oscillation before the
oscillator comes to rest. In this case, the general solution is:

$$x(t) = A_0 e^{-\gamma t/2} cos(w_dt + \phi)$$
Where $w_d^2 = w_{dampened}^2 = \omega_0^2 - \frac{\gamma^2}{4}$

You get this by solving for $r$ with the quadratic formula, plugging the two imaginary
roots in, then taking just the real components because you know that $x(t)$ is real.

\subsubsection{Logarithmic Decrement}
This quantity is the $ln$ of the ratio of 1 peak to the next.

$$\frac{\gamma T_d}{2}$$

Where $T_d$ is the period based on $w_d$.

\subsection{Regieme 2: Heavily Dampened}
In this case both roots are real. This occurs when $\gamma^2-4\omega_0^2 > 0$

Let $r_p, r_m$ be the two real roots. Then the solution is:

$$x(t) = a_pe^{r_pt} + a_me^{r_mt}$$

You can solve for the a values at leisure with two initial conditions.

\subsection{Regieme 3: Critically Dampened}
When the discriminant of the quadratic is zero (i.e. $\gamma^2 = 4\omega_0^2 = $), you
get an overconstrained problem because you have fewer unknowns than you have constraints.
Therefore, the new solution for this exact case is:

$$x(t) = (A + Bt)e^{-\gamma t/2}$$

And it decays the fastest and is pretty cool. Yay.

\section{Energy}
$$E = K + U$$
$$E = \frac{1}{2}mv^2 + \frac{1}{2}kx^2$$
$$\frac{dE}{dt} = -bv^2$$

\subsection{Underdampened Energy}
The equations get really messy so we assume that $\omega_0 >> \gamma/2$ and $\omega_0 = \omega_d$. After
you sub everything in and use the pythagorean identity to cancel out the squared sine/cosine,
you get:

$$E = \frac{1}{2} kA_0^2e^{-\gamma t}$$

\subsection{Critical and Overdampened Energy}
These ones are really easy, they just decay and that's it. Just remember the equations for
energy and solve for the velocity equation.

\section{Q-Factor}
$Q$ is a measure of the tendency to oscillate dividied by the tendency to dampen.
$$Q = \frac{\omega_0}{\gamma}$$
Let $\tau = \frac{1}{gamma}$ be proportional to the lifetime of the oscillator. Then
$n = \frac{\tau}{T_0}$ is the number of cycles in a lifetime.
$$Q = 2\pi n$$

$Q$ is also proportional to the rate at which dampening removes energy from the system. Then
$$E_n = E_0e^{-\gamma t}$$
Represents the amount of mechanical energy in the system at time $t$.
$$\frac{E_n - E_{n+1}}{E_n} = ... = \frac{2\pi}{Q}$$

Therefore $Q$ is the energy in oscillator divided by the amount of energy lost at the
following \textbf{radian}.

\chapter{Forced Oscillators}
\section{Equation of Motion ODE}
$$\ddot{x} + \omega_0^2 x = F_0' cos(\omega t)$$
Where $\omega_0^2 = k/m$, $F_0' = F_0/m$, $\omega$ is the driving frequency.

\section{Solution: Undamped Forced Oscillator}
$$x(t) = A(\omega) cos(\omega t - \delta(\omega))$$
Where
$$\delta(\omega_0) = 0 \,\,if\,\, \omega < \omega_0, else \pi$$
If you shake it too fast, it'll vibrate in exactly opposite phase.
$$A(w) = |\frac{\omega_0^2}{\omega_0^2 - \omega^2}A_f|$$

\subsection{$\omega = \omega_0$}
Perfect resonance makes amplitude tend toward infinity.
$$x(t) = \frac{A_f \omega_0}{\sqrt{2}} t cos(\omega_0t - \frac{pi}{4})$$

\section{Solution: Forced Damped Oscillators}
Equation of Motion: $\ddot{x} + \gamma \dot{x} + \omega_0^2 x = A_f w_0^2 cos(wt)$

\paragraph{General Solution: } Same as before
$$x(t) = A(\omega) cos(\omega t - \delta(\omega))$$

\paragraph{Updates to $A$ and $\delta(\omega)$: }
$$A(\omega) = \frac{\omega_0^2}{\sqrt{\gamma^2\omega^2+(\omega_0^2 - \omega)^2}}A_f$$
$$tan[\delta(\omega)] = \frac{\gamma \omega}{\omega_0^2-\omega^2}$$

\paragraph{Maximum $A(w)$} occurs when $\omega = \omega_0^2 - \frac{\gamma^2}{2}$. In other words,
$$\omega_{max} = \omega_0^2[1-\frac{1}{2Q^2}]$$

$$\therefore A_{max} = \frac{Q}{\sqrt{1-\frac{1}{4Q^2}}}A_f$$

\section{Transient Response}
When you suddenly start a damped, forced oscillator, you have one mode that is the long-term response and one
that is the transient response (as if there is no forcing).

$$x_{free} = A_{free} e^{-\gamma t / 2}cos(\omega_d t + \phi)$$
$$x_{forced} = A(\omega) cos(\omega t - \delta(\omega))$$

$x_{free}$ tend toward zero and only exists for the beginning, so it is the transient response.

\section{Power in FDHO's}
$$\therefore -\dot{E}(w, t) = P(w, t) = bv^2 = bV^2(w)sin^2(wt-\delta)$$
Where $V$ is the amplitude of the velocity.

\subsection{Average Power Per Cycle}
$$\bar{P}(w) = \frac{F_0^2}{2m} \frac{\gamma}{\gamma^2 + (\omega_0^2/\omega - \omega)^2}$$
\paragraph{Maximizing Case: } $\bar{P}$ is maximized when $w = w_0$

\subsection{Power vs. $\omega$}
Let us plot $\bar{P}(w)$ vs. $\omega / \omega_0$. The $\omega_{full width half height} = \omega_{fwhh} = \gamma$

\chapter{Coupled Oscillators}
\paragraph{Main Takeaways}
\begin{itemize}
\item Motion of coupled systems is a linear combination of orthogonal forms of motion.
\item Motion can be found via 2x2 matrix system
\item Each normal mode has a unique frequency common to all elements.
\item `Eigen' = `normal'.
\item \textbf{Beating} = when frequencies of modes are very close.
\end{itemize}

\subsection{How to Solve Coupled Systems}
From tutorial note:
\subsubsection{Determine Equations of Motion}
You must give each mass its own coordinate system where deviation in one direction adds to the zero
coordinate and deviation to the other subtracts from the zero coordinate.

Implement $f = ma$ and $f = kx$ to create equations of motion of each mass depending on its position
and the position of its neighbouring masses.

Now we have $$m\ddot{x_a} = ...$$ $$...$$ $$m\ddot{x_c} = ...$$

\subsubsection{Assume $\exists$ a normal mode}
This implies that $$x_a = A cos(wt)$$ $$...$$ $$x_c = C cos(wt)$$

All we need to solve for now is $w$, and $A \to C$. To do so, we sub our definitions for $x_a, x_b, and x_c$
into our initial EoM, which gives us relationships between $A, B, C, w$ (cosines cancel out!)

Now we just have to put everything in a matrix equation of the form:
$$[...][A, B, C] = [0, 0, 0]$$
$$[M][A, B, C] = [0, 0, 0]$$

which implies that $det(M) = 0$. This will give us $3$ different eigen frequencies (or however many degrees
of freedom we have). From there, we just have to solve for the coefficients $A, B, C$ via initial conditions,
remembering that $x_a = Acos(wt)$ and so on!

\subsection{Review: Results from Linear Algebra}
\begin{itemize}
\item $Av = \lambda v$, $(A-\lambda I)v = 0$
\item We get non-trivial solutions for $v$ if $det(A - \lambda I) = 0$
\item A matrix is diagonizable if $\exists n$ eigen values $\lambda$ (sufficient, not necessary)
\item Each solution to the initial $Ax = b$ is unique because the eigen vectors form a basis.
\end{itemize}

\paragraph{Connecting Eigen Things to Real Things}
\begin{itemize}
\item Each $\lambda_i = \omega_i^2$
\end{itemize}

\chapter{Standing Waves}
\paragraph{travelling wave: } a travelling disturbance where no matter travels on average.
\paragraph{Continuum: } infinite coupled oscillators $\to$ infinite modes can arise, characterized
by nodes and antinodes.

Boundaries give rise to standing waves (think about boundaries on a plucked guitar string).

\section{Expectations and Takeaways}
\begin{enumerate}
\item Wave equation is $$\frac{\partial^2 y}{\partial t^2} = v^2\frac{\partial^2y}{\partial x^2}$$
\item Phase speed equation is $$v = \sqrt{\frac{T}{\mu}}$$
\item \textbf{Seperation of variables} is when you
\item Wavenumbers $k_n$ are basically the number of antinodes in the given mode and angular frequencies $\omega_n$ are frequency at which mode $n$ virbrates,
wavelengths $\lambda_n$ are the physical length of each wave in the mode $n$.
\item Orthogonality relates how different modes of oscillations in standing waves don't interact.
\item Total energy = sum of energy in each mode of oscillation.
\end{enumerate}

\section{The Taut String}
Properties of a taut string:
\begin{itemize}
\item $tan\theta = \frac{dy}{dx} << 1$ where $x$ is along the string and $y$ is across the string.
\item Vibrations occur in only one plane
\item \textbf{Phase speed } is $v^2 = T/\mu$ with $T$ as \textbf{tension} and $\mu$ as \textbf{mass per unit length of string}.
\end{itemize}

\paragraph{WAVE EQUATION: } $$\frac{\partial^2 y}{\partial t^2} = v^2 \frac{\partial^2y}{\partial x^2}$$

\section{Solution to Wave Equation}
We assume that the solution is of the form $$y(x, t) = f(x)h(t)$$ so that it is seperable and works nicely
with the wave equation. When we sub this into the wave equation, we get:

Let $$\frac{1}{h(t)} \frac{d^2h(t)}{dt^2} = -w^2 = v^2 \frac{1}{f(x)} \frac{d^^2f(x)}{dx^2}$$

So we can still have a $w^2$ that fits well into our SHO ODE:

$$h(t) = H cos(wt + \phi)$$ $$f(x) = A cos(kx) + B sin(kx)$$ where $k = w/v$

\subsection{Boundary Conditions}
We know that $y(t, 0) = y(t, L) = 0$ because the string is secured on either end. This forces the $A$ term
in the equation $f(x) = A cos(kx) + B sin(kx)$ to be zero, so now $f(x) = B sin(kx)$. Now $f(kL) = 0$ for
any natural number $k$.

$$k_n = \frac{n \pi}{L} \,\,, \,\, n \in \mathbb{N}$$

Recall that $k = w/v$ so now $w_n = \frac{n \pi v}{L}$.

\subsection{Final Solution}
$$y_n(x, t) = C_n cos(w_n t + \phi_n)sin(k_n x)$$
$$y(x, t) = \sum_{n=1}^{\infty} y_n(x, t)$$

\subsection{Surrounding Math}
We need some tools to do proofs and reasoning about these standing and travelling waves.
$$f \odot g = \frac{2}{L} \int_0^L f(x)g(x) dx$$
which represents the inner product of the vector space and
$\delta_{mn}$ which is 1 when $m=n$.
$$sin(k_n x) \odot sin(k_m x) = \delta_{mn}$$

Because they are orthogonal to eachother.

\subsection{Energy Considerations}
$$E_n = \frac{1}{4} \mu L w_n^2 A_n^2$$

\chapter{Travelling Waves}
The real general solution to the wave equation:
$$\frac{\partial^2 y}{\partial t^2} = v^2 \frac{\partial^2 y}{\partial x^2}$$
$$y(x, t) = f(x-vt) + g(x+vt)$$

It can be shown via trig sums that this is the same as the standing wave solution when you
put it between two boundaries.

\paragraph{Most common travelling wave function: } $$y(x, t) = A sin(\frac{2 \pi}{\lambda} (x \pm vt))$$
$$= A sin(k(x\pm vt))$$
$$= A sin(kx \pm wt)$$
\begin{itemize}
\item $\lambda$ = wavelength
\item $k$ = wavenumber
\item $\omega^2 = v^2k^2$, k is the wave number.
\item $\frac{2\pi}{w}$ = period
\end{itemize}

\section{Energy Considerations}
$$K = \frac{1}{2} \mu (\frac{\partial y}{\partial t})^2$$
$$U = \frac{1}{2} \mu v^2 (\frac{\partial y}{\partial x})^2$$

$<>$ corresponds to integration over one wavelength, so $<f> = \int_0^{\lambda} f(x) dx$.

To find $<E>$ we can apply the above formulae for $K$ and $U$ to the general solution
$y(x, t)=A cos(kx-wt)$.

$$<E> = \frac{1}{2} \mu \lambda A^2 w^2$$

\section{Power Considerations}
Since these waves are unbounded travelling entites, it's useful to consider how much
energy they transport past a certain point per second.

We start with instantaneous energy $E = \mu A^2 w^2 sin^2(kx - wt)$. Then
$$\bar{P} = \frac{w <E>}{2 \pi} = \frac{\mu \lambda A^2 w^3}{4 \pi} = \frac{\mu A^2 w^2 v}{2}$$

\section{Reflection and Transmission}
When medium changes discontinuously, you get reflection and transmission. The boundary conditions
at the discontinuity are
\paragraph{Boundary Condition 1: } matching frequency, amplitude, and phase.
\paragraph{Boundary Condition 2: } transverse (across-string) forces must match.

Therefore we know that
$$y_T(x, t) = A_T cos(wt - k_t x)$$ $$y_R = A_R cos (wt + k_r x)$$

have the same $wt$ term, and $k_R, k_T > 0$ so they propagate in the right directions. The reflected
wave is in the same original medium, so its spatial frequency $k_R = k_I = w/v_1$ while $k_T = w/v_2$.

Now the only numbers that aren't known are $A_R, A_T$. According to boundary condition 1, $A_I + A_R = A_T$.
Now we use boundary condition to solve for
$$\frac{A_T}{A_I} = \frac{2k_1}{k_1+k_2} = \pmb{\tau_{12}} = \frac{2 \sqrt{\mu_1}}{\sqrt{\mu_1} + \sqrt{\mu_2}} $$
$$\frac{A_R}{A_I} = \frac{k_1 - k_2}{k_1 + k_2} = \pmb{\rho_{12}} = \frac{\sqrt{\mu_1} - \sqrt{\mu_2}}{\sqrt{\mu_1} + \sqrt{\mu_2}}$$

\section{travelling Waves in Multiple Dimensions}
The wave equation become: $$\frac{\partial^2 f}{\partial t^2} = v^2\nabla^2f = v^2[\frac{\partial^2 f}{\partial x^2} + \frac{\partial^2 f}{\partial y^2} + \frac{\partial^2 f}{\partial z^2}]$$

\chapter{Dispersion}
For non-dispersive waves (the ones we have been talking about up to this point), the following relationship is true for angular
frequency, wave number $k$, and velocity $v$: $$\omega^2 = k^2v^2$$

For dispersive waves, you have a non-linear relationship. Therefore different waves disperse at different rates.

Let's think about the phenomenon of beating: when you have similar frequency waves going past eachother, you get these ``beats'' from
interference patterns (look up a picture to solidify your understanding!).

Since we have different frequencies going at different rates, the beats now move! Let's have an example:

\section{Example of Dispersive Waves}
Let the following relationship hold for angular frequency and speed of a wave ($g$ is actually just gravity, this is a weird example)
$$\omega = \sqrt{gk}$$
$$v = \omega/k = \sqrt{g/k}$$

The only thing you can really be asked on the exam is to find the group velocity (according to Shayne, at least):
$$v_g = \frac{d\omega}{dk} \approx \frac{\Delta \omega}{\Delta k}$$

Group velocity is just the speed of the little wave packets. It's an absolute speed that a stationary observer would see. If you have
two different waves interfereing at $\omega_1$ and $\omega_2$, use the $\Delta$ form to find the speed of the packets!

\chapter{Special Relativity}
\section{Introduction}
An intuitive grasp of this stuff is pretty easy. Just watch the minute physics videos about it, they do a great job.

The key points are that light speed is constant no matter your frame of reference. In other words, it looks like $v_1 + c = v_1$,
regardless of what $v_1$ is. Therefore, the way you would think to transform a spacetime diagram to be from another person's
reference frame is not correct. You use this cool Lorentz transformation thing:

\section{Formulae}
\paragraph{The Lorentz factor} comes up all the time: $$\gamma(v) = \frac{1}{\sqrt{1-v^2/c^2}}$$
\subsection{Time Dilation}
Let's say you pass by some dude on a planet at close to the speed of light. Both of you will see the other acting really slowly. In other
words, \textbf{the moving clock is always observed to be slower than the stationary clock}. Therefore, for this relationship, $\Delta t'$
is just \textbf{the other clock}:
$$\Delta t' = \gamma(v)\Delta t$$

\subsection{Length Contraction}
Same thing as above applies with \textbf{the moving object is always observed to be scrunched up more than the stationary object}:
$$L=\frac{L_0}{\gamma(v)}$$

\subsection{Relativstic Events: Location and Time}
Let's say you're watching a dude running close to the speed of light ($v$) who is about to detonate a cake. If the dude does so at
position $x$ and time $t$ from his perspective, the position $x_{new}$ and time $t_{new}$ that you observe are given by the following
equations.
$$t_{new}=\gamma(t-\frac{v^2}{c^2}x)$$
$$x_{new}=\gamma(x-vt)$$

\subsection{Relativistic Energy and Momentum}
$$p=\gamma(v)m_0v$$
$$E = \gamma mc^2$$
$$KE=E-mc^2=\gamma mc^2-mc^2=(\gamma-1)mc^2$$

\paragraph{Invariant Energy: }
$$m_0^2c^4=E-p^2c^2$$

Apparently the doppler shift equation (relativistic) is given, so don't worry too much about that.

\subsection{4-Vectors}
In conventional 3-D space, you can have nice invariant properties of objects, like relative distances. In relatistic 4-D space
you can have nice invariant 4-vectors that tell you the relative distance of objects and events in 4-D space!

$$\Delta x = (c\Delta t,\Delta x, \Delta y, \Delta z) = (c\Delta t, \pmb{r})$$

\paragraph{The Inner Product} (the thing that is actually invariant) is given by:
$$A = (A_0, \pmb{A}); \, B = (B_0, \pmb{B})$$
$$A\cdot B=A_0B_0 - \pmb{A}\cdot \pmb{B}$$

Where the bolded A and B are a vector of the three spatial coordinates, the $A_0/B_0$ is the the $c\Delta t$ component of the 4-vector.

The other form of 4-vector is as follows:

$$...$$

\chapter{Quantum}
\section{Particle Energy, Wave Energy}
Here is a hodge podge of fun properties about waves (and particles, cause they're waves too). Shoutout to De Broglie.
\begin{itemize}
\item Energy of particle/wave with angular frequency: $E = \hslash \omega$
\item Momentum of particle/wave with angular frequency: $p = \hslash \omega/c$. Now is as good a time as any to tell you that $k=\omega/c$, which is really annoying and confusing.
\end{itemize}

\section{Compton Scattering}
When a high energy photon with wavelength $\lambda_0$ collides with an electron, the photon scatters of at angle $\theta$ with respect to its original direction. The wavelength of the photon
changes accordingly to $\lambda'$

$$\lambda' - \lambda_0 = \frac{R}{mc}(1-\cos\theta)$$
\begin{itemize}
\item $\lambda'$ is the final wavelength of the photon.
\item $\lambda_0$ is the initial wavelength of the photon.
\item $R$ is some constant we're given.
\item $m$ is the mass of the electron.
\item $c$ is the speed of light.
\item $\theta$ is the angle of deflection of the photon with respect to it's initial line of attack.
\end{itemize}

\section{Photoelectric Effect}
If you yeet some photons at a piece of metal, the electrons get ejected if the energy of the light packets is high enough (if the frequency of light is high enough).
$$E_x = \hslash \omega - \phi$$
where $\omega$ is the frequency of light and $\phi$ is the binding energy of the electrons. $E_x$ is the energy of the ejected electrons.

\section{Hydrogen Model}
$$\omega_{mn}=2\pi R_0 c(\frac{1}{n^2} - \frac{1}{m^2})$$

\section{De Broglie Waves}
$$\lambda = \frac{h}{p} = \frac{h}{mv}$$
$$E = \hslash \omega$$
$$p = \hslash k; \,\,\,\,\,\, k = \omega/c$$

\section{Wave-Particle Duality}
\subsection{Uncertainty Principle}
$$\Delta x\Delta p_x \geq 2\pi \hslash$$
$$$$
\subsection{Double Slit Experiment}
If you shine a laser through two slits (or anything really), the two slits will act as sources of the exact same frequency light and they will
interfere with eachother to create some diffraction patterns on wall. Difference in path length being an integer multiple of the wavelength makes
constructive interference, difference in path length of half makes for destructive.

$$m \lambda = d\sin \theta$$
Where \begin{itemize}
\item $m$ is some integer (0 is the central node, 1 is the next, etc.)
\item $\lambda$ is the wavelength of light (or particles) coming through.
\item $d$ is the distance between the slits.
\item When the equation holds true for integer value of $m$, you get constructive point. If it's an integer and a half, it's a destructive point.
\end{itemize}

\section{Black Body Radiation}
A body absorbs and emits radiation with the exact same efficiency for any given frequency. A black body is a perfect absorber and perfect emitter.
$$P=\sigma T^4$$
$P$ is the power per square meter of the emitter surface, $\sigma$ is the emissivity/absorptivity constant, and $T$ is the temperature.

$$f_{max} \propto T$$
In other words, the frequency of radiation that is absorbed/emitted most is proportional to temperature.

\chapter{Matter Waves and The Schrödinger Equation}
This problem is basically just $\pmb{A}\vec{x} = \lambda \vec{x}$ as follows:
$$(\frac{p^2}{2m} + V) \Psi = E\Psi$$
\begin{itemize}
\item $p$ is the momentum
\item $m$ is the mass
\item $V$ is a conservative field of potential energy..
\item $E$ is the energy.
\end{itemize}

\section{Normalization and Probability}
Solve for $N$ such that $$\int_{-\infty}^{\infty} |(N\psi(x))^2| dx = 1$$

The probability of the particle being between two points is just the integral between those of $\int_a^b |\psi(x)^2| dx$ as long as $\psi$ is
normalized.

\section{Energy Considerations}
\paragraph{Quantum Kinetic and Potential Energy} are given by: $$U = \frac{\hat{p}^2}{2m}\psi(x)$$
$$K = \frac{\hslash^2}{2m}\nabla^2\psi(x)$$
Where
\begin{itemize}
\item $\hat{p} = -i\hslash \frac{d^2}{dx^2}$
\end{itemize}

\section{Infinite Well}
This is a pretty common question to ask. The high level explanation is as follows:
\begin{itemize}
\item There is a square well of potential energy with infinitely high side walls.
\item The particle is trapped inside. It cannot tunnel out because of the infinitely high walls.
\item At the edges of the well and beyond, the wave function must be 0
\end{itemize}
This is starting to sound a lot like the boundary conditions for standing waves! You can derive this relationship with a bunch
of complicated math, but here's the final formula for the energy of each of the possible standing wave pattern that a particle
can occupy:
$$E_n = \frac{n^2\pi^2\hslash^2}{2mL^2}$$
Where $n$ is any positive integer, $m$ is the mass of the particle, $L$ is the length of the well.

\paragraph{The actual equation} for the wave is as follows: $$\Psi(x) = C\sin(Ax)$$
Where $AL = n\pi$.

\subsection{Superposition}
When you \textbf{measure} the particle, there is only one energy level $n$ that tells you the frequency of the standing wave and, by extension,
the energy at that level.

Before you observe the particle, it has a superposition of a bunch of $n$ values. However, once you observe the particle, it becomes constrained
to one $n$ value. This is why the photoelectric effect still has discrete energy levels!

\section{Expectation Values}
Honestly you can get this from our vector calculus course pretty well. We know that the probability distribution is given by $P = |\psi^2|$. From vector calculus,
we also know how to get the expected values of things from probability distributions. They like to write this with annoying complex conjugates but it's the same thing.
$$\langle x \rangle = \int_{-\infty}^{\infty}\psi^* x \psi dx = \int_{-\infty}^{\infty}|\psi^2| x dx = \int_{-\infty}^{\infty}P(x) x \psi dx$$

So yeah, nothing really to worry about. Same stuff different variable names.

\section{Operators}
Operators are applied to the wave function $\Psi$ to get physical values out of the wave function. For some reason, those values are called eigenvalues. Whatever.
Here are the operators.
\begin{enumerate}
\item Position operator $\hat{x}$: $\hat{x}\Psi = x\Psi; \hat{r}\Psi=r\Psi$
\item Momentum operator: $\hat{p}$: $\hat{p}_x=-i\hslash\frac{\partial}{\partial x}$
\item Kinetic energy operator: $\hat{K}$: $\hat{K}=-\frac{\hslash^2}{2m}\nabla^2$
\item Total energy operator: $\hat{H}$: $\hat{H} = \hat{K} + \hat{U}$
\item Potential energy is just from any fields of potential energy the particle happens to be riding on.
\end{enumerate}

\section{Schrödinger's For Free Particle}
Remember the general traveling wave equation? Yup, it's back baby!
$$\Psi=Acos(kx-\omega t)+iAsin(kx-\omega t)$$
And here's how you get all those parameter values:
$$p=\hslash k; \,\,\,K=\frac{1}{2}mv^2;\,\,E=\hslash\omega$$
Just remember that $k$ and $K$ are different things ($k$ is the spatial frequency, $K$ is the kinetic energy of the particle).

\end{document}


\documentclass[12pt]{article}

\usepackage[margin=0.9in, a4paper]{geometry}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{hyperref}

\newtheorem*{claim}{Claim}

\thispagestyle{empty}

\begin{document}
  \section*{Finding a colour with AA contrast ratio} % (fold)
  \label{sec:the_problem}

  The \href{https://www.w3.org/TR/2008/REC-WCAG20-20081211/#visual-audio-contrast-contrast}{WCAG AA contrast requirement} for text requires a contrast ratio between the text and the background of at least 4.5:1.
  Given a background colour, suppose we want to find a colour that satisfies this contrast ratio as quickly as possible.

  The two most opposite colours are white (\texttt{\#ffffff}) and black (\texttt{\#000000}), so it makes sense to try those first.

  \begin{claim}
    Every colour has a contrast ratio of at least 4.5:1 with at least one of white  or black.
  \end{claim}

  \begin{proof}
    The WCAG defines the \emph{contrast ratio} of two colours to be
    \begin{equation*}
      \frac{L_1 + 0.05}{L_2 + 0.05},
    \end{equation*}
    where $L_1$ is the relative luminance of the lighter colour, and $L_2$ is the relative luminance of the darker colour.
    (See \href{https://www.w3.org/TR/WCAG20-TECHS/G17.html}{WCAG technique G17}.)

    The relative luminance of white is $L_\text{white} = 1$, and the relative luminance of black is $L_\text{black} = 0$.

    Suppose there were a colour with relative luminance $L$, which has insufficient contrast with both white and black.
    It must satisfy both:
    \begin{equation*}
      \frac{L_\text{white} + 0.05}{L + 0.05} < 4.5
      \hspace{1cm} \text{and} \hspace{1cm}
      \frac{L + 0.05}{L_\text{black} + 0.05} < 4.5
    \end{equation*}
    The luminance of a colour always satisfies $0 \leq L \leq 1$, so we can simplify these to:
    \begin{equation*}
      L > \frac{11}{60} = 0.18333\ldots
      \hspace{1cm} \text{and} \hspace{1cm}
      L < \frac{7}{40} = 0.175.
    \end{equation*}
    This is a contradiction, which means there is no colour which has insufficient contrast with both white and black.

    This means we can always find a colour with sufficient contrast in at most two lookups: first we try white, then we try black.
  \end{proof}

  % section the_problem (end)

  \section*{Finding a colour with AAA contrast ratio} % (fold)
  \label{sec:finding_a_colour_with_aaa_contrast_ratio}

  The enhanced contrast requirement requires a contrast ratio between the text and the background of at least 7.
  Can we use white and black to find a guaranteed colour with this contrast ratio?

  It turns out not: if you try to repeat the proof above with a contrast ratio of 7, not 4.5, you don't get a contradiction.
  Instead, yopu learn that a colour has insufficient contrast with both white and black if and only if
  \begin{equation*}
    0.1 < L < 0.3
  \end{equation*}
  and there are colours with this luminance.

  If you work through the greys, you find \texttt{\#5a5a5a}, which has a relative luminance of 0.102, a contrast ratio 6.897:1 with white and 3.045:1 with black.
  If you go right to the middle, \texttt{\#7f7f7f} has a contrast ratio 4.004:1 with white and 5.245:1 with black.

  Indeed, there are no colours that have a contrast ratio of 7:1 with \texttt{\#7f7f7f}.
  (For proof, imagine such a colour with luminance $L$, and consider the cases $L<L_\text{\texttt{\#7f7f7f}}$ and $L>L_\text{\texttt{\#7f7f7f}}$.
  In both cases you discover the contrast is less than 7.)
  As you keep increasing your contrast requirement, some colours become unusable.
  Eventually you reach the maximum contrast ratio of 21:1, when the only colours you can use are black and white.

  % section finding_a_colour_with_aaa_contrast_ratio (end)

\end{document}