## 2 Methods
### Modelling of the Outcome, Risk and Uncertainties  
This subsection describes the methods to predict the blood glucose outcome, the hypoglycemia and the hyperglycemia risks of actions based on a predefined set of variables. Uncertainty estimation using Bayesian techniques will be described with these methods. The blood glucose outcome, hyperglycemia and hypoglycemia risks were predicted in this paper by using a deterministic feedforward neural networks (FFNN) and two Bayesian feedforward neural networks (BFNN) with the same structure. FFNN and BFNN are types of artificial neural networks which are constructed by neurons organized into layers. The networks estimate the outcome and risks based on inputs such as the intensity of physical activity, historical blood glucose and dietary information (carbohydrate intake) before the physical activity. The structure of the deterministic FFNN and the two BFNN implemented in this paper are similar and can be found in Figure 2. For demonstration, we include only the food amount (carbohydrate intake) as the input for the networks. It is assumed that other conditions are the same every time physical activity is conducted. Information from the input also flows through hidden nodes, which are nonlinear functions of the carbohydrate intake amount \(D\):  
\[h_{j}=\sigma\left(w_{j}^{(1)}D+b_{j}^{(1)}\right), \tag{9}\]  
where \(h_{j}\) is the output value of the \(j^{\text{th}}\), \(j=1,..,n\) hidden node, \(w_{j}\) and \(b_{j}\) are the weights and biases. The function \(\sigma\) is a rectified linear unit activation function (ReLu):  
\[\sigma(x)=\max(0,x). \tag{10}\]  
The model output \(y\), (either predicted risks or blood glucose outcome), is calculated from the outputs of the hidden layers by a linear function:  
\[\hat{y}=w^{(2)}D+b^{(2)}, \tag{11}\]  
where \(w^{(2)}\) and \(b^{(2)}\) are the weight and bias for the output layer. The task of training the outcome model is to find the optimal values of of \(w\) and \(b\) such that the following mean squared error (MSE) cost function is minimized:  
\[MSE=\frac{1}{N}\sum_{i=1}^{N}(\hat{y}_{i}-y_{i})^{2}, \tag{12}\]  
where \(N\) is the number of training samples, \(\hat{y}_{i}\) is the predicted value and \(y_{i}\) is the actual blood glucose score for each training sample i. Training of the outcome model was done using least square estimation and implemented through the Pytorch library [35]. Two models were built to predict the hypoglycemia and hyperglycemia risks based on a Bayesian feedforward neural network in order to capture the uncertainties in the risk prediction. The networks have similar structure like the outcome prediction model with six hidden nodes. However, the weight and bias of the hidden and output layers are represented by Gaussian distributions instead of discrete numbers. The prior distribution of all the parameters were chosen to be normal distributions with mean of zero and standard deviation of one. The task of training the hyperglycemia and hypoglycemia models is to determine the posterior distribution for the weights and biases using Bayesian inference. The posterior distribution was estimated by using the stochastic variational inference method [36] and implemented with Pyro [37]. For each weight and bias \(z\), a family of distributions is generated with each distribution characterized by parameters \(\theta\). Then, an approximation of the posterior distribution \(p_{\theta}(z)\) is conducted by selecting from this family a distribution that is closest to the true posterior \(p(z|y,x)\), where \(x\) and \(y\) are the training data set. The selection is conducted by finding the optimal set of parameters \(\theta\) that minimize the Kullback-Leibler divergence, which is a measurement of how two probability distributions are different from each other [36; 38]:  
\[ \begin{split} KL(p_{\theta}(z),p(z|y,x))&=\int_{z}p _{\theta}(z)log\left(\frac{p_{\theta}(z)}{p(z|y,x)}\right)\\ &=\int_{z}p_{\theta}(z)log\left(\frac{p(z)}{p(y|z,x)p(z)}\right)+ log(p(y|x))=-L(\theta)+log(p(y|x)),\end{split}