diff --git a/projects/Math/102/org/apache/commons/math/stat/inference/ChiSquareTestImpl.java b/projects/Math/102/org/apache/commons/math/stat/inference/ChiSquareTestImpl.java index c87b010..984d56d 100644 --- a/projects/Math/102/org/apache/commons/math/stat/inference/ChiSquareTestImpl.java +++ b/projects/Math/102/org/apache/commons/math/stat/inference/ChiSquareTestImpl.java @@ -71,11 +71,28 @@ public double chiSquare(double[] expected, long[] observed) throw new IllegalArgumentException( "observed counts must be non-negative and expected counts must be postive"); } + double sumExpected = 0d; + double sumObserved = 0d; + for (int i = 0; i < observed.length; i++) { + sumExpected += expected[i]; + sumObserved += observed[i]; + } + double ratio = 1.0d; + boolean rescale = false; + if (Math.abs(sumExpected - sumObserved) > 10E-6) { + ratio = sumObserved / sumExpected; + rescale = true; + } double sumSq = 0.0d; double dev = 0.0d; for (int i = 0; i < observed.length; i++) { + if (rescale) { + dev = ((double) observed[i] - ratio * expected[i]); + sumSq += dev * dev / (ratio * expected[i]); + } else { dev = ((double) observed[i] - expected[i]); sumSq += dev * dev / expected[i]; + } } return sumSq; }