[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;
|
---|
[16565] | 7 | using HEAL.Attic;
|
---|
[10784] | 8 |
|
---|
[11068] | 9 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[16565] | 10 | [StorableType("F0DC4951-64DF-4058-8E87-748D0FA1AA21")]
|
---|
[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]
|
---|
[16565] | 54 | protected ShiftStandardDistributionTransformation(StorableConstructorFlag _) : base(_) { }
|
---|
[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) {
|
---|
[12612] | 73 | if (OriginalStandardDeviation.IsAlmost(0.0)) {
|
---|
| 74 | return data;
|
---|
[10815] | 75 | }
|
---|
[12612] | 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);
|
---|
[10784] | 83 | }
|
---|
| 84 |
|
---|
| 85 | public override bool Check(IEnumerable<double> data, out string errorMsg) {
|
---|
| 86 | ConfigureParameters(data);
|
---|
[10815] | 87 | errorMsg = "";
|
---|
[12612] | 88 | if (OriginalStandardDeviation.IsAlmost(0.0)) {
|
---|
[10816] | 89 | errorMsg = "Standard deviaton for the original data is 0.0, Transformation cannot be applied onto these values.";
|
---|
[10815] | 90 | return false;
|
---|
| 91 | }
|
---|
[10821] | 92 | return true;
|
---|
[10784] | 93 | }
|
---|
| 94 |
|
---|
[14843] | 95 | public override void ConfigureParameters(IEnumerable<double> data) {
|
---|
[10815] | 96 | OriginalStandardDeviation = data.StandardDeviation();
|
---|
| 97 | OriginalMean = data.Average();
|
---|
[10784] | 98 | }
|
---|
| 99 | }
|
---|
| 100 | }
|
---|