Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SensitivityEvaluator/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/ShiftStandardDistributionTransformation.cs @ 14427

Last change on this file since 14427 was 11114, checked in by mkommend, 10 years ago

#2206: Merged data preprocessing into the trunk.

File size: 4.7 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  [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")]
11  public class ShiftStandardDistributionTransformation : Transformation<double> {
12    protected const string OriginalMeanParameterName = "Original Mean";
13    protected const string OriginalStandardDeviationParameterName = "Original Standard Deviation";
14    protected const string MeanParameterName = "Mean";
15    protected const string StandardDeviationParameterName = "Standard Deviation";
16
17    #region Parameters
18    public IValueParameter<DoubleValue> OriginalMeanParameter {
19      get { return (IValueParameter<DoubleValue>)Parameters[OriginalMeanParameterName]; }
20    }
21    public IValueParameter<DoubleValue> OriginalStandardDeviationParameter {
22      get { return (IValueParameter<DoubleValue>)Parameters[OriginalStandardDeviationParameterName]; }
23    }
24    public IValueParameter<DoubleValue> MeanParameter {
25      get { return (IValueParameter<DoubleValue>)Parameters[MeanParameterName]; }
26    }
27    public IValueParameter<DoubleValue> StandardDeviationParameter {
28      get { return (IValueParameter<DoubleValue>)Parameters[StandardDeviationParameterName]; }
29    }
30    #endregion
31
32    #region properties
33    public override string ShortName {
34      get { return "Std"; }
35    }
36    public double OriginalMean {
37      get { return OriginalMeanParameter.Value.Value; }
38      set { OriginalMeanParameter.Value.Value = value; }
39    }
40    public double OriginalStandardDeviation {
41      get { return OriginalStandardDeviationParameter.Value.Value; }
42      set { OriginalStandardDeviationParameter.Value.Value = value; }
43    }
44    public double Mean {
45      get { return MeanParameter.Value.Value; }
46    }
47    public double StandardDeviation {
48      get { return StandardDeviationParameter.Value.Value; }
49    }
50    #endregion
51
52    [StorableConstructor]
53    protected ShiftStandardDistributionTransformation(bool deserializing) : base(deserializing) { }
54    protected ShiftStandardDistributionTransformation(ShiftStandardDistributionTransformation original, Cloner cloner)
55      : base(original, cloner) {
56    }
57    public ShiftStandardDistributionTransformation(IEnumerable<string> allowedColumns)
58      : base(allowedColumns) {
59      Parameters.Add(new ValueParameter<DoubleValue>(OriginalMeanParameterName, "m_org | Mean value of the original data's deviation.", new DoubleValue()));
60      Parameters.Add(new ValueParameter<DoubleValue>(OriginalStandardDeviationParameterName, "s_org | Standard deviation of the original data.", new DoubleValue()));
61      OriginalMeanParameter.Hidden = true;
62      OriginalStandardDeviationParameter.Hidden = true;
63      Parameters.Add(new ValueParameter<DoubleValue>(MeanParameterName, "m_tar | Mean value for the target deviation.", new DoubleValue(0.0)));
64      Parameters.Add(new ValueParameter<DoubleValue>(StandardDeviationParameterName, "s_tar | Standard deviation for the target data.", new DoubleValue(1.0)));
65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new ShiftStandardDistributionTransformation(this, cloner);
69    }
70
71    // http://en.wikipedia.org/wiki/Standard_deviation
72    // http://www.statistics4u.info/fundstat_germ/ee_ztransform.html
73    // https://www.uni-due.de/~bm0061/vorl12.pdf p5
74    public override IEnumerable<double> Apply(IEnumerable<double> data) {
75      ConfigureParameters(data);
76      if (OriginalStandardDeviation == 0.0) {
77        foreach (var e in data) {
78          yield return e;
79        }
80        yield break;
81      }
82
83      foreach (var e in data) {
84        double unitNormalDistributedValue = (e - OriginalMean) / OriginalStandardDeviation;
85        yield return unitNormalDistributedValue * StandardDeviation + Mean;
86      }
87    }
88
89    public override bool Check(IEnumerable<double> data, out string errorMsg) {
90      ConfigureParameters(data);
91      errorMsg = "";
92      if (OriginalStandardDeviation == 0.0) {
93        errorMsg = "Standard deviaton for the original data is 0.0, Transformation cannot be applied onto these values.";
94        return false;
95      }
96      return true;
97    }
98
99    protected void ConfigureParameters(IEnumerable<double> data) {
100      OriginalStandardDeviation = data.StandardDeviation();
101      OriginalMean = data.Average();
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.