01: /*
02: * This program is free software; you can redistribute it and/or modify
03: * it under the terms of the GNU General Public License as published by
04: * the Free Software Foundation; either version 2 of the License, or
05: * (at your option) any later version.
06: *
07: * This program is distributed in the hope that it will be useful,
08: * but WITHOUT ANY WARRANTY; without even the implied warranty of
09: * 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:
23: package weka.classifiers.trees.j48;
24:
25: import weka.core.*;
26:
27: /**
28: * Class implementing a statistical routine needed by J48 to
29: * compute its error estimate.
30: *
31: * @author Eibe Frank (eibe@cs.waikato.ac.nz)
32: * @version $Revision: 1.8 $
33: */
34: public class Stats {
35:
36: /**
37: * Computes estimated extra error for given total number of instances
38: * and error using normal approximation to binomial distribution
39: * (and continuity correction).
40: *
41: * @param N number of instances
42: * @param e observed error
43: * @param CF confidence value
44: */
45: public static double addErrs(double N, double e, float CF) {
46:
47: // Ignore stupid values for CF
48: if (CF > 0.5) {
49: System.err.println("WARNING: confidence value for pruning "
50: + " too high. Error estimate not modified.");
51: return 0;
52: }
53:
54: // Check for extreme cases at the low end because the
55: // normal approximation won't work
56: if (e < 1) {
57:
58: // Base case (i.e. e == 0) from documenta Geigy Scientific
59: // Tables, 6th edition, page 185
60: double base = N * (1 - Math.pow(CF, 1 / N));
61: if (e == 0) {
62: return base;
63: }
64:
65: // Use linear interpolation between 0 and 1 like C4.5 does
66: return base + e * (addErrs(N, 1, CF) - base);
67: }
68:
69: // Use linear interpolation at the high end (i.e. between N - 0.5
70: // and N) because of the continuity correction
71: if (e + 0.5 >= N) {
72:
73: // Make sure that we never return anything smaller than zero
74: return Math.max(N - e, 0);
75: }
76:
77: // Get z-score corresponding to CF
78: double z = Statistics.normalInverse(1 - CF);
79:
80: // Compute upper limit of confidence interval
81: double f = (e + 0.5) / N;
82: double r = (f + (z * z) / (2 * N) + z
83: * Math.sqrt((f / N) - (f * f / N)
84: + (z * z / (4 * N * N))))
85: / (1 + (z * z) / N);
86:
87: return (r * N) - e;
88: }
89: }
|