Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2965_CancelablePersistence/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegressionModel.cs @ 16854

Last change on this file since 16854 was 16433, checked in by pfleck, 6 years ago

#2965 Merged recent trunk changes.
Enabled the prepared hooks that allows to cancel the save file using the recently introduced cancelable progressbars (in FileManager).

File size: 5.3 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.Union(factorVariables.Select(f => f.Key)); }
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      var stringInputVariables = factorVariables.Select(f => f.Key).Distinct();
84      this.allowedInputVariables = doubleInputVariables.ToArray();
85      this.factorVariables = factorVariables.Select(kvp => new KeyValuePair<string, IEnumerable<string>>(kvp.Key, new List<string>(kvp.Value))).ToList();
86    }
87
88    [StorableHook(HookType.AfterDeserialization)]
89    private void AfterDeserialization() {
90    }
91
92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new LinearRegressionModel(this, cloner);
94    }
95
96    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
97      double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
98      double[,] factorData = dataset.ToArray(factorVariables, rows);
99
100      inputData = factorData.HorzCat(inputData);
101
102      int n = inputData.GetLength(0);
103      int columns = inputData.GetLength(1);
104
105      for (int row = 0; row < n; row++) {
106        double p = 0.0;
107        for (int column = 0; column < columns; column++) {
108          p += W[column] * inputData[row, column];
109        }
110        p += W[columns];
111        yield return p;
112      }
113    }
114
115    public IEnumerable<double> GetEstimatedVariances(IDataset dataset, IEnumerable<int> rows) {
116      double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
117      double[,] factorData = dataset.ToArray(factorVariables, rows);
118
119      inputData = factorData.HorzCat(inputData);
120
121      int n = inputData.GetLength(0);
122      int columns = inputData.GetLength(1);
123
124      double[] d = new double[C.GetLength(0)];
125     
126      for (int row = 0; row < n; row++) {
127        for (int column = 0; column < columns; column++) {
128          d[column] = inputData[row,column];
129        }
130        d[columns] = 1;
131
132        double var = 0.0;
133        for(int i=0;i<d.Length;i++) {
134          for(int j = 0;j<d.Length;j++) {
135            var += d[i] * C[i, j] * d[j];
136          }
137        }
138        yield return var + NoiseSigma*NoiseSigma;
139      }
140    }
141
142
143    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
144      return new ConfidenceRegressionSolution(this, new RegressionProblemData(problemData));
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.