\chapter{Workbook Assignment 1: Basic Probabilities and Visualisations}

\section{Bernoulli Distribution}

Assumed a binary vote with outcome for or against is described by a Bernoulli distribution, with $P(vote = "for") = 0.69$.
Interpreting this as a one-trial Bernoulli experiment with the two outcomes for and against - represented by success and no success respectively - we can visualize it as displayed in figure~\eqref{fig:1a}.

\begin{figure}[h]
\centering
\includegraphics[width=16cm]{pics/1a.pdf}
\caption{Bernoulli distribution of one trial}
\label{fig:1a}
\end{figure}
\FloatBarrier

As this is a one-trial Bernoulli experiment, we can only observe the two outcomes $success = for$ and $no~success = against$. With provided information that $P(vote = "for") = 0.69$, we can calculate that $P(vote=”against”) = 1 - P(vote = "for") = 0.31$. This is visualized by the two bars in figure~\eqref{fig:1a}. Following Hogg et al. \cite[Chapter~3.1]{hogg}, the expected value of X, described by a Bernoulli Distribution, is:
\begin{equation}  \mu = E(X) = (0)(1-p) + (1)(p) = p
\label{eq:expvaluebernoulli}
\end{equation}
Applied to our problem,  $ \mu = p = P(vote = "for") = 0.69 $.
This means if we would repeat the experiment one hundred times, we expect to observe 69 successes.
This result was achieved using Python 3.9 and the following code:
\begin{minted}{python}
from scipy.stats import bernoulli
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

#Dataset creation. Done manually as only one trial
p_for = 0.69
p_against = 0.31 #p_against = 1-p_for
df = pd.DataFrame([[1,p_for],[0,p_against]])

#Visualisation
trace1 = go.Bar(x = df[0],y = df[1],width = 0.1,text = df[1],
    textposition = 'auto')
fig = make_subplots(x_title = 'Number of Successes',y_title = 'P(X = x)')
fig.add_trace(trace1)
fig.show()
fig.write_image("figures/1a.pdf")
\end{minted}

\section{Poisson Distribution}

Assumed we try to model the number of meteorites falling on an ocean each year - why would a Poisson Distribution be a natural candidate?

The Poisson distribution is a discrete probability distribution representing the number of events occurring in a defined period. It assumes the events occur with a known constant mean rate and that the events are independent of the time since the last event \cite[Chapter~4.6]{illowsky2018introductory}.

The event of a meteor impacting the world in a given year fulfills all requirements for such distribution. The event itself is of discrete nature as one cannot have a fractional count of impacts, and we assume meteorite impacts occur with a known constant mean rate and the impacts themselves are independent of the time since the last event. We do not know, whether the variance of the distribution is equal to the mean, but even if it was, we could relax this assumption by using the Poisson-based Gamma-Poisson distribution. For this exercise, we assume $\sigma^2 = \mu$. Additionally assuming $\mu = 64$ allows an exemplary visualisation as in figure \eqref{fig:1b}.

\begin{figure}[h]
\centering
\includegraphics[width=16cm]{pics/1b.pdf}
\caption{Poisson distribution of annual meteorite impacts with $\mu = 64$}
\label{fig:1b}
\end{figure}
\FloatBarrier

To ensure readability of the graphic, probabilities $<0.5$ percent are excluded. Additionally, the variance ($s^2 = 64.36$) is visualised as its root, the sample standard deviation $s$ ($s = 8.02$). Looking at mean ($\bar{x} = 64.01$) and median ($m = 64$), one can barely see a difference, which is to be expected as the distribution displayed is not skewed.

The calculations for this exercise were done in python 3.9 using Jupyter notebooks with the following code:

\begin{minted}{python}
from scipy.stats import poisson
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

#creating a sample with fixed random state
size = 100000
raw = pd.Series(data = poisson.rvs(64, size=size, random_state = 42))

# calculating median, mean, variance, stdev
median = raw.median()
var = raw.var()  # sample assumed
mean = raw.mean()
stdev = np.sqrt(var)
stdev_range = pd.DataFrame([[raw.mean() - stdev, 0.025],
                            [raw.mean() + stdev, 0.025]])

# Filter to only display probabilities >=0.5%
df = pd.DataFrame(raw.value_counts())
df[0] = df[0].div(size)
df = df[df[0] >= 0.005]

# Display measures
print(var)
print(stdev)
print(mean)
print(median)

#creating graphic
trace1 = go.Bar(x = df.index, y = df[0],showlegend = False)
trace2 = go.Bar(x = [median], y = [0.055],width = [0.1], name = 'Median',
text = [median], textposition = 'outside')
trace3 = go.Line(x = stdev_range[0], y = stdev_range[1],
name = 'Standard Deviation', text = ['-1 s', '+1 s'],
mode ="lines+markers+text",textposition = "bottom center")
trace4 = go.Bar(x = [mean], y = [0.025],width = [0.15], name = 'Mean',
text = [mean], textposition = 'outside')

fig = make_subplots(x_title = 'Annual Meteorite Impacts',
y_title = 'P(X = x)')
fig.add_trace(trace1)
fig.add_trace(trace2)
fig.add_trace(trace4)
fig.add_trace(trace3)
fig['layout'].update(barmode = 'overlay')
fig.show()

fig.write_image("figures/1b.pdf")
\end{minted}

\section{Exponential Distributions}
Assumed the probability that you need to wait more than the random variable of y hours to hear an owl from your room is described by:
\begin{equation}  f(Y > y) = 0.2\overline{8}\mathrm{e}^{-0.5y} + 0.7\overline{1}\mathrm{e}^{-0.25y}.
\label{eq:initcdf}
\end{equation}
To facilitate calculations, we reformulate the periodic numbers to fractions:
\begin{equation}  f(Y > y) = \dfrac{13}{45}\mathrm{e}^{-0.5y} + \dfrac{32}{45}\mathrm{e}^{-0.25y}.
\label{eq:initcdffractioned}
\end{equation}
Suspecting that such events are usually exponentially distributed, we reformulate the probability in equation \eqref{eq:truecdf} to the conventional expression \cite{deisenroth}.
\begin{equation}  F_Y(y) = f(Y \leq y) =1-\left( \dfrac{13}{45}\mathrm{e}^{-0.5y} + \dfrac{32}{45}\mathrm{e}^{-0.25y} \right)
\label{eq:truecdf}
\end{equation}
As $Y$ and $y$ are defined as time and time cannot be negative in this case, the domain of the function we are interested in is $[0,+\infty]$.
\begin{equation} f'_Y(y) = f''(Y \leq y) =
\dfrac{-8\mathrm{e}^{-\frac{y}{4}}-13\mathrm{e}^{-\frac{y}{2}}}{180}
\label{eq:secderivative}
\end{equation}

As \eqref{eq:secderivative}, the second derivative of formula \eqref{eq:truecdf},  additionally has no real-valued solution for $y=0$, we can describe $F_Y(y)$ of \eqref{eq:truecdf} as being upwards continuous monotonic increasing. Furthermore, $\lim_{y\to\infty} F_Y(y)= 1$ and $F_Y(0)= 0$ for the support of $y$. This is sufficient evidence to assume that the provided distribution of the random variable $y$ is a cumulative distribution function (CDF) of the family of exponential distributions.

\begin{figure}[h]
\centering
\includegraphics[width=16cm]{pics/1c1.pdf}
\caption{Visualisation of provided and reformulated CDF of task assignment}
\label{fig:1c1}
\end{figure}
\FloatBarrier

For any calculation regarding the probability to hear an owl within a time interval, we use the CDF. If we for example wanted to know $P(2 \leq Y \leq 4)$, the probability of needing to wait two to four hours to hear an owl, we would calculate it as described in equation \eqref{eq:prob24}.

\begin{equation} P(2 \leq Y \leq 4) = P(Y \leq 4) - P(Y \leq 2)
\label{eq:prob24}
\end{equation}
Applied to our distribution, we calculate
\begin{equation} P (2 \leq Y \leq 4) =1-\left( \dfrac{13}{45}\mathrm{e}^{-2} + \dfrac{32}{45}\mathrm{e}^{-1}\right) -\left[ 1-\left( \dfrac{13}{45}\mathrm{e}^{-1} + \dfrac{32}{45}\mathrm{e}^{-0.5}\right)\right] 	\approx 0.23688696
\label{eq:probcalc24}
\end{equation}
Consequently, we can say that in 23.69 percent of cases, we have to wait in between two and four hours to hear an owl.
\\

Next to the CDF, we are also often interested in the corresponding probability density function (PDF). Generally, the PDF $f_Y(y)$ of a CDF $F_Y(y)$ is given by its first derivative \cite[Chapter~4.3]{montgomery2010applied}:
\begin{equation}
f_Y(y) = \frac{df(Y \leq y)}{dy}
\label{eq:firstderivative}
\end{equation}
For our CDF, the PDF is described by:
\begin{equation}
f_Y(y) = \dfrac{16\mathrm{e}^{-\frac{y}{4}}+13\mathrm{e}^{-\frac{y}{2}}}{90}
\label{eq:pdf}
\end{equation}

\begin{figure}[h]
\centering
\includegraphics[width=17cm]{pics/1c2.pdf}
\caption{Visualisation of $f_Y(y)$ (PDF), the first two moments, and applicable quartiles}
\label{fig:1c2}
\end{figure}
\FloatBarrier

The graphical representation of the PDF in figure \eqref{fig:1c2} also includes the mean value. This is calculated using equation \eqref{eq:ExpValueHogg}, following the definition of an expectation for a random variable in Hogg et al. \cite[Definition~1.8.1]{hogg}.
\begin{equation}
E(X) = \int^{+\infty}_{-\infty} x f_X(x) ~ dx
\label{eq:ExpValueHogg}
\end{equation}
Conditions for this to be true are that equation \eqref{eq:ExpValueHoggcondition} is fulfilled and that $X$ is a continuous random variable with $f_X(x)$ as its PDF.
\begin{equation}
\int^{+\infty}_{-\infty} |x| f_X(x) ~ dx < \infty
\label{eq:ExpValueHoggcondition}
\end{equation}
By the task assignment, we know that $Y$ is a continuous random variable. By equation \eqref{eq:pdf}, we calculated $f_Y(y)$ as the PDF of $y$. Lastly, as $y \in [0,\infty)$, the left hand side of inequality \eqref{eq:ExpValueHoggcondition} reduces to the right hand side of equation \eqref{eq:ExpValueHogg} and the lower limit of the integral changes to 0. Consequently, if the mean or expected value $E(Y) < \infty$, it is valid. The calculation for our random variable $y$ in \eqref{eq:ExpValueCalculation} returns $E(Y) < \infty$, we can therefore accept $3.4\overline{2}$ hours as our expected value $E(Y)$ for the waiting time to hear an owl.
%
\begin{equation}
E(Y) = \int^{+\infty}_{0} y f_Y(y) ~ dy = \int^{+\infty}_{0} y\left(\dfrac{16\mathrm{e}^{-\frac{y}{4}}+13\mathrm{e}^{-\frac{y}{2}}}{90} \right) ~dy = \frac{154}{45} = 3.4\overline{2}
\label{eq:ExpValueCalculation}
\end{equation}
Following Hogg et al., the first moment or mean $\mu = E(X)$, if $X$ is a random variable with existing expectation \cite[Definition~1.9.1]{hogg}. This is the case for $Y$. Consequently, we can also assume that the mean value $\mu = E(X) = 3.4\overline{2}$.
\\
To calculate the second moment, the variance $\sigma^2$, we use definition 1.9.2 by Hogg et al. \cite{hogg} as equation \eqref{eq:HoggVariance}.
\begin{equation}
\sigma^2 = E(X^2) - \mu^2
\label{eq:HoggVariance}
\end{equation}
%
The variance of $y$ is calculated by \eqref{eq:YVariance}, using the same assumptions as for equation \eqref{eq:ExpValueCalculation}.
\begin{equation}
\sigma^2 = \int^{+\infty}_{0} y^2 f(y) ~ dy - \mu^2
= \int^{+\infty}_{0} \dfrac{y^2\left(16\mathrm{e}^{-\frac{y}{4}}+13\mathrm{e}^{-\frac{y}{2}}\right)}{90} - \left(\frac{154}{45}\right)^2
= \frac{27044}{2025} \approx 13.3551
\label{eq:YVariance}
\end{equation}

The variance is also depicted in figure \eqref{fig:1c2}     as its root, the standard deviation $\sigma \approx 3.6545$.

Both figures \eqref{fig:1c1} and \eqref{fig:1c2} furthermore display the first three quartiles of the PDF. Hogg et al. \cite[Chapter~1.7.1]{hogg} define the second quartile, the median, as the point dividing area under the PDF into two areas of equal size, which again are divided into two areas of equal size by the first and third quartile. As the CDF is the integral of the PDF and consequently by nature describes the area under the curve of the PDF, we can simply use equation \eqref{eq:truecdf} for the calculation of quartiles. We know that each quartile has to cover a quarter of the area under the curve of PDF \eqref{eq:pdf}. This means we can substitute 0.25, 0.5 and 0.75 for $(F_Y \leq y)$ in equation \eqref{eq:truecdf} and solve for $y$ to get the quartiles. This is exemplarily done in equation \eqref{eq:firstquartile} for the first quartile.

