\section{Background}
\label{s:background}
%
In this section, we review the foundations on which \sys is built.
%
In \autoref{sec:logic}, we review definitions from formal logic; %
in \autoref{sec:rtlc}, we define a $\lambda$-calculus with refinement
types; %
in \autoref{sec:chcs}, we define systems of Constrained Horn Clauses.

% formal logic:
\subsection{Formal logic}
\label{sec:logic}
%
\sys uses logic to express and infer refinement types of programs.
%
All objects defined in this paper are defined over a fixed space of
logical variables $X$.
%
In particular, for theory $\mathcal{T}$, the space of $\mathcal{T}$
formulas over $X$ is denoted $\tformulas{\mathcal{T}}$.
%
For each formula $\varphi \in \tformulas{\mathcal{T}}$, the set of
variables that occur in $\varphi$ (i.e., the \emph{vocabulary} of
$\varphi$) is denoted $\vocab(\varphi)$.
% define models, satisfaction, entailment
For formulas $\varphi_0, \ldots, \varphi_n, \varphi \in
\tformulas{\mathcal{T}}$, the fact that $\varphi_0, \ldots, \varphi_n$
\emph{entail} $\varphi$ is denoted $\varphi_0, \ldots, \varphi_n
\entails \varphi$.

% Define replacement and substitution:
For sequences of variables $Y = [ y_0, \ldots, y_n ]$ and $Z = [ z_0,
\ldots, z_n ]$, the $\mathcal{T}$ formula constraining the equality of
each element in $Y$ with its corresponding element in $Z$, i.e., the
formula $\bigland_{0 \leq i \leq n} y_i = z_i$, is denoted $Y = Z$.
%
The repeated replacement of variables $\varphi[ z_0 / y_0 \ldots z_{n}
/ y_{n} ]$ is denoted $\replace{\varphi}{Y}{Z}$.
%
For each formula $\varphi$ defined over free variables $Y$,
$\replace{\varphi}{Y}{Z}$ is denoted alternatively as $\varphi[ Z ]$.
%
The number of free variables in formula $\varphi$ is denoted
$\degreeof{\varphi}$.

\subsection{A $\lambda$-calculus with refinement types}
\label{sec:rtlc}
\begin{figure*}[t]
  \begin{floatrow}[2]
    \ffigbox[.48\textwidth] %
    { \caption{The space of program expressions, $\expr$.} %
      \label{fig:exprs} }
    {  \begin{align*}
        % expression is a constant,
        e \rightarrow &\ c \\
        % or a variable,
        | &\ x \\
        % or a binary operation,
        | &\ e \oplus e \\
        % or a constructor application,
        | &\ C(e_0, \ldots, e_k) \\
        % or case split,
        | &\ \cc{match}\ e\ \cc{with}\ %
        C_0(\overline{x}_0)\ \cc{->}\ e_0 %
        \ldots %
        C_k(\overline{x}_k)\ \cc{->}\ e_k \\
        % or an abstraction,
        | &\ \lambda x.\ e \\
        % or an application,
        | &\ e\ e \\
        % or a fixpoint,
        | &\ \fix{ f }{ e }
      \end{align*} }
    %
    \ffigbox[.48\textwidth] %
    {\caption{The space of types, $\types$.
        %
        Metavariable $\tau$ ranges over $\types$, %
        $x_i$ and $y_i$ range over individual value variables, %
        $\overline{x}$ range over vectors of value variables, and %
        $X$ ranges over type variables.} %
      \label{fig:types} }
    %
    { \begin{align*} %
        % a type is a base type,
        \tau \rightarrow &\ \setformer{ \nu : \bools }{ \varphi } \\
        | &\ \setformer{ \nu : \ints}{ \varphi } \\
        % a type variable,
        | &\ X \overline{x} \\
        % a function type, or
        | &\ x : \tau \rightarrow \tau \\
        % a recursive type
        | &\ \mu X \overline{x}.\ %
        C_0(y_0^0 : \tau_0^0, \ldots, %
        y_0^{ \arityof{C_0} } : \tau_0^{ \arityof{C_0} }), %
        \ldots, \\
        &\ C_n(y_n^0 : \tau_n^0, \ldots, %
        y_n^{ \arityof{C_n} } : \tau_n^{ \arityof{C_n} })
      \end{align*} }
  \end{floatrow}
\end{figure*}

% define STLC syntax:
\paragraph{Syntax}
% define spaces:
\autoref{fig:exprs} defines the space of
simply-typed $\lambda$-calculus expressions, $\expr$.
%
For the remainder of this paper, metavariable $e$ ranges over
$\expr$, %
$c$ ranges over constants, %
$\oplus$ ranges over binary operators, $x$ and $f$ range over
program variables, and $C$ ranges over constructor symbols.
%
Each constructor symbol $C$ is mapped to a fixed cardinality, denoted
$|C| \in \nats$.

% define expressions:
An expression is a Boolean or integer constant, %
a variable (drawn from space of variables $\vars$), %
an application of an fixed operator to a pair of expressions, %
an application of a constructor to a sequence of expressions, %
a match expression, %
a $\lambda$-abstraction, %
an application, or %
a fixpoint expression.
%
For each $e \in \expr$, the \emph{free-variables} of $e$ are denoted
$\freevars(e)$.
%
A \emph{refinement-typed $\lambda$-calculus (RTLC)} \emph{program} is
a closed expression---i.e., an expression $e$ such that $\freevars(e)
= \emptyset$.
%
The space of RTLC programs is denoted $\rtlc$.
%
For $P \in \rtlc$, the set of all subexpressions of $P$ is denoted
$\subexps(P)$.

