source: src/main/java/weka/experiment/PairedStatsCorrected.java @ 4

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

Import di weka.

File size: 3.4 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 *    PairedStatsCorrected.java
19 *    Copyright (C) 2003 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.experiment;
24
25import weka.core.RevisionUtils;
26import weka.core.Utils;
27import weka.core.Statistics;
28
29/**
30 * A class for storing stats on a paired comparison. This version is
31 * based on the corrected resampled t-test statistic, which uses the
32 * ratio of the number of test examples/the number of training examples.<p>
33 *
34 * For more information see:<p>
35 *
36 * Claude Nadeau and Yoshua Bengio, "Inference for the Generalization Error,"
37 * Machine Learning, 2001.
38 *
39 * @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
40 * @version $Revision: 1.5 $
41 */
42public class PairedStatsCorrected
43  extends PairedStats {
44
45  /** The ratio used to correct the significane test */
46  protected double m_testTrainRatio;
47
48  /**
49   * Creates a new PairedStatsCorrected object with the supplied
50   * significance level and train/test ratio.
51   *
52   * @param sig the significance level for comparisons
53   * @param testTrainRatio the number test examples/training examples
54   */
55  public PairedStatsCorrected(double sig, double testTrainRatio) {
56     
57    super(sig);
58    m_testTrainRatio = testTrainRatio;
59  }
60
61  /**
62   * Calculates the derived statistics (significance etc).
63   */
64  public void calculateDerived() {
65
66    xStats.calculateDerived();
67    yStats.calculateDerived();
68    differencesStats.calculateDerived();
69
70    correlation = Double.NaN;
71    if (!Double.isNaN(xStats.stdDev) && !Double.isNaN(yStats.stdDev)
72        && !Utils.eq(xStats.stdDev, 0)) {
73      double slope = (xySum - xStats.sum * yStats.sum / count)
74        / (xStats.sumSq - xStats.sum * xStats.mean);
75      if (!Utils.eq(yStats.stdDev, 0)) {
76        correlation = slope * xStats.stdDev / yStats.stdDev;
77      } else {
78        correlation = 1.0;
79      }
80    }
81
82    if (Utils.gr(differencesStats.stdDev, 0)) {
83
84      double tval = differencesStats.mean
85        / Math.sqrt((1 / count + m_testTrainRatio)
86                    * differencesStats.stdDev * differencesStats.stdDev);
87     
88      if (count > 1) {
89        differencesProbability = Statistics.FProbability(tval * tval, 1,
90                                                         (int) count - 1);
91      } else differencesProbability = 1;
92    } else {
93      if (differencesStats.sumSq == 0) {
94        differencesProbability = 1.0;
95      } else {
96        differencesProbability = 0.0;
97      }
98    }
99    differencesSignificance = 0;
100    if (differencesProbability <= sigLevel) {
101      if (xStats.mean > yStats.mean) {
102        differencesSignificance = 1;
103      } else {
104        differencesSignificance = -1;
105      }
106    }
107  }
108 
109  /**
110   * Returns the revision string.
111   *
112   * @return            the revision
113   */
114  public String getRevision() {
115    return RevisionUtils.extract("$Revision: 1.5 $");
116  }
117}
Note: See TracBrowser for help on using the repository browser.