Supposing the following experiment : Execute the same bernoulli trial (with the probability of success P) N number of times
I need the following information : All the possible sequences of success/failure with its probability to happen.
Example : A Bernouilli experiment with a probability of success P = 40% executed 3 times would yield the following results (S is a success, F is a failure) :
FFF 0.216
SFF 0.144
FSF 0.144
SSF 0.096
FFS 0.144
SFS 0.096
FSS 0.096
SSS 0.064
I tried to bruteforce it to obtain the results, but it chokes rapidly with only N = 25, I get an OutOfMemoryException...
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace ConsoleApplication
{
class Program
{
static Dictionary<string, double> finalResultProbabilities = new Dictionary<string, double>();
static void Main(string[] args)
{
// OutOfMemoryException if I set it to 25 :(
//var nbGames = 25;
var nbGames = 3;
var probabilityToWin = 0.4d;
CalculateAverageWinningStreak(string.Empty, 1d, nbGames, probabilityToWin);
// Do something with the finalResultProbabilities data...
}
static void CalculateAverageWinningStreak(string currentResult, double currentProbability, int nbGamesRemaining, double probabilityToWin)
{
if (nbGamesRemaining == 0)
{
finalResultProbabilities.Add(currentResult, currentProbability);
return;
}
CalculateAverageWinningStreak(currentResult + "S", currentProbability * probabilityToWin, nbGamesRemaining - 1, probabilityToWin);
CalculateAverageWinningStreak(currentResult + "F", currentProbability * (1 - probabilityToWin), nbGamesRemaining - 1, probabilityToWin);
}
}
}
I need to be able to support up to N = 3000 in a timely manner (obtaining the result in less than 3 seconds for any P)
Is there a mathematical way to do this optimally?