附录 感谢薛定谔猫的答案,我现在能够制作下面的图表。它表明,如果您不想在进一步的模拟中低估低概率事件,您确实需要模拟大量的均匀数……
原始问题 我想用图形展示蒙特卡罗方法的直觉。这个想法是,你使用的随机数越多,下面的模拟分布就越丰富。一旦你达到足够大的数字(500?1000?更多?),这些模拟分布就不会出现任何“漏洞”,可以用来模拟其他过程。
这些标记是每个初始随机数首先水平投影在累积值上,然后垂直投影在密度上。我在这个例子中使用了 40 个随机数,并突出显示了 4 个彩色点,以显示投影顺序。
迄今为止
- 我有 [0,1] 之间的随机数
- 我(想)将它们投影到累积正态分布的 y 轴上
- 它对应于正常密度上的一个点。
MWE 没有连接点,也没有使随机数和它们在图表上的投影相对应。可能是因为我需要逆正态累积,但基于公式 8https://core.ac.uk/download/pdf/41787448.pdf给出了累积正态分布的最佳逻辑拟合的简单表达式:
$\phi(z) \approx \frac{1}{(1 + e^{-1.702z})}$
%%(我在 MWE 中使用了更准确的表达式,但它对于示例来说已经足够了。逆: $z(\phi) \approx - \frac{ln (\frac{1}{\phi}-1)}{1.702}$
到目前为止,MWE 如下,基于对数正态分布的教学图
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots,fillbetween}
\def\m{0}
\def\SIG{1}
\def\NumRand{50}
\begin{document}
\begin{tikzpicture}[declare function={
g(\x)= 1/(sqrt(2*pi))*exp(-0.5*(pow((\x-\m),2))/(2*\SIG^2));
h(\x)=1/(1 + exp(-0.07056*((\x-\m)/\SIG)^3 - 1.5976*(\x-\m)/\SIG));
}]
\begin{groupplot}[group style={
group size=2 by 2, horizontal sep=0pt, vertical sep=0pt,
xticklabels at=edge bottom}, legend pos=south east,
% grid=both
]
\nextgroupplot[group/empty plot]
%---- top right -------------------
\nextgroupplot[]
\addplot[name path=BL1,only marks,very thick,color=red,domain=-4:4,samples=\NumRand] ({x},{g(x)});
\addlegendentry{$\mathcal{N}(0,1)$}
%---- bottom left -------------------
\nextgroupplot
\addplot+[only marks,fill=blue!60, opacity= 0.5, samples=\NumRand,domain=-0.1:0.1] (0,rnd);
\addlegendentry{Uniform random numbers}
%---- bottom right -------------------
\nextgroupplot[]
\addplot[name path=BR1,only marks, color=red!50, domain=-4:4, samples=\NumRand] ({x},{h(x)});
\addlegendentry{Normal cumulative}
\end{groupplot}
\end{tikzpicture}
\end{document}
参考文章详细讨论了累积正态表达式的准确性。那里有更准确的表达式,但它们不太容易反转,并且需要更多的计算开销。这个可以用于编码目的,当您想要从正态分布中生成“随机”样本时
答案1
编辑:您可能会问这个:
\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}[declare function={
g(\x,\m,\SIG)= 1/(sqrt(2*pi))*exp(-0.5*(pow((\x-\m),2))/(2*\SIG^2));
h(\x,\m,\SIG)=1/(1 + exp(-0.07056*((\x-\m)/\SIG)^3 - 1.5976*(\x-\m)/\SIG));
phi(\z)=1/(1+exp(-1.702*\z));
z(\phi)=-ln((1-\phi)/\phi)/1.702;
}]
\edef\m{0}
\edef\SIG{1}
\edef\NumRand{50}
\newcommand\RandDist[1]{\edef\irun{0}%
\pgfmathsetmacro{\mysum}{0}%
\edef\lstcoords{}%
\edef\lstcm{}%
\edef\lstgf{}%
\loop
\pgfmathsetmacro{\myrnd}{rnd}%
\pgfmathsetmacro{\mysum}{\mysum+\myrnd}%
\edef\lstcoords{\lstcoords (#1,\myrnd)}%
\pgfmathsetmacro{\myz}{z(\myrnd)}%
\edef\lstcm{\lstcm (\myz,\myrnd)}%
\pgfmathsetmacro{\myg}{g(\myz,\m,\SIG)}%
\edef\lstgf{\lstgf (\myz,\myg)}%
\edef\irun{\the\numexpr\irun+1}%
\ifnum\irun<\NumRand\relax
\repeat
}
\RandDist{0}
\begin{groupplot}[group style={
group size=2 by 2, horizontal sep=0pt, vertical sep=0pt,
xticklabels at=edge bottom}, legend pos=south east,
% grid=both
]
\nextgroupplot[group/empty plot]
%---- top right -------------------
\nextgroupplot[]
\addplot[forget plot,very thick,color=red,domain=-4:4,samples=\NumRand+1] ({x},{g(x,\m,\SIG)});
\addplot[only marks,very thick,color=red]
coordinates {\lstgf};
\addlegendentry{$\mathcal{N}(0,1)$}
%---- bottom left -------------------
\nextgroupplot
\addplot+[only marks,fill=blue!60, opacity= 0.5]
coordinates {\lstcoords};
\addlegendentry{Uniform random numbers}
%---- bottom right -------------------
\nextgroupplot[]
\addplot[forget plot,very thick,color=red, domain=-4:4, samples=\NumRand+1] ({x},{h(x,\m,\SIG)});
%\addplot[orange, domain=-4:4,]({x},{phi(x)});
\addplot[only marks,fill=red!50] coordinates {\lstcm};
\addlegendentry{Normal cumulative}
\end{groupplot}
\end{tikzpicture}
\end{document}
您可以为其制作动画。
\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgfplotslibrary{groupplots}
\tikzset{declare function={
g(\x,\m,\SIG)= 1/(sqrt(2*pi))*exp(-0.5*(pow((\x-\m),2))/(2*\SIG^2));
h(\x,\m,\SIG)=1/(1 + exp(-0.07056*((\x-\m)/\SIG)^3 - 1.5976*(\x-\m)/\SIG));
phi(\z)=1/(1+exp(-1.702*\z));
z(\phi)=-ln((1-\phi)/\phi)/1.702;
}}
\begin{document}
\begingroup
\edef\m{0}
\edef\SIG{1}
\edef\NumRand{50}
\newcommand\RandDist[1]{\edef\irun{0}%
\pgfmathsetmacro{\mysum}{0}%
\edef\lstcoords{}%
\edef\lstcm{}%
\edef\lstgf{}%
\loop
\pgfmathsetmacro{\myrnd}{rnd}%
\pgfmathsetmacro{\mysum}{\mysum+\myrnd}%
\edef\lstcoords{\lstcoords (#1,\myrnd)}%
\pgfmathsetmacro{\myz}{z(\myrnd)}%
\edef\lstcm{\lstcm (\myz,\myrnd)}%
\pgfmathsetmacro{\myg}{g(\myz,\m,\SIG)}%
\edef\lstgf{\lstgf (\myz,\myg)}%
\edef\irun{\the\numexpr\irun+1}%
\ifnum\irun<\NumRand\relax
\repeat
}
\RandDist{0}
\pgfplotsinvokeforeach{1,...,\NumRand}{\begin{tikzpicture}
\begin{groupplot}[group style={
group size=2 by 2, horizontal sep=0pt, vertical sep=0pt,
xticklabels at=edge bottom}, legend pos=south east,
% grid=both
]
\nextgroupplot[group/empty plot]
%---- top right -------------------
\nextgroupplot[]
\addplot[forget plot,very thick,color=red,domain=-4:4,samples=\NumRand+1] ({x},{g(x,\m,\SIG)});
\addplot[only marks,very thick,color=red,
x filter/.expression={(\coordindex >#1 ? nan : x)}]
coordinates {\lstgf};
\addlegendentry{$\mathcal{N}(0,1)$}
%---- bottom left -------------------
\nextgroupplot
\addplot+[only marks,fill=blue!60, opacity= 0.5,
x filter/.expression={(\coordindex >#1 ? nan : x)}]
coordinates {\lstcoords};
\addlegendentry{Uniform random numbers}
%---- bottom right -------------------
\nextgroupplot[]
\addplot[forget plot,very thick,color=red, domain=-4:4, samples=\NumRand+1] ({x},{h(x,\m,\SIG)});
%\addplot[orange, domain=-4:4,]({x},{phi(x)});
\addplot[only marks,fill=red!50,
x filter/.expression={(\coordindex >#1 ? nan : x)}] coordinates {\lstcm};
\addlegendentry{Normal cumulative}
\end{groupplot}
\end{tikzpicture}}
\endgroup
\end{document}
但我对其解释并不确定。
原始答案:这可能忽略了本练习的重点。它所做的就是生成一组随机分布的点,计算它们的平均值并绘制平均值的分布。并且它会改变动画中的集合数量。
\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgfplotslibrary{groupplots,fillbetween}
\begin{document}
\foreach \X in {4,8,...,80}
{\begin{tikzpicture}
\edef\NumRand{50}
\edef\NumSamples{\X}
\edef\NumBins{25}
\edef\irun{0}%
\loop
\expandafter\edef\csname NumBin\irun\endcsname{0}%
\edef\irun{\the\numexpr\irun+1}%
\ifnum\irun<\NumBins\relax
\repeat
\newcommand\RandDist[1]{\edef\irun{0}%
\pgfmathsetmacro{\mysum}{0}%
\edef\lstcoords{}%
\loop
\pgfmathsetmacro{\myrnd}{rnd}%
\pgfmathsetmacro{\mysum}{\mysum+\myrnd}%
\edef\lstcoords{\lstcoords (##1,\myrnd)}%
\edef\irun{\the\numexpr\irun+1}%
\ifnum\irun<\NumRand\relax
\repeat
}
\pgfplotsforeachungrouped\isample in{0,...,\the\numexpr\NumSamples-1}
{\pgfmathsetmacro{\xsample}{2*\isample/\NumSamples-1}%
\RandDist{\xsample}%
\expandafter\edef\csname lstpst\isample\endcsname{\lstcoords}%
\pgfmathsetmacro{\avg}{\mysum/\NumRand}%
\expandafter\edef\csname avg\isample\endcsname{(\xsample,\avg)}%
\pgfmathtruncatemacro{\nBin}{25*\avg}%
\edef\currbin{\csname NumBin\nBin\endcsname}%
\expandafter\edef\csname NumBin\nBin\endcsname{\the\numexpr\currbin+1}%
}
\edef\lstbars{}%
\edef\irun{0}%
\loop
\edef\lstbars{\lstbars (\irun,\csname NumBin\irun\endcsname)}%
\edef\irun{\the\numexpr\irun+1}%
\ifnum\irun<\NumBins\relax
\repeat
%\typeout{\lstcoords,\mysum,\lstbars}
\begin{groupplot}[group style={
group size=2 by 2, horizontal sep=2em, vertical sep=0pt,
xticklabels at=edge bottom}, legend pos=south east,
% grid=both
]
\nextgroupplot[title=samples]
\edef\temp{\noexpand\addplot[only marks,mark=*,fill=blue!60, opacity= 0.5]
coordinates {\csname lstpst0\endcsname};
\noexpand\addlegendentry{samples}
\noexpand\addplot[only marks,mark=square*,fill=red!60]
coordinates {\csname avg0\endcsname};
\noexpand\addlegendentry{average}}
\temp
\pgfplotsinvokeforeach{1,...,\the\numexpr\NumSamples-1}
{\edef\temp{\noexpand\addplot[forget plot,only marks,mark=*,fill=blue!60, opacity= 0.5]
coordinates {\csname lstpst##1\endcsname};
\noexpand\addplot[forget plot,only marks,mark=square*,fill=red!60]
coordinates {\csname avg##1\endcsname};}
\temp}
% \addlegendentry{Uniform random numbers}
%---- top right -------------------
\nextgroupplot[title=distribution of averages,
xtick={0,...,\NumBins},xticklabel=\empty]
\addplot[ybar,bar width=pi*1pt,fill=blue] coordinates{\lstbars};
%---- bottom left -------------------
\end{groupplot}
\end{tikzpicture}}
\end{document}