| 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 | * NumericPrediction.java |
|---|
| 19 | * Copyright (C) 2002 University of Waikato, Hamilton, New Zealand |
|---|
| 20 | * |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | package weka.classifiers.evaluation; |
|---|
| 24 | |
|---|
| 25 | import weka.classifiers.IntervalEstimator; |
|---|
| 26 | import weka.core.RevisionHandler; |
|---|
| 27 | import weka.core.RevisionUtils; |
|---|
| 28 | |
|---|
| 29 | import java.io.Serializable; |
|---|
| 30 | |
|---|
| 31 | /** |
|---|
| 32 | * Encapsulates an evaluatable numeric prediction: the predicted class value |
|---|
| 33 | * plus the actual class value. |
|---|
| 34 | * |
|---|
| 35 | * @author Len Trigg (len@reeltwo.com) |
|---|
| 36 | * @version $Revision: 5714 $ |
|---|
| 37 | */ |
|---|
| 38 | public class NumericPrediction |
|---|
| 39 | implements Prediction, Serializable, RevisionHandler { |
|---|
| 40 | |
|---|
| 41 | /** for serialization. */ |
|---|
| 42 | private static final long serialVersionUID = -4880216423674233887L; |
|---|
| 43 | |
|---|
| 44 | /** The actual class value. */ |
|---|
| 45 | private double m_Actual = MISSING_VALUE; |
|---|
| 46 | |
|---|
| 47 | /** The predicted class value. */ |
|---|
| 48 | private double m_Predicted = MISSING_VALUE; |
|---|
| 49 | |
|---|
| 50 | /** The weight assigned to this prediction. */ |
|---|
| 51 | private double m_Weight = 1; |
|---|
| 52 | |
|---|
| 53 | /** the prediction intervals. */ |
|---|
| 54 | private double[][] m_PredictionIntervals; |
|---|
| 55 | |
|---|
| 56 | /** |
|---|
| 57 | * Creates the NumericPrediction object with a default weight of 1.0. |
|---|
| 58 | * |
|---|
| 59 | * @param actual the actual value, or MISSING_VALUE. |
|---|
| 60 | * @param predicted the predicted value, or MISSING_VALUE. |
|---|
| 61 | */ |
|---|
| 62 | public NumericPrediction(double actual, double predicted) { |
|---|
| 63 | this(actual, predicted, 1); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * Creates the NumericPrediction object. |
|---|
| 68 | * |
|---|
| 69 | * @param actual the actual value, or MISSING_VALUE. |
|---|
| 70 | * @param predicted the predicted value, or MISSING_VALUE. |
|---|
| 71 | * @param weight the weight assigned to the prediction. |
|---|
| 72 | */ |
|---|
| 73 | public NumericPrediction(double actual, double predicted, double weight) { |
|---|
| 74 | this(actual, predicted, weight, new double[0][]); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * Creates the NumericPrediction object. |
|---|
| 79 | * |
|---|
| 80 | * @param actual the actual value, or MISSING_VALUE. |
|---|
| 81 | * @param predicted the predicted value, or MISSING_VALUE. |
|---|
| 82 | * @param weight the weight assigned to the prediction. |
|---|
| 83 | * @param predInt the prediction intervals from classifiers implementing |
|---|
| 84 | * the <code>IntervalEstimator</code> interface. |
|---|
| 85 | * @see IntervalEstimator |
|---|
| 86 | */ |
|---|
| 87 | public NumericPrediction(double actual, double predicted, double weight, double[][] predInt) { |
|---|
| 88 | m_Actual = actual; |
|---|
| 89 | m_Predicted = predicted; |
|---|
| 90 | m_Weight = weight; |
|---|
| 91 | setPredictionIntervals(predInt); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /** |
|---|
| 95 | * Gets the actual class value. |
|---|
| 96 | * |
|---|
| 97 | * @return the actual class value, or MISSING_VALUE if no |
|---|
| 98 | * prediction was made. |
|---|
| 99 | */ |
|---|
| 100 | public double actual() { |
|---|
| 101 | return m_Actual; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | /** |
|---|
| 105 | * Gets the predicted class value. |
|---|
| 106 | * |
|---|
| 107 | * @return the predicted class value, or MISSING_VALUE if no |
|---|
| 108 | * prediction was made. |
|---|
| 109 | */ |
|---|
| 110 | public double predicted() { |
|---|
| 111 | return m_Predicted; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | /** |
|---|
| 115 | * Gets the weight assigned to this prediction. This is typically the weight |
|---|
| 116 | * of the test instance the prediction was made for. |
|---|
| 117 | * |
|---|
| 118 | * @return the weight assigned to this prediction. |
|---|
| 119 | */ |
|---|
| 120 | public double weight() { |
|---|
| 121 | return m_Weight; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | /** |
|---|
| 125 | * Calculates the prediction error. This is defined as the predicted |
|---|
| 126 | * value minus the actual value. |
|---|
| 127 | * |
|---|
| 128 | * @return the error for this prediction, or |
|---|
| 129 | * MISSING_VALUE if either the actual or predicted value |
|---|
| 130 | * is missing. |
|---|
| 131 | */ |
|---|
| 132 | public double error() { |
|---|
| 133 | if ((m_Actual == MISSING_VALUE) || |
|---|
| 134 | (m_Predicted == MISSING_VALUE)) { |
|---|
| 135 | return MISSING_VALUE; |
|---|
| 136 | } |
|---|
| 137 | return m_Predicted - m_Actual; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | /** |
|---|
| 141 | * Sets the prediction intervals for this prediction. |
|---|
| 142 | * |
|---|
| 143 | * @param predInt the prediction intervals |
|---|
| 144 | */ |
|---|
| 145 | public void setPredictionIntervals(double[][] predInt) { |
|---|
| 146 | m_PredictionIntervals = predInt.clone(); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /** |
|---|
| 150 | * Returns the predictions intervals. Only classifiers implementing the |
|---|
| 151 | * <code>IntervalEstimator</code> interface. |
|---|
| 152 | * |
|---|
| 153 | * @return the prediction intervals. |
|---|
| 154 | * @see IntervalEstimator |
|---|
| 155 | */ |
|---|
| 156 | public double[][] predictionIntervals() { |
|---|
| 157 | return m_PredictionIntervals; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | /** |
|---|
| 161 | * Gets a human readable representation of this prediction. |
|---|
| 162 | * |
|---|
| 163 | * @return a human readable representation of this prediction. |
|---|
| 164 | */ |
|---|
| 165 | public String toString() { |
|---|
| 166 | StringBuffer sb = new StringBuffer(); |
|---|
| 167 | sb.append("NUM: ").append(actual()).append(' ').append(predicted()); |
|---|
| 168 | sb.append(' ').append(weight()); |
|---|
| 169 | return sb.toString(); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | /** |
|---|
| 173 | * Returns the revision string. |
|---|
| 174 | * |
|---|
| 175 | * @return the revision |
|---|
| 176 | */ |
|---|
| 177 | public String getRevision() { |
|---|
| 178 | return RevisionUtils.extract("$Revision: 5714 $"); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|