source: branches/3044_variableScaling/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/ShiftStandardDistributionTransformation.cs @ 17391

Last change on this file since 17391 was 17391, checked in by djoedick, 21 months ago

#3044: Added transformation of input variables to problem data and created scaled dataset.

File size: 4.6 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Parameters;
7using HEAL.Attic;
8
9namespace HeuristicLab.Problems.DataAnalysis {
10  [StorableType("F0DC4951-64DF-4058-8E87-748D0FA1AA21")]
11  [Item("Shift Standard Distribution Transformation", "f(x) = ((x - m_org) / s_org ) * s_tar + m_tar | Represents Transformation to unit standard deviation and additional linear transformation to a target Mean and Standard deviation")]
12  public class ShiftStandardDistributionTransformation : Transformation<double> {
13    protected const string OriginalMeanParameterName = "Original Mean";
14    protected const string OriginalStandardDeviationParameterName = "Original Standard Deviation";
15    protected const string MeanParameterName = "Mean";
16    protected const string StandardDeviationParameterName = "Standard Deviation";
17
18    #region Parameters
19    public IValueParameter<DoubleValue> OriginalMeanParameter {
20      get { return (IValueParameter<DoubleValue>)Parameters[OriginalMeanParameterName]; }
21    }
22    public IValueParameter<DoubleValue> OriginalStandardDeviationParameter {
23      get { return (IValueParameter<DoubleValue>)Parameters[OriginalStandardDeviationParameterName]; }
24    }
25    public IValueParameter<DoubleValue> MeanParameter {
26      get { return (IValueParameter<DoubleValue>)Parameters[MeanParameterName]; }
27    }
28    public IValueParameter<DoubleValue> StandardDeviationParameter {
29      get { return (IValueParameter<DoubleValue>)Parameters[StandardDeviationParameterName]; }
30    }
31    #endregion
32
33    #region properties
34    public override string ShortName {
35      get { return "Std"; }
36    }
37    public double OriginalMean {
38      get { return OriginalMeanParameter.Value.Value; }
39      set { OriginalMeanParameter.Value.Value = value; }
40    }
41    public double OriginalStandardDeviation {
42      get { return OriginalStandardDeviationParameter.Value.Value; }
43      set { OriginalStandardDeviationParameter.Value.Value = value; }
44    }
45    public double Mean {
46      get { return MeanParameter.Value.Value; }
47      set { MeanParameter.Value.Value = value; }
48    }
49    public double StandardDeviation {
50      get { return StandardDeviationParameter.Value.Value; }
51      set { StandardDeviationParameter.Value.Value = value; }
52    }
53    #endregion
54
55    [StorableConstructor]
56    protected ShiftStandardDistributionTransformation(StorableConstructorFlag _) : base(_) { }
57    protected ShiftStandardDistributionTransformation(ShiftStandardDistributionTransformation original, Cloner cloner)
58      : base(original, cloner) {
59    }
60    public ShiftStandardDistributionTransformation(IEnumerable<string> allowedColumns)
61      : base(allowedColumns) {
62      Parameters.Add(new ValueParameter<DoubleValue>(OriginalMeanParameterName, "m_org | Mean value of the original data's deviation.", new DoubleValue()));
63      Parameters.Add(new ValueParameter<DoubleValue>(OriginalStandardDeviationParameterName, "s_org | Standard deviation of the original data.", new DoubleValue()));
64      OriginalMeanParameter.Hidden = true;
65      OriginalStandardDeviationParameter.Hidden = true;
66      Parameters.Add(new ValueParameter<DoubleValue>(MeanParameterName, "m_tar | Mean value for the target deviation.", new DoubleValue(0.0)));
67      Parameters.Add(new ValueParameter<DoubleValue>(StandardDeviationParameterName, "s_tar | Standard deviation for the target data.", new DoubleValue(1.0)));
68    }
69
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new ShiftStandardDistributionTransformation(this, cloner);
72    }
73
74    public override IEnumerable<double> Apply(IEnumerable<double> data) {
75      if (OriginalStandardDeviation.IsAlmost(0.0)) {
76        return data;
77      }
78      var old_m = OriginalMean;
79      var old_s = OriginalStandardDeviation;
80      var m = Mean;
81      var s = StandardDeviation;
82      return data
83        .Select(d => (d - old_m) / old_s) // standardized
84        .Select(d => d * s + m);
85    }
86
87    public override bool Check(IEnumerable<double> data, out string errorMsg) {
88      ConfigureParameters(data);
89      errorMsg = "";
90      if (OriginalStandardDeviation.IsAlmost(0.0)) {
91        errorMsg = "Standard deviaton for the original data is 0.0, Transformation cannot be applied onto these values.";
92        return false;
93      }
94      return true;
95    }
96
97    public override void ConfigureParameters(IEnumerable<double> data) {
98      OriginalStandardDeviation = data.StandardDeviation();
99      OriginalMean = data.Average();
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.