\begin{equation}
\begin{split}
0.25 &
=1-\left( \dfrac{13}{45}\mathrm{e}^{-0.5y_1} + \dfrac{32}{45}\mathrm{e}^{-0.25y_1} \right)\\
y_1&=\ln \left(\frac{413529488+7788544\sqrt{2779}}{332150625}\right)\\
y_1 &\approx 0.9087
\end{split}
\label{eq:firstquartile}
\end{equation}

The second and third quartile can be calculated in the same way, resulting in $y_2 \approx 2.2430$ for the second quartile and $y_3 \approx 4.6588$ for the third quartile. The calculations were done using symbolab \cite{symbolab} with the following commands:
\begin{lstlisting}
solve 0.25 = 1-\left( \dfrac{13}{45}\mathrm{e}^{-0.5y}+\dfrac{32}{45}
\mathrm{e}^{-0.25y} \right)

solve 0.5 = 1-\left( \dfrac{13}{45}\mathrm{e}^{-0.5y}+\dfrac{32}{45}
\mathrm{e}^{-0.25y} \right)

solve 0.75 = 1-\left( \dfrac{13}{45}\mathrm{e}^{-0.5y}+\dfrac{32}{45}
\mathrm{e}^{-0.25y} \right)
\end{lstlisting}

As $\lim_{y\to\infty} F_Y(y)= 1$, we would only get $\infty$ as a result if we tried to calculate the fourth quartile $y_4$. The first three quartiles are visualised in figure \eqref{fig:1c1} as points whereas in figure \eqref{fig:1c2}, they are displayed as vertical lines to indicate that we want to highlight that the individual quartile $y_i$ has $i\cdot25$ percent of PDF \eqref{eq:pdf}'s area under the curve to its left.

\begin{figure}[h]
\centering
\includegraphics[width=17cm]{pics/1c4.pdf}
\caption{Histogram of $f_Y(y)$ by minute}
\label{fig:1c4}
\end{figure}
\FloatBarrier

Lastly, figure \eqref{fig:1c4} displays the PDF as a histogram per minute for one observation. If we for example had 1'000 observations, we would have approximately five observations where we hear an owl in the first minute. As the PDF is per hour while the histogram should be per minute, we need to divide every value $f_Y(y_i)$ for our 600 data points by 60. The standard y-axis comprises 10 hours for all graphics, we have 600 columns which might be too many. However, the graphics are created with python and Plotly, which allows for magnification, if desired. It can also be magnified in this document as it is a vector graphic.
Calculations and visualisations were done in Jupyter notebooks using the following python code:
\begin{minted}{python}
import numpy as np
import pandas as pd
import plotly.graph_objs as go
from plotly.subplots import make_subplots
import plotly
import plotly.express as px

# Define initial function as written in task assignment (P(Y>y))
def func(y):  # cdf
    return ((13 / 45) * np.exp(-0.5 * y) + (32 / 45) * np.exp(-0.25 * y))

# Reformulate function so that you have a standard exponential function (P(Y<=y)
def realfunc(y):  # cdf
    return 1 - func(y)

# Define pdf of realfunc(x)
def pdf_realfunc(y):
    # First derivative of realfunc(x) with respect to x
    return (16 * np.exp(-y / 4) + 13 * np.exp(-y / 2)) / (90)

# create datasets
func_data = []
realfunc_data = []
pdf_realfunc_data = []

for i in np.arange(0, 10, 1 / 60):
    func_data.append([i, func(i)])
    realfunc_data.append([i, realfunc(i)])
    pdf_realfunc_data.append([i, pdf_realfunc(i)])

# create dataframes
func_df = pd.DataFrame(func_data)
realfunc_df = pd.DataFrame(realfunc_data)
pdf_realfunc_df = pd.DataFrame(pdf_realfunc_data)

# Calculate mean value
y_mean = pdf_realfunc(154 / 45)

# Calculate quartiles
# symbolab.com: "solve 0.25 = 1-\left( \dfrac{13}{45}\mathrm{e}^{-0.5y}
# + \dfrac{32}{45}\mathrm{e}^{-0.25y} \right)"
q1 = np.log(413529488 / 332150625 + 7788544 * np.sqrt(2779) / 332150625)

# symbolab.com: "solve 0.5 = 1-\left( \dfrac{13}{45}\mathrm{e}^{-0.5y}
# + \dfrac{32}{45}\mathrm{e}^{-0.25y} \right)"
q2 = np.log(19342148 / 4100625 + 411904 * np.sqrt(2194) / 4100625)

# symbolab.com: "solve 0.75 = 1-\left( \dfrac{13}{45}\mathrm{e}^{-0.5y}
# + \dfrac{32}{45}\mathrm{e}^{-0.25y} \right)"
q3 = np.log(216370448 / 4100625 + 5392384 * np.sqrt(1609) / 4100625)

# Visualisation of initial function and reformulated function
color = px.colors.qualitative.Plotly
trace1 = go.Line(
    x=func_df[0],
    y=func_df[1],
    name='P(Y > y)'
)
trace2 = go.Line(
    x=realfunc_df[0],
    y=realfunc_df[1],
    name='P(Y <= y) (CDF)'
)
# Additionally, the PDF of P(Y <= y) can be displayed.
#trace3 = go.Line(x=pdf_realfunc_df[0],
#                y=pdf_realfunc_df[1],
#                name='PDF of P(Y <= y)'
#)
trace9 = go.Scatter(
    x=[q1],
    y=[0.25],
    showlegend=True,
    name='Quartiles 1-3',
    marker_color=color[9],
    hovertext=['1st Quartile'],
)
trace10 = go.Scatter(
    x=[q2],
    y=[0.5],
    showlegend=False,
    marker_color=color[9],
    hovertext=['2nd Quartile (Median)'],
)
trace11 = go.Scatter(
    x=[q3],
    y=[0.75],
    showlegend=False,
    marker_color=color[9],
    hovertext=['3rd Quartile'],
)
# Mean (can also be displayed additionally)
#trace6 = go.Scatter(x=[154 / 45], y=[y_mean], name='Mean')

# Creating graphic
fig = make_subplots(x_title="y (Hours)", y_title="Probability")
fig.add_trace(trace1)
fig.add_trace(trace2)
#fig.add_trace(trace3)
#fig.add_trace(trace6)
fig.add_trace(trace9)
fig.add_trace(trace10)
fig.add_trace(trace11)
fig.show()
fig.write_image("figures/1c1.pdf")

# Visualisation of PDF
color = px.colors.qualitative.Plotly
trace3 = go.Line(x=pdf_realfunc_df[0],
                 y=pdf_realfunc_df[1],
                 name='f_Y(y) (PDF)',
                 marker_color=color[0])
trace6 = go.Scatter(x=[154 / 45],
                    y=[y_mean],
                    showlegend=True,
                    name='Mean',
                    marker_color=color[2])
trace7 = go.Bar(x=[154 / 45],
                y=[y_mean],
                showlegend=True,
                name='Mean',
                marker_color=color[2],
                width=0.05)
trace8 = go.Line(x=[154 / 45, 154 / 45 + 3.66],
                 y=[y_mean, y_mean],
                 name='Standard Deviation',
                 text=['Mean', '+1 std. dev.'],
                 mode="lines+markers+text",
                 textposition="top center",
                 fillcolor=color[3])
trace9 = go.Bar(
    x=[q1],
    y=[pdf_realfunc(q1)],
    showlegend=True,
    name='Quartiles 1-3',
    marker_color=color[9],
    width=0.05,
    text=['1st Quartile'],
    textposition='outside',
)
trace10 = go.Bar(
    x=[q2],
    y=[pdf_realfunc(q2)],
    showlegend=False,
    marker_color=color[9],
    width=0.05,
    text=['2nd Quartile (Median)'],
    textposition='outside',
)
trace11 = go.Bar(
    x=[q3],
    y=[pdf_realfunc(q3)],
    showlegend=False,
    marker_color=color[9],
    width=0.05,
    text=['3rd Quartile'],
    textposition='outside',
)

fig = make_subplots(x_title="y (Hours)", y_title="P(y)")
fig.add_trace(trace3)
fig.add_trace(trace8)
fig.add_trace(trace6)
#fig.add_trace(trace7) #Additionally, this line can be uncommented
#to get a vertical line for the mean for better visibility. This
#might however be confusing as the mean is no indicator for area
#under the curve, contrary to quartiles, which are displayed as
#columns. Thus, this column was left in as comment, in case some-
#one wants better visuability
fig.add_trace(trace9)
fig.add_trace(trace10)
fig.add_trace(trace11)

fig.update_layout(bargap=0.0, barmode='overlay')
fig.update_traces(marker_line_width=0)

fig.show()
fig.write_image("figures/1c2.pdf")

# Zooming in on the mean, if wanted
#fig.update_xaxes(range=[3.37, 3.47])
#fig.update_yaxes(range=[0.099, 0.105])
#fig.show()
#fig.write_image("figures/1c3.pdf")

#Creating the Histogram per minute

trace4 = go.Bar(y=pdf_realfunc_df[1] / 60,
                name="Histogram of f_Y(y)",
                marker_color='#3399FF')

fig = make_subplots(x_title="y", y_title="P(y)")

fig.add_trace(trace4)

fig.update_layout(bargap=0.0, barmode='overlay')
fig.update_traces(marker_line_width=0)

fig.show()
fig.write_image("figures/1c4.pdf")
\end{minted}

\chapter{Workbook Assignment 2: Basic Probabilities and Visualizations }

\section{Visualisation, Expectations and (Co-)Variances}
Assumed one has recorded mappings of X and Y values, what is an appropriate way of visualisation? How do you find the sample covariance as well as the expectation values and variances per variable; assuming the data comes in the following format?\\
\\
(1.754, 720.15), (-7.385, -260.86), (1.396, -340.56), (-3.304, 954.75),
(-12.159, -370.12),\\ (-6.767, -259.89), (1.233, 261.89), (-7.056, 400.72),
(-1.222, -94.33), (-0.722, -492.02),\\ (-5.065, -789.42), (-9.333, 301.86),
(7.27, -17.04), (-1.989, 257.91), (3.582, -229.89), \\(-1.2, -349.18),
(4.118, 226.05), (-1.834, -534.54), (-1.882, 610.91), (8.677, 70.11) \\

As the variables in the data are both continuous and the question addresses the calculation of correlation, the optimal visualisation is a scatter diagram as for example figure~\eqref{fig:WorkbookAssignment2a}. Other possibilities are excluded as there is not much more information provided on the data set. A line graph for example would be appropriate if we had information that the observations show some development over some time domain.
\begin{figure}[h]
\centering
\includegraphics[width=16cm]{pics/WorkbookAssignment2a.pdf}
\caption{Visualisation of provided X and Y values as scatter plot}
\label{fig:WorkbookAssignment2a}
\end{figure}
\FloatBarrier
Looking at the scatter plot, one can observe that the data is quite evenly dispersed; no pattern can be determined at first glance. This rather evenly distributed data also suggests that the expected value for X and Y is rather close to 0. Calculating the expected value for each variable can be understood at the calculation of the expected value of a discrete random variable as the number of observations is discrete. Equation~\eqref{eq:ExpValDiscrete} is therefore the formula to calculate the expected value \cite{mathcenter}.
\begin{equation}
E(X) = \sum \left[x * P(x)\right]
\label{eq:ExpValDiscrete}
\end{equation}
The results are consequently $E(X) = -1.5944$ and $E(Y) = 3.3250$.
Following Bruce and Bruce \cite{bruce2017practical}, the variance for a sample is calculated by
\begin{equation}
s^2 = \frac{\sum{(x-\bar{x})^2}}{n-1}.
\label{eq:ExpValDiscrete}
\end{equation}
This computes to $s^2_x = -1.5944$ and $s^2_y = 3.3250$, where $\bar{x} = E(X)$ and $\bar{y} = E(Y)$.
The sample covariance is defined in Equation~\eqref{eq:COV} (adapted from Bruce and Bruce \cite{bruce2017practical}).
\begin{equation}  s_{xy} = \frac{\sum(x_{i}-\bar{x})(y_{i}-\bar{y})}{n-1}
\label{eq:COV}
\end{equation}
Applying the formula to the provided data, $s_{xy} = 284.23$ can be observed. This indicates a positive relationship.
The calculations were done using Python 3.9 and the following code:
\begin{minted}{python}
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = (1.754, 720.15), (-7.385, -260.86), (1.396, -340.56), (-3.304, 954.75),
(-12.159, -370.12), (-6.767, -259.89), (1.233, 261.89), (-7.056, 400.72),
(-1.222, -94.33), (-0.722, -492.02), (-5.065, -789.42), (-9.333, 301.86),
(7.27, -17.04), (-1.989, 257.91), (3.582, -229.89), (-1.2, -349.18),
(4.118, 226.05), (-1.834, -534.54), (-1.882, 610.91), (8.677, 70.11)
data = pd.DataFrame(data)
data.columns = ["X", "Y"]
print("The variance of the variables is: \n{}".format(data.var()))
print("The expectation value for the variables is: \n{}".format(data.mean()))
print("The sample covariance is {}".format(data.cov().iloc[0,1]))

