package de.lmu.ifi.dbs.elki.math.statistics.intrinsicdimensionality;

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

 Copyright (C) 2015
 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 de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike.NumberArrayAdapter;
import de.lmu.ifi.dbs.elki.utilities.documentation.Reference;
import de.lmu.ifi.dbs.elki.utilities.optionhandling.AbstractParameterizer;

/**
 * Methods of moments estimator, using the first moment (i.e. average).
 * 
 * This could be generalized to higher order moments, but the variance increases
 * with the order, and we need this to work well with small sample sizes.
 * 
 * Reference:
 * <p>
 * Amsaleg, L., Chelly, O., Furon, T., Girard, S., Houle, M. E., & Nett, M.<br />
 * Estimating Continuous Intrinsic Dimensionality.<br />
 * NII Technical Report NII-2014-001E.
 * </p>
 * 
 * @author Erich Schubert
 */
@Reference(authors = "Amsaleg, L., Chelly, O., Furon, T., Girard, S., Houle, M. E., & Nett, M.", //
title = "Estimating Continuous Intrinsic Dimensionality", //
booktitle = "NII Technical Report NII-2014-001E.", //
url = "http://www.nii.ac.jp/TechReports/14-001E.pdf")
public class MOMEstimator extends AbstractIntrinsicDimensionalityEstimator {
  /**
   * Static instance.
   */
  public static final MOMEstimator STATIC = new MOMEstimator();

  @Override
  public <A> double estimate(A data, NumberArrayAdapter<?, A> adapter, final int len) {
    if(len < 2) {
      return 0.;
    }
    double v1 = 0.;
    final int num = len - 1;
    for(int i = 0; i < num; i++) {
      v1 += adapter.getDouble(data, i);
    }
    v1 /= num * adapter.getDouble(data, num);
    return v1 / (1 - v1);
  }

  /**
   * Parameterization class.
   * 
   * @author Erich Schubert
   *
   * @apiviz.exclude
   */
  public static class Parameterizer extends AbstractParameterizer {
    @Override
    protected MOMEstimator makeInstance() {
      return STATIC;
    }
  }
}

@@ -1249,18 +1249,17 @@
 age).%0A *
- 
 %0A
+
  * This 
@@ -1407,17 +1407,16 @@
 izes.%0A *
- 
 %0A * Refe
@@ -1435,16 +1435,19 @@
 %3E%0A *
+ L.
  Amsaleg
 , L.
@@ -1446,73 +1446,101 @@
 aleg
-, L., Chelly, O., Furon, T., Girard, S., Houle, M. E., &
+ and O. Chelly and T. Furon and S. Girard and M. E. Houle and K.%0A * Kawarabayashi and M.
  Nett
-, M.
 %3Cbr 
@@ -1552,34 +1552,29 @@
  Estimating 
-Continuous
+Local
  Intrinsic D
@@ -1586,17 +1586,16 @@
 ionality
-.
 %3Cbr /%3E%0A 
@@ -1600,43 +1600,92 @@
 %0A * 
-NII Technical Report NII-2014-001E.
+Proc. SIGKDD International Conference on Knowledge Discovery and Data Mining%0A * 2015
 %0A * 
@@ -1691,17 +1691,16 @@
  %3C/p%3E%0A *
- 
 %0A * @aut
@@ -1748,80 +1748,108 @@
  = %22
+L. 
 Amsaleg
-, L., Chelly, O., Furon, T., Girard, S., Houle, M. E., &
+ and O. Chelly and T. Furon and S. Girard and M. E. Houle and K. Kawarabayashi and M.
  Nett
-, M.
 %22, /
@@ -1874,18 +1874,13 @@
 ing 
-Continuous
+Local
  Int
@@ -1923,100 +1923,143 @@
  = %22
-NII Technical Report NII-2014-001E.%22, //%0Aurl = %22http://www.nii.ac.jp/TechReports/14-001E.pdf
+Proc. SIGKDD International Conference on Knowledge Discovery and Data Mining 2015%22, //%0Aurl = %22http://dx.doi.org/10.1145/2783258.2783405
 %22)%0Ap
@@ -2361,17 +2361,91 @@
     
-return 0.
+throw new ArithmeticException(%22ID estimates require at least 2 non-zero distances%22)
 ;%0A  
@@ -2690,21 +2690,20 @@
  class.%0A
+
    *
- 
 %0A   * @a

Update reference to KDD publication.