Free cookie consent management tool by TermsFeed Policy Generator

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/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/Evaluators
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • 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      }
Note: See TracChangeset for help on using the changeset viewer.