data = data.set_index("X")

sns.scatterplot(data = data, x = "X", y = "Y", color = "black").set(
    title = "Visualisation of X and Y Values")
plt.savefig('./figures/2a.pdf')
\end{minted}

\section{Transformations}
Assumed we have a ball being thrown at a random angle $\theta$ [0,360) (in degrees) and a random radius $r$ $\in$ [0,1] (in meters) both independent and uniform. What is the density of the variables $X$ and $Y$, which are the Cartesian coordinates of the point at angle $\theta$ and radius $r$? And, furthermore, what is their expectation and variance?

Following Illowsky and Dean \cite[Chapter~5.2]{illowsky2018introductory}, the pdf of an uniformly distributed variable is $f(x) = \frac{1}{b-a}$ for $a \leq x \leq b$.

As $\theta,r \sim \mathcal{U}$, we can formulate the marginal distributions as described in \eqref{eq:theta_untransformed} (using radiants) and \eqref{eq:r_untransformed}.

\begin{equation}
f_\Theta(\theta) =
\begin{cases}
      \frac{1}{2\pi} & for~\theta \in [0,2\pi)\\
      0&elsewhere
   \end{cases}
\label{eq:theta_untransformed}
\end{equation}

\begin{equation}
f_R(r) =
\begin{cases}
      1 & for~r\in[0,1]\\
      0&elsewhere.
   \end{cases}
\label{eq:r_untransformed}
\end{equation}

Joining these distributions returns the following joint pdf:
\begin{equation}
\begin{split}
f_{R,\Theta}(r,\theta) =
\begin{cases}
      \frac{1}{2\pi} & \theta \in [0,2\pi),~ r\in[0,1]\\
      0&elsewhere.
   \end{cases}
\end{split}
\label{eq:joint_pdf_untransformed}
\end{equation}

Following Dawkins \cite{dawkins}, we can transform to Cartesian $x$ and $y$ using $x = rcos(\theta)$ and $y=rsin(\theta)$. For the transformation, we furthermore need their inverses, which calculate to

\begin{align*}
x^2=r^2cos^2(\theta)& \qquad y^2=r^2sin^2(\theta)\\
x^2+y^2&=r^2cos^2(\theta)sin^2(\theta)\\
x^2+y^2&=r^2\\
r &= \sqrt{x^2+y^2} = u_1(x,y)\\
\label{eq:r-1}
\end{align*}
and
\begin{align*}
 r = \frac{x}{cos(\theta)}& \qquad r = \frac{y}{sin(\theta)}\\
 \frac{x}{cos(\theta)} &= \frac{y}{sin(\theta)}\\
 \frac{x}{cos(\theta)} &= \frac{y}{sin(\theta)}\\
 \frac{y}{x} &= \frac{cos(\theta)}{sin(\theta)}\\
 \frac{y}{x} &= tan(\theta)\\
 \theta &= tan^{-1}\left(\frac{y}{x}\right) = u_2(x,y).
\label{eq:theta-1}
\end{align*}

Using the transformation theorem proved in Hogg et al. \cite[Chapter~2.7]{hogg}, we get

\begin{equation}
\begin{split}
f_{X,Y}(x,y) = f_{R,\Theta}\left(\sqrt{x^2+y^2},tan^{-1}\left(\frac{y}{x}\right)\right) \cdot |J|,
%f_{X,Y}(x,y) = f_{r,\theta}\left(u_1,u_2\right)\right) \cdot |J|
\end{split}
\label{eq:transtheorem}
\end{equation}
where
\begin{equation}
\begin{split}
|J| &=
\left|
\begin{matrix}
\frac{\partial u_1}{\partial x} &
\frac{\partial u_1}{\partial y}  \\[2ex] % <-- 1ex more space between rows of matrix
\frac{\partial u_2}{\partial x} &
\frac{\partial u_2}{\partial y}  \\[2ex]
\end{matrix}
\right|
=
\left|
\begin{matrix}
\frac{x}{\sqrt{x^2+y^2}}&
\frac{y}{\sqrt{x^2+y^2}}  \\[2ex] % <-- 1ex more space between rows of matrix
-\frac{y}{y^2+x^2} &
\frac{x}{y^2+x^2}  \\[2ex]
\end{matrix}
\right|\\[2ex]
&= \frac{x}{\sqrt{x^2+y^2}} \cdot \frac{x}{y^2+x^2} -\frac{y}{\sqrt{x^2+y^2}}  \cdot \left(-\frac{y}{y^2+x^2}\right)\\[2ex]
&= \frac{x^2+y^2}{(x^2+y^2)^{3/2}}\\[2ex]
&=\frac{1}{\sqrt{x^2+y^2}},
\end{split}
\label{eq:jacobiandet}
\end{equation}

with the derivatives being calculated by Symbolab \cite{symbolab} with the following commands:
\begin{lstlisting}
\frac{\partial }{\partial \:x}\left(\sqrt{x^2+y^2}\right)
\frac{\partial }{\partial \:y}\left(\sqrt{x^2+y^2}\right)
\frac{\partial }{\partial \:x}\left(tan^{-1}\left(\frac{y}{x}\right)\right)
\frac{\partial }{\partial \:y}\left(tan^{-1}\left(\frac{y}{x}\right)\right)
\end{lstlisting}

Substituting \eqref{eq:jacobiandet} into \eqref{eq:transtheorem}, we get
\begin{equation}
\begin{split}
f_{X,Y}(x,y) &= f_{r,\theta}\left(\sqrt{x^2+y^2},tan^{-1}\left(\frac{y}{x}\right)\right) \cdot \frac{1}{\sqrt{x^2+y^2}}\\[2ex]
f_{X,Y}(x,y) &= \frac{1}{2\pi} \cdot \frac{1}{\sqrt{x^2+y^2}} = \frac{1}{2\pi\sqrt{x^2+y^2}},
%\begin{cases}
%      \frac{1}{2\pi} & \theta \in [0,2\pi),~ r\in[0,1]\\
%      0&elsewhere
%   \end{cases}
\end{split}
\label{eq:transtheoremcomplete}
\end{equation}

which represents the joint PDF of $X$ and $Y$.\\
To calculate their individual densities, the marginal distributions are needed. As proofed by Larsen and Marx \cite[Theorem~3.7.2]{larsen2005introduction}, the marginal distribution for $X$ can be obtained by

\begin{equation}
\begin{split}
f_{X}(x) = \int_{0}^{\sqrt{1-x^2}}\frac{1}{2\pi\sqrt{x^2+y^2}} dy,
\end{split}
\label{eq:marginalX}
\end{equation}

which calculates to

\begin{equation}
\begin{split}
f_{X}(x) = \frac{1}{2\pi }\ln \left(\frac{\sqrt{1-x^2}+1}{x}\right) \quad x \in (0,1)
%https://www.symbolab.com/solver/step-by-step/%5Cint_%7B0%7D%5E%7B%5Csqrt%7B1-x%5E2%7D%7D%5Cfrac%7B1%7D%7B2%5Cpi%5Csqrt%7Bx%5E%7B2%7D%2By%5E%7B2%7D%7D%7D%20dy
\end{split}
\label{eq:marginalXcalculated}
\end{equation}

using Symbolab \cite{symbolab} with the following code:
\begin{lstlisting}
\int _0^{\sqrt{1-x^2}}\frac{1}{2\pi \sqrt{x^2+y^2}}\:dy
\end{lstlisting}

PDF \eqref{eq:transtheoremcomplete} is symmetric in $x$ and $y$ as $f_{X,Y}(x,y) = f_{Y,X}(y,x)$. Consequently, $f_Y(y)$ has similar form:
\begin{equation}
\begin{split}
f_{Y}(y) = \frac{1}{2\pi }\ln\left(\frac{\sqrt{1-y^2}+1}{y}\right) \quad y \in (0,1).
\end{split}
\label{eq:marginalYcalculated}
\end{equation}
To calculate the expected value, equation \eqref{eq:ExpValueHogg} is used again:
\begin{equation}
\begin{split}
E(X) &= \int_0^1  \frac{x}{2\pi }\ln \left(\frac{\sqrt{1-x^2}+1}{x}\right) dx\\
E(X) &= \frac{1}{2\pi}\int_0^1  x\ln \left(\frac{\sqrt{1-x^2}+1}{x}\right) dx\\
E(X) &= \frac{1}{4\pi } \cdot \left[x^2\ln \left(\frac{\sqrt{1-x^2}+1}{x}\right)-\sqrt{1-x^2}\right]_0^1\\
E(X)&= \frac{1}{4\pi} \approx 0.07958.
\end{split}
\label{eq:expvalF_X}
\end{equation}
$E(X) = E(Y)$, as again the same value calculates for $Y$ due to the symmetry of the distribution.
WolframAlpha \cite{wolfalph} was used for this calculation with the following input:
\begin{lstlisting}
\int_0^1  \frac{x}{2\pi }\ln \left(\frac{\sqrt{1-x^2}+1}{x}\right)
\end{lstlisting}

To calculate the second moment, the variance - equation \eqref{eq:HoggVariance} - is recalled:
\[
Var(x) = E(X^2)-(E(X))^2,
\]
where
\begin{equation}
\begin{split}
E(X^2) =  \frac{1}{2\pi}\int_0^1  x^2\ln \left(\frac{\sqrt{1-x^2}+1}{x}\right) dx = \frac{1}{24}
\end{split}
\label{eq:E(X^2)}
\end{equation}
This calculation was done in WolframAlpha \cite{wolfalph} with the following input:
\begin{lstlisting}
\int_0^1  \frac{x^2}{2\pi }\ln \left(\frac{\sqrt{1-x^2}+1}{x}\right)
\end{lstlisting}
Consequently,
\begin{equation}
\begin{split}
Var(X) = Var(Y) = \frac{1}{24}- \left(\frac{1}{4\pi}\right)^2 = \frac{2\pi ^2-3}{48\pi ^2} \approx 0.1581.
\end{split}
\label{eq:VarXcalculated}
\end{equation}

\chapter{Workbook Assignment 3: A Simple Parameter Estimation}

Supposed we have a network router with a total bandwith until it breaks down described by the random variable $S$, which is exponentially distributed:
\begin{equation}
\begin{split}
%s(t) = f_S(t) =  \frac{1}{\theta} e^{-\frac{t}{\theta}}
s(t) =  \frac{1}{\theta} e^{-\frac{t}{\theta}}.
\end{split}
\label{eq:exphardwarefailure}
\end{equation}

\section{Bandwith Total to Failure}
Assumed we have another backup device, the bandwidth total to failure of both devices is described by $T$, which is the sum of $S_1$ and $S_2$, which each are described by \eqref{eq:exphardwarefailure}. T is consequently
\begin{equation}
\begin{split}
T = S_1 + S_2.
\end{split}
\label{eq:T}
\end{equation}
Both variables are random variables by task assignment and it can furthermore be assumed that they are independent as the performance of one device has no influence on the performance of another device. Furthermore, bandwidth is a continuous variable that cannot take negative values. With these assumptions about the variables, the sum of both variables is consequently calculated by convolution, following Larsen and Marx \cite[Theorem~3.8.3]{larsen2005introduction}.
\begin{equation}
\begin{split}
t(z) = \int_{-\infty}^\infty s(t)s(z-t)~dt.
\end{split}
\label{eq:tz1}
\end{equation}
As only positive values can be obtained for the bandwidth, \eqref{eq:tz1} simplifies to
\begin{equation}
\begin{split}
t(z) = \int_{0}^zs(t)s(z-t)~dt.
\end{split}
\label{eq:tz2}
\end{equation}
Inserting the density of $S$ yields
\begin{equation}
\begin{split}
t(z)
&= \int_{0}^z \left(\frac{1}{\theta} e^{-\frac{t}{\theta}}\right)
\left(\frac{1}{\theta} e^{-\frac{z-t}{\theta}}\right)~dt\\
&= \frac{1}{\theta^2} e^{-\frac{z}{\theta}}\int_{0}^z~dt\\
&= \frac{z}{\theta^2} e^{-\frac{z}{\theta}}\\
&= f_T(z;~\theta).
\end{split}
\label{eq:tz3}
\end{equation}

\section{Likelihood Function}
Given an experiment with the dual-router-system yielding a sample $T_1$, $T_2$, ..., $T_n$, calculate the likelihood function for $\theta$. Propose a transformation of this likelihood function whose maximum is the same and can be computed easily.

Following Definition 5.2.1 in Larsen and Marx \cite{larsen2005introduction}, the likelihood function is described by
\begin{equation}
\begin{split}
L(\theta) = \prod_{i=1}^n f_Y(y_i;~\theta),
\end{split}
\label{eq:LikelyhoodFunctionDefinition}
\end{equation}
which, in the case of this tasks, is
\begin{equation}
\begin{split}
L(\theta)
&= \prod_{i=1}^n f_T(z_i;~\theta)\\
&= \prod_{i=1}^n \frac{z_i}{\theta^2} e^{-\frac{z_i}{\theta}}\\
\end{split}
\label{eq:LikelyhoodFunction2}
\end{equation}
and simplifies to
\begin{equation}
\begin{split}
L(\theta)
&= \theta^{-2n}\left(\prod_{i=1}^n z_i\right)e^{-\frac{1}{\theta}\sum_{i=1}^n z_i}.
\end{split}
\label{eq:LikelyhoodFunction3}
\end{equation}
To calculate the maximum likelihood, equation \eqref{eq:LikelyhoodFunction3} must be derived with respect to $\theta$ and equated to $0$. In its current form, such calculation quickly becomes computationally expensive and eventually inaccurate. To surpass such problem, \eqref{eq:LikelyhoodFunction3} can be transformed using the natural logarithm as the value maximising $L(\theta)$ also maximises $\ln L(\theta)$:
\begin{equation}
\begin{split}
\ln~L(\theta)
&= -2n\ln\theta + \ln\left(\prod_{i=1}^n z_i\right)-\frac{1}{\theta}\sum_{i=1}^n z_i.
\end{split}
\label{eq:LikelyhoodFunction4}
\end{equation}
The derivation then yields
\begin{equation}
\begin{split}
\frac{d\ln~L(\theta)}{d\theta}
&= \frac{-2n}{\theta}+\frac{1}{\theta^2}\sum_{i=1}^n z_i,
\end{split}
\label{eq:LikelyhoodFunction5}
\end{equation}
which results in the maximum likelihood function when equated to $0$ and solved for $\theta$:
\begin{equation}
\begin{split}
\frac{-2n}{\theta}+\frac{1}{\theta^2}\sum_{i=1}^n z_i = 0.
\end{split}
\label{eq:LikelyhoodFunction6}
\end{equation}
The maximum likelihood estimator $\theta_e$ is consequently
\begin{equation}
\begin{split}
\theta_e =\frac{1}{2n}\sum_{i=1}^n z_i.
\end{split}
\label{eq:MLE}
\end{equation}
In an experiment with $n=5$ observations measuring the total bandwidth to failure, the following results were observed:
\begin{center}
16, 86, 96, 17, 339.
\end{center}
To estimate the model-parameter with the maximum likelihood, the values are inserted into \eqref{eq:MLE}:
\begin{equation}
\begin{split}
\theta_e =\frac{1}{2\cdot5}(16+86+96+17+339) = 55.4.
\end{split}
\label{eq:MLECalculation}
\end{equation}
To calculate the expectation of the bandwidth, we reuse equation \eqref{eq:ExpValueHogg} which, applied to this task, is
\begin{equation}
\begin{split}
E(T) = \int_0^\infty z\frac{z}{\theta^2} e^{-\frac{z}{\theta}}~dz,\\
\end{split}
\label{eq:expvalue3}
\end{equation}
and calculates to
\begin{equation}
\begin{split}
E(T)
&= \int_0^\infty z\frac{z}{55.4^2} e^{-\frac{z}{55.4}}~dz\\
&= 110.8.
\end{split}
\label{eq:expvalue4}
\end{equation}
Equation \eqref{eq:expvalue4} was calculated using symbolab \cite{symbolab} with the following command:
\begin{lstlisting}
\int _0^{\infty }\:z\frac{z}{55.4^2}\:e^{-\frac{z}{55.4}}
\end{lstlisting}

\chapter{Workbook Assignment 4: Hypothesis Test}

Assumed we are producing high-quality hammers in our factory and want to model the production quality. From the last 1000 produced hammers, we observed an average weight of $\mu_1=971~g$ and a standard deviation of $\sigma_1 = 42~g$. As $n = 1000$, we assume that we can treat the point estimates for the mean and the standard deviation as population parameters.

Production inaccuracies are usually normally dispersed, and as we neither have access to the raw data of individual observations nor have information on any skewness or non-normal behaviour, a normal distribution $N(\mu, \sigma^2)$ is assumed.
The normal distribution is defined as follows \cite[Definition~3.4.1]{hogg}:

\begin{equation} \frac{1}{{\sigma \sqrt {2\pi } }}e^{{{ - \left( {x - \mu } \right)^2 } \mathord{\left/ {\vphantom {{ - \left( {x - \mu } \right)^2 } {2\sigma ^2 }}} \right. \kern-\nulldelimiterspace}{2\sigma ^2 }}}.
\label{eq:normaldist}
\end{equation}

It depends on two parameters, the population standard deviation $\sigma$ and the population mean $\mu$, which both are provided. In general, any normal distribution has a bell-shaped form, indicating that it is symmetric and not skewed to any side. Apart from the mean and the standard deviation, we are not provided any further data.  The assumption of such model of course does not hold anymore if the data of the 1000 produced hammers introduces any kind of bias or skewness.

Other distributions are excluded as they are not applicable to this case or are less fitting. A Binomial Distribution for example is not applicable as it is binary, whereas the independent variable central to this exercise - hammer weight - is continuous. \\Describing the hammer weight as a repeated Bernoulli trial would also be an option to model the distribution, however such endeavor would firstly approximate a normal distribution as well and would secondly assume that the weight measurements are integer variables, whereas they are of continuous nature. \\
Another popular distribution, the Poisson distribution, is also not applicable as it models the average number of events per time interval \cite{bruce2017practical}. \\
Using an exponential distribution to model the weight dispersion of hammer production would imply that we expect for example continuously increasing counts of hammers for increasing weights, which is not a senseful assumption. \\
Consequently, we assume that the normal distribution is the best approximation.

Using the normal distribution as approximation implies accepting several assumptions. We assume that the model has the x-axis as an horizontal asymptote, that its mean is equal to its median, that its mode is the same as its mean and that it is symmetric about the mean \cite[Chapter~3.4]{hogg}.

Using \eqref{eq:normaldist} with the provided parameters of $\mu_1 = 971$ and $\sigma_1 = 42$, the following model is proposed:

\begin{equation} \frac{1}{{42 \sqrt {2\pi } }}e^{{{ - \left( {x - 971 } \right)^2 } \mathord{\left/ {\vphantom {{ - \left( {x - 971 } \right)^2 } {2(42) ^2 }}} \right. \kern-\nulldelimiterspace}{2(42) ^2 }}} ~~~=~~~ \frac{1}{{42 \sqrt {2\pi } }}e^{{{ - \left( {x - 971 } \right)^2 } \mathord{\left/ {\vphantom {{ - \left( {x - 971 } \right)^2 } {7056 }}} \right. \kern-\nulldelimiterspace}{3528 }}}.
\label{eq:normaldistadapted}
\end{equation}

Assumed a new production system for hammer production is tested. A random sample output of hammers of this new production output returns the following weights in grams: \\

\begin{center}
    1009, 1069, 966, 952, 1084, 897, 909, 995, 975, 983.
\end{center}

How do we know if this new system's weight accuracy is better than the old system's accuracy, i.e. how do we find out whether the weight variance of the new system is lower than the variance of the old system?

To answer this question, we have to perform a statistical test comparing the variances of both samples, which requires hypotheses. Overall, we expect that the new system reduces weight variance and is more accurate. The hypotheses are consequently

\begin{center}
$H_0~~:~~ \sigma^2_2~=~\sigma^2_1$\\
$H_1~~:~~ \sigma^2_2~<~\sigma^2_1$.
\end{center}

where $\sigma^2_1$ is defined as the population variance of the old production system and $\sigma^2_2$ is defined as the population variance of the new production system.

Following Larsen and Marx \cite[Chapter 7.5]{larsen2005introduction}, the chi square distribution is needed for inferences on variances. To test our sample variance $s_2^2 = \frac{32594.9}{9} \approx 3621.66 $ against the variance $\sigma^2_1 = 42^2 = 1764$, we can now use the Chi Square Test or the F-Test. As we earlier assumed that $\sigma_1$ is the population variance instead of a sample variance, we do not need to rely on an F-Test and can work with the Chi Square Test. The hypotheses can then be formulated as

\begin{center}
$H_0~~:~~ \sigma^2_2~=~1764$\\
$H_1~~:~~ \sigma^2_2~<~1764$.
\end{center}

Following Larsen and Marx \cite[Chapter 7.5]{larsen2005introduction}, the Chi Square test statistic is given by

\begin{equation}
\chi^2 = \frac{(n-1)s_2^2}{\sigma_1^2},
\label{eq:chisquarestatistic}
\end{equation}

with the decision rule of rejecting $H_0$ if
\\
\begin{equation}
\chi^2 \leq \chi^2_{1-\alpha,n-1},
\label{eq:decisionrule}
\end{equation}
\\
where our error probability $\alpha$ is our level of significance. An usual assumption is a 95 percent confidence interval, which results in $\alpha = 0.05$. As the losses, which an error in this test might incur, can be expected to be quite minor, there is no need to increase the confidence interval and to work with a smaller $\alpha$.

Inserting the sample figures into \eqref{eq:chisquarestatistic}, we observe
\begin{equation}
\chi^2 = \frac{(10-1) \cdot \frac{32594.9}{9}}{1764} = \frac{32594.9}{1764} \approx 18.48.
\label{eq:chisqstatcalc}
\end{equation}

Using the lower-tail critical values of chi-square distribution of the Engineering Statistics Handbook \cite{nist}, the critical value is approximated by
\\
\begin{equation}
\chi^2_{0.95,9}\approx 3.33,
\label{eq:criticalvalue}
\end{equation}
\\
with the corresponding p-value of
\\
\begin{equation}
p = P(\chi^2 < 18.48) = 1-0.03 = 0.97,
\label{eq:pvalue}
\end{equation}
\\
using the p-Value Calculator for a Chi-Square Test \cite{freepvalue}.

Consequently, as
\\
\begin{equation}
\chi^2 \nleq \chi^2_{0.95,9}
\label{eq:criticalvalue}
\end{equation}
\\
or, put differently, the p-value is much larger than our $\alpha$, we can not reject $H_0$ and can not say that the new model makes more constant weights. Looking at the large initial difference in variances $s_2^2 \approx 3621.66 $ and $\sigma^2_1 = 1764$, this result is not too surprising.

\chapter{Workbook Assignment 5: Sufficient Statistics}
Let $X_1,~X_2,~...,~X_n$ be the random sample of a positive random variable $X$ having the density function
\begin{equation}
\begin{split}
f_X(x;~\theta) = \frac{1}{2!~\theta^3}x^2e^{-\frac{x}{\theta}}
\end{split}
\label{eq:suffpdf}
\end{equation}
with one parameter $\theta \in \mathbb{R}+$. Find an estimator for $\theta$ that is sufficient.
\\
Larsen and Marx define sufficient statistics as follows: "Let $X_1 = k_1, . . . , X_n = k_n$ be a random sample of size $n$ from $p_X(k;~\theta)$. The statistic $\hat{\theta} = h(X_1, . . . , X_n)$ is sufficient for $\theta$ if the likelihood function, $L(\theta)$, factors into the product of the pdf for $\hat{\theta}$ and a constant that does not involve $\theta$ — that is, if
\begin{equation}
\begin{split}
L(\theta)
= \prod_{i=1}^n p_X(k_i;\theta)
=p_{\hat{\theta}}(\theta_e;\theta)b(k_1,...,k_n)
\end{split}
\label{eq:SufficientLikelihood}
\end{equation}
A similar statement holds if the data consist of a random sample $Y_1~=~y_1,...,Y_n~=~ y_n$ drawn from a coninuous pdf $f_Y(y;\theta)$." \cite{larsen2005introduction}.
This can be constructed for the given density function \eqref{eq:suffpdf}. Firstly, to calculate the likelihood function, its definition \eqref{eq:LikelyhoodFunctionDefinition} is used.
\begin{equation}
\begin{split}
L(\theta)
&= \prod_{i=1}^n f_X(x_i;~\theta)\\
&= \prod_{i=1}^n \frac{1}{2!~\theta^3}x_i^2e^{-\frac{x_i}{\theta}}.\\
\end{split}
\label{eq:SufficientLikelihood2}
\end{equation}
This can be simplified to
\begin{equation}
\begin{split}
L(\theta)
&= \frac{1}{2^n\theta^{3n}}\prod_{i=1}^n x_i^2e^{-\frac{x_i}{\theta}}\\
&= \frac{1}{2^n\theta^{3n}} e^{-\frac{1}{\theta}\sum_{i=1}^nx_i} \left(\prod_{i=1}^n x_i\right)^2.\\
\end{split}
\label{eq:SufficientLikelihood3}
\end{equation}
Now
\begin{equation}
\begin{split}
p_{\hat{\theta}}(\theta_e;\theta) = \frac{1}{2^n\theta^{3n}} e^{-\frac{1}{\theta}\sum_{i=1}^nx_i}
\end{split}
\label{eq:SufficientLikelihood4}
\end{equation}
and
\begin{equation}
\begin{split}
b(k_1,...,k_n) = \left(\prod_{i=1}^n x_i\right)^2.
\end{split}
\label{eq:SufficientLikelihood5}
\end{equation}
As \eqref{eq:SufficientLikelihood5} is not a function of $\theta$, $\sum_{i=1}^nx_i$ is a sufficient estimator for $\theta$.

