Changeset 128 for trunk/sources/HeuristicLab.StructureIdentification/Evaluation/CoefficientOfDeterminationEvaluator.cs
- Timestamp:
- 04/18/08 13:53:53 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.StructureIdentification/Evaluation/CoefficientOfDeterminationEvaluator.cs
r2 r128 31 31 32 32 namespace HeuristicLab.StructureIdentification { 33 public class CoefficientOfDeterminationEvaluator : OperatorBase {33 public class CoefficientOfDeterminationEvaluator : GPEvaluatorBase { 34 34 public override string Description { 35 get { return @"Applies 'OperatorTree' to samples 'FirstSampleIndex' - 'LastSampleIndex' (inclusive) of 'Dataset' and calculates 36 the 'coefficient of determination' of estimated values vs. real values of 'TargetVariable'."; } 35 get { 36 return @"Applies 'OperatorTree' to all samples of 'Dataset' and calculates 37 the 'coefficient of determination' of estimated values vs. real values of 'TargetVariable'."; 38 } 37 39 } 38 40 39 41 public CoefficientOfDeterminationEvaluator() 40 42 : base() { 41 AddVariableInfo(new VariableInfo("OperatorTree", "The function tree that should be evaluated", typeof(IFunction), VariableKind.In));42 AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In));43 AddVariableInfo(new VariableInfo("TargetVariable", "Index of the target variable in the dataset", typeof(IntData), VariableKind.In));44 AddVariableInfo(new VariableInfo("FirstSampleIndex", "Index of the first row of the dataset on which the function should be evaluated", typeof(IntData), VariableKind.In));45 AddVariableInfo(new VariableInfo("LastSampleIndex", "Index of the last row of the dataset on which the function should be evaluated (inclusive)", typeof(IntData), VariableKind.In));46 AddVariableInfo(new VariableInfo("PunishmentFactor", "Punishment factor for invalid estimations", typeof(DoubleData), VariableKind.In));47 AddVariableInfo(new VariableInfo("UseEstimatedTargetValues", "When the function tree contains the target variable this variable determines " +48 "if we should use the estimated or the original values of the target variable in the evaluation", typeof(BoolData), VariableKind.In));49 AddVariableInfo(new VariableInfo("Quality", "The coefficient of determination of the model", typeof(DoubleData), VariableKind.New));50 51 43 } 52 44 53 54 private double[] savedTargetVariable = new double[1]; 55 public override IOperation Apply(IScope scope) { 56 int firstSampleIndex = GetVariableValue<IntData>("FirstSampleIndex", scope, true).Data; 57 int lastSampleIndex = GetVariableValue<IntData>("LastSampleIndex", scope, true).Data; 58 59 if(lastSampleIndex < firstSampleIndex) { 60 throw new InvalidProgramException(); 61 } 62 63 IFunction function = GetVariableValue<IFunction>("OperatorTree", scope, true); 64 65 Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true); 66 67 int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data; 68 bool useEstimatedTargetValues = GetVariableValue<BoolData>("UseEstimatedTargetValues", scope, true).Data; 69 double punishmentFactor = GetVariableValue<DoubleData>("PunishmentFactor", scope, true).Data; 70 71 if(useEstimatedTargetValues && savedTargetVariable.Length != lastSampleIndex - firstSampleIndex + 1) { 72 savedTargetVariable = new double[lastSampleIndex - firstSampleIndex + 1]; 73 } 74 75 double maximumPunishment = punishmentFactor * dataset.GetRange(targetVariable, firstSampleIndex, lastSampleIndex); 76 45 public override double Evaluate(IScope scope, IFunction function, int targetVariable, Dataset dataset) { 77 46 double errorsSquaredSum = 0.0; 78 double originalsSum = 0.0; 79 double targetMean = dataset.GetMean(targetVariable, firstSampleIndex, lastSampleIndex); 80 81 for(int sample = firstSampleIndex; sample <= lastSampleIndex; sample++) { 47 double originalDeviationTotalSumOfSquares = 0.0; 48 double targetMean = dataset.GetMean(targetVariable); 49 for(int sample = 0; sample < dataset.Rows; sample++) { 82 50 double estimated = function.Evaluate(dataset, sample); 83 51 double original = dataset.GetValue(sample, targetVariable); 84 85 if(useEstimatedTargetValues) {86 savedTargetVariable[sample - firstSampleIndex] = original;87 dataset.SetValue(sample, targetVariable, estimated);88 }89 90 52 if(!double.IsNaN(original) && !double.IsInfinity(original)) { 91 53 if(double.IsNaN(estimated) || double.IsInfinity(estimated)) … … 98 60 double error = estimated - original; 99 61 errorsSquaredSum += error * error; 100 originalsSum += original; 62 63 double origDeviation = original - targetMean; 64 originalDeviationTotalSumOfSquares += origDeviation * origDeviation; 101 65 } 102 66 } 103 104 double originalsMean = originalsSum / (lastSampleIndex - firstSampleIndex +1); 105 106 double originalTotalSumOfSquares = 0.0; 107 108 for(int sample=0; sample <savedTargetVariable.Length; sample++) { 109 double original = savedTargetVariable[sample]; 110 111 if(!double.IsInfinity(original) && !double.IsNaN(original)) { 112 original = original - originalsMean; 113 originalTotalSumOfSquares += original * original; 114 } 115 } 116 117 double quality = 1 - errorsSquaredSum / originalTotalSumOfSquares; 118 119 if(quality > 1) { 67 double quality = 1 - errorsSquaredSum / originalDeviationTotalSumOfSquares; 68 if(quality > 1) 120 69 throw new InvalidProgramException(); 121 } 122 123 if(double.IsNaN(quality) || double.IsInfinity(quality)) { 70 if(double.IsNaN(quality) || double.IsInfinity(quality)) 124 71 quality = double.MaxValue; 125 } 126 127 if(useEstimatedTargetValues) { 128 // restore original values of the target variable 129 for(int sample = firstSampleIndex; sample <= lastSampleIndex; sample++) { 130 dataset.SetValue(sample, targetVariable, savedTargetVariable[sample - firstSampleIndex]); 131 } 132 } 133 134 scope.AddVariable(new HeuristicLab.Core.Variable("Quality", new DoubleData(quality))); 135 return null; 72 return quality; 136 73 } 137 74 }
Note: See TracChangeset
for help on using the changeset viewer.