# Unsupervised Classification of Human Activity with Hidden Semi-Markov Models
## 2 Materials and Methods
### Model Description  
A diagram illustrating the mathematical model used in this work is shown in Figure 1. The system is modelled as a set of states \(S_{i}\), which in this context represent the different activities. The system remains in a given state for the duration \(D_{i}\), which is a random variable. Then, it transitions to another state, and the whole process forms a Markov chain. The transition parameters \(\pi_{i,j},\ i,j=1,\ldots,n\) form the transition matrix \(\pi\), where \(\pi_{i,j}\) is the probability of transition from the state \(i\) to the state \(j\). The corresponding observation, or emission variables, denoted by \(y\), form another layer, as shown in Figure 1. An HSMM is fully modelled by the transition probability matrix \(\pi\), observation sequence \(y\) and state duration \(D\), whilst the observations are the accelerometer data, either in raw form or as features extracted from the raw data. An efficient Bayesian inference algorithm has been described in [19] for the HSMM message-passing inference. It is based on the explicit-duration hierarchical Dirichlet process HSMM (HDP-HSMM) and sampling algorithms for efficient posterior inference. The HDP-HSMM(\(\zeta,\gamma,H,G\)) with \(n\) states, parameters \(\zeta,\gamma\) and observation and duration parameter distributions \(H\) and \(G\) can be summarised as follows:  
\[\delta \sim GEM(\zeta) \tag{1}\] \[\pi_{i}\overset{iid}{\sim}DP(\gamma,\delta)\hskip 36.135 pti=1,2,\ldots\] \[(\theta_{i},\omega_{i})\overset{iid}{\sim}H\times G\] \[S_{i} \sim\pi_{S_{i-1}}\] \[D_{i} \sim g(\omega_{i})\] \[y_{t_{i}:t_{i}^{i}}\overset{iid}{\sim}h(\theta_{i})\]  
where the first line represents the sampling of the variable \(\delta\) using a Dirichlet process with a single parameter \(\zeta\)--a special case of the Dirichlet process called stick-breaking distribution (GEM) [20]. The next line denotes the sampling of the transition parameter from a Dirichlet process (DP), with parameters \(\gamma\) and \(\delta\) (_iid_ means independent and identically distributed random variables). The third line represents the sampling of the parameters \(\theta\) and \(\omega\) from distributions \(H\) and \(G\) (which we specify later). Then, the subsequent lines represent the sampling of the state \(S_{i}\), duration \(D_{i}\) and observation \(y\), sampled from distributions \(g\) and \(h\) (which are conjugate distributions of \(G\) and \(H\)). The HSMM model parameters were estimated by using Bayesian inference. Bayesian inference is ideal in this application, as it allows the incorporation of prior knowledge on observed activity patterns through an appropriate prior distribution. For the prior distribution of the state duration, we used a Poisson distribution, with parameter \(\lambda\) equal to the mean duration of each state. However, for the acceleration values which we observed, we assumed a Gaussian prior with mean \(\mu\) and variance \(\sigma^{2}\), since an HSMM with observations modelled by a Gaussian performs better in comparison to other distributions in classifying activities [21]. The parameters were estimated in a Bayesian manner with a hierarchical Dirichlet process, which allows for the number of states to be unknown and estimated by the algorithm [19]. The maximum number of states \(n\) and the maximum duration \(D_{max}\) for each state can be set to reduce training time. However, for the Bayesian calculations, we need to know the \(\lambda\) in the Poison distribution, which might be difficult to be accurately directly estimated from the observed data. The usual approach in that case is to use a conjugate prior of the Poisson distribution, which is the Gamma distribution, instead of the Poisson distribution. The Gamma distribution has two parameters \(\alpha\) and \(\beta\), which are related to \(\lambda\) with the relationship \((\lambda=\alpha/\beta)\). This relationship can help us to estimate \(\lambda\).