User:Gezzer898/sandbox

Cryptanalysis
The Baum-Welch algorithm is often used to estimate the parameters of HMMs in deciphering hidden or noisy information and consequently is often used in Cryptanalysis. In data security an observer would like to extract information from a data stream without knowing all the parameters of the transmission. This can involve reverse engineering a channel encoder. HMMs and as a consequence the Baum-Welch algorithm have also been used to identify spoken phrases in encrypted VoIP calls. In addition HMM cryptanalysis is an important tool for automated investigations of cache-timing data. It allows for the automatic discovery of critical algorithm state, for example key values.

=Description= A Hidden Markov Model describes the joint probability of a collection of 'hidden' and observed discrete random variables. It relies on the assumption that the $$i^{th}$$ hidden variable given the $$(i-1)^{th}$$ hidden variable is independent of previous hidden variables and the current observation variables depend only on the current hidden state.

The Baum-Welch algorithm uses the well known EM algorithm to find the maximum likelihood estimate of the parameters of a hidden Markov model given a set of observed feature vectors.

Let $$X_t$$ be a discrete hidden random variable with $$N$$ possible values. We assume the $$P(X_t|X_{t-1})$$ is independent of time $$t$$. We can present this information as a time independent stochastic transition matrix $$A=\{a_{ij}\}=P(X_t=j|X_{t-1}=i)$$
 * The initial state distribution (i.e. when $$t=1$$) is given by $$\pi_i=P(X_1 = i)$$


 * The observation variables $$Y_t$$ can take one of $$K$$ possible values. The probability of a certain observation vector at time $$t$$ for state $$j$$ is given by: $$b_j(y_t)=P(Y_t=y_t|X_t=j)$$ $$B=\{b_{ij}\}$$ is a $$K$$ by $$N$$ matrix.

An observation sequence is given by $$Y= (Y_1=y_1,Y_2=y_2,...,Y_t=y_t)$$

Thus we can describe a hidden Markov chain by $$\theta = (A,B,\pi)$$. The Baum-Welch algorithm finds $$\theta^* = \max_{\theta} P(Y|\theta)$$. (i.e. the HMM parameters $$\theta$$ that maximise the probability of the observation.)

Algorithm
Set $$\theta = (A, B, \pi)$$ with random initial conditions. They can also be set using prior information about the parameters if it is available.

Forward Procedure
Let $$\alpha_i(t)=P(Y_1=y_1,...,Y_t=y_t,X_t=i|\theta)$$, the probability of seeing the $$y_1,y_2,...,y_t$$ and being in state $$i$$ at time $$t$$. This is found recursively:
 * 1) $$\alpha_i(t)=\pi_i b_i(y_1)$$
 * 2) $$\alpha_j(t+1)=b_j(y_{t+1}) \sum_{i=1}^{N}\alpha_i(t) a_{ij}$$

Backward Procedure
Let $$\beta_i(t)=P(Y_{t+1}=y_{t+1},...,Y_{T}=t_{T}|X_t=i,\theta)$$ that is the probability of the ending partial sequence $$y_{t+1},...,y_{T}$$ given starting state $$i$$ and time $$t$$. We calculate $$\beta_i(t)$$ as,


 * 1) $$\beta_i(T)=1$$
 * 2) $$\beta_i(t)=\sum_{j=1}^N \beta_j(t+1) a_{ij} b_j(y_{t+1})$$

Update
We can now calculate the temporary variables:
 * $$\gamma_i(t)=P(X_t=i|Y,\theta) = \frac{\alpha_i(t)\beta_i(t)}{\sum_{j=1}^N \alpha_j(t)\beta_j(t)}$$

which is the probability of being in state $$i$$ at time $$t$$ given the observed sequence $$Y$$ and the parameters $$\theta$$
 * $$\xi_{ij}(t)=P(X_t=i,X_{t+1}=j|Y,\theta)=\frac{\alpha_i(t) a_{ij} \beta_j(t+1) b_j(y_{t+1})}{\sum_{i=1}^N \sum_{j=1}^N \alpha_i(t) a_{ij} \beta_j(t+1) b_j(y_{t+1})}$$

which is the probability of being in state $$i$$ and $$j$$ at times $$t$$ and $$t+1$$ respectively given the observed sequence $$Y$$ and parameters $$\theta$$.
 * $$\theta$$ can now be updated:

which is the expected frequency spent in state $$i$$ at time $$1$$. which is the expected number of transitions from state i to state j compared to the expected total number of transitions away from state i. where $$ 1_{X_t=x_k}= \begin{cases} 1, & \text{if} y_t=v_k\\ 0, & \text{otherwise}\\ \end{cases} $$ is an indicator function and $$b_i^*(k)$$ is the expected number of times the output observations have been equal to $$v_k$$ while in state $$i$$ over the expected total number of times in state $$i$$.
 * $$\pi_i^* = \gamma_i(1)$$
 * $$a_{ij}^*=\frac{\sum^{T-1}_{t=1}\xi_{ij}(t)}{\sum^{T-1}_{t=1}\gamma_i(t)}$$
 * $$b_i^*(k)=\frac{\sum^T_{t=1} 1_{y_t=v_k} \gamma_i(t)}{\sum^T_{t=1} \gamma_i(t)}$$

These steps are now repeated iteratively until a desired level of convergence.

Note: It is possible to over-fit a particular data set. That is $$P(Y|\theta_{final})>P(Y|\theta_{true})$$. The algorithm also does not guarantee a global maximum

=Example= Suppose we have a chicken from which we collect eggs at noon everyday. Now whether or not the chicken has laid eggs for collection depends on some unknown factors that are hidden. We can however (for simplicity) assume that there are only two states that determine whether the chicken lays eggs. Now we don't know the state at the initial starting point, we don't know the transition probabilities between the two states and we don't know the probability that the chicken lays an egg given a particular state. To start we first guess the transition and emission matrices.

We then take set of observations (E = eggs, N = no eggs): NN, NN, NN, NN, NE, EE, EN, NN, NN

The next step is to estimate a new transition matrix.

Thus the new estimate for the S1 to S2 transition is now $$\frac{0.22}{2.3898}=0.0921$$. We then calculate the S2 to S1, S2 to S2 and S1 to S1 transition probabilities and normalize so they add to 1. This gives us the updated transition matrix.

Next, we want to estimate a new emission matrix,

This allows us to calculate the emission matrix as described above in the algorithm, by adding up the probabilities for the respective observed sequences. We then repeat for if N came from S1 and for if N and E came from S2 and normalize.

To estimate the initial probabilities we assume all sequences start with the hidden state S1 and calculate the highest probability and then repeat for S2. Again we then normalize to give an updated initial vector.

Finally we repeat these steps until the resulting probabilities converge satisfactorily.