# Mixture Model Parameter Estimation

## Maximum Likelihood Estimator

MLE is defined as the parameters that maximize the [likelihood function](https://yikun-chi.gitbook.io/notes/statistics-method-notes/mixture-model-setup#likelihood) defined in pthe revious chapter given the observation (treating observation as fixed)

$$
\begin{equation\*}
\hat{\theta} = \argmax\_\theta L(\theta | y\_{1:T})
\end{equation\*}
$$

### Label Switching

In a mixture model, the components can permute and still have the same distribution. (e.g.: If there are two normal distribution states, just swap the parameters for state 1 with parameters for state 2). It has more severe consequences when using Bayesian parameter estimation and inference techniques, and bootstrapping. In that case, relabeling components to make them consistent over iterations becomes necessary.&#x20;

## Method 1: Numerical Optimization of the Likelihood&#x20;

Numerical optimization can iteratively find the minimum of a function. Look at the below trivial example

{% code overflow="wrap" %}

```r
GaussMix2 <- function(par, y){
# set parameter values 
p1 <- par[1]; p2 <- 1 - par[1]
m1 <- par[2]; m2 <- par[3]; s1 <- par[4]; s2 <- par[5]
# return negative log-likelihood 
-sum(log(p1 * dnorm(y, mean = m1, sd = s1) + p2 * dnorm(y, mean = m2, sd = s2)))

}

# define starting values 
pstart <- c("p1"=.5,"m1"=1,"m2"=2,"s1"=.5,"s2"=.5)

# create a fake mixture Gaussian 
y_obs <- c(rnorm(1000,0,1), rnorm(1000,5,2))

# run optim 
opt <- optim(par=pstart,fn=GaussMix2,y=y_obs,lower=c(0,-Inf,-Inf,0,0),upper=c(1,rep(Inf,4)),method="L-BFGS-B")

# read output 
opt$par 

#        p1         m1         m2         s1         s2 
# 0.50854228 0.03480772 5.02649332 0.98800874 2.03568627 

```

{% endcode %}

* *GaussMix2* is a function that takes in parameters and observation y. It then finds the minimum of the negative log-likelihood:$$\sum\_{t=1}^N (-1) \* (\pi\_1f\_1(y\_t) + \pi\_2f\_2(y\_t))$$
* *optim* arguments passed are&#x20;
  * *par = pstart*: Initial values for the parameters to be optimized over&#x20;
  * *fn = GaussMix2*: The function to be minimized. The first argument is the vector of parameters that needs to be minimized&#x20;
  * *y=y\_obs:* the additional argument required for *fn*
  * The requirement of mixing probabilities has to be between 0 and 1 and positive standard deviation is enforced through *lower* and *upper* arguments&#x20;

## Expectation Maximization (EM) Algorithm&#x20;

### EM Goal and Use Canse:&#x20;

Parameter estimation for problems with missing or latent data and maximum likelihood estimation would be easy if we knew the missing or latent values.

### EM Idea&#x20;

Impute expected values for the latent data/states and then estimate parameters by optimizing the joint likelihood of the parameters given the observation and imputed latent states. Then we can separately perform the following step&#x20;

* Expectation step: (Re)compute the expectation of the complete-data log likelihood (The likelihood of observed data + hidden states).&#x20;
* Maximization step: Expected complete-data log-likelihood is a function of parameters $$\theta$$, but not the unobserved state $$S$$. So we can find the optimal values of $$\theta$$ by maximizing the expected complete-data log-likelihood.&#x20;

### EM in Mixture Model&#x20;

The joint log-likelihood function (complete-data log-likelihood) can be decomposed into two parts:&#x20;

$$
\begin{equation\*}
\log f(y\_{1:T}, s\_{1:T}|\theta) = \sum\_{t=1}^T \log P(s\_t|\theta\_{pr}) + \sum\_{t=1}^T \log f(y\_t|s\_t, \theta\_{obs})
\end{equation\*}
$$

* Compared to the model [log-likelihood](https://yikun-chi.gitbook.io/notes/statistics-method-notes/mixture-and-latent-class-model/em-algorithm-and-mixtures-of-generalized-linear-models#model-likelihood), the expression is the joint log-likelihood of data and the hidden states. The hidden states are normally unavailable in the data. &#x20;
* The first part is the probability of $$S\_t$$ (the state at the time $$t$$) given the mixing probability parameter $$\theta\_{pr}$$. The second part is the probability of $$Y\_t$$ (observation at time $$t$$) given the state at that time and the density parameters $$\theta\_{obs}$$.&#x20;

The expected value of complete-data log-likelihood w\.r.t all possible hidden states across each observation  ($$s\_{1:T}$$) given the observations and initial/previous guess parameters $$\theta'$$:&#x20;

$$
\begin{align\*}
Q(\theta, \theta')
& \equiv E\_{s\_{1:T}|y\_{1:T}, \theta'}\left\[ \log f(y\_{1:T}, s\_{1:T}|\theta)\right] \\
& = \sum\_{s\_{1:T}\in S^T} P(s\_{1:T} | y\_{1:T},\theta')\log f(y\_{1:T}, s\_{1:T}|\theta)\\
& = \sum\_{s\_{1:T}\in S^T}P(s\_{1:T} | y\_{1:T},\theta') \* \log (P(s\_{1:T}|\theta\_{pr}) \* f(y\_{1:T}|s\_{1:T},\theta\_{obs})\\
& = \sum\_{t=1}^T \sum\_{i=1}^N \gamma\_t(i) \log P(s\_t = i|\theta\_{pr}) + \sum\_{t=1}^T \sum\_{i=1}^N \gamma\_t(i)\log f(y\_t | s\_t = i, \theta\_{obs}) \\
\gamma\_t(i) & = p(s\_t=i|y\_t, \theta')
\end{align\*}
$$

* The expectation is a function of the true parameter and the initial guess parameters&#x20;
* The first equal sign is applying the definition of expectation.&#x20;
* The second equal sign is applying the definition of complete joint log-likelihood.&#x20;
* Notice $$s\_{1:T}\in S^T$$ is just a shorthand for all possible permutations of hidden states from time 1 to T.&#x20;
* $$\gamma$$ function is the posterior probabilities of the state given the initial/previous guess parameters.&#x20;

So overall, the EM algorithm for the mixture model can be described as&#x20;

1. Start with an initial set of parameters $$\theta'$$
2. Repeat until convergence:&#x20;
   1. Compute the [posterior probabilities](https://yikun-chi.gitbook.io/notes/statistics-method-notes/mixture-model-setup#posterior-probabilities) $$\gamma$$and the [log-likelihood](https://yikun-chi.gitbook.io/notes/statistics-method-notes/mixture-model-setup#likelihood) $$l(\theta'|y\_{1:T})$$as described in the previous chapter.&#x20;

   2. Obtain new estimates&#x20;
      * $$\begin{equation\*} \hat{\theta}*{pr} = \overset{\text{argmax}}{\theta*{pr}} \sum\_{t=1}^T \sum\_{i=1}^N \gamma\_t(i)\log P(s\_t = i|\theta\_{pr}) \end{equation\*}$$
      * $$\begin{equation\*} \hat{\theta}*{obs} = \overset{\text{argmax}}{\theta*{obs}} \sum\_{t=1}^T \sum\_{i=1}^N \gamma\_t(i)\log f(y\_t|s\_t = i,\theta\_{obs}) \end{equation\*}$$

   3. Set $$\theta'=(\hat{\theta}{pr}, \hat{\theta}{obs})$$

The convergence can be checked by 1) the norm of the parameter guesses or 2) the relative increase in the log-likelihood. This can be done in R through package *depmixS4.*&#x20;

In summary, EM algorithm for Mixture Class Model alternative between&#x20;

1. computing the expected component membership probabilities or the posterior state probabilities
2. optimizing the response model parameters conditional on these expected component memberships

## Handling Constraint

Linear (in)equality constraint can be represented as&#x20;

$$
I \leq A\theta\leq U
$$

And numerical optimization can be performed using packages such as *Rsolnp, Rdonlp2, depmixS4* &#x20;

## Starting Values

We have two choices, especially concerning EM algorithm.&#x20;

1. Start with density parameter values for components (obtained through methods such as K-means) and equal value for mixing probability&#x20;
2. Start with posterior probability (such as random assignment of each case to a component, resulting in 1 hot vector for posterior probability) and directly go to maximization step in the first iteration&#x20;
