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.
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.
I don't think this can be calculated in closed-form solution.
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?),![]()
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
=pWin(65%, 7, 9) returns 99.62%.
Last edited by shg; 09-29-2008 at 08:47 AM.
Entia non sunt multiplicanda sine necessitate
WOW!!!!
Thank you so much for your help SHG, this looks awesome!
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.
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?![]()
![]()
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
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
See the example usage in post #3.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks