- Timestamp:
- 12/09/11 12:56:21 (13 years ago)
- Location:
- branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolution.cs
r7100 r7160 49 49 public override IEnumerable<IEnumerable<double>> PrognosedTrainingValues { 50 50 get { 51 return GetPrognosedValues( Enumerable.Range(ProblemData.TrainingPartition.Start,1),51 return GetPrognosedValues(ProblemData.TrainingIndizes.Take(1), 52 52 ProblemData.TrainingPartition.End - ProblemData.TrainingPartition.Start) 53 53 .First(); … … 56 56 public override IEnumerable<IEnumerable<double>> PrognosedTestValues { 57 57 get { 58 return GetPrognosedValues( Enumerable.Range(ProblemData.TestPartition.Start,1),58 return GetPrognosedValues(ProblemData.TestIndizes.Take(1), 59 59 ProblemData.TestPartition.End - ProblemData.TestPartition.Start) 60 60 .First(); -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisSolutionBase.cs
r7154 r7160 45 45 private const string TrainingWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (training)"; 46 46 private const string TestWeightedDirectionalSymmetryResultName = "Average weighted directional symmetry (test)"; 47 private const string TrainingTheilsUStatisticResultName = "Average Theil's U (training)"; 48 private const string TestTheilsUStatisticResultName = "Average Theil's U (test)"; 47 private const string TrainingTheilsUStatisticLastResultName = "Average Theil's U (last) (training)"; 48 private const string TestTheilsUStatisticLastResultName = "Average Theil's U (last) (test)"; 49 private const string TrainingTheilsUStatisticMeanResultName = "Average Theil's U (mean) (training)"; 50 private const string TestTheilsUStatisticMeanResultName = "Average Theil's U (mean) (test)"; 51 private const string TrainingTheilsUStatisticMAResultName = "Average Theil's U (moving average) (training)"; 52 private const string TestTheilsUStatisticMAResultName = "Average Theil's U (moving average) (test)"; 49 53 50 54 public new ITimeSeriesPrognosisModel Model { … … 56 60 get { return (ITimeSeriesPrognosisProblemData)base.ProblemData; } 57 61 set { base.ProblemData = value; } 62 } 63 64 [Storable] 65 private int horizon; 66 public int Horizon { 67 get { return horizon; } 68 set { 69 if (horizon != value) { 70 horizon = value; 71 RecalculateResults(); 72 } 73 } 58 74 } 59 75 … … 119 135 private set { this[TestWeightedDirectionalSymmetryResultName].Value = new DoubleArray(value); } 120 136 } 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); } 137 public double[] TrainingTheilsUStatisticLast { 138 get { return ((DoubleArray)this[TrainingTheilsUStatisticLastResultName].Value).ToArray(); } 139 private set { this[TrainingTheilsUStatisticLastResultName].Value = new DoubleArray(value); } 140 } 141 public double[] TestTheilsUStatisticLast { 142 get { return ((DoubleArray)this[TestTheilsUStatisticLastResultName].Value).ToArray(); } 143 private set { this[TestTheilsUStatisticLastResultName].Value = new DoubleArray(value); } 144 } 145 public double[] TrainingTheilsUStatisticMean { 146 get { return ((DoubleArray)this[TrainingTheilsUStatisticMeanResultName].Value).ToArray(); } 147 private set { this[TrainingTheilsUStatisticMeanResultName].Value = new DoubleArray(value); } 148 } 149 public double[] TestTheilsUStatisticMean { 150 get { return ((DoubleArray)this[TestTheilsUStatisticMeanResultName].Value).ToArray(); } 151 private set { this[TestTheilsUStatisticMeanResultName].Value = new DoubleArray(value); } 152 } 153 public double[] TrainingTheilsUStatisticMovingAverage { 154 get { return ((DoubleArray)this[TrainingTheilsUStatisticMAResultName].Value).ToArray(); } 155 private set { this[TrainingTheilsUStatisticMAResultName].Value = new DoubleArray(value); } 156 } 157 public double[] TestTheilsUStatisticMovingAverage { 158 get { return ((DoubleArray)this[TestTheilsUStatisticMAResultName].Value).ToArray(); } 159 private set { this[TestTheilsUStatisticMAResultName].Value = new DoubleArray(value); } 128 160 } 129 161 #endregion … … 133 165 protected TimeSeriesPrognosisSolutionBase(TimeSeriesPrognosisSolutionBase original, Cloner cloner) 134 166 : base(original, cloner) { 167 this.horizon = original.horizon; 135 168 } 136 169 protected TimeSeriesPrognosisSolutionBase(ITimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData) … … 150 183 Add(new Result(TrainingWeightedDirectionalSymmetryResultName, "The average weighted directional symmetry of the forecasts of the model on the training partition", new DoubleArray())); 151 184 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())); 185 Add(new Result(TrainingTheilsUStatisticLastResultName, "The average Theil's U statistic (reference: previous value) of the forecasts of the model on the training partition", new DoubleArray())); 186 Add(new Result(TestTheilsUStatisticLastResultName, "The average Theil's U statistic (reference: previous value) of the forecasts of the model on the test partition", new DoubleArray())); 187 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())); 188 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())); 189 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())); 190 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())); 191 horizon = 1; 154 192 } 155 193 156 194 [StorableHook(HookType.AfterDeserialization)] 157 195 private void AfterDeserialization() { 158 196 if (horizon == 0) horizon = 1; 159 197 } 160 198 … … 207 245 var trainingWdsCalculators = new OnlineWeightedDirectionalSymmetryCalculator[targetVariables.Length]; 208 246 var testWdsCalculators = new OnlineWeightedDirectionalSymmetryCalculator[targetVariables.Length]; 209 var trainingTheilsUCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length]; 210 var testTheilsUCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length]; 247 var trainingTheilsULastCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length]; 248 var testTheilsULastCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length]; 249 var trainingTheilsUMeanCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length]; 250 var testTheilsUMeanCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length]; 251 var trainingTheilsUMovingAverageCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length]; 252 var testTheilsUMovingAverageCalculators = new OnlineTheilsUStatisticCalculator[targetVariables.Length]; 211 253 for (int i = 0; i < targetVariables.Length; i++) { 212 254 trainingDsCalculators[i] = new OnlineDirectionalSymmetryCalculator(); … … 214 256 trainingWdsCalculators[i] = new OnlineWeightedDirectionalSymmetryCalculator(); 215 257 testWdsCalculators[i] = new OnlineWeightedDirectionalSymmetryCalculator(); 216 trainingTheilsUCalculators[i] = new OnlineTheilsUStatisticCalculator(); 217 testTheilsUCalculators[i] = new OnlineTheilsUStatisticCalculator(); 258 trainingTheilsULastCalculators[i] = new OnlineTheilsUStatisticCalculator(); 259 testTheilsULastCalculators[i] = new OnlineTheilsUStatisticCalculator(); 260 trainingTheilsUMeanCalculators[i] = new OnlineTheilsUStatisticCalculator(); 261 testTheilsUMeanCalculators[i] = new OnlineTheilsUStatisticCalculator(); 262 trainingTheilsUMovingAverageCalculators[i] = new OnlineTheilsUStatisticCalculator(); 263 testTheilsUMovingAverageCalculators[i] = new OnlineTheilsUStatisticCalculator(); 218 264 } 219 265 220 var allPrognosedTrainingValues = PrognosedTrainingValues.SelectMany(x => x).ToArray(); 221 var allPrognosedTestValues = PrognosedTestValues.SelectMany(x => x).ToArray(); 222 for (int t = 0; t < targetVariables.Length; t++) { 223 var actualTrainingValues = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes); 224 double startTrainingValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], ProblemData.TrainingIndizes.First() - 1); 225 var prognosedTrainingValues = allPrognosedTrainingValues.Skip(t).TakeEvery(targetVariables.Length); 226 trainingDsCalculators[t].Add(startTrainingValue, actualTrainingValues, prognosedTrainingValues); 227 trainingWdsCalculators[t].Add(startTrainingValue, actualTrainingValues, prognosedTrainingValues); 228 trainingTheilsUCalculators[t].Add(startTrainingValue, actualTrainingValues, prognosedTrainingValues); 229 230 var actualTestValues = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TestIndizes); 231 double startTestValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], ProblemData.TestIndizes.First() - 1); 232 var prognosedTestValues = allPrognosedTestValues.Skip(t).TakeEvery(targetVariables.Length); 233 testDsCalculators[t].Add(startTestValue, actualTestValues, prognosedTestValues); 234 testWdsCalculators[t].Add(startTestValue, actualTestValues, prognosedTestValues); 235 testTheilsUCalculators[t].Add(startTestValue, actualTestValues, prognosedTestValues); 266 var allPrognosedTrainingValues = GetPrognosedValues(ProblemData.TrainingIndizes, horizon).GetEnumerator(); 267 foreach (var row in ProblemData.TrainingIndizes) { 268 if (row + horizon < ProblemData.Dataset.Rows) { 269 allPrognosedTrainingValues.MoveNext(); 270 var prognosedTrainingValues = allPrognosedTrainingValues.Current.SelectMany(x => x).ToArray(); 271 for (int t = 0; t < targetVariables.Length; t++) { 272 var actualContinuation = ProblemData.Dataset.GetDoubleValues(targetVariables[t], 273 Enumerable.Range(row, horizon)); 274 int maWindow = 10 * horizon; 275 var movingAverageContinuation = from h in Enumerable.Range(0, horizon) 276 select (from r in Enumerable.Range(row + h - maWindow, maWindow - h) 277 where r > 0 278 select ProblemData.Dataset.GetDoubleValue(targetVariables[t], r) 279 ).Average(); 280 var mean = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes).Median(); 281 double startValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], row - 1); 282 var prognosedContinuation = prognosedTrainingValues.Skip(t).TakeEvery(targetVariables.Length); 283 trainingDsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation); 284 trainingWdsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation); 285 trainingTheilsULastCalculators[t].Add(startValue, actualContinuation, prognosedContinuation); 286 trainingTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean), actualContinuation, prognosedContinuation); 287 trainingTheilsUMovingAverageCalculators[t].Add(startValue, movingAverageContinuation, actualContinuation, prognosedContinuation); 288 } 289 } 236 290 } 291 var allPrognosedTestValues = GetPrognosedValues(ProblemData.TestIndizes, horizon).GetEnumerator(); 292 foreach (var row in ProblemData.TestIndizes) { 293 if (row + horizon < ProblemData.Dataset.Rows) { 294 allPrognosedTestValues.MoveNext(); 295 var prognosedTestValues = allPrognosedTestValues.Current.SelectMany(x => x).ToArray(); 296 for (int t = 0; t < targetVariables.Length; t++) { 297 var actualContinuation = ProblemData.Dataset.GetDoubleValues(targetVariables[t], 298 Enumerable.Range(row, horizon)); 299 int maWindow = 10 * horizon; 300 var movingAverageContinuation = from h in Enumerable.Range(0, horizon) 301 select (from r in Enumerable.Range(row + h - maWindow, maWindow - h) 302 where r > 0 303 select ProblemData.Dataset.GetDoubleValue(targetVariables[t], r) 304 ).Average(); 305 // mean must be calculated on the training set! 306 var mean = ProblemData.Dataset.GetDoubleValues(targetVariables[t], ProblemData.TrainingIndizes).Median(); 307 double startValue = ProblemData.Dataset.GetDoubleValue(targetVariables[t], row - 1); 308 var prognosedContinuation = prognosedTestValues.Skip(t).TakeEvery(targetVariables.Length); 309 testDsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation); 310 testWdsCalculators[t].Add(startValue, actualContinuation, prognosedContinuation); 311 testTheilsULastCalculators[t].Add(startValue, actualContinuation, prognosedContinuation); 312 testTheilsUMeanCalculators[t].Add(startValue, actualContinuation.Select(x => mean), actualContinuation, prognosedContinuation); 313 testTheilsUMovingAverageCalculators[t].Add(startValue, movingAverageContinuation, actualContinuation, prognosedContinuation); 314 } 315 } 316 } 237 317 238 318 TrainingDirectionalSymmetry = trainingDsCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : 0.0) … … 244 324 TestWeightedDirectionalSymmetry = testWdsCalculators.Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity) 245 325 .ToArray(); 246 TrainingTheilsUStatistic = trainingDsCalculators 247 .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity) 248 .ToArray(); 249 TestTheilsUStatistic = testTheilsUCalculators 326 TrainingTheilsUStatisticLast = trainingTheilsULastCalculators 327 .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity) 328 .ToArray(); 329 TestTheilsUStatisticLast = testTheilsULastCalculators 330 .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity) 331 .ToArray(); 332 TrainingTheilsUStatisticMean = trainingTheilsUMeanCalculators 333 .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity) 334 .ToArray(); 335 TestTheilsUStatisticMean = testTheilsUMeanCalculators 336 .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity) 337 .ToArray(); 338 TrainingTheilsUStatisticMovingAverage = trainingTheilsUMovingAverageCalculators 339 .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity) 340 .ToArray(); 341 TestTheilsUStatisticMovingAverage = testTheilsUMovingAverageCalculators 250 342 .Select(c => c.ErrorState == OnlineCalculatorError.None ? c.Value : double.PositiveInfinity) 251 343 .ToArray(); -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Interfaces/TimeSeriesPrognosis/ITimeSeriesPrognosisSolution.cs
r7100 r7160 23 23 namespace HeuristicLab.Problems.DataAnalysis { 24 24 public interface ITimeSeriesPrognosisSolution : IDataAnalysisSolution { 25 int Horizon { get; set; } 25 26 new ITimeSeriesPrognosisModel Model { get; } 26 27 new ITimeSeriesPrognosisProblemData ProblemData { get; set; } … … 40 41 double[] TrainingNormalizedMeanSquaredError { get; } 41 42 double[] TestNormalizedMeanSquaredError { get; } 42 double[] TrainingTheilsUStatistic { get; } 43 double[] TestTheilsUStatistic { get; } 43 double[] TrainingTheilsUStatisticLast { get; } 44 double[] TestTheilsUStatisticLast { get; } 45 double[] TrainingTheilsUStatisticMean { get; } 46 double[] TestTheilsUStatisticMean { get; } 44 47 double[] TrainingDirectionalSymmetry { get; } 45 48 double[] TestDirectionalSymmetry { get; } -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineTheilsUStatisticCalculator.cs
r7099 r7160 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq; 24 25 using HeuristicLab.Common; 25 26 … … 52 53 53 54 public void Add(double startValue, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) { 55 Add(startValue, actualContinuation.Select(x => startValue), actualContinuation, predictedContinuation); 56 } 57 58 public void Add(double startValue, IEnumerable<double> referenceContinuation, IEnumerable<double> actualContinuation, IEnumerable<double> predictedContinuation) { 54 59 if (double.IsNaN(startValue) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) { 55 60 errorState = errorState | OnlineCalculatorError.InvalidValueAdded; … … 57 62 var actualEnumerator = actualContinuation.GetEnumerator(); 58 63 var predictedEnumerator = predictedContinuation.GetEnumerator(); 59 while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & ErrorState != OnlineCalculatorError.InvalidValueAdded) { 64 var referenceEnumerator = referenceContinuation.GetEnumerator(); 65 while (actualEnumerator.MoveNext() & predictedEnumerator.MoveNext() & referenceEnumerator.MoveNext() 66 & ErrorState != OnlineCalculatorError.InvalidValueAdded) { 60 67 double actual = actualEnumerator.Current; 61 68 double predicted = predictedEnumerator.Current; 62 if (double.IsNaN(actual) || double.IsNaN(predicted)) { 69 double reference = referenceEnumerator.Current; 70 if (double.IsNaN(actual) || double.IsNaN(predicted) || double.IsNaN(reference)) { 63 71 errorState = errorState | OnlineCalculatorError.InvalidValueAdded; 64 72 } else { … … 67 75 squaredErrorMeanCalculator.Add(errorPredictedChange * errorPredictedChange); 68 76 69 double error NoChange =(actual - startValue);70 unbiasedEstimatorMeanCalculator.Add(error NoChange * errorNoChange);77 double errorReference = (reference - startValue) - (actual - startValue); 78 unbiasedEstimatorMeanCalculator.Add(errorReference * errorReference); 71 79 } 72 80 } 73 81 // check if both enumerators are at the end to make sure both enumerations have the same length 74 if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext() ) {82 if (actualEnumerator.MoveNext() || predictedEnumerator.MoveNext() || referenceEnumerator.MoveNext()) { 75 83 errorState = errorState | OnlineCalculatorError.InvalidValueAdded; 76 84 } else { … … 79 87 } 80 88 } 89 81 90 82 91 public void Reset() {
Note: See TracChangeset
for help on using the changeset viewer.