Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegressionModel.cs @ 16389

Last change on this file since 16389 was 16389, checked in by gkronber, 5 years ago

#2892: merged branch back to trunk

File size: 5.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  /// <summary>
32  /// Represents a linear regression model
33  /// </summary>
34  [StorableClass]
35  [Item("Linear Regression Model", "Represents a linear regression model.")]
36  public sealed class LinearRegressionModel : RegressionModel, IConfidenceRegressionModel {
37
38    [Storable]
39    public double[,] C {
40      get; private set;
41    }
42    [Storable]
43    public double[] W {
44      get; private set;
45    }
46
47    [Storable]
48    public double NoiseSigma {
49      get; private set;
50    }
51   
52    public override IEnumerable<string> VariablesUsedForPrediction {
53      get { return allowedInputVariables; }
54    }
55
56    [Storable]
57    private string[] allowedInputVariables;
58    [Storable]
59    private List<KeyValuePair<string, IEnumerable<string>>> factorVariables;
60
61    [StorableConstructor]
62    private LinearRegressionModel(bool deserializing)
63      : base(deserializing) {
64    }
65    private LinearRegressionModel(LinearRegressionModel original, Cloner cloner)
66      : base(original, cloner) {
67      this.W = original.W;
68      this.C = original.C;
69      this.NoiseSigma = original.NoiseSigma;
70
71      allowedInputVariables = (string[])original.allowedInputVariables.Clone();
72      this.factorVariables = original.factorVariables.Select(kvp => new KeyValuePair<string, IEnumerable<string>>(kvp.Key, new List<string>(kvp.Value))).ToList();
73    }
74    public LinearRegressionModel(double[] w, double[,] covariance, double noiseSigma, string targetVariable, IEnumerable<string> doubleInputVariables, IEnumerable<KeyValuePair<string, IEnumerable<string>>> factorVariables)
75      : base(targetVariable) {
76      this.name = ItemName;
77      this.description = ItemDescription;
78      this.W = new double[w.Length];
79      Array.Copy(w, W, w.Length);
80      this.C = new double[covariance.GetLength(0),covariance.GetLength(1)];
81      Array.Copy(covariance, C, covariance.Length);
82      this.NoiseSigma = noiseSigma;
83      this.allowedInputVariables = doubleInputVariables.ToArray();
84      this.factorVariables = factorVariables.Select(kvp => new KeyValuePair<string, IEnumerable<string>>(kvp.Key, new List<string>(kvp.Value))).ToList();
85    }
86
87    [StorableHook(HookType.AfterDeserialization)]
88    private void AfterDeserialization() {
89    }
90
91    public override IDeepCloneable Clone(Cloner cloner) {
92      return new LinearRegressionModel(this, cloner);
93    }
94
95    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
96      double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
97      double[,] factorData = dataset.ToArray(factorVariables, rows);
98
99      inputData = factorData.HorzCat(inputData);
100
101      int n = inputData.GetLength(0);
102      int columns = inputData.GetLength(1);
103
104      for (int row = 0; row < n; row++) {
105        double p = 0.0;
106        for (int column = 0; column < columns; column++) {
107          p += W[column] * inputData[row, column];
108        }
109        p += W[columns];
110        yield return p;
111      }
112    }
113
114    public IEnumerable<double> GetEstimatedVariances(IDataset dataset, IEnumerable<int> rows) {
115      double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
116      double[,] factorData = dataset.ToArray(factorVariables, rows);
117
118      inputData = factorData.HorzCat(inputData);
119
120      int n = inputData.GetLength(0);
121      int columns = inputData.GetLength(1);
122
123      double[] d = new double[C.GetLength(0)];
124     
125      for (int row = 0; row < n; row++) {
126        for (int column = 0; column < columns; column++) {
127          d[column] = inputData[row,column];
128        }
129        d[columns] = 1;
130
131        double var = 0.0;
132        for(int i=0;i<d.Length;i++) {
133          for(int j = 0;j<d.Length;j++) {
134            var += d[i] * C[i, j] * d[j];
135          }
136        }
137        yield return var + NoiseSigma*NoiseSigma;
138      }
139    }
140
141
142    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
143      return new ConfidenceRegressionSolution(this, new RegressionProblemData(problemData));
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.