在Mathematica中进行概率实验
Mathematica提供了一个非常舒适的框架来处理概率和分布,并且-尽管已经解决了适当限制的主要问题-我想使用这个问题来使这个问题更清楚,并且可能作为参考。
让我们简单地使实验可重复,并定义一些符合我们口味的绘图选项:
SeedRandom["Repeatable_151115"];
$PlotTheme = "Detailed";
SetOptions[Plot, Filling -> Axis];
SetOptions[DiscretePlot, ExtentSize -> Scaled[0.5], PlotMarkers -> "Point"];
使用参数分布
现在,我们可以定义渐近分布一个事件是比例的头一(公平)硬币抛出:πn
distProportionTenCoinThrows = With[
{
n = 10, (* number of coin throws *)
p = 1/2 (* fair coin probability of head*)
},
(* derive the distribution for the proportion of heads *)
TransformedDistribution[
x/n,
x \[Distributed] BinomialDistribution[ n, p ]
];
With[
{
pr = PlotRange -> {{0, 1}, {0, 0.25}}
},
theoreticalPlot = DiscretePlot[
Evaluate @ PDF[ distProportionTenCoinThrows, p ],
{p, 0, 1, 0.1},
pr
];
(* show plot with colored range *)
Show @ {
theoreticalPlot,
DiscretePlot[
Evaluate @ PDF[ distProportionTenCoinThrows, p ],
{p, 0.4, 0.6, 0.1},
pr,
FillingStyle -> Red,
PlotLegends -> None
]
}
]
这给出了比例离散分布的图:
我们可以立即使用分布来计算和:Pr[0.4≤π≤0.6|π∼B(10,12)]Pr[0.4<π<0.6|π∼B(10,12)]
{
Probability[ 0.4 <= p <= 0.6, p \[Distributed] distProportionTenCoinThrows ],
Probability[ 0.4 < p < 0.6, p \[Distributed] distProportionTenCoinThrows ]
} // N
{0.65625,0.246094}
做蒙特卡洛实验
我们可以使用一个事件的分布来重复采样(蒙特卡洛)。
distProportionsOneMillionCoinThrows = With[
{
sampleSize = 1000000
},
EmpiricalDistribution[
RandomVariate[
distProportionTenCoinThrows,
sampleSize
]
]
];
empiricalPlot =
DiscretePlot[
Evaluate@PDF[ distProportionsOneMillionCoinThrows, p ],
{p, 0, 1, 0.1},
PlotRange -> {{0, 1}, {0, 0.25}} ,
ExtentSize -> None,
PlotLegends -> None,
PlotStyle -> Red
]
]
将其与理论/渐近分布进行比较表明,一切都非常适合:
Show @ {
theoreticalPlot,
empiricalPlot
}