[10815] | 1 | using System.Collections.Generic;
|
---|
[10784] | 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 {
|
---|
[10841] | 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> {
|
---|
[10815] | 12 | protected const string OriginalMeanParameterName = "OriginalMean";
|
---|
| 13 | protected const string OriginalStandardDeviationParameterName = "OriginalStandardDeviation";
|
---|
[10784] | 14 | protected const string MeanParameterName = "Mean";
|
---|
[10815] | 15 | protected const string StandardDeviationParameterName = "StandardDeviation";
|
---|
[10784] | 16 |
|
---|
| 17 | #region Parameters
|
---|
[10815] | 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 | }
|
---|
[10784] | 24 | public IValueParameter<DoubleValue> MeanParameter {
|
---|
| 25 | get { return (IValueParameter<DoubleValue>)Parameters[MeanParameterName]; }
|
---|
| 26 | }
|
---|
[10815] | 27 | public IValueParameter<DoubleValue> StandardDeviationParameter {
|
---|
| 28 | get { return (IValueParameter<DoubleValue>)Parameters[StandardDeviationParameterName]; }
|
---|
[10784] | 29 | }
|
---|
| 30 | #endregion
|
---|
| 31 |
|
---|
| 32 | #region properties
|
---|
[10815] | 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 | }
|
---|
[10784] | 41 | public double Mean {
|
---|
| 42 | get { return MeanParameter.Value.Value; }
|
---|
| 43 | }
|
---|
[10815] | 44 | public double StandardDeviation {
|
---|
| 45 | get { return StandardDeviationParameter.Value.Value; }
|
---|
[10784] | 46 | }
|
---|
| 47 | #endregion
|
---|
| 48 |
|
---|
| 49 | [StorableConstructor]
|
---|
[10841] | 50 | protected ShiftStandardDistributionTransformation(bool deserializing) : base(deserializing) { }
|
---|
| 51 | protected ShiftStandardDistributionTransformation(Transformation<double> original, Cloner cloner)
|
---|
[10784] | 52 | : base(original, cloner) {
|
---|
| 53 | }
|
---|
[10841] | 54 | public ShiftStandardDistributionTransformation(IEnumerable<string> allowedColumns)
|
---|
[10784] | 55 | : base(allowedColumns) {
|
---|
[10815] | 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)));
|
---|
[10784] | 62 | }
|
---|
| 63 |
|
---|
| 64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[10841] | 65 | return new ShiftStandardDistributionTransformation(this, cloner);
|
---|
[10784] | 66 | }
|
---|
| 67 |
|
---|
[10815] | 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
|
---|
[10784] | 71 | public override IEnumerable<double> Apply(IEnumerable<double> data) {
|
---|
| 72 | ConfigureParameters(data);
|
---|
[10815] | 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 | }
|
---|
[10784] | 84 | }
|
---|
| 85 |
|
---|
| 86 | public override bool Check(IEnumerable<double> data, out string errorMsg) {
|
---|
| 87 | ConfigureParameters(data);
|
---|
[10815] | 88 | errorMsg = "";
|
---|
| 89 | if (OriginalStandardDeviation == 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 | }
|
---|