001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.dllearner.accuracymethods;
020
021import org.dllearner.core.ComponentAnn;
022import org.dllearner.core.config.ConfigOption;
023
024@ComponentAnn(name = "Predictive Accuracy", shortName = "pred_acc", version = 0)
025public class AccMethodPredAcc implements AccMethodTwoValued, AccMethodWithBeta {
026
027        @ConfigOption(description = "beta factor (0 = do not use)", defaultValue = "0")
028        private double beta = 0;
029
030        public AccMethodPredAcc() {
031        }
032
033        public AccMethodPredAcc(boolean init) {
034                if (init) init();
035        }
036
037        @Override
038        public void init() {
039        }
040
041        @Override
042        public double getAccOrTooWeak2(int tp, int fn, int fp, int tn, double noise) {
043                int posExamples = tp + fn;
044                int negExamples = fp + tn;
045                int allExamples = posExamples + negExamples;
046
047                if (beta == 0) {
048                        int maxNotCovered = (int) Math.ceil(noise * posExamples);
049
050                        if (fn != 0 && fn >= maxNotCovered) {
051                                return -1;
052                        }
053
054                        return (tp + tn) / (double) allExamples;
055
056                } else {
057
058                        if ((beta * tp + negExamples) / (beta * posExamples + negExamples) < 1 - noise) {
059                                return -1;
060                        }
061
062                        // correctly classified divided by all examples
063                        return (beta * tp + tn) / (beta * posExamples + negExamples);
064                }
065                
066        }
067
068        @Override
069        public void setBeta(double beta) {
070                this.beta = beta;
071        }
072}