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 = "Weighted Predictive Accuracy", shortName = "weighted.pred_acc", version = 0)
025public class AccMethodPredAccWeighted implements AccMethodTwoValued {
026
027        @ConfigOption(defaultValue = "false", description = "balance the weights to relative set size")
028        private boolean balanced = false;
029        @ConfigOption(defaultValue = "1", description = "weight on the positive examples")
030        private double posWeight = 1;
031        @ConfigOption(defaultValue = "1", description = "weight on the negative examples")
032        private double negWeight = 1;
033
034        public AccMethodPredAccWeighted() {
035        }
036
037        public AccMethodPredAccWeighted(boolean init) {
038                if (init) init();
039        }
040
041        @Override
042        public void init() {
043        }
044
045        @Override
046        public double getAccOrTooWeak2(int tp, int fn, int fp, int tn, double noise) {
047                int posExamples = tp + fn;
048                int negExamples = fp + tn;
049                
050                int maxNotCovered = (int) Math.ceil(noise*posExamples);
051                
052                if(fn != 0 && fn >= maxNotCovered) {
053                        return -1;
054                }
055                
056                if (balanced) {
057                        posWeight = 1/(double)posExamples;
058                        negWeight = 1/(double)negExamples;
059                }
060                return ((tp*posWeight) + (tn*negWeight)) / ((posExamples*posWeight)+(negExamples*negWeight));
061        }
062
063        public boolean isBalanced() {
064                return balanced;
065        }
066
067        public void setBalanced(boolean balanced) {
068                this.balanced = balanced;
069        }
070
071        public double getPosWeight() {
072                return posWeight;
073        }
074
075        public void setPosWeight(double posWeight) {
076                this.posWeight = posWeight;
077        }
078
079        public double getNegWeight() {
080                return negWeight;
081        }
082
083        public void setNegWeight(double negWeight) {
084                this.negWeight = negWeight;
085        }
086
087}