Sports teams work with athletes for relatively short seasons.
Athletes can get improving, be in homeostasis or overtrained.
These states are not observed but an observable index of athleticism (maximal vertical jump) is observed.
N: Number of individuals, will be suppressed from probability notation since individuals are assumed to be independent given parameters. It will be reintroduced in the R code.
M: the number of hidden states.
T: the number of discrete time indices
\(P(S_{t}=j|S_{t-1}=m )\): probability given that the state in the previous time period is \(m\) it will become \(j\). M by M conditional probability matrix.
Assume time homogeneity. Therefore
\(P(S_{t}=j|S_{t-1}=m ) \forall t\)
\[\begin{equation} \begin{pmatrix} s_{11} & s_{12} & \cdots & s_{1M}\\ s_{21} & s_{22} & \cdots & s_{2M}\\ \vdots & \vdots & \ddots & \vdots\\ s_{M1} & s_{M2} & \cdots & s_{MM} \end{pmatrix} \end{equation}\]
\[ \sum_{j=1}^{M} P(S_{t}=j |S_{t-1}=m)=1 \]
\[ P(S_{t}=j |S_{t-1}=m)=P(S_{t}=j |S_{t-1}=m,S_{1},\ldots,S_{t-2}) \]
\(P(y_{t} | S_{t}=j)\) : probability/density of \(Y_{t}\) given the hidden state is \(j\) at time index \(t\).
Given the value of \(S_{t}\), \(Y_{t}\) is conditionally independent of previous states and observations.
\[P(Y_{t} | S_{t}=j) = P(Y_{t} | S_{t}=j,S_{1:t-1},Y_{1:t-1}) \]
\(\alpha_{t}(j) = P(Y_{1},\ldots,Y_{t},S_{t}=j)\)
\(\alpha_{t}^{'}(j) = P(S_{t}=j \mid Y_{1},\ldots,Y_{t})\)
\(\beta_{t}(j) = P(Y_{t+1},\ldots,Y_{T} \mid S_{t}=j)\)
\(\gamma_{t}(j)=P(S_{t}=j \mid Y_{1},\ldots,Y_{T})\)
Assume the prior probabilities, \(\delta\), a vector of length M for \(S_{t}\). \(\delta=\{P(S_1=1),\ldots,P(S_{1}=M)\}\).
Assume \(P(Y_{t}|S_{t})\) with known parameters.
At t = 1 \(P(y_{1},S_{1}=j)=P(y_{1}|S_{1}=j)\times P(S_{1}=j)=\alpha_{1}(j)\)
How about \(\alpha_{t}(j)\) when \(t>1\) ? Now we have to think about transitions between states.
The formulation is: \(\alpha_{t}(j)=\sum_{k=1}^{M} P(Y_{t}|S_{t}=j)\times P(S_{t}=j|s_{t-1}=k) \times \alpha_{t-1}(k)\)
This calculation for \(\alpha_{t}(j)\) is called the forward calculation and saves on computation (more on this later).
We need to understand each component and why it works the way it does.
Assume t=2. We are trying to evaluate \(P(y_{2},y_{1},S_{2}=j)=\alpha_{2}(j)\)
The formulation is: \[\begin{equation} \alpha_{2}(j)=\sum_{k=1}^{M} P(y_{2}|s_{2}=j)\times P(s_{2}=j|s_{1}=k) \times \alpha_{1}(k) \end{equation}\]
\[\begin{equation} \sum_{k=1}^{M} P(y_{1},y_{2},s_{2}=j,s_{1}=k) \end{equation}\]
\[\begin{equation} \begin{aligned} P(Y_{2}|Y_{1},S_{2}=j,S_{1}=k)= & P(Y_{2}|S_{2}=j),\\ P(S_{2}=j|S_{1}=k,Y_{1})= & P(S_{2}=j|S_{1}=k) \end{aligned} \end{equation}\]
\[\begin{equation} \sum_{k=1}^{M} P(Y_{2}|S_{2}=j) \times P(S_{2}=j|S_{1}=k)\times \alpha_{1}(k) \end{equation}\]
\[\begin{equation} \alpha_{t}(j)= \sum_{k=1}^{M} P(Y_{t}|S_{t}=j) \times P(S_{t}=j|S_{t-1}=k)\times \alpha_{t-1}(k) \end{equation}\]
We will first simulate the calculation of \(\alpha\) in R which is to be used in calculating \(P(S_{t}=j|Y_{1:t})\)
Note the normalization of \(\alpha\) in the code which makes it \(P(S_{t}=j|Y_{1:t})\)
Same starting value for N individuals at \(t=1\) in Y matrix
Constant standard deviation \(\sigma = 1\)
\(\delta={[P(S_{1}=1),P(S_{1}=2),P(S_{1}=3)]}=\left(\frac{1}{3},\frac{1}{3},\frac{1}{3}\right)\)
\(\Gamma\) is the conditional transition probability matrix. Rows add up to 1.
\(\Gamma[j,k]=P(S_{t}=k|S_{t-1}=j)\)
Once again the \(t\) index is not necessary here since we assume a time homogeneous transition matrix.
Setup an array labelled state that has 2 dimensions N*T elements in first dimension and 3 for the second dimension.
sample randomly N elements between 1:M and insert them into the first N elements of state object’s first dimension and first element of the second dimension. Prior probability \(\delta\).
Correspondingly, insert the value 1 for discrete time unit second element of dimension 2.
Correspondingly, 1:N for the athletes for the third element of dimension 2.
We populate the rest of the state array using the Gamma object. Note how at \(t=2\) of the loop the sample object uses the row indices from \(t=1\) which in turn is used as the row index of Gamma. Double loops over i and t where.
t is the outer loop populating 2nd element of second dimension of state array, indexing time.
i is the inner loop populating 3rd element of second dimension of state array, indexing individuals.
For each of the M hidden states we have a constant mean.
Based on the N*T simulated hidden states we establish the mean.
\(\epsilon_{i,t}\) is simulated assuming normality with means changing according to state j.\(\epsilon_{i,t}=N(\mu_{S_{i,t}},\sigma)\)
\(Y_{i,t}\) is equated to \(\epsilon_{i,t}\) in this oversimplified example. Therefore \(P(Y_{i,t}|S_{t}=j) \sim N(\mu_{j},\sigma)\)
Note that \(Y\) represents panel data with dimensions N (15) T (20).
Convert matrix to data.frame, set the athlete ID to panel data. Each row is an athlete, each column is a week.
Set 3 dimensional emission array elements: \(P(Y_{it}|S_{t}=j)\)
-Athlete identifies the id column of wide data the rest of the columns are pivoted
Column ids are turned into Week (t) identifiers.
alpha_state object identifies the state with the maximum probability of \(P(S_{t}=j|y_{1},\ldots,y_{t})\) from \(\alpha^{'}\)
predicted_state converts it into the long form we compare these values to the simulated values in long object.
\[\begin{equation} \begin{aligned} \beta_{t}(j)= & \sum_{m=1}^{M} P(Y_{t+1},\ldots,Y_{T},S_{t+1}=m \mid S_{t}=j) \\ = & \sum_{m=1}^{M} P(S_{t+1}=m \mid S_{t}=j) \times P(Y_{t+1:T}\mid S_{t+1}=m,S_{t}=j) \\ since\\ = & \sum_{m=1}^{M} \frac{P(S_{t+1}=m,S_{t}=j)}{P(S_{t}=j)}\times \frac{P(Y_{t+1:T,S_{t+1}=m,S_{t}=j})}{P(S_{t+1}=m,S_{t}=j)} \end{aligned} \end{equation}\]
\[\begin{equation} \begin{aligned} & \sum_{m=1}^{M} P(S_{t+1}=m \mid S_{t}=j) P(Y_{t+1:T}\mid S_{t+1}=m,S_{t}=j) \\ & note \\ & P(Y_{((t+1):T)}\mid S_{t+1}=m,S_{t}=j) = \\ & P(Y_{(t+1)}\mid S_{t+1}=m) P(Y_{t+2:T}\mid S_{t+1}=m) \\ & \therefore \\ & \sum_{m=1}^{M} P(S_{t+1}=m \mid S_{t} = j) P(Y_{t+1} \mid S_{t+1}=m) P(Y_{t+2:T} \mid S_{t+1}=m) \end{aligned} \end{equation}\]
\[\begin{equation} \begin{aligned} & \sum_{m=1}^{M} P(S_{t+1}=m \mid S_{t} = j) P(Y_{t+1} \mid S_{t+1}=m) P(Y_{t+2:T} \mid S_{t+1}=m) \\ & \sum_{m=1}^{M} (transition)(emission)(\beta_{t+1}(m))\\ & \beta_{T}(j)=1 \end{aligned} \end{equation}\]
3 objects and 3 indices.
i=\(1,\ldots,N\), \(t=1,\ldots,T\), \(j=1,\ldots,M\)
i=\(1,\ldots,15\), \(t=1,\ldots,20\), \(j=1,\ldots,3\)
set the 3 dimensional array \(\beta\) and set for all individuals \(\beta_{T}(j)=1\). dim(beta)= (15,20,3)
Gamma is transition probability matrix and is time homogeneous and is constant for i. dim(Gamma)=(3,3)
B is the emission matrix and contains the densities of \(y_{i,t} \mid S_{t}=m\) dim(B) = (15,20,3)
beta[i, t, j] <-sum(Gamma[j, ]*B[i, t+1, ]*beta[i, t+1, ])
Gamma[j, ] = \(P(S_{t+1}=1 \mid S_{t}=j),P(S_{t+1}=2 \mid S_{t}=j),P(S_{t+1}=3 \mid S_{t}=j )\)
B[i, t+1, ] = \(P(Y_{i,(t+1)} \mid S_{t+1}=1),P(Y_{i,(t+1)} \mid S_{t+1}=2),P(Y_{i,(t+1)} \mid S_{t+1}=3)\)
beta[i, t+1, ] = \(P(Y_{i,t+2:T} \mid S_{t+1}=1),P(Y_{i,t+2:T} \mid S_{t+1}=2),P(Y_{i,t+2:T} \mid S_{t+1}=3)\)
\[\begin{equation} \sum_{m=1}^{M} P(S_{t+1}=m \mid S_{t} = j) P(Y_{t+1} \mid S_{t+1}=m) P(Y_{t+2:T} \mid S_{t+1}=m) \end{equation}\]
In the explanation below note the conditional independence property.
\(Y_{t+1:T} \perp Y_{1:t} \mid S_{t}=j\), in other words if the state at time \(t\) is known, the history of Y does not provide information for the future uncertainty of Y.
\(P(Y_{t+1:T} \mid S_{t}=j)=P(Y_{t+1:T} \mid S_{t}=j, Y_{1:t})\)
\[\begin{equation} \begin{aligned} \gamma_{t}(j) =& \frac{\alpha_{t}(j)\beta_{t}(j)}{\sum_{m=1}^{M}\alpha_{t}(m)\beta_{t}(m)}\\ =& \frac{P(Y_{1},\ldots,Y_{t},S_{t}=j)P(y_{t+1},\ldots,Y_{T}|S_{t}=j)}{\sum_{m=1}^{M}\alpha_{t}(m)\beta_{t}(m)}\\ =& \frac{P(Y_{1},\ldots,Y_{t},S_{t}=j)P(y_{t+1},\ldots,Y_{T}|S_{t}=j,y_{1},\ldots,Y_{t})}{\sum_{m=1}^{M}\alpha_{t}(m)\beta_{t}(m)}\\ =& \frac{P(Y_{1},\ldots,Y_{T},S_{t}=j)}{\sum_{m=1}^{M}P(Y_{1},\ldots,Y_{T},S_{t}=m)}=\frac{P(Y_{1},\ldots,Y_{T},S_{t}=j)}{P(Y_{1},\ldots,Y_{T})} \end{aligned} \end{equation}\]
\[\begin{equation} \begin{aligned} & P(S_{t}=m \mid S_{t-1}=j)=P(S_{t}=m \mid S_{t-1}=j,Y_{1:t-1})\\ &P(S_{t}=m \mid Y_{1:t-1})= \sum_{j=1}^{M}P(S_{t-1}=j\mid Y_{1:t-1})P(S_{t}=m \mid S_{t-1}=j,Y_{1:t-1})\\ & \sum_{j=1}^{M}\alpha^{'}_{t-1}(j)P(S_{t}=m \mid S_{t-1} = j) \end{aligned} \end{equation}\]