Skip to content

Instantly share code, notes, and snippets.

@hiroto-takatoshi
Created January 3, 2018 14:48
Show Gist options
  • Save hiroto-takatoshi/7f5490f3ab2f978a1a4ffa8528877611 to your computer and use it in GitHub Desktop.
Save hiroto-takatoshi/7f5490f3ab2f978a1a4ffa8528877611 to your computer and use it in GitHub Desktop.
key algorithm
% encoded in UTF-8
\documentclass[a4paper]{article}
\usepackage{CJKutf8}
\usepackage{amsmath}
\usepackage{lmodern}
\usepackage{algorithm}
\usepackage{algorithmic}
% \Ja{text}: Outputs Japanese text.
\newcommand*{\Ja}[1]{%
\begin{CJK}{UTF8}{gbsn}#1\end{CJK}}
\renewcommand{\algorithmicforall}{\textbf{for each}}
\begin{document}
\begin{algorithm}
\caption{Incrementally update global and local variables (redisio.py)}
\begin{algorithmic}
\STATE {\textbf{Function} $updatePb(p_o,p_j):$}
\STATE\hspace{\algorithmicindent} {$decisionpoint \gets \lfloor 100 * p_j \rfloor $}
\STATE\hspace{\algorithmicindent} {$n \gets \lfloor 100 * p_o \rfloor$}
\STATE\hspace{\algorithmicindent} {$p_g \gets \binom{100}{n} * p_j ^ {n} * (1 - p_j) ^ {(100 - n)}$}
\STATE\hspace{\algorithmicindent} \textbf{if} {$n < decisionpoint$} \textbf{or} {$p_j = 0$} \textbf{or} {$p_j = 1$} \textbf{then}
\STATE\hspace{\algorithmicindent}\hspace{\algorithmicindent} \textbf{Return} {$0$}
\STATE\hspace{\algorithmicindent} \textbf{endif}
\STATE\hspace{\algorithmicindent} {$peakVal \gets \binom{100}{decisionpoint} * p_j ^ {decisionpoint} * (1 - p_j) ^ {(100 - decisionpoint)}$}
\STATE\hspace{\algorithmicindent} \textbf{Return} {$1 - \frac{p_g}{peakVal}$}
\STATE
\FOR{\textbf{each} ($word \,,\, time$)}
\IF{$lastupd[word] < time$}
\STATE {$p_j[word] \gets p_j[word] + \frac{ \frac{number[word]}{total[lastupd[word]]} - p_o[word]}{time} $}
\STATE {$p_b[word] \gets updatePb(\frac{number[word]}{total[lastupd[word]]}, p_j[word])$}
\STATE {$p_o[word] \gets \frac{1}{total[time]}$}
\STATE {$number[word] \gets 1$}
\STATE {$p_j[word] \gets \frac{lastupd[word] * p_j[word] + p_o[word]}{time}$}
\STATE {$lastupd[word] \gets time$}
\ELSE
\STATE {$number[word] \gets number[word] + 1$}
\STATE {$p_j[word] \gets p_j[word] - \frac{\frac{number[word]}{total[time]} - p_o[word]}{time}$}
\STATE {$p_o[word] \gets \frac{number[word]}{total[time]} $}
\ENDIF
\ENDFOR
\COMMENT{Every DB write operation is atomic.}
\end{algorithmic}
\end{algorithm}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment