+ Reply to Thread
Results 1 to 14 of 14

Using the Binomial Distribution function in excel

Hybrid View

  1. #1
    Registered User
    Join Date
    09-10-2008
    Location
    UK
    Posts
    7
    Hi, my threads seems to have slid off the board so I was hoping that if I brought it back up I might be able to get a bit of input from somebody.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    I don't think this can be calculated in closed-form solution.
    Function pWin(p As Double, nH As Long, nT As Long) As Variant
        ' Returns the probability that a coin with
        ' probability p of heads will accumulate
        ' nH excess heads before nT excess tails
        Dim q       As Double   ' 1-p
        Dim adPrb() As Double  ' array of probabilities
        Dim i       As Long     ' index to array
        Dim pH      As Double   ' probability of Heads winning
        Dim pT      As Double   ' probability of Tails winning
        
        Dim dH      As Double   ' rate at which pH is increasing
        Dim dT      As Double   ' rate at which pT is increasing
        
        Dim dPrb    As Double   ' temporary variable
        Dim iOdd    As Long     ' odd/even roll
        Dim nToss   As Long     ' tosses made
        
        If nH < 1 Then Exit Function
        If nT < 1 Then Exit Function
    
        q = 1# - p
        ReDim adPrb(-nT To nH)
        adPrb(0) = 1#
    
        ' loop until the sum of probabilities is close to 1
        Do While pH + pT < 0.99999
            iOdd = (iOdd + 1) Mod 2
            For i = -nT + ((nT + iOdd) And 1) To nH Step 2
                dPrb = 0#
                If i > 1 - nT Then dPrb = p * adPrb(i - 1)
                If i < nH - 1 Then dPrb = q * adPrb(i + 1) + dPrb
                adPrb(i) = dPrb
                If i = -nT Then pT = pT + dPrb
                If i = nH Then pH = pH + dPrb
            Next i
            nToss = nToss + 1
        Loop
        ' scale result to 100%
        pWin = Array(pH / (pH + pT), nToss)
    End Function
    For your example on the other board (with a coin biased to flip heads 65% of the time, what's the probability of accumulating 7 heads before 9 tails?),

    =pWin(65%, 7, 9) returns 99.62%.
    Last edited by shg; 09-29-2008 at 08:47 AM.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    09-10-2008
    Location
    UK
    Posts
    7
    WOW!!!!

    Thank you so much for your help SHG, this looks awesome!

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

    To be clear, the posted code computes the probability of flipping (for the example) 7 more heads than tails.

    The probability of flipping 7 heads before 9 tails would require changing the code -- you only need to simulate 16 flips total.

  5. #5
    Registered User
    Join Date
    09-10-2008
    Location
    UK
    Posts
    7
    err....

    I've just realised that I don't really know what to do with this bit of code. Could someone enlighten me on how to apply it to a spreadsheet?

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    1. Copy the code from the post
    2. Press Alt+F11 to open the Visual Basic Editor (VBE)
    3. From the menu bar in the VBE window, do Insert > Module
    4. Paste the code in the window that opens
    5. Close the VBE to return to Excel

  7. #7
    Registered User
    Join Date
    09-10-2008
    Location
    UK
    Posts
    7
    Thanks a lot SHG. Without wanting to test your patience - how do I get it to perform the calculations for me now? I've followed the steps that you listed but now I'm back on a blank worksheet.

    I've never used the VBA side of things before you see.

    Once again - really appreciate your help mate. I can't thank you enough

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

+ 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. NORMDIST function in excel giving values greater than 1
    By excelwho in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-09-2013, 11:43 PM
  2. Excel 2007 error when adding custom help file to user defined function
    By sabotuer99 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-04-2009, 01:10 AM
  3. Excel Sum Function Question
    By cmarty5445 in forum Excel General
    Replies: 1
    Last Post: 08-30-2008, 08:27 AM
  4. Writing VBA Macro or Excel Function
    By venkat_kodi in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-06-2007, 03:21 PM
  5. How to use Standard Excel function in VBA
    By akabraha in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-24-2007, 03:50 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