1

I am sure this is looks ugly but it got the job done for what I wanted it to look like aesthetically. The error I am getting is that some blocks are not closed, but I am not sure how to correct it.

Here is my latex:

\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}
5

You are missing an \EndFor, that's why the error message.

In any case, your code is really ugly, I would say a mess...

Probably, the following code is what you are after.

Please note how \MyFor and \EndMyFor are used to obtain a for without 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} 

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.