source: branches/2906_Transformations/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/LinearTransformation.cs @ 15938

Last change on this file since 15938 was 15938, checked in by pfleck, 14 months ago

#2906

  • added Offset to log transformation
  • renamed parameter of linear transformation
  • added RescaleTransformation
File size: 3.6 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.Data;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis {
32  [Item("Linear", "Linear transformation x' = Scale * x + Offset")]
33  [StorableClass]
34  public class LinearTransformation : Transformation<double> {
35
36    #region Parameters
37    private FixedValueParameter<DoubleValue> ScaleParameter {
38      get { return (FixedValueParameter<DoubleValue>)Parameters["Scale"]; }
39    }
40
41    private FixedValueParameter<DoubleValue> OffsetParameter {
42      get { return (FixedValueParameter<DoubleValue>)Parameters["Offset"]; }
43    }
44    #endregion
45
46    #region Properties
47    public double Scale {
48      get { return ScaleParameter.Value.Value; }
49      set { ScaleParameter.Value.Value = value; }
50    }
51
52    public double Offset {
53      get { return OffsetParameter.Value.Value; }
54      set { OffsetParameter.Value.Value = value; }
55    }
56    #endregion
57
58    #region Constructor, Cloning & Persistence
59    public LinearTransformation()
60      : base() {
61      Parameters.Add(new FixedValueParameter<DoubleValue>("Scale", "Multiplicative factor (Slope)", new DoubleValue(1.0)));
62      Parameters.Add(new FixedValueParameter<DoubleValue>("Offset", "Additive factor (Intercept)", new DoubleValue(0.0)));
63    }
64
65    protected LinearTransformation(LinearTransformation original, Cloner cloner)
66      : base(original, cloner) {
67    }
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new LinearTransformation(this, cloner);
70    }
71
72    [StorableConstructor]
73    protected LinearTransformation(bool deserializing)
74      : base(deserializing) {
75    }
76    #endregion
77
78    public override IEnumerable<double> Apply(IEnumerable<double> data) {
79      return Apply(data, Scale, Offset);
80    }
81
82    public override IEnumerable<double> InverseApply(IEnumerable<double> data) {
83      return InverseApply(data, Scale, Offset);
84    }
85
86
87    public static IEnumerable<double> Apply(IEnumerable<double> data, double scale = 1.0, double offset = 0.0) {
88      if (scale.IsAlmost(0.0))
89        throw new InvalidOperationException($"Cannot transform with a {nameof(scale)} of zero because inverse transformation would be invalid.");
90
91      return data.Select(x => scale * x + offset);
92    }
93
94    public static IEnumerable<double> InverseApply(IEnumerable<double> data, double scale = 1.0, double offset = 0.0) {
95      if (scale.IsAlmost(0.0))
96        throw new InvalidOperationException($"Cannot inverse transform with a {nameof(scale)} of zero.");
97
98      return data.Select(x => (x - offset) / scale);
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.