我确信这看起来很丑,但它完成了我想要的美观工作。我得到的错误是一些块没有关闭,但我不知道如何纠正它。
这是我的乳胶:
\begin{algorithm}[H]
\caption{LSM Algorithm}\label{euclid}
\begin{algorithmic}
\State Generate $M$ paths of stock prices $S_i(t)$, $i = 1,\ldots,M$
\State which evolves in discreet time index $j = 1,\ldots, N$ (time interval $\Delta t = \frac{T}{N}$)
\For{$i$}
\For{$j$}
\State Generate $S_{i} = S_{i-1}e^{(r-q-\frac{\sigma^2}{2})\Delta t + \sigma\sqrt{\Delta t}Z_{i,j}}, Z_{i,j}\sim N(0,1)$
\EndFor
\EndFor
\ \ \ \textbf{end for}\\
\textbf{end for}
\State Put $P_i \gets f(S_{i}(t_N))$ for all $i$
\For{$t$ from $t_{N-1}$ to $t_1$}\\
\ \ \ Find in the money paths $\{i_1,i_2,\ldots, i_n\}$ such that $f(S_i(t)) > 0$\\
\ \ \ Set $ipaths \gets \{i_1,i_2,\ldots, i_n\}$\\
\ \ \ Set $x_i\gets S_i(t)$ and $y_i\gets e^{-r\Delta t}P_i$ for $i\in ipaths$\\
\ \ \ Apply regression on $x,y$ to obtain regression coefficients $\hat{\beta}_0,\ldots, \hat{\beta}_k$\\
\ \ \ Estimate continuation values $\hat{C}(S_i(t))$ then calculate the value of immediate exercise $f(S_i(t))$\\ \ \ \ for $i\in ipaths$
\For{$i$}
\If{$i\in ipaths$ \textbf{and} $f(S_i(t)) > \hat{C}(S_i(t))$ }
\State $P_i\gets f(S_i(t))$\\
\ \ \ \ \ \ \ \ \ \textbf{else}\\
\State $P_i\gets e^{-r\Delta t}P_i$\\
\EndIf
\EndFor
\ \ \ \textbf{end if}\\
\ \ \ \textbf{end for}\\
\textbf{end for}\\
$price\gets \frac{1}{M}\sum_{i=1}^{M}e^{-r\Delta t}P_i$
\end{algorithmic}
\end{algorithm}
答案1
您缺少一个\EndFor
,这就是出现错误消息的原因。
无论如何,你的代码是真的很丑,我想说一团糟......
可能,下面的代码就是您想要的。
请注意如何使用\MyFor
和\EndMyFor
来获得for
无do
。
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\algdef{SE}{MyFor}{EndMyFor}[1]{%
\textbf{for} #1}{%
\textbf{end for}}
\begin{document}
\begin{algorithm}[H]
\caption{LSM Algorithm}\label{euclid}
\begin{algorithmic}
\State Generate $M$ paths of stock prices $S_i(t)$, $i = 1,\ldots,M$
\State which evolves in discreet time index $j = 1,\ldots, N$ (time interval $\Delta t = \frac{T}{N}$)
\For{$i$}
\For{$j$}
\State Generate $S_{i} = S_{i-1}e^{(r-q-\frac{\sigma^2}{2})\Delta t + \sigma\sqrt{\Delta t}Z_{i,j}}, Z_{i,j}\sim N(0,1)$
\EndFor
\EndFor
\State Put $P_i \gets f(S_{i}(t_N))$ for all $i$
\For{$t$ from $t_{N-1}$ to $t_1$}
\State Find in the money paths $\{i_1,i_2,\ldots, i_n\}$ such that $f(S_i(t)) > 0$
\State Set $ipaths \gets \{i_1,i_2,\ldots, i_n\}$
\State Set $x_i\gets S_i(t)$ and $y_i\gets e^{-r\Delta t}P_i$ for $i\in ipaths$
\State Apply regression on $x,y$ to obtain regression coefficients $\hat{\beta}_0,\ldots, \hat{\beta}_k$
\State Estimate continuation values $\hat{C}(S_i(t))$ then calculate the value of immediate exercise $f(S_i(t))$
\MyFor{$i\in ipaths$}
\For{$i$}
\If{$i\in ipaths$ \textbf{and} $f(S_i(t)) > \hat{C}(S_i(t))$}
\State $P_i\gets f(S_i(t))$
\Else
\State $P_i\gets e^{-r\Delta t}P_i$
\EndIf
\EndFor
\EndMyFor
\EndFor
\State $price\gets \frac{1}{M}\sum_{i=1}^{M}e^{-r\Delta t}P_i$
\end{algorithmic}
\end{algorithm}
\end{document}