Free cookie consent management tool by TermsFeed Policy Generator

Changeset 4475 for branches/DataAnalysis


Ignore:
Timestamp:
09/23/10 13:49:30 (14 years ago)
Author:
gkronber
Message:

Fixed bugs in time series prognosis classes #1142.

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  
    9595        int samplesEnd = Content.ProblemData.TrainingSamplesEnd.Value;
    9696
    97         double[] alpha, beta;
    9897        double quality;
    9998        conditionVariableName = Content.ConditionalEvaluationVariable;
    10099        targetVariables = Content.ProblemData.TargetVariables.CheckedItems.Select(x => x.Value.Value);
    101100        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());
    104101        interpreter = Content.Model.Interpreter;
    105102        horizon = Content.Horizon;
    106103        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);
    116118
    117119        this.CalculateReplacementNodes();
    118120
    119         this.CalculateNodeImpacts(simplifiedExpressionTree, simplifiedExpressionTree.Root, quality);
     121        this.CalculateNodeImpacts(simplifiedExpressionTree, simplifiedExpressionTree.Root, quality, rows);
    120122        // show only interesing part
    121123        this.treeChart.Tree = new SymbolicExpressionTree(simplifiedExpressionTree.Root.SubTrees[0]);
     
    134136          while (start.SubTrees.Count > 0) start.RemoveSubTree(0);
    135137          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();
    137141          ConstantTreeNode constantTreeNode = MakeConstantTreeNode(constantTreeNodeValue);
    138142          replacementNodes[node] = constantTreeNode;
     
    141145    }
    142146
    143     private void CalculateNodeImpacts(SymbolicExpressionTree tree, SymbolicExpressionTreeNode currentTreeNode, double originalQuality) {
     147    private void CalculateNodeImpacts(SymbolicExpressionTree tree, SymbolicExpressionTreeNode currentTreeNode, double originalQuality, IEnumerable<int> rows) {
    144148      foreach (SymbolicExpressionTreeNode childNode in currentTreeNode.SubTrees.ToList()) {
    145149        if (!(childNode.Symbol is StartSymbol || childNode.Symbol is ProgramRootSymbol)) {
    146150          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;
    151151          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);
    162156
    163157          nodeImpacts[childNode] = newQuality / originalQuality;
    164158          SwitchNode(currentTreeNode, replacementNodes[childNode], childNode);
    165159        }
    166         CalculateNodeImpacts(tree, childNode, originalQuality);
     160        CalculateNodeImpacts(tree, childNode, originalQuality, rows);
    167161      }
    168162    }
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis-3.3.csproj

    r4401 r4475  
    108108    <Compile Include="HeuristicLabProblemsDataAnalysisMultiVariateTimeSeriesPrognosisPlugin.cs" />
    109109    <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>
    110114    <Compile Include="Symbolic\SymbolicTimeSeriesPrognosisSolution.cs" />
    111115    <Compile Include="Symbolic\SymbolicTimeSeriesPrognosisModel.cs" />
    112     <Compile Include="Symbolic\Evaluators\SymbolicTimeSeriesPrognosisNormalizedMseEvaluator.cs" />
    113116    <Compile Include="Symbolic\Evaluators\SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.cs" />
    114117    <Compile Include="Symbolic\Interfaces\ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator.cs" />
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Analyzer/ValidationBestScaledSymbolicTimeSeriesPrognosisSolutionAnalyzer.cs

    r4457 r4475  
    125125      get { return (OptionalValueParameter<StringValue>)Parameters[ConditionVariableParameterName]; }
    126126    }
    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     }
    133127    public IValueLookupParameter<ISymbolicTimeSeriesExpressionInterpreter> SymbolicExpressionTreeInterpreterParameter {
    134128      get { return (IValueLookupParameter<ISymbolicTimeSeriesExpressionInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
     
    137131      get { return (IValueLookupParameter<MultiVariateDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
    138132    }
     133    public ILookupParameter<ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator> EvaluatorParameter {
     134      get { return (ILookupParameter<ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator>)Parameters["Evaluator"]; }
     135    }
    139136    public IValueLookupParameter<IntValue> ValidationSamplesStartParameter {
    140137      get { return (IValueLookupParameter<IntValue>)Parameters[ValidationSamplesStartParameterName]; }
     
    172169    public IValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
    173170      get { return (IValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
     171    }
     172    public ILookupParameter<BoolValue> MaximizationParameter {
     173      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
    174174    }
    175175    #endregion
     
    181181      get { return SymbolicExpressionTreeParameter.ActualValue; }
    182182    }
    183     public ItemArray<DoubleArray> Alpha {
    184       get { return AlphaParameter.ActualValue; }
    185     }
    186     public ItemArray<DoubleArray> Beta {
    187       get { return BetaParameter.ActualValue; }
    188     }
    189183    public ISymbolicTimeSeriesExpressionInterpreter SymbolicExpressionTreeInterpreter {
    190184      get { return SymbolicExpressionTreeInterpreterParameter.ActualValue; }
     
    222216    public PercentValue RelativeNumberOfEvaluatedSamples {
    223217      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
     218    }
     219    public BoolValue Maximization {
     220      get { return MaximizationParameter.ActualValue; }
    224221    }
    225222    #endregion
     
    230227      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
    231228      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."));
    234229      Parameters.Add(new ValueLookupParameter<ISymbolicTimeSeriesExpressionInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used for the analysis of symbolic expression trees."));
    235230      Parameters.Add(new ValueLookupParameter<MultiVariateDataAnalysisProblemData>(ProblemDataParameterName, "The problem data for which the symbolic expression tree is a solution."));
     
    237232      Parameters.Add(new ValueLookupParameter<IntValue>(ValidationSamplesEndParameterName, "The last index of the validation partition of the data set."));
    238233      Parameters.Add(new ValueLookupParameter<IntValue>(PredictionHorizonParameterName, "The number of time steps for which to create a forecast."));
     234      Parameters.Add(new LookupParameter<ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator>("Evaluator", ""));
    239235      Parameters.Add(new ValueLookupParameter<DoubleArray>(UpperEstimationLimitParameterName, "The upper estimation limit that was set for the evaluation of the symbolic expression trees."));
    240236      Parameters.Add(new ValueLookupParameter<DoubleArray>(LowerEstimationLimitParameterName, "The lower estimation limit that was set for the evaluation of the symbolic expression trees."));
     
    246242      Parameters.Add(new LookupParameter<DataTable>(VariableFrequenciesParameterName, "The variable frequencies table to use for the calculation of variable impacts"));
    247243      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"));
    249245    }
    250246
     
    252248    private ValidationBestScaledSymbolicTimeSeriesPrognosisSolutionAnalyzer(bool deserializing) : base(deserializing) { }
    253249
     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
    254257    public override IOperation Apply() {
    255       var alphas = Alpha;
    256       var betas = Beta;
    257258      var trees = SymbolicExpressionTree;
    258259
    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;
    266261
    267262      int trainingStart = ProblemData.TrainingSamplesStart.Value;
     
    275270      int rowCount = (int)Math.Ceiling((validationEnd - validationStart) * RelativeNumberOfEvaluatedSamples.Value);
    276271      IEnumerable<int> rows = RandomEnumerable.SampleRandomNumbers(Random.Next(), validationStart, validationEnd, rowCount);
    277       double bestValidationNmse = double.MaxValue;
     272      double bestValidationQuality = Maximization.Value ? double.MinValue : double.MaxValue;
    278273      SymbolicExpressionTree bestTree = null;
    279274      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;
    287288          bestTree = tree;
    288289        }
     
    290291
    291292
    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);
    294298        model.Name = "Time series prognosis model";
    295299        model.Description = "Best solution on validation partition found over the whole run.";
    296300
    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());
    298302        solution.Name = BestSolutionParameterName;
    299303        solution.Description = "Best solution on validation partition found over the whole run.";
    300304
    301305        BestSolutionParameter.ActualValue = solution;
    302         BestSolutionQualityParameter.ActualValue = new DoubleValue(bestValidationNmse);
     306        BestSolutionQualityParameter.ActualValue = new DoubleValue(bestValidationQuality);
    303307
    304308        // BestSymbolicTimeSeriesPrognosisSolutionAnalyzer.UpdateBestSolutionResults(solution, ProblemData, Results, Generations, VariableFrequencies);
     
    313317      Results[BestSolutionResultName].Value = BestSolutionParameter.ActualValue;
    314318      Results[BestSolutionQualityParameterName].Value = new DoubleValue(BestSolutionQualityParameter.ActualValue.Value);
    315       Results[CurrentBestValidationQualityParameterName].Value = new DoubleValue(bestValidationNmse);
     319      Results[CurrentBestValidationQualityParameterName].Value = new DoubleValue(bestValidationQuality);
    316320
    317321      DataTable validationValues = (DataTable)Results[BestSolutionQualityValuesParameterName].Value;
    318322      AddValue(validationValues, BestSolutionQualityParameter.ActualValue.Value, BestSolutionQualityParameterName, BestSolutionQualityParameterName);
    319       AddValue(validationValues, bestValidationNmse, CurrentBestValidationQualityParameterName, CurrentBestValidationQualityParameterName);
     323      AddValue(validationValues, bestValidationQuality, CurrentBestValidationQualityParameterName, CurrentBestValidationQualityParameterName);
    320324      #endregion
    321325      return base.Apply();
    322326    }
    323327
    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
    327361
    328362    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  
    4141  [Item("SymbolicTimeSeriesPrognosisNormalizedMseEvaluator", "")]
    4242  [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 {
    13044
    13145    public SymbolicTimeSeriesPrognosisNormalizedMseEvaluator()
    13246      : 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)));
    14547    }
    14648
    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);
    15751    }
    15852
    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,
    16755      IEnumerable<int> rows, int predictionHorizon, DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit) {
    16856      double[] zeros = new double[problemData.TargetVariables.CheckedItems.Count()];
    16957      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,
    17159        predictionHorizon, lowerEstimationLimit, upperEstimationLimit, ones, zeros);
    17260    }
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Evaluators/SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator.cs

    r4460 r4475  
    3737using HeuristicLab.Problems.DataAnalysis.Regression;
    3838using HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Interfaces;
     39using HeuristicLab.Problems.DataAnalysis.MultiVariate.Evaluators;
    3940
    4041namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Evaluators {
    4142  [Item("SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator", "")]
    4243  [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 {
    13945
    14046    public SymbolicTimeSeriesPrognosisScaledNormalizedMseEvaluator()
    14147      : 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)));
    15648    }
    15749
    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) {
    15955      double[] alpha, beta;
    16056      double quality;
    161       string conditionVariableName = ConditionVariableName == null ? null : ConditionVariableName.Value;
    162       int nRows = (int)Math.Ceiling((SamplesEnd.Value - SamplesStart.Value) * RelativeNumberOfEvaluatedSamples.Value);
    16357
    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();
    16863
    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,
    17278        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;
    17780    }
    17881
    179     public static double Evaluate(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData,
     82    public static double CalculateWithScaling(SymbolicExpressionTree tree, MultiVariateDataAnalysisProblemData problemData,
    18083      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,
    18984      IEnumerable<int> rows, int predictionHorizon,
    19085      DoubleArray lowerEstimationLimit, DoubleArray upperEstimationLimit,
    19186      double[] beta, double[] alpha) {
    192       if (conditionVariableName != null) {
    193         rows = from row in rows
    194                where !problemData.Dataset[conditionVariableName, row].IsAlmost(0.0)
    195                select row;
    196       }
    19787      IEnumerable<string> selectedTargetVariables = from targetVariable in problemData.TargetVariables
    19888                                                    where problemData.TargetVariables.ItemChecked(targetVariable)
     
    20898                                                     select problemData.Dataset[targetVariable, row + step]).ToArray();
    20999
    210       List<OnlineNormalizedMeanSquaredErrorEvaluator> evaluators = new List<OnlineNormalizedMeanSquaredErrorEvaluator>();
    211       foreach (string targetVariable in selectedTargetVariables)
    212         evaluators.Add(new OnlineNormalizedMeanSquaredErrorEvaluator());
     100      var evaluator = new OnlineMultiVariateEvaluator<OnlineNormalizedMeanSquaredErrorEvaluator>();
    213101
    214102      var estimatedValuesEnumerator = estimatedValues.GetEnumerator();
     
    217105        double[] original = originalValuesEnumerator.Current;
    218106        double[] estimated = estimatedValuesEnumerator.Current;
    219         for (int i = 0; i < evaluators.Count; i++) {
     107        for (int i = 0; i < estimated.Length; i++) {
    220108          if (double.IsNaN(estimated[i])) estimated[i] = upperEstimationLimit[i];
    221109          else estimated[i] = Math.Min(upperEstimationLimit[i], Math.Max(lowerEstimationLimit[i], estimated[i]));
    222           evaluators[i].Add(original[i], estimated[i]);
    223110        }
     111        evaluator.Add(original, estimated);
    224112      }
    225113
    226       double quality = evaluators.Select(x => x.NormalizedMeanSquaredError).Sum();
     114      double quality = evaluator.Value;
    227115      return quality;
    228116    }
    229117
    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) {
    260119      List<OnlineMeanAndVarianceCalculator> estimatedVarianceEvaluators = new List<OnlineMeanAndVarianceCalculator>();
    261120      List<OnlineCovarianceEvaluator> covarianceEvaluators = new List<OnlineCovarianceEvaluator>();
    262121      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();
    269125      var originalEnumerator = originalValues.GetEnumerator();
    270126      while (estimatedEnumerator.MoveNext() & originalEnumerator.MoveNext()) {
    271127        double[] original = originalEnumerator.Current;
    272128        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());
    279137          }
     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");
    280149        }
    281150      }
     151
    282152      if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())
    283153        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++) {
    285157        if (cnt[component] < 2) {
    286           alphas[component] = 0;
    287           betas[component] = 1;
     158          alpha[component] = 0;
     159          beta[component] = 1;
    288160        } else {
    289161          if (estimatedVarianceEvaluators[component].PopulationVariance.IsAlmost(0.0))
    290             betas[component] = 1;
     162            beta[component] = 1;
    291163          else
    292             betas[component] = covarianceEvaluators[component].Covariance / estimatedVarianceEvaluators[component].PopulationVariance;
     164            beta[component] = covarianceEvaluators[component].Covariance / estimatedVarianceEvaluators[component].PopulationVariance;
    293165
    294           alphas[component] = originalMeanCalculators[component].Mean - betas[component] * estimatedVarianceEvaluators[component].Mean;
     166          alpha[component] = originalMeanCalculators[component].Mean - beta[component] * estimatedVarianceEvaluators[component].Mean;
    295167        }
    296168      }
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Interfaces/ISingleObjectiveSymbolicTimeSeriesPrognosisEvaluator.cs

    r4113 r4475  
    3535namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Interfaces {
    3636  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);
    3740  }
    3841
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/SymbolicTimeSeriesExpressionInterpreter.cs

    r4113 r4475  
    201201
    202202    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) {
    204205        var variableTreeNode = instr.dynamicNode as VariableTreeNode;
    205206        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      }
    210208      return instr;
    211209    }
     
    361359            double sum = 0.0;
    362360            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);
    366364          }
    367365        case OpCodes.Differential: {
     
    369367            if (row + diffTreeNode.Lag - 2 < 0 || row + diffTreeNode.Lag >= dataset.Rows)
    370368              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;
    375373          }
    376374        case OpCodes.Integral: {
     
    380378            double sum = 0;
    381379            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;
    385383          }
    386384        case OpCodes.Constant: {
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/SymbolicTimeSeriesPrognosisSolution.cs

    r4461 r4475  
    3131using System.Drawing;
    3232using System;
     33using HeuristicLab.Data;
    3334
    3435namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic {
     
    4445    [Storable]
    4546    private string conditionalEvaluationVariable;
     47    [Storable]
     48    private double[] lowerEstimationLimit;
     49    [Storable]
     50    private double[] upperEstimationLimit;
    4651
    4752    public string Filename { get; set; }
     
    5459    }
    5560
    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)
    5762      : this() {
    5863      this.problemData = problemData;
     
    6065      this.horizon = horizon;
    6166      this.conditionalEvaluationVariable = conditionalEvaluationVariable;
     67      this.lowerEstimationLimit = (double[])lowerEstimationLimit.Clone();
     68      this.upperEstimationLimit = (double[])upperEstimationLimit.Clone();
    6269    }
    6370
     
    99106        }
    100107      }
     108    }
     109
     110    public double GetLowerEstimationLimit(int i) {
     111      return lowerEstimationLimit[i];
     112    }
     113    public double GetUpperEstimationLimit(int i) {
     114      return upperEstimationLimit[i];
    101115    }
    102116
     
    139153    public IEnumerable<double[]> EstimatedValues {
    140154      get {
    141         return model.GetEstimatedValues(problemData, 0, problemData.Dataset.Rows);
     155        return ApplyEstimationLimits(model.GetEstimatedValues(problemData, 0, problemData.Dataset.Rows));
    142156      }
    143157    }
     
    145159    public IEnumerable<double[]> EstimatedTrainingValues {
    146160      get {
    147         return model.GetEstimatedValues(problemData, problemData.TrainingSamplesStart.Value, problemData.TrainingSamplesEnd.Value);
     161        return ApplyEstimationLimits(model.GetEstimatedValues(problemData, problemData.TrainingSamplesStart.Value, problemData.TrainingSamplesEnd.Value));
    148162      }
    149163    }
     
    151165    public IEnumerable<double[]> EstimatedTestValues {
    152166      get {
    153         return model.GetEstimatedValues(problemData, problemData.TestSamplesStart.Value, problemData.TestSamplesEnd.Value);
     167        return ApplyEstimationLimits(model.GetEstimatedValues(problemData, problemData.TestSamplesStart.Value, problemData.TestSamplesEnd.Value));
    154168      }
    155169    }
     
    198212      clone.horizon = horizon;
    199213      clone.conditionalEvaluationVariable = conditionalEvaluationVariable;
     214      clone.lowerEstimationLimit = (double[])lowerEstimationLimit.Clone();
     215      clone.upperEstimationLimit = (double[])upperEstimationLimit.Clone();
    200216      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      }
    201226    }
    202227  }
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate/3.3/HeuristicLab.Problems.DataAnalysis.MultiVariate-3.3.csproj

    r4065 r4475  
    109109    <None Include="HeuristicLabProblemsDataAnalysisMultiVariatePlugin.cs.frame" />
    110110    <None Include="Properties\AssemblyInfo.frame" />
     111    <Compile Include="Evaluators\OnlineMultiVariateEvaluator.cs" />
     112    <Compile Include="Evaluators\OnlineMeanMahalanobisDistanceEvaluator.cs" />
    111113    <Compile Include="HeuristicLabProblemsDataAnalysisMultiVariatePlugin.cs" />
     114    <Compile Include="Interfaces\IMultiVariateOnlineEvaluator.cs" />
    112115    <Compile Include="Interfaces\IMultiVariateDataAnalysisEvaluator.cs" />
    113116    <Compile Include="Interfaces\IMultiVariateDataAnalysisModel.cs" />
     
    143146      <Project>{125D3006-67F5-48CB-913E-73C0548F17FA}</Project>
    144147      <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>
    145156    </ProjectReference>
    146157    <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  
    9292        var model = new SymbolicRegressionModel((ISymbolicExpressionTreeInterpreter)SymbolicExpressionTreeInterpreter.Clone(),
    9393          SymbolicExpressionTree[i]);
    94         var solution = new SymbolicRegressionSolution(ProblemData, model, lowerEstimationLimit, upperEstimationLimit);
     94        var solution = new SymbolicRegressionSolution((DataAnalysisProblemData)ProblemData.Clone(), model, lowerEstimationLimit, upperEstimationLimit);
    9595        solution.Name = BestSolutionParameterName;
    9696        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  
    321321      var scaledBestTrainingTree = GetScaledTree(bestTrainingTree);
    322322
    323       SymbolicRegressionSolution bestTrainingSolution = new SymbolicRegressionSolution(ProblemData,
     323      SymbolicRegressionSolution bestTrainingSolution = new SymbolicRegressionSolution((DataAnalysisProblemData)ProblemData.Clone(),
    324324        new SymbolicRegressionModel(SymbolicExpressionTreeInterpreter, scaledBestTrainingTree),
    325325        lowerEstimationLimit, upperEstimationLimit);
     
    336336        var model = new SymbolicRegressionModel((ISymbolicExpressionTreeInterpreter)SymbolicExpressionTreeInterpreter.Clone(),
    337337          scaledTree);
    338         var solution = new SymbolicRegressionSolution(ProblemData, model, lowerEstimationLimit, upperEstimationLimit);
     338        var solution = new SymbolicRegressionSolution((DataAnalysisProblemData)ProblemData.Clone(), model, lowerEstimationLimit, upperEstimationLimit);
    339339        solution.Name = BestSolutionParameterName;
    340340        solution.Description = "Best solution on validation partition found over the whole run.";
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Views/3.3/RunCollectionMonteCarloVariableImpactView.cs

    r4197 r4475  
    3838  [View("RunCollection Monte-Carlo Variable Impact View")]
    3939  public partial class RunCollectionMonteCarloVariableImpactView : AsynchronousContentView {
    40     private const string validationBestModelResultName = "Best solution (on validiation set)";
     40    private const string validationBestModelResultName = "Best solution (on validation set)";
    4141    public RunCollectionMonteCarloVariableImpactView() {
    4242      InitializeComponent();
  • branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Views/3.3/RunCollectionWinklerVariableImpactView.cs

    r4197 r4475  
    3838  [View("RunCollection Winkler Variable Impact View")]
    3939  public partial class RunCollectionWinklerVariableImpactView : AsynchronousContentView {
    40     private const string validationBestModelResultName = "Best solution (on validiation set)";
     40    private const string validationBestModelResultName = "Best solution (on validation set)";
    4141    public RunCollectionWinklerVariableImpactView() {
    4242      InitializeComponent();
Note: See TracChangeset for help on using the changeset viewer.