source: src/main/java/weka/classifiers/trees/j48/Stats.java @ 16

Last change on this file since 16 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 2.9 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 *    Stats.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.classifiers.trees.j48;
24
25import weka.core.RevisionHandler;
26import weka.core.RevisionUtils;
27import weka.core.Statistics;
28
29/**
30 * Class implementing a statistical routine needed by J48 to
31 * compute its error estimate.
32 *
33 * @author Eibe Frank (eibe@cs.waikato.ac.nz)
34 * @version $Revision: 1.9 $
35 */
36public class Stats
37  implements RevisionHandler {
38
39  /**
40   * Computes estimated extra error for given total number of instances
41   * and error using normal approximation to binomial distribution
42   * (and continuity correction).
43   *
44   * @param N number of instances
45   * @param e observed error
46   * @param CF confidence value
47   */
48  public static double addErrs(double N, double e, float CF){
49
50    // Ignore stupid values for CF
51    if (CF > 0.5) {
52      System.err.println("WARNING: confidence value for pruning " +
53                         " too high. Error estimate not modified.");
54      return 0;
55    }
56
57    // Check for extreme cases at the low end because the
58    // normal approximation won't work
59    if (e < 1) {
60
61      // Base case (i.e. e == 0) from documenta Geigy Scientific
62      // Tables, 6th edition, page 185
63      double base = N * (1 - Math.pow(CF, 1 / N)); 
64      if (e == 0) {
65        return base; 
66      }
67   
68      // Use linear interpolation between 0 and 1 like C4.5 does
69      return base + e * (addErrs(N, 1, CF) - base);
70    }
71   
72    // Use linear interpolation at the high end (i.e. between N - 0.5
73    // and N) because of the continuity correction
74    if (e + 0.5 >= N) {
75
76      // Make sure that we never return anything smaller than zero
77      return Math.max(N - e, 0);
78    }
79
80    // Get z-score corresponding to CF
81    double z = Statistics.normalInverse(1 - CF);
82
83    // Compute upper limit of confidence interval
84    double  f = (e + 0.5) / N;
85    double r = (f + (z * z) / (2 * N) +
86                z * Math.sqrt((f / N) - 
87                              (f * f / N) + 
88                              (z * z / (4 * N * N)))) /
89      (1 + (z * z) / N);
90
91    return (r * N) - e;
92  }
93 
94  /**
95   * Returns the revision string.
96   *
97   * @return            the revision
98   */
99  public String getRevision() {
100    return RevisionUtils.extract("$Revision: 1.9 $");
101  }
102}
Note: See TracBrowser for help on using the repository browser.