Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2898_GeneralizedAdditiveModels/HeuristicLab.Algorithms.DataAnalysis/3.4/GAM/Spline1dModel.cs @ 16101

Last change on this file since 16101 was 15775, checked in by gkronber, 7 years ago

#2898 added simple implementation of GAM based on univariate penalized regression splines with the same penalization factor for each term

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 * and the BEACON Center for the Study of Evolution in Action.
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21#endregion
22
23using System;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.DataAnalysis;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [Item("Spline model (1d)",
33    "Univariate spline model (wrapper for alglib.spline1dmodel)")]
34  [StorableClass]
35  public sealed class Spline1dModel : RegressionModel {
36    // not storable! see persistence properties below
37    private alglib.spline1d.spline1dinterpolant interpolant;
38
39    [Storable]
40    private string[] variablesUsedForPrediction;
41    public override IEnumerable<string> VariablesUsedForPrediction {
42      get {
43        return variablesUsedForPrediction;
44      }
45    }
46
47    [StorableConstructor]
48    private Spline1dModel(bool deserializing) : base(deserializing) {
49      this.interpolant = new alglib.spline1d.spline1dinterpolant();
50    }
51
52    private Spline1dModel(Spline1dModel orig, Cloner cloner) : base(orig, cloner) {
53      this.variablesUsedForPrediction = orig.VariablesUsedForPrediction.ToArray();
54      this.interpolant = (alglib.spline1d.spline1dinterpolant)orig.interpolant.make_copy();
55    }
56    public Spline1dModel(alglib.spline1d.spline1dinterpolant interpolant, string targetVar, string inputVar)
57      : base("Spline model (1d)", "Spline model (1d)") {
58      this.interpolant = (alglib.spline1d.spline1dinterpolant)interpolant.make_copy();
59      this.TargetVariable = targetVar;
60      this.variablesUsedForPrediction = new string[] { inputVar };
61    }
62
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new Spline1dModel(this, cloner);
66    }
67
68    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
69      return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
70    }
71
72    public double GetEstimatedValue(double x) {
73      return alglib.spline1d.spline1dcalc(interpolant, x);
74    }
75
76    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
77      var x = dataset.GetDoubleValues(VariablesUsedForPrediction.First(), rows).ToArray();
78      foreach (var xi in x) {
79        yield return GetEstimatedValue(xi);
80      }
81    }
82
83    #region persistence
84    [Storable]
85    private double[] Interpolant_c {
86      get { return interpolant.c; }
87      set { interpolant.c = value; }
88    }
89    [Storable]
90    private double[] Interpolant_x {
91      get { return interpolant.x; }
92      set { interpolant.x = value; }
93    }
94    [Storable]
95    private int Interpolant_continuity {
96      get { return interpolant.continuity; }
97      set { interpolant.continuity = value; }
98    }
99    [Storable]
100    private int Interpolant_k {
101      get { return interpolant.k; }
102      set { interpolant.k = value; }
103    }
104    [Storable]
105    private int Interpolant_n {
106      get { return interpolant.n; }
107      set { interpolant.n = value; }
108    }
109    [Storable]
110    private bool Interpolant_periodic {
111      get { return interpolant.periodic; }
112      set { interpolant.periodic = value; }
113    }
114    #endregion
115  }
116}
Note: See TracBrowser for help on using the repository browser.