I start writing my note in Latex and I am trying to write a pseudo code based on this equation:

equation

Here is my code I have tried so far.

\begin{addmargin}[10mm]{0mm}\textbf{Repeat until convergence:} \ \{ \[\theta_j := \theta_j + \alpha \sum_{i=1}^m(y^{(i)}-h_\theta(x^{(i)})) \ x_j^{(i)} \quad (\text{for every} \ j).\]\}\end{addmargin}

I still think it is a bit messy. Do you have an easier way to write this?

2

Best Answer


This question should probably be moved to tex.stackexchange.


There exists several different neat Latex packages specifically with the purpose of writing pseudocode. One of the more simple ways to do it is by using the Algorithmicx package (package documentation here), specifically the algpseudocode part of it.

Here is an MWE for your pseudocode using Algoritmicx:

\documentclass{article}\usepackage{amsmath}\usepackage{algpseudocode}\renewcommand{\algorithmicforall}{\textbf{for each}}\begin{document}\noindent Let $\mathcal{J} := \{1, ..., n\}$.\begin{algorithmic}\While {Some convergence criteria is not fulfilled ...}\ForAll {$j \in \mathcal{J}$}\State $\theta_j \gets \theta_j + \alpha \sum_{i=1}^m(y^{(i)}-h_\theta(x^{(i)})) \ x_j^{(i)}$\EndFor \State {... update some convergence measure} \Comment{Describe convergence measure}\EndWhile \end{algorithmic}\end{document}

Note that, at the fourth non-empty line, I've overloaded the text of \ForAll command from algorithmicx (\algorithmicforall), to become "for ... each" rather than "for ... all".

Note also that, generally, only comments may contain text in pseudocode, so the sentences "Some convergence criteria is not fulfilled ..." and "... update some convergence measure" above should be replaced with however your define convergence of algorithm/equation.

Here is what you want, from the packages algorithm and algorithmic

\begin{algorithm}\caption{Gradient Descent}\label{alg-gd}\begin{algorithmic}[1]\STATE Initialize \( \theta_{0} = k\in \R^d \)\STATE Initialize \( t \leftarrow 0 \)\REPEAT\STATE \( \forall j \in \{1,..., d\}, \theta_{j,t+1} \leftarrow \theta_{j,t} - \alpha \frac{\p }{\p \theta_{j} } L(\theta )\bigg|_{\theta = \theta_{t} } \)\STATE \( t \leftarrow t + 1 \)\UNTIL{convergence}

which gives,

enter image description here

Note: if you are using it in beamer, put \begin{algorithm}[H] in the line above instead of just \begin{algorithm}