source: branches/3106_AnalyticContinuedFractionsRegression/HeuristicLab.Algorithms.DataAnalysis/3.4/ContinuedFractionRegression/Transformation.cs @ 17848

Last change on this file since 17848 was 17848, checked in by gkronber, 5 months ago

#3106 initial import of code (translated from HL script)

File size: 3.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Data;
7
8namespace HeuristicLab.Algorithms.DataAnalysis.ContinuedFractionRegression {
9  public class Transformation {
10
11    DoubleMatrix dataMatrix;
12
13    private double[] tempMinMax;
14    private double[] phiMinMax;
15    private double[] phipMinMax;
16
17    public Transformation(DoubleMatrix dataMatrix) {
18      this.dataMatrix = dataMatrix;
19      transform0();
20    }
21
22    private void transform0() { // care! change the other two transformations accordingly!
23      tempMinMax = findMinMax(0);
24      minMaxTransformation(this.dataMatrix, tempMinMax, 0);
25      log10Transformation(this.dataMatrix, 2);
26    }
27
28    public double[] transform0(double[] orig) {
29      double[] trans = new double[3];
30      trans[0] = minMaxTransformation(tempMinMax, orig[0]);
31      trans[1] = orig[1];
32      trans[2] = log10Transformation(orig[2]);
33
34      return trans;
35    }
36
37    public void useSameTransformation(DoubleMatrix dataMatrix) {
38      minMaxTransformation(dataMatrix, tempMinMax, 0);
39      log10Transformation(dataMatrix, 2);
40    }
41
42    //another transformation - not as good as i thougt
43    private void transform1() { // care! change retransformation accordingly!
44      tempMinMax = findMinMax(0);
45      minMaxTransformation(this.dataMatrix, this.tempMinMax, 0);
46      phiMinMax = findMinMax(1);
47      minMaxTransformation(this.dataMatrix, this.phiMinMax, 1);
48      log10Transformation(2); // no negativ values!
49      phipMinMax = findMinMax(2);
50      minMaxTransformation(this.dataMatrix, this.phipMinMax, 2); // Min and Max have to be different!
51    }
52
53    public double[] transform1(double[] orig) {
54      double[] trans = new double[3];
55      trans[0] = minMaxTransformation(tempMinMax, orig[0]);
56      trans[1] = minMaxTransformation(phiMinMax, orig[1]);
57      trans[2] = log10Transformation(orig[2]);
58      trans[2] = minMaxTransformation(phipMinMax, trans[2]);
59
60      return trans;
61    }
62    //
63
64    private double[] findMinMax(int column) {
65      // the first row has index 0
66      double[] MinMax = new double[2] { double.MaxValue, double.MinValue };
67      // find min and max in column
68      for (int i = 0; i < dataMatrix.Rows; i++) {
69        if (dataMatrix[i, column] < MinMax[0])
70          MinMax[0] = dataMatrix[i, column];
71        if (dataMatrix[i, column] > MinMax[1])
72          MinMax[1] = dataMatrix[i, column];
73      }
74      return MinMax;
75    }
76
77    private void minMaxTransformation(DoubleMatrix matrix, double[] minMax, int column) {
78      // transform all values in column
79      for (int i = 0; i < matrix.Rows; i++) {
80        matrix[i, column] = (matrix[i, column] - minMax[0]) / (minMax[1] - minMax[0]);
81      }
82    }
83
84    private double minMaxTransformation(double[] minMax, double x) {
85      return (x - minMax[0]) / (minMax[1] - minMax[0]);
86    }
87
88    private void log10Transformation(DoubleMatrix matrix, int column) {
89      // the first row has index 0
90      for (int i = 0; i < matrix.Rows; i++) {
91        matrix[i, column] = Math.Log10(matrix[i, column]);
92      }
93    }
94
95    private double log10Transformation(double x) {
96      return Math.Log10(x);
97    }
98  }
99
100}
Note: See TracBrowser for help on using the repository browser.