Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13834 was 12667, checked in by gkronber, 10 years ago

#2415: merged r12612 from trunk to stable

File size: 4.5 KB
RevLine 
[10815]1using System.Collections.Generic;
[10784]2using System.Linq;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Parameters;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8
[11068]9namespace HeuristicLab.Problems.DataAnalysis {
[12667]10  [StorableClass]
[10909]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")]
[10841]12  public class ShiftStandardDistributionTransformation : Transformation<double> {
[10909]13    protected const string OriginalMeanParameterName = "Original Mean";
14    protected const string OriginalStandardDeviationParameterName = "Original Standard Deviation";
[10784]15    protected const string MeanParameterName = "Mean";
[10909]16    protected const string StandardDeviationParameterName = "Standard Deviation";
[10784]17
18    #region Parameters
[10815]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    }
[10784]25    public IValueParameter<DoubleValue> MeanParameter {
26      get { return (IValueParameter<DoubleValue>)Parameters[MeanParameterName]; }
27    }
[10815]28    public IValueParameter<DoubleValue> StandardDeviationParameter {
29      get { return (IValueParameter<DoubleValue>)Parameters[StandardDeviationParameterName]; }
[10784]30    }
31    #endregion
32
33    #region properties
[10932]34    public override string ShortName {
35      get { return "Std"; }
36    }
[10815]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    }
[10784]45    public double Mean {
46      get { return MeanParameter.Value.Value; }
47    }
[10815]48    public double StandardDeviation {
49      get { return StandardDeviationParameter.Value.Value; }
[10784]50    }
51    #endregion
52
53    [StorableConstructor]
[10841]54    protected ShiftStandardDistributionTransformation(bool deserializing) : base(deserializing) { }
[11114]55    protected ShiftStandardDistributionTransformation(ShiftStandardDistributionTransformation original, Cloner cloner)
[10784]56      : base(original, cloner) {
57    }
[10841]58    public ShiftStandardDistributionTransformation(IEnumerable<string> allowedColumns)
[10784]59      : base(allowedColumns) {
[10815]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)));
[10784]66    }
67
68    public override IDeepCloneable Clone(Cloner cloner) {
[10841]69      return new ShiftStandardDistributionTransformation(this, cloner);
[10784]70    }
71
72    public override IEnumerable<double> Apply(IEnumerable<double> data) {
73      ConfigureParameters(data);
[12667]74      if (OriginalStandardDeviation.IsAlmost(0.0)) {
75        return data;
[10815]76      }
[12667]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);
[10784]84    }
85
86    public override bool Check(IEnumerable<double> data, out string errorMsg) {
87      ConfigureParameters(data);
[10815]88      errorMsg = "";
[12667]89      if (OriginalStandardDeviation.IsAlmost(0.0)) {
[10816]90        errorMsg = "Standard deviaton for the original data is 0.0, Transformation cannot be applied onto these values.";
[10815]91        return false;
92      }
[10821]93      return true;
[10784]94    }
95
96    protected void ConfigureParameters(IEnumerable<double> data) {
[10815]97      OriginalStandardDeviation = data.StandardDeviation();
98      OriginalMean = data.Average();
[10784]99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.