// Return parameter of a Laplace distribution private static double svm_svr_probability(svm_problem prob, svm_parameter param) { int i; int nr_fold = 5; double[] ymv = new double[prob.l]; double mae = 0; svm_parameter newparam = (svm_parameter) param.Clone(); newparam.probability = 0; svm_cross_validation(prob, newparam, nr_fold, ymv); for (i = 0; i < prob.l; i++) { ymv[i] = prob.y[i] - ymv[i]; mae += System.Math.Abs(ymv[i]); } mae /= prob.l; double std = System.Math.Sqrt(2 * mae * mae); int count = 0; mae = 0; for (i = 0; i < prob.l; i++) if (System.Math.Abs(ymv[i]) > 5 * std) count = count + 1; else mae += System.Math.Abs(ymv[i]); mae /= (prob.l - count); System.Console.Error.Write("Prob. model for test data: target value = predicted value + z,\nz: Laplace distribution e^(-|z|/sigma)/(2sigma),sigma=" + mae + "\n"); return mae; }
// Cross-validation decision values for probability estimates private static void svm_binary_svc_probability(svm_problem prob, svm_parameter param, double Cp, double Cn, double[] probAB) { int i; int nr_fold = 5; int[] perm = new int[prob.l]; double[] dec_values = new double[prob.l]; // random shuffle for (i = 0; i < prob.l; i++) perm[i] = i; for (i = 0; i < prob.l; i++) { //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042_3"' int j = i + (int) (SupportClass.Random.NextDouble() * (prob.l - i)); do { int _ = perm[i]; perm[i] = perm[j]; perm[j] = _; } while (false); } for (i = 0; i < nr_fold; i++) { int begin = i * prob.l / nr_fold; int end = (i + 1) * prob.l / nr_fold; int j, k; svm_problem subprob = new svm_problem(); subprob.l = prob.l - (end - begin); subprob.x = new svm_node[subprob.l][]; subprob.y = new double[subprob.l]; k = 0; for (j = 0; j < begin; j++) { subprob.x[k] = prob.x[perm[j]]; subprob.y[k] = prob.y[perm[j]]; ++k; } for (j = end; j < prob.l; j++) { subprob.x[k] = prob.x[perm[j]]; subprob.y[k] = prob.y[perm[j]]; ++k; } int p_count = 0, n_count = 0; for (j = 0; j < k; j++) if (subprob.y[j] > 0) p_count++; else n_count++; if (p_count == 0 && n_count == 0) for (j = begin; j < end; j++) dec_values[perm[j]] = 0; else if (p_count > 0 && n_count == 0) for (j = begin; j < end; j++) dec_values[perm[j]] = 1; else if (p_count == 0 && n_count > 0) for (j = begin; j < end; j++) dec_values[perm[j]] = - 1; else { svm_parameter subparam = (svm_parameter) param.Clone(); subparam.probability = 0; subparam.C = 1.0; subparam.nr_weight = 2; subparam.weight_label = new int[2]; subparam.weight = new double[2]; subparam.weight_label[0] = + 1; subparam.weight_label[1] = - 1; subparam.weight[0] = Cp; subparam.weight[1] = Cn; svm_model submodel = svm_train(subprob, subparam); for (j = begin; j < end; j++) { double[] dec_value = new double[1]; svm_predict_values(submodel, prob.x[perm[j]], dec_value); dec_values[perm[j]] = dec_value[0]; // ensure +1 -1 order; reason not using CV subroutine dec_values[perm[j]] *= submodel.label[0]; } } } sigmoid_train(prob.l, dec_values, prob.y, probAB); }