# stat946f11pool

## Contents

- 1 Undirected Graphical Models
- 2 Graphical Algorithms
- 3 Basic Statistical Problems
- 4 Example from last class
- 5 EM Algorithm
- 6 Hidden Markov Models
- 7 Sampling Methods

### Undirected Graphical Models

In the previous sections we discussed the Bayes Ball algorithm and the way we can use it to determine if there exists a conditional independence between two nodes in the graph. This algorithm can be easily modified to allow us to determine the same information in an undirected graph. An undirected graph that provides information about the relationships between different random variables can also be called a "Markov Random Field".

As before we must define a set of canonical graphs. The nice thing is that for undirected graphs there is really only one type of canonical graph:

In the first figure (Fig. 21) we have no information about the node Y and so we can not say if the nodes X and Z are independent since the ball can pass from one to the other. On the other hand, in (Fig. 22) the value of Y is known and so the ball can not pass from X to Z or from Z to X. In this case we can say the X and Z are independent given Y.

Now that we have a type of Bayes Ball algorithm for both directed and undirected graphs we can ask ourselves the question: Is there an algorithm or method that we can use to convert between directed and undirected graphs?

In general: **NO**.

In fact, not only does there not exist a method for conversion but some graphs do not have an equivalent and may exist only in the undirected or directed form. Take the following undirected graph (Fig. 23). We can see that the radom variables that are represented in this graph have the following properties:

Now try building a directed graph with the same properties taking into consideration that directed graphs cannot contain a cycle. Under this restriction it is in fact impossible to find an equivalent directed graph that satisfies all of the above properties. Similarly, consider the following directed graph (Fig. 24). It can not be represented by any undirected graph with 3 nodes.

When we want to graph the relationships between a set of random variables it is important to consider both graph types since some relationships can only be graphed on a certain type of graph. We must therefore conclude that undirected graphs are just as important as the directed ones. For the directed graphs we have an expression for

In order to develop the expression we need to introduce more terminology.

- Clique -

A subset of fully connected nodes in a graph G. Every node in the clique C is directly connected to every other node in C.

- Maximal Clique -

A clique where if any other node from the graph G is added to it then the new set is no longer a clique.

Let

Let = A non-negative real valued function.

Now associate one with each clique then,

Where,

#### Conditional independence

For directed graphs Bayes ball method was defined to determine the conditional independence properties of a given graph. We can also employ the Bayes ball algorithm to examine the conditional independency of undirected graphs. Here the Bayes ball rule is simpler and more intuitive. Considering Figure.... , a ball can be thrown either from x to z or from z to x if y is not observed. In other words, if y is not observed a ball thrown from x can reach z and vice versa. On the contrary, given a shaded y, the node can block the ball and make x and z conditionally independent. With this definition one can declare that in an undirected graph, a node is conditionally independent of non-neighbors given neighbours. Technically speaking,

is independent of given if the set of nodes separates the nodes from the nodes . Hence, if every path from a node in to a node in includes at least one node in , then we claim that .## Graphical Algorithms

In the previous chapter there were two kinds of graphical models that were used to represent dependencies between variables. One is a directed graphical model while the other is an undirected graphical model. In the case of directed graphs we can define the joint probability distribution based on a product of conditional probabilities where each node is conditioned on the value(s) of its parent(s). In the case of the undirected graphs we can define the joint probability distribution based on the normalized product of

In the previous chapter we also developed the following two expressions for :

#### For Directed Graphs:

#### For Undirected Graphs:

#### Theorem: Hammersley - Clifford

If we allow

to represent the set of all the decompositions of based on a certain graphical representation and we allow to represent all possible conditional probabilities of those nodes then we will find that the sets and are in fact the same set.

Then:

There is a lot of information contained in the joint probability distribution

. We have defined 6 tasks (listed bellow) that we would like to accomplish with various algorithms for a given disribution . These algorithms may each be able to perform a subset of the tasks listed bellow.### Tasks:

- Marginalization

Given

\underline{ex.} Given find

- Conditioning

Given

find .- Evaluation

Evaluate the probability for a certain configuration.

- Completion

Compute the most probable configuration. In other words, which of the

is the largest for a specific combinations of and .- Simulation

Generate a random configuration for

.- Learning

We would like to find parameters for

.

### Exact Algorithms:

We will be looking at three exact algorithms. An exact algorithm is an algorithm that will find the exact answer to one of the above tasks. The main disadvantage to the exact algorithms approach is that for large graphs which have a large number of nodes these algorithms take a long time to produce a result. When this occurs we can use inexact algorithms to more efficiently find a useful estimate.

- Elimination
- Sum-Product
- Junction Tree

### General Inference:

Let us first define a set of nodes called Evidence Nodes. We will denote evidence nodes with

. These nodes represent the random varibles about which we have information. Similarily, let us define the set of nodes as Query Nodes. These are the set of nodes for which we seek information. By Bayes Theorem we know that:Let

be a graph with vertices and edgesThe group of nodes

and

**Example:**

Consider once again the example from Figure \ref{fig:ClassicExample1}. Suppose we want to calculate . Where refers to a fixed value of .

If we represent the joint probabilities normally we have, \[ P(x_1, x_2, ..., x_5) = \sum_{x_6}P(x_1, x_2, ..., x_6) \] which represents a table of probabilities of size

. In general this table is of size where is the number of values each variable can take on and is the number of vertices. In a computer algorithm this is exponential:We can reduce the complexity if we represent the probabilities in factored form.