\chapter{Workbook Assignment 6: Bayesian Estimates}
Let $X_1, X_2, ... , X_{10}$ be a random sample from a gamma distribution with $\alpha =3$ and $\beta =1/\theta$. Suppose we believe that $\theta$ follows a gamma-distribution with $\alpha =3$ and $\beta = 2$.

\section{Posterior Distribution}
To calculate the posterior distribution, the following Bayesian model can be formulated from the task assignment:
\begin{equation}
\begin{split}
X_i|\theta &\sim \Gamma\left(3,\frac{1}{\theta}\right)\\
\Theta &\sim \Gamma(3,2).
\end{split}
\label{eq:Bayesmodel}
\end{equation}
The Gamma distribution which both $X$ and $\Theta$ follow is given by Hogg et al. \cite[Equation~3.3.2]{hogg}:
\begin{equation}
\begin{split}
f(x) = \frac{x^{\alpha-1}e^{-\frac{x}{\beta}}}{\Gamma(\alpha)\beta^\alpha}\quad x>0.
\end{split}
\label{eq:GammaDist}
\end{equation}
Generally, the posterior pdf $k(\theta|X)$ is given by Hogg et al. as: \cite[Equation~11.1.5]{hogg}
\begin{equation}
\begin{split}
k(\theta|x) = \frac{L(x|\theta)h(\theta)}{g_1(x)}
\end{split}
\label{eq:Posterior}
\end{equation}
where
\begin{equation}
\begin{split}
g_1(x) = \int_{-\infty}^\infty L(x|\theta)h(\theta) ~d\theta.
\end{split}
\label{eq:g_1}
\end{equation}
Following Hogg et al. \cite[Chapter 11.1.1]{hogg} the simplified notation of
\begin{equation}
\begin{split}
k(\theta|x) \propto L(x|\theta)h(\theta)
\end{split}
\label{eq:Posterior2}
\end{equation}
is sufficient to calculate the posterior distribution as $g_1(x)$ is a normalising factor in this context. The calculation of $g_1(x)$ can consequently be dropped. $L(x|\theta)$ is obtained by inserting the upper model of \eqref{eq:Bayesmodel} into \eqref{eq:GammaDist}:
\begin{equation}
\begin{split}
L(x|\theta)
&= \prod_{i=1}^n \frac{x_i^{3-1}e^{-\frac{x_i}{\frac{1}{\theta}}}}{\Gamma(3)\left(\frac{1}{\theta}\right)^3} \\
&= \prod_{i=1}^n \frac{x_i^{2}\theta^3e^{-x_i\theta}}{\Gamma(3)}\\
&= \frac{x^{2n}\theta^{3n}e^{-\theta\sum_{i=1}^nx_i}}{\Gamma(3)^n}.
\end{split}
\label{eq:bayeslikelyhood}
\end{equation}
The prior pdf is given by the lower part of \eqref{eq:Bayesmodel}:
\begin{equation}
\begin{split}
h(\theta) = \frac{\theta^{3-1}e^{-\frac{\theta}{2}}}{\Gamma(3)2^3}
=\frac{\theta^{2}e^{-\frac{\theta}{2}}}{8\Gamma(3)} .
\end{split}
\label{eq:BayesPrior}
\end{equation}
Consequently,
\begin{equation}
\begin{split}
k(\theta|x)
\propto \frac{x^{2n}\theta^{3n}e^{-\theta\sum_{i=1}^nx_i}}{\Gamma(3)^n} \frac{\theta^{2}e^{-\frac{\theta}{2}}}{8\Gamma(3)},
\end{split}
\label{eq:calculatedposterior}
\end{equation}
assuming $0<\theta<\infty$. Dropping the constants results in
\begin{equation}
\begin{split}
k(\theta|x)
&\propto \theta^{3n}\theta^{2}e^{-\theta\sum_{i=1}^nx_i} e^{-\frac{\theta}{2}}\\
&\propto \theta^{3n+2}e^{-\theta(\frac{1}{2}+\sum_{i=1}^nx_i)}
\end{split}
\label{eq:calculatedposterior2}
\end{equation}
Looking at the form of \eqref{eq:calculatedposterior2}, it is apparent that it is a $\Gamma$ function. $\alpha$ and $\beta$ can now be calculated using \eqref{eq:calculatedposterior2}, \eqref{eq:GammaDist} and $n=10$ from the assignment.
\begin{equation}
\begin{split}
\alpha-1 &= 3n+2\\
\alpha &= 33
\end{split}
\label{eq:postalpha}
\end{equation}
\begin{equation}
\begin{split}
-\frac{\theta}{\beta} &= -\theta\left(\frac{1}{2}+\sum_{i=1}^nx_i\right)\\
\beta &= \frac{1}{\frac{1}{2}+\sum_{i=1}^nx_i}
\end{split}
\label{eq:postbeta}
\end{equation}
The posterior distribution is consequently
\begin{equation}
\begin{split}
\Theta|X &\sim \Gamma\left(33,\frac{1}{\frac{1}{2}+\sum_{i=1}^nx_i}\right).
\end{split}
\label{eq:Posteriormodel}
\end{equation}

\section{Bayes Point Estimate by Square-Error Loss Function}
Assumed, $\bar{x} = 16.5$ is observed, what is the Bayes point estimate associated with the square-error loss function?
Following Theorem 5.8.1 \cite{larsen2005introduction}, the Bayes estimate for $\theta$ is the mean of the posterior if a square-error loss function is used. As the distribution is a Gamma-distribution, the mean or expected value is calculated by
\begin{equation}
\begin{split}
E(X|\Theta) = \alpha\beta,
\end{split}
\label{eq:expvalgamma}
\end{equation}
as proven in Chapter 3.3 \cite{hogg}. Using $\alpha = 33$ and
\begin{equation}
\begin{split}
\beta &= \frac{1}{\frac{1}{2}+\sum_{i=1}^nx_i}\\
&=\frac{1}{\frac{1}{2}+n\bar{x}}\\
&=\frac{1}{\frac{1}{2}+10\cdot16.5}\\
&=\frac{1}{165.5},
\end{split}
\label{eq:postbeta}
\end{equation}
the mean computes to
\begin{equation}
\begin{split}
E(X|\Theta) = \frac{33}{165.5} \approx 0.1994.
\end{split}
\label{eq:expvalgammacalc}
\end{equation}

\section{Point Estimate by Mode}
What is the Bayes point estimate using the mode of the posterior distribution?\\
As the mode of a distribution is simply its highest point, it is the maximum of the posterior distribution. Consequently, the maximum is found by deriving the posterior distribution with respect to $\theta$, equating it to 0 and solving it. The posterior distribution is
\begin{equation}
\begin{split}
f(\theta) = \frac{\theta^{32}e^{-\theta(0.5+\sum_{i=1}^n x_i)}}
{\Gamma(33)\beta^\alpha},
\end{split}
\label{eq:postdistinserted}
\end{equation}
where the denominator is constant and can be excluded. The derivative computes to
\begin{equation}
\begin{split}
f_\theta'(\theta) = 32\theta^{31}e^{-\theta\left(0.5+\sum_{i=1}^n x_i\right)}+e^{-\theta\left(0.5+\sum_{i=1}^n x_i\right)}\left(-0.5-\sum_{i=1}^n x_i\right)\theta^{32}
\end{split}
\label{eq:postdistderived}
\end{equation}
and results in the following roots if equated to 0:
\begin{equation}
\begin{split}
\theta_1 = 0 \qquad \theta_2 = \frac{32}{0.5+\sum_{i=1}^n x_i}.
\end{split}
\label{eq:roots}
\end{equation}
$\theta_1 = 0$ is no solution for a gamma distribution. Consequently, $\theta_2$ is the only possible solution. Inserting $\bar{x} = 16.5$ from previous section results in
\begin{equation}
\begin{split}
\theta_2 = \frac{32}{0.5+10\cdot16.5} = \frac{32}{165.5} \approx 0.1934.
\end{split}
\label{eq:rootsN}
\end{equation}


% CREATED BY DAVID FRISK, 2016
\section{Theory}
In the following the section the underlying theory of the models used in the project are introduced and explained, along with relevant background and examples.

\subsection{CPG Model}

\subsubsection{Mathematical Model of Physical Behaviour}
%CPG is a mathematical model of human/animal behaviour. Limit cycle, standing wave. Robust, can handle disturbance. (Model different different joints as CPG. Model different DoF as CPG.)

Biological behaviour in mammals, such as walking or swimming, are usually repetative and oscillatory creating energy efficient movements. This can be seen in Figure \ref{fig:walkingCycle}, which shows the walking cycle of a human, measured as the acceleration in Z-direction whilst walking. Due to the rhytmic patterns in the behaviour, it is possible to construct a mathematical model to mimic this type of behaviour.

\begin{figure}[htbp]
    \centering
    \includegraphics[width=.75\textwidth]{include/figure/reference_right_hip_z.png}
    \caption{Walking cycle of a human. Where \textit{Z} is the horizontal direction of the reference system.}
    \label{fig:walkingCycle}
\end{figure}

A Central Pattern Generator (CPG) is a mathematical model of a neural oscillator, a neural network that produces rhytmic patterned outputs without sensory feedback \cite{CPGHauert}. The output of a CPG can be used as a model of biological rhytmic behaviour, for instance the activation of a certain muscle.

The activity of one muscle in an animal affect the activation of other muscles in its body automatically in order to distribute load and to achieve balance. Similarily CPGs can be modelled as a network, to account for more than a single muscle as well as the interaction between muscles.

\subsubsection{Model of Neural Oscillator} \label{modelOfNeuralOscillator}
%The different existing models. We chose the Matsuoka, show matsuoka sketch, describe parameters.

There exists many different models of CPGs \cite{CPGmodels}, the model to be examined throughout this project is the half-centre model. The half-centre model proposes to account for the alternate activation of the extensor and flexor muscles in the limbs, originally inspired by a cat walking. A mathimatical model was proposed by K. Matsuoka \cite{matsuoka}, using two mutually inhibatory neurons in each oscillator to account for the extensor-flexor behaviour. The model ensures that when one neuron is active, the other is surpressed, creating the oscillatory behaviour.

\begin{figure}[htbp]
    \centering
    \includegraphics[width=0.75\textwidth]{include/figure/matsuoka.png}
    \caption{The Matsuoka model of a half-centre CPG \cite{CPGwolff}, with parameters explained in Table \ref{tab:parameters}.}
    \label{fig:matsuoka}
\end{figure}

The setup of the Matsuoka model is shown in Figure \ref{fig:matsuoka}, with the parameters as described in Table \ref{tab:parameters}. The behaviour of the model is described by the following equations \cite{CPGequations}:

\begin{align}
    \tau_u \dot{u}_i &= -u_i - \beta v_i + \sum^{n}_{j=1} w_{ij}y_j + u_0, \\
    \tau_v \dot{v_i} &= -v_i + y_i, \\
    y_i &= \text{max}(0, u_i), \\
    o &= y_2 - y_1
\end{align}

where the model is simulated by integrating over time using Euler integration and timestep $\Delta t$ and $\dot{u}_i$, $\dot{v}_i$ from above in:

\begin{align}
    u_i(t + \Delta t) &= u_i(t) + \Delta t \dot{u}_i, \\
    v_i(t + \Delta t) &= v_i(t) + \Delta t \dot{v}_i.
\end{align}

\begin{table}[htbp]
    \centering
    \begin{tabular}{|c|p{6.5cm}|c|c|}
         \hline
\textbf{Parameter} & \textbf{Description} & \textbf{Initial value} & \textbf{Comment} \\ \hline
$z_i$ & input of neuron i &  & oscillating  \\ \hline
$u_0$ & external tonic input & 1.0 &  \\ \hline
$u_1$ & inner state of neuron 1 & 0.0 & fixed \\ \hline
$u_2$ & inner state of neuron 2 & 1.0 & fixed \\ \hline
$v_1$ & auxiliary variable measuring the degree of self-inhibition of neuron 1 & 1.0 & fixed \\ \hline
$v_2$ & auxiliary variable measuring the degree of self-inhibition of neuron 2 & 0.0 & fixed \\ \hline
$\beta$ & modulation parameter & 2.5 &  \\ \hline
$w_{ij}$ & weights connecting neuron j to neuron i & -2.0 &  \\ \hline
$y_i$ & the output of neuron i & 0 & \\ \hline
$\tau_u$ & time constant & 0.025 &  \\ \hline
$\tau_v$ & time constant & 0.3 &  \\ \hline
$o$ & the output of the oscillator & & \\ \hline
    \end{tabular}
    \caption{The parameters used in the Matsuoka model of a half-centre CPG.}
    \label{tab:parameters}
\end{table}

