我发现这个布朗运动及其在特定时间的相关正态分布的表示很有趣,并且(我相信)很有教育意义。
Tikz 布朗运动解释如何绘制布朗运动以及旋转正态分布解释如何绘制旋转的法线。
我加入下面的 MWE。以我的水平,将图表和分布中心匹配起来太手动了。有没有办法将分布和布朗运动联系起来,以便显示布朗运动在 $\sqrt(T)$ 中如何增长。结果,$T=100$ 上的图表显示的分布比 $T=400$ 上的分布更窄?
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
%Brownian motion
\newcommand{\BM}[5]{
% points, advance, rand factor, options, end label
\draw[#4] (0,0)
\foreach \x in {1,...,#1}
{ -- ++(#2,rand*#3)
}
node[right] {#5};
}
\begin{tikzpicture}[
declare function= {gauss(\x,\y,\z)=\offset+1/(\y*sqrt(2*pi))*exp(-((\x-\z)^2)/(2*\y^2));}]
\pgfmathsetseed{17}
\draw[help lines] (0,-5) grid (15,5);
\BM{100}{0.02}{0.2}{red}{$BM_1$};
\draw (2,-5) -- (2,5) coordinate[pos=0.6](x2) coordinate[pos=0.5] (y2);
\draw[-latex] (2,0) -- (4,0) node[below left,rotate=-90]{};
\BM{600}{0.02}{0.2}{blue}{$BM_3$}
\end{tikzpicture}
\end{document}
谢谢!
答案1
试图清理。这表明(我认为)我们在这次聊天。答案有两种:
- 一个更无聊的版本,其中高斯只是向右移动并且变得更宽。
- 一个更有趣的版本,其中高斯函数遵循一条路径。(没有人声称这个版本有明确的物理解释,但它可以产生更有趣的动画。;-)
让我们从无聊的版本开始。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
%Brownian motion
\newcommand{\BM}[5]{
% points, advance, rand factor, options, end label
\draw[#4] (0,0)
\foreach \x in {1,...,#1}
{ -- ++(#2,rand*#3) coordinate (aux-\x) % <- added coordinate names
}
node[right] {#5};
}
\newcommand{\AddHorizontalGauss}[3][]{
\draw[#1] let \p1=(aux-#2),\p2=(aux-#3),\n1={0.5*sqrt((\x2-\x1)*1pt/1cm)},
\n2={3*\n1},\n3={0.895*\n2} in \pgfextra{\pgfmathsetmacro{\ymax}{\n2}
\pgfmathsetmacro{\ynext}{\n3}}
plot[variable=\z,domain=-\ymax:\ymax,samples=101]
({\x2+3*gauss(\z,\n1,0)*1cm},{\y1+\z*1cm})
($(\x2,\y1)+(0,-\ymax)$) -- ($(\x2,\y1)+(0,\ymax)$)
foreach \X in {-\ymax,-\ynext,...,\ymax}
{ (\p1) -- ($(\x2,\y1)+({0*gauss(\X,\n1,0)},\X*1cm)$)};
}
\begin{tikzpicture}[
declare function={gauss(\x,\y,\z)=1/(\y*sqrt(2*pi))*exp(-((\x-\z)^2)/(2*\y^2));}]
\pgfmathsetseed{17}
\draw[help lines] (0,-8) grid (15,8);
\BM{100}{0.02}{0.2}{red}{$BM_1$};
\draw (2,-5) -- (2,5) coordinate[pos=0.6](x2) coordinate[pos=0.5] (y2);
\draw[-latex] (2,0) -- (4,0) node[below left,rotate=-90]{};
\BM{600}{0.02}{0.2}{blue}{$BM_3$}
\AddHorizontalGauss{100}{510}
\end{tikzpicture}
\end{document}
这可以做成动画。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
%Brownian motion
\newcommand{\BM}[5]{
% points, advance, rand factor, options, end label
\draw[#4] (0,0)
\foreach \x in {1,...,#1}
{ -- ++(#2,rand*#3) coordinate (aux-\x) % <- added coordinate names
}
node[right] {#5};
}
\newcommand{\AddHorizontalGauss}[3][]{
\draw[#1] let \p1=(aux-#2),\p2=(aux-#3),\n1={0.5*sqrt((\x2-\x1)*1pt/1cm)},
\n2={3*\n1},\n3={0.895*\n2} in \pgfextra{\pgfmathsetmacro{\ymax}{\n2}
\pgfmathsetmacro{\ynext}{\n3}}
plot[variable=\z,domain=-\ymax:\ymax,samples=101]
({\x2+3*gauss(\z,\n1,0)*1cm},{\y1+\z*1cm})
($(\x2,\y1)+(0,-\ymax)$) -- ($(\x2,\y1)+(0,\ymax)$)
foreach \X in {-\ymax,-\ynext,...,\ymax}
{ (\p1) -- ($(\x2,\y1)+({0*gauss(\X,\n1,0)},\X*1cm)$)};
}
\foreach \Z in {120,130,...,540}
{\begin{tikzpicture}[
declare function={gauss(\x,\y,\z)=1/(\y*sqrt(2*pi))*exp(-((\x-\z)^2)/(2*\y^2));}]
\pgfmathsetseed{17}
\draw[help lines] (0,-8) grid (15,8);
\BM{100}{0.02}{0.2}{red}{$BM_1$};
\draw (2,-5) -- (2,5) coordinate[pos=0.6](x2) coordinate[pos=0.5] (y2);
\draw[-latex] (2,0) -- (4,0) node[below left,rotate=-90]{};
\BM{600}{0.02}{0.2}{blue}{$BM_3$}
\AddHorizontalGauss{100}{\Z}
\end{tikzpicture}}
\end{document}
请注意,我还没有计算方差的前因子。这只是一个动画。但此代码将允许那些拥有真正的随机游走问题并计算前因子的人制作更逼真的动画。
现在来看看共动高斯。(为了使答案合理“简短”,我只展示了动画。)
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
%Brownian motion
\newcommand{\BM}[5]{
% points, advance, rand factor, options, end label
\draw[#4] (0,0)
\foreach \x in {1,...,#1}
{ -- ++(#2,rand*#3) coordinate (aux-\x) % <- added coordinate names
}
node[right] {#5};
}
\newcommand{\AddComovingGauss}[3][]{
\draw[#1] let \p1=(aux-#2),\p2=(aux-#3),\n1={0.5*sqrt((\x2-\x1)*1pt/1cm)},
\n2={3*\n1},\n3={0.89*\n2} in \pgfextra{\pgfmathsetmacro{\ymax}{\n2}
\pgfmathsetmacro{\ynext}{\n3}}
plot[variable=\z,domain=-\ymax:\ymax,samples=101]
({\x2+3*gauss(\z,\n1,0)*1cm},{\y2+\z*1cm})
($(\p2)+(0,-\ymax)$) -- ($(\p2)+(0,\ymax)$)
foreach \X in {-\ymax,-\ynext,...,\ymax}
{ (\p1) -- ($(\p2)+({0*gauss(\X,\n1,0)},\X*1cm)$)};
}
\foreach \Z in {120,130,...,540}
{\begin{tikzpicture}[
declare function={gauss(\x,\y,\z)=1/(\y*sqrt(2*pi))*exp(-((\x-\z)^2)/(2*\y^2));}]
\pgfmathsetseed{17}
\draw[help lines] (0,-8) grid (15,8);
\BM{100}{0.02}{0.2}{red}{$BM_1$};
\draw (2,-5) -- (2,5) coordinate[pos=0.6](x2) coordinate[pos=0.5] (y2);
\draw[-latex] (2,0) -- (4,0) node[below left,rotate=-90]{};
\BM{600}{0.02}{0.2}{blue}{$BM_3$}
\AddComovingGauss{100}{\Z}
\end{tikzpicture}}
\end{document}
我也知道可以让代码更短一些。这是为了让代码更容易理解。