Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.LinearRegression/3.2/LinearRegressionOperator.cs @ 2538

Last change on this file since 2538 was 2538, checked in by gkronber, 14 years ago

Fixed #811

File size: 9.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21using System;
22using System.Collections.Generic;
23using System.Linq;
24using System.Text;
25using HeuristicLab.Core;
26using HeuristicLab.Common;
27using HeuristicLab.Data;
28using HeuristicLab.DataAnalysis;
29using HeuristicLab.Modeling;
30using HeuristicLab.GP;
31using HeuristicLab.GP.StructureIdentification;
32using HeuristicLab.GP.Interfaces;
33
34namespace HeuristicLab.LinearRegression {
35  public class LinearRegressionOperator : OperatorBase {
36    private static double constant = 1.0;
37
38    public LinearRegressionOperator() {
39      AddVariableInfo(new VariableInfo("TargetVariable", "Name of the target variable", typeof(StringData), VariableKind.In));
40      AddVariableInfo(new VariableInfo("Dataset", "Dataset with all samples on which to apply the function", typeof(Dataset), VariableKind.In));
41      AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
42      AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
43      AddVariableInfo(new VariableInfo("MaxTimeOffset", "(optional) Maximal time offset for time-series prognosis", typeof(IntData), VariableKind.In));
44      AddVariableInfo(new VariableInfo("MinTimeOffset", "(optional) Minimal time offset for time-series prognosis", typeof(IntData), VariableKind.In));
45      AddVariableInfo(new VariableInfo("LinearRegressionModel", "Formula that was calculated by linear regression", typeof(IGeneticProgrammingModel), VariableKind.Out | VariableKind.New));
46    }
47
48    public override IOperation Apply(IScope scope) {
49      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
50      string targetVariable = GetVariableValue<StringData>("TargetVariable", scope, true).Data;
51      int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
52      int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
53      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
54      IntData maxTimeOffsetData = GetVariableValue<IntData>("MaxTimeOffset", scope, true, false);
55      int maxTimeOffset = maxTimeOffsetData == null ? 0 : maxTimeOffsetData.Data;
56      IntData minTimeOffsetData = GetVariableValue<IntData>("MinTimeOffset", scope, true, false);
57      int minTimeOffset = minTimeOffsetData == null ? 0 : minTimeOffsetData.Data;
58
59      List<int> allowedColumns = CalculateAllowedColumns(dataset, targetVariableIndex, start, end);
60      List<int> allowedRows = CalculateAllowedRows(dataset, targetVariableIndex, allowedColumns, start, end, minTimeOffset, maxTimeOffset);
61
62      double[,] inputMatrix = PrepareInputMatrix(dataset, allowedColumns, allowedRows, minTimeOffset, maxTimeOffset);
63      double[] targetVector = PrepareTargetVector(dataset, targetVariableIndex, allowedRows);
64      double[] coefficients = CalculateCoefficients(inputMatrix, targetVector);
65      IFunctionTree tree = CreateModel(coefficients, allowedColumns.Select(i => dataset.GetVariableName(i)).ToList(), minTimeOffset, maxTimeOffset);
66
67      scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("LinearRegressionModel"), new GeneticProgrammingModel(tree)));
68      return null;
69    }
70
71    private IFunctionTree CreateModel(double[] coefficients, List<string> allowedVariables, int minTimeOffset, int maxTimeOffset) {
72      IFunctionTree root = new Addition().GetTreeNode();
73
74      int timeOffsetRange = (maxTimeOffset - minTimeOffset + 1);
75
76      for (int i = 0; i < allowedVariables.Count; i++) {
77        for (int timeOffset = minTimeOffset; timeOffset <= maxTimeOffset; timeOffset++) {
78          var vNode = (VariableFunctionTree)new GP.StructureIdentification.Variable().GetTreeNode();
79          vNode.VariableName = allowedVariables[i];
80          vNode.Weight = coefficients[(i * timeOffsetRange) + (timeOffset - minTimeOffset)];
81          vNode.SampleOffset = timeOffset;
82          root.AddSubTree(vNode);
83        }
84      }
85      var cNode = (ConstantFunctionTree)new Constant().GetTreeNode();
86
87      cNode.Value = coefficients[coefficients.Length - 1];
88      root.AddSubTree(cNode);
89      return root;
90    }
91
92    private double[] CalculateCoefficients(double[,] inputMatrix, double[] targetVector) {
93      int retVal = 0;
94      alglib.linreg.linearmodel lm = new alglib.linreg.linearmodel();
95      alglib.linreg.lrreport ar = new alglib.linreg.lrreport();
96      int n = targetVector.Length;
97      int p = inputMatrix.GetLength(1);
98      double[,] dataset = new double[n, p];
99      for (int row = 0; row < n; row++) {
100        for (int column = 0; column < p - 1; column++) {
101          dataset[row, column] = inputMatrix[row, column];
102        }
103        dataset[row, p - 1] = targetVector[row];
104      }
105      alglib.linreg.lrbuild(ref dataset, n, p - 1, ref retVal, ref lm, ref ar);
106      if (retVal != 1) throw new ArgumentException("Error in calculation of linear regression model");
107      Console.Out.WriteLine("ALGLIB Linear Regression: Estimated generalization RMS = {0}", ar.cvrmserror);
108
109      double[] coefficients = new double[p];
110      for (int i = 0; i < p; i++) {
111        coefficients[i] = lm.w[i + 4];
112      }
113      return coefficients;
114    }
115
116    //returns list of valid row indexes (rows without NaN values)
117    private List<int> CalculateAllowedRows(Dataset dataset, int targetVariable, IList<int> allowedColumns, int start, int end, int minTimeOffset, int maxTimeOffset) {
118      List<int> allowedRows = new List<int>();
119      bool add;
120      for (int row = start; row < end; row++) {
121        add = true;
122        for (int colIndex = 0; colIndex < allowedColumns.Count && add == true; colIndex++) {
123          for (int timeOffset = minTimeOffset; timeOffset <= maxTimeOffset; timeOffset++) {
124            if (
125              row + timeOffset < 0 ||
126              row + timeOffset > dataset.Rows ||
127              double.IsNaN(dataset.GetValue(row + timeOffset, allowedColumns[colIndex])) ||
128              double.IsInfinity(dataset.GetValue(row + timeOffset, allowedColumns[colIndex])) ||
129              double.IsNaN(dataset.GetValue(row + timeOffset, targetVariable))) {
130              add = false;
131            }
132          }
133        }
134        if (add)
135          allowedRows.Add(row);
136        add = true;
137      }
138      return allowedRows;
139    }
140
141    //returns list of valid column indexes (columns which contain max. 10% NaN (or infinity) and contain at least two different values)
142    private List<int> CalculateAllowedColumns(Dataset dataset, int targetVariable, int start, int end) {
143      List<int> allowedColumns = new List<int>();
144      double n = end - start;
145      for (int i = 0; i < dataset.Columns; i++) {
146        double nanRatio = dataset.CountMissingValues(i, start, end) / n;
147        if (i != targetVariable && nanRatio < 0.1 && dataset.GetRange(i, start, end) > 0.0) {
148          allowedColumns.Add(i);
149        }
150      }
151      return allowedColumns;
152    }
153
154    private double[,] PrepareInputMatrix(Dataset dataset, List<int> allowedColumns, List<int> allowedRows, int minTimeOffset, int maxTimeOffset) {
155      int rowCount = allowedRows.Count;
156      int timeOffsetRange = (maxTimeOffset - minTimeOffset + 1);
157      double[,] matrix = new double[rowCount, (allowedColumns.Count * timeOffsetRange) + 1];
158      for (int row = 0; row < allowedRows.Count; row++)
159        for (int col = 0; col < allowedColumns.Count; col++) {
160          for (int timeOffset = minTimeOffset; timeOffset <= maxTimeOffset; timeOffset++)
161            matrix[row, (col * timeOffsetRange) + (timeOffset - minTimeOffset)] = dataset.GetValue(allowedRows[row] + timeOffset, allowedColumns[col]);
162        }
163      //add constant 1.0 in last column
164      for (int i = 0; i < rowCount; i++)
165        matrix[i, allowedColumns.Count * timeOffsetRange] = constant;
166      return matrix;
167    }
168
169    private double[] PrepareTargetVector(Dataset dataset, int targetVariable, List<int> allowedRows) {
170      int rowCount = allowedRows.Count;
171      double[] targetVector = new double[rowCount];
172      double[] samples = dataset.Samples;
173      for (int row = 0; row < rowCount; row++) {
174        targetVector[row] = dataset.GetValue(allowedRows[row], targetVariable);
175      }
176      return targetVector;
177    }
178  }
179}
Note: See TracBrowser for help on using the repository browser.