HMMs Predict Ramiz’ Mood
- Ramiz’ mood can be either angry or calm
- His mood affects the food he chooses to eat for dinner
- His dinner can be either pizza, burger, or tacos
- If you tell me the food Ramiz chose to eat every day for a month, we can figure out whether Ramiz was angry or calm.
- In HMMs, Ramiz’ mood is a hidden state, while the dinner he eats are the observations.
For simplicity, we will designate food with numbers.
- Pizza = 3
- Burger = 2
- Tacos = 1
We will designate the hidden states (moods) with letters
Load the environment
library(tidyverse)
library(HMM)
set.seed(123)
First, lets take a look at the food Ramiz ate last month
data$Visible
## [1] 1 2 2 3 3 2 3 3 3 3 3 3 3 3 3 3 3 3 1 3 3 2 3 2 3 3 2 1 3 2 3
First lets try to generate what we think would be a good Hidden Markov Model for Ramiz’ mood and diet.
We can assume both of his moods are equally likely. This translates as equal priors.
priors = c(
A=0.5,
B=0.5
)
We can assume that if he is angry today, he is equally likely to be angry or calm tomorrow. Conversely, if he is calm today, he is equally likely to be angry or calm tomorrow. This would mean he has equal transition probabilities.
# transition probabilities
transition.probabilities = matrix(
nrow = 2,
ncol = 2,
data=rep(0.5, 4)
)
colnames(transition.probabilities) = c('A', 'B')
rownames(transition.probabilities) = c('A', 'B')
Now we have to define the emission probabilities. These are the probabilities that Ramiz will eat a certain type of food for dinner given his mood.
From our time being Ramiz’ friend, we have observed that he is more likely to eat pizza than burgers, and more likely to eat bugers than tacos. We also think he is slightly more likely to eat tacos when he is angry.
# emission probabilities
emission.probabilities = data.frame(
A = c(
0.111,
0.333,
0.555
),
B = c(
0.166,
0.333,
0.500
)
)
Finally, let’s save Ramiz’ previous 31 dinner in a vector
# visible/observed data
visible=data$Visible
As we can see, from data generated from the same Hidden Markov Model that is used as input to both implementations of the Viterbi algorithm, the R-Package and our implementation returns pretty similar results.