![创建包含预期值在内的多条轨迹的布朗运动图](https://linux22.com/image/472837/%E5%88%9B%E5%BB%BA%E5%8C%85%E5%90%AB%E9%A2%84%E6%9C%9F%E5%80%BC%E5%9C%A8%E5%86%85%E7%9A%84%E5%A4%9A%E6%9D%A1%E8%BD%A8%E8%BF%B9%E7%9A%84%E5%B8%83%E6%9C%97%E8%BF%90%E5%8A%A8%E5%9B%BE.png)
我正在尝试复制 Bernt Øksendal 教科书封面上的布朗运动图随机微分方程使用 LaTeX。
我对编码有点迷茫。不需要数学。我查过一个类似的主题(布朗运动 - 重复),但这些都没有在图中出现 E[X_t] 线。另外,我不确定如何校正布朗运动轨迹(因为其中一些轨迹变为负值,与教科书封面图不同)以及 x 轴和 y 轴的标记。
我的尝试
\documentclass[12pt, a4paper]{article} %uploading the libraries
\usepackage{pgfplots, pgfplotstable}
\usepackage[font=small,labelfont=bf,labelsep=colon]{caption}
\begin{document}
\pgfmathsetseed{5}
% creating the Brownian motion trajectories
\pgfplotstablenew[
create on use/randwalk1/.style={
create col/expr accum={\pgfmathaccuma + 0.1*rand}{0}
},
create on use/randwalk2/.style={
create col/expr accum={\pgfmathaccuma + 0.1*rand}{0}
},
create on use/randwalk3/.style={
create col/expr accum={\pgfmathaccuma + 0.1*rand}{0}
},
create on use/randwalk4/.style={
create col/expr accum={\pgfmathaccuma + 0.1*rand}{0}
},
create on use/randwalk5/.style={
create col/expr accum={\pgfmathaccuma + 0.1*rand}{0}
},
columns={randwalk1,randwalk2,randwalk3,randwalk4,randwalk5}
]
{700}
% adjusting the plot axes and titles
\loadedtable
\begin{figure}[H]
\caption{Brownian Motion}
\centering
\begin{tikzpicture}
\begin{axis}[
legend pos=outer north east,
axis y line=left,
axis x line=middle,
xlabel= {\scriptsize $t$},
ylabel = {\scriptsize $X_{t}$},
ylabel style = {yshift=-12pt},
xticklabels={,,},
yticklabels={,,},
tick style={draw=none},
line join=bevel,
no markers,
table/x expr={\coordindex/100},
xmin=0,
ymin=-0.5, ymax=3,
enlarge x limits=false
% now, putting it all together
]
\addplot table [y expr={max(\thisrow{randwalk1},-5.0)}] {\loadedtable};
\addplot table [y expr={min(\thisrow{randwalk2},5.0)}] {\loadedtable};
\addplot table [y expr={min(\thisrow{randwalk3},5.0)}] {\loadedtable};
\addplot table [y expr={min(\thisrow{randwalk4},5.0)}] {\loadedtable};
\addplot table [y expr=\thisrow{randwalk5}] {\loadedtable};
\draw (axis cs:5,5) (axis cs:5,-5);
\legend {{\footnotesize $X_t(\omega_1)$}, {\footnotesize $X_t(\omega_2)$}, {\footnotesize $X_t(\omega_3)$},{\footnotesize $X_t(\omega_4)$},{\footnotesize $X_t(\omega_5)$}};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
问题是,rand
在pgf
上确实会生成一个均匀分布的随机值[-1,1]
,而不是正态随机值。因此,严格来说,下面的代码并没有绘制 GBM。但它看起来类似于 GBM 图。您可以看到,我还在0.003
指数中添加了一些漂移,以使轨迹更有可能像原始图一样增加。由于是rand
均匀的,我已经计算E(exp(0.003+0.1*rand))=exp(0.003)*5*(exp(0.1)-exp(-0.1))
并使用此表达式来绘制期望值。
\documentclass[12pt, a4paper]{article} %uploading the libraries
\usepackage{pgfplots, pgfplotstable}
\usepackage[font=small,labelfont=bf,labelsep=colon]{caption}
\begin{document}
\pgfmathsetseed{5}
% creating the Brownian motion trajectories
\pgfplotstablenew[
create on use/randwalk1/.style={
create col/expr accum={\pgfmathaccuma * exp(0.003+0.1*rand)}{1}
},
create on use/randwalk2/.style={
create col/expr accum={\pgfmathaccuma * exp(0.003+0.1*rand)}{1}
},
create on use/randwalk3/.style={
create col/expr accum={\pgfmathaccuma * exp(0.003+0.1*rand)}{1}
},
create on use/randwalk4/.style={
create col/expr accum={\pgfmathaccuma * exp(0.003+0.1*rand)}{1}
},
create on use/randwalk5/.style={
create col/expr accum={\pgfmathaccuma * exp(0.003+0.1*rand)}{1}
},
create on use/expectation/.style={
create col/expr accum={\pgfmathaccuma * exp(0.003)*5*(exp(0.1)-exp(-0.1))}{1}
},
columns={randwalk1,randwalk2,randwalk3,randwalk4,randwalk5,expectation}
]
{700}
% adjusting the plot axes and titles
\loadedtable
\begin{figure}[h]
\caption{Brownian Motion}
\centering
\begin{tikzpicture}
\begin{axis}[
legend pos=outer north east,
axis y line=left,
axis x line=middle,
xlabel={\scriptsize $t$},
ylabel={\scriptsize $X_{t}$},
ylabel style={yshift=-12pt},
xticklabels={,,},
yticklabels={,,},
tick style={draw=none},
line join=bevel,
no markers,
table/x expr={\coordindex/100},
xmin=0,
ymin=0,
enlarge x limits=false
]
\addplot table [y expr=\thisrow{randwalk1}] {\loadedtable};
\addplot table [y expr=\thisrow{randwalk2}] {\loadedtable};
\addplot table [y expr=\thisrow{randwalk3}] {\loadedtable};
\addplot table [y expr=\thisrow{randwalk4}] {\loadedtable};
\addplot table [y expr=\thisrow{randwalk5}] {\loadedtable};
\addplot table [y expr=\thisrow{expectation}] {\loadedtable};
\draw (axis cs:5,5) (axis cs:5,-5);
\legend {{\footnotesize $X_t(\omega_1)$},
{\footnotesize $X_t(\omega_2)$},
{\footnotesize $X_t(\omega_3)$},
{\footnotesize $X_t(\omega_4)$},
{\footnotesize $X_t(\omega_5)$},
{\footnotesize $E[X_t]$}};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}