Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/29/11 20:05:38 (13 years ago)
Author:
gkronber
Message:

#1081 worked on multi-variate time series prognosis

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearTimeSeriesPrognosis.cs

    r7099 r7100  
    7676    #region linear regression
    7777    protected override void Run() {
    78       double rmsError, cvRmsError;
    79       var solution = CreateLinearTimeSeriesPrognosisSolution(Problem.ProblemData, MaximalLag, out rmsError, out cvRmsError);
     78      double[] rmsErrors, cvRmsErrors;
     79      var solution = CreateLinearTimeSeriesPrognosisSolution(Problem.ProblemData, MaximalLag, out rmsErrors, out cvRmsErrors);
    8080      Results.Add(new Result(LinearTimeSeriesPrognosisModelResultName, "The linear time-series prognosis solution.", solution));
    81       Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the linear time-series prognosis solution on the training set.", new DoubleValue(rmsError)));
    82       Results.Add(new Result("Estimated root mean square error (cross-validation)", "The estimated root of the mean of squared errors of the linear time-series prognosis solution via cross validation.", new DoubleValue(cvRmsError)));
     81      Results.Add(new Result("Root mean square errors", "The root of the mean of squared errors of the linear time-series prognosis solution on the training set.", new DoubleArray(rmsErrors)));
     82      Results.Add(new Result("Estimated root mean square errors (cross-validation)", "The estimated root of the mean of squared errors of the linear time-series prognosis solution via cross validation.", new DoubleArray(cvRmsErrors)));
    8383    }
    8484
    85     public static ISymbolicTimeSeriesPrognosisSolution CreateLinearTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData, int maximalLag, out double rmsError, out double cvRmsError) {
     85    public static ISymbolicTimeSeriesPrognosisSolution CreateLinearTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData, int maximalLag, out double[] rmsError, out double[] cvRmsError) {
    8686      Dataset dataset = problemData.Dataset;
    87       string targetVariable = problemData.TargetVariable;
    88       IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
    89       IEnumerable<int> rows = problemData.TrainingIndizes;
    90       IEnumerable<int> lags = Enumerable.Range(1, maximalLag);
    91       double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows, lags);
    92       if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
    93         throw new NotSupportedException("Linear time-series prognosis does not support NaN or infinity values in the input dataset.");
     87      string[] targetVariables = problemData.TargetVariables.ToArray();
    9488
    95       alglib.linearmodel lm = new alglib.linearmodel();
    96       alglib.lrreport ar = new alglib.lrreport();
    97       int nRows = inputMatrix.GetLength(0);
    98       int nFeatures = inputMatrix.GetLength(1) - 1;
    99       double[] coefficients = new double[nFeatures + 1]; // last coefficient is for the constant
    100 
    101       int retVal = 1;
    102       alglib.lrbuild(inputMatrix, nRows, nFeatures, out retVal, out lm, out ar);
    103       if (retVal != 1) throw new ArgumentException("Error in calculation of linear time series prognosis solution");
    104       rmsError = ar.rmserror;
    105       cvRmsError = ar.cvrmserror;
    106 
    107       alglib.lrunpack(lm, out coefficients, out nFeatures);
    108 
     89      // prepare symbolic expression tree to hold the models for each target variable
    10990      ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
    11091      ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
    11192      tree.Root.AddSubtree(startNode);
    112       ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
    113       startNode.AddSubtree(addition);
     93      int i = 0;
     94      rmsError = new double[targetVariables.Length];
     95      cvRmsError = new double[targetVariables.Length];
     96      foreach (var targetVariable in targetVariables) {
     97        IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
     98        IEnumerable<int> rows = problemData.TrainingIndizes;
     99        IEnumerable<int> lags = Enumerable.Range(1, maximalLag);
     100        double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset,
     101                                                              allowedInputVariables.Concat(new string[] { targetVariable }),
     102                                                              rows, lags);
     103        if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
     104          throw new NotSupportedException(
     105            "Linear time-series prognosis does not support NaN or infinity values in the input dataset.");
    114106
    115       int col = 0;
    116       foreach (string column in allowedInputVariables) {
    117         foreach (int lag in lags) {
    118           LaggedVariableTreeNode vNode =
    119             (LaggedVariableTreeNode)new HeuristicLab.Problems.DataAnalysis.Symbolic.LaggedVariable().CreateTreeNode();
    120           vNode.VariableName = column;
    121           vNode.Weight = coefficients[col];
    122           vNode.Lag = -lag;
    123           addition.AddSubtree(vNode);
    124           col++;
     107        alglib.linearmodel lm = new alglib.linearmodel();
     108        alglib.lrreport ar = new alglib.lrreport();
     109        int nRows = inputMatrix.GetLength(0);
     110        int nFeatures = inputMatrix.GetLength(1) - 1;
     111        double[] coefficients = new double[nFeatures + 1]; // last coefficient is for the constant
     112
     113        int retVal = 1;
     114        alglib.lrbuild(inputMatrix, nRows, nFeatures, out retVal, out lm, out ar);
     115        if (retVal != 1) throw new ArgumentException("Error in calculation of linear time series prognosis solution");
     116        rmsError[i] = ar.rmserror;
     117        cvRmsError[i] = ar.cvrmserror;
     118
     119        alglib.lrunpack(lm, out coefficients, out nFeatures);
     120
     121        ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
     122
     123        int col = 0;
     124        foreach (string column in allowedInputVariables) {
     125          foreach (int lag in lags) {
     126            LaggedVariableTreeNode vNode =
     127              (LaggedVariableTreeNode)new HeuristicLab.Problems.DataAnalysis.Symbolic.LaggedVariable().CreateTreeNode();
     128            vNode.VariableName = column;
     129            vNode.Weight = coefficients[col];
     130            vNode.Lag = -lag;
     131            addition.AddSubtree(vNode);
     132            col++;
     133          }
    125134        }
     135
     136        ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
     137        cNode.Value = coefficients[coefficients.Length - 1];
     138        addition.AddSubtree(cNode);
     139
     140        startNode.AddSubtree(addition);
     141        i++;
    126142      }
    127143
    128       ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
    129       cNode.Value = coefficients[coefficients.Length - 1];
    130       addition.AddSubtree(cNode);
    131 
    132       SymbolicTimeSeriesPrognosisSolution solution = new SymbolicTimeSeriesPrognosisSolution(new SymbolicTimeSeriesPrognosisModel(tree, new SymbolicDataAnalysisExpressionTreeInterpreter()), (ITimeSeriesPrognosisProblemData)problemData.Clone());
     144      SymbolicTimeSeriesPrognosisSolution solution = new SymbolicTimeSeriesPrognosisSolution(new SymbolicTimeSeriesPrognosisModel(tree, new SymbolicTimeSeriesPrognosisInterpreter(problemData.TargetVariables.ToArray())), (ITimeSeriesPrognosisProblemData)problemData.Clone());
    133145      solution.Model.Name = "Linear Time-Series Prognosis Model";
    134146      return solution;
Note: See TracChangeset for help on using the changeset viewer.