Where the computational complexity is only

where is the number of parents of a node. In our case the table has been reduced to from .Let

For instance, in our example we can say that .

We know that according to Bayes Theorem we can calculate

and separately in order to find the desired conditional probability.Let us begin by calculating

<center> .And then we can use the above result to calculate the next desired probability. :

.Finally, by using the above two results we can calculate

.### Evaluation

Define * as an evidence node whose observed value is
. To show that ** is fixed at the
value , we define an evidence potential
whose value is 1 if = and 0 otherwise.*

So

When we have more than one variable such as p(F ), the total evidence potential is:

### Elimination and Directed Graphs

Given a graph G =(V,*E*), an evidence set E, and a query node F, we first choose an elimination ordering I such that F appears last in this ordering.

**Example:**

For the graph in (Fig. \ref{fig:ClassicExample1}): . Consider once again that node is the query node and is the evidence node.

(1 should be the last node, ordering is crucial)

We must now crete an active list. There are two rules that must be followed in order to create this list.

- For i put in active list.
- For i {E} put in active list.

Here, our active list is:

We first eliminate node

. We place on the active list, having removed . We now eliminate .Likewise, we can also eliminate

(which yields the unnormalized conditional probability and . Then it yields which is the normalization factor, .#### Elimination and Undirected Graphs

We would also like to do this elimination on undirected graphs such as G'.

The first task is to find the maximal cliques and their associated potential functions.

maximal clique: , , , ,

potential functions: and

The

The general rule for elimination in an undirected graph is that we can remove a node as long as we connect all of the parents of that node together. Effectively, we form a clique out of the parents of that node.

**Example: **

For the graph G in (Fig. \ref{fig:Ex1Lab})

when we remove x1, G becomes (Fig. \ref{fig:Ex2Lab})

if we remove x2, G becomes (Fig. \ref{fig:Ex3Lab})

An interesting thing to point out is that the order of the elimination matters a great deal. Consider the two results. If we remove one node the graph complexity is slightly reduced. (Fig. \ref{fig:Ex2Lab}). But if we try to remove another node the complexity is significantly increased. (Fig. \ref{fig:Ex3Lab}). The reason why we even care about the complexity of the graph is because the complexity of a graph denotes the number of calculations that are required to answer questions about that graph. If we had a huge graph with thousands of nodes the order of the node removal would be key in the complexity of the algorithm. Unfortunately, there is no efficient algorithm that can produce the optimal node removal order such that the elimination algorithm would run quickly.

### Moralization

So far we have shown how to use elimination to successively remove nodes from an undirected graph. We know that this is useful in the process of marginalization. We can now turn to the question of what will happen when we have a directed graph. It would be nice if we could somehow reduce the directed graph to an undirected form and then apply the previous elimination algorithm. This reduction is called moralization and the graph that is produced is called a moral graph.

To moralize a graph we first need to connect the parents of each node together. This makes sense intuitively because the parents of a node need to be considered together in the undirected graph and this is only done if they form a type of clique. By connecting them together we create this clique.

After the parents are connected together we can just drop the orientation on the edges in the directed graph. By removing the directions we force the graph to become undirected.

The previous elimination algorithm can now be applied to the new moral graph. We can do this by assuming that the probability functions in directed graph

are the same as the mass functions from the undirected graph.**Example:**

I =

When we moralize the directed graph (Fig. \ref{fig:Moral1}), then it becomes the
undirected graph (Fig. \ref{fig:Moral2}).

### Sum Product Algorithm

One of the main disadvantages to the elimination algorithm is that the ordering of the nodes defines the number of calculations that are required to produce a result. The optimal ordering is difficult to calculate and without a decent ordering the algorithm may become very slow. In response to this we can introduce the sum product algorithm. It has one major advantage over the elimination algorithm: it is faster. The sum product algorithm has the same complexity when it has to compute the probability of one node as it does to compute the probability of all the nodes in the graph. Unfortunately, the sum product algorithm also has one disadvantage. Unlike the elimination algorithm it can not be used on any graph. The sum product algorithm works only on trees.

For undirected graphs if there is only one path between any two pair of nodes then that graph is a tree (Fig. \ref{fig:UnDirTree}). If we have a directed graph then we must moralize it first. If the moral graph is a tree then the directed graph is also considered a tree (Fig. \ref{fig:DirTree}).

For the undirected graph

(Fig. \ref{fig:UnDirTree}) we can write the joint probability distribution function in the following way.We know that in general we can not convert a directed graph into an undirected graph. There is however an exception to this rule when it comes to trees. In the case of a directed tree there is an algorithm that allows us to convert it to an undirected tree with the same properties.

Take the above example (Fig. \ref{fig:DirTree}) of a directed tree. We can write the joint probability distribution function as:

If we want to convert this graph to the undirected form shown in (Fig. \ref{fig:UnDirTree}) then we can use the following set of rules. \begin{thinlist}

- If is the root then: .
- If is NOT the root then: .
- If = then: .

\end{thinlist} So now we can rewrite the above equation for (Fig. \ref{fig:DirTree}) as:

#### Elimination Algorithm on a Tree

We will derive \textsc{Sum-Product} algorithm from the point of view of the \textsc{Eliminate} algorithm. To marginalize

in (Fig. \ref{fig:TreeStdEx}),where,

which is essentially (potential of the node)

(potential of the edge) (message from the child).The term "*j*, and the remaining neighbor of the variable, *i*. Thus, in the above case, we will use to denote , to denote
, and to denote . We refer to the
intermediate factor as a "message" that *j*
sends to *i*. (Fig. \ref{fig:TreeStdEx})

#### Elimination To Sum Product Algorithm

The Sum-Product algorithm allows us to compute all marginals in the tree by passing messages inward from the leaves of the tree to an (arbitrary) root, and then passing it outward from the root to the leaves, again using (\ref{equ:MsgEquation}) at each step. The net effect is that a single message will flow in both directions along each edge. (See Figure \ref{fig:SumProdEx}) Once all such messages have been computed using (\ref{equ:MsgEquation}), we can compute desired marginals.

As shown in Figure \ref{fig:SumProdEx}, to compute the marginal of

using elimination, we eliminate , which involves computing a message , then eliminate and which involves messages and . We subsequently eliminate , which creates a message .Suppose that we want to compute the marginal of

. As shown in Figure \ref{fig:MsgsFormed}, we first eliminate , which creates , and then eliminate , , and , passing messages , and to .Since the messages can be "reused", marginals over all possible elimination orderings can be computed by computing all possible messages which is small in numbers compared to the number of possible elimination orderings.

The Sum-Product algorithm is not only based on equation
(\ref{equ:MsgEquation}), but also *Message-Passing Protocol*.
**Message-Passing Protocol** tells us that \textit{a node can
send a message to a neighbouring node when (and only when) it has
received messages from all of its other neighbors}.

#### For Directed Graph

Previously we stated that:

Using the above equation (\ref{eqn:Marginal}), we find the marginal of

.Now we denote:

Since the sets, *F* and *E*, add up to ,
is equal to . Thus we can substitute the
equation (\ref{eqn:Dir8}) into (\ref{eqn:Marginal}) and (\ref{eqn:Dir7}), and they become:

We are interested in finding the conditional probability. We substitute previous results, (\ref{eqn:Dir9}) and (\ref{eqn:Dir10}) into the conditional probability equation.

is an unnormalized version of conditional probability, .

#### For Undirected Graphs

We denote

to be:

### Max-Product

We would like to find the Maximum probability that can be achieved by some set of random variables given a set of configurations. The algorithm is similar to the sum product except we replace the sum with max.

**Example:**
Consider the graph in Figure \ref{fig:MaxProdEx}.

### Maximum configuration

We would also like to find the value of the

In many cases we want to use the log of this expression because the numbers tend to be very high. Also, it is important to note that this also works in the continuous case where we replace the summation sign with an integral.

## Basic Statistical Problems

In statistics there are a number of different 'standard' problems that always appear in one form or another. They are as follows: \begin{thinlist}

- Regression
- Classification
- Clustering
- Density Estimation

\end{thinlist}

### Regression

In regression we have a set of data points

for and we would like to determine the way that the variables x and y are related. In certain cases such as (Fig. \ref{img:regression.eps}) we try to fit a line (or other type of function) through the points in such a way that it describes the relationship between the two variables.Once the relationship has been determined we can give a functional value to the following expression. In this way we can determine the value (or distribution) of y if we have the value for x.

### Classification

In classification we also have a set of points

for but we would like to use the x and y values to determine if a certain point belongs in group A or in group B. Consider the example in (Fig. \ref{img:Classification.eps}) where two sets of points have been divided into the set + and the set - by a line. The purpose of classification is to find this line and then place any new points into one group or the other.We would like to obtain the probability distribution of to following equation where c is the class and x and y are the data points. In simple terms we would like to find the probability that this point is in class c when we know that the values of X and Y are x and y.

### Clustering

Clustering is somewhat like classification only that we do not know the groups before we gather and examine the data. We would like to find the probability distribution of the following equation without knowing the value of y.

We can use graphs to represent the three types of statistical problems that have been introduced so far. The first graph (Fig. \ref{fig:RegClass} can be used to represent either the Regression or the Classification problem because both the X and the Y variables are known. The second graph (Fig. \ref{fig:Clustering}) we see that the value of the Y variable is unknown and so we can tell that this graph represents the Clustering situation.

**Classification example: Naive Bayes classifier**

First define a set of boolean random variables and for .

Then we will say that a certain pattern of Xs can either be classified as a 1 or a 0. The result of this classification will be represented by the variable Y. The graphical representation is shown in (Fig. \ref{img:classifi.eps}). One important thing to note here is that the two diagrams represent the same graph. The one on the right uses plate notation to simplify the representation of the graph for variables that are indexed. Such plate notation will also be used later in these notes.

\begin{tabular}{ccc}

& &
\end{tabular}

We are interested in finding the following:

The classification is very intuitive in this case. We will calculate the probability that we are in class 1 and we will calculate the probability that we are in class 0. The higher probability will decide the class. For example if we have a higher probability of being in class 1 then we will place this set of Xs in class 1.

\begin{tabular}{ ccc }

& &

& &
\end{tabular}

Now if we define the following:

We can continue with the above simplification and we arrive at the solution:

\begin{tabular}{ ccc }
& &

& &
\end{tabular}

## Example from last class

John is not a professional trader. However he trades in the copper market. Copper stock increase if demand for copper is more than supply, and decrease if supply is more than demand. Given supply and demand, the price of copper stock is not completely determined because some unknown factors such as prediction of political stability of countries, which supply copper or news about potential new use of copper, may impact the market.

If copper stock increases and John makes a right strategy, he will win; otherwise he will lose. Since John is not a professional trader sometimes he uses a bad trade strategy and in spite of increase of stock price he loses. S: A discrete variable which represents increasing or decreasing in copper supply.

D: A discrete variable which represents increasing or decreasing in copper demand.

C: A discrete variable which represents increasing or decreasing in stack price.

P: A discrete variable that shows whether John wins or loses in his trade.

J: A discrete variable which is 1 when John makes a right choice in his trade strategy and 0 otherwise.

p(S=1)=0.6, p(D=1)=0.7, p(J=1)=0.4

\begin{tabular}{|c|c|}

\hline % after

: \hline or \cline{col1-col2} \cline{col3-col4} ... S D & p(c=1)

\hline 1 1 & 0.5

\hline 1 0 & 0.1

\hline 0 1 & 0.85

\hline 0 0 & 0.5

\hline

\end{tabular} \begin{tabular}{|c|c|}

\hline % after

: \hline or \cline{col1-col2} \cline{col3-col4} ... J C & p(p=1)

\hline 1 1 & 0.85

\hline 1 0 & 0.5

\hline 0 1 & 0.2

\hline 0 0 & 0.1

\hline

\end{tabular} \[ p(S,D,C,J,P) = p(S)p(D)p(J)p(C|S,D)p(P|J,C) \] \end{comment}

### Bayesian and Frequentist Statistics

There are two approaches of parameter estimation: the Bayesian and the Frequentist. This section focuses on the distinctions between these two approaches. We begin with a simple example,

**Example:**

Consider the following table of 1s and 2s. We would like to teach the computer to distinguish between the two sets of numbers so that when a person writes down a number the computer can use a statistical tool to decide if the written digit is a 1 or a 2.

\begin{tabular}{|c|c|c|}

\hline&1& 2

\hline X &1& 2

\hline X & 1 &2

\hline X &1&2

\hline

\end{tabular}

The question that arises is: Given a written number what is the probability that that number belongs to the group of ones and what is the probability that that number belongs to the group of twos. In the Frequentist approach we use

. We view the model as a conditional probability distribution. Here, is known and X is unknown. However, Bayesian approach views X as known and as unknown, which givesWhere *posterior probability* , is *likelihood*, and is the *prior probability* of the parameter. There are some important assumptions about this equation. First, we view as a random variable. This is characteristic of the Bayesian approach, which is that all unknown quantities are treated as random variables. Second, we view the data x as a quantity to be conditioned on. Our inference is conditional on the event . Third, in order to calculate we need . Finally, note that Bayes rule yields a distribution over , not a single estimate of .

The Frequentist approach tries to avoid the use of prior probabilities. The goal of Frequentist methodology is to develop an "objective" statistical theory, in which two statisticians employing the methodology must necessarily draw the same conclusions from a particular set of data.

Consider a coin-tossing experiment as an example. The model is the Bernoulli distribution, *statistician's uncertainty* about the value of . Bayesian statistics views the posterior probability and the prior probability alike as subjective.

### Maximum Likelihood Estimator

There is one particular estimator that is widely used in Frequentist statistics, namely the *maximum likelihood estimator*. Recall that the probability model has the intuitive interpretation of assigning probability to X for each fixed value of . In the Bayesian approach this intuition is formalized by treating as a conditional probability distribution. In the Frequentist approach, however, we treat as a function of for fixed x, and refer to as the likelihood function.
\[
\hat{\theta}_{ML}=argmax_{\theta}p(x|\theta)
\]
where is the likelihood L( )
\[
\hat{\theta}_{ML}=argmax_{\theta}log(p(x|\theta))
\]
where is the log likelihood

Since in the denominator of Bayes Rule is independent of we can consider it as a constant and we can draw the conclusion that:

Symbolically, we can interpret this as follows:

where we see that in the Bayesian approach the likelihood can be viewed as a data-dependent operator that transforms between the prior probability and the posterior probability.

### Connection between Bayesian and Frequentist Statistics

Suppose in particular that we force the Bayesian to choose a particular value of

; that is, to remove the posterior distribution to a point estimate. Various possibilities present themselves; in particular one could choose the mean of the posterior distribution or perhaps the mode.

(i) the mean of the posterior (expectation):

is called *Bayes estimate*.

OR

(ii) the mode of posterior:

Note that MAP is \textsl{Maximum a posterior}.

When the prior probabilities,

is taken to be uniform on , the MAP estimate reduces to the maximum likelihood estimate, .When the prior is not taken to be uniform, the MAP estimate will be the maximization over probability distributions(the fact that the logarithm is a monotonic function implies that it does not alter the optimizing value).

Thus, one has:

as an alternative expression for the MAP estimate.

Here,

is log likelihood and the "penalty" is the additive term . Penalized log likelihoods are widely used in Frequentist statistics to improve on maximum likelihood estimates in small sample settings.

#### Information for an Event

Consider that we have a given event E. The event has a probability P(E). As the probability of that event decreases we say that we have more information about that event. We calculate the information as:

#### Binomial Example

**Probability Example:**

Consider the set of observations which are iid, where are the different observations of . We can also say that this random variable is parameterized by a such that:

In our example we will use the following model:

Suppose now that we also have some data

e.g.

We want to use this data to estimate .

We would now like to use the ML technique. To do this we can construct the following graphical model:

Shade the random variables that we have already observed

Since all of the variables are iid then there are no dependencies between the variables and so we have no edges from one node to another.

How do we find the joint probability distribution function for these variables? Well since they are all independent we can just multiply the marginal probabilities and we get the joint probability.

This is in fact the likelihood that we want to work with. Now let us try to maximise it:

Take the derivative and set it to zero:

Where:

\begin{center} H = \# of all

T = \# of all, e.g. \# of tails

Hence,

\end{center}

And now we can solve for

:

#### Univariate Normal

Now let us assume that the observed values come from normal distribution.

\includegraphics{images/fig4Feb6.eps}
\newline
Our new model looks like:

Now to find the likelihood we once again multiply the independent marginal probabilities to obtain the joint probability and the likelihood function.

Now, since our parameter theta is in fact a set of two parameters,

we must estimate each of the parameters separately.

#### Bayesian

Now we can take a look at the Bayesian approach to the same problem. Assume

is a random variable, and we want to find . Also, assume is the mean and variance of a Gaussian distribution like in the previous example.The graphical model is shown in Figure \ref{fig:fig5Feb6}.

We can begin with the estimation of

. If we assume as uniform, then we become a Frequentist and the result matches the one from the ML estimation. But, if we assume is normal, then we get an interesting result.Assume

as normal, thenWe want to find

and take expectation.Where

is a linear combination of the sample mean and the mean of the prior.

shows a distribution of , not just a single value. Also if we were to do the calculations for the sigma we would find the following result:

#### ML Estimate for Completely Observed Graphical Models

For a given graph G(V, E) each node represents a random variable. We can observe these variables and write down data for each one. If for example we had n nodes in the graph one observation would be

. We can consider that these observations are independent and identically distributed. Note that is not necessarily independent from .**Directed Graph Example**

Consider the following directed graph (Fig. \ref{img:DirGraphObs.eps}).

We can assume that we have made a number of observations, say n, for each of the random variables in this graph.

\begin{tabular}{ccccc}
**Observation** & ** & **** & **** & **** **

1 & & & &

2 & & & &

3 & & & &

& & ... & &

n & & & &
\end{tabular}

Armed with this new information we would like to estimate

We know from before that we can write the joint distribution function as:

Which means that our likelihood function is:

And our log likelihood is:

To maximise

For discrete random variables we can use Bayes Rule. For example:

Intuitively, this means that we count the number of times that both of the variables satisfy their conditions and then divide by the number of times that only one of them satisfies the condition. Then we know what proportion of time the variables satisfy the conditions together. The proportion is in fact the

We can consider another example. We can try to find:

\begin{tabular}{cccc}

0 & 0 & &

0 & 1 & &

1 & 0 & &

1 & 1 & &
\end{tabular}

For the exponential family of distributions there is a general formula for the ML estimates but it does not have a closed form solution. To get around this, one can use the Interactive Reweighted Least Squares (IRLS) method also called the Newton Raphson method to find these parameters.

In the case of the undirected model things get a little more complicated. The

s do not decouple and so they can not be calculated separately. To solve this we can use KL divergence which is a method that considers the distance between two distributions.## EM Algorithm

Let us once again consider the above example only this time the data that was supposed to be collected was not done so properly. Instead of having complete data about every random variable at every step some data points are missing.

\begin{tabular}{ccccc}
**Observation** & ** & **** & **** & **** **

1 & & & &

2 & & & &

3 & & & &

4 & & & &

& & ... & &

n & & & &
\end{tabular}

In the above table the x values represent data as before and the Z values represent missing data (sometimes called latent data) at that point. Now the question here is how do we calculate the values of the parameters

One thing to note here is that in the case of missing values we now have multiple local maxima in the likelihood function and as a result the EM Algorithm does not always reach the global maximum. Instead it may find one of a number of local maxima. Multiple runs of the EM Algorithm with different starting values will possibly produce different results since it may reach a different local maxima.

Define the following types of likelihoods:

complete log likelihood = .

incomplete log likelihood = .

### Derivation of EM

We can rewrite the incomplete likelihood in terms of the complete likelihood. This equation is in fact the discrete case but to convert to the continuous case all we have to do is turn the summation into an integral.

Since the z has not been observed that means that

is in fact a random quantity. In that case we can define the expectation of in terms of some arbitrary density function .#### Jensen's Inequality

In order to properly derive the formula for the EM algorithm we need to first introduce the following theorem.

For any **convex** function f:

This can be shown intuitively through a graph. In the (Fig. \ref{img:JensenIneq.eps}) point A is the point on the function f and point B is the value represented by the right side of the inequality. On the graph one can see why point A will be smaller than point B in a convex graph.

For us it is important that the log function is **concave** and so we must inverse the sign on the equation. Jensen's inequality is used in step (\ref{UseJensen}) of the EM derivation but for the concave log function.

#### Derivation

The function

is called the axillary function and it is used in the EM algorithm. For the EM algorithm we have two steps that we repeat one after the other in order to get better estimates for and . As the steps are repeated the parmeters converge to a local maximum in the likelihood function.**E-Step**

**M-Step**

#### Notes About M-Step

Since the second part of the equation is only a constant with respect to

, in the M-step we only need to maximise the expectation of the complete likelihood. The complete likelihood is the only part that still depends on .#### Notes About E-Step

In this step we are trying to find an estimate for

. To do this we have to maximise .It can be shown that

. So, replace with .But

is the lower bound of so that means that is in fact the maximum for . We can therefore see that we only need to do the E-Step once and then we can use that result for each repetition of the M-Step.

From the above results we can find that we have an alternative representation for the EM algorithm. We can reduce it to:

**E-Step**

Find only once.

**M-Step**

Maximise with respect to .

The EM Algorithm is probably best understood through examples.

#### EM Algorithm Example

Suppose we have the two independent and identically distributed random variables:

In our case

has been observed but has not. Our task is to find an estimate for . We will try to solve the problem first without the EM algorithm. Luckily this problem is simple enough to be solveable without the need for EM.We take our derivative:

And now we can try the same problem with the EM Algorithm.

E-Step

M-Step

Now we pick an initial value for

. Usually we want to pick something reasonable. In this case it does not matter that much and we can pick . Now we repeat the M-Step until the value converges.And as we can see after a number of steps the value converges to the correct answer of 0.2. In the next section we will discuss a more complex model where it would be difficult to solve the problem without the EM Algorithm.

### Mixture Models

In this section we discuss what will happen if the random variables are not identically distributed. The data will now sometimes be sampled from one distribution and sometimes from another.

#### Mixture of Gaussian

Given

We would like to find:

We have no missing data here so we can try to find the parameter estimates using the ML method.

And then we need to take the log to find

It is actually easier to apply the EM algorithm. The only thing is that the EM algorithm works with missing data and here we have all of our data. The solution is to introduce a latent variable z. We are basically introducing missing data to make the calculation easier to compute.

Now we have a data set that includes our latent variable

:We can calculate the joint pdf by:

Let,

& if &
\end{tabular} \right.
Now we can write

and

We can write the joint pdf as:

From the joint pdf we can get the likelihood function as:

Then take the log and find the log likelihood:

In the E-step we need to find the expectation of

For now we can assume that

In M-step, we have to update our data by assuming the expectation is fixed

Taking partial derivatives of the complete log likelihood with respect to the parameters and set them equal to zero, we get our estimated parameters at (t+1).

We can verify that the results of the estimated parameters all make sense by considering what we know about the ML estimates from the standard Gaussian. But we are not done yet. We still need to compute

in the E-step.We can now combine the two steps and we get the expectation

Using the above results for the estimated parameters in the M-step we can evaluate the parameters at (t+2),(t+3)...until they converge and we get our estimated value for each of the parameters.

The mixture model can be summarized as:

- In each step, a state will be selected according to .
- Given a state, a data vector is drawn from .
- The value of each state is independent from the previous state.

A good example of a mixture model can be seen in this example with two coins. Assume that there are two different coins that are not fair. Suppose that the probabilities for each coin are as shown in the table.

\begin{tabular}{|c|c|c|}

\hline & H & T

coin1 & 0.3 & 0.7

coin2 & 0.1 & 0.9

\hline

\end{tabular}

We can choose one coin at random and toss it in the air to see the outcome. Then we place the con back in the pocket with the other one and once again select one coin at random to toss. The resulting outcome of: HHTH \dots HTTHT is a mixture model. In this model the probability depends on which coin was used to make the toss and the probability with which we select each coin. For example, if we were to select coin1 most of the time then we would see more Heads than if we were to choose coin2 most of the time.

## Hidden Markov Models

In a Hidden Markov Model (HMM) we consider that we have two levels of random variables. The first level is called the hidden layer because the random variables in that level cannot be observed. The second layer is the observed or output layer. We can sample from the output layer but not the hidden layer. The only information we know about the hidden layer is that it affects the output layer. The HMM model can be graphed as shown in Figure \ref{fig:HMM}.

In the model the

For the HMM our data comes from the output layer:

We can now write the joint pdf as:

We can use

to represent the i,j entry in the matrix A. We can then define:We can also define:

Now, if we take Y to be multinomial we get:

The random variable Y does not have to be multinomial, this is just an example. We can combine the first two of these definitions back into the joint pdf to produce:

We can go on to the E-Step with this new joint pdf. In the E-Step we need to find the expectation of the missing data given the observed data and the initial values of the parameters. Suppose that we only sample once so

. Take the log of our pdf and we get:Then we take the expectation for the E-Step:

If we continue with our multinomial example then we would get:

So now we need to calculate

Let .

Let .

We could use the sum product algorithm to calculate these equations but in this case we will introduce a new algorithm that is called the - Algorithm.

### The - Algorithm

We have from before the expectation:

As usual we take the derivative with respect to

and then we set that equal to zero and solve. We obtain the following results (You can check these...) . Note that for we are using a specific that is given.For

we can think of this intuitively. It represents the proportion of times that state i prodices . For example we can think of the multinomial case for y where:Notice here that all of these parameters have been solved in terms of

and . If we were to be able to calculate those two parameters then we could calculate everything in this model. This is where the - Algorithm comes in.Now due to the Markovian Memoryless property.

Define

and as follows:Once we have

and then computing is easy.To calculate

For :

Where we begin with:

Then for

:Where we now begin from the other end:

Once both

and have been calculated we can use them to find:

## Sampling Methods

A fundamental problem in statistics has always been to find the expectation of

with respect to .In many cases this integral is quite difficult to compute directly and so certain methods have been developed in an attempt to estimate the value without the need to actually do the integration. One such method is the Monte Carlo method where the integral is estimated by a sum.

We can also find the mean and standard deviation for the estimate. In fact, the mean is the correct mean for

.So the only setback is that we have to be able to sample from

.### Sampling from Uniform

Let us assume that we want to sample from UNIF(0, 1). How would we go about doing this? Sampling from a uniform distribution that is truly random is very difficult. We are only going to look at the way it is done on a computer. On a computer we have a function that looks something like **Pseudo Random Numbers**.

For the rest of this section we will assume that we know how to draw from a uniform distribution. It will provide us with the 'randomness' that is needed by each of our algorithms.

### Inverse Method for Sampling

This is a two step method:

Step 1: Draw .

Step 2: Compute where .

**Example:**

Suppose that we want to draw a sample from where . We need to first find and then .

Now we can generate our random sample

from by:The

The major problem with this approach is that we have to find and for many distributions, such as the Gaussian for instance, it is too difficult to find the inverse of .

Here

is too hard to compute.### Box-Muller

This is a method for sampling from a Gaussian Distribution. This is a unique method and it only works for this particular distribution.

- Draw and from a UNIF(0, 1).
- Accept the above values only if . Otherwise repeat the above step until this condition is met.
- Calculate and :

- and are now independent and distributed N(0,1).

### Rejection Sampling

Suppose that we want to sample from

and we are not in the Gaussian case and we can not find . Suppose also that there exists a that is easy to sample from. For instance the is easy to sample from. Then if there exists a such that for all x then we can use rejection sampling.To present the problem intuitively we can observe the graph (Fig. \ref{fig:RejectSample}) where the top line represents

and the bottom line represents . We have in our example two points and . Consider first . From the graph we can tell that values around will be sampled more often under than under and since we are sampling from we expect to see many more samples in this region than we actually need. We therefore must reject most of the values drawn from around and only keep a few. If we now look at we see that the number of samples that are drawn from that region and the number we need are in fact much closer and we only have to reject a few of the values that are sampled from that area. So the question is: when we get an from how do we know if we should keep the value or if we should throw it away? In regions where is far from we must reject many more values than in regions where is close to . This is how rejection sampling works.- Draw from .
- Accept with probability and reject the value otherwise.
- The accepted values are now a random sample from your .

**Proof:**

What we need to show is that .

We know from the definition of the algorithm that

.We have proven that rejection sampling works. But this type of sampling has some disadvantages too. For one thing we can look at the acceptance rate

. For a large k we are discarding many values and so this method is very inefficient. Also, there are distributions where it would be difficult to find a suitable or that would allow us to sample from .

**Example of Rejection Sampling:**

Suppose we want to sample from a .

Now we must find a

and a . We can use the as our because it is easy to sample from. For the value of we must find the maximum value of . In this case:So we will choose our

for this example and now we can run the algorithm.- Draw from .
- Accept with probability and reject the value otherwise.
- The accepted values are now a random sample from .

### Importance Sampling

We return once again to our problem of finding the expectation of

.which can be approximated by:

We can try to rewrite the first equation so that we sample from

and not .which can be approximated by:

The algorithm is as follows:

- Draw from .
- Find the weight for , .
- The set can now be used to estimate .

The main disadvantage is that in many cases we can have the weight very close to zero and the sample itself will become almost useless. We need to have a

and a that are very close for this algorithm to be more efficient. This technique does turn out to be unbiased but due to the problem of low weights the variance tends to be very high.### Greedy Importance Sampling

This method, as the name indicates, is somewhat similar to the method in the previous section. The difference from the previous algorithm is that we need to find the maximum point in

. The algorithm works as follows:- Draw from .
- Move from towards the maximum point in and sample along the way. The new sample set must have the property that where is the weight of the sample .
- The set can now be used to estimate .

This method is more difficult to compute but it is unbiased and has the advantage that it also has a low variance. In short this algorithm is more complex than the regular Importance Sampling but it has a lower variance.

### Markov Chain Monte Carlo

This is best explained with an example. Say that we have a series random variables that each have a boolean state. Between two states

and we have a set of transition probabilities.- If then with probability .
- If then with probability .
- If then with probability .
- If then with probability .

We can say that the initial value for

. From that we can deduce that:- and
- and
- and
- ...
- and

We can see that the probabilities converge to 0.5 each. This is called the equilibrium probability distribution for this particular MCMC. If we have a

we want to sample from but don't know how, there may be a way to make that the equilibrium probability for a MCMC and then sample from the tail end of the chain to get our random samples.

#### Metropolis Algorithm

We would like to sample from some

and this time use the metropolis algorithm, which is a type of MCMC, to do it. In order for this algorithm to work we first need a number of things.- We need some staring value . This value can come from anywhere.
- We need to find a value that comes from the function .
- We need the function to be symmetrical. .
- We also need .

Once we have all of these conditions we can run the algorithm to find our random sample.

- Get a staring value .
- Find the value from the function .
- Accept with the probability .
- If the is accepted it becomes the new x value.
- After a large number of accepted values the series will converge.
- When the series has converged any new accepted values can be treated as random samples from .

The point at which the series converges is called the 'burn in point'. We must always burn in a series before we can use it to sample because we have to make sure that the series has converged. The number of values before the burn in point depends on the functions we are using since some converge faster than others.

We want to prove that the Metropolis Algorithm works. How do we know that is in fact the equilibrium distribution for this MC? We have a condition called the detailed balance condition that is sufficient but not necessary when we want to prove that is the equilibrium distribution.

**Theorem 3**
If and is the transformation matrix for the MC then is the equilibrium distribution. This is called the Detailed Balance Condition.

**Proof of Sufficiency for Detailed Balance Condition:**

Need to show:

We need to show that Metropolis satisfies the detailed balance condition. We can define

as follows:Then,

Therefore the detailed balance condition holds for the Metropolis Algorithm and we can say that

is the equilibrium distribution.**Example:**

Suppose that we want to sample from a .

Now define

where and . This type of is called a random walk. We can select any from the range of x as a starting value. Then we can calculate a y value based on our function. We will accept the y value as our new with the probability . Once we have gathered many accepted values, say 10000, and the series has converged we can begin to sample from that point on in the series. That sample is now the random sample from a .#### Metropolis Hastings

As the name suggests the *Metropolis Hastings* algorithm is related to the *Metropolis* algorithm. It is a more generalized version of the *Metropolis* algorithm where we no longer require the condition that the function be symmetric. The algorithm can be outlined as:

- Get a staring value . This value can be chosen at random.
- Find the value from the function . Note that no longer has to be symmetric.
- Accept with the probability . Notice how the acceptance probability now contains the function .
- If the is accepted it becomes the new value.
- After a large number of accepted values the series will converge.
- When the series has converged any new accepted values can be treated as random samples from .

To prove that *Metropolis Hastings* algorithm works we once again need to show that the Detailed Balance Condition holds.

**Proof:**

If then this reduces to the *Metropolis* algorithm which we have already proven. Otherwise,

Which means that the Detailed Balance Condition holds and therefore

is the equilibrium.### Gibbs Sampling

Suppose we want to sample from the joint probability

but we cannot sample from it directly. We can however sample from the conditional distribution . The process can be defined as follows:- Start with a randomly chosen where .
- Once we have an we can find an by sampling from the conditional probability distribution.

- We continue this process until the burn-in point, after which we are sampling from .

This process may seem different from the previous methods but in fact *Gibbs Sampling* is only a special case of *Metropolis Hastings*. Suppose one would like to sample from where . Propose a and a . We can define the function from the *Metropolis Hastings* algorithm as . In *Gibbs Sampling* we do not reject any of the values we sampled because our rejection probability is:

This quality makes *Gibbs Sampling* quite popular because we use everything we sample.

**Example:**

Say that we want to sample from:
N \left[
\left( \begin{array}{c}
u_1

u_2 \end{array} \right),
\left( \begin{array}{cc}
\Sigma_{11} & \Sigma_{12}

\Sigma_{21} & \Sigma_{22} \end{array} \right)
\right ]
And we know that we can find the parameters with:

For this example suppose we want to sample from :

0 \end{array} \right),
\left( \begin{array}{cc}
1 & L

L & 1 \end{array} \right)
\right ]
Then we can calculate:

