1 | using System.Collections.Generic;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Parameters;
|
---|
7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Problems.DataAnalysis.Transformations {
|
---|
10 | [Item("ShiftStandardDistributionTransformation.cs", "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 = "OriginalMean";
|
---|
13 | protected const string OriginalStandardDeviationParameterName = "OriginalStandardDeviation";
|
---|
14 | protected const string MeanParameterName = "Mean";
|
---|
15 | protected const string StandardDeviationParameterName = "StandardDeviation";
|
---|
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 double OriginalMean {
|
---|
34 | get { return OriginalMeanParameter.Value.Value; }
|
---|
35 | set { OriginalMeanParameter.Value.Value = value; }
|
---|
36 | }
|
---|
37 | public double OriginalStandardDeviation {
|
---|
38 | get { return OriginalStandardDeviationParameter.Value.Value; }
|
---|
39 | set { OriginalStandardDeviationParameter.Value.Value = value; }
|
---|
40 | }
|
---|
41 | public double Mean {
|
---|
42 | get { return MeanParameter.Value.Value; }
|
---|
43 | }
|
---|
44 | public double StandardDeviation {
|
---|
45 | get { return StandardDeviationParameter.Value.Value; }
|
---|
46 | }
|
---|
47 | #endregion
|
---|
48 |
|
---|
49 | [StorableConstructor]
|
---|
50 | protected ShiftStandardDistributionTransformation(bool deserializing) : base(deserializing) { }
|
---|
51 | protected ShiftStandardDistributionTransformation(Transformation<double> original, Cloner cloner)
|
---|
52 | : base(original, cloner) {
|
---|
53 | }
|
---|
54 | public ShiftStandardDistributionTransformation(IEnumerable<string> allowedColumns)
|
---|
55 | : base(allowedColumns) {
|
---|
56 | Parameters.Add(new ValueParameter<DoubleValue>(OriginalMeanParameterName, "m_org | Mean value of the original data's deviation.", new DoubleValue()));
|
---|
57 | Parameters.Add(new ValueParameter<DoubleValue>(OriginalStandardDeviationParameterName, "s_org | Standard deviation of the original data.", new DoubleValue()));
|
---|
58 | OriginalMeanParameter.Hidden = true;
|
---|
59 | OriginalStandardDeviationParameter.Hidden = true;
|
---|
60 | Parameters.Add(new ValueParameter<DoubleValue>(MeanParameterName, "m_tar | Mean value for the target deviation.", new DoubleValue(0.0)));
|
---|
61 | Parameters.Add(new ValueParameter<DoubleValue>(StandardDeviationParameterName, "s_tar | Standard deviation for the target data.", new DoubleValue(1.0)));
|
---|
62 | }
|
---|
63 |
|
---|
64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
65 | return new ShiftStandardDistributionTransformation(this, cloner);
|
---|
66 | }
|
---|
67 |
|
---|
68 | // http://en.wikipedia.org/wiki/Standard_deviation
|
---|
69 | // http://www.statistics4u.info/fundstat_germ/ee_ztransform.html
|
---|
70 | // https://www.uni-due.de/~bm0061/vorl12.pdf p5
|
---|
71 | public override IEnumerable<double> Apply(IEnumerable<double> data) {
|
---|
72 | ConfigureParameters(data);
|
---|
73 | if (OriginalStandardDeviation == 0.0) {
|
---|
74 | foreach (var e in data) {
|
---|
75 | yield return e;
|
---|
76 | }
|
---|
77 | yield break;
|
---|
78 | }
|
---|
79 |
|
---|
80 | foreach (var e in data) {
|
---|
81 | double unitNormalDistributedValue = (e - OriginalMean) / OriginalStandardDeviation;
|
---|
82 | yield return unitNormalDistributedValue * StandardDeviation + Mean;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override bool Check(IEnumerable<double> data, out string errorMsg) {
|
---|
87 | ConfigureParameters(data);
|
---|
88 | errorMsg = "";
|
---|
89 | if (OriginalStandardDeviation == 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 | }
|
---|