\subsubsection{Model of CPG Network} \label{CPGnetwork}
%Combining different CPG neurons on a network to model a human where each Neuron is a DoF.

Coupling multiple CPGs into a network makes it possible to model more complex structures, such as an animal or a human. This can be done by connecting the output of one neuron to the input of another. This project investigatex the walking behaviour of a human implemented on a humanoid robot, thus human behaviour can be modelled by looking at the movement in different joints of the body, corresponding to the joints of the humanoid robot. Since the output of a CPG is one-dimensional over time, and human movement is three dimensional, one can imagine modelling the movement in each dimension with a separate CPG. The output signals can then be superimposed to obtain the total movement.

\subsubsection{Choice of Input to Oscillator} \label{choiseOfInputToOscillator}
%The different models (summing output and weights). Asyncronous vs syncronous.
%2.1.3 in old report
Each of the neurons in a CPG network have inputs ($z_i$) that are affected by the output of the other neurons ($o$). One can imagine that all neurons are fully connected, but by setting the weight between two neurons to zero, that weight is \textit{removed}. The input to the neurons can be calculated in different ways, one way inspired by Liu et.al. \cite{liu} is to define the inputs to neuron $i$ as
\begin{align}
z_1 &= \sum_{j \text{ neighbour of } i} a_{ij}u_j, \\
z_2 &= 0.
\end{align}
Another implementation inspired by Shan et.al. \cite{shan} defines the inputs as
\begin{align}
z_1 &= \sum_{j \text{ neighbour of } i} a_{ij}[y_j]^+, \\
z_2 &= \sum_{j \text{ neighbour of } i} a_{ij}[y_j]^-,
\end{align}
where
\begin{align}
[y_j]^+ &:= \text{max}(y_j,0), \\
[y_j]^- &:= \text{min}(y_j,0).
\end{align}

Neurons can also be affected by an external tonic input ($u_0$). The amplitude of the oscillation in the CPG is proportional to the tonic input. If the input is oscillatory, the oscillator will lock to the frequency of the input. When the input is removed, the oscillator will smoothly return to the original frequency \cite{CPGwolff}.

\subsection{Optimisation Using Genetic Algorithm}
A CPG alone might not achieve satisfactory results, as the parameters in the CPG or network might need to be tweaked and optimised to achieve the desired behaviour. A commonly used technique for optimising behaviour is to use reinforcement learning to improve the model based on an external measure of success. This project uses a Genetic algorithm (GA) as a reinforcement learning technique to achieve desired walking behaviour. A genetic algorithm is an evoutionary algorithm that takes inspration from natural selection implemented as an optimisation problem \cite{GAHauert}.

\subsubsection{Genome}
%Difference between using only weights or the entire CPG network as genome.

The \textit{genome} is a set of parameters that defines a proposed solution to the problem that the GA is trying to solve. One can imagine various different genomes when optimising the behaviour of a CPG, for instance the internal parameters in the CPG, shown in Table \ref{tab:parameters}. The connection weights between neurons in a CPG network, as described in Section \ref{CPGnetwork}, could also be used to account relation between CPGs in a network.

\subsubsection{Operators}
%Init population, simulate behaviour, evaluation, selection, mutation, permutation
The basic implementation of a GA is in summary; initialise the population, simulate the behaviour, evaluate success, selecion process, mutation and permutations, repeat until converge. A more detailed description can be found in \cite{wahde}. The evaluation process, also known as the \textit{fitness function}, produces a fitness score which is the measurement the success of the individual. Since this project investigates walking behaviour in a humanoid robot, a fitness function may include e.g.; distance walked, time walked, stability, falling, walking style, etc.


% This LaTeX was auto-generated from an M-file by MATLAB.
% To make changes, update the M-file and republish this document.

\subsection*{dynamics\_cdp.m}

\begin{par}
\textbf{Summary:} Implements ths ODE for simulating the cart-double pendulum dynamics.
\end{par} \vspace{1em}

\begin{verbatim}  function dz = dynamics_cdp(t,z,f)\end{verbatim}
    \begin{par}
\textbf{Input arguments:}
\end{par} \vspace{1em}

\begin{lstlisting}
%		t     current time step (called from ODE solver)
%   z     state                                                    [6 x 1]
%   f     (optional): force f(t)
%
% *Output arguments:*
%
%   dz    if 3 input arguments:      state derivative wrt time
%         if only 2 input arguments: total mechanical energy
%
%   Note: It is assumed that the state variables are of the following order:
%         x:        [m]     position of cart
%         dx:       [m/s]   velocity of cart
%         dtheta1:  [rad/s] angular velocity of inner pendulum
%         dtheta2:  [rad/s] angular velocity of outer pendulum
%         theta1:   [rad]   angle of inner pendulum
%         theta2:   [rad]   angle of outer pendulum
%
%
% A detailed derivation of the dynamics can be found in:
%
% M.P. Deisenroth:
% Efficient Reinforcement Learning Using Gaussian Processes, Appendix C,
% KIT Scientific Publishing, 2010.
%
% Copyright (C) 2008-2013 by
% Marc Deisenroth, Andrew McHutchon, Joe Hall, and Carl Edward Rasmussen.
%
% Last modified: 2013-03-05

function dz = dynamics_cdp(t,z,f)
\end{lstlisting}

\subsection*{Code}

\begin{lstlisting}
% set up the system
m1 = 0.5;  % [kg]     mass of cart
m2 = 0.5;  % [kg]     mass of 1st pendulum
m3 = 0.5;  % [kg]     mass of 2nd pendulum
l2 = 0.6;  % [m]      length of 1st pendulum
l3 = 0.6;  % [m]      length of 2nd pendulum
b  = 0.1;  % [Ns/m]   coefficient of friction between cart and ground
g  = 9.82; % [m/s^2]  acceleration of gravity

if nargin == 3

  A = [2*(m1+m2+m3) -(m2+2*m3)*l2*cos(z(5)) -m3*l3*cos(z(6))
       -(3*m2+6*m3)*cos(z(5)) (2*m2+6*m3)*l2 3*m3*l3*cos(z(5)-z(6))
       -3*cos(z(6)) 3*l2*cos(z(5)-z(6)) 2*l3];
  b = [2*f(t)-2*b*z(2)-(m2+2*m3)*l2*z(3)^2*sin(z(5))-m3*l3*z(4)^2*sin(z(6))
       (3*m2+6*m3)*g*sin(z(5))-3*m3*l3*z(4)^2*sin(z(5)-z(6))
       3*l2*z(3)^2*sin(z(5)-z(6))+3*g*sin(z(6))];
  x = A\b;

  dz = zeros(6,1);
  dz(1) = z(2);
  dz(2) = x(1);
  dz(3) = x(2);
  dz(4) = x(3);
  dz(5) = z(3);
  dz(6) = z(4);

else

  dz = (m1+m2+m3)*z(2)^2/2+(m2/6+m3/2)*l2^2*z(3)^2+m3*l3^2*z(4)^2/6 ...
       -(m2/2+m3)*l2*z(2)*z(3)*cos(z(5))-m3*l3*z(2)*z(4)*cos(z(6))/2 ...
       +m3*l2*l3*z(3)*z(4) *cos(z(5)-z(6))/2+(m2/2+m3)*l2*g*cos(z(5)) ...
       +m3*l3*g*cos(z(6))/2;

% I2 = m2*l2^2/12;  % moment of inertia around pendulum midpoint (1st link)
% I3 = m3*l3^2/12;  % moment of inertia around pendulum midpoint (2nd link)
%
%
% dz = m1*z(2)^2/2 + m2/2*(z(2)^2-l2*z(2)*z(3)*cos(z(5))) ...
%     + m3/2*(z(2)^2 - 2*l2*z(2)*z(3)*cos(z(5)) - l3*z(2)*z(4)*cos(z(6))) ...
%     + m2*l2^2*z(3)^2/8 + I2*z(3)^2/2 ...
%     + m3/2*(l2^2*z(3)^2 + l3^2*z(4)^2/4 + l2*l3*z(3)*z(4)*cos(z(5)-z(6))) ...
%     + I3*z(4)^2/2 ...
%     + m2*g*l2*cos(z(5))/2 + m3*g*(l2*cos(z(5))+l3*cos(z(6))/2);

end
\end{lstlisting}


