source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/ShiftStandardDistributionTransformation.cs @ 14400

Last change on this file since 14400 was 14400, checked in by gkronber, 6 years ago

#2697: reverse merge of r14378, r14390, r14391, r14393, r14394, r14396

File size: 4.5 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Parameters;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8
9namespace HeuristicLab.Problems.DataAnalysis {
10  [StorableClass]
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    }
48    public double StandardDeviation {
49      get { return StandardDeviationParameter.Value.Value; }
50    }
51    #endregion
52
53    [StorableConstructor]
54    protected ShiftStandardDistributionTransformation(bool deserializing) : base(deserializing) { }
55    protected ShiftStandardDistributionTransformation(ShiftStandardDistributionTransformation original, Cloner cloner)
56      : base(original, cloner) {
57    }
58    public ShiftStandardDistributionTransformation(IEnumerable<string> allowedColumns)
59      : base(allowedColumns) {
60      Parameters.Add(new ValueParameter<DoubleValue>(OriginalMeanParameterName, "m_org | Mean value of the original data's deviation.", new DoubleValue()));
61      Parameters.Add(new ValueParameter<DoubleValue>(OriginalStandardDeviationParameterName, "s_org | Standard deviation of the original data.", new DoubleValue()));
62      OriginalMeanParameter.Hidden = true;
63      OriginalStandardDeviationParameter.Hidden = true;
64      Parameters.Add(new ValueParameter<DoubleValue>(MeanParameterName, "m_tar | Mean value for the target deviation.", new DoubleValue(0.0)));
65      Parameters.Add(new ValueParameter<DoubleValue>(StandardDeviationParameterName, "s_tar | Standard deviation for the target data.", new DoubleValue(1.0)));
66    }
67
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new ShiftStandardDistributionTransformation(this, cloner);
70    }
71
72    public override IEnumerable<double> Apply(IEnumerable<double> data) {
73      ConfigureParameters(data);
74      if (OriginalStandardDeviation.IsAlmost(0.0)) {
75        return data;
76      }
77      var old_m = OriginalMean;
78      var old_s = OriginalStandardDeviation;
79      var m = Mean;
80      var s = StandardDeviation;
81      return data
82        .Select(d => (d - old_m) / old_s) // standardized
83        .Select(d => d * s + m);
84    }
85
86    public override bool Check(IEnumerable<double> data, out string errorMsg) {
87      ConfigureParameters(data);
88      errorMsg = "";
89      if (OriginalStandardDeviation.IsAlmost(0.0)) {
90        errorMsg = "Standard deviaton for the original data is 0.0, Transformation cannot be applied onto these values.";
91        return false;
92      }
93      return true;
94    }
95
96    protected void ConfigureParameters(IEnumerable<double> data) {
97      OriginalStandardDeviation = data.StandardDeviation();
98      OriginalMean = data.Average();
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.