# On IoT-Friendly Skewness Monitoring for Skewness-Aware Online Edge Learning
## 3 Three Versions of Running Skewness and Efficient Calculation Methods
### The CSS Calculation Method from GNU Scientific Library  
The first third-party method involved in our comparison is from GNU Scientific Library (GSL) (the source code is located in the folder rstat within the zipped library gsl-latest.tar.gz at [[https://www.gnu.org/software/gsl/](https://www.gnu.org/software/gsl/)]([https://www.gnu.org/software/gsl/](https://www.gnu.org/software/gsl/)), accessed on 10 August 2021). GSL is a well-known numerical library covering a wide range of mathematical routines in C language. At the time of this research, the stable version is GSL-2.6, released on 20 August 2019. To facilitate the contrast between the GSL method and ours, we abstract GSL's source code into an equation system using our notations, as shown in (11).  
\[\begin{split}&\bar{x}_{n+1}=\bar{x}_{n}+\frac{x_{n+1}-\bar{x}_{n} }{n+1},\qquad\bar{x}_{0}=0,\ n>0\\ & M2_{n+1}=M2_{n}+\frac{n\cdot(x_{n+1}-\bar{x}_{n})^{2}}{n+1}, \quad M2_{0}=0\\ & M3_{n+1}=M3_{n}-3\cdot\frac{x_{n+1}-\bar{x}_{n}}{n+1}\cdot M2_{ n}+\frac{n\cdot(n-1)}{(n+1)^{2}}\cdot(x_{n+1}-\bar{x}_{n})^{3},\quad M3_{0}=0\\ & S_{n+1}=\frac{(\sqrt{n})^{3}}{n+1}\cdot\frac{M3_{n+1}}{(\sqrt{ M2_{n+1}})^{3}}\end{split} \tag{11}\]  
where \(\bar{x}_{n}\) and \(S_{n}\), respectively, represent the sample mean and sample skewness of \(n\) observation values \(x_{1},x_{2},\ldots,x_{n}\). \(\bar{x}_{n+1}\) and \(S_{n+1}\) are the updated sample mean and sample skewness after a new value \(x_{n+1}\) joins the observation dataset. (The equation of \(S_{n+1}\) is extracted from the code Line 184 to Line 186 of rstat.c). As for the auxiliary variables \(M2\) and \(M3\), GSL generally defines \(Mk_{n}=\sum_{i=1}^{n}(x_{i}-\bar{x}_{n})^{k}\) (in gsl_rstat.h).  
When it comes to designing algorithms from formulas, a popular and practical strategy is to save reusable intermediate results to minimize computational workloads and to speed up programs. Thus, compared to Equation System (11), the source code of GSL employs a set of temporary variables to reuse the intermediate results, as shown in Algorithm 1. It should be noted that the equation systems (4) and (11) require \(n>2\) and \(n>1\) respectively to avoid the error of division by zero. To ensure \"apple-to-apple\" performance comparison among different algorithms, we intentionally set \(n>2\) in the if statement in Algorithm 1.  
**Input:** The incoming observation value \(x\).  
**Global Parameters:** The current amount of observations \(n\), the sample mean _mean_ of the current observation values, and the current values of auxiliary variables \(M2\) and \(M3\).  
**Output:** Cumulative sample skewness of the \(n+1\) observations.  
```
1:\(n\gets n+1\)\(\triangleright\) The new observation is counted.
2:\(delta\_\gets x-mean\)\(\triangleright\) i.e., \((x_{n+1}-\bar{x}_{n})\) in Equation System (11).
3:\(delta\_n\gets delta/n\)\(\triangleright\) i.e., \(\frac{n\cdot(x_{n+1}-\bar{x}_{n})^{2}}{n+1}\) in Equation System (11).
4:\(term\gets delta\_\)\(+delta\_n*(n-1)\)\(\triangleright\) i.e., \(\frac{n\cdot(x_{n+1}-\bar{x}_{n})^{2}}{n+1}\) in Equation System (11).
5:\(mean\gets mean+delta\_n\)
6:\(M3\gets M3-3*delta\_n*M2+term1*delta\_n*(n-2)\)
7:\(M2\gets M2+term1\)\(\triangleright\) The if statement is originally \"if\((n>0)\)\" in GSL's source code.
8:if\(n>2\)and (optionally) not in a chunk then
9:\(fac\leftarrow(\sqrt{n-1})^{3}/n\)
10:return\(fac*M3/(\sqrt{M2})^{3}\)
11:else
12:return 0\(\triangleright\) Numeric 0 or Boolean false depending on contexts.
13:endif
```  
**Algorithm 1** Cumulative Sample Skewness in GSL