+ Reply to Thread
Results 1 to 6 of 6

Generating variables with Poisson Distribution

Hybrid View

  1. #1
    Registered User
    Join Date
    09-22-2015
    Location
    Port-Louis, Mauritius
    MS-Off Ver
    MS Office 2010
    Posts
    4

    Generating variables with Poisson Distribution

    Consider a variable X which has a poisson distribution with mean 1.5.
    How do I generate 100 values of X using VBA ?
    Thanks.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Generating variables with Poisson Distribution

    Try:
    Sub main()
    Dim i As Integer
    Application.ScreenUpdating = False
    For i = 1 To 100
    Cells(i, "A") = random_Poisson(1.25)
    Next i
    End Sub
    
    Public Function random_Poisson(Mean As Double) As Double
    Dim e As Double, p As Double
    e = Exp(-Mean)
    p = 1
    Do Until (p < e)
    random_Poisson = random_Poisson + 1
    p = p * Rnd()
    Loop
    random_Poisson = random_Poisson - 1
    End Function
    Best Regards,

    Kaper

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Generating variables with Poisson Distribution

    @Kaper,

    Here's my version of that:

    Function RandPois(Lambda As Single, _
                      Optional bVolatile As Boolean = False) As Long
      ' shg 2007
    
      ' UDF or VBA
    
      ' Returns a random Poisson variate with variance Lambda
      ' Knuth, Seminumerical Algorithms, p 132
    
      Dim L             As Single
      Dim k             As Long
      Dim p             As Single
    
      If bVolatile Then Application.Volatile
    
      L = Exp(-Lambda)
      p = 1
      Do
        k = k + 1
        p = p * Rnd()
      Loop Until p <= L
    
      RandPois = k - 1
    End Function
    It differs from yours in that the loop always executes at least once.

    I went back to Knuth, and his description is a little arm-waving. Any thought on which is correct?
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Generating variables with Poisson Distribution

    Hi shg,
    Honestly - I just made a search in my HomeCloud and in one of files there was this function.

    But having a brief look (it's almost midnight here):
    Initial p is 1
    so
    Until (p < e)
    condition could be met before first execution of loop only for e>1
    but as
    e = Exp(-Mean)
    it means ... for mean <0

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Generating variables with Poisson Distribution

    Of course, thank you.

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,863

    Re: Generating variables with Poisson Distribution

    My pleasure :-)

+ 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. Poisson distribution - simulation
    By Hjahren in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 08-14-2021, 09:09 AM
  2. [SOLVED] VBA to simulate poisson distribution
    By Ztv in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-18-2014, 05:53 AM
  3. Generating Poisson Random Variables
    By telk02 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2012, 04:56 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. Poisson distribution
    By swfc in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-23-2008, 07:02 PM
  7. 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