summaryrefslogtreecommitdiff
path: root/src/de/lmu/ifi/dbs/elki/datasource/filter/normalization/AttributeWiseCDFNormalization.java
blob: dd86cc5a6c9ec410c37b87cdfe7993f88b47d4a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package de.lmu.ifi.dbs.elki.datasource.filter.normalization;

/*
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures

 Copyright (C) 2013
 Ludwig-Maximilians-Universität München
 Lehr- und Forschungseinheit für Datenbanksysteme
 ELKI Development Team

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import de.lmu.ifi.dbs.elki.data.NumberVector;
import de.lmu.ifi.dbs.elki.data.type.SimpleTypeInformation;
import de.lmu.ifi.dbs.elki.data.type.TypeUtil;
import de.lmu.ifi.dbs.elki.data.type.VectorFieldTypeInformation;
import de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle;
import de.lmu.ifi.dbs.elki.datasource.filter.FilterUtil;
import de.lmu.ifi.dbs.elki.logging.Logging;
import de.lmu.ifi.dbs.elki.math.linearalgebra.LinearEquationSystem;
import de.lmu.ifi.dbs.elki.math.statistics.distribution.Distribution;
import de.lmu.ifi.dbs.elki.math.statistics.distribution.estimator.DistributionEstimator;
import de.lmu.ifi.dbs.elki.math.statistics.distribution.estimator.meta.BestFitEstimator;
import de.lmu.ifi.dbs.elki.math.statistics.tests.KolmogorovSmirnovTest;
import de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike.NumberArrayAdapter;
import de.lmu.ifi.dbs.elki.utilities.exceptions.ExceptionMessages;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.AbstractParameterizer;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.OptionID;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameterization.Parameterization;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.parameters.ObjectListParameter;

/**
 * Class to perform and undo a normalization on real vectors by estimating the
 * distribution of values along each dimension independently, then rescaling
 * objects to the cumulative density function (CDF) value at the original
 * coordinate.
 * 
 * This process is for example also discussed in section 3.4 of
 * <p>
 * Effects of Feature Normalization on Image Retrieval <br/>
 * S. Aksoy, R. M. Haralick
 * </p>
 * but they do not detail how to obtain an appropriate function `F`.
 * 
 * @author Erich Schubert
 * @param <V> vector type
 * 
 * @apiviz.uses NumberVector
 * @apiviz.uses DistributionEstimator
 */
// TODO: extract superclass AbstractAttributeWiseNormalization
public class AttributeWiseCDFNormalization<V extends NumberVector<?>> implements Normalization<V> {
  /**
   * Class logger.
   */
  private static final Logging LOG = Logging.getLogger(AttributeWiseCDFNormalization.class);

  /**
   * Stores the distribution estimators
   */
  private List<DistributionEstimator<?>> estimators;

  /**
   * Stores the estimated distributions
   */
  private List<Distribution> dists;

  /**
   * Number vector factory.
   */
  protected NumberVector.Factory<V, ?> factory;

  /**
   * Constructor.
   * 
   * @param estimators Distribution estimators
   */
  public AttributeWiseCDFNormalization(List<DistributionEstimator<?>> estimators) {
    super();
    this.estimators = estimators;
  }

  @Override
  public MultipleObjectsBundle filter(MultipleObjectsBundle objects) {
    if (objects.dataLength() == 0) {
      return objects;
    }
    for (int r = 0; r < objects.metaLength(); r++) {
      SimpleTypeInformation<?> type = (SimpleTypeInformation<?>) objects.meta(r);
      final List<?> column = (List<?>) objects.getColumn(r);
      if (!TypeUtil.NUMBER_VECTOR_FIELD.isAssignableFromType(type)) {
        continue;
      }
      @SuppressWarnings("unchecked")
      final List<V> castColumn = (List<V>) column;
      // Get the replacement type information
      @SuppressWarnings("unchecked")
      final VectorFieldTypeInformation<V> castType = (VectorFieldTypeInformation<V>) type;
      factory = FilterUtil.guessFactory(castType);

      // Scan to find the best
      final int dim = castType.getDimensionality();
      dists = new ArrayList<>(dim);
      // Scratch space for testing:
      double[] test = new double[castColumn.size()];

      // We iterate over dimensions, this kind of filter needs fast random
      // access.
      Adapter adapter = new Adapter();
      for (int d = 0; d < dim; d++) {
        adapter.dim = d;
        if (estimators.size() == 1) {
          dists.add(estimators.get(0).estimate(castColumn, adapter));
        } else {
          Distribution best = null;
          double bestq = Double.POSITIVE_INFINITY;
          trials: for (DistributionEstimator<?> est : estimators) {
            try {
              Distribution dist = est.estimate(castColumn, adapter);
              for (int i = 0; i < test.length; i++) {
                test[i] = dist.cdf(castColumn.get(i).doubleValue(d));
                if (Double.isNaN(test[i])) {
                  LOG.warning("Got NaN after fitting " + est.toString() + ": " + dist.toString());
                  continue trials;
                }
                if (Double.isInfinite(test[i])) {
                  LOG.warning("Got infinite value after fitting " + est.toString() + ": " + dist.toString());
                  continue trials;
                }
              }
              Arrays.sort(test);
              double q = KolmogorovSmirnovTest.simpleTest(test);
              if (LOG.isVeryVerbose()) {
                LOG.veryverbose("Estimator " + est.toString() + " (" + dist.toString() + ") has maximum deviation " + q + " for dimension " + d);
              }
              if (best == null || q < bestq) {
                best = dist;
                bestq = q;
              }
            } catch (ArithmeticException e) {
              if (LOG.isVeryVerbose()) {
                LOG.veryverbose("Fitting distribution " + est + " failed: " + e.getMessage());
              }
              continue;
            }
          }
          if (LOG.isVerbose()) {
            LOG.verbose("Best fit for dimension " + d + ": " + best.toString());
          }
          dists.add(best);
        }
      }

      // Normalization scan
      double[] buf = new double[dim];
      for (int i = 0; i < objects.dataLength(); i++) {
        final V obj = castColumn.get(i);
        for (int d = 0; d < dim; d++) {
          buf[d] = dists.get(d).cdf(obj.doubleValue(d));
        }
        castColumn.set(i, factory.newNumberVector(buf));
      }
    }
    return objects;
  }

