+ Reply to Thread
Results 1 to 5 of 5

Generate Random numbers from Poisson distribution

  1. #1
    Forum Contributor
    Join Date
    01-28-2013
    Location
    sweden
    MS-Off Ver
    Excel 2010
    Posts
    108

    Generate Random numbers from Poisson distribution

    Hi,

    I am writing a sub that where I have to generate numbers from the Poisson distribution with different values of Lambda.

    I can do this in a worksheet by add Data Analysis add in. I use the random number generation and specify my lambda, number of variables and number of generated numbers.

    Does anyone know how I can access this function from VBA?

    Or better up, anyone with a written function for this?

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,444

    Re: Generate Random numbers from Poisson distribution

    Hi,

    I don't claim credit for this (can't remember from whence it came!) but I think it will work for you.

    Please Login or Register  to view this content.
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Forum Contributor
    Join Date
    01-28-2013
    Location
    sweden
    MS-Off Ver
    Excel 2010
    Posts
    108

    Re: Generate Random numbers from Poisson distribution

    Hi,

    I have seen this function elsewhere, but something is wrong with it.

    I cant say I understand why but function will always return 0 if lambda <= 0.5

    If I had statement debug.print in loop b wont increase by one on each iteration.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Generate Random numbers from Poisson distribution

    If I understand the problem with the posted UDF, the mistake is so obvious that I am not going to give it away. Instead, I am going to suggest that this is a good opportunity to practice using VBA's debugging tools and help you find the issue yourself (if you are unfamiliar with VBA's debugging tools, I recommend something like this: http://www.cpearson.com/excel/DebuggingVBA.aspx ). Debugging steps I took:

    1) I will almost always add a Stop statement of some kind to the UDF so that I can enter debug mode. In this case, I added
    Please Login or Register  to view this content.
    right after the function statement.
    2) Then I entered the function in B1 of the spreadsheet =PoissonRand(A1) and entered 0.5 into A1.
    3) When the function entered debug mode, the first thing I did was look at the locals window to see if lambda had been correctly passed to the function. At this point, I saw something other than 0.5 for lambda, and the error was obvious to me. Follow these steps and see what value is stored in lambda when you send 0.5 to the function.

    A couple of other thoughts.

    If you want this to be volatile -- so that it generates new random numbers each time Excel calculates -- then you can add
    Please Login or Register  to view this content.
    to the function. As currently programmed, the function will only calculate when lambda changes (or some other full calculate event is triggered). You can decide what behavior you want from this. Personally, I would tend to want this to be like Excel's built in random number functions and would make it volatile.

    Wikipedia describes this and two other algorithms for generating random Poisson numbers from uniform random numbers https://en.wikipedia.org/wiki/Poisso...ndom_variables They indicate that this algorithm works well, but may have some troubles for large lambda, and they suggest alternatives pseudocode. Only you know if this will be a concern for your implementation. If lambda can be large, then it would seem prudent to investigate these other algorithms in order to program a robust Poisson random number generator.
    Last edited by MrShorty; 02-24-2017 at 12:30 PM.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Forum Contributor
    Join Date
    01-28-2013
    Location
    sweden
    MS-Off Ver
    Excel 2010
    Posts
    108

    Re: Generate Random numbers from Poisson distribution

    ah.. lambda will obviously have to be dim as double!

    Thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 03-26-2013, 02:24 PM
  2. [SOLVED] How do you generate random numbers in a normal distribution?
    By Ladee_bird in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-13-2013, 08:18 AM
  3. vba code to generate random z-scores for normal distribution
    By bruceday in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-30-2012, 02:07 PM
  4. [SOLVED] Poisson distribution
    By hnasir in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-19-2012, 03:47 PM
  5. Poisson Distribution--Help
    By neocube in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-08-2011, 07:24 AM
  6. Can I use a macro to enter Poisson random numbers?
    By raphile in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 03-19-2009, 12:08 PM
  7. Replies: 6
    Last Post: 12-05-2008, 02:31 PM
  8. [SOLVED] Poisson Distribution
    By Capote in forum Excel General
    Replies: 1
    Last Post: 04-02-2006, 11:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1