[2154] | 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
|
---|
| 21 | using System;
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[2324] | 26 | using HeuristicLab.Common;
|
---|
[2154] | 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.DataAnalysis;
|
---|
[2226] | 29 | using HeuristicLab.Modeling;
|
---|
[2154] | 30 | using HeuristicLab.GP;
|
---|
| 31 | using HeuristicLab.GP.StructureIdentification;
|
---|
[2222] | 32 | using HeuristicLab.GP.Interfaces;
|
---|
[2154] | 33 |
|
---|
| 34 | namespace HeuristicLab.LinearRegression {
|
---|
| 35 | public class LinearRegressionOperator : OperatorBase {
|
---|
| 36 | private static double constant = 1.0;
|
---|
| 37 |
|
---|
| 38 | public LinearRegressionOperator() {
|
---|
[2440] | 39 | AddVariableInfo(new VariableInfo("TargetVariable", "Name of the target variable", typeof(StringData), VariableKind.In));
|
---|
[2154] | 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));
|
---|
[2360] | 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));
|
---|
[2222] | 45 | AddVariableInfo(new VariableInfo("LinearRegressionModel", "Formula that was calculated by linear regression", typeof(IGeneticProgrammingModel), VariableKind.Out | VariableKind.New));
|
---|
[2154] | 46 | }
|
---|
| 47 |
|
---|
| 48 | public override IOperation Apply(IScope scope) {
|
---|
| 49 | Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
|
---|
[2440] | 50 | string targetVariable = GetVariableValue<StringData>("TargetVariable", scope, true).Data;
|
---|
| 51 | int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
|
---|
[2154] | 52 | int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
|
---|
| 53 | int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
|
---|
[2360] | 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 |
|
---|
[2542] | 59 | IFunctionTree tree = CreateModel(dataset, targetVariable, dataset.VariableNames, start, end, minTimeOffset, maxTimeOffset);
|
---|
| 60 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("LinearRegressionModel"), new GeneticProgrammingModel(tree)));
|
---|
| 61 | return null;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public static IFunctionTree CreateModel(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables, int start, int end) {
|
---|
| 65 | return CreateModel(dataset, targetVariable, inputVariables, start, end, 0, 0);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public static IFunctionTree CreateModel(Dataset dataset, string targetVariable, IEnumerable<string> inputVariables,
|
---|
| 69 | int start, int end,
|
---|
| 70 | int minTimeOffset, int maxTimeOffset) {
|
---|
| 71 | int targetVariableIndex = dataset.GetVariableIndex(targetVariable);
|
---|
| 72 | List<int> allowedColumns = CalculateAllowedColumns(dataset, targetVariableIndex, inputVariables.Select(x => dataset.GetVariableIndex(x)), start, end);
|
---|
[2440] | 73 | List<int> allowedRows = CalculateAllowedRows(dataset, targetVariableIndex, allowedColumns, start, end, minTimeOffset, maxTimeOffset);
|
---|
[2154] | 74 |
|
---|
[2360] | 75 | double[,] inputMatrix = PrepareInputMatrix(dataset, allowedColumns, allowedRows, minTimeOffset, maxTimeOffset);
|
---|
[2440] | 76 | double[] targetVector = PrepareTargetVector(dataset, targetVariableIndex, allowedRows);
|
---|
[2154] | 77 | double[] coefficients = CalculateCoefficients(inputMatrix, targetVector);
|
---|
[2542] | 78 | return CreateModel(coefficients, allowedColumns.Select(i => dataset.GetVariableName(i)).ToList(), minTimeOffset, maxTimeOffset);
|
---|
[2154] | 79 | }
|
---|
| 80 |
|
---|
[2542] | 81 | private static IFunctionTree CreateModel(double[] coefficients, List<string> allowedVariables, int minTimeOffset, int maxTimeOffset) {
|
---|
[2154] | 82 | IFunctionTree root = new Addition().GetTreeNode();
|
---|
[2538] | 83 |
|
---|
[2360] | 84 | int timeOffsetRange = (maxTimeOffset - minTimeOffset + 1);
|
---|
[2154] | 85 |
|
---|
[2360] | 86 | for (int i = 0; i < allowedVariables.Count; i++) {
|
---|
| 87 | for (int timeOffset = minTimeOffset; timeOffset <= maxTimeOffset; timeOffset++) {
|
---|
| 88 | var vNode = (VariableFunctionTree)new GP.StructureIdentification.Variable().GetTreeNode();
|
---|
| 89 | vNode.VariableName = allowedVariables[i];
|
---|
| 90 | vNode.Weight = coefficients[(i * timeOffsetRange) + (timeOffset - minTimeOffset)];
|
---|
| 91 | vNode.SampleOffset = timeOffset;
|
---|
[2538] | 92 | root.AddSubTree(vNode);
|
---|
[2360] | 93 | }
|
---|
[2154] | 94 | }
|
---|
[2222] | 95 | var cNode = (ConstantFunctionTree)new Constant().GetTreeNode();
|
---|
[2154] | 96 |
|
---|
[2222] | 97 | cNode.Value = coefficients[coefficients.Length - 1];
|
---|
[2538] | 98 | root.AddSubTree(cNode);
|
---|
| 99 | return root;
|
---|
[2154] | 100 | }
|
---|
| 101 |
|
---|
[2542] | 102 | private static double[] CalculateCoefficients(double[,] inputMatrix, double[] targetVector) {
|
---|
[2445] | 103 | int retVal = 0;
|
---|
| 104 | alglib.linreg.linearmodel lm = new alglib.linreg.linearmodel();
|
---|
| 105 | alglib.linreg.lrreport ar = new alglib.linreg.lrreport();
|
---|
| 106 | int n = targetVector.Length;
|
---|
| 107 | int p = inputMatrix.GetLength(1);
|
---|
| 108 | double[,] dataset = new double[n, p];
|
---|
| 109 | for (int row = 0; row < n; row++) {
|
---|
[2538] | 110 | for (int column = 0; column < p - 1; column++) {
|
---|
[2445] | 111 | dataset[row, column] = inputMatrix[row, column];
|
---|
| 112 | }
|
---|
[2538] | 113 | dataset[row, p - 1] = targetVector[row];
|
---|
[2445] | 114 | }
|
---|
[2538] | 115 | alglib.linreg.lrbuild(ref dataset, n, p - 1, ref retVal, ref lm, ref ar);
|
---|
[2445] | 116 | if (retVal != 1) throw new ArgumentException("Error in calculation of linear regression model");
|
---|
| 117 | Console.Out.WriteLine("ALGLIB Linear Regression: Estimated generalization RMS = {0}", ar.cvrmserror);
|
---|
[2154] | 118 |
|
---|
[2445] | 119 | double[] coefficients = new double[p];
|
---|
| 120 | for (int i = 0; i < p; i++) {
|
---|
[2538] | 121 | coefficients[i] = lm.w[i + 4];
|
---|
[2445] | 122 | }
|
---|
[2154] | 123 | return coefficients;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | //returns list of valid row indexes (rows without NaN values)
|
---|
[2542] | 127 | private static List<int> CalculateAllowedRows(Dataset dataset, int targetVariable, IList<int> allowedColumns, int start, int end, int minTimeOffset, int maxTimeOffset) {
|
---|
[2154] | 128 | List<int> allowedRows = new List<int>();
|
---|
| 129 | bool add;
|
---|
| 130 | for (int row = start; row < end; row++) {
|
---|
| 131 | add = true;
|
---|
[2367] | 132 | for (int colIndex = 0; colIndex < allowedColumns.Count && add == true; colIndex++) {
|
---|
[2360] | 133 | for (int timeOffset = minTimeOffset; timeOffset <= maxTimeOffset; timeOffset++) {
|
---|
| 134 | if (
|
---|
| 135 | row + timeOffset < 0 ||
|
---|
| 136 | row + timeOffset > dataset.Rows ||
|
---|
[2367] | 137 | double.IsNaN(dataset.GetValue(row + timeOffset, allowedColumns[colIndex])) ||
|
---|
| 138 | double.IsInfinity(dataset.GetValue(row + timeOffset, allowedColumns[colIndex])) ||
|
---|
[2360] | 139 | double.IsNaN(dataset.GetValue(row + timeOffset, targetVariable))) {
|
---|
| 140 | add = false;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
[2154] | 143 | }
|
---|
| 144 | if (add)
|
---|
| 145 | allowedRows.Add(row);
|
---|
| 146 | add = true;
|
---|
| 147 | }
|
---|
| 148 | return allowedRows;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[2367] | 151 | //returns list of valid column indexes (columns which contain max. 10% NaN (or infinity) and contain at least two different values)
|
---|
[2542] | 152 | private static List<int> CalculateAllowedColumns(Dataset dataset, int targetVariable, IEnumerable<int> inputVariables, int start, int end) {
|
---|
[2165] | 153 | List<int> allowedColumns = new List<int>();
|
---|
[2367] | 154 | double n = end - start;
|
---|
[2542] | 155 | foreach (int inputVariable in inputVariables) {// = 0; i < dataset.Columns; i++) {
|
---|
| 156 | double nanRatio = dataset.CountMissingValues(inputVariable, start, end) / n;
|
---|
| 157 | if (inputVariable != targetVariable && nanRatio < 0.1 && dataset.GetRange(inputVariable, start, end) > 0.0) {
|
---|
| 158 | allowedColumns.Add(inputVariable);
|
---|
[2367] | 159 | }
|
---|
[2165] | 160 | }
|
---|
| 161 | return allowedColumns;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[2542] | 164 | private static double[,] PrepareInputMatrix(Dataset dataset, List<int> allowedColumns, List<int> allowedRows, int minTimeOffset, int maxTimeOffset) {
|
---|
[2154] | 165 | int rowCount = allowedRows.Count;
|
---|
[2360] | 166 | int timeOffsetRange = (maxTimeOffset - minTimeOffset + 1);
|
---|
| 167 | double[,] matrix = new double[rowCount, (allowedColumns.Count * timeOffsetRange) + 1];
|
---|
| 168 | for (int row = 0; row < allowedRows.Count; row++)
|
---|
| 169 | for (int col = 0; col < allowedColumns.Count; col++) {
|
---|
| 170 | for (int timeOffset = minTimeOffset; timeOffset <= maxTimeOffset; timeOffset++)
|
---|
| 171 | matrix[row, (col * timeOffsetRange) + (timeOffset - minTimeOffset)] = dataset.GetValue(allowedRows[row] + timeOffset, allowedColumns[col]);
|
---|
| 172 | }
|
---|
[2154] | 173 | //add constant 1.0 in last column
|
---|
| 174 | for (int i = 0; i < rowCount; i++)
|
---|
[2360] | 175 | matrix[i, allowedColumns.Count * timeOffsetRange] = constant;
|
---|
[2154] | 176 | return matrix;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[2542] | 179 | private static double[] PrepareTargetVector(Dataset dataset, int targetVariable, List<int> allowedRows) {
|
---|
[2154] | 180 | int rowCount = allowedRows.Count;
|
---|
| 181 | double[] targetVector = new double[rowCount];
|
---|
| 182 | double[] samples = dataset.Samples;
|
---|
| 183 | for (int row = 0; row < rowCount; row++) {
|
---|
| 184 | targetVector[row] = dataset.GetValue(allowedRows[row], targetVariable);
|
---|
| 185 | }
|
---|
| 186 | return targetVector;
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 | }
|
---|