From user-return-26839-apmail-commons-user-archive=commons.apache.org@commons.apache.org Sun Oct 23 08:22:49 2011 Return-Path: X-Original-To: apmail-commons-user-archive@www.apache.org Delivered-To: apmail-commons-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9DB219984 for ; Sun, 23 Oct 2011 08:22:49 +0000 (UTC) Received: (qmail 42179 invoked by uid 500); 23 Oct 2011 08:22:47 -0000 Delivered-To: apmail-commons-user-archive@commons.apache.org Received: (qmail 42006 invoked by uid 500); 23 Oct 2011 08:22:46 -0000 Mailing-List: contact user-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Users List" Delivered-To: mailing list user@commons.apache.org Received: (qmail 41998 invoked by uid 99); 23 Oct 2011 08:22:43 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 23 Oct 2011 08:22:43 +0000 X-ASF-Spam-Status: No, hits=3.2 required=5.0 tests=FREEMAIL_FROM,FREEMAIL_REPLY,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [80.67.169.19] (HELO solo.fdn.fr) (80.67.169.19) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 23 Oct 2011 08:22:36 +0000 Received: from lehrin.spaceroots.org (reverse-229.fdn.fr [80.67.176.229]) by smtp.fdn.fr (Postfix) with ESMTP id 84D41451CF for ; Sun, 23 Oct 2011 10:22:12 +0200 (CEST) Received: from [127.0.0.1] (lehrin.spaceroots.org [127.0.0.1]) by lehrin.spaceroots.org (Postfix) with ESMTP id 1ACC05F3F2 for ; Sun, 23 Oct 2011 10:22:12 +0200 (CEST) Message-ID: <4EA3CEB3.3070405@free.fr> Date: Sun, 23 Oct 2011 10:22:11 +0200 From: Luc Maisonobe User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.23) Gecko/20111010 Icedove/3.1.15 MIME-Version: 1.0 To: Commons Users List Subject: Re: [math] Re: binomial random generator References: <4EA38A25.7000506@gmail.com>, <03FEE575BFE70B4AA3BB5014DC59648B01912C6B38B7@HERMES8.ds.leeds.ac.uk> In-Reply-To: <03FEE575BFE70B4AA3BB5014DC59648B01912C6B38B7@HERMES8.ds.leeds.ac.uk> X-Enigmail-Version: 1.1.2 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Le 23/10/2011 09:47, Andy Turner a écrit : > Hi, Hello All, You could also give a try to other random generators provided by Apache Commons Math. Did you try the Well generators like Well1024a for example ? These generators are tailored towards large simulation like Monte-Carlo ones. Luc > > Ahmed, I may be doing something similar. I have simulation models driven by probabilities (values between 0 and 1). In this range I used fixed decimal precision probabilities by using BigDecimal. I have written code that does a pseudo random test of a specific value in the range to return true or false. I beleive this is without bias. For example for a test value of 0.75, 25% of the time it is likely to return true. I've copied the test code for this below. It is part of the generic package I have developed which I release via the following URL: > http://www.geog.leeds.ac.uk/people/a.turner/src/andyt/java/generic/ > (If you go to get the code, please get it from the subversion repository as the bundled release is out of date.) > > I have another method that returns a pseudo random BigDecimal at a given decimal place precision within a specified range. This might also be useful to you if you really need to have the value rather than perform a test. > > A while ago I offered my code to math (because it does BigDecimal exponentiation). I took some steps towards fullfilling the requirements for its inclusion, but because of other demands on my time, I have not managed to complete this which I am still keen to do this... > > I hope this helps. Please let me know if it does. It may be that you are doing something more complicated and this would need adapting to work with non uniform distributions you are using. > > Best wishes, > > Andy > > Here are the two methods I use for the random test, the private one is for internal working: > > /** > * Effectively this is the same as generating a random number between 0 and > * 1 and comparing it with probability and if it were higher then return > * false and otherwise return true > * @param a_Random > * @param probability 0 <= probability <=1 > * @return true or false based on a random uniform test of probability > */ > public static boolean randomUniformTest( > Random a_Random, > BigDecimal probability, > int decimalPlaces, > RoundingMode a_RoundingMode) { > // Special case probability <= 0 > if (probability.compareTo(BigDecimal.ZERO) != 1) { > if (probability.compareTo(BigDecimal.ZERO) == 0) { > return false; > } else { > System.out.println( > "Warning probabilty negative in " > + Generic_BigDecimal.class > + ".randomUniformTest(Random,BigDecimal). " > + "Returning false."); > return false; > } > } > // Special case probability >= 1 > if (probability.compareTo(BigDecimal.ONE) != -1) { > if (probability.compareTo(BigDecimal.ONE) == 0) { > return true; > } else { > System.out.println( > "Warning probabilty greater > 1 in " > + Generic_BigDecimal.class > + ".randomUniformTest(Random,BigDecimal). " > + "Returning true."); > return true; > } > } > // Set decimalPlace precision to maximum of decimalPlaces and > // probability.scale(); > int probabilityScale = probability.scale(); > if (decimalPlaces < probabilityScale) { > System.out.println( > "Input decimalPlaces < probabilty.scale() in " > + Generic_BigDecimal.class > + ".randomUniformTest(Random,BigDecimal). " > + "Set decimalPlaces = probabilty.scale()."); > decimalPlaces = probabilityScale; > } > BigDecimal midTestValue = new BigDecimal("0.5"); > BigDecimal half_BigDecimal = new BigDecimal("0.5"); > BigDecimal two_BigDecimal = new BigDecimal("2.0"); > if (probability.compareTo(midTestValue) == 0) { > return a_Random.nextBoolean(); > } > if (probability.compareTo(midTestValue) == -1) { > return randomTest( > a_Random, > probability, > BigDecimal.ZERO, > BigDecimal.ONE, > midTestValue, > half_BigDecimal, > two_BigDecimal, > decimalPlaces, > a_RoundingMode); > } else { > return !randomTest( > a_Random, > BigDecimal.ONE.subtract(probability), > BigDecimal.ZERO, > BigDecimal.ONE, > midTestValue, > half_BigDecimal, > two_BigDecimal, > decimalPlaces, > a_RoundingMode); > } > } > private static boolean randomTest( > Random a_Random, > BigDecimal probability, > BigDecimal minTestValue, > BigDecimal maxTestValue, > BigDecimal midTestValue, > BigDecimal half_BigDecimal, > BigDecimal two_BigDecimal, > int decimalPlaces, > RoundingMode a_RoundingMode) { > if (probability.compareTo(midTestValue) == 0) { > return a_Random.nextBoolean(); > } > boolean above = a_Random.nextBoolean(); > if (above) { > if (probability.compareTo(midTestValue) == 1) { > // Test > BigDecimal newMinTestValue = midTestValue; > BigDecimal newMidTestValue = divide( > newMinTestValue.add(maxTestValue), > two_BigDecimal, > decimalPlaces, > a_RoundingMode); > return randomTest( > a_Random, > probability, > newMinTestValue, > maxTestValue, > newMidTestValue, > half_BigDecimal, > two_BigDecimal, > decimalPlaces, > a_RoundingMode); > } else { > return false; > } > } else { > if (probability.compareTo(midTestValue) == 1) { > return true; > } else { > //Test > BigDecimal newMaxTestValue = midTestValue; > BigDecimal newMidTestValue = divide( > newMaxTestValue.add(minTestValue), > two_BigDecimal, > decimalPlaces, > a_RoundingMode); > return randomTest( > a_Random, > probability, > minTestValue, > newMaxTestValue, > newMidTestValue, > half_BigDecimal, > two_BigDecimal, > decimalPlaces, > a_RoundingMode); > } > } > } > > ________________________________________ > From: Ahmed Abdeen Hamed [ahmed.elmasri@gmail.com] > Sent: 23 October 2011 04:40 > To: Commons Users List > Subject: Re: [math] Re: binomial random generator > > Hi Phil, > > Thanks very much for your quick response. I am doing some simulations that > produce probailities which are in turn must be compared to certains rates. > Here is an example of what I am doing: > > if (randBinomial.nextBinomial(1, Simulation.MRATE)==1) { > > // the condition can be replaced by the following uncommented condition > > //randM.nextDouble() < Simulation.M_RATE; > > if (gt[i] == 0) { > > gt[i] = 1; > > } else { > > gt[i] = 0; > > } > > } > > Please let me know if I am using Binomial object and its method correctly. > > Thanks again! > > Ahmed > > > On Sat, Oct 22, 2011 at 11:29 PM, Phil Steitz wrote: > >> On 10/22/11 8:15 PM, Ahmed Abdeen Hamed wrote: >>> Hello friends, >>> >>> I am using the following code in a program I am writing: >>> private static JDKRandomGenerator rg = new JDKRandomGenerator(); >>> private static RandomDataImpl randBinomial = new RandomDataImpl(rg); >>> >>> However, it is slowing down things very much. When I replace generator >> with >>> a java.util.Random object it works fine without the performance issues. >> Is >>> that expected with this library? Is there any work around? My programs >> are >>> very time consuming and I have been repeatedly running them. I really >> need >>> some tips if there is anything I can do to enhance the performance. >> >> That depends on how you are using randBinomial above. Can you share >> the code that uses randBinomial and what you mean by "replace >> generator with a java.util.Random object?" It will also help to >> describe what exactly you are trying to accomplish with the code. >> >> Phil >>> >>> Thanks very much! >>> Ahmed >>> >> >> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org >> For additional commands, e-mail: user-help@commons.apache.org >> >> > --------------------------------------------------------------------- > To unsubscribe, e-mail: user-unsubscribe@commons.apache.org > For additional commands, e-mail: user-help@commons.apache.org > --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@commons.apache.org For additional commands, e-mail: user-help@commons.apache.org