/** * Calculates the cost of the <code>trainingSet</code>. * * @param hyp the hypothesis to use in calculating the cost. * @return the cost associated with the hypothesis. */ public double defaultCostFunction(Hypothesis hyp) { double error = 0; double h; int answer; for (TrainingExample t : trainingSet) { try { h = (Double) hyp.predict(t.getInput()); } catch (Exception e) { e.printStackTrace(); continue; } answer = t.getAnswer(); error -= answer * log(h) + (1 - answer) * log(1 - h); } double regError = 0; for (int i = 0; i < hyp.getNumFeatures(); ++i) { regError += pow(hyp.getParameter(i), 2); } error += regError / regularizationParam; return error / (2 * trainingSet.length); }