No, I used the same model that I used for the coin toss, but obviously with the corresponding probabilities. I added the probability of a tie to the probability of winning.
You can also check those numbers on the website suggested by Don:
https://sites.google.com/view/krapstuff/home
I think you also know that my results were simulated, although they coincide quite accurately with the results obtained from online calculators.
There is no combinatorial analysis involved.
Sincerely,
Cac
Luck is what happens when preparation meets opportunity.
This is my implementation.
Code:#include <iostream> #include <vector> #include <cmath> long long binomialCoefficient(int n, int k) { std::vector<long long> dp(k + 1, 0); dp[0] = 1; for (int i = 1; i <= n; ++i) { for (int j = std::min(i, k); j > 0; --j) { dp[j] = dp[j] + dp[j - 1]; } } return dp[k]; } double binomialProbability(int n, int k, double p) { long long coeff = binomialCoefficient(n, k); double prob = coeff * std::pow(p, k) * std::pow(1 - p, n - k); return prob; } // Function to calculate the binomial cumulative distribution function double binomialCDF(int k, int n, double p) { double cdf = 0.0; for (int i = 0; i <= k; ++i) { cdf += binomialProbability(n, i, p); } return cdf; } // Function to calculate the probability of exactly one streak of length l in n trials double probabilityOfExactStreak(int l, double p, int n) { if (l > n) return 0; // No streaks possible if l > n double q = 1.0 - p; double totalProbability = 0.0; // Calculating the probability of having exactly one streak starting at position k for (int k = 0; k <= n - l; ++k) { // Probability of a streak of exactly length l starting at position k double probStreak = (k + l < n) ? std::pow(p, l) * q : std::pow(p, l); // Probability that there are no streaks of length l before k double probNoStreakBeforeK = 1 - binomialCDF(l - 1, k, p); // Probability that there are no streaks of length l after k + l double probNoStreakAfterKL = (k + l < n) ? 1 - binomialCDF(l - 1, n - (k + l) - 1, p) : 1; // Adding the contribution of the streak starting at k and no other streaks of the same length totalProbability += probStreak * probNoStreakBeforeK * probNoStreakAfterKL; } return totalProbability; }
Chance favors the prepared mind
Exactly 2 or more than 2. Exactly the same of getting 2 or more tails. Regardless, the Law of Small Numbers should apply. Notwithstanding semantics, seems to me the probability is dependant on wheather the first head (or tail) is achieved on the 1st, 2nd or 3rd toss. Clearly, the odds of a second head is greatest when the first toss is a head.
Of course, the probability of heads on any toss is 50%.
Luck is what happens when preparation meets opportunity.
So my code returns 0.0625 which is blatantly incorrect as the correct number is 0.3125 if we do exactly 2 consecutive heads or 0.5 if we do 2 or more consecutive heads (3 or 4 in this case). My code is an adaptation of de Moivre formula which works fairly well when the number of trials increases but fails for small number of trials.
I have of course looked at brute force combinatorial analysis but while the code is fairly simple, the calculation quickly becomes intractable as the number of trials increases because for n trials you have 2^n possible permutations, so you can kiss solving 7000 rounds (2^7000) good bye .
I was able to implement a fairly easy sim that reproduces the values. Below is the implementation in VBA if you want to test it in Excel. @Norm please dont hate me for using the built-in RNG
Code:Sub TestMe() Debug.Print ProbabilityOfStreak(2, 0.5, 4, 100000, True) End Sub Function ProbabilityOfStreak(l As Long, p As Double, n As Long, simulations As Long, allowLonger As Boolean) As Double Dim streakCount As Long Dim currentStreak As Long Dim i As Long Dim j As Long Dim successfulSimulations As Long successfulSimulations = 0 ' Seed the random number generator Randomize ' Randomly generate each trial and count the number of successful simulations with exactly one streak of length l For i = 1 To simulations currentStreak = 0 streakCount = 0 For j = 1 To n If Rnd() < p Then ' Increment streak if the event occurs currentStreak = currentStreak + 1 Else ' Check if the streak ended and was exactly length l or longer based on allowLonger flag If (allowLonger And currentStreak >= l) Or (Not allowLonger And currentStreak = l) Then streakCount = streakCount + 1 End If currentStreak = 0 End If Next j ' Check streak at the end of the trials If (allowLonger And currentStreak >= l) Or (Not allowLonger And currentStreak = l) Then streakCount = streakCount + 1 End If ' Check if exactly one streak of length l occurred If streakCount = 1 Then successfulSimulations = successfulSimulations + 1 End If Next i ' Calculate probability based on sim results ProbabilityOfStreak = successfulSimulations / simulations End Function
Chance favors the prepared mind
Exactly, that's why using combinatorial analysis is not advisable in this type of problems. Not to mention the use of factorials.
A long simulation is much more reliable. Let me give you an example of why combinatorial analysis fails.
Suppose we want to calculate C (1000, 30). The following are 3 values obtained by different routines:
The first one corresponds to the code you posted above (binomialCoefficient).
The next two correspond to two routines of mine.
The correct one is none of the 3.
BC1 = 6427395792647100880
BC2 = 2429608192173745103000302810053683821765033166213128126464
BC3 = 2429608192173745103170443993514153053496720469929012232192
Here is the correct value:
BC4 = 24296081921737451032703898385767507193022226061986 31438800
Your 0.0625 is correct for 4 consecutive heads in 4 tosses. There might be a bug somewhere. Maybe by fixing that, the program will work for these simpler cases.
Sincerely,
Cac
Luck is what happens when preparation meets opportunity.
I can pretty much reproduce these results. For now stick to the case where win = 50%, loss = 50% as in the case of a coin flip.
Last entry is to show method of counting.Code:streak of 20 heads in 7000 trials: 0.0016648769378662109 streak of 5 heads in 7000 trials: 54.671875000000000 streak of 5 heads in 1000 trials: 7.7968750000000000 streak of 5 heads in 100 trials: 0.76562500000000000 streak of 1 head in 4 trials: 0.75000000000000000
There are 16 possible sequences of 4 coin flips, each with probability of 1/16. Each sequence has either 0, 1, or 2 streaks of 1 head. Following are the sequences and number of streaks of 1,2,3,4 heads in each.
Streaks of exactly 1,2,3,4 heads can sum to > 1 because it is possible there are multiple streaks in the same iteration. Streaks of >= a given streak length cannot sum to more than 1 because each iteration contributes a value of either 1 or 0.Code:tttt 0,0,0,0 thtt 1,0,0,0 httt 1,0,0,0 hhtt 0,1,0,0 ttth 1,0,0,0 thth 2,0,0,0 htth 2,0,0,0 hhth 1,1,0,0 ttht 1,0,0,0 thht 0,1,0,0 htht 2,0,0,0 hhht 0,0,1,0 tthh 0,1,0,0 thhh 0,0,1,0 hthh 1,1,0,0 hhhh 0,0,0,1 streak of 1 head: (1/16 * 7 * 0) + (1/16 * 6 * 1) + (1/16 * 3 * 2) = 12/16 = .75 streak of 2 heads: (1/16 * 11 * 0) + (1/16 * 5 * 1) = 5/16 = .3125 streak of 3 heads: (1/16 * 14 * 0) + (1/16 * 2 * 1) = 2/16 = .125 streak of 4 heads: (1/16 * 15 * 0) + (1/16 * 1 * 1) = 1/16 = .0625
streak of 1 or more = (1/16 * 1 * 0) + (1/16 * 15 * 1) = 15/16
streak of 2 or more = (1/16 * 8 * 0) + (1/16 * 8 * 1) = 8/16
streak of 3 or more = (1/16 * 13 * 0) + (1/16 * 3 * 1) = 3/16
streak of 4 or more = (1/16 * 15 * 0) + (1/16 * 1 * 1) = 1/16
k_c
Last edited by k_c; 05-10-2024 at 07:40 AM.
"Perfection is the enemy of success."
-Elon Musk-
Actually, they should add up to 1. For the case of 4 flips of a fair coin:streak of 1 head: (1/16 * 7 * 0) + (1/16 * 6 * 1) + (1/16 * 3 * 2) = 12/16 = .75
streak of 2 heads: (1/16 * 11 * 0) + (1/16 * 5 * 1) = 5/16 = .3125
streak of 3 heads: (1/16 * 14 * 0) + (1/16 * 2 * 1) = 2/16 = .125
streak of 4 heads: (1/16 * 15 * 0) + (1/16 * 1 * 1) = 1/16 = .0625
Streaks of exactly 1,2,3,4 heads can sum to > 1 because it is possible there are multiple streaks in the same iteration. Streaks of >= a given streak length cannot sum to more than 1 because each iteration contributes a value of either 1 or 0.
streak of 0 heads = 0.0625
streak of 1 heads = 0.4375
streak of 2 heads = 0.3125
streak of 3 heads = 0.1250
streak of 4 heads = 0.0625
Sincerely,
Cac
Luck is what happens when preparation meets opportunity.
Bookmarks