Github user feynmanliang commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7278#discussion_r34304312
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/stat/test/ADTest.scala ---
    @@ -0,0 +1,264 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.mllib.stat.test
    +
    +import collection.immutable.ListMap
    +
    +import org.apache.commons.math3.distribution.{ExponentialDistribution, 
GumbelDistribution,
    +  LogisticDistribution, NormalDistribution, WeibullDistribution}
    +
    +import org.apache.spark.rdd.RDD
    +
    +/**
    + * The Anderson Darling (AD) test, similarly to the Kolmogorov Smirnov 
(KS) test, tests whether the
    + * data follow a given theoretical distribution. It should be used with 
continuous data and
    + * assumes that no ties occur (the presence of ties can affect the 
validity of the test).
    + * The AD test provides an alternative to the Kolmogorov-Smirnov test. 
Namely, it is better
    + * suited to identify departures from the theoretical distribution at the 
tails.
    + * It is worth noting that the the AD test's critical values depend on the
    + * distribution being tested against.
    + * The  AD statistic is defined as -n - s/n, where
    + * s = sum from i=1 to n of (2i + 1)(ln(z_i) + ln(1 - z_{n+1-i})
    + * where z_i is the CDF value of the ith observation in the sorted sample.
    + * For more information 
@see[[https://en.wikipedia.org/wiki/Anderson%E2%80%93Darling_test]]
    + */
    +private[stat] object ADTest {
    +
    +  object NullHypothesis extends Enumeration {
    +    type NullHypothesis = Value
    +    val oneSample = Value("Sample follows theoretical distribution.")
    +  }
    +
    +  /**
    +   * ADTheoreticalDist is a trait that every distribution used in an AD 
test must extend.
    +   * The rationale for this is that the AD test has distribution-dependent 
critical values, and by
    +   * requiring extension of this trait we guarantee that future additional 
distributions
    +   * make sure to add the appropriate critical values (CVs) (or at least 
acknowledge
    +   * that they should be added)
    +   */
    +  sealed trait ADTheoreticalDist {
    +    val params: Array[Double]  // parameters used to initialized the 
distribution
    +
    +    def cdf(x: Double): Double // calculate the cdf under the given 
distribution for value x
    +
    +    def getCVs(n: Double): Map[Double, Double] // return appropriate CVs, 
adjusted for sample size
    +  }
    +
    +  /**
    +   * Sourced from
    +   * 
http://civil.colorado.edu/~balajir/CVEN5454/lectures/Ang-n-Tang-Chap7-Goodness-of-fit-PDFs-
    +   * test.pdf
    +   * 
https://github.com/scipy/scipy/blob/v0.15.1/scipy/stats/morestats.py#L1017
    +   */
    +
    +  // Exponential distribution
    +  class ADExponential(val params: Array[Double]) extends ADTheoreticalDist 
{
    +    private val theoretical = new ExponentialDistribution(params(0))
    +
    +    private val rawCVs = ListMap(
    +      0.15 -> 0.922, 0.10 -> 1.078,
    +      0.05 -> 1.341, 0.025 -> 1.606, 0.01 -> 1.957
    +    )
    +
    +    def cdf(x: Double): Double = theoretical.cumulativeProbability(x)
    +
    +    def getCVs(n: Double): Map[Double, Double] = {
    +      rawCVs.map { case (sig, cv) => sig -> cv / (1 + 0.6 / n)}
    +    }
    +  }
    +
    +  // Normal Distribution
    +  class ADNormal(val params: Array[Double]) extends ADTheoreticalDist {
    +    private val theoretical = new NormalDistribution(params(0), params(1))
    +
    +    private val rawCVs = ListMap(
    +      0.15 -> 0.576, 0.10 -> 0.656,
    +      0.05 -> 0.787, 0.025 -> 0.918, 0.01 -> 1.092
    +    )
    +
    +    def cdf(x: Double): Double = theoretical.cumulativeProbability(x)
    +
    +    def getCVs(n: Double): Map[Double, Double] = {
    +      rawCVs.map { case (sig, cv) => sig -> cv / (1 + 4.0 / n - 25.0 / (n 
* n)) }
    +    }
    +  }
    +
    +  // Gumbel distribution
    +  class ADGumbel(val params: Array[Double]) extends ADTheoreticalDist {
    +    private val theoretical = new GumbelDistribution(params(0), params(1))
    +
    +    private val rawCVs = ListMap(
    +      0.25 -> 0.474, 0.10 -> 0.637,
    +      0.05 -> 0.757, 0.025 -> 0.877, 0.01 -> 1.038
    +    )
    +
    +    def cdf(x: Double): Double = theoretical.cumulativeProbability(x)
    +
    +    def getCVs(n: Double): Map[Double, Double] = {
    +      rawCVs.map { case (sig, cv) => sig -> cv / (1 + 0.2 / math.sqrt(n))}
    +    }
    +  }
    +
    +  // Logistic distribution
    +  class ADLogistic(val params: Array[Double]) extends ADTheoreticalDist {
    +    private val theoretical = new LogisticDistribution(params(0), 
params(1))
    +
    +    private val rawCVs = ListMap(
    +      0.25 -> 0.426, 0.10 -> 0.563, 0.05 -> 0.660,
    +      0.025 -> 0.769, 0.01 -> 0.906, 0.005 -> 1.010
    +    )
    +
    +    def cdf(x: Double): Double = theoretical.cumulativeProbability(x)
    +
    +    def getCVs(n: Double): Map[Double, Double] = {
    +      rawCVs.map { case (sig, cv) => sig -> cv / (1 + 0.25 / n)}
    +    }
    +  }
    +
    +  // Weibull distribution
    +  class ADWeibull(val params: Array[Double]) extends ADTheoreticalDist {
    +    private val theoretical = new WeibullDistribution(params(0), params(1))
    +
    +    private val rawCVs = ListMap(
    +      0.25 -> 0.474, 0.10 -> 0.637,
    +      0.05 -> 0.757, 0.025 -> 0.877, 0.01 -> 1.038
    +    )
    +
    +    def cdf(x: Double): Double = theoretical.cumulativeProbability(x)
    +
    +    def getCVs(n: Double): Map[Double, Double] = {
    +      rawCVs.map { case (sig, cv) => sig -> cv / (1 + 0.2 / math.sqrt(n))}
    +    }
    +  }
    +
    +  /**
    +   * Perform a one sample Anderson Darling test
    +   * @param data `RDD[Double]` data to test for a given distribution
    --- End diff --
    
    Types are unnecessary here, ditto for the other scaladocs


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to