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

Last change on this file since 15938 was 15938, checked in by pfleck, 13 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("Logarithm", "Logarithm transformation x' = log(x, Base)")]
33  [StorableClass]
34  public class LogarithmTransformation : Transformation<double> {
35    #region Parameters
36    private FixedValueParameter<DoubleValue> BaseParameter {
37      get { return (FixedValueParameter<DoubleValue>)Parameters["Base"]; }
38    }
39    private FixedValueParameter<DoubleValue> OffsetParameter {
40      get { return (FixedValueParameter<DoubleValue>)Parameters["Offset"]; }
41    }
42    #endregion
43
44    #region Properties
45    public double Base {
46      get { return BaseParameter.Value.Value; }
47      set { BaseParameter.Value.Value = value; }
48    }
49    public double Offset {
50      get { return OffsetParameter.Value.Value; }
51      set { OffsetParameter.Value.Value = value; }
52    }
53    #endregion
54
55    #region Constructor, Cloning & Persistence
56    public LogarithmTransformation()
57      : base() {
58      Parameters.Add(new FixedValueParameter<DoubleValue>("Base", "Base the logarithm", new DoubleValue(Math.E)));
59      Parameters.Add(new FixedValueParameter<DoubleValue>("Offset", "Offset before the logarithm", new DoubleValue(0.0)));
60    }
61
62    protected LogarithmTransformation(LogarithmTransformation original, Cloner cloner)
63      : base(original, cloner) {
64    }
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new LogarithmTransformation(this, cloner);
67    }
68
69    [StorableConstructor]
70    protected LogarithmTransformation(bool deserializing)
71      : base(deserializing) {
72    }
73    #endregion
74
75    public override bool Check(IEnumerable<double> data, out string errorMessage) {
76      if (data.Any(x => x + Offset <= 0)) {
77        errorMessage = "Log is not available for zero or negative values";
78        return false;
79      }
80      return base.Check(data, out errorMessage);
81    }
82
83    public override IEnumerable<double> Apply(IEnumerable<double> data) {
84      return Apply(data, Base, Offset);
85    }
86
87    public override IEnumerable<double> InverseApply(IEnumerable<double> data) {
88      return InverseApply(data, Base, Offset);
89    }
90
91
92    public static IEnumerable<double> Apply(IEnumerable<double> data, double @base = Math.E, double offset = 0.0) {
93      return data.Select(x => Math.Log(x + offset, @base));
94    }
95
96    public static IEnumerable<double> InverseApply(IEnumerable<double> data, double @base = Math.E, double offset = 0.0) {
97      return ExponentialTransformation.InverseApply(data, @base).Select(x => x - offset);
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.