# CoSiWiNeT: A Clock Synchronization Algorithm for Wide Area IIoT Network
## 4 CoSiWiNeT: RANSAC-Based Clock Synchronization Algorithm  
Unpredictable communication network performance often leads to delayed, incomplete, or missing data packet transfers, resulting in a noisy clock offset and delay measurements. Curve or line fitting algorithms for clock synchronization are being explored for their ability to deal with the effects of network bursts and surges. However, curve fitting becomes challenging in the presence of noisy data, outliers, and missing data. Furthermore, data may not always fit the model precisely because of measurement or detection noise and intra-class variability. A clock synchronization algorithm called the CoSiWiNeT based on RANSAC was proposed, which performs better than LS methods in the presence of outliers or noisy offset data. CoSiWiNeT converts an estimation problem in the continuous domain into a selection problem in the discrete domain. For example, if there are 100 data points to fit a line, LS would use 2 points, whereas there are \({}^{100}\)C\({}_{2}\) or 4950 possible ways to fit a line. CoSiWiNeT chooses the best suitable pair among 4950 options. To do so, the CoSiWiNeT algorithm iteratively executes two steps: hypothesis formation and evaluation of framework, as shown in Algorithm 1. ```
1:Measured offset, \(U=\{\Theta_{i}\}\)
2:Function \(f\) computes model parameters \(p\) given a sample \(S\) from \(U,f(S):S\gets p\)
3:Minimum number of offset samples required to estimate model parameters, \(n\)
4:Maximum number of iterations, \(k\)
5:Error tolerance threshold, \(t\)
6:Minimum valid consensus threshold, \(d\)
7:Outputs:
8:Model parameters fit best to offset data, \(p^{*}\)
9:\(k=0\)
10:Best error \(E^{*}\)= large value
11:for\(i\gets i:1:k\)do
12:Hypothesis formation:
13:Select random sample space \(S_{k}\subset U\) with sample size \(\left|S_{k}\right|=n\)
14:Compute model parameters \(p_{k}=f(S_{k})\)
15:Hypothesis Evaluation:
16:for every sample \(S^{\prime}_{k}\
eq S_{k}\)do
17: Compute error \(E_{k}\)
18:if\(E_{k}<t\)then
19: Inliner list \(I^{*}\gets CS^{\prime}_{k}\)
20:endif
21:endfor
22:if\(I^{*}>d\)then
23: Corresponding model parameters \(p^{\prime}\gets p_{k}\)
24:if Best error \(E^{*}\) > Current error \(E_{k}\)then
25: Best error \(E^{*}=E_{k}\)
26: Best fit model parameters \(p^{*}\gets p^{\prime}\)
27:endif
28:endif
29:endfor
```  
**Algorithm 1** CoSiWiNeT algorithm  
In the first step, a minimal sample set of size \(n\) is randomly selected from the input dataset of size \(N\). Then, the model parameters are computed based on an earlier selected random sample set. The elements of a random set (\(n\)) are smallest sufficient to determine the model parameters in contrast with other methods such as LS, where the model parameters are estimated using all the data points (\(N\)) with appropriate weights. In the evaluation step, the error function is calculated for each data point. CoSiWiNeT checks which elements of the entire dataset are consistent with the selected model whose parameters have been estimated in the first step. The data elements which do not fit the model within some error threshold attributed to the effect of noise are called outliers (\(O\)). Thus, CoSiWiNeT selects the remaining data elements that support the current hypothesis. The set of such elements is called consensus set (\(CS\)) or inlier set (\(I\)) [31; 32]. CoSiWiNeT executes iteratively for \(k\) times and terminates when the probability of finding a better-ranked \(CS\) drops below a certain threshold. The total number of inliers typically decides the ranking. A \(CS\) that contains more elements is ranked better than a \(CS\) that contains fewer elements. The total number of iterations that are required to ensure, with a probability \(p\), that at least one random sample produces an inlier set that is free from \"real\" outliers is given by  
\[k=\frac{log(1-p)}{log(1-(1-\frac{I}{O})^{n})}. \tag{3}\]  
It can be learned that \(k\) can be very large if \(p\) gets closer to one.