public void RunTest3()
{
// Example XOR problem
double[][] inputs =
{
new double[] { 0, 0 }, // 0 xor 0: 1 (label +1)
new double[] { 0, 1 }, // 0 xor 1: 0 (label -1)
new double[] { 1, 0 }, // 1 xor 0: 0 (label -1)
new double[] { 1, 1 } // 1 xor 1: 1 (label +1)
};
// Dichotomy SVM outputs should be given as [-1;+1]
int[] labels =
{
1, -1, -1, 1
};
// Create a Kernel Support Vector Machine for the given inputs
KernelSupportVectorMachine svm = new KernelSupportVectorMachine(new Gaussian(0.1), inputs[0].Length);
// Instantiate a new learning algorithm for SVMs
SequentialMinimalOptimization smo = new SequentialMinimalOptimization(svm, inputs, labels);
// Set up the learning algorithm
smo.Complexity = 1.0;
// Run the learning algorithm
double error = smo.Run();
Assert.IsFalse(svm.IsProbabilistic);
Assert.AreEqual(0, error);
Assert.AreEqual(-1, svm.Weights[0]);
Assert.AreEqual(1, svm.Weights[1]);
Assert.AreEqual(-1, svm.Weights[2]);
Assert.AreEqual(1, svm.Weights[3]);
// Instantiate the probabilistic learning calibration
var calibration = new ProbabilisticOutputCalibration(svm, inputs, labels);
// Run the calibration algorithm
double loglikelihood = calibration.Run();
Assert.IsTrue(svm.IsProbabilistic);
Assert.AreEqual(-1.0986109988055595, svm.Weights[0]);
Assert.AreEqual(1.0986109988055595, svm.Weights[1]);
Assert.AreEqual(-1.0986109988055595, svm.Weights[2]);
Assert.AreEqual(1.0986109988055595, svm.Weights[3]);
// Compute the decision output for one of the input vectors,
// while also retrieving the probability of the answer
double probability;
int decision = svm.Compute(inputs[0], out probability);
// At this point, decision is +1 with a probability of 75%
Assert.AreEqual(1, decision);
Assert.AreEqual(0.74999975815069375, probability, 1e-10);
Assert.AreEqual(0, error);
Assert.AreEqual(5.5451735748925355, loglikelihood);
}