假设以下实验:执行相同的伯努利试验(成功概率为 P)N 次
我需要以下信息:所有可能的成功/失败序列及其发生概率。
示例:成功概率 P = 40% 执行 3 次的伯努利实验将产生以下结果(S 为成功,F 为失败):
FFF 0.216
SFF 0.144
FSF 0.144
SSF 0.096
FFS 0.144
SFS 0.096
FSS 0.096
不锈钢 0.064
我试图暴力破解它以获得结果,但它仅在 N = 25 的情况下迅速窒息,我得到了 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);
}
}
}
我需要能够及时支持最多 N = 3000(任何 P 不到 3 秒即可获得结果)
有没有一种数学方法可以最佳地做到这一点?