The sampling process is then done with:

### Independence Chains

In the *Metropolis Hastings* algorithm we used a to get the next values in the sample. Suppose now that . In other words, the function does not depend on . The acceptance probability would now become .

#### Bayesian Inference

In Bayesian Inference we would like to find *Metropolis Hastings*. Our acceptance probability would become:

Now, recall that using Bayes rule we can write

. We also know that . From that we can rewrite the above Bayes formula as .Therefore, to sample from the posterior in a Bayesian Inference we can simply propose a

from the prior and then we accept with probability:**Example:**

We would like to sample from:

and from:

The problem is that we are missing the parameter

. We do however know that . The best way to sample from the above distribution is to start with a randomly chosen and accept with probability . When we reject we simply use the previous value again. This method also requires a burn in time so we must wait before we can begin sampling.

### Simulated Annealing

Consider the general optimization problem *temperature* and it determines the shape of the distribution. As increases the distribution expands but as then the that we sample from the are very close to the global min.

**Note:** If is the minimum of then is also the maximum of .

We can define the steps to the problem as:

- Start with a randomly chosen and set to a large value.
- Propose a from the function .
- Accept the value with probability .
- Decrease the value of and return to step 1.

But what exactly does

mean? We can estimate each of these probabilities with the expression we introduced earlier.We are now left with two possible cases. If

