Changeset 4475 for branches/DataAnalysis
- Timestamp:
- 09/23/10 13:49:30 (14 years ago)
- Location:
- branches/DataAnalysis
- Files:
-
- 6 added
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Views/3.3/InteractiveSymbolicTimeSeriesPrognosisSolutionSimplifierView.cs
r4464 r4475 95 95 int samplesEnd = Content.ProblemData.TrainingSamplesEnd.Value; 96 96 97 double[] alpha, beta;98 97 double quality; 99 98 conditionVariableName = Content.ConditionalEvaluationVariable; 100 99 targetVariables = Content.ProblemData.TargetVariables.CheckedItems.Select(x => x.Value.Value); 101 100 int nTargetVariables = Content.ProblemData.TargetVariables.CheckedItems.Count(); 102 lowerEstimationLimit = new DoubleArray(Enumerable.Repeat(double.NegativeInfinity, nTargetVariables).ToArray());103 upperEstimationLimit = new DoubleArray(Enumerable.Repeat(double.PositiveInfinity, nTargetVariables).ToArray());104 101 interpreter = Content.Model.Interpreter; 105 102 horizon = Content.Horizon; 106 103 IEnumerable<int> rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart); 107 SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.CalculateScalingParameters(simplifiedExpressionTree, 108 Content.ProblemData, interpreter, 109 conditionVariableName, rows, 110 out beta, out alpha); 111 112 quality = SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.Evaluate(simplifiedExpressionTree, Content.ProblemData, interpreter, 113 conditionVariableName, rows, horizon, 114 lowerEstimationLimit, upperEstimationLimit, 115 beta, alpha); 104 if (!string.IsNullOrEmpty(conditionVariableName)) { 105 rows = (from row in rows 106 where !Content.ProblemData.Dataset[conditionVariableName, row].IsAlmost(0.0) 107 select row).ToList(); 108 } 109 lowerEstimationLimit = new DoubleArray(nTargetVariables); 110 upperEstimationLimit = new DoubleArray(nTargetVariables); 111 for (int i = 0; i < nTargetVariables; i++) { 112 lowerEstimationLimit[i] = Content.GetLowerEstimationLimit(i); 113 upperEstimationLimit[i] = Content.GetUpperEstimationLimit(i); 114 } 115 quality = SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.Calculate(simplifiedExpressionTree, Content.ProblemData, interpreter, 116 rows, horizon, 117 lowerEstimationLimit, upperEstimationLimit); 116 118 117 119 this.CalculateReplacementNodes(); 118 120 119 this.CalculateNodeImpacts(simplifiedExpressionTree, simplifiedExpressionTree.Root, quality );121 this.CalculateNodeImpacts(simplifiedExpressionTree, simplifiedExpressionTree.Root, quality, rows); 120 122 // show only interesing part 121 123 this.treeChart.Tree = new SymbolicExpressionTree(simplifiedExpressionTree.Root.SubTrees[0]); … … 134 136 while (start.SubTrees.Count > 0) start.RemoveSubTree(0); 135 137 start.AddSubTree(node); 136 double constantTreeNodeValue = interpreter.GetSymbolicExpressionTreeValues(tree, Content.ProblemData.Dataset, targetVariables, trainingSamples, 1).Select(x => x[0]).Median(); 138 // we only want a scalar replacement value for the single branch that should be evaluated 139 // so assume we only create an estimation for the first target variable 140 double constantTreeNodeValue = interpreter.GetSymbolicExpressionTreeValues(tree, Content.ProblemData.Dataset, targetVariables.Take(1) , trainingSamples, 1).Select(x => x[0]).Median(); 137 141 ConstantTreeNode constantTreeNode = MakeConstantTreeNode(constantTreeNodeValue); 138 142 replacementNodes[node] = constantTreeNode; … … 141 145 } 142 146 143 private void CalculateNodeImpacts(SymbolicExpressionTree tree, SymbolicExpressionTreeNode currentTreeNode, double originalQuality ) {147 private void CalculateNodeImpacts(SymbolicExpressionTree tree, SymbolicExpressionTreeNode currentTreeNode, double originalQuality, IEnumerable<int> rows) { 144 148 foreach (SymbolicExpressionTreeNode childNode in currentTreeNode.SubTrees.ToList()) { 145 149 if (!(childNode.Symbol is StartSymbol || childNode.Symbol is ProgramRootSymbol)) { 146 150 SwitchNode(currentTreeNode, childNode, replacementNodes[childNode]); 147 int samplesStart = Content.ProblemData.TrainingSamplesStart.Value;148 int samplesEnd = Content.ProblemData.TrainingSamplesEnd.Value;149 double[] alpha;150 double[] beta;151 151 int horizon = Content.Horizon; 152 IEnumerable<int> rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart); 153 SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.CalculateScalingParameters(tree, 154 Content.ProblemData, interpreter, 155 conditionVariableName, rows, 156 out beta, out alpha); 157 158 double newQuality = SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.Evaluate(tree, Content.ProblemData, interpreter, 159 conditionVariableName, rows, horizon, 160 lowerEstimationLimit, upperEstimationLimit, 161 beta, alpha); 152 153 double newQuality = SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.Calculate(tree, Content.ProblemData, interpreter, 154 rows, horizon, 155 lowerEstimationLimit, upperEstimationLimit); 162 156 163 157 nodeImpacts[childNode] = newQuality / originalQuality; 164 158 SwitchNode(currentTreeNode, replacementNodes[childNode], childNode); 165 159 } 166 CalculateNodeImpacts(tree, childNode, originalQuality );160 CalculateNodeImpacts(tree, childNode, originalQuality, rows); 167 161 } 168 162 } -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis-3.3.csproj
r4401 r4475 108 108 <Compile Include="HeuristicLabProblemsDataAnalysisMultiVariateTimeSeriesPrognosisPlugin.cs" /> 109 109 <Compile Include="Symbolic\Analyzer\ValidationBestScaledSymbolicTimeSeriesPrognosisSolutionAnalyzer.cs" /> 110 <Compile Include="Symbolic\Evaluators\SymbolicTimeSeriesPrognosisEvaluator.cs" /> 111 <Compile Include="Symbolic\Evaluators\SymbolicTimeSeriesPrognosisMahalanobisEvaluator.cs"> 112 <SubType>Code</SubType> 113 </Compile> 110 114 <Compile Include="Symbolic\SymbolicTimeSeriesPrognosisSolution.cs" /> 111 115 <Compile Include="Symbolic\SymbolicTimeSeriesPrognosisModel.cs" /> 112 <Compile Include="Symbolic\Evaluators\SymbolicTimeSeriesPrognosisNormalizedMseEvaluator.cs" />113 116 <Compile Include="Symbolic\Evaluators\SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.cs" /> 114 117 <Compile Include="Symbolic\Interfaces\ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator.cs" /> -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Analyzer/ValidationBestScaledSymbolicTimeSeriesPrognosisSolutionAnalyzer.cs
r4457 r4475 125 125 get { return (OptionalValueParameter<StringValue>)Parameters[ConditionVariableParameterName]; } 126 126 } 127 public ScopeTreeLookupParameter<DoubleArray> AlphaParameter {128 get { return (ScopeTreeLookupParameter<DoubleArray>)Parameters[AlphaParameterName]; }129 }130 public ScopeTreeLookupParameter<DoubleArray> BetaParameter {131 get { return (ScopeTreeLookupParameter<DoubleArray>)Parameters[BetaParameterName]; }132 }133 127 public IValueLookupParameter<ISymbolicTimeSeriesExpressionInterpreter> SymbolicExpressionTreeInterpreterParameter { 134 128 get { return (IValueLookupParameter<ISymbolicTimeSeriesExpressionInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; } … … 137 131 get { return (IValueLookupParameter<MultiVariateDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; } 138 132 } 133 public ILookupParameter<ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator> EvaluatorParameter { 134 get { return (ILookupParameter<ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator>)Parameters["Evaluator"]; } 135 } 139 136 public IValueLookupParameter<IntValue> ValidationSamplesStartParameter { 140 137 get { return (IValueLookupParameter<IntValue>)Parameters[ValidationSamplesStartParameterName]; } … … 172 169 public IValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter { 173 170 get { return (IValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; } 171 } 172 public ILookupParameter<BoolValue> MaximizationParameter { 173 get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; } 174 174 } 175 175 #endregion … … 181 181 get { return SymbolicExpressionTreeParameter.ActualValue; } 182 182 } 183 public ItemArray<DoubleArray> Alpha {184 get { return AlphaParameter.ActualValue; }185 }186 public ItemArray<DoubleArray> Beta {187 get { return BetaParameter.ActualValue; }188 }189 183 public ISymbolicTimeSeriesExpressionInterpreter SymbolicExpressionTreeInterpreter { 190 184 get { return SymbolicExpressionTreeInterpreterParameter.ActualValue; } … … 222 216 public PercentValue RelativeNumberOfEvaluatedSamples { 223 217 get { return RelativeNumberOfEvaluatedSamplesParameter.Value; } 218 } 219 public BoolValue Maximization { 220 get { return MaximizationParameter.ActualValue; } 224 221 } 225 222 #endregion … … 230 227 Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze.")); 231 228 Parameters.Add(new OptionalValueParameter<StringValue>(ConditionVariableParameterName, "The name of the condition variable indicating if a row should be considered for evaluation or not.")); 232 Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>(AlphaParameterName, "The alpha parameter for linear scaling."));233 Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>(BetaParameterName, "The beta parameter for linear scaling."));234 229 Parameters.Add(new ValueLookupParameter<ISymbolicTimeSeriesExpressionInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used for the analysis of symbolic expression trees.")); 235 230 Parameters.Add(new ValueLookupParameter<MultiVariateDataAnalysisProblemData>(ProblemDataParameterName, "The problem data for which the symbolic expression tree is a solution.")); … … 237 232 Parameters.Add(new ValueLookupParameter<IntValue>(ValidationSamplesEndParameterName, "The last index of the validation partition of the data set.")); 238 233 Parameters.Add(new ValueLookupParameter<IntValue>(PredictionHorizonParameterName, "The number of time steps for which to create a forecast.")); 234 Parameters.Add(new LookupParameter<ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator>("Evaluator", "")); 239 235 Parameters.Add(new ValueLookupParameter<DoubleArray>(UpperEstimationLimitParameterName, "The upper estimation limit that was set for the evaluation of the symbolic expression trees.")); 240 236 Parameters.Add(new ValueLookupParameter<DoubleArray>(LowerEstimationLimitParameterName, "The lower estimation limit that was set for the evaluation of the symbolic expression trees.")); … … 246 242 Parameters.Add(new LookupParameter<DataTable>(VariableFrequenciesParameterName, "The variable frequencies table to use for the calculation of variable impacts")); 247 243 Parameters.Add(new ValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation between the start and end index.", new PercentValue(1))); 248 244 Parameters.Add(new LookupParameter<BoolValue>("Maximization")); 249 245 } 250 246 … … 252 248 private ValidationBestScaledSymbolicTimeSeriesPrognosisSolutionAnalyzer(bool deserializing) : base(deserializing) { } 253 249 250 [StorableHook(Persistence.Default.CompositeSerializers.Storable.HookType.AfterDeserialization)] 251 private void Initialize() { 252 //if (!Parameters.ContainsKey("Evaluator")) { 253 // Parameters.Add(new LookupParameter<ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator>("Evaluator", "")); 254 //} 255 } 256 254 257 public override IOperation Apply() { 255 var alphas = Alpha;256 var betas = Beta;257 258 var trees = SymbolicExpressionTree; 258 259 259 IEnumerable<SymbolicExpressionTree> scaledTrees; 260 if (alphas.Length == trees.Length) { 261 scaledTrees = from i in Enumerable.Range(0, trees.Length) 262 select SymbolicVectorRegressionSolutionLinearScaler.Scale(trees[i], betas[i].ToArray(), alphas[i].ToArray()); 263 } else { 264 scaledTrees = trees; 265 } 260 ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator evaluator = EvaluatorParameter.ActualValue; 266 261 267 262 int trainingStart = ProblemData.TrainingSamplesStart.Value; … … 275 270 int rowCount = (int)Math.Ceiling((validationEnd - validationStart) * RelativeNumberOfEvaluatedSamples.Value); 276 271 IEnumerable<int> rows = RandomEnumerable.SampleRandomNumbers(Random.Next(), validationStart, validationEnd, rowCount); 277 double bestValidation Nmse =double.MaxValue;272 double bestValidationQuality = Maximization.Value ? double.MinValue : double.MaxValue; 278 273 SymbolicExpressionTree bestTree = null; 279 274 string conditionalVariableName = ConditionVariableName == null ? null : ConditionVariableName.Value; 280 foreach (var tree in scaledTrees) { 281 double validationNmse; 282 validationNmse = SymbolicTimeSeriesPrognosisNormalizedMseEvaluator.Evaluate(tree, ProblemData, 283 SymbolicExpressionTreeInterpreter, conditionalVariableName, 284 rows, PredictionHorizon.Value, LowerEstimationLimit, UpperEstimationLimit); 285 if (validationNmse < bestValidationNmse) { 286 bestValidationNmse = validationNmse; 275 if (conditionalVariableName != null) { 276 rows = from row in rows 277 where !ProblemData.Dataset[conditionalVariableName, row].IsAlmost(0.0) 278 select row; 279 } 280 281 foreach (var tree in trees) { 282 double validationQuality; 283 validationQuality = evaluator.Evaluate(tree, ProblemData, 284 SymbolicExpressionTreeInterpreter, rows, PredictionHorizon.Value, LowerEstimationLimit, UpperEstimationLimit); 285 if ((Maximization.Value && validationQuality > bestValidationQuality) || 286 (!Maximization.Value && validationQuality < bestValidationQuality)) { 287 bestValidationQuality = validationQuality; 287 288 bestTree = tree; 288 289 } … … 290 291 291 292 292 if (BestSolutionQualityParameter.ActualValue == null || BestSolutionQualityParameter.ActualValue.Value > bestValidationNmse) { 293 var model = new SymbolicTimeSeriesPrognosisModel((ISymbolicTimeSeriesExpressionInterpreter)SymbolicExpressionTreeInterpreter.Clone(), bestTree); 293 if (BestSolutionQualityParameter.ActualValue == null || 294 (Maximization.Value && BestSolutionQualityParameter.ActualValue.Value < bestValidationQuality) || 295 (!Maximization.Value && BestSolutionQualityParameter.ActualValue.Value > bestValidationQuality)) { 296 var scaledTree = GetScaledTree(bestTree); 297 var model = new SymbolicTimeSeriesPrognosisModel((ISymbolicTimeSeriesExpressionInterpreter)SymbolicExpressionTreeInterpreter.Clone(), scaledTree); 294 298 model.Name = "Time series prognosis model"; 295 299 model.Description = "Best solution on validation partition found over the whole run."; 296 300 297 var solution = new SymbolicTimeSeriesPrognosisSolution( ProblemData, model, PredictionHorizon.Value, conditionalVariableName);301 var solution = new SymbolicTimeSeriesPrognosisSolution((MultiVariateDataAnalysisProblemData)ProblemData.Clone(), model, PredictionHorizon.Value, conditionalVariableName, LowerEstimationLimit.ToArray(), UpperEstimationLimit.ToArray()); 298 302 solution.Name = BestSolutionParameterName; 299 303 solution.Description = "Best solution on validation partition found over the whole run."; 300 304 301 305 BestSolutionParameter.ActualValue = solution; 302 BestSolutionQualityParameter.ActualValue = new DoubleValue(bestValidation Nmse);306 BestSolutionQualityParameter.ActualValue = new DoubleValue(bestValidationQuality); 303 307 304 308 // BestSymbolicTimeSeriesPrognosisSolutionAnalyzer.UpdateBestSolutionResults(solution, ProblemData, Results, Generations, VariableFrequencies); … … 313 317 Results[BestSolutionResultName].Value = BestSolutionParameter.ActualValue; 314 318 Results[BestSolutionQualityParameterName].Value = new DoubleValue(BestSolutionQualityParameter.ActualValue.Value); 315 Results[CurrentBestValidationQualityParameterName].Value = new DoubleValue(bestValidation Nmse);319 Results[CurrentBestValidationQualityParameterName].Value = new DoubleValue(bestValidationQuality); 316 320 317 321 DataTable validationValues = (DataTable)Results[BestSolutionQualityValuesParameterName].Value; 318 322 AddValue(validationValues, BestSolutionQualityParameter.ActualValue.Value, BestSolutionQualityParameterName, BestSolutionQualityParameterName); 319 AddValue(validationValues, bestValidation Nmse, CurrentBestValidationQualityParameterName, CurrentBestValidationQualityParameterName);323 AddValue(validationValues, bestValidationQuality, CurrentBestValidationQualityParameterName, CurrentBestValidationQualityParameterName); 320 324 #endregion 321 325 return base.Apply(); 322 326 } 323 327 324 [StorableHook(HookType.AfterDeserialization)] 325 private void Initialize() { 326 } 328 private SymbolicExpressionTree GetScaledTree(SymbolicExpressionTree tree) { 329 double[] alpha, beta; 330 int trainingStart = ProblemData.TrainingSamplesStart.Value; 331 int trainingEnd = ProblemData.TrainingSamplesEnd.Value; 332 IEnumerable<int> trainingRows = Enumerable.Range(trainingStart, trainingEnd - trainingStart); 333 string conditionalVariableName = ConditionVariableName == null ? null : ConditionVariableName.Value; 334 if (conditionalVariableName != null) { 335 trainingRows = from row in trainingRows 336 where !ProblemData.Dataset[conditionalVariableName, row].IsAlmost(0.0) 337 select row; 338 } 339 340 // calculate scaling parameters based on one-step-predictions 341 IEnumerable<string> selectedTargetVariables = from item in ProblemData.TargetVariables 342 where ProblemData.TargetVariables.ItemChecked(item) 343 select item.Value; 344 int dimension = selectedTargetVariables.Count(); 345 346 IEnumerable<double[]> oneStepPredictions = 347 SymbolicExpressionTreeInterpreter.GetSymbolicExpressionTreeValues(tree, ProblemData.Dataset, selectedTargetVariables, trainingRows, 1); 348 IEnumerable<double[]> originalValues = from row in trainingRows 349 select (from targetVariable in selectedTargetVariables 350 select ProblemData.Dataset[targetVariable, row]).ToArray(); 351 alpha = new double[dimension]; 352 beta = new double[dimension]; 353 354 SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.CalculateScalingParameters(originalValues, oneStepPredictions, ref beta, ref alpha); 355 356 // scale tree for solution 357 return SymbolicVectorRegressionSolutionLinearScaler.Scale(tree, beta, alpha); 358 } 359 360 327 361 328 362 private static void AddValue(DataTable table, double data, string name, string description) { -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Evaluators/SymbolicTimeSeriesPrognosisNormalizedMseEvaluator.cs
r4401 r4475 41 41 [Item("SymbolicTimeSeriesPrognosisNormalizedMseEvaluator", "")] 42 42 [StorableClass] 43 public class SymbolicTimeSeriesPrognosisNormalizedMseEvaluator : SingleSuccessorOperator, ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator { 44 private const string RandomParameterName = "Random"; 45 private const string DataAnalysisProblemDataParameterName = "MultiVariateDataAnalysisProblemData"; 46 private const string TimeSeriesExpressionInterpreterParameterName = "TimeSeriesExpressionInterpreter"; 47 private const string TimeSeriesPrognosisModelParameterName = "TimeSeriesPrognosisModel"; 48 private const string PredictionHorizontParameterName = "PredictionHorizon"; 49 private const string LowerEstimationLimitParameterName = "LowerEstimationLimit"; 50 private const string UpperEstimationLimitParameterName = "UpperEstimationLimit"; 51 private const string ConditionVariableParameterName = "ConditionVariableName"; 52 private const string SamplesStartParameterName = "SamplesStart"; 53 private const string SamplesEndParameterName = "SamplesEnd"; 54 private const string QualityParameterName = "Quality"; 55 private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples"; 56 57 #region parameter properties 58 public ILookupParameter<IRandom> RandomParameter { 59 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; } 60 } 61 public ILookupParameter<MultiVariateDataAnalysisProblemData> ProblemDataParameter { 62 get { return (ILookupParameter<MultiVariateDataAnalysisProblemData>)Parameters[DataAnalysisProblemDataParameterName]; } 63 } 64 public ILookupParameter<ISymbolicTimeSeriesExpressionInterpreter> TimeSeriesExpressionInterpreterParameter { 65 get { return (ILookupParameter<ISymbolicTimeSeriesExpressionInterpreter>)Parameters[TimeSeriesExpressionInterpreterParameterName]; } 66 } 67 public IValueLookupParameter<IntValue> PredictionHorizonParameter { 68 get { return (IValueLookupParameter<IntValue>)Parameters[PredictionHorizontParameterName]; } 69 } 70 public OptionalValueParameter<StringValue> ConditionVariableNameParameter { 71 get { return (OptionalValueParameter<StringValue>)Parameters[ConditionVariableParameterName]; } 72 } 73 public IValueLookupParameter<IntValue> SamplesStartParameter { 74 get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; } 75 } 76 public IValueLookupParameter<IntValue> SamplesEndParameter { 77 get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; } 78 } 79 public IValueLookupParameter<DoubleArray> LowerEstimationLimitParameter { 80 get { return (IValueLookupParameter<DoubleArray>)Parameters[LowerEstimationLimitParameterName]; } 81 } 82 public IValueLookupParameter<DoubleArray> UpperEstimationLimitParameter { 83 get { return (IValueLookupParameter<DoubleArray>)Parameters[UpperEstimationLimitParameterName]; } 84 } 85 public ILookupParameter<SymbolicExpressionTree> TimeSeriesPrognosisModelParameter { 86 get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[TimeSeriesPrognosisModelParameterName]; } 87 } 88 public ILookupParameter<DoubleValue> QualityParameter { 89 get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; } 90 } 91 public IValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter { 92 get { return (IValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; } 93 } 94 #endregion 95 #region 96 public IRandom Random { 97 get { return RandomParameter.ActualValue; } 98 } 99 public MultiVariateDataAnalysisProblemData ProblemData { 100 get { return ProblemDataParameter.ActualValue; } 101 } 102 public ISymbolicTimeSeriesExpressionInterpreter TimeSeriesExpressionInterpreter { 103 get { return TimeSeriesExpressionInterpreterParameter.ActualValue; } 104 } 105 public IntValue PredictionHorizon { 106 get { return PredictionHorizonParameter.ActualValue; } 107 } 108 public StringValue ConditionVariableName { 109 get { return ConditionVariableNameParameter.Value; } 110 } 111 public IntValue SamplesStart { 112 get { return SamplesStartParameter.ActualValue; } 113 } 114 public IntValue SamplesEnd { 115 get { return SamplesEndParameter.ActualValue; } 116 } 117 public DoubleArray LowerEstimationLimit { 118 get { return LowerEstimationLimitParameter.ActualValue; } 119 } 120 public DoubleArray UpperEstimationLimit { 121 get { return UpperEstimationLimitParameter.ActualValue; } 122 } 123 public SymbolicExpressionTree TimeSeriesPrognosisModel { 124 get { return TimeSeriesPrognosisModelParameter.ActualValue; } 125 } 126 public PercentValue RelativeNumberOfEvaluatedSamples { 127 get { return RelativeNumberOfEvaluatedSamplesParameter.Value; } 128 } 129 #endregion 43 public class SymbolicTimeSeriesPrognosisNormalizedMseEvaluator : SymbolicTimeSeriesPrognosisEvaluator { 130 44 131 45 public SymbolicTimeSeriesPrognosisNormalizedMseEvaluator() 132 46 : base() { 133 Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "A random number generator."));134 Parameters.Add(new LookupParameter<MultiVariateDataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The data analysis problem data to use for training."));135 Parameters.Add(new LookupParameter<ISymbolicTimeSeriesExpressionInterpreter>(TimeSeriesExpressionInterpreterParameterName, "The interpreter that should be used to evaluate the time series model represented as a symbolic expression tree."));136 Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The first index of the data set partition to use for training."));137 Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The last index of the data set partition to use for training."));138 Parameters.Add(new ValueLookupParameter<IntValue>(PredictionHorizontParameterName, "The number of time steps for which to create a forecast."));139 Parameters.Add(new ValueLookupParameter<DoubleArray>(LowerEstimationLimitParameterName, "The lower limit for estimated values."));140 Parameters.Add(new ValueLookupParameter<DoubleArray>(UpperEstimationLimitParameterName, "The upper limit for estimated values."));141 Parameters.Add(new OptionalValueParameter<StringValue>(ConditionVariableParameterName, "The name of the condition variable indicating if a row should be considered for evaluation or not."));142 Parameters.Add(new LookupParameter<SymbolicExpressionTree>(TimeSeriesPrognosisModelParameterName, "The time series prognosis model encoded as a symbolic expression tree."));143 Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality of the time series prognosis model encoded as a symbolic expression tree."));144 Parameters.Add(new ValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation between the start and end index.", new PercentValue(1)));145 47 } 146 48 147 public override IOperation Apply() { 148 double quality; 149 string conditionVariableName = ConditionVariableName == null ? null : ConditionVariableName.Value; 150 int nRows = (int)Math.Ceiling((SamplesEnd.Value - SamplesStart.Value) * RelativeNumberOfEvaluatedSamples.Value); 151 IEnumerable<int> rows = RandomEnumerable.SampleRandomNumbers(Random.Next(), SamplesStart.Value, SamplesEnd.Value, nRows); 152 153 quality = Evaluate(TimeSeriesPrognosisModel, ProblemData, TimeSeriesExpressionInterpreter, 154 conditionVariableName, rows, PredictionHorizon.Value, LowerEstimationLimit, UpperEstimationLimit); 155 QualityParameter.ActualValue = new DoubleValue(quality); 156 return base.Apply(); 49 public override double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, ISymbolicTimeSeriesExpressionInterpreter interpreter, IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit) { 50 return Calculate(tree, problemData, interpreter, rows, predictionHorizon, lowerEstimationLimit, upperEstimationLimit); 157 51 } 158 52 159 public static double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, 160 ISymbolicTimeSeriesExpressionInterpreter interpreter, 161 IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit) { 162 return Evaluate(tree, problemData, interpreter, null, rows, predictionHorizon, lowerEstimationLimit, upperEstimationLimit); 163 } 164 165 public static double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, 166 ISymbolicTimeSeriesExpressionInterpreter interpreter, string conditionVariableName, 53 public static double Calculate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, 54 ISymbolicTimeSeriesExpressionInterpreter interpreter, 167 55 IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit) { 168 56 double[] zeros = new double[problemData.TargetVariables.CheckedItems.Count()]; 169 57 double[] ones = Enumerable.Repeat(1.0, zeros.Length).ToArray(); 170 return SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator. Evaluate(tree, problemData, interpreter, conditionVariableName, rows,58 return SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.CalculateWithScaling(tree, problemData, interpreter, rows, 171 59 predictionHorizon, lowerEstimationLimit, upperEstimationLimit, ones, zeros); 172 60 } -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Evaluators/SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.cs
r4460 r4475 37 37 using HeuristicLab.Problems.DataAnalysis.Regression; 38 38 using HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Interfaces; 39 using HeuristicLab.Problems.DataAnalysis.MultiVariate.Evaluators; 39 40 40 41 namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Evaluators { 41 42 [Item("SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator", "")] 42 43 [StorableClass] 43 public class SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator : SingleSuccessorOperator, ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator { 44 private const string RandomParameterName = "Random"; 45 private const string DataAnalysisProblemDataParameterName = "MultiVariateDataAnalysisProblemData"; 46 private const string TimeSeriesExpressionInterpreterParameterName = "TimeSeriesExpressionInterpreter"; 47 private const string TimeSeriesPrognosisModelParameterName = "TimeSeriesPrognosisModel"; 48 private const string PredictionHorizontParameterName = "PredictionHorizon"; 49 private const string ConditionVariableParameterName = "ConditionVariableName"; 50 private const string SamplesStartParameterName = "SamplesStart"; 51 private const string SamplesEndParameterName = "SamplesEnd"; 52 private const string AlphaParameterName = "Alpha"; 53 private const string BetaParameterName = "Beta"; 54 private const string LowerEstimationLimitParameterName = "LowerEstimationLimit"; 55 private const string UpperEstimationLimitParameterName = "UpperEstimationLimit"; 56 private const string QualityParameterName = "Quality"; 57 private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples"; 58 59 #region parameter properties 60 public ILookupParameter<IRandom> RandomParameter { 61 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; } 62 } 63 public ILookupParameter<MultiVariateDataAnalysisProblemData> ProblemDataParameter { 64 get { return (ILookupParameter<MultiVariateDataAnalysisProblemData>)Parameters[DataAnalysisProblemDataParameterName]; } 65 } 66 public ILookupParameter<ISymbolicTimeSeriesExpressionInterpreter> TimeSeriesExpressionInterpreterParameter { 67 get { return (ILookupParameter<ISymbolicTimeSeriesExpressionInterpreter>)Parameters[TimeSeriesExpressionInterpreterParameterName]; } 68 } 69 public IValueLookupParameter<IntValue> PredictionHorizonParameter { 70 get { return (IValueLookupParameter<IntValue>)Parameters[PredictionHorizontParameterName]; } 71 } 72 public OptionalValueParameter<StringValue> ConditionVariableNameParameter { 73 get { return (OptionalValueParameter<StringValue>)Parameters[ConditionVariableParameterName]; } 74 } 75 public IValueLookupParameter<IntValue> SamplesStartParameter { 76 get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; } 77 } 78 public IValueLookupParameter<IntValue> SamplesEndParameter { 79 get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; } 80 } 81 public ILookupParameter<SymbolicExpressionTree> TimeSeriesPrognosisModelParameter { 82 get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[TimeSeriesPrognosisModelParameterName]; } 83 } 84 public ILookupParameter<DoubleValue> QualityParameter { 85 get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; } 86 } 87 public ILookupParameter<DoubleArray> AlphaParameter { 88 get { return (ILookupParameter<DoubleArray>)Parameters[AlphaParameterName]; } 89 } 90 public ILookupParameter<DoubleArray> BetaParameter { 91 get { return (ILookupParameter<DoubleArray>)Parameters[BetaParameterName]; } 92 } 93 public IValueLookupParameter<DoubleArray> LowerEstimationLimitParameter { 94 get { return (IValueLookupParameter<DoubleArray>)Parameters[LowerEstimationLimitParameterName]; } 95 } 96 public IValueLookupParameter<DoubleArray> UpperEstimationLimitParameter { 97 get { return (IValueLookupParameter<DoubleArray>)Parameters[UpperEstimationLimitParameterName]; } 98 } 99 public IValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter { 100 get { return (IValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; } 101 } 102 103 #endregion 104 #region properties 105 public IRandom Random { 106 get { return RandomParameter.ActualValue; } 107 } 108 public MultiVariateDataAnalysisProblemData ProblemData { 109 get { return ProblemDataParameter.ActualValue; } 110 } 111 public ISymbolicTimeSeriesExpressionInterpreter TimeSeriesExpressionInterpreter { 112 get { return TimeSeriesExpressionInterpreterParameter.ActualValue; } 113 } 114 public IntValue PredictionHorizon { 115 get { return PredictionHorizonParameter.ActualValue; } 116 } 117 public StringValue ConditionVariableName { 118 get { return ConditionVariableNameParameter.Value; } 119 } 120 public IntValue SamplesStart { 121 get { return SamplesStartParameter.ActualValue; } 122 } 123 public IntValue SamplesEnd { 124 get { return SamplesEndParameter.ActualValue; } 125 } 126 public DoubleArray LowerEstimationLimit { 127 get { return LowerEstimationLimitParameter.ActualValue; } 128 } 129 public DoubleArray UpperEstimationLimit { 130 get { return UpperEstimationLimitParameter.ActualValue; } 131 } 132 public SymbolicExpressionTree TimeSeriesPrognosisModel { 133 get { return TimeSeriesPrognosisModelParameter.ActualValue; } 134 } 135 public PercentValue RelativeNumberOfEvaluatedSamples { 136 get { return RelativeNumberOfEvaluatedSamplesParameter.Value; } 137 } 138 #endregion 44 public class SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator : SymbolicTimeSeriesPrognosisEvaluator { 139 45 140 46 public SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator() 141 47 : base() { 142 Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "A random number generator."));143 Parameters.Add(new LookupParameter<MultiVariateDataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The data analysis problem data to use for training."));144 Parameters.Add(new LookupParameter<ISymbolicTimeSeriesExpressionInterpreter>(TimeSeriesExpressionInterpreterParameterName, "The interpreter that should be used to evaluate the time series model represented as a symbolic expression tree."));145 Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The first index of the data set partition to use for training."));146 Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The last index of the data set partition to use for training."));147 Parameters.Add(new ValueLookupParameter<IntValue>(PredictionHorizontParameterName, "The number of time steps for which to create a forecast."));148 Parameters.Add(new ValueLookupParameter<DoubleArray>(LowerEstimationLimitParameterName, "The lower limit for estimated values."));149 Parameters.Add(new ValueLookupParameter<DoubleArray>(UpperEstimationLimitParameterName, "The upper limit for estimated values."));150 Parameters.Add(new OptionalValueParameter<StringValue>(ConditionVariableParameterName, "The name of the condition variable indicating if a row should be considered for evaluation or not."));151 Parameters.Add(new LookupParameter<SymbolicExpressionTree>(TimeSeriesPrognosisModelParameterName, "The time series prognosis model encoded as a symbolic expression tree."));152 Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality of the time series prognosis model encoded as a symbolic expression tree."));153 Parameters.Add(new LookupParameter<DoubleArray>(AlphaParameterName, "The alpha parameter for linear scaling based on one step predictions."));154 Parameters.Add(new LookupParameter<DoubleArray>(BetaParameterName, "The beta parameter for linear scaling based on one step predictions."));155 Parameters.Add(new ValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation between the start and end index.", new PercentValue(1)));156 48 } 157 49 158 public override IOperation Apply() { 50 public override double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, ISymbolicTimeSeriesExpressionInterpreter interpreter, IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit) { 51 return Calculate(tree, problemData, interpreter, rows, predictionHorizon, lowerEstimationLimit, upperEstimationLimit); 52 } 53 54 public static double Calculate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, ISymbolicTimeSeriesExpressionInterpreter interpreter, IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit) { 159 55 double[] alpha, beta; 160 56 double quality; 161 string conditionVariableName = ConditionVariableName == null ? null : ConditionVariableName.Value;162 int nRows = (int)Math.Ceiling((SamplesEnd.Value - SamplesStart.Value) * RelativeNumberOfEvaluatedSamples.Value);163 57 164 IEnumerable<int> rows = RandomEnumerable.SampleRandomNumbers(Random.Next(), SamplesStart.Value, SamplesEnd.Value, nRows); 165 CalculateScalingParameters(TimeSeriesPrognosisModel, ProblemData, TimeSeriesExpressionInterpreter, 166 conditionVariableName, rows, 167 out beta, out alpha); 58 // calculate scaling parameters based on one-step-predictions 59 IEnumerable<string> selectedTargetVariables = from item in problemData.TargetVariables 60 where problemData.TargetVariables.ItemChecked(item) 61 select item.Value; 62 int dimension = selectedTargetVariables.Count(); 168 63 169 quality = Evaluate(TimeSeriesPrognosisModel, ProblemData, TimeSeriesExpressionInterpreter, 170 conditionVariableName, rows, PredictionHorizon.Value, 171 LowerEstimationLimit, UpperEstimationLimit, 64 IEnumerable<double[]> oneStepPredictions = 65 interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, selectedTargetVariables, rows, 1); 66 IEnumerable<double[]> originalValues = from row in rows 67 select (from targetVariable in selectedTargetVariables 68 select problemData.Dataset[targetVariable, row]).ToArray(); 69 alpha = new double[dimension]; 70 beta = new double[dimension]; 71 72 CalculateScalingParameters(originalValues, oneStepPredictions, ref beta, ref alpha); 73 74 // calculate the quality for the full horizon 75 quality = CalculateWithScaling(tree, problemData, interpreter, 76 rows, predictionHorizon, 77 lowerEstimationLimit, upperEstimationLimit, 172 78 beta, alpha); 173 QualityParameter.ActualValue = new DoubleValue(quality); 174 AlphaParameter.ActualValue = new DoubleArray(alpha); 175 BetaParameter.ActualValue = new DoubleArray(beta); 176 return base.Apply(); 79 return quality; 177 80 } 178 81 179 public static double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData,82 public static double CalculateWithScaling(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, 180 83 ISymbolicTimeSeriesExpressionInterpreter interpreter, 181 IEnumerable<int> rows, int predictionHorizon,182 DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit,183 double[] beta, double[] alpha) {184 return Evaluate(tree, problemData, interpreter, null, rows, predictionHorizon, lowerEstimationLimit, upperEstimationLimit, beta, alpha);185 }186 187 public static double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData,188 ISymbolicTimeSeriesExpressionInterpreter interpreter, string conditionVariableName,189 84 IEnumerable<int> rows, int predictionHorizon, 190 85 DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit, 191 86 double[] beta, double[] alpha) { 192 if (conditionVariableName != null) {193 rows = from row in rows194 where !problemData.Dataset[conditionVariableName, row].IsAlmost(0.0)195 select row;196 }197 87 IEnumerable<string> selectedTargetVariables = from targetVariable in problemData.TargetVariables 198 88 where problemData.TargetVariables.ItemChecked(targetVariable) … … 208 98 select problemData.Dataset[targetVariable, row + step]).ToArray(); 209 99 210 List<OnlineNormalizedMeanSquaredErrorEvaluator> evaluators = new List<OnlineNormalizedMeanSquaredErrorEvaluator>(); 211 foreach (string targetVariable in selectedTargetVariables) 212 evaluators.Add(new OnlineNormalizedMeanSquaredErrorEvaluator()); 100 var evaluator = new OnlineMultiVariateEvaluator<OnlineNormalizedMeanSquaredErrorEvaluator>(); 213 101 214 102 var estimatedValuesEnumerator = estimatedValues.GetEnumerator(); … … 217 105 double[] original = originalValuesEnumerator.Current; 218 106 double[] estimated = estimatedValuesEnumerator.Current; 219 for (int i = 0; i < e valuators.Count; i++) {107 for (int i = 0; i < estimated.Length; i++) { 220 108 if (double.IsNaN(estimated[i])) estimated[i] = upperEstimationLimit[i]; 221 109 else estimated[i] = Math.Min(upperEstimationLimit[i], Math.Max(lowerEstimationLimit[i], estimated[i])); 222 evaluators[i].Add(original[i], estimated[i]);223 110 } 111 evaluator.Add(original, estimated); 224 112 } 225 113 226 double quality = evaluator s.Select(x => x.NormalizedMeanSquaredError).Sum();114 double quality = evaluator.Value; 227 115 return quality; 228 116 } 229 117 230 public static void CalculateScalingParameters(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, 231 ISymbolicTimeSeriesExpressionInterpreter interpreter, 232 IEnumerable<int> rows, 233 out double[] betas, out double[] alphas) { 234 CalculateScalingParameters(tree, problemData, interpreter, null, rows, out betas, out alphas); 235 } 236 237 public static void CalculateScalingParameters(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, 238 ISymbolicTimeSeriesExpressionInterpreter interpreter, string conditionVariableName, 239 IEnumerable<int> rows, out double[] betas, out double[] alphas) { 240 IEnumerable<string> selectedTargetVariables = from item in problemData.TargetVariables 241 where problemData.TargetVariables.ItemChecked(item) 242 select item.Value; 243 int dimension = selectedTargetVariables.Count(); 244 245 if (conditionVariableName != null) { 246 rows = from row in rows 247 where !problemData.Dataset[conditionVariableName, row].IsAlmost(0.0) 248 select row; 249 } 250 251 IEnumerable<double[]> oneStepPredictions = 252 interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, selectedTargetVariables, rows, 1); 253 IEnumerable<double[]> originalValues = from row in rows 254 select (from targetVariable in selectedTargetVariables 255 select problemData.Dataset[targetVariable, row]).ToArray(); 256 257 alphas = new double[dimension]; 258 betas = new double[dimension]; 259 int[] cnt = new int[dimension]; 118 public static void CalculateScalingParameters(IEnumerable<double[]> originalValues, IEnumerable<double[]> estimatedValues, ref double[] beta, ref double[] alpha) { 260 119 List<OnlineMeanAndVarianceCalculator> estimatedVarianceEvaluators = new List<OnlineMeanAndVarianceCalculator>(); 261 120 List<OnlineCovarianceEvaluator> covarianceEvaluators = new List<OnlineCovarianceEvaluator>(); 262 121 List<OnlineMeanAndVarianceCalculator> originalMeanCalculators = new List<OnlineMeanAndVarianceCalculator>(); 263 foreach (var selectedTargetVariable in selectedTargetVariables) { 264 estimatedVarianceEvaluators.Add(new OnlineMeanAndVarianceCalculator()); 265 covarianceEvaluators.Add(new OnlineCovarianceEvaluator()); 266 originalMeanCalculators.Add(new OnlineMeanAndVarianceCalculator()); 267 } 268 var estimatedEnumerator = oneStepPredictions.GetEnumerator(); 122 int[] cnt = null; 123 124 var estimatedEnumerator = estimatedValues.GetEnumerator(); 269 125 var originalEnumerator = originalValues.GetEnumerator(); 270 126 while (estimatedEnumerator.MoveNext() & originalEnumerator.MoveNext()) { 271 127 double[] original = originalEnumerator.Current; 272 128 double[] estimated = estimatedEnumerator.Current; 273 for (int component = 0; component < dimension; component++) { 274 if (IsValidValue(original[component]) && IsValidValue(estimated[component])) { 275 cnt[component]++; 276 estimatedVarianceEvaluators[component].Add(estimated[component]); 277 covarianceEvaluators[component].Add(original[component], estimated[component]); 278 originalMeanCalculators[component].Add(original[component]); 129 int dimension = original.Length; 130 // initialize 131 if (cnt == null) { 132 cnt = new int[dimension]; 133 for (int i = 0; i < dimension; i++) { 134 estimatedVarianceEvaluators.Add(new OnlineMeanAndVarianceCalculator()); 135 covarianceEvaluators.Add(new OnlineCovarianceEvaluator()); 136 originalMeanCalculators.Add(new OnlineMeanAndVarianceCalculator()); 279 137 } 138 } else if (cnt.Length == dimension) { 139 for (int component = 0; component < dimension; component++) { 140 if (IsValidValue(original[component]) && IsValidValue(estimated[component])) { 141 cnt[component]++; 142 estimatedVarianceEvaluators[component].Add(estimated[component]); 143 covarianceEvaluators[component].Add(original[component], estimated[component]); 144 originalMeanCalculators[component].Add(original[component]); 145 } 146 } 147 } else { 148 throw new ArgumentException("Dimension of input array doesn't match"); 280 149 } 281 150 } 151 282 152 if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) 283 153 throw new InvalidOperationException("Number of elements in estimated and original series doesn't match."); 284 for (int component = 0; component < dimension; component++) { 154 if (cnt == null) throw new ArgumentException("No elements in estimated and original."); 155 156 for (int component = 0; component < cnt.Length; component++) { 285 157 if (cnt[component] < 2) { 286 alpha s[component] = 0;287 beta s[component] = 1;158 alpha[component] = 0; 159 beta[component] = 1; 288 160 } else { 289 161 if (estimatedVarianceEvaluators[component].PopulationVariance.IsAlmost(0.0)) 290 beta s[component] = 1;162 beta[component] = 1; 291 163 else 292 beta s[component] = covarianceEvaluators[component].Covariance / estimatedVarianceEvaluators[component].PopulationVariance;164 beta[component] = covarianceEvaluators[component].Covariance / estimatedVarianceEvaluators[component].PopulationVariance; 293 165 294 alpha s[component] = originalMeanCalculators[component].Mean - betas[component] * estimatedVarianceEvaluators[component].Mean;166 alpha[component] = originalMeanCalculators[component].Mean - beta[component] * estimatedVarianceEvaluators[component].Mean; 295 167 } 296 168 } -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Interfaces/ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator.cs
r4113 r4475 35 35 namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Interfaces { 36 36 public interface ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator : ISymbolicTimeSeriesPrognosisEvaluator, ISingleObjectiveEvaluator { 37 double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData, 38 ISymbolicTimeSeriesExpressionInterpreter interpreter, 39 IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit); 37 40 } 38 41 -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/SymbolicTimeSeriesExpressionInterpreter.cs
r4113 r4475 201 201 202 202 private Instruction PostProcessInstruction(Instruction instr) { 203 if (instr.opCode == OpCodes.Variable) { 203 if (instr.opCode == OpCodes.Variable || instr.opCode == OpCodes.LagVariable || 204 instr.opCode == OpCodes.Integral || instr.opCode == OpCodes.MovingAverage || instr.opCode == OpCodes.Differential) { 204 205 var variableTreeNode = instr.dynamicNode as VariableTreeNode; 205 206 instr.iArg0 = (ushort)dataset.GetVariableIndex(variableTreeNode.VariableName); 206 } else if (instr.opCode == OpCodes.LagVariable) { 207 var variableTreeNode = instr.dynamicNode as LaggedVariableTreeNode; 208 instr.iArg0 = (ushort)dataset.GetVariableIndex(variableTreeNode.VariableName); 209 } 207 } 210 208 return instr; 211 209 } … … 361 359 double sum = 0.0; 362 360 for (int relativeRow = movingAvgTreeNode.MinTimeOffset; relativeRow < movingAvgTreeNode.MaxTimeOffset; relativeRow++) { 363 sum += GetVariableValue(currentInstr.iArg0, relativeRow) * movingAvgTreeNode.Weight;364 } 365 return sum / (movingAvgTreeNode.MaxTimeOffset - movingAvgTreeNode.MinTimeOffset);361 sum += GetVariableValue(currentInstr.iArg0, relativeRow); 362 } 363 return movingAvgTreeNode.Weight * sum / (movingAvgTreeNode.MaxTimeOffset - movingAvgTreeNode.MinTimeOffset); 366 364 } 367 365 case OpCodes.Differential: { … … 369 367 if (row + diffTreeNode.Lag - 2 < 0 || row + diffTreeNode.Lag >= dataset.Rows) 370 368 return double.NaN; 371 double y_0 = GetVariableValue(currentInstr.iArg0, diffTreeNode.Lag) * diffTreeNode.Weight;372 double y_1 = GetVariableValue(currentInstr.iArg0, diffTreeNode.Lag - 1) * diffTreeNode.Weight;373 double y_2 = GetVariableValue(currentInstr.iArg0, diffTreeNode.Lag - 2) * diffTreeNode.Weight;374 return (3 * y_0 - 4 * y_1 + 3 * y_2) / 2;369 double y_0 = GetVariableValue(currentInstr.iArg0, diffTreeNode.Lag); 370 double y_1 = GetVariableValue(currentInstr.iArg0, diffTreeNode.Lag - 1); 371 double y_2 = GetVariableValue(currentInstr.iArg0, diffTreeNode.Lag - 2); 372 return diffTreeNode.Weight * (3 * y_0 - 4 * y_1 + 3 * y_2) / 2; 375 373 } 376 374 case OpCodes.Integral: { … … 380 378 double sum = 0; 381 379 for (int relativeRow = integralVarTreeNode.MinTimeOffset; relativeRow < integralVarTreeNode.MaxTimeOffset; relativeRow++) { 382 sum += GetVariableValue(currentInstr.iArg0, relativeRow) * integralVarTreeNode.Weight;383 } 384 return sum;380 sum += GetVariableValue(currentInstr.iArg0, relativeRow); 381 } 382 return integralVarTreeNode.Weight * sum; 385 383 } 386 384 case OpCodes.Constant: { -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/SymbolicTimeSeriesPrognosisSolution.cs
r4461 r4475 31 31 using System.Drawing; 32 32 using System; 33 using HeuristicLab.Data; 33 34 34 35 namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic { … … 44 45 [Storable] 45 46 private string conditionalEvaluationVariable; 47 [Storable] 48 private double[] lowerEstimationLimit; 49 [Storable] 50 private double[] upperEstimationLimit; 46 51 47 52 public string Filename { get; set; } … … 54 59 } 55 60 56 public SymbolicTimeSeriesPrognosisSolution(MultiVariateDataAnalysisProblemData problemData, SymbolicTimeSeriesPrognosisModel model, int horizon, string conditionalEvaluationVariable )61 public SymbolicTimeSeriesPrognosisSolution(MultiVariateDataAnalysisProblemData problemData, SymbolicTimeSeriesPrognosisModel model, int horizon, string conditionalEvaluationVariable, double[] lowerEstimationLimit, double[] upperEstimationLimit) 57 62 : this() { 58 63 this.problemData = problemData; … … 60 65 this.horizon = horizon; 61 66 this.conditionalEvaluationVariable = conditionalEvaluationVariable; 67 this.lowerEstimationLimit = (double[])lowerEstimationLimit.Clone(); 68 this.upperEstimationLimit = (double[])upperEstimationLimit.Clone(); 62 69 } 63 70 … … 99 106 } 100 107 } 108 } 109 110 public double GetLowerEstimationLimit(int i) { 111 return lowerEstimationLimit[i]; 112 } 113 public double GetUpperEstimationLimit(int i) { 114 return upperEstimationLimit[i]; 101 115 } 102 116 … … 139 153 public IEnumerable<double[]> EstimatedValues { 140 154 get { 141 return model.GetEstimatedValues(problemData, 0, problemData.Dataset.Rows);155 return ApplyEstimationLimits(model.GetEstimatedValues(problemData, 0, problemData.Dataset.Rows)); 142 156 } 143 157 } … … 145 159 public IEnumerable<double[]> EstimatedTrainingValues { 146 160 get { 147 return model.GetEstimatedValues(problemData, problemData.TrainingSamplesStart.Value, problemData.TrainingSamplesEnd.Value);161 return ApplyEstimationLimits(model.GetEstimatedValues(problemData, problemData.TrainingSamplesStart.Value, problemData.TrainingSamplesEnd.Value)); 148 162 } 149 163 } … … 151 165 public IEnumerable<double[]> EstimatedTestValues { 152 166 get { 153 return model.GetEstimatedValues(problemData, problemData.TestSamplesStart.Value, problemData.TestSamplesEnd.Value);167 return ApplyEstimationLimits(model.GetEstimatedValues(problemData, problemData.TestSamplesStart.Value, problemData.TestSamplesEnd.Value)); 154 168 } 155 169 } … … 198 212 clone.horizon = horizon; 199 213 clone.conditionalEvaluationVariable = conditionalEvaluationVariable; 214 clone.lowerEstimationLimit = (double[])lowerEstimationLimit.Clone(); 215 clone.upperEstimationLimit = (double[])upperEstimationLimit.Clone(); 200 216 return clone; 217 } 218 219 private IEnumerable<double[]> ApplyEstimationLimits(IEnumerable<double[]> values) { 220 foreach (var xs in values) { 221 for (int i = 0; i < xs.Length; i++) { 222 xs[i] = Math.Max(lowerEstimationLimit[i], Math.Min(upperEstimationLimit[i], xs[i])); 223 } 224 yield return xs; 225 } 201 226 } 202 227 } -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate/3.3/HeuristicLab.Problems.DataAnalysis.MultiVariate-3.3.csproj
r4065 r4475 109 109 <None Include="HeuristicLabProblemsDataAnalysisMultiVariatePlugin.cs.frame" /> 110 110 <None Include="Properties\AssemblyInfo.frame" /> 111 <Compile Include="Evaluators\OnlineMultiVariateEvaluator.cs" /> 112 <Compile Include="Evaluators\OnlineMeanMahalanobisDistanceEvaluator.cs" /> 111 113 <Compile Include="HeuristicLabProblemsDataAnalysisMultiVariatePlugin.cs" /> 114 <Compile Include="Interfaces\IMultiVariateOnlineEvaluator.cs" /> 112 115 <Compile Include="Interfaces\IMultiVariateDataAnalysisEvaluator.cs" /> 113 116 <Compile Include="Interfaces\IMultiVariateDataAnalysisModel.cs" /> … … 143 146 <Project>{125D3006-67F5-48CB-913E-73C0548F17FA}</Project> 144 147 <Name>HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3</Name> 148 </ProjectReference> 149 <ProjectReference Include="..\..\HeuristicLab.ExtLibs\HeuristicLab.ALGLIB\2.5.0\ALGLIB-2.5.0\ALGLIB-2.5.0.csproj"> 150 <Project>{29E4B033-1FEF-4FE1-AE17-0A9319D7C54E}</Project> 151 <Name>ALGLIB-2.5.0</Name> 152 </ProjectReference> 153 <ProjectReference Include="..\..\HeuristicLab.ExtLibs\HeuristicLab.ALGLIB\2.5.0\HeuristicLab.ALGLIB-2.5.0\HeuristicLab.ALGLIB-2.5.0.csproj"> 154 <Project>{01BBCB5A-144A-4A7E-908E-079849E2F7CF}</Project> 155 <Name>HeuristicLab.ALGLIB-2.5.0 %28HeuristicLab.ExtLibs\HeuristicLab.ALGLIB\HeuristicLab.ALGLIB-2.5.0\HeuristicLab.ALGLIB-2.5.0%29</Name> 145 156 </ProjectReference> 146 157 <ProjectReference Include="..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj"> -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/BestSymbolicRegressionSolutionAnalyzer.cs
r4195 r4475 92 92 var model = new SymbolicRegressionModel((ISymbolicExpressionTreeInterpreter)SymbolicExpressionTreeInterpreter.Clone(), 93 93 SymbolicExpressionTree[i]); 94 var solution = new SymbolicRegressionSolution( ProblemData, model, lowerEstimationLimit, upperEstimationLimit);94 var solution = new SymbolicRegressionSolution((DataAnalysisProblemData)ProblemData.Clone(), model, lowerEstimationLimit, upperEstimationLimit); 95 95 solution.Name = BestSolutionParameterName; 96 96 solution.Description = "Best solution on validation partition found over the whole run."; -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer.cs
r4308 r4475 321 321 var scaledBestTrainingTree = GetScaledTree(bestTrainingTree); 322 322 323 SymbolicRegressionSolution bestTrainingSolution = new SymbolicRegressionSolution( ProblemData,323 SymbolicRegressionSolution bestTrainingSolution = new SymbolicRegressionSolution((DataAnalysisProblemData)ProblemData.Clone(), 324 324 new SymbolicRegressionModel(SymbolicExpressionTreeInterpreter, scaledBestTrainingTree), 325 325 lowerEstimationLimit, upperEstimationLimit); … … 336 336 var model = new SymbolicRegressionModel((ISymbolicExpressionTreeInterpreter)SymbolicExpressionTreeInterpreter.Clone(), 337 337 scaledTree); 338 var solution = new SymbolicRegressionSolution( ProblemData, model, lowerEstimationLimit, upperEstimationLimit);338 var solution = new SymbolicRegressionSolution((DataAnalysisProblemData)ProblemData.Clone(), model, lowerEstimationLimit, upperEstimationLimit); 339 339 solution.Name = BestSolutionParameterName; 340 340 solution.Description = "Best solution on validation partition found over the whole run."; -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Views/3.3/RunCollectionMonteCarloVariableImpactView.cs
r4197 r4475 38 38 [View("RunCollection Monte-Carlo Variable Impact View")] 39 39 public partial class RunCollectionMonteCarloVariableImpactView : AsynchronousContentView { 40 private const string validationBestModelResultName = "Best solution (on valid iation set)";40 private const string validationBestModelResultName = "Best solution (on validation set)"; 41 41 public RunCollectionMonteCarloVariableImpactView() { 42 42 InitializeComponent(); -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Views/3.3/RunCollectionWinklerVariableImpactView.cs
r4197 r4475 38 38 [View("RunCollection Winkler Variable Impact View")] 39 39 public partial class RunCollectionWinklerVariableImpactView : AsynchronousContentView { 40 private const string validationBestModelResultName = "Best solution (on valid iation set)";40 private const string validationBestModelResultName = "Best solution (on validation set)"; 41 41 public RunCollectionWinklerVariableImpactView() { 42 42 InitializeComponent();
Note: See TracChangeset
for help on using the changeset viewer.