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;
023import org.dllearner.learningproblems.Heuristics;
024
025@ComponentAnn(name = "FMeasure", shortName = "fmeasure", version = 0)
026public class AccMethodFMeasure implements AccMethodTwoValued, AccMethodWithBeta {
027
028        @ConfigOption(description = "beta factor (0 = do not use)", defaultValue = "0")
029        protected double beta = 0;
030
031        @Override
032        public void init() {
033        }
034        
035        public AccMethodFMeasure() {
036        }
037        
038        public AccMethodFMeasure(boolean init) {
039                if (init) init();
040        }
041
042        @Override
043        public double getAccOrTooWeak2(int tp, int fn, int fp, int tn, double noise) {
044                double recall = Heuristics.divideOrZero( tp , tp+fn );
045
046                if (beta == 0) {
047                        if (recall == 0 || recall < 1 - noise) {
048                                return -1;
049                        }
050                } else {
051                        if (recall == 0 || ((1 + Math.sqrt(beta)) * recall) / (Math.sqrt(beta) + 1) < 1 - noise) {
052                                return -1;
053                        }
054                }
055
056                double precision = Heuristics.divideOrZero( tp , tp+fp );
057
058                if (beta == 0) {
059                        return Heuristics.getFScore(recall, precision);
060                } else {
061                        return Heuristics.getFScore(recall, precision, beta);
062                }
063        }
064
065        @Override
066        public void setBeta(double beta) {
067                this.beta = beta;
068        }
069}