then which is desired and so we will always accept the new . Otherwise, if we may not accept the new value and we can see that as then will also go to zero and so the acceptance probability will go to zero.For this method we can write down a rough algorithm:

Start with and consider a set of values.

for to

\hspace*{20pt}for to

\hspace*{20pt}Propose a from .

\hspace*{20pt}

\hspace*{20pt}if

\hspace*{30pt}

\hspace*{20pt}else

\hspace*{30pt}

\hspace*{20pt}endif

endfor

endfor

### Bootstrap

In data analysis we usually have an observed set of data

from a probability distribution and we have an estimator for our parameter of interest . In general it would be useful to know the distribution of our . For instance, if the estimator has a larger variance then we know that it is not very accurate. The problem is that it is not always easy to determine the distribution of an estimator. Ideally we would like to be able to sample directly from and then for each sample of size we can calculate a . In this way a number of estimates for can be found and their distribution can be determined from the samples.**For Example:**

Based on

we can try to determine the distribution of .However, this idea is unrealistic because we don't know *Bootstrap* idea comes in. Assume that we have a set of data from an unknown distribution . To simulate sampling from we can resample with replacement from the set of data points. Every sample we get in this way we can use to estimate a different . We can use this method to find a collection of parameters from which we can:

- Find the expectation of .

- Find the variance of .

- Find a confidence interval.

- Find the bias.

- Bias correction.

At first, this method seems strange. We are sampling from the sample itself and not the distribution. However, it has been shown that the *Bootstrap* method does indeed work and can provide more useful information on top of what the raw data could have provided.

This kind of *Bootstrap* is called the *Naive Bootstrap* because the values are sampled one at a time independently and this destroys any kind of correlation in the initial distribution. The correct *Bootstrap* method requires the selection of blocks of data in order to keep the correlation in the data. These blocks are sampled with replacement and may overlap.