Changeset 7100
- Timestamp:
- 11/29/11 20:05:38 (13 years ago)
- Location:
- branches/HeuristicLab.TimeSeries
- Files:
-
- 3 added
- 18 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.TimeSeries/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearTimeSeriesPrognosis.cs
r7099 r7100 76 76 #region linear regression 77 77 protected override void Run() { 78 double rmsError, cvRmsError;79 var solution = CreateLinearTimeSeriesPrognosisSolution(Problem.ProblemData, MaximalLag, out rmsError , out cvRmsError);78 double[] rmsErrors, cvRmsErrors; 79 var solution = CreateLinearTimeSeriesPrognosisSolution(Problem.ProblemData, MaximalLag, out rmsErrors, out cvRmsErrors); 80 80 Results.Add(new Result(LinearTimeSeriesPrognosisModelResultName, "The linear time-series prognosis solution.", solution)); 81 Results.Add(new Result("Root mean square error ", "The root of the mean of squared errors of the linear time-series prognosis solution on the training set.", new DoubleValue(rmsError)));82 Results.Add(new Result("Estimated root mean square error (cross-validation)", "The estimated root of the mean of squared errors of the linear time-series prognosis solution via cross validation.", new DoubleValue(cvRmsError)));81 Results.Add(new Result("Root mean square errors", "The root of the mean of squared errors of the linear time-series prognosis solution on the training set.", new DoubleArray(rmsErrors))); 82 Results.Add(new Result("Estimated root mean square errors (cross-validation)", "The estimated root of the mean of squared errors of the linear time-series prognosis solution via cross validation.", new DoubleArray(cvRmsErrors))); 83 83 } 84 84 85 public static ISymbolicTimeSeriesPrognosisSolution CreateLinearTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData, int maximalLag, out double rmsError, out doublecvRmsError) {85 public static ISymbolicTimeSeriesPrognosisSolution CreateLinearTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData, int maximalLag, out double[] rmsError, out double[] cvRmsError) { 86 86 Dataset dataset = problemData.Dataset; 87 string targetVariable = problemData.TargetVariable; 88 IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables; 89 IEnumerable<int> rows = problemData.TrainingIndizes; 90 IEnumerable<int> lags = Enumerable.Range(1, maximalLag); 91 double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows, lags); 92 if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x))) 93 throw new NotSupportedException("Linear time-series prognosis does not support NaN or infinity values in the input dataset."); 87 string[] targetVariables = problemData.TargetVariables.ToArray(); 94 88 95 alglib.linearmodel lm = new alglib.linearmodel(); 96 alglib.lrreport ar = new alglib.lrreport(); 97 int nRows = inputMatrix.GetLength(0); 98 int nFeatures = inputMatrix.GetLength(1) - 1; 99 double[] coefficients = new double[nFeatures + 1]; // last coefficient is for the constant 100 101 int retVal = 1; 102 alglib.lrbuild(inputMatrix, nRows, nFeatures, out retVal, out lm, out ar); 103 if (retVal != 1) throw new ArgumentException("Error in calculation of linear time series prognosis solution"); 104 rmsError = ar.rmserror; 105 cvRmsError = ar.cvrmserror; 106 107 alglib.lrunpack(lm, out coefficients, out nFeatures); 108 89 // prepare symbolic expression tree to hold the models for each target variable 109 90 ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode()); 110 91 ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode(); 111 92 tree.Root.AddSubtree(startNode); 112 ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode(); 113 startNode.AddSubtree(addition); 93 int i = 0; 94 rmsError = new double[targetVariables.Length]; 95 cvRmsError = new double[targetVariables.Length]; 96 foreach (var targetVariable in targetVariables) { 97 IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables; 98 IEnumerable<int> rows = problemData.TrainingIndizes; 99 IEnumerable<int> lags = Enumerable.Range(1, maximalLag); 100 double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, 101 allowedInputVariables.Concat(new string[] { targetVariable }), 102 rows, lags); 103 if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x))) 104 throw new NotSupportedException( 105 "Linear time-series prognosis does not support NaN or infinity values in the input dataset."); 114 106 115 int col = 0; 116 foreach (string column in allowedInputVariables) { 117 foreach (int lag in lags) { 118 LaggedVariableTreeNode vNode = 119 (LaggedVariableTreeNode)new HeuristicLab.Problems.DataAnalysis.Symbolic.LaggedVariable().CreateTreeNode(); 120 vNode.VariableName = column; 121 vNode.Weight = coefficients[col]; 122 vNode.Lag = -lag; 123 addition.AddSubtree(vNode); 124 col++; 107 alglib.linearmodel lm = new alglib.linearmodel(); 108 alglib.lrreport ar = new alglib.lrreport(); 109 int nRows = inputMatrix.GetLength(0); 110 int nFeatures = inputMatrix.GetLength(1) - 1; 111 double[] coefficients = new double[nFeatures + 1]; // last coefficient is for the constant 112 113 int retVal = 1; 114 alglib.lrbuild(inputMatrix, nRows, nFeatures, out retVal, out lm, out ar); 115 if (retVal != 1) throw new ArgumentException("Error in calculation of linear time series prognosis solution"); 116 rmsError[i] = ar.rmserror; 117 cvRmsError[i] = ar.cvrmserror; 118 119 alglib.lrunpack(lm, out coefficients, out nFeatures); 120 121 ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode(); 122 123 int col = 0; 124 foreach (string column in allowedInputVariables) { 125 foreach (int lag in lags) { 126 LaggedVariableTreeNode vNode = 127 (LaggedVariableTreeNode)new HeuristicLab.Problems.DataAnalysis.Symbolic.LaggedVariable().CreateTreeNode(); 128 vNode.VariableName = column; 129 vNode.Weight = coefficients[col]; 130 vNode.Lag = -lag; 131 addition.AddSubtree(vNode); 132 col++; 133 } 125 134 } 135 136 ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode(); 137 cNode.Value = coefficients[coefficients.Length - 1]; 138 addition.AddSubtree(cNode); 139 140 startNode.AddSubtree(addition); 141 i++; 126 142 } 127 143 128 ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode(); 129 cNode.Value = coefficients[coefficients.Length - 1]; 130 addition.AddSubtree(cNode); 131 132 SymbolicTimeSeriesPrognosisSolution solution = new SymbolicTimeSeriesPrognosisSolution(new SymbolicTimeSeriesPrognosisModel(tree, new SymbolicDataAnalysisExpressionTreeInterpreter()), (ITimeSeriesPrognosisProblemData)problemData.Clone()); 144 SymbolicTimeSeriesPrognosisSolution solution = new SymbolicTimeSeriesPrognosisSolution(new SymbolicTimeSeriesPrognosisModel(tree, new SymbolicTimeSeriesPrognosisInterpreter(problemData.TargetVariables.ToArray())), (ITimeSeriesPrognosisProblemData)problemData.Clone()); 133 145 solution.Model.Name = "Linear Time-Series Prognosis Model"; 134 146 return solution; -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis.Views/3.4
- Property svn:ignore
-
old new 3 3 *.user 4 4 HeuristicLabProblemsDataAnalysisSymbolicTimeSeriesPrognosisViewsPlugin.cs 5 Plugin.cs
-
- Property svn:ignore
-
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4
- Property svn:ignore
-
old new 3 3 HeuristicLabProblemsDataAnalysisSymbolicTimeSeriesPrognosisPlugin.cs 4 4 obj 5 Plugin.cs
-
- Property svn:ignore
-
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis-3.4.csproj
r7099 r7100 110 110 </ItemGroup> 111 111 <ItemGroup> 112 <Compile Include="Interfaces\ISymbolicTimeSeriesPrognosisInterpreterOperator.cs" /> 113 <Compile Include="Interfaces\ISymbolicTimeSeriesPrognogisInterpreter.cs" /> 112 114 <Compile Include="Interfaces\ISymbolicTimeSeriesPrognosisEvaluator.cs" /> 113 115 <Compile Include="Interfaces\ISymbolicTimeSeriesPrognosisModel.cs" /> … … 117 119 <Compile Include="SingleObjective\SymbolicTimeSeriesPrognosisSingleObjectiveEvaluator.cs" /> 118 120 <Compile Include="SingleObjective\SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator.cs" /> 119 <Compile Include="SingleObjective\SymbolicTimeSeriesPrognosisSingleObjectiveOverfittingAnalyzer.cs" />120 121 <Compile Include="SingleObjective\SymbolicTimeSeriesPrognosisSingleObjectivePearsonRSquaredEvaluator.cs" /> 121 122 <Compile Include="SingleObjective\SymbolicTimeSeriesPrognosisSingleObjectiveProblem.cs" /> 122 123 <Compile Include="SingleObjective\SymbolicTimeSeriesPrognosisSingleObjectiveTrainingBestSolutionAnalyzer.cs" /> 123 <Compile Include="S ingleObjective\SymbolicTimeSeriesPrognosisSingleObjectiveValidationBestSolutionAnalyzer.cs" />124 <Compile Include="SymbolicTimeSeriesPrognosisInterpreter.cs" /> 124 125 <Compile Include="SymbolicTimeSeriesPrognosisModel.cs" /> 125 126 <Compile Include="SymbolicTimeSeriesPrognosisSolution.cs" /> -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator.cs
r6802 r7100 48 48 IEnumerable<int> rows = GenerateRowsToEvaluate(); 49 49 50 double quality = Calculate(Symbolic DataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);50 double quality = Calculate(SymbolicTimeSeriesPrognosisInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows); 51 51 QualityParameter.ActualValue = new DoubleValue(quality); 52 52 … … 56 56 public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, ITimeSeriesPrognosisProblemData problemData, IEnumerable<int> rows) { 57 57 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); 58 IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 59 IEnumerable<double> boundedEstimationValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit); 60 OnlineCalculatorError errorState; 61 double mse = OnlineMeanSquaredErrorCalculator.Calculate(originalValues, boundedEstimationValues, out errorState); 62 if (errorState != OnlineCalculatorError.None) return double.NaN; 63 else return mse; 58 OnlineMeanAndVarianceCalculator meanCalculator = new OnlineMeanAndVarianceCalculator(); 59 foreach (var targetVariable in problemData.TargetVariables) { 60 IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(targetVariable, rows); 61 IEnumerable<double> boundedEstimationValues = estimatedValues.LimitToRange(lowerEstimationLimit, 62 upperEstimationLimit); 63 OnlineCalculatorError errorState; 64 meanCalculator.Add(OnlineMeanSquaredErrorCalculator.Calculate(originalValues, boundedEstimationValues, out errorState)); 65 if (errorState != OnlineCalculatorError.None) return double.NaN; 66 } 67 return meanCalculator.Mean; 64 68 } 65 69 66 70 public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, ITimeSeriesPrognosisProblemData problemData, IEnumerable<int> rows) { 67 Symbolic DataAnalysisTreeInterpreterParameter.ExecutionContext = context;71 SymbolicTimeSeriesPrognosisInterpreterParameter.ExecutionContext = context; 68 72 EstimationLimitsParameter.ExecutionContext = context; 69 73 70 double mse = Calculate(Symbolic DataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);74 double mse = Calculate(SymbolicTimeSeriesPrognosisInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows); 71 75 72 76 73 Symbolic DataAnalysisTreeInterpreterParameter.ExecutionContext = null;77 SymbolicTimeSeriesPrognosisInterpreterParameter.ExecutionContext = null; 74 78 EstimationLimitsParameter.ExecutionContext = null; 75 79 -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectivePearsonRSquaredEvaluator.cs
r6802 r7100 48 48 IEnumerable<int> rows = GenerateRowsToEvaluate(); 49 49 50 double quality = Calculate(Symbolic DataAnalysisTreeInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows);50 double quality = Calculate(SymbolicTimeSeriesPrognosisInterpreterParameter.ActualValue, solution, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, ProblemDataParameter.ActualValue, rows); 51 51 QualityParameter.ActualValue = new DoubleValue(quality); 52 52 … … 56 56 public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, ITimeSeriesPrognosisProblemData problemData, IEnumerable<int> rows) { 57 57 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows); 58 IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows); 59 OnlineCalculatorError errorState; 60 double r2 = OnlinePearsonsRSquaredCalculator.Calculate(estimatedValues, originalValues, out errorState); 61 if (errorState != OnlineCalculatorError.None) return 0.0; 62 else return r2; 58 var meanCalculator = new OnlineMeanAndVarianceCalculator(); 59 foreach (var targetVariable in problemData.TargetVariables) { 60 IEnumerable<double> originalValues = problemData.Dataset.GetDoubleValues(targetVariable, rows); 61 OnlineCalculatorError errorState; 62 meanCalculator.Add(OnlinePearsonsRSquaredCalculator.Calculate(estimatedValues, originalValues, out errorState)); 63 if (errorState != OnlineCalculatorError.None) return 0.0; 64 } 65 return meanCalculator.Mean; 63 66 } 64 67 65 68 public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, ITimeSeriesPrognosisProblemData problemData, IEnumerable<int> rows) { 66 Symbolic DataAnalysisTreeInterpreterParameter.ExecutionContext = context;69 SymbolicTimeSeriesPrognosisInterpreterParameter.ExecutionContext = context; 67 70 EstimationLimitsParameter.ExecutionContext = context; 68 71 69 double r2 = Calculate(Symbolic DataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows);72 double r2 = Calculate(SymbolicTimeSeriesPrognosisInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows); 70 73 71 Symbolic DataAnalysisTreeInterpreterParameter.ExecutionContext = null;74 SymbolicTimeSeriesPrognosisInterpreterParameter.ExecutionContext = null; 72 75 EstimationLimitsParameter.ExecutionContext = null; 73 76 -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveProblem.cs
r7099 r7100 69 69 UpdateEstimationLimits(); 70 70 } 71 71 72 72 private void ConfigureGrammarSymbols() { 73 73 var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar; … … 77 77 private void InitializeOperators() { 78 78 Operators.Add(new SymbolicTimeSeriesPrognosisSingleObjectiveTrainingBestSolutionAnalyzer()); 79 Operators.Add(new SymbolicTimeSeriesPrognosisSingleObjectiveValidationBestSolutionAnalyzer());80 Operators.Add(new SymbolicTimeSeriesPrognosisSingleObjectiveOverfittingAnalyzer());79 //Operators.Add(new SymbolicTimeSeriesPrognosisSingleObjectiveValidationBestSolutionAnalyzer()); 80 //Operators.Add(new SymbolicTimeSeriesPrognosisSingleObjectiveOverfittingAnalyzer()); 81 81 ParameterizeOperators(); 82 82 } 83 83 84 84 private void UpdateEstimationLimits() { 85 if (ProblemData.TrainingIndizes.Any()) {86 var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToList();87 var mean = targetValues.Average();88 var range = targetValues.Max() - targetValues.Min();89 EstimationLimits.Upper = mean + PunishmentFactor * range;90 EstimationLimits.Lower = mean - PunishmentFactor * range;91 } else {92 93 94 }85 //if (ProblemData.TrainingIndizes.Any()) { 86 // var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariables, ProblemData.TrainingIndizes).ToList(); 87 // var mean = targetValues.Average(); 88 // var range = targetValues.Max() - targetValues.Min(); 89 // EstimationLimits.Upper = mean + PunishmentFactor * range; 90 // EstimationLimits.Lower = mean - PunishmentFactor * range; 91 //} else { 92 EstimationLimits.Upper = double.MaxValue; 93 EstimationLimits.Lower = double.MinValue; 94 //} 95 95 } 96 96 -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveTrainingBestSolutionAnalyzer.cs
r6802 r7100 34 34 [StorableClass] 35 35 public sealed class SymbolicTimeSeriesPrognosisSingleObjectiveTrainingBestSolutionAnalyzer : SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<ISymbolicTimeSeriesPrognosisSolution>, 36 ISymbolic DataAnalysisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator {36 ISymbolicTimeSeriesPrognosisInterpreterOperator, ISymbolicDataAnalysisBoundedOperator { 37 37 private const string ProblemDataParameterName = "ProblemData"; 38 private const string Symbolic DataAnalysisTreeInterpreterParameterName = "SymbolicDataAnalysisTreeInterpreter";38 private const string SymbolicTimeSeriesPrognosisInterpreterParameterName = "SymbolicTimeSeriesPrognosisInterpreter"; 39 39 private const string EstimationLimitsParameterName = "EstimationLimits"; 40 40 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling"; … … 43 43 get { return (ILookupParameter<ITimeSeriesPrognosisProblemData>)Parameters[ProblemDataParameterName]; } 44 44 } 45 public ILookupParameter<ISymbolic DataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {46 get { return (ILookupParameter<ISymbolic DataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }45 public ILookupParameter<ISymbolicTimeSeriesPrognosisInterpreter> SymbolicTimeSeriesPrognosisInterpreterParameter { 46 get { return (ILookupParameter<ISymbolicTimeSeriesPrognosisInterpreter>)Parameters[SymbolicTimeSeriesPrognosisInterpreterParameterName]; } 47 47 } 48 48 public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter { … … 66 66 : base() { 67 67 Parameters.Add(new LookupParameter<ITimeSeriesPrognosisProblemData>(ProblemDataParameterName, "The problem data for the symbolic regression solution.")); 68 Parameters.Add(new LookupParameter<ISymbolic DataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The symbolic data analysis treeinterpreter for the symbolic expression tree."));68 Parameters.Add(new LookupParameter<ISymbolicTimeSeriesPrognosisInterpreter>(SymbolicTimeSeriesPrognosisInterpreterParameterName, "The symbolic time series prognosis interpreter for the symbolic expression tree.")); 69 69 Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic regression model.")); 70 70 Parameters.Add(new ValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the produced symbolic regression solution should be linearly scaled.", new BoolValue(true))); … … 75 75 76 76 protected override ISymbolicTimeSeriesPrognosisSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) { 77 var model = new SymbolicTimeSeriesPrognosisModel((ISymbolicExpressionTree)bestTree.Clone(), Symbolic DataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);77 var model = new SymbolicTimeSeriesPrognosisModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicTimeSeriesPrognosisInterpreterParameter.ActualValue); 78 78 if (ApplyLinearScaling.Value) 79 79 SymbolicTimeSeriesPrognosisModel.Scale(model, ProblemDataParameter.ActualValue); -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveValidationBestSolutionAnalyzer.cs
r6802 r7100 67 67 68 68 protected override ISymbolicTimeSeriesPrognosisSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) { 69 var model = new SymbolicTimeSeriesPrognosisModel((ISymbolicExpressionTree)bestTree.Clone(), Symbolic DataAnalysisTreeInterpreterParameter.ActualValue, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);69 var model = new SymbolicTimeSeriesPrognosisModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicTimeSeriesPrognosisInterpreterParameter.ActualValue); 70 70 if (ApplyLinearScaling.Value) 71 71 SymbolicTimeSeriesPrognosisModel.Scale(model, ProblemDataParameter.ActualValue); -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SymbolicTimeSeriesPrognosisModel.cs
r7099 r7100 21 21 22 22 using System.Collections.Generic; 23 using System.Linq; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 33 34 [Item(Name = "Symbolic Time-Series Prognosis Model", Description = "Represents a symbolic time series prognosis model.")] 34 35 public class SymbolicTimeSeriesPrognosisModel : SymbolicDataAnalysisModel, ISymbolicTimeSeriesPrognosisModel { 35 [Storable] 36 private double lowerEstimationLimit; 37 [Storable] 38 private double upperEstimationLimit; 36 public new ISymbolicTimeSeriesPrognosisInterpreter Interpreter { 37 get { return (ISymbolicTimeSeriesPrognosisInterpreter)base.Interpreter; } 38 } 39 39 40 40 [StorableConstructor] … … 42 42 protected SymbolicTimeSeriesPrognosisModel(SymbolicTimeSeriesPrognosisModel original, Cloner cloner) 43 43 : base(original, cloner) { 44 this.lowerEstimationLimit = original.lowerEstimationLimit;45 this.upperEstimationLimit = original.upperEstimationLimit;46 44 } 47 public SymbolicTimeSeriesPrognosisModel(ISymbolicExpressionTree tree, ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, 48 double lowerEstimationLimit = double.MinValue, double upperEstimationLimit = double.MaxValue) 45 public SymbolicTimeSeriesPrognosisModel(ISymbolicExpressionTree tree, ISymbolicTimeSeriesPrognosisInterpreter interpreter) 49 46 : base(tree, interpreter) { 50 this.lowerEstimationLimit = lowerEstimationLimit;51 this.upperEstimationLimit = upperEstimationLimit;52 47 } 53 48 … … 56 51 } 57 52 58 public IEnumerable<double> GetPrognosedValues(Dataset dataset, IEnumerable<int> rows) { 59 return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows) 60 .LimitToRange(lowerEstimationLimit, upperEstimationLimit); 53 public IEnumerable<IEnumerable<IEnumerable<double>>> GetPrognosedValues(Dataset dataset, IEnumerable<int> rows, int horizon) { 54 return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows, horizon); 61 55 } 62 56 … … 70 64 public static void Scale(SymbolicTimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData) { 71 65 var dataset = problemData.Dataset; 72 var targetVariable = problemData.TargetVariable;66 var targetVariables = problemData.TargetVariables; 73 67 var rows = problemData.TrainingIndizes; 74 var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows); 75 var targetValues = dataset.GetDoubleValues(targetVariable, rows); 76 double alpha; 77 double beta; 78 OnlineCalculatorError errorState; 79 OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta, out errorState); 80 if (errorState != OnlineCalculatorError.None) return; 68 int i = 0; 69 int horizon = 1; 70 var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows, horizon) 71 .ToArray(); 72 foreach (var targetVariable in targetVariables) { 73 var targetValues = dataset.GetDoubleValues(targetVariable, rows); 74 double alpha; 75 double beta; 76 OnlineCalculatorError errorState; 77 OnlineLinearScalingParameterCalculator.Calculate(estimatedValues[i].Select(x => x.First()), targetValues, 78 out alpha, out beta, out errorState); 79 if (errorState != OnlineCalculatorError.None) return; 81 80 82 ConstantTreeNode alphaTreeNode = null; 83 ConstantTreeNode betaTreeNode = null; 84 // check if model has been scaled previously by analyzing the structure of the tree 85 var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0); 86 if (startNode.GetSubtree(0).Symbol is Addition) { 87 var addNode = startNode.GetSubtree(0); 88 if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) { 89 alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode; 90 var mulNode = addNode.GetSubtree(0); 91 if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) { 92 betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode; 81 ConstantTreeNode alphaTreeNode = null; 82 ConstantTreeNode betaTreeNode = null; 83 // check if model has been scaled previously by analyzing the structure of the tree 84 var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0); 85 if (startNode.GetSubtree(i).Symbol is Addition) { 86 var addNode = startNode.GetSubtree(i); 87 if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && 88 addNode.GetSubtree(1).Symbol is Constant) { 89 alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode; 90 var mulNode = addNode.GetSubtree(0); 91 if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) { 92 betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode; 93 } 93 94 } 94 95 } 95 } 96 // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes 97 if (alphaTreeNode != null && betaTreeNode != null) { 98 betaTreeNode.Value *= beta; 99 alphaTreeNode.Value *= beta; 100 alphaTreeNode.Value += alpha; 101 } else { 102 var mainBranch = startNode.GetSubtree(0); 103 startNode.RemoveSubtree(0); 104 var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha); 105 startNode.AddSubtree(scaledMainBranch); 106 } 96 // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes 97 if (alphaTreeNode != null && betaTreeNode != null) { 98 betaTreeNode.Value *= beta; 99 alphaTreeNode.Value *= beta; 100 alphaTreeNode.Value += alpha; 101 } else { 102 var mainBranch = startNode.GetSubtree(i); 103 startNode.RemoveSubtree(i); 104 var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha); 105 startNode.InsertSubtree(i, scaledMainBranch); 106 } 107 i++; 108 } // foreach 107 109 } 108 110 -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Views/3.4/HeuristicLab.Problems.DataAnalysis.Views-3.4.csproj
r7099 r7100 128 128 <DependentUpon>TimeSeriesPrognosisSolutionView.cs</DependentUpon> 129 129 </Compile> 130 <Compile Include="TimeSeriesPrognosis\TimeSeriesPrognosisSolutionErrorCharacteristicsCurveView.cs">131 <SubType>UserControl</SubType>132 </Compile>133 <Compile Include="TimeSeriesPrognosis\TimeSeriesPrognosisSolutionErrorCharacteristicsCurveView.Designer.cs">134 <DependentUpon>TimeSeriesPrognosisSolutionErrorCharacteristicsCurveView.cs</DependentUpon>135 </Compile>136 <Compile Include="TimeSeriesPrognosis\TimeSeriesPrognosisSolutionLineChartView.cs">137 <SubType>UserControl</SubType>138 </Compile>139 <Compile Include="TimeSeriesPrognosis\TimeSeriesPrognosisSolutionLineChartView.Designer.cs">140 <DependentUpon>TimeSeriesPrognosisSolutionLineChartView.cs</DependentUpon>141 </Compile>142 <Compile Include="TimeSeriesPrognosis\TimeSeriesPrognosisSolutionPrognosedValuesView.cs">143 <SubType>UserControl</SubType>144 </Compile>145 <Compile Include="TimeSeriesPrognosis\TimeSeriesPrognosisSolutionPrognosedValuesView.Designer.cs">146 <DependentUpon>TimeSeriesPrognosisSolutionPrognosedValuesView.cs</DependentUpon>147 </Compile>148 <Compile Include="TimeSeriesPrognosis\TimeSeriesPrognosisSolutionScatterPlotView.cs">149 <SubType>UserControl</SubType>150 </Compile>151 <Compile Include="TimeSeriesPrognosis\TimeSeriesPrognosisSolutionScatterPlotView.Designer.cs">152 <DependentUpon>TimeSeriesPrognosisSolutionScatterPlotView.cs</DependentUpon>153 </Compile>154 130 <Compile Include="DataAnalysisSolutionEvaluationView.cs"> 155 131 <SubType>UserControl</SubType> … … 280 256 <Compile Include="Regression\RegressionSolutionScatterPlotView.Designer.cs"> 281 257 <DependentUpon>RegressionSolutionScatterPlotView.cs</DependentUpon> 258 </Compile> 259 <Compile Include="TimeSeriesPrognosis\TimeSeriesPrognosisSolutionPrognosedValuesView.cs"> 260 <SubType>UserControl</SubType> 261 </Compile> 262 <Compile Include="TimeSeriesPrognosis\TimeSeriesPrognosisSolutionPrognosedValuesView.Designer.cs"> 263 <DependentUpon>TimeSeriesPrognosisSolutionPrognosedValuesView.cs</DependentUpon> 282 264 </Compile> 283 265 <None Include="HeuristicLab.snk" /> … … 396 378 </BootstrapperPackage> 397 379 </ItemGroup> 380 <ItemGroup /> 398 381 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 399 382 <!-- To modify your build process, add your task inside one of the targets below and uncomment it. -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Views/3.4/TimeSeriesPrognosis/TimeSeriesPrognosisSolutionPrognosedValuesView.cs
r6802 r7100 20 20 #endregion 21 21 using System; 22 using System.Collections.Generic; 22 23 using System.Linq; 23 24 using System.Windows.Forms; … … 32 33 public partial class TimeSeriesPrognosisSolutionEstimatedValuesView : DataAnalysisSolutionEvaluationView { 33 34 private const string TARGETVARIABLE_SERIES_NAME = "Target Variable"; 34 private const string PROGNOSEDVALUES_SERIES_NAME = "Prognosed Values (all)";35 35 private const string PROGNOSEDVALUES_TRAINING_SERIES_NAME = "Prognosed Values (training)"; 36 36 private const string PROGNOSEDVALUES_TEST_SERIES_NAME = "Prognosed Values (test)"; … … 85 85 else { 86 86 StringMatrix matrix = null; 87 List<string> columnNames = new List<string>(); 87 88 if (Content != null) { 88 string[,] values = new string[Content.ProblemData.Dataset.Rows, 7];89 columnNames.Add("Id"); 89 90 90 double[] target = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable).ToArray(); 91 var prognosed = Content.PrognosedValues.GetEnumerator(); 92 var prognosed_training = Content.PrognosedTrainingValues.GetEnumerator(); 93 var prognosed_test = Content.PrognosedTestValues.GetEnumerator(); 91 string[,] values = new string[Content.ProblemData.Dataset.Rows, 1 + 3 * Content.ProblemData.TargetVariables.Count()]; 92 foreach (var row in Enumerable.Range(0, Content.ProblemData.Dataset.Rows)) 93 values[row, 0] = row.ToString(); 94 94 95 foreach (var row in Content.ProblemData.TrainingIndizes) { 96 prognosed_training.MoveNext(); 97 values[row, 3] = prognosed_training.Current.ToString(); 98 } 95 var prognosedTraining = Content.PrognosedTrainingValues.ToArray(); 96 var prognosedTest = Content.PrognosedTestValues.ToArray(); 99 97 100 foreach (var row in Content.ProblemData.TestIndizes) {101 prognosed_test.MoveNext();102 values[row, 4] = prognosed_test.Current.ToString();103 }98 int i = 0; 99 int targetVariableIndex = 0; 100 foreach (var targetVariable in Content.ProblemData.TargetVariables) { 101 double[] target = Content.ProblemData.Dataset.GetDoubleValues(targetVariable).ToArray(); 104 102 105 foreach (var row in Enumerable.Range(0, Content.ProblemData.Dataset.Rows)) { 106 prognosed.MoveNext(); 107 double est = prognosed.Current; 108 double res = Math.Abs(est - target[row]); 109 values[row, 0] = row.ToString(); 110 values[row, 1] = target[row].ToString(); 111 values[row, 2] = est.ToString(); 112 values[row, 5] = Math.Abs(res).ToString(); 113 values[row, 6] = Math.Abs(res / est).ToString(); 114 } 103 var prognosedTrainingEnumerator = prognosedTraining[targetVariableIndex].GetEnumerator(); 104 foreach (var row in Content.ProblemData.TrainingIndizes) { 105 prognosedTrainingEnumerator.MoveNext(); 106 values[row, i + 2] = prognosedTrainingEnumerator.Current.ToString(); 107 } 108 109 var prognosedTestEnumerator = prognosedTest[targetVariableIndex].GetEnumerator(); 110 foreach (var row in Content.ProblemData.TestIndizes) { 111 prognosedTestEnumerator.MoveNext(); 112 values[row, i + 3] = prognosedTestEnumerator.Current.ToString(); 113 } 114 115 foreach (var row in Enumerable.Range(0, Content.ProblemData.Dataset.Rows)) { 116 values[row, i + 1] = target[row].ToString(); 117 } 118 119 columnNames.AddRange(new string[] { targetVariable + "(actual)", targetVariable + "(training)", targetVariable + "(test)" }); 120 i += 3; 121 targetVariableIndex++; 122 } // foreach 123 115 124 116 125 matrix = new StringMatrix(values); 117 matrix.ColumnNames = new string[] { "Id", TARGETVARIABLE_SERIES_NAME, PROGNOSEDVALUES_SERIES_NAME, PROGNOSEDVALUES_TRAINING_SERIES_NAME, PROGNOSEDVALUES_TEST_SERIES_NAME, "Absolute Error (all)", "Relative Error (all)" };126 matrix.ColumnNames = columnNames.ToArray(); 118 127 matrix.SortableView = true; 119 } 128 129 } // if 120 130 matrixView.Content = matrix; 121 131 } -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisProblemData.cs
r6802 r7100 34 34 [Item("TimeSeriesPrognosisProblemData", "Represents an item containing all data defining a time series prognosis problem.")] 35 35 public class TimeSeriesPrognosisProblemData : DataAnalysisProblemData, ITimeSeriesPrognosisProblemData { 36 protected const string TargetVariable ParameterName = "TargetVariable";36 protected const string TargetVariablesParameterName = "TargetVariables"; 37 37 38 38 #region default data … … 1541 1541 private static readonly Dataset defaultDataset; 1542 1542 private static readonly IEnumerable<string> defaultAllowedInputVariables; 1543 private static readonly string defaultTargetVariable;1543 private static readonly string[] defaultTargetVariables; 1544 1544 1545 1545 private static readonly TimeSeriesPrognosisProblemData emptyProblemData; … … 1552 1552 defaultDataset.Name = "Mackey-Glass (t=17) Time Series Benchmark Dataset"; 1553 1553 defaultAllowedInputVariables = new List<string>() { "x" }; 1554 defaultTargetVariable = "x";1554 defaultTargetVariables = new string[] { "x" }; 1555 1555 1556 1556 var problemData = new TimeSeriesPrognosisProblemData(); … … 1564 1564 problemData.Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", (IntRange)new IntRange(0, 0).AsReadOnly())); 1565 1565 problemData.Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", (IntRange)new IntRange(0, 0).AsReadOnly())); 1566 problemData.Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariable ParameterName, new ItemSet<StringValue>()));1566 problemData.Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariablesParameterName, new ItemSet<StringValue>())); 1567 1567 emptyProblemData = problemData; 1568 1568 } 1569 1569 #endregion 1570 1570 1571 public ConstrainedValueParameter<StringValue> TargetVariableParameter {1572 get { return ( ConstrainedValueParameter<StringValue>)Parameters[TargetVariableParameterName]; }1571 public ValueParameter<CheckedItemList<StringValue>> TargetVariablesParameter { 1572 get { return (ValueParameter<CheckedItemList<StringValue>>)Parameters[TargetVariablesParameterName]; } 1573 1573 } 1574 public string TargetVariable{1575 get { return TargetVariable Parameter.Value.Value; }1574 public IEnumerable<string> TargetVariables { 1575 get { return TargetVariablesParameter.Value.CheckedItems.Select(x => x.Value.Value); } 1576 1576 } 1577 1577 … … 1593 1593 1594 1594 public TimeSeriesPrognosisProblemData() 1595 : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable ) {1595 : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariables) { 1596 1596 } 1597 1597 1598 public TimeSeriesPrognosisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable)1598 public TimeSeriesPrognosisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, IEnumerable<string> targetVariables) 1599 1599 : base(dataset, allowedInputVariables) { 1600 1600 var variables = InputVariables.Select(x => x.AsReadOnly()).ToList(); 1601 Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>(variables), variables.Where(x => x.Value == targetVariable).First())); 1601 var targetVariablesList = new CheckedItemList<StringValue>(variables); 1602 foreach (var targetVar in targetVariables) { 1603 targetVariablesList.SetItemCheckedState(targetVariablesList.Single(x => x.Value == targetVar), true); 1604 } 1605 Parameters.Add(new FixedValueParameter<CheckedItemList<StringValue>>(TargetVariablesParameterName, targetVariablesList)); 1602 1606 RegisterParameterEvents(); 1603 1607 } 1604 1608 1605 1609 private void RegisterParameterEvents() { 1606 TargetVariable Parameter.ValueChanged += TargetVariableParameter_ValueChanged;1610 TargetVariablesParameter.Value.CheckedItemsChanged += TargetVariableParameter_ValueChanged; 1607 1611 } 1608 1612 … … 1619 1623 dataset.Name = Path.GetFileName(fileName); 1620 1624 1621 TimeSeriesPrognosisProblemData problemData = new TimeSeriesPrognosisProblemData(dataset, dataset.DoubleVariables, dataset.DoubleVariables. First());1625 TimeSeriesPrognosisProblemData problemData = new TimeSeriesPrognosisProblemData(dataset, dataset.DoubleVariables, dataset.DoubleVariables.Take(1)); 1622 1626 problemData.Name = "Data imported from " + Path.GetFileName(fileName); 1623 1627 return problemData; -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolution.cs
r6802 r7100 47 47 } 48 48 49 public override IEnumerable<double> PrognosedValues { 50 get { return GetPrognosedValues(Enumerable.Range(0, ProblemData.Dataset.Rows)); } 49 public override IEnumerable<IEnumerable<double>> PrognosedTrainingValues { 50 get { 51 return GetPrognosedValues(Enumerable.Range(ProblemData.TrainingPartition.Start, 1), 52 ProblemData.TrainingPartition.End - ProblemData.TrainingPartition.Start) 53 .First(); 54 } 51 55 } 52 public override IEnumerable<double> PrognosedTrainingValues { 53 get { return GetPrognosedValues(ProblemData.TrainingIndizes); } 56 public override IEnumerable<IEnumerable<double>> PrognosedTestValues { 57 get { 58 return GetPrognosedValues(Enumerable.Range(ProblemData.TestPartition.Start, 1), 59 ProblemData.TestPartition.End - ProblemData.TestPartition.Start) 60 .First(); 61 } 54 62 } 55 public override IEnumerable<double> PrognosedTestValues { 56 get { return GetPrognosedValues(ProblemData.TestIndizes); } 57 } 58 public override IEnumerable<double> GetPrognosedValues(IEnumerable<int> rows) { 59 return Model.GetPrognosedValues(ProblemData.Dataset, rows); 63 public override IEnumerable<IEnumerable<IEnumerable<double>>> GetPrognosedValues(IEnumerable<int> rows, int horizon) { 64 return Model.GetPrognosedValues(ProblemData.Dataset, rows, horizon); 60 65 } 61 66 -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolutionBase.cs
r7099 r7100 20 20 #endregion 21 21 22 using System.Collections.Concurrent; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 57 58 } 58 59 59 public abstract IEnumerable<double> PrognosedValues { get; } 60 public abstract IEnumerable<double> PrognosedTrainingValues { get; } 61 public abstract IEnumerable<double> PrognosedTestValues { get; } 62 public abstract IEnumerable<double> GetPrognosedValues(IEnumerable<int> rows); 60 public abstract IEnumerable<IEnumerable<double>> PrognosedTrainingValues { get; } 61 public abstract IEnumerable<IEnumerable<double>> PrognosedTestValues { get; } 62 public abstract IEnumerable<IEnumerable<IEnumerable<double>>> GetPrognosedValues(IEnumerable<int> rows, int horizon); 63 63 64 64 #region Results 65 public double TrainingMeanSquaredError {66 get { return ((Double Value)this[TrainingMeanSquaredErrorResultName].Value).Value; }67 private set { ((DoubleValue)this[TrainingMeanSquaredErrorResultName].Value).Value = value; }68 } 69 public double TestMeanSquaredError {70 get { return ((Double Value)this[TestMeanSquaredErrorResultName].Value).Value; }71 private set { ((DoubleValue)this[TestMeanSquaredErrorResultName].Value).Value = value; }72 } 73 public double TrainingMeanAbsoluteError {74 get { return ((Double Value)this[TrainingMeanAbsoluteErrorResultName].Value).Value; }75 private set { ((DoubleValue)this[TrainingMeanAbsoluteErrorResultName].Value).Value = value; }76 } 77 public double TestMeanAbsoluteError {78 get { return ((Double Value)this[TestMeanAbsoluteErrorResultName].Value).Value; }79 private set { ((DoubleValue)this[TestMeanAbsoluteErrorResultName].Value).Value = value; }80 } 81 public double TrainingRSquared {82 get { return ((Double Value)this[TrainingSquaredCorrelationResultName].Value).Value; }83 private set { ((DoubleValue)this[TrainingSquaredCorrelationResultName].Value).Value = value; }84 } 85 public double TestRSquared {86 get { return ((Double Value)this[TestSquaredCorrelationResultName].Value).Value; }87 private set { ((DoubleValue)this[TestSquaredCorrelationResultName].Value).Value = value; }88 } 89 public double TrainingRelativeError {90 get { return ((Double Value)this[TrainingRelativeErrorResultName].Value).Value; }91 private set { ((DoubleValue)this[TrainingRelativeErrorResultName].Value).Value = value; }92 } 93 public double TestRelativeError {94 get { return ((Double Value)this[TestRelativeErrorResultName].Value).Value; }95 private set { ((DoubleValue)this[TestRelativeErrorResultName].Value).Value = value; }96 } 97 public double TrainingNormalizedMeanSquaredError {98 get { return ((Double Value)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value; }99 private set { ((DoubleValue)this[TrainingNormalizedMeanSquaredErrorResultName].Value).Value = value; }100 } 101 public double TestNormalizedMeanSquaredError {102 get { return ((Double Value)this[TestNormalizedMeanSquaredErrorResultName].Value).Value; }103 private set { ((DoubleValue)this[TestNormalizedMeanSquaredErrorResultName].Value).Value = value; }104 } 105 public double TrainingDirectionalSymmetry {106 get { return ((Double Value)this[TrainingDirectionalSymmetryResultName].Value).Value; }107 private set { ((DoubleValue)this[TrainingDirectionalSymmetryResultName].Value).Value = value; }108 } 109 public double TestDirectionalSymmetry {110 get { return ((Double Value)this[TestDirectionalSymmetryResultName].Value).Value; }111 private set { ((DoubleValue)this[TestDirectionalSymmetryResultName].Value).Value = value; }112 } 113 public double TrainingWeightedDirectionalSymmetry {114 get { return ((Double Value)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value; }115 private set { ((DoubleValue)this[TrainingWeightedDirectionalSymmetryResultName].Value).Value = value; }116 } 117 public double TestWeightedDirectionalSymmetry {118 get { return ((Double Value)this[TestWeightedDirectionalSymmetryResultName].Value).Value; }119 private set { ((DoubleValue)this[TestWeightedDirectionalSymmetryResultName].Value).Value = value; }120 } 121 public double TrainingTheilsUStatistic {122 get { return ((Double Value)this[TrainingTheilsUStatisticResultName].Value).Value; }123 private set { ((DoubleValue)this[TrainingTheilsUStatisticResultName].Value).Value = value; }124 } 125 public double TestTheilsUStatistic {126 get { return ((Double Value)this[TestTheilsUStatisticResultName].Value).Value; }127 private set { ((DoubleValue)this[TestTheilsUStatisticResultName].Value).Value = value; }65 public double[] TrainingMeanSquaredError { 66 get { return ((DoubleArray)this[TrainingMeanSquaredErrorResultName].Value).ToArray(); } 67 private set { this[TrainingMeanSquaredErrorResultName].Value = new DoubleArray(value); } 68 } 69 public double[] TestMeanSquaredError { 70 get { return ((DoubleArray)this[TestMeanSquaredErrorResultName].Value).ToArray(); } 71 private set { this[TestMeanSquaredErrorResultName].Value = new DoubleArray(value); } 72 } 73 public double[] TrainingMeanAbsoluteError { 74 get { return ((DoubleArray)this[TrainingMeanAbsoluteErrorResultName].Value).ToArray(); } 75 private set { this[TrainingMeanAbsoluteErrorResultName].Value = new DoubleArray(value); } 76 } 77 public double[] TestMeanAbsoluteError { 78 get { return ((DoubleArray)this[TestMeanAbsoluteErrorResultName].Value).ToArray(); } 79 private set { this[TestMeanAbsoluteErrorResultName].Value = new DoubleArray(value); } 80 } 81 public double[] TrainingRSquared { 82 get { return ((DoubleArray)this[TrainingSquaredCorrelationResultName].Value).ToArray(); } 83 private set { this[TrainingSquaredCorrelationResultName].Value = new DoubleArray(value); } 84 } 85 public double[] TestRSquared { 86 get { return ((DoubleArray)this[TestSquaredCorrelationResultName].Value).ToArray(); } 87 private set { this[TestSquaredCorrelationResultName].Value = new DoubleArray(value); } 88 } 89 public double[] TrainingRelativeError { 90 get { return ((DoubleArray)this[TrainingRelativeErrorResultName].Value).ToArray(); } 91 private set { this[TrainingRelativeErrorResultName].Value = new DoubleArray(value); } 92 } 93 public double[] TestRelativeError { 94 get { return ((DoubleArray)this[TestRelativeErrorResultName].Value).ToArray(); } 95 private set { this[TestRelativeErrorResultName].Value = new DoubleArray(value); } 96 } 97 public double[] TrainingNormalizedMeanSquaredError { 98 get { return ((DoubleArray)this[TrainingNormalizedMeanSquaredErrorResultName].Value).ToArray(); } 99 private set { this[TrainingNormalizedMeanSquaredErrorResultName].Value = new DoubleArray(value); } 100 } 101 public double[] TestNormalizedMeanSquaredError { 102 get { return ((DoubleArray)this[TestNormalizedMeanSquaredErrorResultName].Value).ToArray(); } 103 private set { this[TestNormalizedMeanSquaredErrorResultName].Value = new DoubleArray(value); } 104 } 105 public double[] TrainingDirectionalSymmetry { 106 get { return ((DoubleArray)this[TrainingDirectionalSymmetryResultName].Value).ToArray(); } 107 private set { this[TrainingDirectionalSymmetryResultName].Value = new DoubleArray(value); } 108 } 109 public double[] TestDirectionalSymmetry { 110 get { return ((DoubleArray)this[TestDirectionalSymmetryResultName].Value).ToArray(); } 111 private set { this[TestDirectionalSymmetryResultName].Value = new DoubleArray(value); } 112 } 113 public double[] TrainingWeightedDirectionalSymmetry { 114 get { return ((DoubleArray)this[TrainingWeightedDirectionalSymmetryResultName].Value).ToArray(); } 115 private set { this[TrainingWeightedDirectionalSymmetryResultName].Value = new DoubleArray(value); } 116 } 117 public double[] TestWeightedDirectionalSymmetry { 118 get { return ((DoubleArray)this[TestWeightedDirectionalSymmetryResultName].Value).ToArray(); } 119 private set { this[TestWeightedDirectionalSymmetryResultName].Value = new DoubleArray(value); } 120 } 121 public double[] TrainingTheilsUStatistic { 122 get { return ((DoubleArray)this[TrainingTheilsUStatisticResultName].Value).ToArray(); } 123 private set { this[TrainingTheilsUStatisticResultName].Value = new DoubleArray(value); } 124 } 125 public double[] TestTheilsUStatistic { 126 get { return ((DoubleArray)this[TestTheilsUStatisticResultName].Value).ToArray(); } 127 private set { this[TestTheilsUStatisticResultName].Value = new DoubleArray(value); } 128 128 } 129 129 #endregion … … 136 136 protected TimeSeriesPrognosisSolutionBase(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData) 137 137 : base(model, problemData) { 138 Add(new Result(TrainingMeanSquaredErrorResultName, "Mean of squared errors of the model on the training partition", new Double Value()));139 Add(new Result(TestMeanSquaredErrorResultName, "Mean of squared errors of the model on the test partition", new Double Value()));140 Add(new Result(TrainingMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the training partition", new Double Value()));141 Add(new Result(TestMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the test partition", new Double Value()));142 Add(new Result(TrainingSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition", new Double Value()));143 Add(new Result(TestSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new Double Value()));144 Add(new Result(TrainingRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the training partition", new PercentValue()));145 Add(new Result(TestRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the test partition", new PercentValue()));146 Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the training partition", new Double Value()));147 Add(new Result(TestNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the test partition", new Double Value()));148 Add(new Result(TrainingDirectionalSymmetryResultName, "The average directional symmetry of the forecasts of the model on the training partition", new PercentValue()));149 Add(new Result(TestDirectionalSymmetryResultName, "The average directional symmetry of the forecasts of the model on the test partition", new PercentValue()));150 Add(new Result(TrainingWeightedDirectionalSymmetryResultName, "The average weighted directional symmetry of the forecasts of the model on the training partition", new Double Value()));151 Add(new Result(TestWeightedDirectionalSymmetryResultName, "The average weighted directional symmetry of the forecasts of the model on the test partition", new Double Value()));152 Add(new Result(TrainingTheilsUStatisticResultName, "The average Theil's U statistic of the forecasts of the model on the training partition", new Double Value()));153 Add(new Result(TestTheilsUStatisticResultName, "The average Theil's U statistic of the forecasts of the model on the test partition", new Double Value()));138 Add(new Result(TrainingMeanSquaredErrorResultName, "Mean of squared errors of the model on the training partition", new DoubleArray())); 139 Add(new Result(TestMeanSquaredErrorResultName, "Mean of squared errors of the model on the test partition", new DoubleArray())); 140 Add(new Result(TrainingMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the training partition", new DoubleArray())); 141 Add(new Result(TestMeanAbsoluteErrorResultName, "Mean of absolute errors of the model on the test partition", new DoubleArray())); 142 Add(new Result(TrainingSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the training partition", new DoubleArray())); 143 Add(new Result(TestSquaredCorrelationResultName, "Squared Pearson's correlation coefficient of the model output and the actual values on the test partition", new DoubleArray())); 144 Add(new Result(TrainingRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the training partition", new DoubleArray())); 145 Add(new Result(TestRelativeErrorResultName, "Average of the relative errors of the model output and the actual values on the test partition", new DoubleArray())); 146 Add(new Result(TrainingNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the training partition", new DoubleArray())); 147 Add(new Result(TestNormalizedMeanSquaredErrorResultName, "Normalized mean of squared errors of the model on the test partition", new DoubleArray())); 148 Add(new Result(TrainingDirectionalSymmetryResultName, "The average directional symmetry of the forecasts of the model on the training partition", new DoubleArray())); 149 Add(new Result(TestDirectionalSymmetryResultName, "The average directional symmetry of the forecasts of the model on the test partition", new DoubleArray())); 150 Add(new Result(TrainingWeightedDirectionalSymmetryResultName, "The average weighted directional symmetry of the forecasts of the model on the training partition", new DoubleArray())); 151 Add(new Result(TestWeightedDirectionalSymmetryResultName, "The average weighted directional symmetry of the forecasts of the model on the test partition", new DoubleArray())); 152 Add(new Result(TrainingTheilsUStatisticResultName, "The average Theil's U statistic of the forecasts of the model on the training partition", new DoubleArray())); 153 Add(new Result(TestTheilsUStatisticResultName, "The average Theil's U statistic of the forecasts of the model on the test partition", new DoubleArray())); 154 154 } 155 155 … … 160 160 161 161 protected void CalculateResults() { 162 OnlineCalculatorError errorState; 163 /* 162 164 double[] estimatedTrainingValues = PrognosedTrainingValues.ToArray(); // cache values 163 165 double[] originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToArray(); … … 165 167 double[] originalTestValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndizes).ToArray(); 166 168 167 OnlineCalculatorError errorState;168 169 double trainingMse = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState); 169 170 TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMse : double.NaN; … … 190 191 double testNmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState); 191 192 TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNmse : double.NaN; 192 193 var startTrainingValues = originalTrainingValues; 194 // each continuation is only one element long 195 var actualContinuationsTraining = from x in originalTrainingValues.Skip(1) 196 select Enumerable.Repeat(x, 1); 197 // each forecast is only one elemnt long 198 // disregards the first estimated value (we could include this again by extending the list of original values by one step to the left 199 // this is the easier way 200 var predictedContinuationsTraining = from x in estimatedTrainingValues.Skip(1) 201 select Enumerable.Repeat(x, 1); 202 203 var startTestValues = originalTestValues; 204 var actualContinuationsTest = from x in originalTestValues.Skip(1) 205 select Enumerable.Repeat(x, 1); 206 var predictedContinuationsTest = from x in estimatedTestValues.Skip(1) 207 select Enumerable.Repeat(x, 1); 208 209 double trainingDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(startTrainingValues, actualContinuationsTraining, predictedContinuationsTraining, out errorState); 210 TrainingDirectionalSymmetry = errorState == OnlineCalculatorError.None ? trainingDirectionalSymmetry : double.NaN; 211 double testDirectionalSymmetry = OnlineDirectionalSymmetryCalculator.Calculate(startTestValues, actualContinuationsTest, predictedContinuationsTest, out errorState); 212 TestDirectionalSymmetry = errorState == OnlineCalculatorError.None ? testDirectionalSymmetry : double.NaN; 213 214 double trainingWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(startTrainingValues, actualContinuationsTraining, predictedContinuationsTraining, out errorState); 215 TrainingWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? trainingWeightedDirectionalSymmetry : double.NaN; 216 double testWeightedDirectionalSymmetry = OnlineWeightedDirectionalSymmetryCalculator.Calculate(startTestValues, actualContinuationsTest, predictedContinuationsTest, out errorState); 217 TestWeightedDirectionalSymmetry = errorState == OnlineCalculatorError.None ? testWeightedDirectionalSymmetry : double.NaN; 218 219 double trainingTheilsU = OnlineTheilsUStatisticCalculator.Calculate(startTrainingValues, actualContinuationsTraining, predictedContinuationsTraining, out errorState); 220 TrainingTheilsUStatistic = errorState == OnlineCalculatorError.None ? trainingTheilsU : double.NaN; 221 double testTheilsU = OnlineTheilsUStatisticCalculator.Calculate(startTestValues, actualContinuationsTest, predictedContinuationsTest, out errorState); 222 TestTheilsUStatistic = errorState == OnlineCalculatorError.None ? testTheilsU : double.NaN; 193 */ 194 195 double[] trainingDs = new double[ProblemData.TargetVariables.Count()]; 196 double[] testDs = new double[ProblemData.TargetVariables.Count()]; 197 198 double[] trainingWds = new double[ProblemData.TargetVariables.Count()]; 199 double[] testWds = new double[ProblemData.TargetVariables.Count()]; 200 201 double[] trainingTheilsU = new double[ProblemData.TargetVariables.Count()]; 202 double[] testTheilsU = new double[ProblemData.TargetVariables.Count()]; 203 int i = 0; 204 var predictedContinuationTraining = PrognosedTrainingValues.ToArray(); 205 var predictedContinuationTest = PrognosedTestValues.ToArray(); 206 207 foreach (var targetVariable in ProblemData.TargetVariables) { 208 var actualTrainingValues = ProblemData.Dataset.GetDoubleValues(targetVariable, ProblemData.TrainingIndizes).ToArray(); 209 var startTrainingValues = actualTrainingValues.Take(1); 210 // only one continuation (but the full training set) 211 var actualContinuationTraining = actualTrainingValues.Skip(1); 212 213 var actualTestValues = ProblemData.Dataset.GetDoubleValues(targetVariable, ProblemData.TestIndizes).ToArray(); 214 var startTestValues = actualTestValues.Take(1); 215 // only one continuation (but the full training set) 216 var actualContinuationTest = actualTestValues.Skip(1); 217 218 219 trainingDs[i] = OnlineDirectionalSymmetryCalculator.Calculate(startTrainingValues, 220 Enumerable.Repeat(actualContinuationTraining, 1), 221 Enumerable.Repeat(predictedContinuationTraining[i], 1), out errorState); 222 if (errorState != OnlineCalculatorError.None) trainingDs[i] = double.NaN; 223 224 testDs[i] = OnlineDirectionalSymmetryCalculator.Calculate(startTestValues, 225 Enumerable.Repeat(actualContinuationTest, 1), 226 Enumerable.Repeat(predictedContinuationTest[i], 1), out errorState); 227 if (errorState != OnlineCalculatorError.None) testDs[i] = double.NaN; 228 229 trainingWds[i] = 230 OnlineWeightedDirectionalSymmetryCalculator.Calculate(startTrainingValues, 231 Enumerable.Repeat(actualContinuationTraining, 1), 232 Enumerable.Repeat(predictedContinuationTraining[i], 1), out errorState); 233 if (errorState != OnlineCalculatorError.None) trainingWds[i] = double.NaN; 234 235 testWds[i] = OnlineWeightedDirectionalSymmetryCalculator.Calculate(startTestValues, 236 Enumerable.Repeat(actualContinuationTest, 1), 237 Enumerable.Repeat(predictedContinuationTest[i], 1), out errorState); 238 if (errorState != OnlineCalculatorError.None) testWds[i] = double.NaN; 239 240 trainingTheilsU[i] = OnlineTheilsUStatisticCalculator.Calculate(startTrainingValues, 241 Enumerable.Repeat(actualContinuationTraining, 1), 242 Enumerable.Repeat(predictedContinuationTraining[i], 1), out errorState); 243 if (errorState != OnlineCalculatorError.None) trainingTheilsU[i] = double.NaN; 244 245 testTheilsU[i] = OnlineTheilsUStatisticCalculator.Calculate(startTestValues, 246 Enumerable.Repeat(actualContinuationTest, 1), 247 Enumerable.Repeat(predictedContinuationTest[i], 1), out errorState); 248 if (errorState != OnlineCalculatorError.None) testTheilsU[i] = double.NaN; 249 i++; 250 } 251 252 TrainingDirectionalSymmetry = trainingDs; 253 TestDirectionalSymmetry = testDs; 254 TrainingWeightedDirectionalSymmetry = trainingWds; 255 TestWeightedDirectionalSymmetry = testWds; 256 TrainingTheilsUStatistic = trainingTheilsU; 257 TestTheilsUStatistic = testTheilsU; 223 258 } 224 259 } -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/TimeSeriesPrognosis/ITimeSeriesPrognosisModel.cs
r6802 r7100 23 23 namespace HeuristicLab.Problems.DataAnalysis { 24 24 public interface ITimeSeriesPrognosisModel : IDataAnalysisModel { 25 IEnumerable< double> GetPrognosedValues(Dataset dataset, IEnumerable<int> rows);25 IEnumerable<IEnumerable<IEnumerable<double>>> GetPrognosedValues(Dataset dataset, IEnumerable<int> rows, int horizon); 26 26 ITimeSeriesPrognosisSolution CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData); 27 27 } -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/TimeSeriesPrognosis/ITimeSeriesPrognosisProblemData.cs
r6802 r7100 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 namespace HeuristicLab.Problems.DataAnalysis { 23 24 public interface ITimeSeriesPrognosisProblemData : IDataAnalysisProblemData { 24 string TargetVariable{ get; }25 IEnumerable<string> TargetVariables { get; } 25 26 } 26 27 } -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/TimeSeriesPrognosis/ITimeSeriesPrognosisSolution.cs
r6802 r7100 26 26 new ITimeSeriesPrognosisProblemData ProblemData { get; set; } 27 27 28 IEnumerable<double> PrognosedTrainingValues { get; } 29 IEnumerable<double> PrognosedTestValues { get; } 30 IEnumerable<double> PrognosedValues { get; } 31 IEnumerable<double> GetPrognosedValues(IEnumerable<int> rows); 28 IEnumerable<IEnumerable<double>> PrognosedTrainingValues { get; } 29 IEnumerable<IEnumerable<double>> PrognosedTestValues { get; } 30 IEnumerable<IEnumerable<IEnumerable<double>>> GetPrognosedValues(IEnumerable<int> rows, int horizon); 32 31 33 double TrainingMeanSquaredError { get; }34 double TestMeanSquaredError { get; }35 double TrainingMeanAbsoluteError { get; }36 double TestMeanAbsoluteError { get; }37 double TrainingRSquared { get; }38 double TestRSquared { get; }39 double TrainingRelativeError { get; }40 double TestRelativeError { get; }41 double TrainingNormalizedMeanSquaredError { get; }42 double TestNormalizedMeanSquaredError { get; }43 double TrainingTheilsUStatistic { get; }44 double TestTheilsUStatistic { get; }45 double TrainingDirectionalSymmetry { get; }46 double TestDirectionalSymmetry { get; }47 double TrainingWeightedDirectionalSymmetry { get; }48 double TestWeightedDirectionalSymmetry { get; }49 } 32 double[] TrainingMeanSquaredError { get; } 33 double[] TestMeanSquaredError { get; } 34 double[] TrainingMeanAbsoluteError { get; } 35 double[] TestMeanAbsoluteError { get; } 36 double[] TrainingRSquared { get; } 37 double[] TestRSquared { get; } 38 double[] TrainingRelativeError { get; } 39 double[] TestRelativeError { get; } 40 double[] TrainingNormalizedMeanSquaredError { get; } 41 double[] TestNormalizedMeanSquaredError { get; } 42 double[] TrainingTheilsUStatistic { get; } 43 double[] TestTheilsUStatistic { get; } 44 double[] TrainingDirectionalSymmetry { get; } 45 double[] TestDirectionalSymmetry { get; } 46 double[] TrainingWeightedDirectionalSymmetry { get; } 47 double[] TestWeightedDirectionalSymmetry { get; } 48 } 50 49 }
Note: See TracChangeset
for help on using the changeset viewer.