Advertisement
Guest User

probability of streak of K successes in N trials

a guest
Nov 10th, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include <vector>
  2. #include <cmath>
  3.  
  4. // this function adapted from
  5. // http://www.askamathematician.com/2010/07/q-whats-the-chance-of-getting-a-run-of-k-successes-in-n-bernoulli-trials-why-use-approximations-when-the-exact-answer-is-known/
  6. double probOfStreak(int numCoins, int minHeads, double headProb)
  7. {
  8.     std::vector<double> memo(numCoins+1);
  9.  
  10.     for (int i = 0; i < minHeads; ++i)
  11.     {
  12.         memo[i] = 0;
  13.     }
  14.  
  15.     for (int i = minHeads; i <= numCoins; ++i)
  16.     {
  17.         double result = std::pow(headProb, minHeads);
  18.         for (int firstTail = 1; firstTail <= minHeads; ++firstTail)
  19.         {
  20.             double pr = memo[i - firstTail];
  21.             result += std::pow(headProb, firstTail-1) * (1 - headProb) * pr;
  22.         }
  23.         memo[i] = result;
  24.     }
  25.  
  26.     return memo[numCoins];
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement