## 3 Methods
### Function Selection Mode
#### 3.3.1 Offline P300 Detector Training and Optimization
In the training stage, SVM finds a separating hyperplane which minimizes the training error and maximize the margin of separation between classes in a kernel-induced feature space by solving the constrained optimization problem,  
\[ \begin{split}\text{Minimize}\ \frac{1}{2}\parallel\mathbf{w} \parallel^{2}+C\sum\limits_{i=1}^{L}\xi_{i}\\ \text{subject to}\ y_{i}\left(\mathbf{w}^{i}\varphi\left( \mathbf{x}_{i}\right)+b\right)-1+\xi_{i}\geq 0;\ \xi_{i}\geq 0,\forall i\end{split}  \tag{9}\]  
where \(\mathbf{w}\) is the weight vector of the hyperplane, \(b\) is its bias, \(\xi_{i}\) are slack variables representing training errors, and \(C\) is the penalty weight. The optimal bias \(b\) can be computed using the Kuhn-Tucker conditions of (9). In testing stage, the label of a test data \(\mathbf{x}\) is computed by the decision function:  
\[D\left(\mathbf{x}\right)=\sum\limits_{i=1}^{L}\alpha_{i}y_{i}K\left(\mathbf{ x}_{i},\mathbf{x}\right)+b \tag{10}\]  
where \(\alpha_{i}\) are Lagrange multipliers. If \(D>0\), the test data is classified as a P300 data; otherwise, it is classified as non-P300 data. The RBF function in (8) is also used as the kernel for SVM. P300 Detector - Optimization  
The parameters in KFDA that need to be optimized include the number of eigenvectors \(d\) and kernel parameter \(\sigma\) (letting \(\sigma_{k}=\sigma\)). SVM has also two parameters to optimize, including \(C\) and \(\sigma\) (letting \(\sigma_{s}=\sigma\)). These parameters are optimized using a grid search and 10-run two-fold cross validation procedure. For each subject, a data set \(S\) containing 400 EEG data has been collected, including 80 P300 and 320 non-P300 data. The parameter \(d\) should be searched in the range of 1-\(L\). Since two-fold cross validation is used, only half of the set \(S\) is used for training. Therefore, \(L\) = 200. However, according to our preliminary test, the values of optimal \(d\) for all subjects were smaller than 10. When \(d\) was larger than 10, the classification accuracies would not change significantly or worsen. Thus, we only search the best \(d\) in the range of 1-10; otherwise, the optimization would be computationally expensive. The penalty weight \(C\) has three possible values in the experiment, including 10, 100, and 1000. Both \(\sigma_{k}\) and \(\sigma_{s}\) are searched over the set \(\{2^{-4},2^{-3},2^{-2},2^{-1},1,2^{2},2^{3},2^{4},2^{5},2^{6},2^{7},2^{8},2^{9 },2^{10},2^{11},2^{12},2^{13},2^{14},2^{15}\}\), where the size of the set is 20. Therefore, the four parameters \((d,C,\sigma_{k},\sigma_{s})\) have 120,008 possible combinations in total,where \(12000=10\times 3\times 20\times\ 20\). For each combination, the following two-fold cross validation of 10 independent runs [40] is performed. In each run, half of the 400 EEG data in \(S\) are randomly selected as the elements of the set \(S_{1}\), and the remaining ones form another set, \(S_{2}\). In either \(S_{1}\) or \(S_{2}\), the numbers of P300 data and non-P300 data are 40 and 160, respectively. Initially, the set \(S_{1}\) is used for training the P300 detector, and \(S_{2}\) is used for testing. Then, \(S_{2}\) is used for training, and \(S_{1}\) becomes the test set. A classification rate is computed based on the two testing results. After the 10-run two-fold cross validation procedure is complete, we obtain an average classification rate over the 10 runs for the current parameter combination. The 10-run two-fold cross validation procedure is repeated until all the 12,000 parameter combinations are tested. Finally, the optimal parameter combination gives the highest average classification rate. The optimal parameters for each subject are recorded, and are listed in Table 1. In Section 4, the reported experimental results are the best 10-run two-fold cross validation accuracies.