Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/14/12 13:59:47 (12 years ago)
Author:
mkommend
Message:

#1081: Corrected evaluators and time series models.

Location:
branches/HeuristicLab.TimeSeries
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveEvaluator.cs

    r7989 r8486  
    2323using System;
    2424using System.Collections.Generic;
     25using System.Linq;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
     
    6162    [ThreadStatic]
    6263    private static double[] cache;
    63     protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues, IOnlineCalculator calculator, int maxRows) {
     64    protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues,
     65      double lowerEstimationLimit, double upperEstimationLimit,
     66      IOnlineCalculator calculator, int maxRows) {
    6467      if (cache == null || cache.GetLength(0) < maxRows) {
    6568        cache = new double[maxRows];
     
    7679        double target = targetValuesEnumerator.Current;
    7780        double estimated = estimatedValuesEnumerator.Current;
     81        cache[i] = estimated;
    7882        linearScalingCalculator.Add(estimated, target);
    79         cache[i] = estimated;
    8083        i++;
    8184      }
     
    8588      //calculate the quality by using the passed online calculator
    8689      targetValuesEnumerator = targetValues.GetEnumerator();
    87       i = 0;
    88       while (targetValuesEnumerator.MoveNext()) {
    89         double target = targetValuesEnumerator.Current;
    90         double estimated = cache[i] * beta + alpha;
    91         calculator.Add(target, estimated);
    92         i++;
     90      var scaledBoundedEstimatedValuesEnumerator = Enumerable.Range(0, i).Select(x => cache[x] * beta + alpha)
     91        .LimitToRange(lowerEstimationLimit, upperEstimationLimit).GetEnumerator();
     92
     93      while (targetValuesEnumerator.MoveNext() & scaledBoundedEstimatedValuesEnumerator.MoveNext()) {
     94        calculator.Add(targetValuesEnumerator.Current, scaledBoundedEstimatedValuesEnumerator.Current);
    9395      }
    9496    }
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator.cs

    r8458 r8486  
    3535    [StorableConstructor]
    3636    protected SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator(bool deserializing) : base(deserializing) { }
    37     protected SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator(SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator original, Cloner cloner)
    38       : base(original, cloner) {
    39     }
     37    protected SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator(SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator original, Cloner cloner) : base(original, cloner) { }
    4038    public override IDeepCloneable Clone(Cloner cloner) {
    4139      return new SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator(this, cloner);
     
    6563      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows.Zip(horizions, Enumerable.Range).SelectMany(r => r));
    6664      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows, horizions).SelectMany(x => x);
    67       IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
    6865      OnlineCalculatorError errorState;
    6966
     
    7168      if (applyLinearScaling && horizon == 1) { //perform normal evaluation and afterwards scale the solution and calculate the fitness value       
    7269        var mseCalculator = new OnlineMeanSquaredErrorCalculator();
    73         CalculateWithScaling(targetValues, boundedEstimatedValues, mseCalculator, problemData.Dataset.Rows * horizon);
     70        CalculateWithScaling(targetValues, estimatedValues, lowerEstimationLimit, upperEstimationLimit, mseCalculator, problemData.Dataset.Rows * horizon);
    7471        errorState = mseCalculator.ErrorState;
    7572        mse = mseCalculator.MeanSquaredError;
     
    7976        var scaledSolution = model.SymbolicExpressionTree;
    8077        estimatedValues = interpreter.GetSymbolicExpressionTreeValues(scaledSolution, problemData.Dataset, rows, horizions).SelectMany(x => x);
    81         boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     78        var boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
    8279        mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
    83       } else
     80      } else {
     81        var boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
    8482        mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
     83      }
    8584
    8685      if (errorState != OnlineCalculatorError.None) return Double.NaN;
    8786      else return mse;
    88 
    8987    }
    9088
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs

    r8436 r8486  
    419419          }
    420420        case OpCodes.Variable: {
    421             if (row < 0 || row >= dataset.Rows)
    422               return double.NaN;
     421            if (row < 0 || row >= dataset.Rows) return double.NaN;
    423422            var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
    424423            return ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight;
     
    427426            var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode;
    428427            int actualRow = row + laggedVariableTreeNode.Lag;
    429             if (actualRow < 0 || actualRow >= dataset.Rows)
    430               return double.NaN;
     428            if (actualRow < 0 || actualRow >= dataset.Rows) return double.NaN;
    431429            return ((IList<double>)currentInstr.iArg0)[actualRow] * laggedVariableTreeNode.Weight;
    432430          }
     
    439437        //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function
    440438        case OpCodes.VariableCondition: {
    441             if (row < 0 || row >= dataset.Rows)
    442               return double.NaN;
     439            if (row < 0 || row >= dataset.Rows) return double.NaN;
    443440            var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode;
    444441            double variableValue = ((IList<double>)currentInstr.iArg0)[row];
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Views/3.4/HeuristicLab.Problems.DataAnalysis.Views-3.4.csproj

    r7989 r8486  
    278278      <DependentUpon>RegressionSolutionView.cs</DependentUpon>
    279279    </Compile>
    280     <Compile Include="TimeSeriesPrognosis\TimeSeriesPrognosisSolutionLineChartView.cs">
    281       <SubType>UserControl</SubType>
    282     </Compile>
    283     <Compile Include="TimeSeriesPrognosis\TimeSeriesPrognosisSolutionLineChartView.Designer.cs">
    284       <DependentUpon>TimeSeriesPrognosisSolutionLineChartView.cs</DependentUpon>
    285     </Compile>
    286     <Compile Include="TimeSeriesPrognosis\TimeSeriesPrognosisSolutionPrognosedValuesView.cs">
    287       <SubType>UserControl</SubType>
    288     </Compile>
    289     <Compile Include="TimeSeriesPrognosis\TimeSeriesPrognosisSolutionPrognosedValuesView.Designer.cs">
    290       <DependentUpon>TimeSeriesPrognosisSolutionPrognosedValuesView.cs</DependentUpon>
    291     </Compile>
    292280    <None Include="HeuristicLab.snk" />
    293281    <None Include="Plugin.cs.frame" />
     
    312300    </BootstrapperPackage>
    313301  </ItemGroup>
    314   <ItemGroup />
     302  <ItemGroup>
     303    <Folder Include="TimeSeriesPrognosis\" />
     304  </ItemGroup>
    315305  <ItemGroup>
    316306    <ProjectReference Include="..\..\HeuristicLab.Problems.DataAnalysis\3.4\HeuristicLab.Problems.DataAnalysis-3.4.csproj">
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/Models/TimeSeriesPrognosisAutoRegressiveModel.cs

    r8468 r8486  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     
    6566        int row = rowsEnumerator.Current;
    6667        int horizon = horizonsEnumerator.Current;
     68        if (row - TimeOffset < 0) {
     69          yield return Enumerable.Repeat(double.NaN, horizon);
     70          continue;
     71        }
     72
    6773        double[] prognosis = new double[horizon];
    68 
    6974        for (int h = 0; h < horizon; h++) {
    7075          double estimatedValue = 0.0;
    71           for (int i = 1; i < TimeOffset; i++) {
     76          for (int i = 1; i <= TimeOffset; i++) {
    7277            int offset = h - i;
    7378            if (offset >= 0) estimatedValue += prognosis[offset] * Phi[i - 1];
     
    9095      foreach (int row in rows) {
    9196        double estimatedValue = 0.0;
     97        if (row - TimeOffset < 0) {
     98          yield return double.NaN;
     99          continue;
     100        }
     101
    92102        for (int i = 1; i <= TimeOffset; i++) {
    93103          estimatedValue += targetVariables[row - i] * Phi[i - 1];
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/Models/TimeSeriesPrognosisMovingAverageModel.cs

    r8468 r8486  
    6060        int horizon = horizonsEnumerator.Current;
    6161        int startIndex = row - WindowSize;
     62        if (startIndex < 0) {
     63          yield return Enumerable.Repeat(double.NaN, horizon);
     64          continue;
     65        }
    6266
    63         if (startIndex < 0) startIndex = 0;
    64         int count = row - startIndex - 1;
    65 
    66         List<double> targetValues = dataset.GetDoubleValues(TargetVariable, Enumerable.Range(startIndex, count)).ToList();
     67        List<double> targetValues = dataset.GetDoubleValues(TargetVariable, Enumerable.Range(startIndex, WindowSize)).ToList();
    6768        int position = 0;
    6869        for (int i = 0; i < horizon; i++) {
    69           double prognosis = targetValues.GetRange(position, count).Average();
     70          double prognosis = targetValues.GetRange(position, WindowSize).Average();
    7071          targetValues.Add(prognosis);
    71           if (count < WindowSize) count++;
    72           else position++;
     72          position++;
    7373        }
    7474        yield return targetValues.GetRange(targetValues.Count - horizon, horizon);
     
    8080
    8181    public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
    82       return GetPrognosedValues(dataset, rows, rows.Select(r => 1)).SelectMany(e => e);
    83     }
    84     public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows, int x) {
    8582      var targetValues = dataset.GetReadOnlyDoubleValues(TargetVariable).ToList();
    8683      foreach (int row in rows) {
    87         yield return targetValues.GetRange(row - WindowSize, WindowSize).Average();
     84        if (row - WindowSize < 0) yield return double.NaN;
     85        else yield return targetValues.GetRange(row - WindowSize, WindowSize).Average();
    8886      }
    8987    }
    90 
    9188
    9289    public ITimeSeriesPrognosisSolution CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData) {
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/TimeSeriesPrognosis/TimeSeriesPrognosisProblemData.cs

    r8477 r8486  
    15781578    }
    15791579
    1580     public TimeSeriesPrognosisProblemData() : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable) { }
     1580    public TimeSeriesPrognosisProblemData()
     1581      : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable) {
     1582      TrainingPartition.Start = 50;
     1583    }
    15811584    public TimeSeriesPrognosisProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable)
    15821585      : base(dataset, allowedInputVariables, targetVariable) {
     
    15871590      TestHorizonParameter.Hidden = true;
    15881591
     1592      TrainingPartition.Start = Math.Min((int)(TrainingPartition.Size * 0.2), 50);
     1593
    15891594      RegisterParameterEventHandlers();
    15901595    }
Note: See TracChangeset for help on using the changeset viewer.