% define semantics:
\paragraph{Semantics}
%
\begin{figure*}
  \centering
  \begin{gather*}
  % evaluate const:
  \inference[ CONST ]{ }{ \Gamma \judges c \evalsto c }
  % evaluate variable:
  \inference[ VAR ]{ }{ \Gamma \judges x \evalsto \Gamma(x) }
  % evaluate binary operations:
  \inference[ OP ]{ %
    \Gamma \judges e_0 \evalsto v_0 & %
    \Gamma \judges e_1 \evalsto v_1 } %
  { \Gamma \judges e_0 \oplus e_1 \evalsto v_0 \oplus v_1 } \\ \\
  % evaluate constructor application:
  \inference[ CONS ]{ %
    \Gamma \judges e_0 \evalsto v_0 & \ldots & %
    \Gamma \judges e_k \evalsto v_k } %
  { \Gamma \judges C(e_0, \ldots, e_k) \evalsto C(v_0, \ldots, v_k) }
  % evaluate if-then-else by then branch
  \inference[ $\mathrm{MATCH}_{i}$ ]{ %
    \Gamma \judges e \evalsto C_i(v_0, \ldots, v_{ |C_i| }) & %
    X_i = x_1, \ldots, x_{|C_i|} \\ %
    \Gamma[ x_0 \mapsto v_0, \ldots, x_{|C_i|} \mapsto v_{|C_i|} ]
    \judges %
    e_i \evalsto v' } %
  { \Gamma \judges \cc{match}\ e\ \cc{with}\ %
    C_0(X_0)\ \cc{->}\ e_0 %
    \ldots %
    C_k(X_k)\ \cc{->}\ e_k \evalsto v' } \\ \\
  % evaluate an abstraction:
  \inference[ ABS ]{ } %
  { \Gamma \judges \lambda x.\ e \evalsto \lambda x.\ e }
  % apply a lambda:
  \inference[ APP-ABS ]{ %
    \Gamma \judges e_0 \evalsto \lambda x.\ e_0' & %
    \Gamma \judges e_1 \evalsto v_1 & %
    \upd{\Gamma}{x}{v_1} \judges e_0' \evalsto v }
  { \Gamma \judges e_0\ e_1 \evalsto v } \\ \\
  % evaluate a fix:
  \inference[ FIX ]{ } %
  { \Gamma \judges \fix{ f }{ e } \evalsto \fix{ f }{ e } }
  % apply a fixpoint:
  \inference[ APP-FIX ]{ %
    \Gamma \judges e_0 \evalsto \fix{ f }{ e_0' } & %
    \upd{ \Gamma }{ f }{ \fix{ f }{ e_0' } } \judges %
    e_0'\ e_1 \evalsto v }
  { \Gamma \judges e_0\ e_1 \evalsto v }
  \end{gather*}
  \caption{The natural semantics of $\rtlc$.}
  \label{fig:nat-sem}
\end{figure*}
%
A value is a Boolean, an integer, a $\lambda$ abstraction, a fixpoint
abstraction, or a constructor applied to a sequence of values;
%
the space of values is denoted $\values$.
%
The semantics of $\rtlc$ is defined by a natural semantics that
relates expressions to values, given in \autoref{fig:nat-sem}.
%
For $e \in \expr$ and $v \in \values$, the fact that $e$
\emph{evaluates} to $v$ is denoted $e \evalsto v$.

\paragraph{Typing}
%
Each expression has types that describe the spaces of values that it
takes as input and the spaces of values to which it evaluates.
%
The space of types, defined over a space of type variable $\typevars$
is given in \autoref{fig:types}.
%
A type is either a refined Boolean value, %
a refined Integer value, %
a type vector applied to a vector of value variables, %
%
\BH{does this also need to have a refinement?}
%
a function over types, %
%
or a recursive type, defined over a type parameter $X$ abstracted over
a sequence of value variables $\overline{x}$.
%
For each $\tau \in \types$, the free value variables and free type
variables of $\tau$, denoted $\freevaluevars(\tau) \subseteq \vars$
and $\freetypevars(\tau) \subseteq \typevars$, are the value variables
and type variables that occur in $\tau$ that are not bound in a
recursive type definition.
%
$\tau$ is \emph{well-formed} if $\freevars(\tau) = \emptyset$.
%
Note that $\tau$ may contain occurrences of free value variables.

% introduce has-type symbol:
For expression $e \in \expr$ and type $\tau \in \types$, the fact that
$e$ has type $\tau$ is denoted $e \hastype \tau$.
%
The definition of the has-type relation is standard.

% define base types:
A type $\tau$ in which the only formula is that occurs in a component
type of $\tau$ is $\true$ is a \emph{base} type, denoted
$\basetype(\tau)$.
%
Each expression that has a type has a unique base type, which can be
inferred automatically by a conventional type-inference algorithm,
such as Hindley-Milner algorithm.
%
\sys assumes access to an algorithm that, given $e \in \expr$,
computes $\basetype(\tau)$.
%
\BH{extend this to give base type of all free variables of an
  expression}

% problem statement
The problem that we address in this paper is, given program $P \in
\rtlc$ and well-formed $\tau \in \types$, to determine if $P \hastype
\tau$.

\paragraph{Symbolic encoding of semantics}
%
The semantics of $\rtlc$ is represented symbolically using \lia
formulas.
%
\BH{hoist defn of disjoint copies of variables for expressions}
%
In particular, for each $e \in \expr$ with subexpressions $e_0,
\ldots, e_n \in \expr$, there is some $\varphi \in \formulas{\vars[e],
  \vars[e_0], \ldots, \vars[e_n]}$ such that

\BH{merge updated defn from current draft of equivalence paper}

% define CHC's:
\subsection{Constrained Horn Clauses}
\label{sec:chcs}

\subsubsection{Structure}
% definition of CHC
A Constrained Horn Clause is a body, consisting of a conjunctive set
of uninterpreted relational predicates and a constraint, and a head
relational predicate.
%
Relational predicates are predicate symbols paired with a map from
each symbol to its arity.
%
\begin{defn}
  \label{defn:rel-preds}
  For each space of symbols $\mathcal{R}$ and function $a: \mathcal{R}
  \to \nats$, $(\mathcal{R}, a)$ is a space of \emph{relational
    predicates}.
\end{defn}
%
The space of all relational predicates is denoted $\relpreds$.
%
For each space of relational predicates $\mathcal{R} \in \relpreds$,
we denote the predicate symbols and arity of $\mathcal{R}$ as
$\relsof{\mathcal{R}}$ and $\arityof{\mathcal{R}}$, respectively.
%
For relational predicates $\mathcal{R} \in \relpreds$ and symbol $R$,
we denote $R \in \relsof{ \mathcal{R} }$ alternatively as $R \in
\mathcal{R}$.
%
All definitions introduced in this section are given over a fixed,
arbitrary set of relational-predicate symbols $\mathcal{R}$.

% define applications of relational predicates:
An application of a relational predicate is a relational-predicate
symbol $R$ paired with a sequence of variables of length equal to the
arity of $R$.
%
\begin{defn}
  \label{defn:pred-apps}
  For $R \in \mathcal{R}$ and sequence of variables $Y \in X^{*}$ such
  that $|Y| = \arityof{\mathcal{R}}(R)$, $(R, Y)$ is an
  \emph{application}.
\end{defn}
%
The space of applications is denoted $\apps{\mathcal{R}}$.
%
For each application $A \in \apps{\mathcal{R}}$, the predicate symbol
and argument sequence of $A$ are denoted $\relof{A}$ and $\argsof{A}$
respectively.

% define CHC:
A Constrained Horn Clause is a body of applications, a constraint, and
a head application.
%
\begin{defn}
  \label{defn:chcs-structure}
  For $\mathcal{A} \subseteq \apps{\mathcal{R}}$ and %
  $\varphi \in \formulas$, $\mathcal{B} = (\mathcal{A}, \varphi)$ is a
  \emph{clause body}.
  %
  For $H \in \apps{\mathcal{R}}$, $(\mathcal{B}, H)$ is a
  \emph{Constrained Horn Clause}.
\end{defn}
% define space of bodies:
The space of clause bodies is denoted $\bodies{ \mathcal{R} } =
\pset(\apps{\mathcal{R}}) \times \formulas$.
% define space, projection:
The space of Constrained Horn Clauses is denoted $\chc{\mathcal{R}} =
\bodies{ \mathcal{R} } \times \apps{\mathcal{R}}$.
%
For each clause $\mathcal{C} \in \chc{\mathcal{R}}$, the body and head
of $\mathcal{C}$ are denoted $\bodyof{ \mathcal{C} }$ and $\headof{
  \mathcal{C} }$, respectively.
% define siblings:
For each $\mathcal{S} \subseteq \chc{ \mathcal{R} }$, $\mathcal{C} \in
\mathcal{S}$, and all applications $A_0, A_1 \in \appsof{ \bodyof{
    \mathcal{C} } }$, $\relof{A_0}$ and $\relof{A_1}$ are
\emph{siblings} in $\mathcal{S}$.

% define system of CHC's
We will present \sys as a solver for CHCs without recursive
definitions of predicates, represented in a normalized form.
% give conditions on normalization:
Let $\mathcal{S} \subseteq \chc{ \mathcal{R} }$ be such that %
\textbf{(1)} each relational predicate occurs in the head of some
clause, %
\textbf{(2)} each $R \in \mathcal{R}$ is applied to the same variables
$\varsof{ \mathcal{S}}(R)$ in each of its occurrences in a clause as a
head, %
\textbf{(3)} all clauses with distinct head relational predicates are
defined over disjoint spaces of variables, %
\textbf{(4)} each relational predicate occurs in each clause body in
at most one application, and %
\textbf{(5)} there is exactly one relational predicate $\queryof{
  \mathcal{S} }$ that is the dependency of no relational predicate.
%
Then $\mathcal{S}$ is a \emph{normalized} recursion-free system.
%
For the remainder of this paper, we consider only normalized sets of
CHCs, and denote the space of such sets as $\chcs{\mathcal{R}}$.

\subsubsection{Solutions}
\label{sec:chc-solns}
%
A solution to a CHC system $\mathcal{S}$ is an interpretation of each
relational predicate $R$ of arity $n$ as a formula over $n$ free
variables such that for each clause $\mathcal{C} \in \mathcal{S}$, the
conjunction of interpretations of all relational predicates in the
body of $\mathcal{C}$ and the constraint of $\mathcal{C}$ entail the
interpretation of the head of $\mathcal{C}$.
%
Let a map from each $R \in \relsof{ \mathcal{R} }$ to a formula over
an ordered vector of $\arityof{ \mathcal{R} }(R)$ free variables be an
\emph{interpretation} of $\mathcal{R}$;
%
let the space of interpretations of $\mathcal{R}$ be denoted
$\interps{ \mathcal{R} }$.
%
\begin{defn}
  \label{defn:chc-soln}
  For $\mathcal{B} \in \bodies{ \mathcal{R} }$ and $H \in \apps{
    \mathcal{R} }$, %
  let $\sigma \in \interps{ \mathcal{R} }$ be such that for each $R
  \in \mathcal{R}$, $\arityof{\mathcal{R}}(R) = \degreeof{\sigma(R)}$
  and %
  %
  \[ \elts{ \sigma(\relof{ A })[ \argsof{ A } ] }_{ A \in \appsof{
      \mathcal{B} } }, %
  \ctrof{ \mathcal{B} } \entails %
  \sigma( \relof{H} )[ \argsof{ H } ]
  \]
  Then $\sigma$ is a \emph{solution} of $(\mathcal{B}, H)$.
\end{defn}
% define partial solution
For $\mathcal{S} \subseteq \chc{ \mathcal{R} }$, if
% solution respects dependence order:
\textbf{(a)} for each $R \in \domain(\sigma)$ and $R' \in \mathcal{R}$
a dependence of $R$ in $\mathcal{S}$, it holds that $R' \in
\domain(\sigma)$;
%
\textbf{(b)} for each $\mathcal{C} \in \mathcal{S}$ such that
$\relof{ \headof{ \mathcal{C} } } \in \domain(\sigma)$, $\sigma$ is a
solution of $\mathcal{C}$;
%
then $\sigma$ is a \emph{partial solution} of $\mathcal{S}$.
%
If, in addition, $\sigma(\queryof{ \mathcal{S} }) \entails \false$,
then $\sigma$ is a \emph{solution} of $\mathcal{S}$.
%

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "p"
%%% End:


\section{Algorithms}

As mentioned earlier in this paper we investigate stock picking, by investing all assets in the single stock which yields highest Sharpe ratio at time $t$. Our ensemble of algorithms should therefore predict the Sharpe ratio for individual stocks.

For a given set of data: $\rr^{(t)} = (r_1^{(t)}, r_2^{(t)}, \cdots, r_k^{(t)})$, predict the sharp ratio: $f(\rr^{(t)}) = \mathbf{sr}^{(t)} = (sr_1^{(t)}, sr_2^{(t)}, \cdots, sr_k^{(t)}$), choose weights such that only $max$ of the Sharpe ratio is 1:
$\w^{(t)} = (0, 0, \cdots,0, 1, 0, \cdots, 0)$ which can be described by equation \ref{eq:maxsharp}.

\begin{equation}\label{eq:maxsharp}
    w_i^{(t)} =
    \begin{cases}
    1, &\text{if } \max({\mathbf{sr}^{(t)}}) = sr_i^{(t)} \\
    0, &\text{else}
    \end{cases}
\end{equation}.

\subsection{LSTM}

An LSTM (Long Short Term Memory) model is a deep neural network model that has proved itself extremely effective at explaining time series. Before delving into the inner workings of an LSTM it is useful to first present some general concepts of deep learning. Deep learning is a term used for deep neural networks. A neural network is an algorithm that has shown itself capable of representing extremely flexible functional forms. The network consists of layers: \textit{input layer, hidden layers, output layer}. Each layer consists of a number of units. The input layer takes some data input. The hidden layer makes a non linear transformation of these inputs. There can be multiple hidden layers, such that a hidden layer takes the output of a previous hidden layer as input. Lastly an output layer maps the hidden layers to some target. These deep neural networks usually contains many thousands (and in some cases millions) of parameters, these being biases and weights of the inputs to different units in the layers. These parameters are tuned by using a gradient descent algorithm. This can be done because the network is differentiable, and so it's possible to establish a loss function between the predicted target and the real target, that can be differentiated with respect to the parameters.

A special case of deep neural networks are recurrent neural networks. These networks are made for time series problems, where the input of the network in period $t$, is the output of the network in period $t-1$. LSTM models are subset of recurrent neural networks. LSTM models utilizes leaky units which is a way to represent longterm dependencies:

\begin{displayquote}

Leaky units allow the network to \textit{accumulate} information (such as evidence for a particular feature or category) over a long duration. Once that information has been used, however, it might be useful for the neural network to forget the old state. For example, if a sequence is made of subsequences and we want a leaky unit to accumulate evidence inside each sub-subsequence, we need a mechanism to forget the old state by setting it to zero. Instead of manually deciding when to clear the state, we want the neural network to learn to decide when to do it. This is what gated RNNs do \cite{goodfellow_deep_2016}.
\end{displayquote}

LSTM models contains cells of four different units: \textit{state unit, input gate, forget gate, output gate}. The \textit{state unit} represents the state of the network (or some part of the network) at period $t$. This cell has an internal recurrence. The \textit{forget gate} controls how this state evolves. This is done by controlling how much of the input should be transferred (or rather forgot) to the next state. The \textit{input gate} controls how much information should be accumulated into the current state. The \textit{output gate} controls how much of the state should be transferred forward. Figure \ref{fig:lstmcell} in the appendix show a representation of the cell.

\subsubsection{Architecture of network}

In this paper I have chosen a fairly standard LSTM architecture of a single LSTM layer with 256 outputs, and an output layer with 11 outputs, which represents the Sharpe ratios of the individual stocks. The input layer takes the returns for the individual stocks. The LSTM contains $277259$ trainable parameters. I train the network on sequences of length $250$. That is I sample sequences of $250$ consecutive observations from the simulated data, which the network can be trained on. I use \textit{mean squared error} (mse)\footnote{Equation \ref{eq:meansquarederror} shows how mean squared error is calculated} as the loss metric, where I let the network warm up for the first 50 observations in each sequence. Warming up implies not calculating the loss for the first 50 observations until the network has had time to represent the state of the system.

\subsection{Rolling Sharpe (Naive)}

The LSTM is a very complex algorithm, which even though they have proven themselves extremely effective at predicting difficult time series patterns, they have a tendency to be hard to train. To accommodate this challenge i propose this simpler algorithm.

Essentially this works by calculating the rolling Sharpe ratio for period of time for each stock, and using this as the prediction of the sharpe ratio. So for a given stock $s_i$ in period $t$ calculate the $\E[R_{i}^{(t)}]$ and $\std({R_{i}^{(t)}})$. And calculate the sharpe ratio by $(\E[R_{i}^{(t)}] - \bar{r}) / \std({R_{i}^{(t)}})$. Note that $\E[R_{i}^{(t)}] - \bar{r} = \E[R_{i}^{(t)} -\bar{r}^{(t)}]$ because $\bar{r}$ is constant throughout the period. The expectation and standard deviation is calculated as presented in equation \ref{eq:rs_mean_naive} and equation \ref{eq:rs_std_naive}.

\begin{equation}\label{eq:rs_mean_naive}
    \E[R_{i}^{(t)}] = \frac{1}{h}\sum_{i=0}^{h} r_{i}^{(t-i)}
\end{equation}

\begin{equation}\label{eq:rs_std_naive}
    \std (R_{i}^{(t)})  = \sqrt{\frac{1}{h}\sum_{i=0}^{h} \lp r_{i}^{(t-i)} - \E[R_{i}^{(t)}] \rp^{2}}
\end{equation}

where $h$ denotes the window length / the length of the memory of the algorithm. This hyper parameter $h$ can be tuned to get the best possible prediction of the Sharpe ratio.

This is called naive because this algorithm does not take structural breaks into account. Or in other words, this algorithm cannot forget its previous state even if it would be advantageous.

\subsection{Rolling Sharpe}

An extension to the naive approach is proposed to address the issue of the algorithm not being able to forget the previous state of the system, given a structural break happens.

Now consider we calculate the rolling Sharpe ratio as done above, but with the caveat it is now done for two different time periods, long term rolling Sharpe ratio and a short term rolling Sharpe ratio. In each period $t$ the algorithm stores to vectors of Sharpe ratios:

\begin{align}
    \mathbf{sr}^{(t)}_{\text{short term}} &= \lp \tilde{sr}_1^{(t)}, \tilde{sr}_2^{(t)}, \cdots , \tilde{sr}_k^{(t)}\rp \\
    \mathbf{sr}^{(t)}_{\text{long term}} &= \lp sr_1^{(t)}, sr_2^{(t)}, \cdots , sr_k^{(t)}\rp
\end{align}

where $\tilde{sr}$ is used to denote a sharpe ratio from the short term memory. A metrix can now be used to establish if the vectors $ \mathbf{sr}^{(t)}_{\text{short term}}$,  $ \mathbf{sr}^{(t)}_{\text{long term}}$ deviates to much. If it is the case they deviate to much, the long term memory is cleared and replaced  with the short term memory. The metric for this deviation of long and short term memory is calculated by equation \ref{eq:rs_distance}

\begin{equation}\label{eq:rs_distance}
    \text{Distance}_{sr^{(t)}} = \frac{1}{k}\sum_{i=1}^{k} \lp sr^{(t)}_{i} - \tilde{sr}^{(t)}_{i} \rp^{2}
\end{equation}

This algorithm has three hyper parameters: $h_{\text{long term}}$, $h_{\text{short term}}$, and lastly $\tau$, which is the threshold for the $\text{Distance}_{sr^{(t)}}$ where the long term memory of the algorithm is cleared.

The sharpe ratio is predicted using the long term memory the same way it was done for the naive version.


\section{Introduction}
In this course, we will continue the analysis of groups from last year, to topics like simple groups, $p$-(sub)groups.
The main highlight in this part will be the Sylow's Theorems.\\
Second, we will look into another algebraic structure, rings, where you have two operations linked together by certain axioms (like you can add, substract and multiply).
Examples include $\mathbb Z$, $\mathbb C[x]$ and so on.
More important examples are ``rings of integers'' (rings behave like integers), like $\mathbb Z[i]$ or $\mathbb Z[\sqrt{2}]$.
The unique (or not unique) factorizations in these rings give rise to methods in number theories.
Another important thing is the ring of polynomials, which is the core object studies in Algebraic Geometry.\\
Fields, where divisions are possible for nonzero elements and multiplications commute, are also studied.
Examples are $\mathbb Q,\mathbb R,\mathbb C,\mathbb Z/p\mathbb Z$.\\
Lastly, we study modules, which are like vector spaces where one replace the underlying field by a ring.
In particular, every vector space is a module.
We will classify finitely generated modules over certain rings, which allows us to prove Jordan Normal Form for modules and to classify finite abelian groups.


\chapter{Introduction}

\begin{summary}
It is well known that the inverted Collatz sequence can be represented as a graph or a tree. Similarly, it is acknowledged that in order to prove the Collatz conjecture, one must demonstrate that this tree covers all odd natural numbers. A structured reachability analysis is hitherto unavailable. This paper investigates the problem from a graph theory perspective. We define a tree that consists of nodes labeled with Collatz sequence numbers. This tree will be transformed into a sub-tree that only contains odd labeled nodes. Furthermore, we derive and prove several formulas that can be used to traverse the graph. The analysis covers the Collatz problem both in it's original form $3x+1$ as well as in the generalized variant $kx+1$. Finally, we transform the Collatz graph into a binary tree, following the approach of Kleinnijenhuis, which could form the basis for a comprehensive proof of the conjecture.
\end{summary}

\section{Motivation}
The Collatz conjecture is a number theoretical problem, which has puzzled countless researchers using myriad approaches to solve the problem. Presently, there are scarcely any methodologies to describe and treat the problem from the perspective of the Algebraic Theory of Graphs. Such an approach is promising with respect to facilitating the comprehension of the Collatz sequence’s "mechanics."

The current gap in research forms the motivation behind the present contribution. The authors are convinced that exploring the Collatz conjecture in an algebraic manner, relying on the ﬁndings and fundamentals of Graph Theory, will contribute to a simplification of the problem.

Key results of this manuscript have been achieved using Data Science techniques. Our main tool was a Python-API, which implements the theorems that will be introduced later and is optimized for processing arbitrarily big integers \cite{Ref_Koch_Github}, see chapter {\color{wisogreen}"\nameref{ch:our_approach}"}.

\section{Related Research}
The following literature study is largely based on one given by a similar earlier essay \cite{Ref_Sultanow_Volkov_Cox_2017} which deals with the Collatz conjecture from the vantage point of automata theory.

The Collatz conjecture is one of the unsolved "Million Dollar Problems" \cite{Ref_Williams_2000}. When Lothar Collatz began his professorship in Hamburg in 1952, he mentioned this problem to his colleague Helmut Hasse. From 1976 to 1980, Collatz wrote several letters but missed referencing that he first proposed the problem in 1937. He introduced a function $g:\mathbb{N}\rightarrow\mathbb{N}$ as follows:
\begin{equation}
\label{eq:func_collatz}
g(x)=
\begin{cases}
3x+1	&	2\nmid x\\
x/2		&	\text{otherwise}
\end{cases}
\end{equation}

This function is surjective, but it is not injective (for example $g(3)=g(20))$ and thus is not reversible. The Collatz conjecture states that for each start number $x_1>0$ the sequence $x_1,x_2=g(x_1),x_3=g(x_2),\ldots$ will at some point enter the so called trivial cycle $(4,2,1)$. One example is the sequence $(17,52,26,13,40,20,10,5,16,8,4,2,1)$ starting at $x_1=17$. The assumption has not yet been proven. The conjecture would be falsified if the sequence either diverges indefinitely for a starting number $x_1$ or enters a cycle different from the trivial one (a so called non-trivial cycle). In order to specify compressed Collatz sequences containing only the odd members, Bruckman \cite{Ref_Bruckman_2008} for instance used the more convenient function that opts out all even integers:
\begin{equation}
\label{eq:func_collatz_odd}
f(x)=(3x+1)\cdot2^{-\alpha(x)},\text{where}\hspace{1em}2^{\alpha(x)}\mathrel\Vert(3x+1)
\end{equation}
Note that $\alpha(x)$ is the largest possible exponent for which $2^{\alpha(x)}$ exactly divides $3x+1$. Especially for prime powers, one often says $p^\alpha$ \textit{divides} the integer $x$ \textit{exactly}, denoted as $p^\alpha\mathrel\Vert x$, if $p^\alpha$ is the greatest power of the prime $p$ that divides $x$.

In his book “The Ultimate Challenge: The 3x+1 Problem” \cite{Ref_Lagarias_2010}, along with his annotated bibliographies \cite{Ref_Lagarias_2011}, \cite{Ref_Lagarias_2012} and other manuscripts like an earlier paper from 1985 \cite{Ref_Lagarias_1985}, Lagarias researched and put together different approaches from various authors intended to describe and solve the Collatz conjecture.

For the integers up to 2,367,363,789,863,971,985,761 the conjecture holds valid. For instance, see the computation history given by Kahermanes \cite{Ref_Kahermanes_2011} that provides a timeline of the results which have already been achieved.

\par\medskip
\textit{\textbf{Inverting the Collatz sequence and constructing a Collatz tree}} is an approach that has been carried out by many researchers. It is well known that inverse sequences \cite{Ref_Klisse_2010} arise from all functions $h\in H$, which can be composed of the two mappings $q,r:\mathbb{N}\rightarrow\mathbb{N}$ with $q:m\mapsto2m$ and $r:m\mapsto(m-1)/3$:
\begin{center}
$H=\{h:\mathbb{N}\rightarrow
\mathbb{N}\mid h=r^{(j)}\circ q^{(i)}\circ\ldots,i,j,h(1)\in\mathbb{N}\}$
\end{center}

\par\medskip
\textit{\textbf{An argumentation that the Collatz Conjecture cannot be formally proved}} can be found in the work of Craig Alan Feinstein \cite{Ref_Feinstein_2012}, who presents the position that any proof of the Collatz conjecture must have an infinite number of lines and thus no formal proof is possible. However, this statement will not be acknowledged in depth within this study.

\par\medskip
\textit{\textbf{Treating Collatz sequences in a binary system}} can be performed as well. For example, Ethan Akin \cite{Ref_Akin_2004} handles the Collatz sequence with natural numbers written in base 2 (using the Ring $\mathbb{Z}_2$ of two-adic integers), because divisions by 2 are easier to deal with in this method. He uses a shift map $\sigma$ on $\mathbb{Z}_2$ and a map $\tau$:

\begin{table}[H]
	\centering
	\parbox{.45\linewidth}{
		$\sigma(x)=
		\begin{cases}
		(x-1)/2		&	2\nmid x\\
		x/2			&	\text{otherwise}
		\end{cases}$
	}
	\parbox[][][b]{.45\linewidth}{
		$\tau(x)=
		\begin{cases}
		(3x+1)/2	&	2\nmid x\\
		x/2			&	\text{otherwise}
		\end{cases}$
	}
\end{table}

The shift map's fundamental property is $\sigma(x)_i=x_{i+1}$, noting that $\sigma(x)_i$ is the i-th digit of $\sigma(x)$. This property can easily be comprehended by an example $x=5=1010000\ldots=x_0x_1x_2\ldots$, containing $\sigma(x)=2=0100000\ldots$

Akin then defines a transformation $Q:\mathbb{Z}_2\rightarrow\mathbb{Z}_2$ by $Q(x)_i=\tau^i(x)_0$ for non-negative integers $i$ which means $Q(x)_i$ is zero if $\tau^i(x)$ is even and then it is one in any other instance. This transformation is a bijective map that defines a conjugacy between $\tau$ and $\sigma$: $Q\circ\tau=\sigma\circ Q$ and it is equivalent to the map denoted $Q_\infty$ by Lagarias \cite{Ref_Lagarias_1985} and it is the inverse of the map $\Phi$ introduced by Bernstein \cite{Ref_Bernstein_Lagarias_1996}. $Q$ can be described as follows: Let $x$ be a 2-adic integer. The transformation result $Q(x)$ is a 2-adic integer $y$, so that $y_n=\tau^{(n)}(x)_0$. This means, the first bit $y_0$ is the parity of $x=\tau^{(0)}(x)$, which is one, if $x$ is odd and otherwise zero. The next bit $y_1$ is the parity of $\tau^{(1)}(x)$, and the bit after next $y_2$ is parity of $\tau\circ\tau(x)$ and so on. The conjugancy $Q\circ\tau=\sigma\circ Q$ can be demonstrated by transforming the expression as follows: $(\sigma\circ Q(x))_i=Q(x)_{i+1}=\tau^{(i+1)}(x)_0=\tau^{(i)}(\tau(x))_0=Q(\tau(x))_i$

\par\medskip
\textit{\textbf{A simulation of the Collatz function by Turing machines}} has been presented by Michel \cite{Ref_Michel_2014}. He introduces Turing machines that simulate the iteration of the Collatz function, where he considers them having 3 states and 4 symbols. Michel examines both Turing machines, those that never halt and those that halt on the final loop.

\par\medskip
\textit{\textbf{A function-theoretic approach}} to this problem has been provided by Berg and Meinardus \cite{Ref_Berg_Meinardus_1994}, \cite{Ref_Berg_Meinardus_1995} as well as Gerhard Opfer \cite{Ref_Opfer_2011}, who consistently relies on the Berg’s and Meinardus’ idea. Opfer tries to prove the Collatz conjecture by determining the kernel intersection of two linear operators U, V that act on complex-valued functions. First he determined the kernel of V, and then he attempted to prove that its image by U is empty. Benne de Weger \cite{Ref_de_Weger_2011} contradicted Opfer’s attempted proof.

\par\medskip
\textit{\textbf{At the number of divisions by two}} Paul S. Bruckman \cite{Ref_Bruckman_2008} and Koch et al. \cite{Ref_Koch_2020} have taken a deeper look. Bruckmann has attempted to provide an elementary proof by contradiction. He repeatedly applies the Collatz function using a starting value $n_0$ and defines:
\[
\{e_k\}:n_1=(3n_0+1)\cdot2^{-e_1},n_2=(3n_1+1)\cdot2^{-e_2}=(3^2n_0+3+2^{e_1})\cdot2^{-(e_1+e_2)},\ldots
\]
Denoting the sum of exponents as $E_k=e_1+e_2+\ldots+e_k$ Bruckman obtains the following equation:
\[
2^{E_k}n_k=3^kn_0+\sum_{j=0}^{k-1}3^{k-1-j}2^{E_j}
\]

\par\medskip
\textit{\textbf{Reachability Considerations}} based on a Collatz tree exist as well. It is well known that the inverted Collatz sequence can be represented as a graph; to be more specific, they can be depicted as a tree \cite{Ref_Andrei_Masalagiu}, \cite{Ref_Kak_2014}. It is acknowledged that in order to prove the Collatz conjecture, one needs to demonstrate that this tree covers all odd natural numbers.

\par\medskip
\textit{\textbf{The Stopping Time}} theory has been introduced by Terras \cite{Ref_Terras_1976}, it has been taken up and continued, inter alia, by Silva \cite{Ref_Silva_1999} and Idowu \cite{Ref_Idowu_2015}. Terras introduces another notation of the Collatz function $T(n)=(3^{X(n)}n+X(n))/2$, where $X(n)=1$ when $n$ is odd and $X(n)=0$ when $n$ is even, and defined the stopping time of $n$, denoted by $\chi(n)$, as the least positive $k$ for which $T^{(k)}(n)<n$, if it exists, or otherwise it reaches infinity. Let $L_i$ be a set of natural numbers, it is observable that the stopping time exhibits the regularity $\chi(n)=i$ for all $n$ fulfilling $n\equiv l(mod 2^i)$, $l\in L_i$, $L_1=\{4\}$, $L_2=\{5\}$, $L_4=\{3\}$, $L_5=\{11,23\}$, $L_7=\{7,15,59\}$ and so on. As $i$ increases, the sets $L_i$, including their elements, become significantly larger. Sets $L_i$ are empty when $i\equiv l(mod 19)$ for $l=3,6,9,11,14,17,19$. Additionally, the largest element of a non-empty set $L_i$ is always less than $2^i$.

\par\medskip
\textit{\textbf{Dynamical systems}} provide a wide basis for examining the Collatz sequence as well \cite{Ref_Wirsching_1998}. A dynamical system \cite[p.~464]{Ref_Walz_2017} is a triple $(M,G,\Phi)$ for a set $M$, a group $(G,+)$ and a map $\Phi:M\times G\to M$ for which $\Phi(\cdot,0)=id_M(\cdot)$ firstly applies and secondly $\Phi\left(\Phi(m,s),t\right)=\Phi(m,s+t)$ for all $m\in M$, $s,t\in G$. The set $M$ is called phase space. Terence Tao \cite{Ref_Tao_2019} considers orbits of the dynamical system generated by the Collatz map (an orbit, also called trajectory, is a subset of the phase space). For an integer function $f:\mathbb{Z}\rightarrow\mathbb{Z}$, we denote by $f^i=f\circ f^{i-1}$ the $i$-fold iterate of $f$ with the convention $f^0=id_\mathbb{Z}$. If $n\in\mathbb{Z}$, the orbit (trajectory) of $n$ under $f$ is the sequence $T_f(n)=\left(n,f(n),f\circ f(n),f\circ f\circ f(n),\ldots\right)$, see \cite[p.~10]{Ref_Wirsching_1998}. Tao proved that nearly all of these orbits attain almost bounded values. To achieve this, he advanced the results of Allouche \cite{Ref_Allouche_1978} and Korec \cite{Ref_Korec_1994}. Their main idea was to prove that the set of positive integers with finite stopping time has a density one, in this case the term density refers to the concept of \textit{natural density} (also known as \textit{asymptotic density}). It measures how large a subset of the set of natural numbers is. The natural density of a set $M\subseteq\mathbb{N}$ is defined as:
\[
\lim_{n\to\infty}\frac{\#\{m\in M:m<n\}}{n}
\]

In this context, the authors used the Collatz map as the map $\Phi$. They proved that the set $\{x\in\mathbb{N}:(\exists t\in\mathbb{N})(\Phi(x,t)<x)\}$ has a natural density one.

\par\medskip
\textit{\textbf{Many other approaches}} exist as well. From an algebraic perspective, Trümper \cite{Ref_Truemper_2014} analyzes the Collatz problem in the light of an Infinite Free Semigroup. Kohl \cite{Ref_Kohl_2008} generalized the problem by introducing residue class-wise affine mappings, in short, rcwa mappings. A polynomial analogue of the Collatz Conjecture has been provided by Hicks et al. \cite{Ref_Hicks_Mullen_Yucas_Zavislak_2008} \cite{Ref_Snapp_Tracy_2008} and there are also stochastical, statistical and Markov chain-based and permutation-based approaches to proving this elusive theory.


Simulation and Code has attached to Homework(Q3\_b.m). For this $\beta$ we can find out that for 100 second all $J$ costs are zero and very near to zero so we neglect time after that.
\begin{figure}[H]
	\caption{System simulation for $\beta = 1000$}
	\centering
	\includegraphics[width=12cm]{../Code/Q3/figures/FinalBeta.png}
\end{figure}
\newpage
\subsubsection{I}
$u(t)$ Cost:
$$J_u = \int_{0}^{\infty} u(t)^2d(t)$$
\begin{figure}[H]
	\caption{$u(t)$ cost in different $\beta$}
	\centering
	\includegraphics[width=12cm]{../Code/Q3/figures/uCost.png}
\end{figure}
\newpage
\subsubsection{II}
$x_1^2 + \dot x_2^2$ Cost:
$$J_u = \int_{0}^{\infty} (x^2 + \dot{x}^2)d(t)$$
\begin{figure}[H]
	\caption{$x^2 + \dot{x}^2$ cost in different $\beta$}
	\centering
	\includegraphics[width=12cm]{../Code/Q3/figures/xCost.png}
\end{figure}


\documentclass{article}
\usepackage{nicematrix}
\usepackage{fullpage}

\begin{document}

\section{Mathematical model}

Let $R=\{T_0, T_1, \ldots, T_n, T_{n + 1}\}$ be the fixed order of
tasks within the route for a given vehicle. The vehicle has to perform
$n$ geolocated tasks ($T_1$ to $T_n$) while $T_0$ and $T_{n + 1}$ are
its optional start and end tasks. For all $i$, we denote $A_i$ the
action time\footnote{Action time for a task is a combination of
  service time and setup time, the latter only applied to a non-break
  task if its location is different than the location visited in the
  previous non-break task.} for task $i$, $d_i$ the travel duration
from task $T_i$ to the next non-break task. Task $T_i$
($1\leq i \leq n$) has $K_i$ time windows $[a_{i1}, b_{i1}]$, $\dots$,
$[a_{iK_i}, b_{iK_i}]$ for expected task start. In particular for
vehicle start we have $s_0 = 0$
while working hours associated to the vehicle are $[a, b]$.\\

The following program aim at deciding start times $t_i$ for all tasks
$T_i$ in order to minimize timing violations measured by variable
$Y_i$: either \textit{lead time} whenever a task starts earlier than
it's available date, either \textit{delay} when a task starts later
that it's deadline. For each task $T_i$ ($1\leq i\leq n$), the chosen
time window is decided by binary variables $X_{ik}$
($0\leq k \leq K_i$). Variables $\delta_i$ allow to split required
travel duration before and after a (set of) breaks. Let $J$ be the
subset of indices $i$ ($0\leq i \leq n$) such that $T_i$ is not a
break. For all $i\in J$, let $B_i$ be the number of tasks following
task $T_i$ that are breaks.

\begin{quote}
  Minimize
  \begin{align}
    M_1 \times \sum_{i = 0}^{n + 1} Y_i + M_2 \times (t_{n +1} - t_0) + M_3\times\sum_{i = 1}^{n}t_i + \sum_{i\in J}\sum_{k = i}^{i + B_i}(k - i)\times\delta_k\label{obj}\tag{Obj}
  \end{align}
  Subject to
  \begin{align}
    t_{i + 1} - t_i - \delta_i &\geq A_i && 0 \leq i \leq n\label{precedence}\tag{$P_i$}\\
    Y_0 + t_0 &\geq a &&  \label{lead_start}\tag{$L_0$}\\
    Y_i + t_i - \sum_{k = 0}^{K_i}a_{ik}X_{ik}&\geq 0 && 1 \leq i \leq n \label{lead}\tag{$L_i$}\\
    t_i - Y_i - \sum_{k = 0}^{K_i}b_{ik}X_{ik} &\leq 0 && 1 \leq i \leq n \label{delay}\tag{$D_i$}\\
    t_{n + 1} - Y_{n + 1} &\leq b && \label{delay_end}\tag{$D_{n + 1}$}\\
    \sum_{k = 0}^{K_i}X_{ik} &= 1 && 1 \leq i \leq n \label{tw_sum}\tag{$A_i$}\\
    \sum_{k = i}^{i + B_i}\delta_k &= d_i && \forall i\in J \label{break_duration}\tag{$\Delta_i$}\\
    t_i &\geq 0 && 0 \leq i \leq n + 1 \nonumber\\
    Y_i &\geq 0 && 0 \leq i \leq n + 1 \nonumber\\
    \delta_i &\geq 0 && 0 \leq i \leq n \nonumber\\
    X_i &\in \{0; 1\} && 1 \leq i \leq n \nonumber
  \end{align}
\end{quote}

The optimization objective (\ref{obj}) uses values
$M_1 >> M_2 >> M_3 >> 1$ to combine the sum of time violations as
first objective, then route makespan as a secondary objective, then
tasks start date, and last force split travel time to be done as soon
as possible (required for situations with breaks surrounded by waiting
time). Precedence constraints (\ref{precedence}) make sure service and
travel times are accounted for between task start times. In the time
violation constraints for lead time (\ref{lead}) and delay
(\ref{delay}), variable $Y_i$ is zero iff task start matches the
expected time window. Sums (\ref{tw_sum}) ensure exactly one time
window is picked for each task. Sums (\ref{break_duration}) ensure the
required travel duration between any two consecutive non-break tasks
is split across all intermediate breaks. In particular if $T_i$ and
$T_{i + 1}$ are both non-break tasks, we have $\delta_i = d_i$. In
practice, variables $t_i$ can be bounded by additional user-defined
hard constraints.

\section{glpk matrix layout}

\setcounter{MaxMatrixCols}{30}

\begin{center}
  \rotatebox[origin=c]{90}{
    $\begin{NiceMatrix}
      & t_0 & t_1 & \Cdots & t_n & t_{n + 1} & Y_0 & Y_1 & \Cdots & Y_n & Y_{n + 1} & X_{10} & \Cdots & X_{1K_1} & \Cdots & X_{n0} & \Cdots & X_{nK_n} & \delta_0 & & \Cdots & \delta_n\\
      P_0 & -1 & 1 & & & & & & & & & & & & & & & & -1 \\
      \Vdots & & \Ddots & \Ddots & & & & & & & & & & & & & & & & \Ddots \\
      \Vdots & & &  \Ddots & \Ddots & & & & & & & & & & & & & & & & \Ddots \\
      P_n & & & & -1 & 1 & & & & & & & & & & & & & & & & -1 \\\hline
      L_0 & 1 & & & & & 1 \\
      L_1 & & 1 & & & & & 1 & & & & -a_{10} & \Cdots & -a_{1K_1} \\
      \Vdots & & & \Ddots & & & & & \Ddots & & & & & & \Ddots \\
      L_n & & & & 1 & & & & & 1 & & & & & & -a_{n0} & \Cdots & -a_{nK_n} \\\hline
      D_1 & & 1 & & & & & -1  & & & & -b_{10} & \Cdots & -b_{1K_1} \\
      \Vdots & & & \Ddots & & & & & \Ddots & & & & & & \Ddots \\
      D_n & & & & 1 & & & & & -1 & & & & & & -b_{n0} & \Cdots & -b_{nK_n} \\
      D_{n + 1} & & & & & 1 & & & & & -1\\\hline
      S_1 & & & & & & &  & & & & 1 & \Cdots & 1 \\
      \Vdots & & & & & & & & & & & & & & \Ddots \\
      S_n & & & & & & & & & & & & & & & 1 & \Cdots & 1 \\\hline
      \Delta_0 & & & & & & & & & & & & & & & & & & 1\cdots \\
      \Vdots & & & & & & & & & & & & & & & & & & & \Ddots \\
      \Vdots & & & & & & & & & & & & & & & & & & & & \Ddots \\
      \Delta_{max(J)} & & & & & & & & & & & & & & & & & & & & & \cdots 1 \\\hline
      Makespan & -1 & & & & 1\\\hline
      \sum_{i = 0}^{n + 1} Y_i & & & & & & 1 & \Cdots & \Cdots & \Cdots & 1 \\
    \end{NiceMatrix}$
  }
\end{center}

\end{document}


\documentclass[letterpaper]{article}

\usepackage{fullpage}
\usepackage{nopageno}
\usepackage{amsmath}
\usepackage{amssymb}
\allowdisplaybreaks

\newcommand{\abs}[1]{\left\lvert #1 \right\rvert}

\begin{document}
\title{Notes}
\date{September 26, 2014}
\maketitle
\section*{assignment}
Read Section 3.2 (up to Theorem 3.2.10)
Section 3.2: \# 14, 16, 18, 19, 21.

\section*{last time}
group $G$ and subgroup $H$ where $x\sim y\Leftrightarrow xy^{-1}\in H$, for $a\in G$ the
\begin{align*}
  [a]&=\{x\in G|x\sim a\}\\
  &=\{x\in G|xa^{-1}\in H\}\\
  &=\{x\in G|x\in Ha\}\\
  &=Ha
\end{align*}
Note that $[e]=H$.

\subsubsection*{claim}
there is a one to one correspondence $H\xrightarrow{\varphi}Ha$ and $h\to ha$.
\section*{lagrange's theorem}
let $G$ be a finite group and $H$ a subgroup of $G$. Then $|H|$ is a divisor of $|G|$.

\subsection*{proof}
consider on $G$ the previous equivalence relation. recall that equivalence classes partition $G$.

let $t$ be the number of distinct equivalence classes. note that we also proved that any two equivalence classes have the same cardinality. then $t\cdot |H|=|G|$. so $|H|$ divides $|G|$

\subsection*{corollary}
let $G$ be finite and $a\in G$ then the order of $a$ divides $|G|$. Take the smallest subgroup that contains $a$: $<a>=\{e,a,a^2,\dots,a^{ord(a)-1}\}$. Then $|H|=ord(a)$ and divides $|G|$.

\subsection*{corollary}
say that $G$ is finite, and the number of elements in $G$ is prime, then $G$ is cyclic.

choose $a\in G, a\ne e$, then $|<a>|p$ and so $|<a>|=p$. Because $<a>\subseteq G$ and so $G=<a>$.

\section*{example}
$\mathbb{Z}_n^*=\{[a]|(a,n)=1\}$ and $|\mathbb{Z}_n^*|=\varphi(n)$

take $\mathbb{Z}_8^*=\{[1],[3],[5],[7]\}=\{e,a,b,ab\}=\{a^ib^j|a^2=b^2=e,ab=ba\}$. Note that $x^2=[1]$ for all $x\in \mathbb{Z}_8^*$. so then every element has order 2 except identity which always has order 1.

also note that this is not cyclic as there is no element of order $4$.

\section*{example}
$S_3=\{(1),(12),(23),(31),(123),(132)\}$

let $(12)=a, (123)=b$ then $(123)^2=(132), ab=(123)(12)=(13), a^2b=(23)$ and $S_3=\{e,a,a^2,ab,a^2b,b\}$

$S_3=\{a^ib^j|a^3=e,b^2=e,ba=a^2b\}$

\section*{proposition}
let $G$ be a group and $\{H_i\}_{i\in I}$ be a family of subgroups of $G$ then $\cap_{i\in I}H_i$ is a subgroup

\subsection*{proof}
exercise, write it down.

unions are different, take $S_3, H=\{(1),(12)\}, K=\{(1),(123),(132)\}$ then the union is not a subgroup. Note that $|H\cup G|=4$ and $6=|G|$ but $4|6$ is not true
\end{document}


\documentclass{article}
\usepackage{amsmath,amssymb,amsfonts,amsthm}
\usepackage{bm}
\usepackage{color}
\numberwithin{equation}{section}
\let\vec\bm
\newcommand{\qev}[1]{\langle #1 \rangle} % Quantum mechanical average.
\newcommand{\eav}[1]{\{ #1 \}}           % Ensemble average.
\DeclareMathOperator{\Tr}{Tr}
\theoremstyle{plain}
\newtheorem{thm}{Theorem}
\numberwithin{thm}{section}

\theoremstyle{plain}
\newtheorem{prop}{Proposition}
\numberwithin{prop}{section}

\theoremstyle{definition}
\newtheorem{defn}{Definition}
\numberwithin{defn}{section}

\theoremstyle{remark}
\newtheorem*{rem}{Remark}

\newtheorem*{cor}{Corollary}
\title{Entropy and Information}
\author{Amey Joshi}
\begin{document}
\maketitle
\abstract{This article reviews the various notions of entropy prevalent in
physics and information theory.}
\section{Mathematical preliminaries}\label{s1}
\begin{defn}\label{s1d1}
The sample space $\Omega$ is the set of all outcomes of an experiment.
\end{defn}
\begin{defn}\label{s1d2}
A $\sigma$-algebra $\mathcal{F}$ over $\Omega$ is the family of sets with the
following properties
\begin{enumerate}
\item $\Omega \in \mathcal{F}$.
\item If $A \in \mathcal{F}$ then so does $A^c$.
\item If $\{A_n\}$ is a sequence of elements of $\mathcal{F}$ then
\[
\bigcup_{n \ge 1} A_n \in \mathcal{F}.
\]
\end{enumerate}
\end{defn}
\begin{defn}\label{s1d3}
Members of $\mathcal{F}$ are called measurable sets.
\end{defn}
\begin{defn}\label{s1d4}
The pair $\{\Omega, \mathcal{F}\}$ is called a measurable space.
\end{defn}
\begin{defn}\label{s1d5}
A measure on the measurable space $\{\Omega, \mathcal{F}\}$ is a mapping
$m: \mathcal{F} \mapsto \mathbf{R}$ with the properties
\begin{enumerate}
\item $m(\varnothing) = 0$.
\item If $A_1, A_2, \ldots$ are disjoint members of $\mathcal{F}$ then
\[
m\left(\bigcup_{n \ge 1} A_n \right) = \sum_{n \ge 1}m(A_n).
\]
\end{enumerate}
\end{defn}
\begin{defn}\label{s1d6}
The triple $\{\Omega, \mathcal{F}, m\}$ is called a measure space.
\end{defn}
\begin{defn}\label{s1d7}
A probability measure $P$ on a measurable space $\{\Omega, \mathcal{F}\}$ is
is a measure with the additional property that $P(\Omega) = 1$.
\end{defn}
\begin{defn}\label{s1d8}
The triple $\{\Omega, \mathcal{F}, P\}$ is called a probability space.
\end{defn}
\begin{defn}\label{s1d9}
Let $\{\Omega,\mathcal{F}\}$ be a measurable set. A function $f:\Omega \mapsto
\mathbf{R}$ is measurable if the set $\{\omega \in \Omega : f(\omega) \le a\}$
is measurable for all $a \in \mathbf{R}$.
\end{defn}
\begin{defn}\label{s1d10}
A random variable is a measurable function on a probability space.
\end{defn}
The probability that a random variable $X$ on a probability space $\{\Omega,
\mathcal{F}, P\}$ takes values lesser than a number $a$ is the probability of
the set $\{\omega \in \Omega: X(\omega) \le a\}$.

\begin{defn}\label{s1d11}
The function $F_X: \mathbf{R} \mapsto [0, 1]$ defined by
\[
F_X(x) = P(X \le x)
\]
is called the distribution function of the random variable $X$.
\end{defn}

\begin{defn}\label{s1d12}
A random variable whose range is a countable set is called a discrete
random variable. Otherwise, it is called a continuous random variable.
\end{defn}

If $X$ is discrete random variable then one can describe it by specifying the
probablity that $X$ takes a particular value $x$. The function
\begin{equation}\label{s1e1}
p_X(x) = P(X = x)
\end{equation}
is called the \emph{probability mass function} of the discrete random variable
$X$.

If $X$ is a continuous random variable then it can be described by a \emph{
probability density function}
\begin{equation}\label{s1e2}
\int_a^b p_X(x)dx = P(X \in [a, b]).
\end{equation}

\section{Information}\label{s2}
Information revealed by an event is measured by the `degree of surprise' one
experiences after getting to know that it has occured. This anthropomorphic
definition can be cast into mathematical terms by noticing that the `degree of
surprise' associated with an event is inversely proportionate to the
probability of the event. We also demand that the information content of two
independent events is additive. A function that satisfies these criteria is
\cite[Lemma in section 1]{renyi1961measures}
\begin{equation}\label{s2e1}
h(x) = -\log p(x),
\end{equation}
where $p$ is the probability mass function of the random variable $X$. If $X$
takes values $x_1, x_2, \ldots,$ then the expectation value of $h$ is
\begin{equation}\label{s2e2}
H = \sum_{i \ge 1}p(x_i)h(x_i) = -\sum_{i \ge 1}p(x_i)\log p(x_i).
\end{equation}
$H$ is called the entropy \cite[section 6 and appendix II]{shannon1948} of the
random variable $X$. It is measured in bits if
the base of the logarithm is $2$, nats if the base is $e$ and hartleys if the
base is $10$. If $X$ is a continuous random variable with density $p$ then
its equation \eqref{s2e1} remains unchanged and \eqref{s2e2} is written as
\begin{equation}\label{s2e3}
H = -\int p(x)\log p(x)dx.
\end{equation}
In this case $H$ is called the \emph{differential entropy} of the continuous
random variable $X$.

We will compute the entropy for a few random variables.
\begin{enumerate}
\item Bernoulli random variable $X$ that takes a value $1$ with probability $p$
and $0$ with probability $1 - p$.
\begin{equation}\label{s2e4}
H = -p(0)\log p(0) - p(1)\log p(1) = -(1 - p)\log(1 - p) - p\log p.
\end{equation}
It is quite common to write $1 - p$ as $q$ so that
\begin{equation}\label{s2e5}
H = -q\log q - p\log p.
\end{equation}
\item If $X$ is a random variable representing the outcome of a fair die then
its entropy is
\begin{equation}\label{s2e6}
H = -\sum_{i=1}^6 p(X=i)\log p(X=i) = -\log\frac{1}{6} = \log 6.
\end{equation}
\item If $X$ is a binomial random variable with parameters $n$ and $p$ then
\begin{equation}\label{s2e7}
H = -\sum_{i=0}^n p(X=i)\log p(X=i),
\end{equation}
with
\begin{equation}\label{s2e8}
p(X=i) = \binom{n}{i}p^i(1 - p)^{n-i}
\end{equation}
This sum cannot be expressed in a close form. However, if $n$ is large then
one can use de Moivre-Laplace theorem to approximate
\begin{equation}\label{s2e9}
\binom{n}{i}p^i(1-p)^{n-i} = \frac{1}{\sigma\sqrt{2\pi}}
\exp\left(-\frac{1}{2}\left(\frac{i - \mu}{\sigma}\right)^2\right),
\end{equation}
where
\begin{eqnarray}
\mu &=& np \label{s2e10} \\
\sigma^2 &=& np(1-p) \label{s2e11}
\end{eqnarray}
The sum in equation \eqref{s2e7} can be approximated as the integral
\begin{equation}\label{s2e12}
-\frac{1}{\sigma\sqrt{2\pi}}\int_0^\infty
\exp\left(-\frac{1}{2}\left(\frac{x - \mu}{\sigma}\right)^2\right)
\left(-\log (\sigma\sqrt{2\pi}) -
\frac{1}{2}\left(\frac{x - \mu}{\sigma}\right)^2\right)dx.
\end{equation}
Since the integrand is even, we can write it as
\begin{equation}\label{s2e13}
H \approx \frac{1}{2\sigma\sqrt{2\pi}}\int_{-\infty}^\infty
\exp\left(-\frac{1}{2}\left(\frac{x - \mu}{\sigma}\right)^2\right)
\left(\log (\sigma\sqrt{2\pi}) +
\frac{1}{2}\left(\frac{x - \mu}{\sigma}\right)^2\right)dx.
\end{equation}
If $y = (x - \mu)/\sigma$ then
\begin{equation}\label{s2e14}
H \approx \frac{1}{2\sqrt{2\pi}}\int_{-\infty}^\infty e^{-y^2/2}
\left(\log(\sigma\sqrt{2\pi}) + \frac{y^2}{2}\right)dy.
\end{equation}
We now use the standard Gaussian integrals
\begin{eqnarray}
\int_{-\infty}^\infty e^{-ax^2}dx &=& \sqrt{\frac{\pi}{a}} \label{s2e15} \\
\int_{-\infty}^\infty x^2e^{-ax^2}dx &=& \frac{1}{2}\sqrt{\frac{\pi}{a^3}}
\label{s2e16}
\end{eqnarray}
to get
\begin{equation}\label{s2e17}
H \approx \frac{\log(\sigma\sqrt{2\pi})}{2} + \frac{1}{4} =
\frac{1 + \log (2\pi np(1-p))}{4}.
\end{equation}
\item If $X \sim \mathcal{N}(\mu,\sigma)$ then
\begin{equation}\label{s2e18}
p(x) = \frac{1}{\sigma\sqrt{2\pi}}
\exp\left(-\frac{1}{2}\left(\frac{x - \mu}{\sigma}\right)^2\right).
\end{equation}
The differential entropy is
\begin{eqnarray*}
H &=& -\int_{-\infty}^\infty p(x)\log p(x)dx \\
 &=& \frac{\log(\sigma\sqrt{2\pi})}{\sigma\sqrt{2\pi}}\int_{-\infty}^\infty
\exp\left(-\frac{1}{2}\left(\frac{x - \mu}{\sigma}\right)^2\right)dx + \\
 & & \frac{1}{2\sigma\sqrt{2\pi}}
 \int_{-\infty}^\infty\left(\frac{x - \mu}{\sigma}\right)^2
\exp\left(-\frac{1}{2}\left(\frac{x - \mu}{\sigma}\right)^2\right)dx \\
 &=& \log(\sigma\sqrt{2\pi}) + \frac{1}{2}.
\end{eqnarray*}
Simplifying further, we get
\begin{equation}\label{s2e19}
H = \frac{1 + \log(2\pi\sigma^2)}{2}.
\end{equation}
\end{enumerate}

\subsection{Relation with Boltzmann's definition}
Boltzmann defined entropy of a system as
\cite[equation (6), chapter 1]{pathria32statistical}
\begin{equation}\label{s2e20}
S = k\log W,
\end{equation}
where the constant $k = 1.38 \times 10^{-16}$ erg/K is now called Boltzmann's
constant and $W$ is the number of microstates corresponding to a given
macrostate. Consider a system of $N$ particles such that $n_i$ of them are
in state $i = 1, \ldots, m$. There are $N!$ permutations of $N$ distinguishable
objects. Since the rearrangements within a state do not matter,
\begin{equation}\label{s2e21}
W = \frac{N!}{\prod_{i=1}^m n_i}.
\end{equation}
Now,
\begin{eqnarray*}
\log W &=& \log N! - \sum_{i=1}^m \log n_i! \\
 &=& N\log N - N - \sum_{i=1}^n n_i\log n_i + \sum_{i=1}^m n_i \\
 &=& -\sum_{i=1}^n \frac{n_i}{N}\log\frac{n_i}{N}.
\end{eqnarray*}
where we have used Stirling's approximation $\log N! \approx N\log N - N$ and
the fact that
\[
\sum_{i=1}^m n_i = N.
\]
If we define
\begin{equation}\label{s2e22}
p_i = \frac{n_i}{N},
\end{equation}
as the probability of an object to be in state $i$ then
\begin{equation}\label{s2e23}
\log W = -\sum_{i=1}^m p_i \log p_i.
\end{equation}
This equation coincides with equation \eqref{s2e1} of the information theoretic
entropy. Thus, the relation between Boltzmann's thermodynamic entropy and
information theoretic entropy is
\begin{equation}\label{s2e24}
S = kH.
\end{equation}

\bibliographystyle{plain}
\bibliography{re}
\end{document}


\chapter{Parameter Estimation I: Bayes' Box}
One of the most important times to use Bayes' rule is when you want to do
{\it parameter estimation}. Parameter estimation is a fairly common
situation in statistics. In fact, it is possible to interpret almost any
problem in statistics as a parameter estimation problem and approach it
in this way!

Firstly, what is a parameter? One way to think of a parameter is that
it is just a fancy term for a quantity or a number that is
unknown\footnote{Another use for the term parameter
is any quantity that something else depends on. For example, a normal distribution
has a mean $\mu$ and a standard deviation $\sigma$ that defines which normal
distribution we are talking about. $\mu$ and $\sigma$ are then said to be
parameters of the normal distribution.}.
For example, how many people are currently in New
Zealand? Well, a Google search suggests 4.405 million. But that does not mean
there are {\bf exactly} 4,405,000 people. It could be a bit more or a bit
less. Maybe it is 4,405,323, or maybe it is 4,403,886. We don't really know.
We could call the true number of people in New Zealand right now $\theta$, or
we could use some other letter or symbol if we want. When talking about
parameter estimation in general we often call the unknown parameter(s)
$\theta$, but in specific applications we will call the parameter(s) something
else more appropriate for that application.

The key is to realise that we can use the Bayes' Box, like in previous chapters.
But now, our list of possible hypotheses is a list of possible values for
the unknown parameter. For example, a Bayes' Box for the precise number of
people in New Zealand might look something like the one in
Table~\ref{tab:nz_pop}.

\begin{table}[!ht]
\begin{center}
\begin{tabular}{|c|c|c|c|c|}
\hline
{\bf Possible Hypotheses} & {\tt prior} & {\tt likelihood} &
{\tt prior $\times$ likelihood} & {\tt posterior}\\
\hline
\ldots & \ldots & \ldots & \ldots & \ldots\\
$\theta = 4404999$ & 0.000001 & \ldots   & \ldots  & \ldots\\
$\theta = 4405000$ & 0.000001 & \ldots   & \ldots  & \ldots\\
$\theta = 4405001$ & 0.000001 & \ldots   & \ldots  & \ldots\\
$\theta = 4405002$ & 0.000001 & \ldots   & \ldots  & \ldots\\
$\theta = 4405003$ & 0.000001 & \ldots   & \ldots  & \ldots\\
$\theta = 4405004$ & 0.000001 & \ldots   & \ldots  & \ldots\\
$\theta = 4405005$ & 0.000001 & \ldots   & \ldots  & \ldots\\
$\theta = 4405006$ & 0.000001 & \ldots   & \ldots  & \ldots\\
\ldots & \ldots & \ldots & \ldots & \ldots\\
\hline
Totals: & 1 & & \ldots & 1\\
\hline
\end{tabular}
\caption{\it An example of how a Bayes' Box may be used in a parameter
estimation situation.\label{tab:nz_pop}}
\end{center}
\end{table}

There are a few things to note about this Bayes' box. Firstly, it is big, which
is why I just put a bunch of ``\ldots''s in there instead of making up numbers.
There are lots of possible hypotheses, each one corresponding to a possible value
for
$\theta$. The prior probabilities I have put in the second column were for
illustrative purposes. They needn't necessarily all be equal (although that is
often a convenient assumption). All the
stuff we've seen in smaller examples of Bayes' rule and/or use of a Bayes' Box
still applies here. The likelihoods will
still be calculated by seeing how the probability of the data depends on the
value of the unknown parameter. You still go through all the same steps,
multiplying prior times likelihood and then normalising that to get the
posterior probabilities for all of the possibilities listed. Note that a set
of possible values together with the probabilities is what is commonly termed
a {\it probability distribution}. In basic Bayesian problems, like in the
introductory chapters, we start with some prior probabilities and update them
to get posterior probabilities. In parameter estimation, we start with a prior
{\it distribution} for the unknown parameter(s) and update that to get a
posterior {\it distribution} for the unknown parameter(s).

\begin{framed}
{\bf
A quantity which has a probability associated with each possible value is
traditionally called a ``random variable''. Random variables have probability
distributions associated with them. In Bayesian stats, an unknown
parameter looks mathematically like a ``random variable'', but I try to avoid
the word random itself because it usually has connotations about something that
fluctuates or varies. In Bayesian statistics, the prior distribution and
posterior distribution only describe our uncertainty. The actual parameter is
a single fixed number.}
\end{framed}

\section{Parameter Estimation: Bus Example}
This is a beginning example of parameter estimation from a Bayesian point of
view. It shows the various features that are always present in a Bayesian
parameter estimation problem. There will be a prior distribution, the likelihood,
and the posterior distribution. We will spend a lot of time on this problem but
keep in mind that this is just a single example, and certain things about this
example (such as the choice of the prior and the likelihood) are specific to
this example only, while other things about it are very general and will apply
in all parameter estimation problems. You will see and gain experience with
different problems in lectures, labs, and assignments.

After moving to Auckland, I decided that I would take the bus to work
each day. However, I wasn't very confident with the bus system in my new city,
so for the first
week I just took the first bus that came along and was heading in the right
direction, towards the city. In the first week, I caught 5 morning buses.
Of these 5 buses, two of them took me to the right place, while three of them
took me far from work, leaving me with an extra 20 minute walk. Given this
information, I would like to try to infer the proportion
of the buses that are ``good'', that would take me right to campus. Let us call
this fraction $\theta$ and we will infer $\theta$ using the Bayesian framework.
We will start with a prior distribution that describes initial uncertainty about
$\theta$ and update this to get the posterior distribution, using the data that
2/5 buses I took were ``good''.

First we must think about the meaning of the parameter $\theta$ in our particular
problem so we can choose a sensible prior distribution. Since $\theta$ is, in
this example, a proportion, we know it cannot be less than 0 or greater than 1.
In principle, $\theta$ could be any real value between 0 and 1. To keep things
simple {\it to begin with}, we shall make an approximation and assume that the
set of possible values for $\theta$ is:
\begin{center}
$\{0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1\}$.
\end{center}
This discrete
approximation means that we can use a Bayes' Box. The first things to fill out
in the Bayes' Box are the possible values and the prior probabilities (the prior
distribution). For starters, let us assume that before we got the data (two
successes out of 5 trials), we were very uncertain about the value of $\theta$,
and this can be modelled by using a uniform prior distribution. There are 11
possible values for $\theta$ that are being considered with our discrete
approximation, so the probability of each is $1/11 = 0.0909$. The partially
complete Bayes' Box is given in Table~\ref{tab:bus_bayes_box1}. Note the
new notation that I have put in the column titles. We will use this notation
in all of our parameter estimation examples (although the parameter(s) and data
may have different symbols when $\theta$ and $x$ respectively are not
appropriate).

\begin{table}[!ht]
\begin{center}
\begin{tabular}{|c|c|c|c|c|}
\hline
\tt{possible values} & \tt{prior} & \tt{likelihood} & \tt{prior} $\times$ \tt{likelihood} & \tt{posterior}\\
$\theta$ & $p(\theta)$ & $p(x|\theta)$ & $p(\theta)p(x|\theta)$ & $p(\theta|x)$\\
\hline
0 & 0.0909 &  &  & \\
0.1 & 0.0909 &  &  & \\
0.2 & 0.0909 &  &  & \\
0.3 & 0.0909 &  &  & \\
0.4 & 0.0909 &  &  & \\
0.5 & 0.0909 &  &  & \\
0.6 & 0.0909 &  &  & \\
0.7 & 0.0909 &  &  & \\
0.8 & 0.0909 &  &  & \\
0.9 & 0.0909 &  &  & \\
1 & 0.0909 &  &  & \\
\hline
Totals & 1 & & & 1\\
\hline
\end{tabular}
\caption{\it Starting to make a Bayes' Box for the bus problem. This one
just has the possible parameter values and the prior distribution.
\label{tab:bus_bayes_box1}}
\end{center}
\end{table}

To get the likelihoods, we need to think about the properties of our experiment.
In particular, we should imagine that we knew the value of $\theta$ and were
trying to predict what experimental outcome (data) would occur. Ultimately,
we want to find the probability of our actual data set (2 out of the 5 buses
were ``good''), for all of our possible $\theta$ values.

Recall that,
if there are $N$
repetitions of a ``random experiment'' and the ``success'' probability is
$\theta$ at each repetition,
then the number of ``successes'' $x$ has a binomial distribution:
\begin{eqnarray}
p(x|\theta) &=& \left(\begin{array}{c}N \\ x\end{array}\right)
\theta^x\left(1-\theta\right)^{N - x}.\label{eq:binomial_likelihood2}
\end{eqnarray}
where $\left(\begin{array}{c}N \\ x\end{array}\right) = \frac{N!}{x!(N-x)!}$.
This is the probability mass function for $x$ (if we imagine $\theta$ to be known), hence the notation $p(x|\theta)$, read as ``the probability distribution for $x$ given $\theta$''.
Since there are five trials
($N=5$) in the bus problem, the number of successes $x$ must be one of
0, 1, 2, 3, 4, or 5.
If $\theta$ is a high number close to 1, then we would expect the resulting
value of the data (number of successes) $x$ to be something high like 4 or 5. Low values for $x$ would
still be possible but they would have a small probability. If $\theta$ is a
small number, we would expect the data to be 0, 1, or 2, with less probability
for more than 2 successes. This is just saying in words what is written
precisely in Equation~\ref{eq:binomial_likelihood}. The probability distribution
for the data $x$ is plotted in Figure~\ref{fig:binomial} for three illustrative
values of the parameter $\theta$.
\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.6]{Figures/binomial.pdf}
\caption{\it The binomial probability distribution for the data $x$, for three different values of the parameter $\theta$. If $\theta$ is
low then we would expect to see lower values for the data. If $\theta$ is high
then high values are more probable (but all values from 0 to 5 inclusive are
still possible). The actual observed value of the data was $x=2$. If we focus
only on the values of the curves at $x=2$, then the heights of the curves give
the likelihood values for these three illustrative values of $\theta$.
\label{fig:binomial}}
\end{center}
\end{figure}
To obtain the actual likelihood values that go into the Bayes' Box, we can
simply substitute in the known values $N=5$ and $x=2$:
\begin{eqnarray}
P(x=2|\theta) &=& \left(\begin{array}{c}5 \\ 2\end{array}\right)
\theta^2\left(1-\theta\right)^{5 - 2}\\
&=& 10\times\theta^2\left(1-\theta\right)^3.\label{eq:binomial_likelihood3}
\end{eqnarray}
The resulting equation depends on $\theta$ only! We can go through the list of
$\theta$ values and get a numerical answer for the likelihood $P(x=2|\theta)$,
which is what we need for the Bayes' Box. The final steps are, as usual, to
multiply the prior by the likelihood and then normalise that to get the
posterior distribution. The completed Bayes' Box is given in
Table~\ref{tab:bus_bayes_box2}.

\begin{table}[!ht]
\begin{center}
\begin{tabular}{|c|c|c|c|c|}
\hline
\tt{possible values} & \tt{prior} & \tt{likelihood} & \tt{prior} $\times$ \tt{likelihood} & \tt{posterior}\\
$\theta$ & $p(\theta)$ & $p(x|\theta)$ & $p(\theta)p(x|\theta)$ & $p(\theta|x)$\\
\hline
0 & 0.0909 & 0 & 0 & 0\\
0.1 & 0.0909 & 0.0729 & 0.0066 & 0.0437\\
0.2 & 0.0909 & 0.2048 & 0.0186 & 0.1229\\
0.3 & 0.0909 & 0.3087 & 0.0281 & 0.1852\\
0.4 & 0.0909 & 0.3456 & 0.0314 & 0.2074\\
0.5 & 0.0909 & 0.3125 & 0.0284 & 0.1875\\
0.6 & 0.0909 & 0.2304 & 0.0209 & 0.1383\\
0.7 & 0.0909 & 0.1323 & 0.0120 & 0.0794\\
0.8 & 0.0909 & 0.0512 & 0.0047 & 0.0307\\
0.9 & 0.0909 & 0.0081 & 0.0007 & 0.0049\\
1 & 0.0909 & 0 & 0 & 0\\
\hline
Totals & 1 & & 0.1515 & 1\\
\hline
\end{tabular}
\caption{\it The completed Bayes' Box for the bus problem
(using a binomial distribution to obtain the likelihood).
\label{tab:bus_bayes_box2}}
\end{center}
\end{table}

There are a few interesting values in the likelihood column that should help you
to understand the concept of likelihood a bit better. Look at the likelihood for
$\theta = 0$: it is zero. What does this mean? It means that if we imagine
$\theta=0$ is the true solution, the probability of obtaining the data that we
got ($x=2$ successes) would be zero. That makes sense! If $\theta=0$, it means
none of the buses are the ``good'' buses, so how could I have caught a
good bus twice? The probability of that is zero.

The likelihood for $\theta=1$ is also zero for similar reasons. If all of the
buses are good, then having 2/5 successes is impossible. You would get 5/5
with 100\% certainty. So $P(x=2|\theta=1) = 0$. The likelihood is highest
for $\theta = 0.4$, which just so happens to equal 2/5. This $\theta=0.4$ predicted
the data best. It does
not necessarily mean that $\theta=0.4$ is the most probable value. That depends
on the prior as well (but with a uniform prior, it does end up being that way.
As you can see in the posterior distribution column, $\theta=0.4$ has the
highest probability in this case).

\subsection{Sampling Distribution and Likelihood}
As we study more examples of parameter estimation, you might notice that
we always find the likelihood by specifying a probability distribution for
the data given the parameters $p(x|\theta)$, and then we substituting in the
actual observed data (Equations~\ref{eq:binomial_likelihood2}
and~\ref{eq:binomial_likelihood3}). Technically, only the version with the
actual data set substituted in is called the likelihood. The probability
distribution $p(x|\theta)$, which gives the probability of other data sets
that did not occur (as well as the one that did), is sometimes called the
{\it sampling distribution}. At times, I will distinguish between the sampling
distribution and the likelihood, and at other times I might just use the word
likelihood for both concepts.

\subsection{What is the ``Data''?}
Even though this example is meant to be introductory, there is a subtlety
that has been swept under the rug. Notice that our data consisted of the fact
that we got 2/5 successes in the experiment. When we worked out the likelihood,
we were considering the probability of getting $x=2$, but we didn't have a
probability for $N=5$. In principle, we could treat $x$ and $N$ as two separate data sets.
We could first update from the prior to the posterior given $N=5$, and then update
again to take into account $x$ as well as $N$. However, the first update would
be a bit weird. Why would knowing the number of trials tell you anything about
the success probability? Effectively, what we have done in our analysis is assume that $N=5$ is prior information that lurks in the background the whole time. Therefore our uniform prior for $\theta$ already ``knows''
that $N=5$, so we didn't have to consider $P(N=5|\theta)$ in the likelihood.
This subtlety usually doesn't matter much.

\section{Prediction in the Bus Problem}\label{sec:prediction_bus_problem}
We have now seen how to use information (data) to update from a prior distribution
to a posterior distribution when the set of possible parameter values is
discrete. The
posterior distribution is the complete answer to the problem. It tells us exactly
how strongly we should believe in the various possible solutions (possible
values for the unknown parameter). However, there
are other things we might want to do with this information. Predicting the future
is one! It's fun, but risky. Here we will look at how prediction is done using the Bayesian
framework, continuing with the bus example. To be concrete, we are interested
in the following question: {\it what is the probability that I will catch the
right bus tomorrow?}. This is like trying to predict the result of a future
experiment.

In the Bayesian framework, our predictions are always in the form of
probabilities or (later) probability distributions. They are usually calculated
in three stages. First, you pretend you {\it actually know} the true value of
the parameters, and calculate the probability based on that assumption. Then,
you do this for all possible values of the parameter $\theta$
(alternatively, you can calculate the probability as a function of $\theta$).
Finally, you combine all of these probabilities in a particular way to get
one final probability which tells you how confident you are of your prediction.

Suppose we knew the true value of $\theta$ was 0.3. Then, we would know
the probability of catching the right bus tomorrow is 0.3. If we knew the
true value of $\theta$ was 0.4, we would say the probability of catching
the right bus tomorrow is 0.4. The problem is, we don't know what the true value
is. We only have the posterior distribution. Luckily, the sum rule of
probability (combined with the product rule) can help us out. We are interested in whether I will get the good
bus tomorrow. There are 11 different ways that can happen. Either $\theta=0$ and
I get the good bus, or $\theta=0.1$ and I get the good bus, or $\theta=0.2$ and
I get the good bus, and so on. These 11 ways are all mutually exclusive. That is,
only one of them can be true (since $\theta$ is actually just a single number).
Mathematically, we can obtain the posterior probability of catching the good
bus tomorrow using the sum rule:
\begin{eqnarray}
P(\textnormal{good bus tomorrow} | x) &=& \sum_{\theta}
p(\theta | x)P(\textnormal{good bus tomorrow} | \theta, x)\\
&=& \sum_{\theta}
p(\theta | x)\theta
\end{eqnarray}
This says that the total probability for a good bus tomorrow (given the data,
i.e. {\it using the posterior distribution} and not the prior distribution)
is given by going through each possible $\theta$ value, working out the probability
{\it assuming the $\theta$ value you are considering is true}, multiplying by the probability
(given the data) this $\theta$ value is actually true, and summing. In this
particular problem, because $P(\textnormal{good bus tomorrow} | \theta, x) = \theta$, it
just so happens that the probability for tomorrow is the expectation value of
$\theta$ using the posterior distribution. To three decimal places, the result
for the probability tomorrow is 0.429. Interestingly, this is not equal to
$2/5 = 0.4$.

In practice, these kinds of calculations are usually done in a computer.
The R code for computing the Bayes' Box and the probability for tomorrow
is given below. This is very much like many of the problems we will work on in
labs.

\begin{minted}[mathescape,
               numbersep=5pt,
               gobble=0,
               frame=single,
               framesep=2mm, fontsize=\small]{r}
# Make a vector of possibilities (first column of the Bayes' Box)
theta = seq(0, 1, by=0.1)

# Corresponding vector of prior probabilities
# (second column of the Bayes' Box)
prior = rep(1/11,11)

# Likelihood. Notice use of dbinom() rather than formula
# because R conveniently knows a lot of
# standard probability distributions already
lik = dbinom(2,5,theta)

# Prior times likelihood, then normalise to get posterior
h = prior*lik
post = h/sum(h)

# Probability for good bus tomorrow (prediction!)
# This happens to be the same as the posterior expectation of theta
# *in this particular problem* because the probability of a
# good bus tomorrow GIVEN theta is just theta.
prob_tomorrow = sum(theta*post)
\end{minted}

\section{Bayes' Rule, Parameter Estimation Version}
Mathematically, what we did to calculate the posterior distribution was to
take the prior distribution as a whole (the whole second column) and multiply it
by the likelihood (the whole third column) to get the
unnormalised posterior, then normalise to get the final posterior distribution.
This can be written as follows, which we will call the ``parameter estimation''
version of Bayes' rule. There are three ways to write it:
\begin{eqnarray}
p(\theta | x) &=& \frac{p(\theta)p(x|\theta)}{p(x)}\\
p(\theta | x) &\propto& p(\theta)p(x|\theta)\\
{\tt posterior} &\propto& {\tt prior}\times{\tt likelihood}.\label{eq:bayes_pe}
\end{eqnarray}
Writing the equations in these ways is most useful when you can write the
prior $p(\theta)$ and the likelihood $p(x|\theta)$ as formulas (telling you
how the values depend on $\theta$ as you go through the rows). Then you can get
the equation for the posterior distribution (whether it is a discrete
distribution, or a continuous one, in which case $p(\theta)$ and
$p(\theta|x)$ are probability densities. We will do this in the next chapter.

The notation in Equation~\ref{eq:bayes_pe} is very simplified and concise, but
is a popular kind of notation in Bayesian work. For an explanation of the
relationship between this notation and other common
choices (such as $P(X=x)$ for a
discrete distribution or $f(x)$ for a density), see
Appendix~\ref{sec:probability}.

\chapter{Parameter Estimation: Analytical Methods}
Analytical methods are those which can be carried out with a pen and paper,
or the ``old school'' way before we all started using computers. There are some
problems in Bayesian statistics that can be solved in this way, and we will
see a few of them in this course. For an analytical solution to be possible, the
maths usually has to work out nicely, and that doesn't always happen, so
the techniques shown here don't {\it always} work. When they do -- great! When
they don't, that's what MCMC (and JAGS) is for!

Let's look at the {\it binomial likelihood} problem again, with the familiar
bus example. Out of $N=5$ attempts at a ``repeatable'' experiment, there were
$x=2$ successes. From this, we want to infer the value of $\theta$, the
success probability that applied on each trial, or the overall fraction of
buses that are good. Because of its meaning, we know
with 100\% certainty that $\theta$ must be between 0 and 1 (inclusive).

Recall that, if we knew the
value of $\theta$ and wanted to predict the data $x$ (regarding $N$ as being
known in advance), then we would use the binomial distribution:
\begin{eqnarray}
p(x|\theta) &=& \left(\begin{array}{c}N \\ x\end{array}\right)
\theta^x\left(1-\theta\right)^{N - x}.\label{eq:binomial_likelihood}
\end{eqnarray}

Let's use a uniform prior for $\theta$, but instead of making the discrete
approximation and using a Bayes' Box, let's keep the continuous set of possibilities,
that $\theta$ can be any real number between 0 and 1. Because the set of
possibilities is continuous, the prior and the posterior for $\theta$ will both
be probability {\it densities}. If we tried to do a Bayes' Box now, it would
have infinitely many rows!
The equation for our prior, a uniform probability density between 0 and 1, is:
\begin{eqnarray}
p(\theta) &=& \left\{
\begin{array}{lr}
1, & 0 \leq \theta \leq 1\\
0, & \textnormal{otherwise}
\end{array}
\right.\label{eq:uniform}
\end{eqnarray}
If we keep in mind that $\theta$ is between 0 and 1, and therefore remember at
all times that we are restricting our attention to $\theta \in [0, 1]$, we can
write the uniform prior much more simply as:
\begin{eqnarray}
p(\theta) &=& 1.
\end{eqnarray}
If you find the Bayes' Box way of thinking easier to follow than the mathematics
here, you can imagine we are making a Bayes' Box like in
Table~\ref{tab:bus_bayes_box1}, but with an ``infinite'' number of rows, and
the equation for the prior tells us how the prior probability varies as a function
of $\theta$ as we go down through the rows (since the prior is uniform, the
probabilities don't vary at all).

To find the posterior probability density for $\theta$, we use the ``parameter
estimation'' form of Bayes' rule:
\begin{eqnarray}
\textnormal{posterior} \propto \textnormal{prior} \times \textnormal{likelihood}\\
p(\theta|x) \propto p(\theta)p(x|\theta).
\end{eqnarray}
We already wrote down the equations for the prior and the likelihood, so we
just need to multiply them.
\begin{eqnarray}
p(\theta|x) &\propto& p(\theta)p(x|\theta)\\
&\propto& 1 \times \left(\begin{array}{c}N \\ x\end{array}\right)
\theta^x\left(1-\theta\right)^{N - x}
\end{eqnarray}
Since we are using the abbreviated form of the prior, we must remember this
equation only applies for $\theta \in [0, 1]$.
To simplify the maths, there are some useful tricks you can use a lot of the
time when working things out analytically. Notice that the ``parameter estimation''
form of Bayes' rule has a proportional sign in it, not an equals sign. That's
because the prior times the likelihood can't actually be the posterior
distribution because it is not normalised. The sum or integral is not 1.
However, the equation still gives the correct shape of the posterior probability density
function (the way it varies as a function of $\theta$). This is helpful because
you can save ink. If there are some constant factors in your expression for
the posterior
that don't involve the parameter (in this case, $\theta$), you can ignore them.
The proportional sign will take care of them. In this case, it means we can
forget about the pesky ``$N$ choose $x$'' term, and just write:
\begin{eqnarray}
p(\theta|x) &\propto& \theta^x\left(1-\theta\right)^{N - x}\\
&\propto& \theta^2\left(1-\theta\right)^3.
\end{eqnarray}
The final step I included was to substitute in the actual values of $N$ and $x$ instead of
leaving the symbols there. That's it! We have the correct shape of the
posterior distribution. We can use this to plot the posterior, as you can see
in Figure~\ref{fig:bus_inference}.

\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.6]{Figures/bus_inference.pdf}
\caption{\it The prior and the posterior for $\theta$ in the bus problem, given
that we had 2/5 successes. The prior is just a uniform density and this is
plotted as a flat line, describing the fact that $\theta$ can be anywhere between
0 and 1 and we don't have much of an idea. After getting the data, the distribution
changes to the posterior which is peaked at 0.4, although there is still a pretty
wide range of uncertainty.\label{fig:bus_inference}}
\end{center}
\end{figure}

\section{``$\sim$'' Notation}
While it is very helpful to know the full equations for different kinds of
probability distributions (both discrete and continuous), it is
useful to be able to communicate about probability distributions in an
easier manner. There is a good notation for this which we will sometimes use
in STATS 331. If we want to communicate about our above analysis, and someone
wanted to know what prior distribution we used, we could do several things.
We could say ``the prior for $\theta$
was uniform between 0 and 1'', or we could give the formula for the prior distribution
(Equation~\ref{eq:uniform}). However, a convenient shorthand in common use is
to simply write:
\begin{eqnarray}
\theta \sim \textnormal{Uniform}(0, 1)
\end{eqnarray}
or, even more concisely:
\begin{eqnarray}
\theta \sim U(0, 1).
\end{eqnarray}
This notation conserves ink, and is good for quick communication.
It is also
very similar to the notation used in JAGS, which will be introduced in later
chapters.

We can also write the binomial likelihood (which we used for the bus problem)
in this notation, instead of writing out
the full equation (Equation~\ref{eq:binomial_likelihood}). We can write:
\begin{eqnarray}
x | \theta \sim \textnormal{Binomial}(N, \theta)
\end{eqnarray}
This says that if we knew the value of $\theta$, $x$ would have a binomial
distribution with $N$ trials and success probability $\theta$. We can also
make this one more concise:
\begin{eqnarray}
x \sim \textnormal{Bin}(N, \theta)
\end{eqnarray}
The differences here are that ``Binomial'' has been shortened to ``Bin'' and
the ``given $\theta$'' part has been left out. However, we see that there is
a $\theta$ present on the right hand side, so the ``given $\theta$'' must
be understood implicitly.

\section{The Effect of Different Priors}
We decided to do this problem with a uniform prior, because it is the obvious
first choice to describe ``prior ignorance''. However, in principle, the prior
could be different. This will change the posterior distribution, and hence
the conclusions. This isn't a problem of Bayesian analysis, but a feature. Data
on its own doesn't tell us exactly what should believe. We must
combine the data with all our other prior knowledge (i.e. put the data in context)
to arrive at reasoned conclusions.

In this section we will look at the effect of different priors
on the results, again focusing on the bus problem for continuity.
Specifically, we will look at three different priors: the
uniform one that we already used, and two other priors discussed below.

\subsection{Prior 2: Emphasising the Extremes}
One possible criticism of the uniform prior is that there is not much probability
given to extreme solutions. For example, according to the Uniform(0, 1) prior,
the prior probability that $\theta$ is
between 0 and 0.1 is only $\int_0^{0.1} 1 \, d\theta = 0.1$. But, depending on the situation, we might think
values near zero should be more plausible\footnote{Here's another parameter that is between 0 and 1: the proportion of
households in New Zealand that keep a Macaw as a pet (call that $\phi$).
I hope this number is low
(it is very difficult to take responsible care of such a smart bird). I also
think it probably is low. I would definitely object to a prior that implied
$P(\phi < 0.1) = 0.1$. I would want a prior that implied something like
$P(\phi < 0.1) = 0.999999$.}.
One
possible choice of prior distribution
that assigns more probability to the extreme values (close to 0 or 1) is:
\begin{eqnarray}
p(\theta) \propto \theta^{-\frac{1}{2}}(1 - \theta)^{-\frac{1}{2}}.\label{eq:prior2}
\end{eqnarray}

\subsection{Prior 3: Already Being Well Informed}
Here's another scenario that we might want to describe in our prior.
Suppose that, before getting this data, you weren't ignorant at all, but already
had a lot of information about the value of the parameter. Say that we already had a lot of information which
suggested the value of $\theta$ was probably close to 0.5. This could be modelled
by the following choice of prior:
\begin{eqnarray}
p(\theta) \propto \theta^{100}(1 - \theta)^{100}.\label{eq:prior3}
\end{eqnarray}

The three priors are plotted in Figure~\ref{fig:three_priors} as dotted lines.
The three corresponding posterior distributions are plotted as solid lines.
The posteriors were computed by multiplying the three priors by the likelihood
and normalising.
The blue curves correspond to the uniform prior we used before, the red
curves use the ``emphasising the extremes'' prior, and the green curves use the
``informative'' prior which assumes that $\theta$ is known to be close to 0.5.

\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.6]{Figures/three_priors.pdf}
\caption{\it Three different priors (dotted lines) and the corresponding
three posteriors (solid lines) given the bus example data. See the text
for discussion of these results.\label{fig:three_priors}}
\end{center}
\end{figure}

There are a few interesting things to notice about this plot. Firstly, the
posterior distributions are basically the same for the red and blue priors (the
uniform prior and the ``emphasising the extremes'' prior). The main difference in the
posterior is, as you would expect, that the extremes are emphasised a little
more. If something is more plausible before you get the data, it's more
plausible afterwards as well.

The big difference is with the informative prior. Here, we were already pretty
confident that $\theta$ was close to 0.5, and the data (since it's not very
much data) hasn't given us any reason to doubt that, so we still think $\theta$
is close to 0.5. Since we already knew that $\theta$ was close to 0.5, the data
are acting only to increase the precision of our estimate (i.e. make the
posterior distribution narrower). But since we had so much prior information,
the data aren't providing much ``extra'' information, and the posterior looks
basically the same as the prior.

\subsection{The Beta Distribution}
The three priors we have used are all examples of {\it beta} distributions. The beta
distributions are a family of probability distributions (like the normal, Poisson,
binomial, and so on) which can be applied to continuous random variables known
to be between 0 and 1. The general form of a beta distribution (here written
for a variable $x$) is:
\begin{eqnarray}
p(x|\alpha, \beta) \propto x^{\alpha - 1}(1 - x)^{\beta - 1}\label{eq:beta}.
\end{eqnarray}
The quantities $\alpha$ and $\beta$ are two parameters that control the shape
of the beta distribution. Since we know the variable $x$ is between 0 and 1 with probability 1, the
normalisation constant could be found by doing an integral. Then you could write
the probability distribution with an equals sign instead of a proportional sign,
\begin{eqnarray}
p(x|\alpha, \beta) &=& \frac{x^{\alpha - 1}(1 - x)^{\beta - 1}}
{\int_0^1 x^{\alpha - 1}(1 - x)^{\beta - 1} \, dx}\\
&=& \frac{x^{\alpha - 1}(1 - x)^{\beta - 1}}
{B(\alpha, \beta)}.
\end{eqnarray}
where $B(\alpha, \beta)$ (called the ``beta function'') is defined (usefully\ldots) as the
result of doing that very integral (it can be related to factorials too, if you're interested).
Thankfully, we can get away with the ``proportional'' version most of the time.
In ``$\sim$'' notation the beta distribution is written as:
\begin{eqnarray}
x|\alpha, \beta &\sim& \textnormal{Beta}(\alpha, \beta).
\end{eqnarray}
Again, the ``given $\alpha$ and $\beta$'' can be dropped. It is implicit because
they appear on the right hand side. By identifying the terms of
Equation~\ref{eq:beta} with the form of our three priors (Equations~\ref{eq:uniform},~\ref{eq:prior2} and~\ref{eq:prior3})),
we see that our three priors can be written in ``$\sim$'' notation like this:

\begin{table}[!ht]\begin{center}
\begin{tabular}{ll}
Prior 1: & $\theta \sim \textnormal{Beta}(1, 1)$\\
Prior 2: & $\theta \sim \textnormal{Beta}\left(\frac{1}{2}, \frac{1}{2}\right)$\\
Prior 3: & $\theta \sim \textnormal{Beta}(101, 101)$\\
\end{tabular}\end{center}
\end{table}

When you work out the posterior distributions analytically and then compare them
to the formula for the beta distribution, you can see that the three posteriors
are also beta distributions! Specifically, you get:

\begin{table}[!ht]\begin{center}
\begin{tabular}{ll}
Posterior 1: & $\theta \sim \textnormal{Beta}(3, 4)$\\
Posterior 2: & $\theta \sim \textnormal{Beta}(2.5, 3.5)$\\
Posterior 3: & $\theta \sim \textnormal{Beta}(103, 104)$
\end{tabular}\end{center}
\end{table}
This is ``magic'' that is made possible by the mathematical form of the beta
prior and the binomial likelihood\footnote{The technical term for this magic is that the beta distribution is a {\it conjugate prior} for the binomial
likelihood.}. It is not always possible to do this.

We can also derive the general solution for the posterior for $\theta$ when
the prior is a Beta$(\alpha, \beta)$ distribution, the likelihood is a
binomial distribution, and $x$ successes were observed out of $N$ trials.
The posterior is:
\begin{eqnarray}
p(\theta | x) &\propto& p(\theta)p(x|\theta)\\
&\propto& \theta^{\alpha - 1}(1-\theta)^{\beta-1} \times
\theta^x (1-\theta)^{N-x}\\
&=& \theta^{\alpha + x - 1}(1 - \theta)^{\beta + N - x - 1}
\end{eqnarray}
which can be recognised as a Beta$(\alpha+x, \beta+N-x)$ distribution.

Remember that in this particular problem, the probability of a success tomorrow
is simply the expectation value (mean) of the posterior distribution for $\theta$.
We can look up (or derive) the formula for the mean of a beta distribution and find that
if $x \sim \textnormal{Beta}(\alpha, \beta)$ then $\mathds{E}(x) = \alpha/(\alpha + \beta)$.
Applying this to the three posterior distributions gives:

\begin{table}[!ht]\begin{center}
\begin{tabular}{lll}
$P(\textnormal{good bus tomorrow}|x) = 3/7 $    & $\approx 0.429$ & (using prior 1)\\
$P(\textnormal{good bus tomorrow}|x) = 2.5/6$   & $\approx 0.417$ & (using prior 2)\\
$P(\textnormal{good bus tomorrow}|x) = 103/207$ & $\approx 0.498$ & (using prior 3)
\end{tabular}\end{center}
\end{table}
The result for Prior 1 is Laplace's infamous ``rule of succession'' which I will
discuss a little bit in lectures.

\subsection{A Lot of Data}
As shown above, the choice of prior distribution has an impact on the conclusions. Sometimes
it has a big impact (the results using prior 3 were pretty different to the results
from priors 1 and 2), and sometimes not much impact (e.g. the results from
priors 1 and 2 were pretty similar). There is a common phenomenon that happens
when there is a lot of data: the prior tends not to matter so much. Imagine
we did a much bigger version of the bus experiment with $N=1000$ trials, which
resulted in $x=500$ successes. Then the posterior distributions corresponding
to the three different priors are all very similar (Figure~\ref{fig:lots_of_data}).

\begin{figure}[!ht]
\begin{center}
\includegraphics[scale=0.6]{Figures/lots_of_data.pdf}
\caption{\it When you have a lot of data, the results are less sensitive
to the choice of prior distribution. Note that we have zoomed in and are only
looking around $\theta=0.5$: these posterior distributions are quite narrow
because there is now a lot more information about $\theta$. The red and blue
posteriors (based on priors 1 and 2) are so similar that they overlap and look
like one purple curve.\label{fig:lots_of_data}}
\end{center}
\end{figure}

This is reassuring. Note, however, that this only occurs because the three
analyses used the same likelihood. If three people have different prior
distributions for something {\it and} they can't agree on what the experiment
even means, there is no guarantee they will end up agreeing, even if there's
a large amount of data!

Remember though, that when the results {\it are} sensitive
to the choice of prior, that is not a problem with the Bayesian approach, but
rather an important warning message: the data aren't very
informative! Then, the options are: i) think really hard about your prior
distribution and be careful when deciding what it should be, and ii) get more or
better data!

%\section{Poisson Example}
%We have just seen an example of how to calculate the posterior distribution for
%a single parameter, given some data. Now let's consider another example
%involving different probability distributions. The Poisson distribution is
%commonly used for so-called ``rare events''. For example, how many ...

%The probability mass function for the Poisson distribution is:
%\begin{eqnarray}
%p(x | \lambda) &=& \frac{\lambda^x e^{-\lambda}}{x!}
%\end{eqnarray}