# An Improved Algorithm of Drift Compensation for Olfactory Sensors
## 3 Materials and Methods
### Sensor Drift Compensation Algorithm
\(h(x_{Ti})\) is the \(i-\) th point obtained by the source data through a single hidden layer neuron. \(x_{Si}^{T}\) represents the transpose of the source domain data samples, \(N_{S}\) and \(N_{T}\) are the number of source domain samples and the number of target domain samples, respectively. Transforming the constrained optimization problem in Equation (6) into an unconstrained optimization problem, Equation (7) can be obtained  
\[\underset{\beta}{min}\frac{\lambda}{2}MMD^{2}(X_{S},X_{T})+\frac{1}{2}Tr\Big{[} (X_{S}-H_{S}\beta)^{T}\Lambda_{C}(X_{S}-H_{S}\beta)\Big{]}+\frac{1}{2}\|\beta\| ^{2} \tag{7}\]  
In the formula \(\Lambda_{C}\) is a diagonal matrix, the element on the main diagonal is the parameter \(C\), and \(Tr\) represents the trace of a matrix. Domain adaptation algorithms that minimize reconstruction error are different from traditional autoencoders or Boltzmann machines, which use backpropagation to update parameters to learn the weights of the input and output layers. Domain adaptation algorithms focus on the mapping of source and target domains to new domains rather than feature extraction. Equation (4) can be calculated from Equation (8):  
\[MMD^{2}(X_{S},X_{T})=\|\frac{1}{N_{S}}\sum\limits_{i=1}^{N_{S}}\varnothing(x_{Si })-\frac{1}{N_{T}}\sum\limits_{j=1}^{N_{T}}\varnothing(x_{Tj})\|^{2}_{\mathcal{ H}} \tag{8}\]  
Defining \(H= \begin{bmatrix}H_{S}\\ H_{T}\end{bmatrix} \), Equation (8) can be rewritten as Equation (9):  
\[MMD^{2}(X_{S},X_{T})=Tr\Big{[}\beta^{T}H^{T}DH\beta\Big{]} \tag{9}\]  
where \(D\in R^{(N_{S}+N_{T})\times(N_{S}+N_{T})}\) is the matrix of MMD, which can be defined in the form of Equation (10):  
\[D_{ij}= \begin{cases}\frac{1}{N_{T}^{2}}\ \ if\ i,j>N_{S}\\ \frac{1}{N_{S}^{2}}\ \ if\ i,j\leq N_{S}\\ \frac{1}{N_{S}N_{T}}\ \textit{others}\end{cases}  \tag{10}\]  
To sum up, Equation (7) can be rewritten as Equation (11):  
\[\underset{\beta}{min}\frac{\lambda}{2}Tr\Big{[}\beta^{T}H^{T}DH\beta\Big{]}+ \frac{1}{2}Tr\Big{[}(X_{S}-H_{S}\beta)^{T}\Lambda_{C}(X_{S}-H_{S}\beta)\Big{]} +\frac{1}{2}\|\beta\|^{2} \tag{11}\]  
For convenience, the source domain data and the target domain data are combined as \(X^{*}\). The first \(N_{S}\) is the source domain data \(X_{S}\), and the last \(N_{T}\) is 0. \(\Lambda=dig(C,C,\cdots,C,0,\cdots,0)\), where the number of \(C\) is the number of source domain data, and the number of 0 is the number of target domain data. In this way, Equation (11) can be transformed into Equation (12):  
\[\underset{\beta}{min}\frac{\lambda}{2}Tr\Big{[}\beta^{T}H^{T}DH\beta\Big{]}+ \frac{1}{2}Tr\Big{[}(X^{*}-H\beta)^{T}\Lambda(X^{*}-H\beta)\Big{]}+\frac{1}{ 2}\|\beta\|^{2} \tag{12}\]Obviously, Equation (12) is a convex optimization problem, finding its gradient and letting it equal to 0, we can get:  
\[\lambda H^{T}DH\beta+H^{T}\Lambda H\beta-H^{T}\Lambda X^{*}+\beta=0 \tag{13}\]  
Finally, we get the output layer matrix, as shown in Equation (14):  
\[\beta=H^{T}\Big{(}I_{N_{S}+N_{T}}+(\Lambda+\lambda D)HH^{T}\Big{)}^{-1}\Lambda X ^{*} \tag{14}\]  
Because the input data are mapped to the hidden layer using the kernel function, the mapped data H cannot be directly and explicitly obtained, and the output layer matrix \(\beta\) cannot be directly calculated. However, since the kernel matrix \(K=HH^{T}\), it can be directly calculated. The data after domain transformation, as shown in Equations (15) and (16):  
\[X_{IS} =K(X_{S},X)\big{(}(\Lambda+\lambda D)K+I_{N_{S}+N_{T}}\big{)}^{-1} \Lambda X^{*} \tag{15}\] \[X_{IT} =K(X_{T},X)\big{(}(\Lambda+\lambda D)K+I_{N_{S}+N_{T}}\big{)}^{-1} \Lambda X^{*} \tag{16}\]  
where \(X=\big{[}X_{S}^{T},X_{T}^{T}\big{]}^{T}\) is the combination of source domain data and target domain data. The obtained new source domain data and target domain data are sent as input into the SWKELM model, the semi-supervised classifier is trained, and the trained classifier is used to predict the X_IT data, and finally the accuracy is calculated.