創建具有許多軌跡(包括期望值)的布朗運動圖

創建具有許多軌跡(包括期望值)的布朗運動圖

我正在嘗試複製 Bernt Øksendal 教科書封面上的布朗運動圖隨機微分方程使用乳膠。

在此輸入影像描述

我在編碼中有點迷失了。不需要數學。我查過類似的主題(布朗運動 - 重複),但繪圖上都沒有 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

問題是randin確實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}

在此輸入影像描述

相關內容