% !TEX root = ../main.tex
\newpage
\section*{Nomenclature}
\addcontentsline{toc}{subsection}{Nomenclature}
\vspace{-.5cm}
\begin{alignat*}{2}
& \ic &&\text{Imaginary unit.}\\
&e \text{ (or} \exp\text{)} &&\text{Euler's number.}\\ \\
& n&&\text{Network node.}\\
& N &&\text{Network degree. The number of nodes in the network.}\\
& A_{ij} &&\text{Adjacency matrix. Models which node $i$ is connected to node $j$ and vice-versa.}\\
& \kmean &&\text{Average node degree in the network.}\\
& \degree(n) = \k &&\text{Degree of node $n$, as a vector of the in- and out-degree $\left(\kin, \kout \right)$.}\\
& \kinb, \koutb && \text{Node degree vector of all in- and out degrees of the network.} \\
& M_{\k} &&\text{Number of unique node degrees in the network. Cardinality of $\K$.}\\
& P(k), P(\k) &&\text{Univariate and bivariate network degree distribution.}\\
&\kmin, \kmax &&\text{Lowest and highest degree in a network.}\\
&\gamma &&\text{Degree exponent of a scale-free network.}\\
&p &&\text{Probability threshold of forming a link in random networks.}\\ \\
&\theta_i(t) &&\text{Phase variable function of the theta model (of neuron $i$).}\\
& \Pulse_s (\theta)&&\text{Pulse-shaped synaptic coupling function with sharpness } s \text{.}\\
&\kappa &&\text{Macroscopic coupling strength.}\\
&\eta_i, I_i(t) &&\text{Excitability threshold and input current (of neuron $i$).}\\
&g(\eta \rvert \k) \quad &&\text{Excitability threshold distribution with mean $\eta_0(\k)$ and width $\sigma(\k)$.}\\ \\
& Z(t) &&\text{Kuramoto order parameter function.}\\
& z(\k, t) &&\text{Synchronisation dynamics function for nodes of degree $\k$.}\\
& \bar{Z}(t) &&\text{Mean field order parameter function for arbitrary networks.}\\ \\
& S_i^{\rm in}(t), S_i^{\rm out}(t)\quad &&\text{Spike trains received and emitted by neuron $i$ as a sum of delta functions in time.}\\
& K_{ij} &&\text{Coupling matrix. Strength of the synaptic connections between neurons $i$ and $j$.}\\
& \Delta t_{ij} &&\text{Time difference between spikes of neurons $i$ and $j$.}\\
& W(t) &&\text{Learning window. Correlation between change in synaptic strength and spike times.}\\
& \phi(t) &&\text{IP learning function. Correlation between excitability strength and spike times.}\\ \\
& \T &&\text{Set of angles in [-$\pi$, $\pi$[.}\\
& \K &&\text{Set of $M_{\k}$ unique degrees in a network, support of $P$.}\\
& \R, \c &&\text{Set of real and complex numbers.}\\
&  \C && \text{Set in the complex unit circle so that $\C = \{ z \in \c : \: \rvert z \rvert \leq 1 \} $.} \\ \\
& \permute(v), \permuteinv(v) &&\text{Random permutation and inverse permutation of the elements of a vector $v$.}\\
\end{alignat*}


% declare document class and geometry
\documentclass[12pt]{article} % use larger type; default would be 10pt
\usepackage[margin=1in]{geometry} % handle page geometry

% import packages and commands
\input{../header2.tex}

\title{Math 217 -- Geometry and Physics -- Lec06}
\author{UCLA, Fall 2014}
\date{\formatdate{15}{10}{2014}} % Activate to display a given date or no date (if empty),
         % otherwise the current date is printed

\begin{document}
\maketitle

\section{More stuff}

Let $E^r \rightarrow M$ be a complex or real vector bundle on a compact closed manifold.

\begin{definition}
Define
\begin{eqn}
\Gamma(M,E) = \Gamma(E) = \set{ s : M \rightarrow E \text{ such that } \pi \circ s = \opname{id}_M}.
\end{eqn}
\end{definition}

For example, we have
\begin{align}
\Gamma(T^*M) &= \set{ \sum_i f_i dx_i \text{ on $U_\alpha$}} \\
\Gamma(TM) &= \set{ \sum_i f_i \pd{}{x_i} }.
\end{align}

\begin{definition}
We define $S^p E$ the symmetric products, $\Lambda^p E$ the exterior products, and $\Omega^p(M) = \Gamma(\Lambda^p T^*M)$.
\end{definition}

\begin{definition}
We define a connection on $E$ as a map
\begin{eqn}
\nabla : \Gamma(E) \rightarrow \Gamma(T^*M \otimes E)
\end{eqn}
such that
\begin{enumerate}
\item $\nabla(s_1 + s_2) = \nabla s_1 + \nabla s_2$, and
\item $\nabla(fs) = (\dif{f}) s + f (\nabla s)$ for $f \in C^\infty (M)$.
\end{enumerate}
\end{definition}

\begin{definition}
We define the curvature of $\nabla$ as
\begin{eqn}
\Omega = \nabla^2 : \Gamma(E) \rightarrow \Gamma(\Lambda^2 T^* M \otimes E).
\end{eqn}
\end{definition}

\begin{claim}
$\Omega$ is a $C^\infty(M)$-linear operator (tensor).
\end{claim}

\begin{proof}
We have
\begin{align}
\Omega(fs) &= \nabla^2 (fs) = \nabla(\nabla(fs)) \\
	&= \nabla( \dif{f} \, s) + \nabla(f \nabla s) \\
	&= [ \underbrace{\dif (\dif{f})}_{=0} s + \cancel{(-1) \dif{f} \nabla s}] + [\cancel{\dif{f} \nabla s} + f \nabla^2 s] \\
	&= f \Omega(s).
\end{align}
\end{proof}

\begin{definition}
Given a curvature $\Omega^\nabla$, we can expand
\begin{eqn}
\det(t I_r + \frac{i}{2\pi} \Omega^\nabla) = \underbrace{c_0^\nabla(E)}_{1} t^r + \underbrace{c_1^\nabla(E)}_\text{2-form} t^{r-1} + \dots + \underbrace{c_r^\nabla(E)}_\text{$2r$-form}.
\end{eqn}
We call $\set{c_k^\nabla(E)}$ the Chern forms of $(E,\nabla)$.
\end{definition}

\begin{theorem}[Chern-Weil]
Let $M$ be a compact closed manifold, $E$ a complex vector bundle. Then
\begin{enumerate}
\item $\dif c_k^\nabla(E) = 0$, which implies that
\item $[c_k^\nabla(E)] \in H^{2k}(M)$. Furthermore, $[c_k^\nabla(E)]$ is independent of $\nabla$, so we define $c_k(E) = [c_k^\nabla(E)]$ as the \textbf{Chern class} of $E$.
\end{enumerate}
\end{theorem}

We will see that Chern-Simons follows from Chern-Weil.

\begin{remark}
Given a function with series expansion
\begin{align}
f(x) &= a_0 + a_1 x + \dots + a_r x^r + \dots \in \C[[x]], \\
\shortintertext{we have}
f(\Omega) &= a_0 I + a_1 \Omega + \dots + a_r \Omega^r \in \C[\Omega],
\end{align}
thus,
\begin{eqn}
\tr f(\Omega) = \sum_{k=0}^n a_k \tr \Omega^k.
\end{eqn}
\end{remark}

\begin{lemma}
We find that
\begin{enumerate}
\item $\dif \tr f(\Omega) = 0$, and
\item if $\nabla'$ is another connection, then
\begin{eqn}
\tr f(\Omega') - \tr f(\Omega) = \dif{\eta}
\end{eqn}
for some $\eta \in \Omega^*(M)$.
\end{enumerate}
\end{lemma}

This lemma implies Chern-Weil. We also find that
\begin{align}
\det (I + \frac{i}{2\pi} \Omega) &= \exp \tr \log (I + \frac{i}{2\pi} \Omega) \\
	&= \exp \log \det (I + \frac{i}{2\pi} \Omega),
\end{align}
from the linear algebra identity $\tr \log A = \log \det A$.

\begin{remark}
Now, given
\begin{eqn}
\omega, \eta \in \Omega^*(M), \qquad A, B \in \Gamma(\opname{Hom}(E,E)) = \opname{End}(E),
\end{eqn}
we have
\begin{eqn}
[\omega A, \eta B] = (\omega A)(\eta B) - (-1)^{\deg \omega \deg \eta} (\eta B) (\omega A).
\end{eqn}
Then we find
\begin{eqn}
\tr [\omega A, \eta B] = 0.
\end{eqn}
\end{remark}
(Show this as an exercise.)

\end{document}


\chapter{Ordinals}
\label{ch:ordinal}
\section{Counting for preschoolers}
In preschool, we were told to count as follows.
We defined a set of symbols $1$, $2$, $3$, $4$, \dots.
Then the teacher would hold up three apples and say:
\begin{quote}
	``One . . .\ two . . .\ three!  There are three apples.''
\end{quote}

\begin{center}
	\includegraphics[height=5cm]{media/three-apples.jpg}
	\\ \scriptsize Image from \cite{img:apples}
\end{center}

The implicit definition is that the \emph{last} number said is the final answer.
This raises some obvious problems if we try to count infinite sets,
but even in the finite world,
this method of counting fails for the simplest set of all:
how many apples are in the following picture?

\begin{center}
	\includegraphics[height=7cm]{media/velociraptor.jpg}
	\\ \scriptsize Image from \cite{img:velociraptor}
\end{center}

Answer: $0$. There is nothing to say, and our method of counting has failed
for the simplest set of all: the empty set.

\section{Counting for set theorists}
\prototype{$\omega+1 = \{0,1,2,\dots,\omega\}$ might work.}
Rather than using the \emph{last} number listed, I propose instead
starting with a list of symbols $0$, $1$, $2$, \dots\ and making
the final answer the \emph{first} number which was \emph{not} said.
Thus to count three apples, we would say
\begin{quote}
	``Zero . . .\ one . . .\ two!  There are three apples.''
\end{quote}
We will call these numbers \emph{ordinal numbers} (rigorous definition later).
In particular, we'll \emph{define} each ordinal to be the set of things we say:
\begin{align*}
	0 &= \varnothing \\
	1 &= \{0\} \\
	2 &= \{0,1\} \\
	3 &= \{0,1,2\} \\
	&\vdotswithin=
\end{align*}
In this way we can write out the natural numbers.
You can have some fun with this, by saying things like
\[
	4 \defeq
	\left\{
		\left\{  \right\},
		\left\{ \left\{  \right\} \right\},
		\left\{ \left\{  \right\}, \left\{ \left\{  \right\} \right\} \right\},
		\left\{
			\left\{  \right\},
			\left\{ \left\{  \right\} \right\},
			\left\{ \left\{  \right\}, \left\{ \left\{  \right\} \right\} \right\}
		\right\}
	\right\}.
\]
In this way, we soon write down all the natural numbers.
The next ordinal, $\omega$,\footnote{As mentioned in the last chapter,
	it's not immediate that $\omega$ is a set;
	its existence is generally postulated by the $\Infinity$ axiom.
} is defined as
\begin{align*}
	\omega &= \left\{ 0, 1, 2, \dots \right\} \\
	\intertext{Then comes}
	\omega+1 &= \left\{ 0, 1, 2, \dots, \omega \right\} \\
	\omega+2 &= \left\{ 0, 1, 2, \dots, \omega, \omega+1 \right\} \\
	\omega+3 &= \left\{ 0, 1, 2, \dots, \omega, \omega+1, \omega+2 \right\} \\
	&\vdotswithin= \\
	\intertext{And in this way we define $\omega+n$, and eventually reach}
	\omega \cdot 2 = \omega+\omega &= \left\{ 0, 1, 2 \dots, \omega, \omega+1, \omega+2, \dots \right\} \\
	\omega \cdot 2 + 1 &= \left\{ 0, 1, 2 \dots, \omega, \omega+1, \omega+2, \dots, \omega \cdot 2 \right\}.
\end{align*}
In this way we obtain
\begin{align*}
	0,\; & 1,\; 2,\; 3,\; \dots,\; \omega \\
	& \omega+1,\; \omega+2,\; \dots,\; \omega+\omega \\
	& \omega \cdot 2 +1,\; \omega \cdot 2 +2,\; \dots,\; \omega \cdot 3,\; \\
	& \vdots \\
	& \omega^2 + 1,\; \omega^2+2,\; \dots \\
	& \vdots \\
	& \omega^3,\; \dots,\; \omega^4,\; \dots,\; \omega^\omega \\
	& \vdots \\
	& \omega^{\omega^{\omega^{\dots}}} \\
\end{align*}

The first several ordinals can be illustrated in a nice spiral.
\begin{center}
	\includegraphics[scale=0.60]{media/500px-Omega-exp-omega-labeled.png}
\end{center}

\begin{remark}
	(Digression)
	The number $\omega^{\omega^{\omega^{\dots}}}$ has a name, $\eps_0$;
	it has the property that $\omega^{\eps_0} = \eps_0$.
	The reason for using ``$\eps$'' (which is usually used to denote small quantities)
	is that, despite how huge it may appear, it is actually a countable set.
	More on that later.
\end{remark}

\section{Definition of an ordinal}
Our informal description of ordinals gives us a chain
\[ 0 \in 1 \in 2 \in \dots \in \omega \in \omega+1 \in \dots. \]
To give the actual definition of an ordinal, I need to define two auxiliary terms first.
\begin{definition}
	A set $x$ is \vocab{transitive} if whenever $z \in y \in x$, we have $z \in x$ also.
\end{definition}
\begin{example}
	[$7$ is transitive]
	The set $7$ is transitive: for example, $2 \in 5 \in 7 \implies 2 \in 7$.
\end{example}
\begin{ques}
	Show that this is equivalent to: whenever $y \in x$, $y \subseteq x$.
\end{ques}
Moreover, recall the definition of ``well-ordering'': a strict linear order
with no infinite descending chains.
\begin{example}
	[$\in$ is a well-ordering on $\omega \cdot 3$]
	In $\omega \cdot 3$, we have an ordering
	\[ 0 \in 1 \in 2 \in \dots \in \omega \in \omega+1 \in \dots
		\in \omega \cdot 2 \in \omega \cdot 2 + 1 \in \dots. \]
	which has no infinite descending chains.
	Indeed, a typical descending chain might look like
	\[ \omega \cdot 2 + 6 \ni \omega \cdot 2 \ni
		\omega + 2015 \ni \omega+3 \ni \omega \ni 1000 \ni 256 \ni 42 \ni 7 \ni 0. \]
	Even though there are infinitely many elements, there is no way
	to make an infinite descending chain.
\end{example}
\begin{exercise}
	(Important)
	Convince yourself there are no infinite
	descending chains of ordinals at all,
	without using the $\Foundation$ axiom.
\end{exercise}

\begin{definition}
	An \vocab{ordinal} is a transitive set which is well-ordered by $\in$.
	The class of all ordinals is denoted $\On$.
\end{definition}

\begin{ques}
	Satisfy yourself that this definition works.
\end{ques}

We typically use Greek letters $\alpha$, $\beta$, etc.\ for ordinal numbers.
\begin{definition}
	We write
	\begin{itemize}
		\ii $\alpha < \beta$ to mean $\alpha \in \beta$,
		and $\alpha > \beta$ to mean $\alpha \ni \beta$.
		\ii $\alpha \le \beta$ to mean $\alpha \in \beta$ or $\alpha = \beta$,
		and $\alpha \ge \beta$ to mean $\alpha \ni \beta$ or $\alpha = \beta$,
	\end{itemize}
\end{definition}

\begin{theorem}[Ordinals are strictly ordered]
	Given any two ordinal numbers $\alpha$ and $\beta$,
	either $\alpha < \beta$, $\alpha = \beta$ or $\alpha > \beta$.
\end{theorem}
\begin{proof}
	Surprisingly annoying, thus omitted.
\end{proof}
\begin{theorem}[Ordinals represent all order types]
	Suppose $<$ is a well-ordering on a set $X$.
	Then there exists a unique ordinal $\alpha$
	such that there is a bijection $\alpha \to X$
	which is order preserving.
\end{theorem}
Thus ordinals represent the possible \emph{equivalence classes} of order types.
Any time you have a well-ordered set, it is isomorphic to a unique ordinal.

We now formalize the ``$+1$'' operation we were doing:
\begin{definition}
	Given an ordinal $\alpha$, we let $\alpha+1 = \alpha \cup \{\alpha\}$.
	An ordinal of the form $\alpha+1$ is called a \vocab{successor ordinal}.
\end{definition}
\begin{definition}
	If $\lambda$ is an ordinal which is neither zero nor a successor ordinal,
	then we say $\lambda$ is a \vocab{limit ordinal}.
\end{definition}
\begin{example}
	[Sucessor and limit ordinals]
	$7$, $\omega+3$, $\omega\cdot2+2015$ are successor ordinals,
	but $\omega$ and $\omega \cdot 2$ are limit ordinals.
\end{example}

\section{Ordinals are ``tall''}
First, we note that:
\begin{theorem}
	[There is no set of all ordinals]
	$\On$ is a proper class.
\end{theorem}
\begin{proof}
	Assume for contradiction not.
	Then $\On$ is well-ordered by $\in$ and transitive, so $\On$ is an ordinal,
	i.e.\ $\On \in \On$, which violates $\Foundation$.
\end{proof}
\begin{exercise}
	[Unimportant] Give a proof without $\Foundation$ by considering $\On+1$.
\end{exercise}

From this we deduce:
\begin{theorem}
	[Sets of ordinals are bounded]
	Let $A \subseteq \On$.
	Then there is some ordinal $\alpha$ such that $A \subseteq \alpha$
	(i.e.\ $A$ must be bounded).
\end{theorem}
\begin{proof}
	Otherwise, look at $\bigcup A$.
	It is a set.
	But if $A$ is unbounded it must equal $\On$,
	which is a contradiction.
\end{proof}
In light of this, every set of ordinals has a \vocab{supremum},
which is the least upper bound. We denote this by $\sup A$.

\begin{ques}
	Show that
	\begin{enumerate}[(a)]
		\ii $\sup (\alpha+1) = \alpha$ for any ordinal $\alpha$.
		\ii $\sup \lambda = \lambda$ for any limit ordinal $\lambda$.
	\end{enumerate}
\end{ques}

The pictorial ``tall'' will be explained in a few sections.

\section{Transfinite induction and recursion}
The fact that $\in$ has no infinite descending chains means that induction and recursion still work verbatim.
\begin{theorem}[Transfinite induction]
	Given a statement $P(-)$, suppose that
	\begin{itemize}
		\ii $P(0)$ is true, and
		\ii If $P(\alpha)$ is true for all $\alpha < \beta$, then $P(\beta)$ is true.
	\end{itemize}
	Then $P(\alpha)$ is true for every ordinal $\alpha$.
\end{theorem}
\begin{theorem}
	[Transfinite recursion]
	To define a sequence $x_\alpha$ for every ordinal $\alpha$,
	it suffices to
	\begin{itemize}
		\ii define $x_0$, then
		\ii for any $\beta$, define $x_\beta$ for any $\alpha < \beta$.
	\end{itemize}
\end{theorem}

The difference between this and normal induction lies in the \emph{limit ordinals}.
In real life, we might only do things like ``define $x_{n+1} = \dots$''.
But this is not enough to define $x_\alpha$ for all $\alpha$,
because we can't hit $\omega$ this way.
Similarly, the simple $+1$ doesn't let us hit the ordinal $2\omega$,
even if we already have $\omega+n$ for all $n$.
In other words, simply incrementing by $1$ cannot get us past limit stages,
but using transfinite induction to jump upwards lets us sidestep this issue.

So a transfinite induction or recursion is very often broken up into three cases.
In the induction phrasing, it looks like
\begin{itemize}
	\ii (Zero Case) First, resolve $P(0)$.
	\ii (Successor Case) Show that from $P(\alpha)$ we can get $P(\alpha+1)$.
	\ii (Limit Case) For $\lambda$ a limit ordinal,
	show that $P(\lambda)$ holds given $P(\alpha)$ for all $\alpha < \lambda$.
\end{itemize}
Similarly, transfinite recursion often is split into cases too.
\begin{itemize}
	\ii (Zero Case) First, define $x_0$.
	\ii (Successor Case) Define $x_{\alpha+1}$ from $x_\alpha$.
	\ii (Limit Case) Define $x_\lambda$ from $x_\alpha$ for all $\alpha < \lambda$,
	where $\lambda$ is a limit ordinal.
\end{itemize}
In both situations, finite induction only does the first two cases,
but if we're able to do the third case we can climb above the barrier $\omega$.

\section{Ordinal arithmetic}
\prototype{$1+\omega=\omega \neq \omega+1$.}
To give an example of transfinite recursion, let's define addition of ordinals.
Recall that we defined $\alpha+1 = \alpha \cup \{\alpha\}$.
By transfinite recursion, let
\begin{align*}
	\alpha + 0 &= \alpha \\
	\alpha + (\beta + 1) &= (\alpha + \beta) + 1 \\
	\alpha + \lambda &= \bigcup_{\beta < \lambda} (\alpha + \beta).
\end{align*}
Here $\lambda \neq 0$.

We can also do this explicitly:
The picture is to just line up $\alpha$ after $\beta$.
That is, we can consider the set
\[
	X =
	\left( \left\{ 0 \right\} \times \alpha \right)
	\cup
	\left( \left\{ 1 \right\} \times \beta \right)
\]
(i.e.\ we tag each element of $\alpha$ with a $0$, and
each element of $\beta$ with a $1$).
We then impose a well-ordering on $X$ by a lexicographic ordering $\llex$
(sort by first component, then by second).
This well-ordering is isomorphic to a unique ordinal,
\begin{example}
	[$2+3=5$]
	Under the explicit construction for $\alpha = 2$ and $\beta = 3$, we get the set
	\[
		X = \left\{ (0,0) < (0,1) < (1,0) < (1,1) < (1,2) \right\}
	\]
	which is isomorphic to $5$.
\end{example}

\begin{example}[Ordinal arithmetic is not commutative]
	Note that $1 + \omega = \omega$!
	Indeed, under the transfinite definition, we have
	\[ 1 + \omega = \cup_n (1+n) = 2 \cup 3 \cup 4 \cup \dots = \omega. \]
	With the explicit construction, we have
	\[ X = \left\{ (0,0) < (1,0) < (1,1) < (1,2) < \dots \right\} \]
	which is isomorphic to $\omega$.
\end{example}
\begin{exercise}
	Show that $n+\omega = \omega$ for any $n \in \omega$.
\end{exercise}

\begin{remark}
	Ordinal addition is not commutative.
	However, from the explicit construction
	we can see that it is at least associative.
\end{remark}

Similarly, we can define multiplication in two ways.
By transfinite induction:
\begin{align*}
	\alpha \cdot 0 &= 0 \\
	\alpha \cdot (\beta + 1) &= (\alpha \cdot \beta) + \alpha \\
	\alpha \cdot \lambda &= \bigcup_{\beta < \lambda} \alpha \cdot \beta.
\end{align*}
We can also do an explicit construction: $\alpha \cdot \beta$
is the order type of
\[ \llex \text{ applied to } \beta \times \alpha. \]
\begin{example}[Ordinal multiplication is not commutative]
	We have $\omega \cdot 2 = \omega + \omega$,
	but $2 \cdot \omega = \omega$.
\end{example}
\begin{exercise}
	Prove this.
\end{exercise}
\begin{exercise}
	Verify that ordinal multiplication
	(like addition) is associative but not commutative.
	(Look at $\gamma \times \beta \times \alpha$.)
\end{exercise}

Exponentiation can also be so defined, though the explicit construction is less natural.
\begin{align*}
	\alpha^0 &= 1 \\
	\alpha^{\beta+1} &= \alpha^{\beta} \cdot \alpha \\
	\alpha^{\lambda} &= \bigcup_{\beta < \lambda} \alpha^\beta.
\end{align*}
\begin{exercise}
	Verify that $2^\omega = \omega$.
\end{exercise}

\section{The hierarchy of sets}
We now define the \vocab{von Neumann Hierarchy} by transfinite recursion.
\begin{definition}
	By transfinite recursion, we set
	\begin{align*}
		V_0 &= \varnothing \\
		V_{\alpha + 1} &= \PP(V_\alpha) \\
		V_\lambda &= \bigcup_{\alpha<\lambda} V_\alpha
	\end{align*}
\end{definition}
By transfinite induction, we see $V_\alpha$ is transitive
and that $V_\alpha \subseteq V_\beta$ for all $\alpha < \beta$.

\begin{example}[$V_\alpha$ for $\alpha \le 3$]
	The first few levels of the hierarchy are:
	\begin{align*}
		V_0 &= \varnothing \\
		V_1 &= \left\{ 0 \right\} \\
		V_2 &=  \left\{ 0, 1 \right\} \\
		V_3 &= \left\{ 0, 1, 2, \left\{ 1 \right\} \right\}.
	\end{align*}
	Notice that for each $n$, $V_n$ consists of only finite sets,
	and each $n$ appears in $V_{n+1}$ for the first time.
	Observe that
	\[ V_\omega = \bigcup_{n \in \omega} V_n \]
	consists only of finite sets; thus $\omega$ appears for the first time
	in $V_{\omega+1}$.
\end{example}
\begin{ques}
	How many sets are in $V_5$?
\end{ques}

\begin{definition}
	The \vocab{rank} of a set $y$, denoted $\rank(y)$,
	is the smallest ordinal $\alpha$ such that $y \in V_{\alpha+1}$.
\end{definition}
\begin{example}
	$\rank(2) = 2$, and actually $\rank(\alpha)=\alpha$
	for any ordinal $\alpha$ (problem later).
	This is the reason for the extra ``$+1$''.
\end{example}
\begin{ques}
	Show that $\rank(y)$ is the smallest ordinal $\alpha$
	such that $y \subseteq V_\alpha$.
\end{ques}

It's not yet clear that the rank of a set actually exists, so we prove:
\begin{theorem}[The von Neumann hierachy is complete]
	The class $V$ is equal to $\bigcup_{\alpha \in \On} V_\alpha$.
	In other words, every set appears in some $V_\alpha$.
\end{theorem}
\begin{proof}
	Assume for contradiction this is false.
	The key is that because $\in$ satisfies $\Foundation$,
	we can take a $\in$-minimal counterexample $x$.
	Thus $\rank(y)$ is defined for every $y \in x$,
	and we can consider (by $\Replacement$) the set
	\[ \left\{ \rank(y) \mid y \in x \right\}. \]
	Since it is a set of ordinals, it is bounded.
	So there is some large ordinal $\alpha$ such that $y \in V_\alpha$
	for all $y \in x$, i.e.\ $x \subseteq V_\alpha$,
	so $x \in V_{\alpha+1}$.
\end{proof}

This leads us to a picture of the universe $V$:
\begin{center}
	\begin{asy}
		size(11cm);
		pair A = (12,30);
		pair B = -conj(A);
		pair M = midpoint(A--B);
		pair O = origin;
		MP("V", A, dir(10));
		draw(A--O--B);

		fill(A--O--B--cycle, opacity(0.3)+palecyan);

		MP("V_0 = \varnothing", origin, dir(-20));
		MP("V_1 = \{\varnothing\}", 0.05*A, dir(0));
		MP("V_2 = \{\varnothing, \{\varnothing\} \}", 0.10*A, dir(0));

		draw(MP("V_n", 0.3*A, dir(0))--0.3*B);
		draw(MP("V_{n+1} = \mathcal P(V_n)", 0.35*A, dir(0))--0.35*B);
		Drawing("n", 0.35*M, dir(45));

		draw(MP("V_\omega = \bigcup V_n", 0.5*A, dir(0))--0.5*B);
		draw(MP("V_{\omega+1} = \mathcal P(V_{\omega})", 0.55*A, dir(0))--0.55*B);
		Drawing("\omega", 0.55*M, dir(45));
		draw(MP("V_{\omega+2} = \mathcal P(V_{\omega+1})", 0.6*A, dir(0))--0.6*B);
		Drawing("\omega+1", 0.6*M, dir(45));

		draw(MP("V_{\omega+\omega}", 0.8*A, dir(0))--0.8*B);

		draw(origin--M);
		MP("\mathrm{On}", M, dir(90));

	\end{asy}
\end{center}

We can imagine the universe $V$ as a triangle,
built in several stages or layers,
$V_0 \subsetneq V_1 \subsetneq V_2 \subsetneq \dots$.
This universe doesn't have a top: but each of the $V_i$ do.
However, the universe has a very clear bottom.
Each stage is substantially wider than the previous one.

In the center of this universe are the ordinals:
for every successor $V_\alpha$, exactly one new ordinal appears, namely $\alpha$.
Thus we can picture the class of ordinals as a thin line
that stretches the entire height of the universe.
A set has rank $\alpha$ if it appears at the same stage that $\alpha$ does.

All of number theory, the study of the integers, lives inside $V_\omega$.
Real analysis, the study of real numbers, lives inside $V_{\omega+1}$, since a real number
can be encoded as a subset of $\NN$ (by binary expansion).
Functional analysis lives one step past that, $V_{\omega+2}$.
For all intents and purposes, most mathematics does not go beyond $V_{\omega+\omega}$.
This pales in comparison to the true magnitude of the whole universe.

\section\problemhead
\begin{problem}
	Prove that $\rank(\alpha) = \alpha$ for any $\alpha$
	by transfinite induction.
\end{problem}

\begin{problem}
	[Online Math Open]
	Count the number of transitive sets in $V_5$.
\end{problem}

\begin{problem}
	[Goodstein]
	Let $a_2$ be any positive integer.
	We define the infinite sequence $a_2$, $a_3$, \dots recursively as follows.
	If $a_{n} = 0$, then $a_{n+1} = 0$.
	Otherwise, we write $a_n$ in base $n$,
	then write all exponents in base $n$, and so on until all
	numbers in the expression are at most $n$.
	Then we replace all instances of $n$ by $n+1$
	(including the exponents!), subtract $1$,
	and set the result to $a_{n+1}$.
	For example, if $a_2 = 11$ we have
		\begin{align*}
			a_2 &= 2^{3} + 2 + 1 = 2^{2+1} + 2 + 1 \\
			a_3 &= 3^{3+1}+3+1-1 = 3^{3+1} + 3\\
			a_4 &= 4^{4+1} + 4 - 1 = 4^{4+1} + 3 \\
			a_5 &= 5^{5+1} + 3 - 1 = 5^{5+1} + 2
		\end{align*}
	and so on. Prove that $a_N = 0$ for some integer $N > 2$.
\end{problem}