Return-Path: X-Original-To: apmail-commons-issues-archive@minotaur.apache.org Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CCD601871F for ; Sun, 10 Jan 2016 23:54:40 +0000 (UTC) Received: (qmail 22532 invoked by uid 500); 10 Jan 2016 23:54:40 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 22447 invoked by uid 500); 10 Jan 2016 23:54:40 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 22436 invoked by uid 99); 10 Jan 2016 23:54:40 -0000 Received: from arcas.apache.org (HELO arcas) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 10 Jan 2016 23:54:40 +0000 Received: from arcas.apache.org (localhost [127.0.0.1]) by arcas (Postfix) with ESMTP id D49FB2C1F57 for ; Sun, 10 Jan 2016 23:54:39 +0000 (UTC) Date: Sun, 10 Jan 2016 23:54:39 +0000 (UTC) From: "Gilles (JIRA)" To: issues@commons.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (MATH-1313) Wrong tolerance in some unit tests of "RandomGeneratorAbstractTest" MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/MATH-1313?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15091294#comment-15091294 ] Gilles commented on MATH-1313: ------------------------------ bq. Testing uniformity seems better than what we currently have I thought that the current was testing uniformity (unless out of bad luck, the sample follows another distribution that also happens to have a 0.5 mean). The problem with the current tolerance is that it is wrong: it is proportional to sqrt(N) instead of being proportional to sqrt(1/N). > Wrong tolerance in some unit tests of "RandomGeneratorAbstractTest" > ------------------------------------------------------------------- > > Key: MATH-1313 > URL: https://issues.apache.org/jira/browse/MATH-1313 > Project: Commons Math > Issue Type: Bug > Reporter: Gilles > Assignee: Gilles > Priority: Minor > Labels: unit-test > Fix For: 4.0 > > > I doubt that the mean check in the unit test below is ever going to trigger an assertion failure... > {noformat} > @Test > public void testDoubleDirect() { > SummaryStatistics sample = new SummaryStatistics(); > final int N = 10000; > for (int i = 0; i < N; ++i) { > sample.addValue(generator.nextDouble()); > } > Assert.assertEquals("Note: This test will fail randomly about 1 in 100 times.", > 0.5, sample.getMean(), FastMath.sqrt(N/12.0) * 2.576); > Assert.assertEquals(1.0 / (2.0 * FastMath.sqrt(3.0)), > sample.getStandardDeviation(), 0.01); > } > {noformat} > And similar in "testFloatDirect()". > I propose the following replacement: > {noformat} > @Test > public void testDoubleDirect() { > SummaryStatistics sample = new SummaryStatistics(); > final int N = 100000; > for (int i = 0; i < N; ++i) { > sample.addValue(generator.nextDouble()); > } > assertUniformInUnitInterval(sample, 0.99); > } > {noformat} > where "assertUniformInUnitInterval" is defined as: > {noformat} > /** > * Check that the sample follows a uniform distribution on the {@code [0, 1)} interval. > * > * @param sample Data summary. > * @param confidenceIntervalLevel Confidence level. Must be in {@code (0, 1)} interval. > */ > private void assertUniformInUnitInterval(SummaryStatistics sample, > double confidenceIntervalLevel) { > final int numSamples = (int) sample.getN(); > final double mean = sample.getMean(); > final double stddev = sample.getStandardDeviation() / FastMath.sqrt(numSamples); > final TDistribution t = new TDistribution(numSamples - 1); > final double criticalValue = t.inverseCumulativeProbability(1 - 0.5 * (1 - confidenceIntervalLevel)); > final double tol = stddev * criticalValue; > Assert.assertEquals("mean=" + mean + " tol=" + tol + " (note: This test will fail randomly about " + > (100 * (1 - confidenceIntervalLevel)) + " in 100 times).", > 0.5, mean, tol); > Assert.assertEquals(FastMath.sqrt(1d / 12), sample.getStandardDeviation(), 0.01); > } > {noformat} > Please correct if this new test is not what was intended. -- This message was sent by Atlassian JIRA (v6.3.4#6332)