1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
31 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
32 | using System.Collections.Generic;
|
---|
33 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
34 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
37 | /// <summary>
|
---|
38 | /// Linear regression data analysis algorithm.
|
---|
39 | /// </summary>
|
---|
40 | [Item("Linear Regression", "Linear regression data analysis algorithm.")]
|
---|
41 | [Creatable("Data Analysis")]
|
---|
42 | [StorableClass]
|
---|
43 | public sealed class LinearRegression : FixedDataAnalysisAlgorithm<IRegressionProblem> {
|
---|
44 | private const string LinearRegressionModelResultName = "Linear regression solution";
|
---|
45 |
|
---|
46 | [StorableConstructor]
|
---|
47 | private LinearRegression(bool deserializing) : base(deserializing) { }
|
---|
48 | private LinearRegression(LinearRegression original, Cloner cloner)
|
---|
49 | : base(original, cloner) {
|
---|
50 | }
|
---|
51 | public LinearRegression()
|
---|
52 | : base() {
|
---|
53 | Problem = new RegressionProblem();
|
---|
54 | }
|
---|
55 | [StorableHook(HookType.AfterDeserialization)]
|
---|
56 | private void AfterDeserialization() { }
|
---|
57 |
|
---|
58 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
59 | return new LinearRegression(this, cloner);
|
---|
60 | }
|
---|
61 |
|
---|
62 | #region linear regression
|
---|
63 | protected override void Run() {
|
---|
64 | double rmsError, cvRmsError;
|
---|
65 | var solution = CreateLinearRegressionSolution(Problem.ProblemData, out rmsError, out cvRmsError);
|
---|
66 | Results.Add(new Result(LinearRegressionModelResultName, "The linear regression solution.", solution));
|
---|
67 | Results.Add(new Result("Root mean square error", "The root of the mean of squared errors of the linear regression solution on the training set.", new DoubleValue(rmsError)));
|
---|
68 | Results.Add(new Result("Estimated root mean square error (cross-validation)", "The estimated root of the mean of squared errors of the linear regression solution via cross validation.", new DoubleValue(cvRmsError)));
|
---|
69 | }
|
---|
70 |
|
---|
71 | public static ISymbolicRegressionSolution CreateLinearRegressionSolution(IRegressionProblemData problemData, out double rmsError, out double cvRmsError) {
|
---|
72 | Dataset dataset = problemData.Dataset;
|
---|
73 | string targetVariable = problemData.TargetVariable;
|
---|
74 | IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
|
---|
75 | int samplesStart = problemData.TrainingPartitionStart.Value;
|
---|
76 | int samplesEnd = problemData.TrainingPartitionEnd.Value;
|
---|
77 | IEnumerable<int> rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart);
|
---|
78 | double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows);
|
---|
79 |
|
---|
80 | alglib.linearmodel lm = new alglib.linearmodel();
|
---|
81 | alglib.lrreport ar = new alglib.lrreport();
|
---|
82 | int nRows = inputMatrix.GetLength(0);
|
---|
83 | int nFeatures = inputMatrix.GetLength(1) - 1;
|
---|
84 | double[] coefficients = new double[nFeatures + 1]; // last coefficient is for the constant
|
---|
85 |
|
---|
86 | int retVal = 1;
|
---|
87 | alglib.lrbuild(inputMatrix, nRows, nFeatures, out retVal, out lm, out ar);
|
---|
88 | if (retVal != 1) throw new ArgumentException("Error in calculation of linear regression solution");
|
---|
89 | rmsError = ar.rmserror;
|
---|
90 | cvRmsError = ar.cvrmserror;
|
---|
91 |
|
---|
92 | alglib.lrunpack(lm, out coefficients, out nFeatures);
|
---|
93 |
|
---|
94 | ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
|
---|
95 | ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
|
---|
96 | tree.Root.AddSubTree(startNode);
|
---|
97 | ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
|
---|
98 | startNode.AddSubTree(addition);
|
---|
99 |
|
---|
100 | int col = 0;
|
---|
101 | foreach (string column in allowedInputVariables) {
|
---|
102 | VariableTreeNode vNode = (VariableTreeNode)new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable().CreateTreeNode();
|
---|
103 | vNode.VariableName = column;
|
---|
104 | vNode.Weight = coefficients[col];
|
---|
105 | addition.AddSubTree(vNode);
|
---|
106 | col++;
|
---|
107 | }
|
---|
108 |
|
---|
109 | ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
|
---|
110 | cNode.Value = coefficients[coefficients.Length - 1];
|
---|
111 | addition.AddSubTree(cNode);
|
---|
112 |
|
---|
113 | SymbolicRegressionSolution solution = new SymbolicRegressionSolution(new SymbolicRegressionModel(tree, new SymbolicDataAnalysisExpressionTreeInterpreter()), problemData);
|
---|
114 | return solution;
|
---|
115 | }
|
---|
116 | #endregion
|
---|
117 | }
|
---|
118 | }
|
---|