+ 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

    Using the Binomial Distribution function in excel

    Hi everyone,

    I have posted this question on another forum but hoped that I may be able to get a different perspective from other people.


    I have been working on a problem that requires being able to calculate the chances of any winnng a series of coin tosses with a weighted coin.

    The coin has for arguments sake a 65% / 35% chance of landing heads or tails and to win the game you need to hit your side 9 times.

    Can you use excel to calculate things like: If heads takes the lead 1-0, what does that mean the its overall percentage chance of winning? If the score is 7-3 to tails, what are the chances of either side gaining victory? etc, etc.

    I thought that there would be a way of setting up a spreadsheet that listed the goal (first to 9, 12, 15 or whatever) and below this the percentage chance of heads and the percentage chance of tails. Then somewhere would be a box to enter the current score (0-0, 3-5, 1-1 or whatever) and beside this the current chance of heads or tails gaining victory.

    I've tried setting up something like this but am a bit of a dufus when it comes to knowing how, where and when to use the different functions of excel.

    Would anyone be able to give me a hand in how to set up something like this?


    Thanks in advance for any help.


    Here is the link to the same question on another board http://www.mrexcel.com/forum/showthread.php?t=340808

  2. #2
    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.

  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
    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

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

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

  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
    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.

  6. #6
    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?

  7. #7
    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

  8. #8
    Registered User
    Join Date
    09-10-2008
    Location
    UK
    Posts
    7
    Thats amazing, thank you very much SHG

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    You're welcome, thanks for the feedback.

  10. #10
    Registered User
    Join Date
    09-10-2008
    Location
    UK
    Posts
    7
    Hi SHG,

    I've gone over the excellent piece of code that you contributed a few times and think that there is a small anomoly but cannot figure out exactly where.

    I set up a small tool to use the function and think that the percentage split for heads and tails is being amplified too much when you seperate them.

    When they are split 50/50 the results look correct but any other weighting looks wrong when I've experimented. I'll give a few examples:

    50/50 split and the goal is first to reach 5 successes (best of 9):

    Chances of either side winning (50%/50%)

    2-1 (57%/43%)
    3-1 (67%/33%)
    4-1 (80%/20%)
    3-2 (50%/40%)
    4-2 (75%/25%)
    4-3 (67%/33%)

    Like I said before these look to be bang on in terms of reflecting the chances of either side winning.

    In a 60/40 split with the same goal however I get these results:

    Chances of either side winning (88%/12%) ???

    2-1 (85%/15%)
    3-1 (88%/12%) ???
    4-1 (92%/8%)
    3-2 (81%/19%)
    4-2 (88%/12%) ???
    4-3 (79%/21%)

    With a 60/40 split the tails (40% side) has to get to a score of 0-4 before it is considered more likely to win which surely cannot be correct.

    Have I applied the piece of code that you made me wrong or is there a simple solution the problem? I have attached a copy of the spreadsheet that I was using on here (the cells using the formula are in D13 and D14)

    Once again, thanks for all of your help up until now I really appreciate it.


    Thanks

    Gadam
    Attached Files Attached Files

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    As I tried to explain (and apparently not well), that's not what the code calculates.

    Start flipping a coin, and in a column, write down the running total, adding one for each heads and subtracting 1 for each tails. This is a random walk; if 9 tosses went

    H-H-T-H-T-T-T-H-H

    the column would read

    +1, +2, +1, +2, +1, 0, -1, 0, +1.

    The code calculates the probability that you get to (say) +3 before getting to (say) -5. Capische?

    The code keeps flipping until the probability of one event or the other occurring (e.g., reaching +3 or -5) is 99.99% (which converges quickly with a biased coin), and then scales the numbers upward to total 100%.

    Simulating a finite number of flips (e.g., best of 9) could be done with a slight modification.

    Edit: In fact, for 'best of' trials, you can just use the BINOMDIST function.
    Last edited by shg; 10-17-2008 at 11:43 AM.

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    ... for example,
          ---A--- --B--- -----C----- ----D-----
      1   # Flips nHeads prob(Heads) Heads Wins
      2      3      2        60%       64.8%   
      3      4      3        60%       47.5%   
      4      5      4        60%       33.7%   
      5      5      3        60%       68.3%   
      6      6      4        60%       54.4%   
      7      7      4        60%       71.0%
    Row 2 is interpreted as, "In three flips of a coin biased to flip heads 60% of the time, what's the probability of flipping two or more heads? (64.8%)

    The array formula in D2 and copied down is

    =SUM(BINOMDIST( ROW(INDIRECT(B2 + 1 & ":" & A2 + 1) ) - 1, A2, C2, FALSE))
    Last edited by shg; 10-17-2008 at 12:04 PM.

+ 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