# Adaptive Learning Approach for Human Activity Recognition Using Data from Smartphone Sensors
## 2 Adaptive Activity Recognition
### Proposed System Architecture
#### 2.1.3 Algorithm for Classification
The logarithmic likelihood function, which describes the observation in a state \(s\), is as follows:  
\[l(o,\mu,\sigma)=-\frac{n}{2}\ln(2\pi)-\frac{1}{2}\ln(|\sigma|)-\frac{1}{2}(o- \mu)^{T}\sigma^{-1}(o-\mu). \tag{2}\]  
Now one can define  
\[L(o,\mu,\sigma,\gamma)=\sum_{i=1}^{N}e^{-l(o,\mu_{i}\sigma_{i})+\ln(\gamma_{i})} \tag{3}\]  
Let us calculate \(\ln L\) derivatives according to \(\mu\), \(\sigma\), and \(\gamma\) using the Lagrange multiplier method regarding the constraint \(\sum_{i=1}^{N}\gamma_{i}=1\):  
\[(\ln L)^{\prime}_{\mu_{i}}=\phi_{i}^{i}(o-\mu_{i})\sigma_{i}^{-1}, \tag{4}\]  
\[(\ln L)^{\prime}_{\sigma_{i}}=\phi_{i}^{i}\Big{(}\sigma_{i}^{-1}(o-\mu_{i})(o -\mu_{i})^{T}\sigma_{i}^{-1}-\sigma_{i}^{-1}\Big{)}, \tag{5}\]  
\[\left(\ln L+\lambda\left(1-\sum_{i=1}^{N}\gamma_{i}\right)\right)^{\prime}_{ \gamma_{i}}=\frac{\phi_{i}^{i}}{\gamma_{i}}-\lambda, \tag{6}\]  
where \(\phi_{i}^{i}=\frac{e^{-l(o,\mu_{i}\tilde{\sigma}_{i})+\ln(\gamma_{i})}}{\sum_ {i=1}^{N}e^{-l(o,\mu_{i}\tilde{\sigma}_{i})+\ln(\gamma_{i})}}\), and \(\lambda\) is the Lagrange multiplier. The equations with derivatives \(\sum_{t}(\ln L)^{\prime}_{\mu}=0\), \(\sum_{t}(\ln L)^{\prime}_{\sigma}=0\), and \(\sum_{t}\left(\ln L+\lambda\left(1-\sum_{i=1}^{N}\gamma_{i}\right)\right)^{ \prime}_{\gamma_{i}}=0\) are solved with respect to \(\mu\), \(\sigma\) and \(\gamma\) as follows:  
\[\tilde{\mu_{i}}=\frac{\sum_{t=1}^{T}\phi_{i}^{i}o_{t}}{\sum_{t=1}^{T}\phi_{t} ^{i}}, \tag{7}\]  
\[\tilde{\sigma_{i}}=\frac{\sum_{t=1}^{T}\phi_{i}^{i}(o_{t}-\widehat{\mu_{i}})( o_{t}-\widehat{\mu_{i}})^{T}}{\sum_{t=1}^{T}\phi_{t}^{i}}, \tag{8}\]  
\[\gamma_{i}=\frac{1}{T}\sum_{t=1}^{T}\phi_{t}^{i}. \tag{9}\]  
The derived Formulas (7) and (8) can be used in offline MLE estimation, given a fixed data sample. The complexity of such calculations is linear. However, if applied for online estimation, their complexity becomes of second order because the calculations must be carried out over the entire dataset at each appearance of new data (i.e., each re-estimation of parameters). Therefore, in order to enable online estimation, recursive formulas for re-estimating the HMM parameters are needed. It is not difficult to observe that the estimates obtained by Formulas (7) and (8) at \(t-1\) and \(t\) satisfy the following recursive relations:  
\[b(\mu_{s},\sigma_{s})=\frac{1}{\sqrt{(2\pi)^{n}|\sigma_{s}|}}e^{-\frac{1}{2}(o- \mu_{s})^{T}\sigma_{s}^{-1}(o-\mu_{s})}, \tag{1}\]0  
\[b(\mu_{s},\sigma_{s})=\frac{1}{\sqrt{(2\pi)^{n}|\sigma_{s}|}}e^{-\frac{1}{2}(o- \mu_{s})^{T}\sigma_{s}^{-1}(o-\mu_{s})}, \tag{1}\]1  
where the state probabilities \(\gamma_{t}^{i}=\frac{1}{t}\sum_{i=1}^{t}\phi_{t}^{i}\) are calculated as  
\[b(\mu_{s},\sigma_{s})=\frac{1}{\sqrt{(2\pi)^{n}|\sigma_{s}|}}e^{-\frac{1}{2}(o- \mu_{s})^{T}\sigma_{s}^{-1}(o-\mu_{s})}, \tag{1}\]2  
There are two parts of the RHMM parameter estimation algorithm. The first part uses Equations (10)-(12) to estimate initial parameters given a small fixed-size observation set. During the initial training process, \(\widehat{\mu}\) and \(\widehat{\sigma}\) represent fixed parameter values used in estimation. The algorithm's stability is ensured by initial training. However, usually, it is not difficult to have a large enough dataset to initialize parameter values that correctly identify and classify the observations. This crucial size is about 100-200; moreover, the proposed online algorithm is easily adapted for offline estimation. The second part uses Equations (10)-(12) to re-estimate parameters based on classified observations. The values of the previous steps \(\widehat{\mu}\) and \(\widehat{\sigma}\) are denoted by \(\mu_{t-1}^{i}\) and \(\sigma_{t-1}^{i}\) in the re-estimation process. A Bayes classifier is used to classify the observations into groups. The proposed algorithm will be applied to human activity recognition. In this scenario, data from sensors are used to train and initialize the HMM model. Then, the trained model will be used for continuous activity recognition and real-time model parameter updating.