Changeset 1894
- Timestamp:
- 05/25/09 18:43:26 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.GP.StructureIdentification/3.3
- Files:
-
- 1 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/CoefficientOfDeterminationEvaluator.cs
r1891 r1894 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.Operators; 29 using HeuristicLab.Modeling; 29 30 30 31 namespace HeuristicLab.GP.StructureIdentification { 31 public class CoefficientOfDeterminationEvaluator : GPEvaluatorBase { 32 public class CoefficientOfDeterminationEvaluator : SimpleGPEvaluatorBase { 33 34 public override string OutputVariableName { 35 get { 36 return "R2"; 37 } 38 } 39 32 40 public override string Description { 33 41 get { … … 37 45 } 38 46 39 public CoefficientOfDeterminationEvaluator() 40 : base() { 41 AddVariableInfo(new VariableInfo("R2", "The coefficient of determination of the model", typeof(DoubleData), VariableKind.New)); 42 } 47 public override double Evaluate(double[,] values) { 43 48 44 public override void Evaluate(IScope scope, ITreeEvaluator evaluator, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) { 45 double errorsSquaredSum = 0.0; 46 double originalDeviationTotalSumOfSquares = 0.0; 47 double targetMean = dataset.GetMean(targetVariable, start, end); 48 49 double originalSum = 0.0; 50 int n = 0; 51 for (int sample = start; sample < end; sample++) { 52 double estimated = evaluator.Evaluate(sample); 53 double original = dataset.GetValue(sample, targetVariable); 54 if (updateTargetValues) { 55 dataset.SetValue(sample, targetVariable, estimated); 56 } 57 if (!double.IsNaN(original) && !double.IsInfinity(original)) { 58 double error = estimated - original; 59 errorsSquaredSum += error * error; 60 61 originalSum += original; 62 n++; 63 } 64 } 65 66 double originalMean = originalSum / n; 67 for(int sample = start; sample < end; sample++){ 68 double original = dataset.GetValue(sample, targetVariable); 69 if (!double.IsNaN(original) && !double.IsInfinity(original)) { 70 original = original - originalMean; 71 original = original * original; 72 originalDeviationTotalSumOfSquares += original; 73 } 74 } 75 76 double quality = 1 - errorsSquaredSum / originalDeviationTotalSumOfSquares; 77 if (quality > 1) 78 throw new InvalidProgramException(); 49 double quality = SimpleR2Evaluator.Calculate(values); 79 50 if (double.IsNaN(quality) || double.IsInfinity(quality)) 80 51 quality = double.MaxValue; 81 52 82 DoubleData r2 = GetVariableValue<DoubleData>("R2", scope, false, false); 83 if (r2 == null) { 84 r2 = new DoubleData(); 85 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("R2"), r2)); 86 } 87 88 r2.Data = quality; 53 return quality; 89 54 } 90 55 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/MeanAbsolutePercentageErrorEvaluator.cs
r1891 r1894 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.Operators; 29 using HeuristicLab.Modeling; 29 30 using HeuristicLab.DataAnalysis; 30 31 31 32 namespace HeuristicLab.GP.StructureIdentification { 32 public class MeanAbsolutePercentageErrorEvaluator : GPEvaluatorBase { 33 public class MeanAbsolutePercentageErrorEvaluator : SimpleGPEvaluatorBase { 34 public override string OutputVariableName { 35 get { 36 return "MAPE"; 37 } 38 } 39 33 40 public override string Description { 34 41 get { … … 38 45 } 39 46 40 public MeanAbsolutePercentageErrorEvaluator() 41 : base() { 42 AddVariableInfo(new VariableInfo("MAPE", "The mean absolute percentage error of the model", typeof(DoubleData), VariableKind.New)); 43 } 44 45 public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) { 46 double errorsSum = 0.0; 47 int n = 0; 48 for (int sample = start; sample < end; sample++) { 49 double estimated = evaluator.Evaluate(sample); 50 double original = dataset.GetValue(sample, targetVariable); 51 52 if (updateTargetValues) { 53 dataset.SetValue(sample, targetVariable, estimated); 54 } 55 56 if (!double.IsNaN(original) && !double.IsInfinity(original) && original != 0.0) { 57 double percent_error = Math.Abs((estimated - original) / original); 58 errorsSum += percent_error; 59 n++; 60 } 61 } 62 double quality = errorsSum / n; 47 public override double Evaluate(double[,] values) { 48 double quality = SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(values); 63 49 if (double.IsNaN(quality) || double.IsInfinity(quality)) 64 50 quality = double.MaxValue; 65 51 66 // create a variable for the MAPE 67 DoubleData mape = GetVariableValue<DoubleData>("MAPE", scope, false, false); 68 if (mape == null) { 69 mape = new DoubleData(); 70 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MAPE"), mape)); 71 } 72 73 mape.Data = quality; 52 return quality; 74 53 } 75 54 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/MeanAbsolutePercentageOfRangeErrorEvaluator.cs
r1891 r1894 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.Operators; 29 using HeuristicLab.Modeling; 29 30 using HeuristicLab.DataAnalysis; 30 31 31 32 namespace HeuristicLab.GP.StructureIdentification { 32 public class MeanAbsolutePercentageOfRangeErrorEvaluator : GPEvaluatorBase { 33 public class MeanAbsolutePercentageOfRangeErrorEvaluator : SimpleGPEvaluatorBase { 34 public override string OutputVariableName { 35 get { 36 return "MAPRE"; 37 } 38 } 33 39 public override string Description { 34 40 get { … … 38 44 } 39 45 40 public MeanAbsolutePercentageOfRangeErrorEvaluator() 41 : base() { 42 AddVariableInfo(new VariableInfo("MAPRE", "The mean absolute percentage range error of the model", typeof(DoubleData), VariableKind.New)); 43 } 44 45 public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) { 46 double errorsSum = 0.0; 47 int n = 0; 48 double range = dataset.GetRange(targetVariable, start, end); 49 for (int sample = start; sample < end; sample++) { 50 double estimated = evaluator.Evaluate(sample); 51 double original = dataset.GetValue(sample, targetVariable); 52 53 if (updateTargetValues) { 54 dataset.SetValue(sample, targetVariable, estimated); 55 } 56 57 if (!double.IsNaN(original) && !double.IsInfinity(original) && original != 0.0) { 58 double percent_error = Math.Abs((estimated - original) / range); 59 errorsSum += percent_error; 60 n++; 61 } 62 } 63 double quality = errorsSum / n; 46 public override double Evaluate(double[,] values) { 47 double quality = SimpleMeanAbsolutePercentageOfRangeErrorEvaluator.Calculate(values); 64 48 if (double.IsNaN(quality) || double.IsInfinity(quality)) 65 49 quality = double.MaxValue; 66 50 67 // create a variable for the MAPRE 68 DoubleData mapre = GetVariableValue<DoubleData>("MAPRE", scope, false, false); 69 if (mapre == null) { 70 mapre = new DoubleData(); 71 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MAPRE"), mapre)); 72 } 73 74 mapre.Data = quality; 51 return quality; 75 52 } 76 53 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/MeanSquaredErrorEvaluator.cs
r1891 r1894 28 28 using HeuristicLab.Operators; 29 29 using HeuristicLab.DataAnalysis; 30 using HeuristicLab.Modeling; 30 31 31 32 namespace HeuristicLab.GP.StructureIdentification { 32 public class MeanSquaredErrorEvaluator : GPEvaluatorBase { 33 public class MeanSquaredErrorEvaluator : SimpleGPEvaluatorBase { 34 public override string OutputVariableName { 35 get { 36 return "MSE"; 37 } 38 } 33 39 public override string Description { 34 40 get { … … 38 44 } 39 45 40 public MeanSquaredErrorEvaluator() 41 : base() { 42 AddVariableInfo(new VariableInfo("MSE", "The mean squared error of the model", typeof(DoubleData), VariableKind.New)); 43 } 46 public override double Evaluate(double[,] values) { 47 double quality = SimpleMSEEvaluator.Calculate(values); 44 48 45 public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) { 46 double errorsSquaredSum = 0; 47 int n = 0; 48 for (int sample = start; sample < end; sample++) { 49 double original = dataset.GetValue(sample, targetVariable); 50 double estimated = evaluator.Evaluate(sample); 51 if (updateTargetValues) { 52 dataset.SetValue(sample, targetVariable, estimated); 53 } 54 if (!double.IsNaN(original) && !double.IsInfinity(original)) { 55 double error = estimated - original; 56 errorsSquaredSum += error * error; 57 n++; 58 } 49 if (double.IsNaN(quality) || double.IsInfinity(quality)) { 50 quality = double.MaxValue; 59 51 } 60 52 61 errorsSquaredSum /= n; 62 if (double.IsNaN(errorsSquaredSum) || double.IsInfinity(errorsSquaredSum)) { 63 errorsSquaredSum = double.MaxValue; 64 } 65 66 DoubleData mse = GetVariableValue<DoubleData>("MSE", scope, false, false); 67 if (mse == null) { 68 mse = new DoubleData(); 69 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("MSE"), mse)); 70 } 71 72 mse.Data = errorsSquaredSum; 53 return quality; 73 54 } 74 55 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Evaluators/VarianceAccountedForEvaluator.cs
r1891 r1894 28 28 using HeuristicLab.Operators; 29 29 using HeuristicLab.DataAnalysis; 30 using HeuristicLab.Modeling; 30 31 31 32 namespace HeuristicLab.GP.StructureIdentification { 32 public class VarianceAccountedForEvaluator : GPEvaluatorBase { 33 /// <summary> 34 /// The Variance Accounted For (VAF) function calculates is computed as 35 /// VAF(y,y') = ( 1 - var(y-y')/var(y) ) 36 /// where y' denotes the predicted / modelled values for y and var(x) the variance of a signal x. 37 /// </summary> 38 public class VarianceAccountedForEvaluator : SimpleGPEvaluatorBase { 39 public override string OutputVariableName { 40 get { 41 return "VAF"; 42 } 43 } 33 44 public override string Description { 34 45 get { … … 42 53 } 43 54 44 /// <summary> 45 /// The Variance Accounted For (VAF) function calculates is computed as 46 /// VAF(y,y') = ( 1 - var(y-y')/var(y) ) 47 /// where y' denotes the predicted / modelled values for y and var(x) the variance of a signal x. 48 /// </summary> 49 public VarianceAccountedForEvaluator() 50 : base() { 51 AddVariableInfo(new VariableInfo("VAF", "The variance-accounted-for quality of the model", typeof(DoubleData), VariableKind.New)); 52 53 } 54 55 public override void Evaluate(IScope scope, ITreeEvaluator evaluator, Dataset dataset, int targetVariable, int start, int end, bool updateTargetValues) { 56 int nSamples = end - start; 57 double[] errors = new double[nSamples]; 58 double[] originalTargetVariableValues = new double[nSamples]; 59 for (int sample = start; sample < end; sample++) { 60 double estimated = evaluator.Evaluate(sample); 61 double original = dataset.GetValue(sample, targetVariable); 62 if (updateTargetValues) { 63 dataset.SetValue(sample, targetVariable, estimated); 64 } 65 if (!double.IsNaN(original) && !double.IsInfinity(original)) { 66 errors[sample - start] = original - estimated; 67 originalTargetVariableValues[sample - start] = original; 68 } else { 69 errors[sample - start] = double.NaN; 70 originalTargetVariableValues[sample - start] = double.NaN; 71 } 72 } 73 double errorsVariance = Statistics.Variance(errors); 74 double originalsVariance = Statistics.Variance(originalTargetVariableValues); 75 double quality = 1 - errorsVariance / originalsVariance; 55 public override double Evaluate(double[,] values) { 56 double quality = SimpleVarianceAccountedForEvaluator.Calculate(values); 76 57 77 58 if (double.IsNaN(quality) || double.IsInfinity(quality)) { 78 59 quality = double.MaxValue; 79 60 } 80 DoubleData vaf = GetVariableValue<DoubleData>("VAF", scope, false, false); 81 if (vaf == null) { 82 vaf = new DoubleData(); 83 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("VAF"), vaf)); 84 } 85 86 vaf.Data = quality; 61 return quality; 87 62 } 88 63 } -
trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/HeuristicLab.GP.StructureIdentification-3.3.csproj
r1856 r1894 89 89 <Compile Include="Constant.cs" /> 90 90 <Compile Include="AlgorithmBase.cs" /> 91 <Compile Include="Evaluators\SimpleGPEvaluatorBase.cs" /> 91 92 <Compile Include="TreeEvaluatorBase.cs" /> 92 93 <Compile Include="HL2TreeEvaluator.cs" />
Note: See TracChangeset
for help on using the changeset viewer.