Changeset 7194
- Timestamp:
- 12/16/11 14:48:26 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolutionBase.cs
r7183 r7194 50 50 private const string TrainingTheilsUStatisticMeanResultName = "Average Theil's U (mean) (training)"; 51 51 private const string TestTheilsUStatisticMeanResultName = "Average Theil's U (mean) (test)"; 52 private const string TrainingTheilsUStatisticM AResultName = "Average Theil's U (moving average) (training)";53 private const string TestTheilsUStatisticM AResultName = "Average Theil's U (moving average) (test)";52 private const string TrainingTheilsUStatisticMaResultName = "Average Theil's U (moving average) (training)"; 53 private const string TestTheilsUStatisticMaResultName = "Average Theil's U (moving average) (test)"; 54 54 55 55 public new ITimeSeriesPrognosisModel Model { … … 153 153 } 154 154 public double[] TrainingTheilsUStatisticMovingAverage { 155 get { return ((DoubleArray)this[TrainingTheilsUStatisticM AResultName].Value).ToArray(); }156 private set { this[TrainingTheilsUStatisticM AResultName].Value = new DoubleArray(value); }155 get { return ((DoubleArray)this[TrainingTheilsUStatisticMaResultName].Value).ToArray(); } 156 private set { this[TrainingTheilsUStatisticMaResultName].Value = new DoubleArray(value); } 157 157 } 158 158 public double[] TestTheilsUStatisticMovingAverage { 159 get { return ((DoubleArray)this[TestTheilsUStatisticM AResultName].Value).ToArray(); }160 private set { this[TestTheilsUStatisticM AResultName].Value = new DoubleArray(value); }159 get { return ((DoubleArray)this[TestTheilsUStatisticMaResultName].Value).ToArray(); } 160 private set { this[TestTheilsUStatisticMaResultName].Value = new DoubleArray(value); } 161 161 } 162 162 #endregion … … 188 188 Add(new Result(TrainingTheilsUStatisticMeanResultName, "The average Theil's U statistic (reference: mean value) of the forecasts of the model on the training partition", new DoubleArray())); 189 189 Add(new Result(TestTheilsUStatisticMeanResultName, "The average Theil's U statistic (reference: mean value) of the forecasts of the model on the test partition", new DoubleArray())); 190 Add(new Result(TrainingTheilsUStatisticM AResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the training partition", new DoubleArray()));191 Add(new Result(TestTheilsUStatisticM AResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the test partition", new DoubleArray()));190 Add(new Result(TrainingTheilsUStatisticMaResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the training partition", new DoubleArray())); 191 Add(new Result(TestTheilsUStatisticMaResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the test partition", new DoubleArray())); 192 192 horizon = 1; 193 193 } … … 213 213 anyNewResult = true; 214 214 } 215 if (!ContainsKey(TrainingTheilsUStatisticM AResultName)) {216 Add(new Result(TrainingTheilsUStatisticM AResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the training partition", new DoubleArray()));217 anyNewResult = true; 218 } 219 if (!ContainsKey(TestTheilsUStatisticM AResultName)) {220 Add(new Result(TestTheilsUStatisticM AResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the test partition", new DoubleArray()));215 if (!ContainsKey(TrainingTheilsUStatisticMaResultName)) { 216 Add(new Result(TrainingTheilsUStatisticMaResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the training partition", new DoubleArray())); 217 anyNewResult = true; 218 } 219 if (!ContainsKey(TestTheilsUStatisticMaResultName)) { 220 Add(new Result(TestTheilsUStatisticMaResultName, "The average Theil's U statistic (reference: moving average) of the forecasts of the model on the test partition", new DoubleArray())); 221 221 anyNewResult = true; 222 222 } … … 226 226 227 227 protected void CalculateResults() { 228 OnlineCalculatorError errorState;229 228 string[] targetVariables = ProblemData.TargetVariables.ToArray(); 230 /*231 double[] estimatedTrainingValues = PrognosedTrainingValues.ToArray(); // cache values232 double[] originalTrainingValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndizes).ToArray();233 double[] estimatedTestValues = PrognosedTestValues.ToArray(); // cache values234 double[] originalTestValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TestIndizes).ToArray();235 236 double trainingMse = OnlineMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);237 TrainingMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingMse : double.NaN;238 double testMse = OnlineMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);239 TestMeanSquaredError = errorState == OnlineCalculatorError.None ? testMse : double.NaN;240 241 double trainingMae = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);242 TrainingMeanAbsoluteError = errorState == OnlineCalculatorError.None ? trainingMae : double.NaN;243 double testMae = OnlineMeanAbsoluteErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);244 TestMeanAbsoluteError = errorState == OnlineCalculatorError.None ? testMae : double.NaN;245 246 double trainingR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);247 TrainingRSquared = errorState == OnlineCalculatorError.None ? trainingR2 : double.NaN;248 double testR2 = OnlinePearsonsRSquaredCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);249 TestRSquared = errorState == OnlineCalculatorError.None ? testR2 : double.NaN;250 251 double trainingRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);252 TrainingRelativeError = errorState == OnlineCalculatorError.None ? trainingRelError : double.NaN;253 double testRelError = OnlineMeanAbsolutePercentageErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);254 TestRelativeError = errorState == OnlineCalculatorError.None ? testRelError : double.NaN;255 256 double trainingNmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);257 TrainingNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? trainingNmse : double.NaN;258 double testNmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);259 TestNormalizedMeanSquaredError = errorState == OnlineCalculatorError.None ? testNmse : double.NaN;260 */261 262 //double[] trainingDs = new double[targetVariables.Length];263 //double[] testDs = new double[targetVariables.Length];264 265 //double[] trainingWds = new double[targetVariables.Length];266 //double[] testWds = new double[targetVariables.Length];267 268 //double[] trainingTheilsU = new double[targetVariables.Length];269 //double[] testTheilsU = new double[targetVariables.Length];270 229 271 230 var trainingMseCalculators = new OnlineMeanSquaredErrorCalculator[targetVariables.Length]; … … 315 274 316 275 var allPrognosedTrainingValues = GetPrognosedValues(ProblemData.TrainingIndizes, horizon).GetEnumerator(); 276 double[] mean = new double[targetVariables.Length]; 277 for (int t = 0; t < targetVariables.Length; t++) { 278 double variance; 279 OnlineCalculatorError meanErrorState, varErrorState; 280 OnlineMeanAndVarianceCalculator.Calculate(ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes), out mean[t], out variance, out meanErrorState, out varErrorState); 281 if (meanErrorState != OnlineCalculatorError.None) mean[t] = 0.0; 282 } 317 283 foreach (var row in ProblemData.TrainingIndizes) { 318 284 if (row + horizon < ProblemData.Dataset.Rows) { 319 285 allPrognosedTrainingValues.MoveNext(); 320 var prognosedTrainingValues = allPrognosedTrainingValues.Current.SelectMany(x => x ).ToArray();286 var prognosedTrainingValues = allPrognosedTrainingValues.Current.SelectMany(x => x.ToArray()).ToArray(); 321 287 for (int t = 0; t < targetVariables.Length; t++) { 322 288 var actualContinuation = ProblemData.Dataset.GetDoubleValues(targetVariables[t], … … 328 294 select ProblemData.Dataset.GetDoubleValue(targetVariables[t], r) 329 295 ).Average(); 330 var mean = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes).Median();331 296 double startValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], row - 1); 332 297 var prognosedContinuation = prognosedTrainingValues.Skip(t).TakeEvery(targetVariables.Length); … … 334 299 trainingWdsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation); 335 300 trainingTheilsULastCalculators[t].Add(startValue, actualContinuation, prognosedContinuation); 336 trainingTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean ), actualContinuation, prognosedContinuation);301 trainingTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean[t]), actualContinuation, prognosedContinuation); 337 302 trainingTheilsUMovingAverageCalculators[t].Add(startValue, movingAverageContinuation, actualContinuation, prognosedContinuation); 338 303 … … 352 317 } 353 318 } 354 var allPrognosedTestValues = GetPrognosedValues(ProblemData.TestIndizes, horizon). GetEnumerator();319 var allPrognosedTestValues = GetPrognosedValues(ProblemData.TestIndizes, horizon).ToArray().AsEnumerable().GetEnumerator(); 355 320 foreach (var row in ProblemData.TestIndizes) { 356 321 if (row + horizon < ProblemData.Dataset.Rows) { 357 322 allPrognosedTestValues.MoveNext(); 358 var prognosedTestValues = allPrognosedTestValues.Current.SelectMany(x => x) .ToArray();323 var prognosedTestValues = allPrognosedTestValues.Current.SelectMany(x => x); 359 324 for (int t = 0; t < targetVariables.Length; t++) { 360 325 var actualContinuation = ProblemData.Dataset.GetDoubleValues(targetVariables[t], … … 366 331 select ProblemData.Dataset.GetDoubleValue(targetVariables[t], r) 367 332 ).Average(); 368 // mean must be calculated on the training set!369 var mean = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes).Median();370 333 double startValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], row - 1); 371 var prognosedContinuation = prognosedTestValues.Skip(t).TakeEvery(targetVariables.Length) ;334 var prognosedContinuation = prognosedTestValues.Skip(t).TakeEvery(targetVariables.Length).ToArray(); 372 335 testDsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation); 373 336 testWdsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation); 374 337 testTheilsULastCalculators[t].Add(startValue, actualContinuation, prognosedContinuation); 375 testTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean ), actualContinuation, prognosedContinuation);338 testTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean[t]), actualContinuation, prognosedContinuation); 376 339 testTheilsUMovingAverageCalculators[t].Add(startValue, movingAverageContinuation, actualContinuation, prognosedContinuation); 377 340 378 341 var actualContinuationEnumerator = actualContinuation.GetEnumerator(); 379 var prognosedContinuationEnumerator = prognosedContinuation. GetEnumerator();342 var prognosedContinuationEnumerator = prognosedContinuation.AsEnumerable().GetEnumerator(); 380 343 while (actualContinuationEnumerator.MoveNext() & prognosedContinuationEnumerator.MoveNext()) { 381 344 testMseCalculators[t].Add(actualContinuationEnumerator.Current, prognosedContinuationEnumerator.Current);
Note: See TracChangeset
for help on using the changeset viewer.