  @Override
  public V restore(V featureVector) throws NonNumericFeaturesException {
    throw new UnsupportedOperationException(ExceptionMessages.UNSUPPORTED_NOT_YET);
  }

  @Override
  public LinearEquationSystem transform(LinearEquationSystem linearEquationSystem) {
    throw new UnsupportedOperationException(ExceptionMessages.UNSUPPORTED_NOT_YET);
  }

  @Override
  public String toString() {
    StringBuilder result = new StringBuilder();
    result.append("normalization class: ").append(getClass().getName());
    result.append('\n');
    result.append("normalization distributions: ");
    boolean first = true;
    for (DistributionEstimator<?> est : estimators) {
      if (!first) {
        result.append(',');
      }
      first = false;
      result.append(est.getClass().getSimpleName());
    }
    return result.toString();
  }

  /**
   * Array adapter class for vectors.
   * 
   * @author Erich Schubert
   *
   * @apiviz.exclude
   */
  private static class Adapter implements NumberArrayAdapter<Double, List<? extends NumberVector<?>>> {
    /**
     * Dimension to process.
     */
    int dim;

    @Override
    public int size(List<? extends NumberVector<?>> array) {
      return array.size();
    }

    @Override
    public Double get(List<? extends NumberVector<?>> array, int off) throws IndexOutOfBoundsException {
      return getDouble(array, off);
    }

    @Override
    public double getDouble(List<? extends NumberVector<?>> array, int off) throws IndexOutOfBoundsException {
      return array.get(off).doubleValue(dim);
    }

    @Override
    public float getFloat(List<? extends NumberVector<?>> array, int off) throws IndexOutOfBoundsException {
      return array.get(off).floatValue(dim);
    }

    @Override
    public int getInteger(List<? extends NumberVector<?>> array, int off) throws IndexOutOfBoundsException {
      return array.get(off).intValue(dim);
    }

    @Override
    public short getShort(List<? extends NumberVector<?>> array, int off) throws IndexOutOfBoundsException {
      return array.get(off).shortValue(dim);
    }

    @Override
    public long getLong(List<? extends NumberVector<?>> array, int off) throws IndexOutOfBoundsException {
      return array.get(off).longValue(dim);
    }

    @Override
    public byte getByte(List<? extends NumberVector<?>> array, int off) throws IndexOutOfBoundsException {
      return array.get(off).byteValue(dim);
    }
  }

  /**
   * Parameterization class.
   * 
   * @author Erich Schubert
   * 
   * @apiviz.exclude
   */
  public static class Parameterizer<V extends NumberVector<?>> extends AbstractParameterizer {
    /**
     * Parameter for distribution estimators.
     */
    public static final OptionID DISTRIBUTIONS_ID = new OptionID("normalize.distributions", "A list of the distribution estimators to try.");

    /**
     * Stores the distribution estimators
     */
    private List<DistributionEstimator<?>> estimators;

    @Override
    protected void makeOptions(Parameterization config) {
      super.makeOptions(config);
      ObjectListParameter<DistributionEstimator<?>> estP = new ObjectListParameter<>(DISTRIBUTIONS_ID, DistributionEstimator.class);
      List<Class<? extends DistributionEstimator<?>>> def = new ArrayList<>(1);
      def.add(BestFitEstimator.class);
      estP.setDefaultValue(def);
      if (config.grab(estP)) {
        estimators = estP.instantiateClasses(config);
      }
    }

    @Override
    protected AttributeWiseCDFNormalization<V> makeInstance() {
      return new AttributeWiseCDFNormalization<>(estimators);
    }
  }
}