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 742C911E10 for ; Fri, 12 Sep 2014 07:47:34 +0000 (UTC) Received: (qmail 49587 invoked by uid 500); 12 Sep 2014 07:47:34 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 49496 invoked by uid 500); 12 Sep 2014 07:47:34 -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 49454 invoked by uid 99); 12 Sep 2014 07:47:34 -0000 Received: from arcas.apache.org (HELO arcas.apache.org) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 12 Sep 2014 07:47:34 +0000 Date: Fri, 12 Sep 2014 07:47:33 +0000 (UTC) From: "Andras Sereny (JIRA)" To: issues@commons.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Created] (MATH-1152) Suboptimal implementation of EnumeratedDistribution.sample() MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 Andras Sereny created MATH-1152: ----------------------------------- Summary: Suboptimal implementation of EnumeratedDistribution.sample() Key: MATH-1152 URL: https://issues.apache.org/jira/browse/MATH-1152 Project: Commons Math Issue Type: Bug Affects Versions: 3.3 Reporter: Andras Sereny org.apache.commons.math3.distribution.EnumeratedDistribution.sample() performs a *linear* search to find the appropriate element in the probability space (called singletons here) given a random double value. For large probability spaces, this is not effective. Instead, we should cache the cumulative probabilities and do a *binary* search. Rough implementation: {code:title=Bar.java|borderStyle=solid} void computeCumulative() { cumulative = new double[size]; double sum = 0; for (int i = 1; i < weights.length - 1; i++) { cumulative[i] = cumulative[i-1] + weights[i-1]; } cumulative[size - 1] = 1; } {code} and then {code:title=Bar.java|borderStyle=solid} int sampleIndex() { double randomValue = random.nextDouble(); int result = Arrays.binarySearch(cumulative, randomValue); if (result >= 0) return result; int insertionPoint = -result-1; return insertionPoint; } {code} -- This message was sent by Atlassian JIRA (v6.3.4#6332)