Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
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 // $example on$
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 // $example off$
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     // $example on$
0043     // a vector composed of the frequencies of events
0044     Vector vec = Vectors.dense(0.1, 0.15, 0.2, 0.3, 0.25);
0045 
0046     // compute the goodness of fit. If a second vector to test against is not supplied
0047     // as a parameter, the test runs against a uniform distribution.
0048     ChiSqTestResult goodnessOfFitTestResult = Statistics.chiSqTest(vec);
0049     // summary of the test including the p-value, degrees of freedom, test statistic,
0050     // the method used, and the null hypothesis.
0051     System.out.println(goodnessOfFitTestResult + "\n");
0052 
0053     // Create a contingency matrix ((1.0, 2.0), (3.0, 4.0), (5.0, 6.0))
0054     Matrix mat = Matrices.dense(3, 2, new double[]{1.0, 3.0, 5.0, 2.0, 4.0, 6.0});
0055 
0056     // conduct Pearson's independence test on the input contingency matrix
0057     ChiSqTestResult independenceTestResult = Statistics.chiSqTest(mat);
0058     // summary of the test including the p-value, degrees of freedom...
0059     System.out.println(independenceTestResult + "\n");
0060 
0061     // an RDD of labeled points
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     // The contingency table is constructed from the raw (label, feature) pairs and used to conduct
0071     // the independence test. Returns an array containing the ChiSquaredTestResult for every feature
0072     // against the label.
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");  // summary of the test
0078       i++;
0079     }
0080     // $example off$
0081 
0082     jsc.stop();
0083   }
0084 }