/** * @param counts array representation of 2-way table * @param alpha significance level of the test * @return true iff null hypothesis can be rejected with confidence * 1 - alpha * @throws IllegalArgumentException if preconditions are not met * @throws MathException if an error occurs performing the test */ public boolean chiSquareTest(long[][] counts, double alpha) throws IllegalArgumentException, MathException { if ((alpha <= 0) || (alpha > 0.5)) { throw new IllegalArgumentException("bad significance level: " + alpha); } return (chiSquareTest(counts) < alpha); }
/** * @param counts array representation of 2-way table * @return p-value * @throws IllegalArgumentException if preconditions are not met * @throws MathException if an error occurs computing the p-value */ public double chiSquareTest(long[][] counts) throws IllegalArgumentException, MathException { checkArray(counts); double df = ((double) counts.length -1) * ((double) counts[0].length - 1); distribution.setDegreesOfFreedom(df); return 1 - distribution.cumulativeProbability(chiSquare(counts)); }
public IValue chiSquare(IList dataValues){ makeChi(dataValues); return values.real(new ChiSquareTestImpl().chiSquare(expected, observed)); }
ChiSquareTestImpl cs = new ChiSquareTestImpl(); try { pValues.addValue(cs.chiSquareTest(expected, observed)); xs.addValue(cs.chiSquare(expected, observed)); } catch (Exception e) { e.printStackTrace();
public IValue chiSquareTest(IList dataValues){ makeChi(dataValues); try { return values.real(new ChiSquareTestImpl().chiSquareTest(expected, observed)); } catch (IllegalArgumentException e) { throw RuntimeExceptionFactory.illegalArgument(dataValues, null, null, e.getMessage()); } catch (MathException e) { throw RuntimeExceptionFactory.illegalArgument(dataValues, null, null, e.getMessage()); } }
/** * {@inheritDoc} * <p><strong>Note: </strong>This implementation rescales the * <code>expected</code> array if necessary to ensure that the sum of the * expected and observed counts are equal.</p> * * @param observed array of observed frequency counts * @param expected array of expected frequency counts * @return p-value * @throws IllegalArgumentException if preconditions are not met * @throws MathException if an error occurs computing the p-value */ public double chiSquareTest(double[] expected, long[] observed) throws IllegalArgumentException, MathException { distribution.setDegreesOfFreedom(expected.length - 1.0); return 1.0 - distribution.cumulativeProbability( chiSquare(expected, observed)); }
/** * Checks to make sure that the input long[][] array is rectangular, * has at least 2 rows and 2 columns, and has all non-negative entries, * throwing IllegalArgumentException if any of these checks fail. * * @param in input 2-way table to check * @throws IllegalArgumentException if the array is not valid */ private void checkArray(long[][] in) throws IllegalArgumentException { if (in.length < 2) { throw MathRuntimeException.createIllegalArgumentException( LocalizedFormats.INSUFFICIENT_DIMENSION, in.length, 2); } if (in[0].length < 2) { throw MathRuntimeException.createIllegalArgumentException( LocalizedFormats.INSUFFICIENT_DIMENSION, in[0].length, 2); } checkRectangular(in); checkNonNegative(in); }
"dimension mismatch {0} != {1}", expected.length, observed.length); checkPositive(expected); checkNonNegative(observed); double sumExpected = 0d; double sumObserved = 0d;
/** * Create a ChiSquareTest instance. * * @return a new ChiSquareTest instance */ public ChiSquareTest createChiSquareTest() { return new ChiSquareTestImpl(); }
/** * Create a test instance using the given distribution for computing * inference statistics. * @param x distribution used to compute inference statistics. * @since 1.2 */ public ChiSquareTestImpl(ChiSquaredDistribution x) { super(); setDistribution(x); } /**
/** * @param observed1 array of observed frequency counts of the first data set * @param observed2 array of observed frequency counts of the second data set * @return p-value * @throws IllegalArgumentException if preconditions are not met * @throws MathException if an error occurs computing the p-value * @since 1.2 */ public double chiSquareTestDataSetsComparison(long[] observed1, long[] observed2) throws IllegalArgumentException, MathException { distribution.setDegreesOfFreedom((double) observed1.length - 1); return 1 - distribution.cumulativeProbability( chiSquareDataSetsComparison(observed1, observed2)); }
/** * @param observed1 array of observed frequency counts of the first data set * @param observed2 array of observed frequency counts of the second data set * @param alpha significance level of the test * @return true iff null hypothesis can be rejected with confidence * 1 - alpha * @throws IllegalArgumentException if preconditions are not met * @throws MathException if an error occurs performing the test * @since 1.2 */ public boolean chiSquareTestDataSetsComparison(long[] observed1, long[] observed2, double alpha) throws IllegalArgumentException, MathException { if ((alpha <= 0) || (alpha > 0.5)) { throw new IllegalArgumentException( "bad significance level: " + alpha); } return (chiSquareTestDataSetsComparison(observed1, observed2) < alpha); }
checkArray(counts); int nRows = counts.length; int nCols = counts[0].length;
checkNonNegative(observed1); checkNonNegative(observed2);
ChiSquareTestImpl cs = new ChiSquareTestImpl(); try { pValue = cs.chiSquareTest(expected, observed); chiSq = cs.chiSquare(expected, observed); } catch (Exception e) { e.printStackTrace();
public IValue chiSquareTest(IList dataValues, IReal alpha){ makeChi(dataValues); try { return values.bool(new ChiSquareTestImpl().chiSquareTest(expected, observed, alpha.doubleValue())); } catch (IllegalArgumentException e) { throw RuntimeExceptionFactory.illegalArgument(dataValues, null, null, e.getMessage()); } catch (MathException e) { throw RuntimeExceptionFactory.illegalArgument(dataValues, null, null, e.getMessage()); } }
/** * {@inheritDoc} * <p><strong>Note: </strong>This implementation rescales the * <code>expected</code> array if necessary to ensure that the sum of the * expected and observed counts are equal.</p> * * @param observed array of observed frequency counts * @param expected array of expected frequency counts * @return p-value * @throws IllegalArgumentException if preconditions are not met * @throws MathException if an error occurs computing the p-value */ public double chiSquareTest(double[] expected, long[] observed) throws IllegalArgumentException, MathException { distribution.setDegreesOfFreedom(expected.length - 1.0); return 1.0 - distribution.cumulativeProbability( chiSquare(expected, observed)); }
/** * Checks to make sure that the input long[][] array is rectangular, * has at least 2 rows and 2 columns, and has all non-negative entries, * throwing IllegalArgumentException if any of these checks fail. * * @param in input 2-way table to check * @throws IllegalArgumentException if the array is not valid */ private void checkArray(long[][] in) throws IllegalArgumentException { if (in.length < 2) { throw MathRuntimeException.createIllegalArgumentException( "invalid row dimension: {0} (must be at least 2)", in.length); } if (in[0].length < 2) { throw MathRuntimeException.createIllegalArgumentException( "invalid column dimension: {0} (must be at least 2)", in[0].length); } checkRectangular(in); checkNonNegative(in); }
LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, expected.length, observed.length); checkPositive(expected); checkNonNegative(observed); double sumExpected = 0d; double sumObserved = 0d;
/** * Create a test instance using the given distribution for computing * inference statistics. * @param x distribution used to compute inference statistics. * @since 1.2 */ public ChiSquareTestImpl(ChiSquaredDistribution x) { super(); setDistribution(x); } /**