0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 package org.apache.spark.examples.mllib;
0019
0020 import org.apache.spark.SparkConf;
0021 import org.apache.spark.api.java.JavaSparkContext;
0022
0023
0024 import java.util.Arrays;
0025
0026 import org.apache.spark.api.java.JavaRDD;
0027 import org.apache.spark.mllib.linalg.Matrices;
0028 import org.apache.spark.mllib.linalg.Matrix;
0029 import org.apache.spark.mllib.linalg.Vector;
0030 import org.apache.spark.mllib.linalg.Vectors;
0031 import org.apache.spark.mllib.regression.LabeledPoint;
0032 import org.apache.spark.mllib.stat.Statistics;
0033 import org.apache.spark.mllib.stat.test.ChiSqTestResult;
0034
0035
0036 public class JavaHypothesisTestingExample {
0037 public static void main(String[] args) {
0038
0039 SparkConf conf = new SparkConf().setAppName("JavaHypothesisTestingExample");
0040 JavaSparkContext jsc = new JavaSparkContext(conf);
0041
0042
0043
0044 Vector vec = Vectors.dense(0.1, 0.15, 0.2, 0.3, 0.25);
0045
0046
0047
0048 ChiSqTestResult goodnessOfFitTestResult = Statistics.chiSqTest(vec);
0049
0050
0051 System.out.println(goodnessOfFitTestResult + "\n");
0052
0053
0054 Matrix mat = Matrices.dense(3, 2, new double[]{1.0, 3.0, 5.0, 2.0, 4.0, 6.0});
0055
0056
0057 ChiSqTestResult independenceTestResult = Statistics.chiSqTest(mat);
0058
0059 System.out.println(independenceTestResult + "\n");
0060
0061
0062 JavaRDD<LabeledPoint> obs = jsc.parallelize(
0063 Arrays.asList(
0064 new LabeledPoint(1.0, Vectors.dense(1.0, 0.0, 3.0)),
0065 new LabeledPoint(1.0, Vectors.dense(1.0, 2.0, 0.0)),
0066 new LabeledPoint(-1.0, Vectors.dense(-1.0, 0.0, -0.5))
0067 )
0068 );
0069
0070
0071
0072
0073 ChiSqTestResult[] featureTestResults = Statistics.chiSqTest(obs.rdd());
0074 int i = 1;
0075 for (ChiSqTestResult result : featureTestResults) {
0076 System.out.println("Column " + i + ":");
0077 System.out.println(result + "\n");
0078 i++;
0079 }
0080
0081
0082 jsc.stop();
0083 }
0084 }