2014-08-12 20:15:21 -04:00
---
layout: global
2016-07-15 16:38:23 -04:00
title: Basic Statistics - RDD-based API
displayTitle: Basic Statistics - RDD-based API
2014-08-12 20:15:21 -04:00
---
* Table of contents
{:toc}
`\[
\newcommand{\R}{\mathbb{R}}
2016-03-24 05:34:54 -04:00
\newcommand{\E}{\mathbb{E}}
2014-08-12 20:15:21 -04:00
\newcommand{\x}{\mathbf{x}}
\newcommand{\y}{\mathbf{y}}
\newcommand{\wv}{\mathbf{w}}
\newcommand{\av}{\mathbf{\alpha}}
\newcommand{\bv}{\mathbf{b}}
\newcommand{\N}{\mathbb{N}}
2016-03-24 05:34:54 -04:00
\newcommand{\id}{\mathbf{I}}
\newcommand{\ind}{\mathbf{1}}
\newcommand{\0}{\mathbf{0}}
\newcommand{\unit}{\mathbf{e}}
\newcommand{\one}{\mathbf{1}}
2014-08-12 20:15:21 -04:00
\newcommand{\zero}{\mathbf{0}}
\]`
2016-03-24 05:34:54 -04:00
## Summary statistics
2014-08-26 18:18:42 -04:00
2016-03-24 05:34:54 -04:00
We provide column summary statistics for `RDD[Vector]` through the function `colStats`
2014-08-26 18:18:42 -04:00
available in `Statistics` .
< div class = "codetabs" >
< div data-lang = "scala" markdown = "1" >
[`colStats()` ](api/scala/index.html#org.apache.spark.mllib.stat.Statistics$ ) returns an instance of
[`MultivariateStatisticalSummary` ](api/scala/index.html#org.apache.spark.mllib.stat.MultivariateStatisticalSummary ),
which contains the column-wise max, min, mean, variance, and number of nonzeros, as well as the
total count.
2015-10-07 10:00:19 -04:00
Refer to the [`MultivariateStatisticalSummary` Scala docs ](api/scala/index.html#org.apache.spark.mllib.stat.MultivariateStatisticalSummary ) for details on the API.
2016-03-24 05:34:54 -04:00
{% include_example scala/org/apache/spark/examples/mllib/SummaryStatisticsExample.scala %}
2014-08-26 18:18:42 -04:00
< / div >
< div data-lang = "java" markdown = "1" >
[`colStats()` ](api/java/org/apache/spark/mllib/stat/Statistics.html ) returns an instance of
[`MultivariateStatisticalSummary` ](api/java/org/apache/spark/mllib/stat/MultivariateStatisticalSummary.html ),
which contains the column-wise max, min, mean, variance, and number of nonzeros, as well as the
total count.
2015-10-07 10:00:19 -04:00
Refer to the [`MultivariateStatisticalSummary` Java docs ](api/java/org/apache/spark/mllib/stat/MultivariateStatisticalSummary.html ) for details on the API.
2016-03-24 05:34:54 -04:00
{% include_example java/org/apache/spark/examples/mllib/JavaSummaryStatisticsExample.java %}
2014-08-26 18:18:42 -04:00
< / div >
< div data-lang = "python" markdown = "1" >
2015-03-22 11:56:25 -04:00
[`colStats()` ](api/python/pyspark.mllib.html#pyspark.mllib.stat.Statistics.colStats ) returns an instance of
[`MultivariateStatisticalSummary` ](api/python/pyspark.mllib.html#pyspark.mllib.stat.MultivariateStatisticalSummary ),
2014-08-26 18:18:42 -04:00
which contains the column-wise max, min, mean, variance, and number of nonzeros, as well as the
total count.
2015-10-07 10:00:19 -04:00
Refer to the [`MultivariateStatisticalSummary` Python docs ](api/python/pyspark.mllib.html#pyspark.mllib.stat.MultivariateStatisticalSummary ) for more details on the API.
2016-03-24 05:34:54 -04:00
{% include_example python/mllib/summary_statistics_example.py %}
2014-08-26 18:18:42 -04:00
< / div >
< / div >
2014-08-27 04:19:48 -04:00
## Correlations
2014-08-12 20:15:21 -04:00
2015-12-10 15:50:46 -05:00
Calculating the correlation between two series of data is a common operation in Statistics. In `spark.mllib`
2016-03-24 05:34:54 -04:00
we provide the flexibility to calculate pairwise correlations among many series. The supported
2014-08-26 18:18:42 -04:00
correlation methods are currently Pearson's and Spearman's correlation.
2016-03-24 05:34:54 -04:00
2014-08-26 18:18:42 -04:00
< div class = "codetabs" >
< div data-lang = "scala" markdown = "1" >
2016-03-24 05:34:54 -04:00
[`Statistics` ](api/scala/index.html#org.apache.spark.mllib.stat.Statistics$ ) provides methods to
calculate correlations between series. Depending on the type of input, two `RDD[Double]` s or
2014-08-26 18:18:42 -04:00
an `RDD[Vector]` , the output will be a `Double` or the correlation `Matrix` respectively.
2016-06-11 07:55:38 -04:00
Refer to the [`Statistics` Scala docs ](api/scala/index.html#org.apache.spark.mllib.stat.Statistics$ ) for details on the API.
2015-10-07 10:00:19 -04:00
2016-03-24 05:34:54 -04:00
{% include_example scala/org/apache/spark/examples/mllib/CorrelationsExample.scala %}
2014-08-26 18:18:42 -04:00
< / div >
< div data-lang = "java" markdown = "1" >
2016-03-24 05:34:54 -04:00
[`Statistics` ](api/java/org/apache/spark/mllib/stat/Statistics.html ) provides methods to
calculate correlations between series. Depending on the type of input, two `JavaDoubleRDD` s or
2014-08-26 18:18:42 -04:00
a `JavaRDD<Vector>` , the output will be a `Double` or the correlation `Matrix` respectively.
2015-10-07 10:00:19 -04:00
Refer to the [`Statistics` Java docs ](api/java/org/apache/spark/mllib/stat/Statistics.html ) for details on the API.
2016-03-24 05:34:54 -04:00
{% include_example java/org/apache/spark/examples/mllib/JavaCorrelationsExample.java %}
2014-08-26 18:18:42 -04:00
< / div >
2014-08-12 20:15:21 -04:00
2014-08-26 18:18:42 -04:00
< div data-lang = "python" markdown = "1" >
2016-03-24 05:34:54 -04:00
[`Statistics` ](api/python/pyspark.mllib.html#pyspark.mllib.stat.Statistics ) provides methods to
calculate correlations between series. Depending on the type of input, two `RDD[Double]` s or
2014-08-26 18:18:42 -04:00
an `RDD[Vector]` , the output will be a `Double` or the correlation `Matrix` respectively.
2015-10-07 10:00:19 -04:00
Refer to the [`Statistics` Python docs ](api/python/pyspark.mllib.html#pyspark.mllib.stat.Statistics ) for more details on the API.
2016-03-24 05:34:54 -04:00
{% include_example python/mllib/correlations_example.py %}
2014-08-26 18:18:42 -04:00
< / div >
< / div >
## Stratified sampling
2015-12-10 15:50:46 -05:00
Unlike the other statistics functions, which reside in `spark.mllib` , stratified sampling methods,
2014-08-26 18:18:42 -04:00
`sampleByKey` and `sampleByKeyExact` , can be performed on RDD's of key-value pairs. For stratified
2016-03-24 05:34:54 -04:00
sampling, the keys can be thought of as a label and the value as a specific attribute. For example
the key can be man or woman, or document ids, and the respective values can be the list of ages
of the people in the population or the list of words in the documents. The `sampleByKey` method
will flip a coin to decide whether an observation will be sampled or not, therefore requires one
pass over the data, and provides an *expected* sample size. `sampleByKeyExact` requires significant
2014-08-26 18:18:42 -04:00
more resources than the per-stratum simple random sampling used in `sampleByKey` , but will provide
2016-03-24 05:34:54 -04:00
the exact sampling size with 99.99% confidence. `sampleByKeyExact` is currently not supported in
2014-08-26 18:18:42 -04:00
python.
2014-08-12 20:15:21 -04:00
< div class = "codetabs" >
< div data-lang = "scala" markdown = "1" >
2014-08-26 18:18:42 -04:00
[`sampleByKeyExact()` ](api/scala/index.html#org.apache.spark.rdd.PairRDDFunctions ) allows users to
2016-03-24 05:34:54 -04:00
sample exactly $\lceil f_k \cdot n_k \rceil \, \forall k \in K$ items, where $f_k$ is the desired
2014-08-26 18:18:42 -04:00
fraction for key $k$, $n_k$ is the number of key-value pairs for key $k$, and $K$ is the set of
2016-03-24 05:34:54 -04:00
keys. Sampling without replacement requires one additional pass over the RDD to guarantee sample
2014-08-26 18:18:42 -04:00
size, whereas sampling with replacement requires two additional passes.
2014-08-12 20:15:21 -04:00
2016-03-24 05:34:54 -04:00
{% include_example scala/org/apache/spark/examples/mllib/StratifiedSamplingExample.scala %}
2014-08-12 20:15:21 -04:00
< / div >
< div data-lang = "java" markdown = "1" >
2014-08-26 18:18:42 -04:00
[`sampleByKeyExact()` ](api/java/org/apache/spark/api/java/JavaPairRDD.html ) allows users to
2016-03-24 05:34:54 -04:00
sample exactly $\lceil f_k \cdot n_k \rceil \, \forall k \in K$ items, where $f_k$ is the desired
2014-08-26 18:18:42 -04:00
fraction for key $k$, $n_k$ is the number of key-value pairs for key $k$, and $K$ is the set of
2016-03-24 05:34:54 -04:00
keys. Sampling without replacement requires one additional pass over the RDD to guarantee sample
2014-08-26 18:18:42 -04:00
size, whereas sampling with replacement requires two additional passes.
2014-08-12 20:15:21 -04:00
2016-03-24 05:34:54 -04:00
{% include_example java/org/apache/spark/examples/mllib/JavaStratifiedSamplingExample.java %}
2014-08-26 18:18:42 -04:00
< / div >
< div data-lang = "python" markdown = "1" >
2015-03-22 11:56:25 -04:00
[`sampleByKey()` ](api/python/pyspark.html#pyspark.RDD.sampleByKey ) allows users to
2016-03-24 05:34:54 -04:00
sample approximately $\lceil f_k \cdot n_k \rceil \, \forall k \in K$ items, where $f_k$ is the
desired fraction for key $k$, $n_k$ is the number of key-value pairs for key $k$, and $K$ is the
2014-08-26 18:18:42 -04:00
set of keys.
*Note:* `sampleByKeyExact()` is currently not supported in Python.
2016-03-24 05:34:54 -04:00
{% include_example python/mllib/stratified_sampling_example.py %}
2014-08-12 20:15:21 -04:00
< / div >
2014-08-26 18:18:42 -04:00
< / div >
## Hypothesis testing
2016-03-24 05:34:54 -04:00
Hypothesis testing is a powerful tool in statistics to determine whether a result is statistically
significant, whether this result occurred by chance or not. `spark.mllib` currently supports Pearson's
[SPARK-8598] [MLLIB] Implementation of 1-sample, two-sided, Kolmogorov Smirnov Test for RDDs
This contribution is my original work and I license it to the project under it's open source license.
Author: jose.cambronero <jose.cambronero@cloudera.com>
Closes #6994 from josepablocam/master and squashes the following commits:
bbb30b1 [jose.cambronero] renamed KSTestResult to KolmogorovSmirnovTestResult, to stay consistent with method name
0d0c201 [jose.cambronero] kstTest -> kolmogorovSmirnovTest in statistics.md
1f56371 [jose.cambronero] changed ksTest in public API to kolmogorovSmirnovTest for clarity
a48ae7b [jose.cambronero] refactor code to account for serializable RealDistribution. Reuse testOneSample( _, cdf)
1bb44bd [jose.cambronero] style and doc changes. Factored out ks test into 2 separate tests
2ec2aa6 [jose.cambronero] initialize to stdnormal when no params passed (and log). Change unit tests to approximate equivalence rather than strict
a4bc0c7 [jose.cambronero] changed ksTest(data, distName) to ksTest(data, distName, params*) after api discussions. Changed tests and docs accordingly
7e66f57 [jose.cambronero] copied implementation note to public api docs, and added @see for links to wiki info
e760ebd [jose.cambronero] line length changes to fit style check
3288e42 [jose.cambronero] addressed style changes, correctness change to simpler approach, and fixed edge case for foldLeft in searchOneSampleCandidates when a partition is empty
9026895 [jose.cambronero] addressed style changes, correctness change to simpler approach, and fixed edge case for foldLeft in searchOneSampleCandidates when a partition is empty
1226b30 [jose.cambronero] reindent multi-line lambdas, prior intepretation of style guide was wrong on my part
9c0f1af [jose.cambronero] additional style changes incorporated and added documentation to mllib statistics docs
3f81ad2 [jose.cambronero] renamed ks1 sample test for clarity
992293b [jose.cambronero] Style changes as per comments and added implementation note explaining the distributed approach.
6a4784f [jose.cambronero] specified what distributions are available for the convenience method ksTest(data, name) (solely standard normal)
4b8ba61 [jose.cambronero] fixed off by 1/N in cases when post-constant adjustment ecdf is above cdf, but prior to adj it was below
0b5e8ec [jose.cambronero] changed KS one sample test to perform just 1 distributed pass (in addition to the sorting pass), operates on each partition separately. Implementation of Sandy Ryza's algorithm
16b5c4c [jose.cambronero] renamed dat to data and eliminated recalc of RDD size by sharing as argument between empirical and evalOneSampleP
c18dc66 [jose.cambronero] removed ksTestOpt from API and changed comments in HypothesisTestSuite accordingly
f6951b6 [jose.cambronero] changed style and some comments based on feedback from pull request
b9cff3a [jose.cambronero] made small changes to pass style check
ce8e9a1 [jose.cambronero] added kstest testing in HypothesisTestSuite
4da189b [jose.cambronero] added user facing ks test functions
c659ea1 [jose.cambronero] created KS test class
13dfe4d [jose.cambronero] created test result class for ks test
2015-07-10 23:55:45 -04:00
chi-squared ( $\chi^2$) tests for goodness of fit and independence. The input data types determine
2016-03-24 05:34:54 -04:00
whether the goodness of fit or the independence test is conducted. The goodness of fit test requires
2014-08-26 18:18:42 -04:00
an input type of `Vector` , whereas the independence test requires a `Matrix` as input.
2016-03-24 05:34:54 -04:00
`spark.mllib` also supports the input type `RDD[LabeledPoint]` to enable feature selection via chi-squared
2014-08-26 18:18:42 -04:00
independence tests.
< div class = "codetabs" >
< div data-lang = "scala" markdown = "1" >
2016-03-24 05:34:54 -04:00
[`Statistics` ](api/scala/index.html#org.apache.spark.mllib.stat.Statistics$ ) provides methods to
run Pearson's chi-squared tests. The following example demonstrates how to run and interpret
2014-08-26 18:18:42 -04:00
hypothesis tests.
2016-03-24 05:34:54 -04:00
{% include_example scala/org/apache/spark/examples/mllib/HypothesisTestingExample.scala %}
2014-08-12 20:15:21 -04:00
< / div >
2014-08-26 18:18:42 -04:00
< div data-lang = "java" markdown = "1" >
2016-03-24 05:34:54 -04:00
[`Statistics` ](api/java/org/apache/spark/mllib/stat/Statistics.html ) provides methods to
run Pearson's chi-squared tests. The following example demonstrates how to run and interpret
2014-08-26 18:18:42 -04:00
hypothesis tests.
2015-10-07 10:00:19 -04:00
Refer to the [`ChiSqTestResult` Java docs ](api/java/org/apache/spark/mllib/stat/test/ChiSqTestResult.html ) for details on the API.
2016-03-24 05:34:54 -04:00
{% include_example java/org/apache/spark/examples/mllib/JavaHypothesisTestingExample.java %}
2014-08-26 18:18:42 -04:00
< / div >
[SPARK-3964] [MLlib] [PySpark] add Hypothesis test Python API
```
pyspark.mllib.stat.StatisticschiSqTest(observed, expected=None)
:: Experimental ::
If `observed` is Vector, conduct Pearson's chi-squared goodness
of fit test of the observed data against the expected distribution,
or againt the uniform distribution (by default), with each category
having an expected frequency of `1 / len(observed)`.
(Note: `observed` cannot contain negative values)
If `observed` is matrix, conduct Pearson's independence test on the
input contingency matrix, which cannot contain negative entries or
columns or rows that sum up to 0.
If `observed` is an RDD of LabeledPoint, conduct Pearson's independence
test for every feature against the label across the input RDD.
For each feature, the (feature, label) pairs are converted into a
contingency matrix for which the chi-squared statistic is computed.
All label and feature values must be categorical.
:param observed: it could be a vector containing the observed categorical
counts/relative frequencies, or the contingency matrix
(containing either counts or relative frequencies),
or an RDD of LabeledPoint containing the labeled dataset
with categorical features. Real-valued features will be
treated as categorical for each distinct value.
:param expected: Vector containing the expected categorical counts/relative
frequencies. `expected` is rescaled if the `expected` sum
differs from the `observed` sum.
:return: ChiSquaredTest object containing the test statistic, degrees
of freedom, p-value, the method used, and the null hypothesis.
```
Author: Davies Liu <davies@databricks.com>
Closes #3091 from davies/his and squashes the following commits:
145d16c [Davies Liu] address comments
0ab0764 [Davies Liu] fix float
5097d54 [Davies Liu] add Hypothesis test Python API
2014-11-05 00:35:52 -05:00
< div data-lang = "python" markdown = "1" >
[`Statistics` ](api/python/index.html#pyspark.mllib.stat.Statistics$ ) provides methods to
run Pearson's chi-squared tests. The following example demonstrates how to run and interpret
hypothesis tests.
2015-10-07 10:00:19 -04:00
Refer to the [`Statistics` Python docs ](api/python/pyspark.mllib.html#pyspark.mllib.stat.Statistics ) for more details on the API.
2016-03-24 05:34:54 -04:00
{% include_example python/mllib/hypothesis_testing_example.py %}
[SPARK-3964] [MLlib] [PySpark] add Hypothesis test Python API
```
pyspark.mllib.stat.StatisticschiSqTest(observed, expected=None)
:: Experimental ::
If `observed` is Vector, conduct Pearson's chi-squared goodness
of fit test of the observed data against the expected distribution,
or againt the uniform distribution (by default), with each category
having an expected frequency of `1 / len(observed)`.
(Note: `observed` cannot contain negative values)
If `observed` is matrix, conduct Pearson's independence test on the
input contingency matrix, which cannot contain negative entries or
columns or rows that sum up to 0.
If `observed` is an RDD of LabeledPoint, conduct Pearson's independence
test for every feature against the label across the input RDD.
For each feature, the (feature, label) pairs are converted into a
contingency matrix for which the chi-squared statistic is computed.
All label and feature values must be categorical.
:param observed: it could be a vector containing the observed categorical
counts/relative frequencies, or the contingency matrix
(containing either counts or relative frequencies),
or an RDD of LabeledPoint containing the labeled dataset
with categorical features. Real-valued features will be
treated as categorical for each distinct value.
:param expected: Vector containing the expected categorical counts/relative
frequencies. `expected` is rescaled if the `expected` sum
differs from the `observed` sum.
:return: ChiSquaredTest object containing the test statistic, degrees
of freedom, p-value, the method used, and the null hypothesis.
```
Author: Davies Liu <davies@databricks.com>
Closes #3091 from davies/his and squashes the following commits:
145d16c [Davies Liu] address comments
0ab0764 [Davies Liu] fix float
5097d54 [Davies Liu] add Hypothesis test Python API
2014-11-05 00:35:52 -05:00
< / div >
2014-08-26 18:18:42 -04:00
< / div >
2014-08-27 04:19:48 -04:00
2015-12-10 15:50:46 -05:00
Additionally, `spark.mllib` provides a 1-sample, 2-sided implementation of the Kolmogorov-Smirnov (KS) test
[SPARK-8598] [MLLIB] Implementation of 1-sample, two-sided, Kolmogorov Smirnov Test for RDDs
This contribution is my original work and I license it to the project under it's open source license.
Author: jose.cambronero <jose.cambronero@cloudera.com>
Closes #6994 from josepablocam/master and squashes the following commits:
bbb30b1 [jose.cambronero] renamed KSTestResult to KolmogorovSmirnovTestResult, to stay consistent with method name
0d0c201 [jose.cambronero] kstTest -> kolmogorovSmirnovTest in statistics.md
1f56371 [jose.cambronero] changed ksTest in public API to kolmogorovSmirnovTest for clarity
a48ae7b [jose.cambronero] refactor code to account for serializable RealDistribution. Reuse testOneSample( _, cdf)
1bb44bd [jose.cambronero] style and doc changes. Factored out ks test into 2 separate tests
2ec2aa6 [jose.cambronero] initialize to stdnormal when no params passed (and log). Change unit tests to approximate equivalence rather than strict
a4bc0c7 [jose.cambronero] changed ksTest(data, distName) to ksTest(data, distName, params*) after api discussions. Changed tests and docs accordingly
7e66f57 [jose.cambronero] copied implementation note to public api docs, and added @see for links to wiki info
e760ebd [jose.cambronero] line length changes to fit style check
3288e42 [jose.cambronero] addressed style changes, correctness change to simpler approach, and fixed edge case for foldLeft in searchOneSampleCandidates when a partition is empty
9026895 [jose.cambronero] addressed style changes, correctness change to simpler approach, and fixed edge case for foldLeft in searchOneSampleCandidates when a partition is empty
1226b30 [jose.cambronero] reindent multi-line lambdas, prior intepretation of style guide was wrong on my part
9c0f1af [jose.cambronero] additional style changes incorporated and added documentation to mllib statistics docs
3f81ad2 [jose.cambronero] renamed ks1 sample test for clarity
992293b [jose.cambronero] Style changes as per comments and added implementation note explaining the distributed approach.
6a4784f [jose.cambronero] specified what distributions are available for the convenience method ksTest(data, name) (solely standard normal)
4b8ba61 [jose.cambronero] fixed off by 1/N in cases when post-constant adjustment ecdf is above cdf, but prior to adj it was below
0b5e8ec [jose.cambronero] changed KS one sample test to perform just 1 distributed pass (in addition to the sorting pass), operates on each partition separately. Implementation of Sandy Ryza's algorithm
16b5c4c [jose.cambronero] renamed dat to data and eliminated recalc of RDD size by sharing as argument between empirical and evalOneSampleP
c18dc66 [jose.cambronero] removed ksTestOpt from API and changed comments in HypothesisTestSuite accordingly
f6951b6 [jose.cambronero] changed style and some comments based on feedback from pull request
b9cff3a [jose.cambronero] made small changes to pass style check
ce8e9a1 [jose.cambronero] added kstest testing in HypothesisTestSuite
4da189b [jose.cambronero] added user facing ks test functions
c659ea1 [jose.cambronero] created KS test class
13dfe4d [jose.cambronero] created test result class for ks test
2015-07-10 23:55:45 -04:00
for equality of probability distributions. By providing the name of a theoretical distribution
2016-03-24 05:34:54 -04:00
(currently solely supported for the normal distribution) and its parameters, or a function to
[SPARK-8598] [MLLIB] Implementation of 1-sample, two-sided, Kolmogorov Smirnov Test for RDDs
This contribution is my original work and I license it to the project under it's open source license.
Author: jose.cambronero <jose.cambronero@cloudera.com>
Closes #6994 from josepablocam/master and squashes the following commits:
bbb30b1 [jose.cambronero] renamed KSTestResult to KolmogorovSmirnovTestResult, to stay consistent with method name
0d0c201 [jose.cambronero] kstTest -> kolmogorovSmirnovTest in statistics.md
1f56371 [jose.cambronero] changed ksTest in public API to kolmogorovSmirnovTest for clarity
a48ae7b [jose.cambronero] refactor code to account for serializable RealDistribution. Reuse testOneSample( _, cdf)
1bb44bd [jose.cambronero] style and doc changes. Factored out ks test into 2 separate tests
2ec2aa6 [jose.cambronero] initialize to stdnormal when no params passed (and log). Change unit tests to approximate equivalence rather than strict
a4bc0c7 [jose.cambronero] changed ksTest(data, distName) to ksTest(data, distName, params*) after api discussions. Changed tests and docs accordingly
7e66f57 [jose.cambronero] copied implementation note to public api docs, and added @see for links to wiki info
e760ebd [jose.cambronero] line length changes to fit style check
3288e42 [jose.cambronero] addressed style changes, correctness change to simpler approach, and fixed edge case for foldLeft in searchOneSampleCandidates when a partition is empty
9026895 [jose.cambronero] addressed style changes, correctness change to simpler approach, and fixed edge case for foldLeft in searchOneSampleCandidates when a partition is empty
1226b30 [jose.cambronero] reindent multi-line lambdas, prior intepretation of style guide was wrong on my part
9c0f1af [jose.cambronero] additional style changes incorporated and added documentation to mllib statistics docs
3f81ad2 [jose.cambronero] renamed ks1 sample test for clarity
992293b [jose.cambronero] Style changes as per comments and added implementation note explaining the distributed approach.
6a4784f [jose.cambronero] specified what distributions are available for the convenience method ksTest(data, name) (solely standard normal)
4b8ba61 [jose.cambronero] fixed off by 1/N in cases when post-constant adjustment ecdf is above cdf, but prior to adj it was below
0b5e8ec [jose.cambronero] changed KS one sample test to perform just 1 distributed pass (in addition to the sorting pass), operates on each partition separately. Implementation of Sandy Ryza's algorithm
16b5c4c [jose.cambronero] renamed dat to data and eliminated recalc of RDD size by sharing as argument between empirical and evalOneSampleP
c18dc66 [jose.cambronero] removed ksTestOpt from API and changed comments in HypothesisTestSuite accordingly
f6951b6 [jose.cambronero] changed style and some comments based on feedback from pull request
b9cff3a [jose.cambronero] made small changes to pass style check
ce8e9a1 [jose.cambronero] added kstest testing in HypothesisTestSuite
4da189b [jose.cambronero] added user facing ks test functions
c659ea1 [jose.cambronero] created KS test class
13dfe4d [jose.cambronero] created test result class for ks test
2015-07-10 23:55:45 -04:00
calculate the cumulative distribution according to a given theoretical distribution, the user can
test the null hypothesis that their sample is drawn from that distribution. In the case that the
user tests against the normal distribution (`distName="norm"`), but does not provide distribution
2016-03-24 05:34:54 -04:00
parameters, the test initializes to the standard normal distribution and logs an appropriate
[SPARK-8598] [MLLIB] Implementation of 1-sample, two-sided, Kolmogorov Smirnov Test for RDDs
This contribution is my original work and I license it to the project under it's open source license.
Author: jose.cambronero <jose.cambronero@cloudera.com>
Closes #6994 from josepablocam/master and squashes the following commits:
bbb30b1 [jose.cambronero] renamed KSTestResult to KolmogorovSmirnovTestResult, to stay consistent with method name
0d0c201 [jose.cambronero] kstTest -> kolmogorovSmirnovTest in statistics.md
1f56371 [jose.cambronero] changed ksTest in public API to kolmogorovSmirnovTest for clarity
a48ae7b [jose.cambronero] refactor code to account for serializable RealDistribution. Reuse testOneSample( _, cdf)
1bb44bd [jose.cambronero] style and doc changes. Factored out ks test into 2 separate tests
2ec2aa6 [jose.cambronero] initialize to stdnormal when no params passed (and log). Change unit tests to approximate equivalence rather than strict
a4bc0c7 [jose.cambronero] changed ksTest(data, distName) to ksTest(data, distName, params*) after api discussions. Changed tests and docs accordingly
7e66f57 [jose.cambronero] copied implementation note to public api docs, and added @see for links to wiki info
e760ebd [jose.cambronero] line length changes to fit style check
3288e42 [jose.cambronero] addressed style changes, correctness change to simpler approach, and fixed edge case for foldLeft in searchOneSampleCandidates when a partition is empty
9026895 [jose.cambronero] addressed style changes, correctness change to simpler approach, and fixed edge case for foldLeft in searchOneSampleCandidates when a partition is empty
1226b30 [jose.cambronero] reindent multi-line lambdas, prior intepretation of style guide was wrong on my part
9c0f1af [jose.cambronero] additional style changes incorporated and added documentation to mllib statistics docs
3f81ad2 [jose.cambronero] renamed ks1 sample test for clarity
992293b [jose.cambronero] Style changes as per comments and added implementation note explaining the distributed approach.
6a4784f [jose.cambronero] specified what distributions are available for the convenience method ksTest(data, name) (solely standard normal)
4b8ba61 [jose.cambronero] fixed off by 1/N in cases when post-constant adjustment ecdf is above cdf, but prior to adj it was below
0b5e8ec [jose.cambronero] changed KS one sample test to perform just 1 distributed pass (in addition to the sorting pass), operates on each partition separately. Implementation of Sandy Ryza's algorithm
16b5c4c [jose.cambronero] renamed dat to data and eliminated recalc of RDD size by sharing as argument between empirical and evalOneSampleP
c18dc66 [jose.cambronero] removed ksTestOpt from API and changed comments in HypothesisTestSuite accordingly
f6951b6 [jose.cambronero] changed style and some comments based on feedback from pull request
b9cff3a [jose.cambronero] made small changes to pass style check
ce8e9a1 [jose.cambronero] added kstest testing in HypothesisTestSuite
4da189b [jose.cambronero] added user facing ks test functions
c659ea1 [jose.cambronero] created KS test class
13dfe4d [jose.cambronero] created test result class for ks test
2015-07-10 23:55:45 -04:00
message.
< div class = "codetabs" >
< div data-lang = "scala" markdown = "1" >
[`Statistics` ](api/scala/index.html#org.apache.spark.mllib.stat.Statistics$ ) provides methods to
run a 1-sample, 2-sided Kolmogorov-Smirnov test. The following example demonstrates how to run
and interpret the hypothesis tests.
2016-06-11 07:55:38 -04:00
Refer to the [`Statistics` Scala docs ](api/scala/index.html#org.apache.spark.mllib.stat.Statistics$ ) for details on the API.
2015-10-07 10:00:19 -04:00
2016-03-24 05:34:54 -04:00
{% include_example scala/org/apache/spark/examples/mllib/HypothesisTestingKolmogorovSmirnovTestExample.scala %}
[SPARK-8598] [MLLIB] Implementation of 1-sample, two-sided, Kolmogorov Smirnov Test for RDDs
This contribution is my original work and I license it to the project under it's open source license.
Author: jose.cambronero <jose.cambronero@cloudera.com>
Closes #6994 from josepablocam/master and squashes the following commits:
bbb30b1 [jose.cambronero] renamed KSTestResult to KolmogorovSmirnovTestResult, to stay consistent with method name
0d0c201 [jose.cambronero] kstTest -> kolmogorovSmirnovTest in statistics.md
1f56371 [jose.cambronero] changed ksTest in public API to kolmogorovSmirnovTest for clarity
a48ae7b [jose.cambronero] refactor code to account for serializable RealDistribution. Reuse testOneSample( _, cdf)
1bb44bd [jose.cambronero] style and doc changes. Factored out ks test into 2 separate tests
2ec2aa6 [jose.cambronero] initialize to stdnormal when no params passed (and log). Change unit tests to approximate equivalence rather than strict
a4bc0c7 [jose.cambronero] changed ksTest(data, distName) to ksTest(data, distName, params*) after api discussions. Changed tests and docs accordingly
7e66f57 [jose.cambronero] copied implementation note to public api docs, and added @see for links to wiki info
e760ebd [jose.cambronero] line length changes to fit style check
3288e42 [jose.cambronero] addressed style changes, correctness change to simpler approach, and fixed edge case for foldLeft in searchOneSampleCandidates when a partition is empty
9026895 [jose.cambronero] addressed style changes, correctness change to simpler approach, and fixed edge case for foldLeft in searchOneSampleCandidates when a partition is empty
1226b30 [jose.cambronero] reindent multi-line lambdas, prior intepretation of style guide was wrong on my part
9c0f1af [jose.cambronero] additional style changes incorporated and added documentation to mllib statistics docs
3f81ad2 [jose.cambronero] renamed ks1 sample test for clarity
992293b [jose.cambronero] Style changes as per comments and added implementation note explaining the distributed approach.
6a4784f [jose.cambronero] specified what distributions are available for the convenience method ksTest(data, name) (solely standard normal)
4b8ba61 [jose.cambronero] fixed off by 1/N in cases when post-constant adjustment ecdf is above cdf, but prior to adj it was below
0b5e8ec [jose.cambronero] changed KS one sample test to perform just 1 distributed pass (in addition to the sorting pass), operates on each partition separately. Implementation of Sandy Ryza's algorithm
16b5c4c [jose.cambronero] renamed dat to data and eliminated recalc of RDD size by sharing as argument between empirical and evalOneSampleP
c18dc66 [jose.cambronero] removed ksTestOpt from API and changed comments in HypothesisTestSuite accordingly
f6951b6 [jose.cambronero] changed style and some comments based on feedback from pull request
b9cff3a [jose.cambronero] made small changes to pass style check
ce8e9a1 [jose.cambronero] added kstest testing in HypothesisTestSuite
4da189b [jose.cambronero] added user facing ks test functions
c659ea1 [jose.cambronero] created KS test class
13dfe4d [jose.cambronero] created test result class for ks test
2015-07-10 23:55:45 -04:00
< / div >
2015-08-17 22:09:45 -04:00
< div data-lang = "java" markdown = "1" >
[`Statistics` ](api/java/org/apache/spark/mllib/stat/Statistics.html ) provides methods to
run a 1-sample, 2-sided Kolmogorov-Smirnov test. The following example demonstrates how to run
and interpret the hypothesis tests.
2015-10-07 10:00:19 -04:00
Refer to the [`Statistics` Java docs ](api/java/org/apache/spark/mllib/stat/Statistics.html ) for details on the API.
2016-03-24 05:34:54 -04:00
{% include_example java/org/apache/spark/examples/mllib/JavaHypothesisTestingKolmogorovSmirnovTestExample.java %}
2015-08-17 22:09:45 -04:00
< / div >
< div data-lang = "python" markdown = "1" >
[`Statistics` ](api/python/pyspark.mllib.html#pyspark.mllib.stat.Statistics ) provides methods to
run a 1-sample, 2-sided Kolmogorov-Smirnov test. The following example demonstrates how to run
and interpret the hypothesis tests.
2015-10-07 10:00:19 -04:00
Refer to the [`Statistics` Python docs ](api/python/pyspark.mllib.html#pyspark.mllib.stat.Statistics ) for more details on the API.
2016-03-24 05:34:54 -04:00
{% include_example python/mllib/hypothesis_testing_kolmogorov_smirnov_test_example.py %}
2015-08-17 22:09:45 -04:00
< / div >
[SPARK-8598] [MLLIB] Implementation of 1-sample, two-sided, Kolmogorov Smirnov Test for RDDs
This contribution is my original work and I license it to the project under it's open source license.
Author: jose.cambronero <jose.cambronero@cloudera.com>
Closes #6994 from josepablocam/master and squashes the following commits:
bbb30b1 [jose.cambronero] renamed KSTestResult to KolmogorovSmirnovTestResult, to stay consistent with method name
0d0c201 [jose.cambronero] kstTest -> kolmogorovSmirnovTest in statistics.md
1f56371 [jose.cambronero] changed ksTest in public API to kolmogorovSmirnovTest for clarity
a48ae7b [jose.cambronero] refactor code to account for serializable RealDistribution. Reuse testOneSample( _, cdf)
1bb44bd [jose.cambronero] style and doc changes. Factored out ks test into 2 separate tests
2ec2aa6 [jose.cambronero] initialize to stdnormal when no params passed (and log). Change unit tests to approximate equivalence rather than strict
a4bc0c7 [jose.cambronero] changed ksTest(data, distName) to ksTest(data, distName, params*) after api discussions. Changed tests and docs accordingly
7e66f57 [jose.cambronero] copied implementation note to public api docs, and added @see for links to wiki info
e760ebd [jose.cambronero] line length changes to fit style check
3288e42 [jose.cambronero] addressed style changes, correctness change to simpler approach, and fixed edge case for foldLeft in searchOneSampleCandidates when a partition is empty
9026895 [jose.cambronero] addressed style changes, correctness change to simpler approach, and fixed edge case for foldLeft in searchOneSampleCandidates when a partition is empty
1226b30 [jose.cambronero] reindent multi-line lambdas, prior intepretation of style guide was wrong on my part
9c0f1af [jose.cambronero] additional style changes incorporated and added documentation to mllib statistics docs
3f81ad2 [jose.cambronero] renamed ks1 sample test for clarity
992293b [jose.cambronero] Style changes as per comments and added implementation note explaining the distributed approach.
6a4784f [jose.cambronero] specified what distributions are available for the convenience method ksTest(data, name) (solely standard normal)
4b8ba61 [jose.cambronero] fixed off by 1/N in cases when post-constant adjustment ecdf is above cdf, but prior to adj it was below
0b5e8ec [jose.cambronero] changed KS one sample test to perform just 1 distributed pass (in addition to the sorting pass), operates on each partition separately. Implementation of Sandy Ryza's algorithm
16b5c4c [jose.cambronero] renamed dat to data and eliminated recalc of RDD size by sharing as argument between empirical and evalOneSampleP
c18dc66 [jose.cambronero] removed ksTestOpt from API and changed comments in HypothesisTestSuite accordingly
f6951b6 [jose.cambronero] changed style and some comments based on feedback from pull request
b9cff3a [jose.cambronero] made small changes to pass style check
ce8e9a1 [jose.cambronero] added kstest testing in HypothesisTestSuite
4da189b [jose.cambronero] added user facing ks test functions
c659ea1 [jose.cambronero] created KS test class
13dfe4d [jose.cambronero] created test result class for ks test
2015-07-10 23:55:45 -04:00
< / div >
2015-11-30 18:38:44 -05:00
### Streaming Significance Testing
2015-12-10 15:50:46 -05:00
`spark.mllib` provides online implementations of some tests to support use cases
2015-11-30 18:38:44 -05:00
like A/B testing. These tests may be performed on a Spark Streaming
`DStream[(Boolean,Double)]` where the first element of each tuple
indicates control group (`false`) or treatment group (`true`) and the
second element is the value of an observation.
Streaming significance testing supports the following parameters:
* `peacePeriod` - The number of initial data points from the stream to
ignore, used to mitigate novelty effects.
* `windowSize` - The number of past batches to perform hypothesis
testing over. Setting to `0` will perform cumulative processing using
all prior batches.
< div class = "codetabs" >
< div data-lang = "scala" markdown = "1" >
[`StreamingTest` ](api/scala/index.html#org.apache.spark.mllib.stat.test.StreamingTest )
provides streaming hypothesis testing.
{% include_example scala/org/apache/spark/examples/mllib/StreamingTestExample.scala %}
< / div >
2016-03-17 05:09:02 -04:00
< div data-lang = "java" markdown = "1" >
[`StreamingTest` ](api/java/index.html#org.apache.spark.mllib.stat.test.StreamingTest )
provides streaming hypothesis testing.
{% include_example java/org/apache/spark/examples/mllib/JavaStreamingTestExample.java %}
< / div >
2015-11-30 18:38:44 -05:00
< / div >
[SPARK-8598] [MLLIB] Implementation of 1-sample, two-sided, Kolmogorov Smirnov Test for RDDs
This contribution is my original work and I license it to the project under it's open source license.
Author: jose.cambronero <jose.cambronero@cloudera.com>
Closes #6994 from josepablocam/master and squashes the following commits:
bbb30b1 [jose.cambronero] renamed KSTestResult to KolmogorovSmirnovTestResult, to stay consistent with method name
0d0c201 [jose.cambronero] kstTest -> kolmogorovSmirnovTest in statistics.md
1f56371 [jose.cambronero] changed ksTest in public API to kolmogorovSmirnovTest for clarity
a48ae7b [jose.cambronero] refactor code to account for serializable RealDistribution. Reuse testOneSample( _, cdf)
1bb44bd [jose.cambronero] style and doc changes. Factored out ks test into 2 separate tests
2ec2aa6 [jose.cambronero] initialize to stdnormal when no params passed (and log). Change unit tests to approximate equivalence rather than strict
a4bc0c7 [jose.cambronero] changed ksTest(data, distName) to ksTest(data, distName, params*) after api discussions. Changed tests and docs accordingly
7e66f57 [jose.cambronero] copied implementation note to public api docs, and added @see for links to wiki info
e760ebd [jose.cambronero] line length changes to fit style check
3288e42 [jose.cambronero] addressed style changes, correctness change to simpler approach, and fixed edge case for foldLeft in searchOneSampleCandidates when a partition is empty
9026895 [jose.cambronero] addressed style changes, correctness change to simpler approach, and fixed edge case for foldLeft in searchOneSampleCandidates when a partition is empty
1226b30 [jose.cambronero] reindent multi-line lambdas, prior intepretation of style guide was wrong on my part
9c0f1af [jose.cambronero] additional style changes incorporated and added documentation to mllib statistics docs
3f81ad2 [jose.cambronero] renamed ks1 sample test for clarity
992293b [jose.cambronero] Style changes as per comments and added implementation note explaining the distributed approach.
6a4784f [jose.cambronero] specified what distributions are available for the convenience method ksTest(data, name) (solely standard normal)
4b8ba61 [jose.cambronero] fixed off by 1/N in cases when post-constant adjustment ecdf is above cdf, but prior to adj it was below
0b5e8ec [jose.cambronero] changed KS one sample test to perform just 1 distributed pass (in addition to the sorting pass), operates on each partition separately. Implementation of Sandy Ryza's algorithm
16b5c4c [jose.cambronero] renamed dat to data and eliminated recalc of RDD size by sharing as argument between empirical and evalOneSampleP
c18dc66 [jose.cambronero] removed ksTestOpt from API and changed comments in HypothesisTestSuite accordingly
f6951b6 [jose.cambronero] changed style and some comments based on feedback from pull request
b9cff3a [jose.cambronero] made small changes to pass style check
ce8e9a1 [jose.cambronero] added kstest testing in HypothesisTestSuite
4da189b [jose.cambronero] added user facing ks test functions
c659ea1 [jose.cambronero] created KS test class
13dfe4d [jose.cambronero] created test result class for ks test
2015-07-10 23:55:45 -04:00
2014-08-27 04:19:48 -04:00
## Random data generation
Random data generation is useful for randomized algorithms, prototyping, and performance testing.
2015-12-10 15:50:46 -05:00
`spark.mllib` supports generating random RDDs with i.i.d. values drawn from a given distribution:
2014-08-27 04:19:48 -04:00
uniform, standard normal, or Poisson.
< div class = "codetabs" >
< div data-lang = "scala" markdown = "1" >
2016-06-11 07:55:38 -04:00
[`RandomRDDs` ](api/scala/index.html#org.apache.spark.mllib.random.RandomRDDs$ ) provides factory
2014-08-27 04:19:48 -04:00
methods to generate random double RDDs or vector RDDs.
The following example generates a random double RDD, whose values follows the standard normal
distribution `N(0, 1)` , and then map it to `N(1, 4)` .
2016-06-11 07:55:38 -04:00
Refer to the [`RandomRDDs` Scala docs ](api/scala/index.html#org.apache.spark.mllib.random.RandomRDDs$ ) for details on the API.
2015-10-07 10:00:19 -04:00
2014-08-27 04:19:48 -04:00
{% highlight scala %}
import org.apache.spark.SparkContext
import org.apache.spark.mllib.random.RandomRDDs._
val sc: SparkContext = ...
// Generate a random double RDD that contains 1 million i.i.d. values drawn from the
// standard normal distribution `N(0, 1)` , evenly distributed in 10 partitions.
val u = normalRDD(sc, 1000000L, 10)
// Apply a transform to get a random double RDD following `N(1, 4)` .
val v = u.map(x => 1.0 + 2.0 * x)
{% endhighlight %}
< / div >
< div data-lang = "java" markdown = "1" >
[`RandomRDDs` ](api/java/index.html#org.apache.spark.mllib.random.RandomRDDs ) provides factory
methods to generate random double RDDs or vector RDDs.
The following example generates a random double RDD, whose values follows the standard normal
distribution `N(0, 1)` , and then map it to `N(1, 4)` .
2015-10-07 10:00:19 -04:00
Refer to the [`RandomRDDs` Java docs ](api/java/org/apache/spark/mllib/random/RandomRDDs ) for details on the API.
2014-08-27 04:19:48 -04:00
{% highlight java %}
import org.apache.spark.SparkContext;
import org.apache.spark.api.JavaDoubleRDD;
import static org.apache.spark.mllib.random.RandomRDDs.*;
JavaSparkContext jsc = ...
// Generate a random double RDD that contains 1 million i.i.d. values drawn from the
// standard normal distribution `N(0, 1)` , evenly distributed in 10 partitions.
JavaDoubleRDD u = normalJavaRDD(jsc, 1000000L, 10);
// Apply a transform to get a random double RDD following `N(1, 4)` .
JavaDoubleRDD v = u.map(
new Function< Double , Double > () {
public Double call(Double x) {
return 1.0 + 2.0 * x;
}
});
{% endhighlight %}
< / div >
< div data-lang = "python" markdown = "1" >
2015-03-22 11:56:25 -04:00
[`RandomRDDs` ](api/python/pyspark.mllib.html#pyspark.mllib.random.RandomRDDs ) provides factory
2014-08-27 04:19:48 -04:00
methods to generate random double RDDs or vector RDDs.
The following example generates a random double RDD, whose values follows the standard normal
distribution `N(0, 1)` , and then map it to `N(1, 4)` .
2015-10-07 10:00:19 -04:00
Refer to the [`RandomRDDs` Python docs ](api/python/pyspark.mllib.html#pyspark.mllib.random.RandomRDDs ) for more details on the API.
2014-08-27 04:19:48 -04:00
{% highlight python %}
from pyspark.mllib.random import RandomRDDs
sc = ... # SparkContext
# Generate a random double RDD that contains 1 million i.i.d. values drawn from the
# standard normal distribution `N(0, 1)`, evenly distributed in 10 partitions.
2015-11-08 06:15:58 -05:00
u = RandomRDDs.normalRDD(sc, 1000000L, 10)
2014-08-27 04:19:48 -04:00
# Apply a transform to get a random double RDD following `N(1, 4)`.
v = u.map(lambda x: 1.0 + 2.0 * x)
{% endhighlight %}
< / div >
2015-08-17 20:57:51 -04:00
< / div >
## Kernel density estimation
[Kernel density estimation ](https://en.wikipedia.org/wiki/Kernel_density_estimation ) is a technique
useful for visualizing empirical probability distributions without requiring assumptions about the
particular distribution that the observed samples are drawn from. It computes an estimate of the
probability density function of a random variables, evaluated at a given set of points. It achieves
2017-01-04 10:07:29 -05:00
this estimate by expressing the PDF of the empirical distribution at a particular point as the
2015-08-17 20:57:51 -04:00
mean of PDFs of normal distributions centered around each of the samples.
< div class = "codetabs" >
< div data-lang = "scala" markdown = "1" >
[`KernelDensity` ](api/scala/index.html#org.apache.spark.mllib.stat.KernelDensity ) provides methods
to compute kernel density estimates from an RDD of samples. The following example demonstrates how
to do so.
2015-10-07 10:00:19 -04:00
Refer to the [`KernelDensity` Scala docs ](api/scala/index.html#org.apache.spark.mllib.stat.KernelDensity ) for details on the API.
2016-03-24 05:34:54 -04:00
{% include_example scala/org/apache/spark/examples/mllib/KernelDensityEstimationExample.scala %}
2015-08-17 20:57:51 -04:00
< / div >
< div data-lang = "java" markdown = "1" >
[`KernelDensity` ](api/java/index.html#org.apache.spark.mllib.stat.KernelDensity ) provides methods
to compute kernel density estimates from an RDD of samples. The following example demonstrates how
to do so.
2015-10-07 10:00:19 -04:00
Refer to the [`KernelDensity` Java docs ](api/java/org/apache/spark/mllib/stat/KernelDensity.html ) for details on the API.
2016-03-24 05:34:54 -04:00
{% include_example java/org/apache/spark/examples/mllib/JavaKernelDensityEstimationExample.java %}
2015-08-17 20:57:51 -04:00
< / div >
< div data-lang = "python" markdown = "1" >
[`KernelDensity` ](api/python/pyspark.mllib.html#pyspark.mllib.stat.KernelDensity ) provides methods
to compute kernel density estimates from an RDD of samples. The following example demonstrates how
to do so.
2015-10-07 10:00:19 -04:00
Refer to the [`KernelDensity` Python docs ](api/python/pyspark.mllib.html#pyspark.mllib.stat.KernelDensity ) for more details on the API.
2016-03-24 05:34:54 -04:00
{% include_example python/mllib/kernel_density_estimation_example.py %}
2015-08-17 20:57:51 -04:00
< / div >
2014-08-27 04:19:48 -04:00
< / div >