Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Transformations/ShiftStandardDistributionTransformation.cs

Last change on this file was 17097, checked in by mkommend, 5 years ago

#2520: Merged 16565 - 16579 into stable.

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 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    }
48    public double StandardDeviation {
49      get { return StandardDeviationParameter.Value.Value; }
50    }
51    #endregion
52
53    [StorableConstructor]
54    protected ShiftStandardDistributionTransformation(StorableConstructorFlag _) : base(_) { }
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      if (OriginalStandardDeviation.IsAlmost(0.0)) {
74        return data;
75      }
76      var old_m = OriginalMean;
77      var old_s = OriginalStandardDeviation;
78      var m = Mean;
79      var s = StandardDeviation;
80      return data
81        .Select(d => (d - old_m) / old_s) // standardized
82        .Select(d => d * s + m);
83    }
84
85    public override bool Check(IEnumerable<double> data, out string errorMsg) {
86      ConfigureParameters(data);
87      errorMsg = "";
88      if (OriginalStandardDeviation.IsAlmost(0.0)) {
89        errorMsg = "Standard deviaton for the original data is 0.0, Transformation cannot be applied onto these values.";
90        return false;
91      }
92      return true;
93    }
94
95    public override void ConfigureParameters(IEnumerable<double> data) {
96      OriginalStandardDeviation = data.StandardDeviation();
97      OriginalMean = data.Average();
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.