# Leveraging Artificial Intelligence in Blockchain-Based E-Health for Safer Decision Making Framework
## 4 Implementation
### ML Detection Implementation
#### 4.5.2 SVM Decision Function  
An RBF-kernel SVM's decision function returns values close to (-1,1) and is generally described in the math equation below:  
\[\begin{array}{l}h^{*}=(\mathbf{x^{*}}\phi(x))+w_{0}^{*}\\ h^{*}=\sum\limits_{i\in P_{5}}a_{i}^{*}u_{i}\cdot K(\mathbf{x}_{i}-\mathbf{x}) +w_{0}^{*}\end{array} \tag{2}\]  
In the above representation, \(h^{*}\) is the decision function, \(a_{i}^{*}\) is the value of the coefficients, \(u_{i}\) is the support vector output of the kernel function \(K\), \(x\) is the new data point vector, \(\mathbf{x}_{i}\) is the support vector, and \(w_{0}^{*}\).  
Since the kernel function of this SVM implementation was the RBF function, it has the mathematical representation as follows:  
\[K(\mathbf{x},\mathbf{x^{\prime}})=\mathbf{exp}(-\gamma\|\mathbf{x}-\mathbf{x^{ \prime}}\|^{2}) \tag{3}\]  
In the above equation, \(\gamma\) is the gamma value acquired previously. The RBF kernel function returns the product of negative gamma with the Frobenius norm of two input vectors. Function \(\mathbf{exp}()\) is the exponent of Euler number, \(e\).  
\[\|\mathbf{x},\mathbf{x^{\prime}}\|_{F}=\sqrt{\sum\limits_{i=1}^{m}\sum \limits_{j=1}^{n}\mid a_{i,j}\mid^{2}} \tag{4}\]  
The above math representation shows that the Frobenius norm, \(F\), is the square root of the summation of two input vectors, \(x,x^{\prime}\), squared difference, \(a\).  
Smart contracts did not provide complex decimal math libraries support for exp() function, nor did they allow execution for decimal numbers to be the base or the exponent of exponentials. This study came with the workaround to use the Taylor Maclaurin series to calculate the exponential of decimals:  
\[f(x)=\sum\limits_{n=0}^{\infty}\frac{x^{n}}{n!} \tag{5}\]  
The above math representation shows that the Taylor Maclaurin series is the summation of a number \(x\) raised to power \(n\) divided by the factorial of that \(n\).  
Taylor Maclaurin series is an approximation calculation, which means that calculating \(e^{x}\) using the summation of an infinite number \(n\) iterations will progressively produce a value closer to the actual value.  
The following Algorithm 4 shows the steps of calculating the Taylor Maclaurin series in fifty rounds of calculations, as it was better suited for execution efficiency while obtaining more precise values. Algorithm 5 shows the steps of calculating the Frobenius norm of Equation (4).  
```
0:\(x\) decimal number
0:\(result\) decimal number
1:procedureTaylor
2:\(term\leftarrowresult\gets n\gets 1\)
3:for\(i\gets 1\) in range(50) do
4:\(term\leftarrow(term*x)/n\)
5:\(n\gets n+1\)
6:\(result\gets result+term\)
7:return\(result\)
```  
**Algorithm 4** Approximate \(e^{x}\) using Taylor Maclaurin series  
```
0:\(x\) decimal support vector,
0:\(z\) decimal input vector
0:\(sum\) decimal number
1:procedureFrom
2:\(sum\gets 0\)
3:for\(i\gets 1\) to length of \(x\)do
4:\(y\leftarrow(x[i]-z[i])^{2}\)
5:\(sum\gets y+sum\)
6:return sqrt(\(sum\))
```  
**Algorithm 5** Frobenius norm of two vectors  
Equation (3) of the kernel function is calculated by using the Frobenius norm and the Taylor Maclaurin series, as shown by Algorithm 6.  
```
0:\(x\) decimal support vector,
0:\(z\) decimal input vector,
0:\(g\) gamma value
0:Output:
0:\(y\) decimal number
1:procedureRBF
2:\(norm\leftarrow\text{Fnorm}(x,z)^{2}\)\(\triangleright\) call Fnorm procedure
3:\(y\leftarrow(g*norm)*(--1)\)
4:return Taylor(\(y\))\(\triangleright\) call Taylor procedure
```  
**Algorithm 6** RBF kernel of support vector and input vector  
It is worth noting that the square root step in Algorithm 5 line 7 cancels the squaring step in Algorithm 6, line 2. For this reason, these steps were omitted in the smart contract code implementation.  
Algorithm 7 applies Equation (2), where it takes an integer vector input and returns a value > 0 or <0. If the output of this function is larger than zero, a positive number, it has a label of class 1. Alternatively, if the output is less than zero, a negative number, it has a label of class 0.