## 3 The Digital Twin in Cycling
### Implementation of the Digital Twin
#### 3.2.3 Digital Twin Prediction  
Although a lot of AI prediction tools exist nowadays (e.g., linear regression, Bayesian inference), the preliminary tests showed that the application of these tools is not possible on limited hardware. Therefore, we searched for our own mathematical prediction model that is simple and fast enough to prevail over these hardware limitations. Indeed, the interval training session consists of speed and recovery intervals. Typically, a cyclist in the speed interval tries to overcome the proposed \(\overline{HR}_{spe}\) as fast as possible. When the cyclist's \(HR_{n}\) in the \(n\)-th interval is under the \(\overline{HR}_{spe}\), the corresponding TRIM values are negative but become positive, when the \(HR_{n}\) increases over the \(\overline{HR}_{spe}\) in the same interval. The opposite is true for the recovery interval. Consequently, two TRIM areas can be distinguished by an integration of the \(HR_{n}\) by time slices \(\Delta t_{n}\) for \(n\in[1,T]\), i.e., \(plus\) and \(minus\), where the former denotes the positive and the latter the negative values of the integrated TRIMP. For the integration, the trapezoidal rule is applied as follows:  
\[area=\sum_{n=1}^{T}\left(\frac{\overline{HR}_{n+1}-\overline{HR}_{n}}{2}\cdot \Delta t_{n}-\overline{HR}_{pred}\cdot\Delta t_{n}\right), \tag{4}\]  
where \(area\) denotes either \(plus\) or \(minus\), and \(\overline{HR}_{pred}\) is predicted average HR. Let us mention that the second term in Equation (4) denotes the TRIMP value needed for overcoming the observed time slice to the proposed average heart rate, and must be subtracted from the total TRIMP value.  
The motivation behind predicting the intensity value \(\overline{HR}_{pred}\) is to make up for the deficit of TRIMP before reaching the \(\overline{HR}_{spe}\) proposed in a training plan by increasing this value in the later phases of the speed interval. It holds also in the opposite way, i.e., to expend an overproduced TRIMP before reaching the \(\overline{HR}_{rec}\) by decreasing this value in the latter phases of the recovery interval.  
Let us emphasize that the proposed prediction is directed toward determining the predicted value of the heart rate by fixing the duration of the time intervals. The goal of the prediction is to balance the TRIMP values of \(plus\) and \(minus\) by considering the expected, remaining number of the training intervals \(n_{expect}\), expressed mathematically as:  
\[plus+n_{expect}\cdot\overline{HR}_{pred}(t_{prop}-t_{n})=minus, \tag{5}\]  
from which the predicted \(\overline{HR}_{pred}\) is derived as:  
\[\overline{HR}_{pred}=\frac{minus-plus}{n_{expect}(t_{prop}-t_{n})}, \tag{6}\]and the \(n_{expect}\) is calculated according to deductive calculus as:  
\[n_{expect}=\frac{t_{prop}\cdot n}{t_{n}}. \tag{7}\]  
Let us notice that \(t_{prop}\in\{t_{spe},t_{rec}\}\).  
A pseudo-code of the prediction component of the proposed solution is illustrated in Algorithm 3, from which it can be seen that the TRIMP areas _plus_ and _minus_ are first updated by the \(\Delta TRIMP\) value obtained during the last time slice. Then, either the proposed value of average heart rate is taken when the proposed \(\overline{HR}_{prop}\) value is not reached yet, or the predicted value is calculated according to Equation (6).  
```
0:\(\Delta TRIMP\), \(\overline{HR}\), \(\overline{HR}_{prop}\), \(\Delta T\), minus, plus
0:\(\overline{HR}_{pred}\)
1:if\(\Delta TRIMP<0\)then
2:\(minus=minus+\Delta TRIMP\)
3:else
4:\(plus=plus+\Delta TRIMP\)
5:endif
6:if\(\overline{HR}<\overline{HR}_{prop}\)then
7:\(\overline{HR}_{pred}=\overline{HR}_{prop}\); \(\triangleright\) under/over proposed \(HR\)
8:else
9:\(\overline{HR}_{pred}=\overline{HR}_{prop}+\frac{minus-plus}{n_{expect}\cdot \Delta T}\)\(\triangleright\) Equation (6)
10:endif
```  
**Algorithm 3** Digital twin prediction calculation.