% NB: use pdflatex to compile NOT pdftex.  Also make sure youngtab is
% there...

% converting eps graphics to pdf with ps2pdf generates way too much
% whitespace in the resulting pdf, so crop with pdfcrop
% cf. http://www.cora.nwra.com/~stockwel/rgspages/pdftips/pdftips.shtml

\documentclass[10pt,aspectratio=169,dvipsnames]{beamer}

\usetheme[color/block=transparent]{metropolis}

\usepackage[absolute,overlay]{textpos}
\usepackage{booktabs}
\usepackage[utf8]{inputenc}

\usepackage{tikz}

\usepackage[scale=2]{ccicons}

\usepackage[official]{eurosym}

%use this to add space between rows
\newcommand{\ra}[1]{\renewcommand{\arraystretch}{#1}}

\newcommand{\R}{\mathbb{R}}

\setbeamerfont{alerted text}{series=\bfseries}
\setbeamercolor{alerted text}{fg=Mahogany}
\setbeamercolor{background canvas}{bg=white}

\def\l{\lambda}
\def\m{\mu}
\def\d{\partial}
\def\cL{\mathcal{L}}
\def\co2{CO${}_2$}
\def\bra#1{\left\langle #1\right|}
\def\ket#1{\left| #1\right\rangle}
\newcommand{\braket}[2]{\langle #1 | #2 \rangle}
\newcommand{\norm}[1]{\left\| #1 \right\|}
\def\corr#1{\Big\langle #1 \Big\rangle}
\def\corrs#1{\langle #1 \rangle}

\def\mw{\text{ MW}}
\def\mwh{\text{ MWh}}
\def\emwh{\text{ \euro/MWh}}

\newcommand{\ubar}[1]{\text{\b{$#1$}}}

% for sources http://tex.stackexchange.com/questions/48473/best-way-to-give-sources-of-images-used-in-a-beamer-presentation

\setbeamercolor{framesource}{fg=gray}
\setbeamerfont{framesource}{size=\tiny}

\newcommand{\source}[1]{\begin{textblock*}{5cm}(10.5cm,8.35cm)
    \begin{beamercolorbox}[ht=0.5cm,right]{framesource}
        \usebeamerfont{framesource}\usebeamercolor[fg]{framesource} Source: {#1}
    \end{beamercolorbox}
\end{textblock*}}

\usepackage{hyperref}

\usepackage{tikz}

\usepackage[europeanresistors,americaninductors]{circuitikz}

%\usepackage[pdftex]{graphicx}

\graphicspath{{graphics/}}

\DeclareGraphicsExtensions{.pdf,.jpeg,.png,.jpg,.gif}

\def\goat#1{{\scriptsize\color{green}{[#1]}}}

\let\olditem\item
\renewcommand{\item}{%
\olditem\vspace{5pt}}

\title{Energy System Modelling\\ Summer Semester 2020, Lecture 8}
%\subtitle{---}
\author{
  {\bf Dr. Tom Brown}, \href{mailto:tom.brown@kit.edu}{tom.brown@kit.edu}, \url{https://nworbmot.org/}\\
  \emph{Karlsruhe Institute of Technology (KIT), Institute for Automation and Applied Informatics (IAI)}
}

\date{}

\titlegraphic{
  \vspace{0cm}
  \hspace{10cm}
    \includegraphics[trim=0 0cm 0 0cm,height=1.8cm,clip=true]{kit.png}

\vspace{5.1cm}

  {\footnotesize

  Unless otherwise stated, graphics and text are Copyright \copyright Tom Brown, 2020.
  Graphics and text for which no other attribution are given are licensed under a
  \href{https://creativecommons.org/licenses/by/4.0/}{Creative Commons
  Attribution 4.0 International Licence}. \ccby}
}

\begin{document}

\maketitle

\begin{frame}

  \frametitle{Table of Contents}
  \setbeamertemplate{section in toc}[sections numbered]
  \tableofcontents[hideallsubsections]
\end{frame}

\section{Optimisation Revision}

\begin{frame}
  \frametitle{Optimisation problem}

We have an \alert{objective function} $f: \R^k \to \R$
\begin{equation*}
  \max_{x} f(x)
\end{equation*}
[$x = (x_1, \dots x_k)$] subject to some \alert{constraints} within $\R^k$:
\begin{align*}
  g_i(x) & = c_i \hspace{1cm}\leftrightarrow\hspace{1cm} \l_i \hspace{1cm} i = 1,\dots n \\
  h_j(x) & \leq d_j \hspace{1cm}\leftrightarrow\hspace{1cm} \m_j \hspace{1cm} j = 1,\dots m
\end{align*}

$\l_i$ and $\m_j$ are the \alert{KKT multipliers} we introduce for
each constraint equation; they measure the change in the objective value of the optimal solution obtained by relaxing the constraints (for this reason they are also called \alert{shadow prices}).

\end{frame}

\begin{frame}
  \frametitle{KKT conditions}

The \alert{Karush-Kuhn-Tucker (KKT) conditions} are necessary conditions that an optimal solution $x^*,\m^*,\l^*$ always satisfies (up to some regularity conditions):
\begin{enumerate}
\item \alert{Stationarity}: For $l = 1,\dots k$
  \begin{equation*}
  \frac{\d \cL}{\d x_l} =   \frac{\d f}{\d x_l} - \sum_i \l_i^* \frac{\d g_i}{\d x_l}  - \sum_j \m_j^* \frac{\d h_j}{\d x_l} = 0
  \end{equation*}
    \item \alert{Primal feasibility}:
      \begin{align*}
        g_i(x^*) & = c_i \\
        h_j(x^*) &\leq d_j
      \end{align*}
    \item \alert{Dual feasibility}: $\m_j^* \geq 0$
    \item \alert{Complementary slackness}: $\m_j^* (h_j(x^*) - d_j) = 0$
\end{enumerate}

\end{frame}

\begin{frame}
  \frametitle{min/max and signs}

  If the problem is a \alert{maximisation} problem (e.g. welfare maximisation), then
  \alert{$\m_j^* \geq 0$} since $\m_j = \frac{\d \cL}{\d d_j}$ and if we
  increase $d_j$ in the constraint $h_j(x) \leq d_j$, then the
  feasible space can only get bigger. Since if $X \subseteq X'$
  \begin{equation*}
 \max_{x\in X} f(x ) \leq     \max_{x\in X'} f(x)
  \end{equation*}
  then the objective value at the optimum point can only get bigger, and thus $\m_j^* \geq 0$. (If $d_j \to \infty$
  then the constraint is no longer binding, if $d_j \to -\infty$ then
  the feasible space vanishes.)

  If however the problem is a \alert{minimisation} problem (e.g. cost minimisation) then we can use
  \begin{equation*}
      \min_{x\in X} f(x) = - \max_{x\in X} \left[ -f(x)\right]
  \end{equation*}
  We can keep our definition of the Lagrangian and almost all the KKT
  conditions, but we have a change of sign \alert{$\m_j^* \leq 0$}, since
  \begin{equation*}
   \min_{x\in X} f(x ) \geq     \min_{x\in X'} f(x)
  \end{equation*}
  The $\l_i^*$ also change sign.

\end{frame}

\section{Welfare maximisation revision}

\begin{frame}{KKT and Welfare Maximisation 1/2}

  Apply KKT now to maximisation of total economic welfare:
  \begin{align*}
    \max_{\{d_b\}, \{g_s\}} f(\{d_b\}, \{g_s\}) = \left[ \sum_b U_b (d_b)  -  \sum_s C_s (g_s) \right]
  \end{align*}
  subject to the balance constraint:
  \begin{align*}
    g(\{d_b\}, \{g_s\}) = \sum_b d_b -  \sum_s g_s  = 0 \hspace{1cm} \leftrightarrow \hspace{1cm} \l
  \end{align*}
  and any other constraints (e.g. limits on generator capacity, etc.).

  Our optimisation variables are $\{x\} = \{d_b\} \cup \{g_s\}$.

  We get from KKT stationarity at the optimal point:
  \begin{align*}
    0 & =   \frac{\d f}{\d d_b} - \sum_b \l^* \frac{\d g}{\d d_b} = U_b'(d_b^*) - \l^* = 0 \\
    0 & =   \frac{\d f}{\d g_s} - \sum_s \l^* \frac{\d g}{\d g_s} = -C_s'(g_s^*) + \l^* = 0
  \end{align*}

\end{frame}

\begin{frame}{KKT and Welfare Maximisation 2/2}

  So at the optimal point of maximal total economic welfare we get the
  same result as if everyone maximises their own welfare separately based on the price $\l^*$:
  \begin{align*}
 U_b'(d_b^*) =  \l^*  \\
 C_s'(g_s^*) = \l^*
  \end{align*}

  This is the CENTRAL result of microeconomics.

  If we have further inequality constraints that are binding (e.g. capacity constraints), then
  these equations will receive additions with $\m_i^* > 0$.

\end{frame}

\section{Optimise Single Node with Linear Generation Costs and Demand Utility}

\begin{frame}[fragile]
  \frametitle{Simplified world: linear generation costs, linear demand utility}

  We will now turn to a simpler world: all the generator cost
  functions are linear
  \begin{align*}
    C_s(g_s) = o_s g_s
  \end{align*}
  and each generator has limited output $0 \leq g_s \leq G_s$. The marginal cost function is a constant $C_s'(g_s) = o_s$.

  The quantity $G_s$ and marginal cost $o_s$ define a \alert{supply offer}.

  All the consumer utility functions are also linear
  \begin{align*}
    U_b(d_b)  = v_b d_b
  \end{align*}
  and each consumer has limited consumption $0 \leq d_b \leq D_b$. The marginal utility function is a constant $U_b'(d_b)  = v_b$.

  The quantity $D_b$ and marginal utility $v_b$ define a \alert{demand bid}.

\end{frame}

\begin{frame}{Supply-demand linear example: generator offers}

  Example from Kirschen and Strbac pages 56-58.

  The following generators offer into the market for the hour between 0900 and 1000 on 20th April 2016:
  \ra{1.1}
  \begin{table}[!t]
    \begin{tabular}{lrr}
      \toprule
      Company & Quantity [MW] &  Marginal cost [\$/MWh]\\
      \midrule
      Red & 200 & 12 \\
      Red & 50 & 15 \\
      Red & 150 & 20 \\
      Green & 150 & 16 \\
      Green & 50 & 17 \\
      Blue & 100 & 13 \\
      Blue & 50 & 18 \\
      \bottomrule
    \end{tabular}
  \end{table}

\end{frame}

\begin{frame}{Supply-demand linear example: consumer bids}

  The following consumers make bids for the same period:
  \ra{1.1}
  \begin{table}[!t]
    \begin{tabular}{lrr}
      \toprule
      Company & Quantity [MW] &  Marginal utility [\$/MWh]\\
      \midrule
      Yellow & 50 & 13 \\
      Yellow & 100 & 23 \\
      Purple & 50 & 11 \\
      Purple & 150 & 22 \\
      Orange & 50 & 10 \\
      Orange & 200 & 25 \\
      \bottomrule
    \end{tabular}
  \end{table}

\end{frame}

\begin{frame}{Supply-demand example: Curve}

  If the bids and offers are stacked up in order, the supply and
  demand curves meet with a demand of 450~MW at a system marginal
  price of $\l^* = 16$~\$/MWh.

  \centering

  \includegraphics[width=10cm]{supply-bid-example}

  \source{Kirschen \& Strbac}

\end{frame}

\begin{frame}{Supply-demand example: Revenue and Expenses}

  Dispatch and revenue/expense of each company:
  \ra{1.1}
  \begin{table}[!t]
    \begin{tabular}{l|rr|rr}
      \toprule
      Company & Production& Consumption & Revenue& Expense\\
       & [MWh] & [MWh] & [\$] & [\$] \\
      \midrule
      Red & 250 &  & 4000 & \\
      Blue & 100 & & 1600 & \\
      Green & 100 & & 1600 & \\
      Orange && 200 && 3200 \\
      Yellow && 100 && 1600 \\
      Purple && 150 && 2400 \\
      \midrule
      Total & 450 & 450 & 7200 & 7200 \\
      \bottomrule
    \end{tabular}
  \end{table}

\end{frame}

\begin{frame}[fragile]
  \frametitle{Simplified world: linear generation costs, single inelastic demand}

  For the analysis of the KKT equations, we will simplify even further.

  We consider a single demand bid of volume $D$ so that the demand does not respond to price changes (i.e. the demand is \alert{inelastic}) up to a very high marginal utility $v >> o_s\,\, \forall s$, i.e.
  \begin{align*}
    U(d) = vd
  \end{align*}
  for $d\leq D$.

  $v$ is sometimes called the \alert{Value Of Lost Load (VOLL)}.
\end{frame}

\begin{frame}[fragile]
  \frametitle{Simplify representation of consumers and generators}

  In this case we get for our welfare maximisation:
  \begin{align*}
    \max_{d, \{g_s\} }\left[ vd  -  \sum_s o_s g_s \right]
  \end{align*}
  subject to:
  \begin{align*}
    d -  \sum_s g_s   = 0 \hspace{1cm} & \leftrightarrow \hspace{1cm} \l \\
    d  \leq D  \hspace{1cm}  & \leftrightarrow \hspace{1cm} \m \\
        g_s   \leq  G_s  \hspace{1cm}& \leftrightarrow\hspace{1cm} \bar{\m}_s \\
    - g_s   \leq  0   \hspace{1cm}& \leftrightarrow\hspace{1cm} \ubar{\m}_s
  \end{align*}

\end{frame}

\begin{frame}{Simplest example: one generator type, inelastic demand}

  Suppose all generators have the same marginal cost $o$ and we
  represent their total dispatch by $g$ and total capacity by $G$
  \begin{align*}
    \max_{d, g }\left[ vd  -  og\right]
  \end{align*}
  such that:
  \begin{align*}
    d - g & = 0  \hspace{1cm}\leftrightarrow\hspace{1cm} \l \\
    d & \leq D  \hspace{1cm} \leftrightarrow \hspace{1cm} \m \\
    g  & \leq  G  \hspace{1cm}\leftrightarrow\hspace{1cm} \bar{\m} \\
    - g  & \leq  0  \hspace{1cm}\leftrightarrow\hspace{1cm} \ubar{\m}
  \end{align*}

\end{frame}

\begin{frame}{Simplest example: one generator type, inelastic demand}

  If $D < G$ then since $v >> o$, it will be always be welfare-maximising to dispatch to satisfy the load, i.e.
  \begin{equation*}
    g^* = d^* = D
  \end{equation*}
  If the demand is non-zero then since $g^* > 0$ by complementarity we
  have $\ubar{\m}^* = 0$. Since $D < G$ then $g^* < G$ and
  by complementarity we have $\bar{\m}^* = 0$. To compute $\l^*$ we use stationarity:
  \begin{align*}
    0 & = \frac{\d \cL}{\d g } =   \frac{\d f}{\d g} - \sum_i \l_i^* \frac{\d g_i}{\d g}  - \sum_j \m_j^* \frac{\d h_j}{\d g} = - o + \l^* - \bar{\m}^* + \ubar{\m}^*
  \end{align*}
  Thus $\l^* = o$, which is the cost per unit of supplying extra demand. The \alert{generator sets the price}. There is no generator profit and a large consumer surplus.

  For the load $\m^*$ can be non-zero because $d^*=D$:
    \begin{align*}
      0 & = \frac{\d \cL}{\d d} =   \frac{\d f}{\d d} - \sum_i \l_i^* \frac{\d g_i}{\d d}  - \sum_j \m_j^* \frac{\d h_j}{\d d} = v - \l^* - \m^*
    \end{align*}
    $\m^* = v - \l^*$ is the marginal benefit of each increase in demand.
\end{frame}

\begin{frame}{Simplest example: one generator type, inelastic demand}

  For the case $D < G$:

  \includegraphics[width=12cm]{supply-bigger-demand-single.pdf}

\end{frame}

\begin{frame}{Simplest example: one generator type, inelastic demand}

  If $D > G$ then the generator will dispatch up to its maximum capacity
  \begin{equation*}
    g^* = d^* = G
  \end{equation*}
  For its lower limit we have $\ubar{\m}^* = 0$. From stationarity:
  \begin{align*}
    0 & = \frac{\d \cL}{\d g } =   \frac{\d f}{\d g} - \sum_i \l_i^* \frac{\d g_i}{\d g}  - \sum_j \m_j^* \frac{\d h_j}{\d g} = - o + \l^* - \bar{\m}^* + \ubar{\m}^*
  \end{align*}
  Thus $\l^* = o + \bar{\m}^*$. To find $\l^*$ we have to look at the demand:
    \begin{align*}
      0 & = \frac{\d \cL}{\d d} =   \frac{\d f}{\d d} - \sum_i \l_i^* \frac{\d g_i}{\d d}  - \sum_j \m_j^* \frac{\d h_j}{\d d} = v - \l^* - \m^*
    \end{align*}
  Since $d^* < D$, $\m^* = 0$, $\l^* = v$ and thus $\bar{\m}^* = v-o$, which is the marginal benefit of increasing the generator capacity $G$. The \alert{demand sets the price}. There is no consumer surplus and the generator makes a large profit.

\end{frame}

\begin{frame}{Simplest example: one generator type, inelastic demand}

  For the case $D > G$:

  \includegraphics[width=12cm]{demand-bigger-supply.pdf}

\end{frame}

\begin{frame}{Next simplest example: several generators, fixed demand}

  Suppose we have several generators with dispatch $g_s$ and strictly ordered
  operating costs $o_s$ such that $o_s < o_{s+1}$. We now maximise
  \begin{align*}
    \max_{\{d, g_s\}}  \left[ vd  -  \sum_s o_s g_s \right]
  \end{align*}
  such that
  \begin{align*}
    d -  \sum_s g_s  & = 0 \hspace{1cm} \leftrightarrow \hspace{1cm} \l \\
    d & \leq D  \hspace{1cm} \leftrightarrow \hspace{1cm} \m \\
        g_s  & \leq  G_s  \hspace{1cm}\leftrightarrow\hspace{1cm} \bar{\m}_s \\
    - g_s  & \leq  0  \hspace{1cm}\leftrightarrow\hspace{1cm} \ubar{\m}_s
  \end{align*}

\end{frame}

\begin{frame}{Next simplest example: several generators, fixed demand}

  Stationarity gives us for each generator $g_s$:
  \begin{align*}
    0  = \frac{\d \cL}{\d g_s} = -o_s + \l^* - \bar{\m}^*_s + \ubar{\m}^*_s
  \end{align*}
  and from complementarity we get
  \begin{align*}
    \bar{\m}_s(g_s^* - G_s)  = 0 \hspace{2cm}    \ubar{\m}_sg_s^*  = 0
  \end{align*}
  We can see by inspection that we will dispatch the cheapest
  generation first. Suppose that we have enough generation for the
  demand, i.e. $D < \sum_s G_s$. [If $D > \sum_s G_s$ we have the same
    situation as for a single generator, i.e. $\l^* = v$, so that the demand sets the price.]

  Find the generator $m$ on the margin where the supply curve
  intersects with the demand $D$, i.e. the $m$ where $\sum_{s=1}^{m-1}
  G_s < D < \sum_{s=1}^{m} G_s$.

  For $s \leq m-1$ we have $g_s^* = G_s$, $\ubar{\m}^*_s = 0$,
  $\bar{\m}^*_s =  \l^* - o_s$.

  For $s = m$ we have $g_m^* = D - \sum_{s=1}^{m-1} G_s$ to cover
  what's left of the demand. Since $0 < g_m^* < G_m$ we have
  $\ubar{\m}^*_m = \bar{\m}^*_m = 0$ and thus $\l^* = o_m$.

\end{frame}

\begin{frame}{Next simplest example: several generators, fixed demand}

  Specific example of two generators with $G_1 = 300$~MW, $G_2 =
  400$~MW, $o_1 = 10$~\euro/MWh, $o_2 = 30$~\euro/MWh and $D = 500$~MW.

  In this case $m=2$, $g_1^* = G_1 = 300$~MW, $g_2^* = d - G_1 = 200$~MW,
  $\l^* = o_2$, $\ubar{\m}_i = 0$, $\bar{\m}_2 = 0$ and $\bar{\m}_1 =
  o_2 - o_1$.

  \centering
  \includegraphics[width=8cm]{supply-demand-two.pdf}

\end{frame}

\begin{frame}{From welfare maximisation to cost minimisation}

  For the case $D > \sum_s G_s$ we can instead imagine that the demand
  is rigidly fixed to $D$ and that instead we have a dummy generator
  with dispatch $g_d = D-\sum_s G_s$ that represents \alert{load shedding}.
  In this case we can substitute $d = D - g_d$ to get
  \begin{align*}
    \max_{\{g_d, g_s\}}  \left[ vD - vg_d  -  \sum_s o_s g_s \right]
  \end{align*}
  such that
  \begin{align*}
    D - g_d -  \sum_s g_s  & = 0 \hspace{1cm} \leftrightarrow \hspace{1cm} \l \\
        g_s  & \leq  G_s  \hspace{1cm}\leftrightarrow\hspace{1cm} \bar{\m}_s \\
    - g_s  & \leq  0  \hspace{1cm}\leftrightarrow\hspace{1cm} \ubar{\m}_s
  \end{align*}

  Since $vD$ is a constant, we can use
      $\max_{x\in X} \left[ -f(x)\right] = - \min_{x\in X} f(x)$
  to recast this as a minimisation of the total generator costs,
  absorbing $g_d$ into the set $\{g_s\}$. The constant $vD$ is dropped.

\end{frame}

\begin{frame}{From welfare maximisation to cost minimisation}

 We have  turned the maximisation of total welfare into \alert{cost minimisation}:
  \begin{align*}
    \min_{\{g_s\}}  \sum_s o_s g_s
  \end{align*}
  such that:
  \begin{align*}
    \sum_s g_s - d & = 0  \hspace{1cm}\leftrightarrow\hspace{1cm} \l \\
        g_s  & \leq  G_s  \hspace{1cm}\leftrightarrow\hspace{1cm} \bar{\m}_s \\
    - g_s  & \leq  0  \hspace{1cm}\leftrightarrow\hspace{1cm} \ubar{\m}_s
  \end{align*}

  The most expensive generator has $o_s = v$ and $G_s = \infty$ and
  represents \alert{load shedding}.

  We've replaced the symbol $D$  with $d$ for simplicity going forward ($d$ is now a constant).

  NB: Because the signs of the KKT multipliers change when we go from
  maximisation to minimisation, we've also changed the sign of the
  balance constraint to keep the marginal price $\l$ positive.
\end{frame}

\section{Optimise nodes in a network}

\begin{frame}[fragile]
  \frametitle{Welfare optimisation for several nodes in a network}

  Now let's suppose we have several nodes $i$ with different loads and
  different generators, with flows $f_\ell$ in the network lines $\ell$.

  Now we have additional optimisation variables $f_\ell$ AND
  additional constraints for welfare maximisation:
  \begin{align*}
    \max_{\{d_{i,b}\},\{g_{i,s}\},\{f_\ell\}}\left[\sum_{i,b} U_{i,b}(d_{i,b}) - \sum_{i,s} C_{i,s} (g_{i,s}) \right]
  \end{align*}
  such that demand is met either by generation or by the network at each node $i$
  \begin{align*}
    \sum_{b} d_{i,b} - \sum_{s} g_{i,s} +  \sum_\ell K_{i\ell}f_\ell  = 0 \hspace{1cm}\leftrightarrow\hspace{1cm} \l_i
  \end{align*}
  Note there is now a \alert{market price for each node}. As before, generator constraints are satisified
    \begin{align*}
        g_{i,s}  & \leq  G_{i,s}  \hspace{1cm}\leftrightarrow\hspace{1cm} \bar{\m}_{i,s} \\
    - g_{i,s}  & \leq  0  \hspace{1cm}\leftrightarrow\hspace{1cm} \ubar{\m}_{i,s}
  \end{align*}

\end{frame}

\begin{frame}[fragile]
  \frametitle{Linear cost minimisation at several nodes in a network}

  For cost minimisation we have a fixed load $d_i$ at each node, and
  absorb load-shedding above a value $v$ into a dummy generator.

  Now we minimise over $f_\ell$ and $g_{i,s}$ for the case of linear cost functions:
  \begin{align*}
    \min_{\{g_{i,s}\},\{f_\ell\}}  \sum_{i,s} o_{i,s} g_{i,s}
  \end{align*}
  such that demand is met either by generation or by the network at each node $i$
  \begin{align*}
    \sum_{s} g_{i,s} - d_i = \sum_\ell K_{i\ell}f_\ell  \hspace{1cm}\leftrightarrow\hspace{1cm} \l_i
  \end{align*}
  and generator constraints are satisified
    \begin{align*}
        g_{i,s}  & \leq  G_{i,s}  \hspace{1cm}\leftrightarrow\hspace{1cm} \bar{\m}_{i,s} \\
    - g_{i,s}  & \leq  0  \hspace{1cm}\leftrightarrow\hspace{1cm} \ubar{\m}_{i,s}
  \end{align*}

\end{frame}

\begin{frame}[fragile]
  \frametitle{Several generators at different nodes in a network}

  In addition we have constraints on the line flows.

  First, they have to satisfy Kirchoff's Voltage Law around each closed cycle $c$:
  \begin{align*}
    \sum_{c} C_{\ell c} x_\ell f_\ell = 0  \hspace{1cm}\leftrightarrow\hspace{1cm} \l_c
  \end{align*}
  and in addition the flows cannot overload the thermal limits, $|f_\ell| \leq F_\ell$
  \begin{align*}
    f_\ell \leq F_\ell  \hspace{1cm}\leftrightarrow\hspace{1cm} \bar{\m}_\ell \\
        - f_\ell \leq F_\ell  \hspace{1cm}\leftrightarrow\hspace{1cm} \ubar{\m}_\ell
  \end{align*}

\end{frame}

\begin{frame}[fragile]
  \frametitle{Simplest example: two nodes connected by a single line}

  At node 1 we have demand of $d_1 = 100$~MW and a generator with
  costs $o_1 = 10$~\euro/MWh and a capacity of $G_1 = 300$~MW.

  At node 2 we have demand of $d_2 = 100$~MW and a generator with
  costs $o_2 = 20$~\euro/MWh and a capacity of $G_2 = 300$~MW.

  What happens if the capacity of the line connecting them is $F = 0$?

  What about $F = 50$~MW?

  What about $F = \infty$?

\end{frame}

\begin{frame}[fragile]
  \frametitle{Simplest example: two nodes connected by a single line}

\centering
\begin{circuitikz}
  \draw [ultra thick] (1,13) node[anchor=south]{1} -- (4,13);
  \draw(2.5,13) |- +(0,0.5) to [short,i^=$f$] +(5,0.5) |- +(0,-0.5);
  \draw [ultra thick] (6,13) node[anchor=south]{2} -- (9,13);
  \draw (1.5,13) -- +(0,-0.5) node[sground]{};
  \draw (3,12) node[vsourcesinshape, rotate=270](V2){}
  (V2.left) -- +(0,0.6);
  \draw (1.5,11) node{$d_1$};
  \draw (3,11) node{$g_{1}$};
  \draw (6.5,13) -- +(0,-0.5) node[sground]{};
  \draw (8,12) node[vsourcesinshape, rotate=270](V2){}
  (V2.left) -- +(0,0.6);
  \draw (6.5,11) node{$d_2$};
  \draw (8,11) node{$g_{2}$};

\end{circuitikz}

\centering
\begin{columns}[T]
  \begin{column}{5cm}

    \centering

 $g_1 - d_1 = f \quad \leftrightarrow \quad \l_1$

    \hspace{1cm}

    $d_1 =$ 100~MW

    $G_1 =$ 300~MW

    $o_1 = 10$~\euro/MWh

  \end{column}
  \begin{column}{5cm}

    \centering
    $g_2 - d_2 =  -f   \quad \leftrightarrow \quad \l_2$

    \hspace{1cm}

    $d_2 =$ 100~MW

    $G_2 =$ 300~MW

    $o_2 = 20$~\euro/MWh

  \end{column}
\end{columns}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Simplest example: two nodes connected by a single line}

  Out optimisation problem has objective function:
  \begin{equation*}
    \min_{g_1,g_2,f}  \left[o_1 g_1 + o_2 g_2 \right]
  \end{equation*}
  subject to the following constraints:
  \begin{align*}
    g_1 - d_1  = f \hspace{1cm}\leftrightarrow\hspace{1cm} \l_1 \\
    g_2 - d_2  = -f \hspace{1cm}\leftrightarrow\hspace{1cm} \l_2 \\
  g_1 \leq G_1  \hspace{1cm}\leftrightarrow\hspace{1cm} \bar{\m}_1 \\
    - g_1 \leq 0  \hspace{1cm}\leftrightarrow\hspace{1cm} \ubar{\m}_1  \\
  g_2 \leq G_2  \hspace{1cm}\leftrightarrow\hspace{1cm} \bar{\m}_2  \\
  - g_2 \leq 0  \hspace{1cm}\leftrightarrow\hspace{1cm} \ubar{\m}_2  \\
    f \leq F  \hspace{1cm}\leftrightarrow\hspace{1cm} \bar{\m}  \\
    - f \leq F  \hspace{1cm}\leftrightarrow\hspace{1cm} \ubar{\m}
\end{align*}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Two nodes: Case $F = 0$ }

  For the case $F = 0$ the nodes are like two separated islands, $f^* = 0$.

  The generator on each island provides the demand separately, so:
  \begin{equation*}
    g_1^* = d_1  \hspace{2cm} \textrm{and} \hspace{2cm} g_2^* = d_2
  \end{equation*}

  Neither generator has any binding constraints, since in each case the demand (100~MW) is less than the generator capacity (300~MW), so
  \begin{equation*}
    \bar{\m}_1^* = \ubar{\m}_1^* =     \bar{\m}_2^* = \ubar{\m}_2^* = 0
  \end{equation*}

  From stationarity for each site we get
  \begin{equation*}
    0 = \frac{\d \cL}{\d g_{i}} = o_i - \l_i^*  -  \bar{\m}_i^* + \ubar{\m}_i^*
  \end{equation*}

  Thus we have at each site $\l_i^* = o_i$, as if we had optimised the nodes separately.

\end{frame}

\begin{frame}[fragile]
  \frametitle{Two nodes: Case $F =$ 50~MW }

  For the case $F = $50~MW the cheaper node 1 will export to the more
  expensive node 2 as much as the restricted capacity $F$ allows:
  \begin{equation*}
    f^* = F = 50\textrm{ MW}
  \end{equation*}
  Generator 1 covers 50~MW of the demand from node 2:
  \begin{equation*}
    g_1^* = d_1+f^* = 150\textrm{ MW}  \hspace{2cm} \textrm{and} \hspace{2cm} g_2^* = d_2 - f^* = 50\textrm{ MW}
  \end{equation*}
  Neither generator has any binding constraints, so
  \begin{equation*}
    \bar{\m}_1^* = \ubar{\m}_1^* =     \bar{\m}_2^* = \ubar{\m}_2^* = 0
  \end{equation*}
  and thus we have again different prices at each $\l_i^* = o_i$. For the flow:
  \begin{equation*}
    0 = \frac{\d \cL}{\d f} = 0 +  \l_1^* - \l_2^*  -  \bar{\m}^* + \ubar{\m}^*
  \end{equation*}
  Only the upper limit is binding, so we get $\ubar{\m}^* = 0$ and
    $\bar{\m}^* = \l_1^* - \l_2^* = o_1 - o_2 = -10 $ \euro/MWh.

$\bar{\m}^*$ is the cost reduction if we expand the transmission capacity $F$ by $\varepsilon$, allowing us to substitute some of the expensive generation at node 2 with cheap generation from node 1.

\end{frame}

\begin{frame}[fragile]
  \frametitle{Two nodes: Case $F = \infty$ }

  For the case $F = \infty$ we have unrestricted capacity, so it is
  like merging the two nodes to one node. Now all the demand is
  covered by the cheapest node:
  \begin{equation*}
    f^* = d_2 = 100\textrm{ MW}
  \end{equation*}
  Generator 1 covers all the demand:
  \begin{equation*}
    g_1^* = d_1+d_2  = 200\textrm{ MW} \hspace{2cm} \textrm{and} \hspace{2cm} g_2^* = 0
  \end{equation*}
  Only generator 2 has a non-zero KKT multiplier, so at node 1 we have
  $\l_1^* =o_1$ and at node 2 we have:
  \begin{equation*}
   \ubar{\m}_2^* = \l_2^* - o_2
  \end{equation*}
  From KKT for the flow $f$ we have no constraints so $\bar{\m}^* = \ubar{\m}^* = 0$ and from stationarity
  \begin{equation*}
    0 = \frac{\d \cL}{\d f} = 0 +  \l_1^* - \l_2^*  -  \bar{\m}^* + \ubar{\m}^*
  \end{equation*}
  i.e. $\l_1^* = \l_2^*$. We have price equalisation, as if it were a single node.

\end{frame}

\begin{frame}{Two node: demand pagements versus generation revenue}

  Now let's compare for our examples what each demand pays $\l_i^*d_i$ and what each generator receives as revenue $\l_i^*g_i^*$ from each market.
  \ra{1.1}
  \begin{table}[!t]
    \begin{tabular}{rrrrrrrrr}
      \toprule
      Case & $\l_1^*$  & $\l_2^*$ & $\l_1^*d_1$ & $\l_2^*d_2$ & $\sum_i \l_i^*d_i$ & $\l_1^*g_1^*$  & $\l_2^*g_2^*$ & $\sum_i \l_i^*g_i^*$ \\
       & [\euro/MWh] &  [\euro/MWh] & [\euro/h] & [\euro/h] & [\euro/h] & [\euro/h] & [\euro/h] & [\euro/h] \\
      \midrule
      $F = 0$ & 10 & 20 & 1000 & 2000 & 3000 & 1000 & 2000 & 3000 \\
      $F = 50$ & 10 & 20 & 1000 & 2000 & 3000 & 1500 & 1000 & 2500 \\
      $F = \infty$ & 10 & 10 & 1000 & 1000 & 2000 & 2000 & 0 & 2000 \\
%      $F = 50$ & 3000 & 2500 & 10 & 50 & 500
%      $F = \infty$ & 2000 & 2000 & 0 & 100 & 0  \\
      \bottomrule
    \end{tabular}
  \end{table}

  NB: In the case with $F=50$, total demand payments are 3000~\euro/h, whereas the generators are only receiving 2500~\euro/h.

  Where is the missing money (500~\euro/h) going?

  Answer: to the network operator for service of doing arbitrage, buying low and selling high.

\end{frame}

\begin{frame}{Congestion rent}

Due to the congestion of the transmission line, the marginal cost of producing electricity can be different at node 1 and node 2. The competitive price at node 2 is higher than at node 1 -- this corresponds to \alert{locational marginal pricing}, or \alert{nodal pricing}.

Since consumers pay and generators get paid the price in their local market, in case of congestion there is a difference between the total payment of consumers and the total revenue of producers -- this is the \alert{merchandising surplus} or \alert{congestion rent}, collected by the network operator. For each line it is given by the price difference in both regions times the amount of power flow between them:
\begin{align*}
\text{Congestion rent =}\:\Delta \lambda\times f
\end{align*}

\end{frame}

\begin{frame}{Congestion rent: Two node example}

  Returning to our two node example:
  \ra{1.1}
  \begin{table}[!t]
    \begin{tabular}{rrrrrr}
      \toprule
      Case & Demand pays & Generator gets & $\l_2^* - \l_1^*$ & flow $f$ & Cong. rent \\
       & [\euro/h] & [\euro/h] & [\euro/MWh] & [MW] & [\euro/h] \\
      \midrule
      $F = 0$ & 3000 & 3000 & 10 & 0 & 0 \\
      $F = 50$ & 3000 & 2500 & 10 & 50 & 500 \\
      $F = \infty$ & 2000 & 2000 & 0 & 100 & 0 \\
      \bottomrule
    \end{tabular}
  \end{table}

To get a congestion rent, we need congestion to cause a price
difference between the nodes, as well as a non-zero flow between the
nodes.

\end{frame}

\begin{frame}[fragile]
  \frametitle{Congestion rent}

  In this example we saw that the sum of what consumers pay does not always equal the sum of generator revenue.

  In fact if we take the balance constraint and sum it weighted by the market price at each node we find
  \begin{align*}
    \sum_i \l_i^* d_i -   \sum_i \l_i^* \sum_{s} g^*_{i,s} = -\sum_i \l_i^* \sum_\ell K_{i\ell}f^*_\ell
  \end{align*}

  The quantity for each $\ell$
  \begin{equation*}
    -f_\ell^*\sum_i K_{i\ell} \l_i^* = f_\ell (\l_{\textrm{end}}^* - \l_{\textrm{start}}^*)
  \end{equation*}
  is called the \alert{congestion rent} and is the money the network
  operator receives for transferring power from a low price node (start)
  to a high price node (end), `buy it low, sell it high'.

  It is zero if: a) the flow is zero or b) the price difference is zero.

\end{frame}

\section{The European Market}

\begin{frame}
  \frametitle{Existing bidding zones}

\begin{columns}[T]
  \begin{column}{5cm}

    \includegraphics[width=5.8cm]{european_bidding_zones.png}
  \end{column}

  \begin{column}{8cm}

    \begin{itemize}
    \item Bids for German electricity take place in a \alert{giant bidding zone} encompassing both Germany and Luxembourg (Austria was separated from the German bidding zone in October 2018)
    \item This means that transmission constraints are only visible to the market at the \alert{borders} to the other national zones
    \item Internal transmission constraints are \alert{ignored} - market bids are handled as if they do not exist
    \item Only KCL enforced on most borders - KVL much harder
    \end{itemize}

\end{column}
\end{columns}

\end{frame}

\begin{frame}
  \frametitle{The Problem}

  Renewables are not always located near demand centres, as in this example from Germany.

\begin{columns}[T]
  \begin{column}{6cm}
\includegraphics[width=6cm]{scigrid-load}
  \end{column}

  \begin{column}{6cm}
      % left bottom right top
  \includegraphics[trim=0 0cm 0 1cm,width=6.3cm,clip=true]{scigrid-wind}

\end{column}
\end{columns}

\end{frame}

\begin{frame}
  \frametitle{The Problem}

\begin{columns}[T]
  \begin{column}{5.5cm}
        \vspace{.5cm}
\includegraphics[width=6cm]{scigrid-loading}
  \end{column}

  \begin{column}{6cm}
    \begin{itemize}
      \item This leads to \alert{overloaded lines} in the middle of Germany, which
   cannot transport all the wind energy from North Germany to the load
   in South Germany

   \item It also overloads lines in neighbouring countries due to
     \alert{loop flows} (unplanned physical flows `according to least
     resistance' which do not correspond to traded flows)

     \item It also \alert{blocks imports and exports} with
       neighbouring countries, e.g. Denmark

    \end{itemize}

\end{column}
\end{columns}

\end{frame}

\begin{frame}
  \frametitle{Solution 1: Redispatch after energy market clearing}

  These problems are \alert{not visible} in the day-ahead electricity
  market, which treats the whole of Germany and Austria as a single
  bidding zone. It dispatches wind in North Germany as if there was no
  internal congestion...

  To ensure that the physical limits of transmission are not exceeded,
  the network operator must \alert{`re-dispatch'} power stations and
  \alert{curtail} (Einspeisemanagement) renewables to restore order.
  This is \alert{costly} (0.8 redispatch + 0.6 RE-compensation = 1.4 billion
  EUR in 2017 - although exceptional circumstances in 1st quarter) and
  results in \alert{lost CO$_2$-free generation} (5.5 TWh curtailment of RE
  and CHP in 2017).

  \alert{International redispatch} is sometimes also required
  (Multilateral Remedial Actions = MRA).

  Furthermore, there are \alert{no market incentives} to reinforce the North-South
  grid, to locate more power stations in South Germany or to
  build storage / P2X in North Germany.

\end{frame}

\begin{frame}
  \frametitle{Solution 2: Smaller bidding zones to ``see'' congested boundaries}

\begin{columns}[T]
  \begin{column}{5cm}
\includegraphics[width=6cm]{nordel.png}
  \end{column}
  \begin{column}{5cm}
    \begin{itemize}
      \item In Scandinavia they have solved this by introducing \alert{smaller bidding zones}
      \item Now congestion at the boundaries between zones is taken
        into account in the \alert{implicit auctions} of the market
      \item This is also done in Italy (again, a long country),
          where prices for small consumers are \alert{uniformised} for fairness
    \end{itemize}

\end{column}
\end{columns}

\end{frame}

\begin{frame}
  \frametitle{Solution 3: Nodal pricing}

\begin{columns}[T]
  \begin{column}{6cm}

    \vspace{.5cm}
\includegraphics[width=7cm]{lmp.png}
  \end{column}
  \begin{column}{5cm}
    \begin{itemize}
    \item The ultimate solution, as used in the US and other markets,
      is \alert{nodal pricing}, which exposes all transmission congestion
    \item Considered too complex and subject to market power to be
      used in Europe, but this is questionable...
      \item Here we see clearly why many argue for a North-South
        German split
    \end{itemize}

\end{column}
\end{columns}

\end{frame}

\begin{frame}
  \frametitle{First step: Split Germany North-South}

\begin{columns}[T]
  \begin{column}{5cm}
    \vspace{.5cm}
\includegraphics[width=5.2cm]{DE_split.png}
  \end{column}
  \begin{column}{6cm}

    \vspace{.7cm}
    \begin{itemize}

    \item Initial price difference could average up to 12 EUR/MWh
    \item Prices would converge with more network expansion
    \item Redispatch costs reduced by 39\% in 2025, 58\% in 2035 (assuming NEP 2030 transmission projects get built)
    \item Politically difficult, may require, like Italy, uniformised
      price on consumer side
    \end{itemize}
  \end{column}
\end{columns}

\source{\href{https://www.strommarkttreffen.org/2018-02_Fraunholz&Hladik_Zwei_Preiszonen_in_D.pdf}{Fraunholz \& Hladik, 2018}}
\end{frame}

\begin{frame}
  \frametitle{Solution 1.5: Flow-based market coupling}

  Flow-based market coupling can be used in zonal markets to see
  precise individual line constraints, instead of ``boxing'' the feasible space
  like ATC/NTC schemes do.

  \centering \includegraphics[width=13cm]{fbmc}

  \source{Van den Bergh, Boury, Delarue}
\end{frame}

\section{Storage Optimisation}

\begin{frame}[fragile]
  \frametitle{Storage equations}

  Now, like the network case where we add different nodes $i$
  with different loads, for storage we have to
  consider different time periods $t$.

  Label conventional generators by $s$, storage by $r$ and now minimise
  \begin{align*}
    &    \min_{\{g_{i,s,t}\},\{g_{i,r,t,\textrm{charge}}\},\{g_{i,r,t,\textrm{discharge}}\},\{f_{\ell,t}\}}\\
    &\left[  \sum_{i,s,t} o_{i,s} g_{i,s,t} +   \sum_{i,r,t} o_{i,r,\textrm{charge} }\, g_{i,r,t,\textrm{charge}} +   \sum_{i,r,t} o_{i,r,\textrm{discharge}}\, g_{i,r,t,\textrm{discharge}} \right]
  \end{align*}
  The power balance constraints are now (cf. Lecture 5) for each node $i$ and time $t$ that the demand is met either by generation, storage or network flows:
  \begin{align*}
    \sum_{s} g_{i,s,t} + \sum_{r}(g_{i,r,t,\textrm{discharge}} - g_{i,r,t,\textrm{charge}}) - d_{i,t} = \sum_\ell K_{i\ell}f_{\ell,t}  \hspace{0.5cm}\leftrightarrow\hspace{0.5cm} \l_{i,t}
  \end{align*}
  Now we have a market price $\l_{i,t}$ for each node $i$ and time $t$.
\end{frame}

\begin{frame}[fragile]
  \frametitle{Storage equations}

  We have constraints on normal generators
    \begin{align*}
        0 \leq g_{i,s,t}  \leq  G_{i,s}
  \end{align*}
    and on the storage
    \begin{align*}
    0 & \leq g_{i,r,t,\textrm{discharge}} \leq G_{i,r,\textrm{discharge}} \\
    0 & \leq g_{i,r,t,\textrm{charge}} \leq G_{i,r,\textrm{charge}}
    \end{align*}

    The energy level of the storage is given by
  \begin{align*}
    e_{i,r,t} = \eta_0e_{i,r,t-1} + \eta_1g_{i,r,t,\textrm{charge}} -  \eta_2^{-1} g_{i,r,t,\textrm{discharge}}
  \end{align*}
  and limited by
  \begin{align*}
    0 \leq e_{i,r,t} \leq E_{i,r}
  \end{align*}

\end{frame}

\begin{frame}[fragile]
  \frametitle{Idea of storage}

  Storage does `buy it low, sell it high' \alert{arbitrage}, like network, but
  in time rather than space, i.e. between cheap times (e.g. with lots
  of zero-marginal-cost renewables) and expensive times (e.g. with
  high demand, low renewables and expensive conventional generators).

\end{frame}

\begin{frame}[fragile]
  \frametitle{Storage charges at low prices, discharges at high prices}

  Simplified example from \url{https://model.energy} For Germany with only wind and hydrogen storage to meet a flat 100~MW demand.

  Average charging price (with electrolyser): 43 \euro/MWh

  Average discharging price (with turbine): 144 \euro/MWh

  %single_node_storage_bids.ipynb
    \centering \includegraphics[width=8cm]{storage_bids.pdf}

\end{frame}

\begin{frame}[fragile]
  \frametitle{Storage plus network equations}

  Finally for the flows we repeat the constraints for each time $t$.

  We have KVL for each cycle $c$ and time $t$
  \begin{align*}
    \sum_{c} C_{\ell c} x_\ell f_{\ell,t} = 0  \hspace{1cm}\leftrightarrow\hspace{1cm} \l_{c,t}
  \end{align*}
  and in addition the flows cannot overload the thermal limits, $|f_{\ell,t}| \leq F_\ell$
  \begin{align*}
    f_{\ell,t} \leq F_\ell  \hspace{1cm}\leftrightarrow\hspace{1cm} \bar{\m}_{\ell,t} \\
        - f_{\ell,t} \leq  F_\ell  \hspace{1cm}\leftrightarrow\hspace{1cm} \ubar{\m}_{\ell,t}
  \end{align*}

\end{frame}

\begin{frame}[fragile]
  \frametitle{Preview: Investment optimisation}

  Preview for next time:

  Next time we will also optimise \alert{investment} in the \alert{capacities} of generators,
  storage and network lines, to maximise \alert{long-run efficiency}.

  We will promote the capacities $G_{i,s}$, $G_{i,r,*}$, $E_{i,r}$ and
  $F_{\ell}$ to optimisation variables.

\end{frame}

\end{document}


%%%%%%%% ICML 2020 EXAMPLE LATEX SUBMISSION FILE %%%%%%%%%%%%%%%%%

\documentclass{article}

% Recommended, but optional, packages for figures and better typesetting:
\usepackage{microtype}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{booktabs} % for professional tables

% hyperref makes hyperlinks in the resulting PDF.
% If your build breaks (sometimes temporarily if a hyperlink spans a page)
% please comment out the following usepackage line and replace
% \usepackage{icml2020} with \usepackage[nohyperref]{icml2020} above.
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{amssymb}

% Attempt to make hyperref and algorithmic work together better:
\newcommand{\theHalgorithm}{\arabic{algorithm}}

% Use the following line for the initial blind version submitted for review:
\usepackage{icml2020}

% If accepted, instead use the following line for the camera-ready submission:
%\usepackage[accepted]{icml2020}

% The \icmltitle you define below is probably too long as a header.
% Therefore, a short form for the running title is supplied here:
\icmltitlerunning{Kalman Filter Powered Variational Autoencoder for Acoustic Unit Discovery}

\begin{document}

\twocolumn[
\icmltitle{Kalman Filter Powered Variational Autoencoder \\
      for Acoustic Unit Discovery}

% It is OKAY to include author information, even for blind
% submissions: the style file will automatically remove it for you
% unless you've provided the [accepted] option to the icml2020
% package.

% List of affiliations: The first argument should be a (short)
% identifier you will use later to specify author affiliations
% Academic affiliations should list Department, University, City, Region, Country
% Industry affiliations should list Company, City, Region, Country

% You can specify symbols, otherwise they are numbered in order.
% Ideally, you should not use this facility. Affiliations will be numbered
% in order of appearance and this is the preferred way.
\icmlsetsymbol{equal}{*}

\begin{icmlauthorlist}
\icmlauthor{Jayanta Mukherjee}{linkedin}
\end{icmlauthorlist}

\icmlaffiliation{linkedin}{LinkedIn Corporation, Mountain View, CA, USA 94043}

\icmlcorrespondingauthor{Jayanta Mukherjee}{jmukherjee@linkedin.com}

% You may provide any keywords that you
% find helpful for describing your paper; these are used to populate
% the "keywords" metadata in the PDF but will not be shown in the document
\icmlkeywords{Deep Learning, Variational Autoencoder, Kalman Filter, Unsupervised Learning, Acoustic Unit Discovery}

\vskip 0.3in
]

% this must go after the closing bracket ] following \twocolumn[ ...

% This command actually creates the footnote in the first column
% listing the affiliations and the copyright notice.
% The command takes one argument, which is text to display at the start of the footnote.
% The \icmlEqualContribution command is standard text for equal contribution.
% Remove it (just {}) if you do not need this facility.

\printAffiliationsAndNotice{}  % leave blank if no need to mention equal contribution
%\printAffiliationsAndNotice{\icmlEqualContribution} % otherwise use the standard text.

\begin{abstract}
Variational Autoencoder (VAE) empowers identification of dominant latent structure to approximate Bayesian Inference for observation models. Structured Variational Autoencoders (SVAE) have been shown to provide efficient neural network-based approximate inference in the presence of both discrete and continuous latent variables. Inspired by SVAE, a VAE has been developed with Extended Kalman Filter(EKF)s to model latent variables. The contribution of this paper is to introduce the Extended Kalman Filter (EKF) for Variational Autoencoder (VAE) to the task of acoustic unit discovery combining the benefit of EKF for continuous space modeling of latent variables with the power of deep generative models provided by VAE. The EKF-VAE is designed to identify and leverage the latent variable structure. With Extended Kalman Filter in Linear-Gaussian State-Space models, the accuracy of the acoustic unit discovery has been significantly improved by reducing the training loss by 47\% and root-mean-square error by more than 50\% by the EKF-VAE for acoustic discovery on the TIMIT dataset \cite{timit-article}. The experimental results demonstrated that the EKF-VAE model outperforms Hidden Markov Model (HMM) VAEs \cite{Raj_2017} in acoustic discovery.

\end{abstract}

\section{Introduction}
\label{intro}
Many speech technologies such as automatic speech recognition (ASR) and text-to-speech synthesis (TTS) have been used widely around the world. However, most such systems only cover rich-resource languages. For low-resource languages, using such technologies remains limited due to lack of labeled datasets to achieve good performance.

 \par
It is important to develop unsupervised learning algorithms which can use the unlabelled data. For acoustic discovery where methods of speech processing needs to be learnt from raw speech, the task of acoustic discovery can be recognized. Finding the phone-like subword units as acoustic building blocks to discover semantically meaningful linguistic building blocks is necessary. The Variational Autoencoder which can be used for generative purposes as its latent variable models uses deep neural networks to parameterize flexible probability distributions can be an important tool to perform any recognition task including AUD. An auto-encoder encodes some input into a new and usually more compact representation which can be used to reconstruct the input data again. A VAE makes the assumption that the compact representation follows a probabilistic distribution (usually Gaussian) which makes it possible to sample new points and decode them into new data from a trained variational auto-encoder. There have been lots of work being done using Hidden Markov Model (HMM) \cite{Raj_2017} as well as using Latent Variable Models (LVM). One striking difference between these two models is that in the Hidden Markov Model (HMM) the latent variables are discrete while in LVM the latent variables can be continuous. One more difference is that for LVM both the latent and observed variables follow Gaussian Distribution, while for HMM only the observed variables have to be of Gaussian Distribution. LVM is a class of statistical models that seek to model the relationship of observed variables with a set of latent variables to allow for modeling of more complex, generative processes. The inference in these models is often difficult or intractable, motivating a class of variational methods that frame the inference problem as optimization. Variational Autoencoders \cite{Kingma2014}, in particular, have seen success in tasks of image generation \cite{gregor15}.

\par
On the other hand, the Kalman Filter \cite{Kalman_1960} has been popular as one of state-of-the-art algorithms for estimating dynamic state for noisy and incomplete measurements in discrete-time for multiple decades. The Extended Kalman Filter (EKF) \cite{Julier97anew} handles the system nonlinearities through conversion of nonlinear system equations into linear ones by applying a first order Taylor-series approximation around the current mean error and covariance, so that the traditional linear Kalman filter can be applied.

\par
The contribution of this work is to apply the Extended Kalman Filter \cite{ekf_2002} to VAE to improvise the prediction of the words by moving in a direction which will reduce the distance between the actual word and predicted word. Applied Kalman Filter at every training step of the VAE to reduce the error and training losses by as much as 48\% while incurring a maximum overhead of less than 14\% of the epoch duration. Used PyTorch \cite{paszke2017automatic},\cite{NEURIPS2019_9015} Gated Recurrent Unit (GRU) and Recurrent Neural Network (RNN) \cite{SchusterP97} to model the speech recognition aka Acoustic Unit Discovery (AUD). Applied the model to TIMIT \cite{timit-article} and compared its performance against a Hidden Markov Model (HMM) \cite{Raj_2017}.

\par
The paper is organized as follows. In Section \ref{section:related} discusses about the related work in the field of Variational Autoencoder (VAE) and Kalman Filter. The Section \ref{section:background} recapitulates the core concepts used in building the proposed model, e.g., VAE, Recurrent Neural Network based encoder and decoder. The proposed EKF-VAE model is introduced in Section \ref{section:model} along with model estimation forward algorithms, Kalman Filter and its extended version. Section \ref{section:experiments} describes the AUD experiments being conducted on the TIMIT \cite{timit-article} database, while Section \ref{section:conclusions} offers some conclusions.

\section{Related Work}
\label{section:related}
There has been substantial exploration on both the acoustic discovery and variational autoencoder fronts.
The attention mechanism \cite{bahdanau2016neural} has been extensively used with RNN encoder-decoder models \cite{wang2016learning} to enhance their ability to deal with long source inputs. A basic RNN-based VAE \cite{bowman2016generating} generative model has been used to explicitly model different properties of sentences to propose two workarounds: 1. KL cost annealing and 2. masking parts of the source and target tokens with special symbols in order to improvise inference by weakening the decoder. The intent was to generate coherent novel sentences as opposed to AUD that interpolate between known sentences using RNN-based VAE.

The Kalman variational autoencoder (KVAE) \cite{nipsFraccaroKPW17} extends ideas from the SVAE, modelling latent state using a Linear Gaussian State Space Model (LGSSM). To allow for non-linear dynamics, the KVAE uses a recognition model to produce time-varying parameters for the LGSSM, weighting a set of K constant parameters using weights generated by a neural network. They applied KVAE to learn a recognition and dynamics model from video and used it to impute missing data and perform long-term generation in four different environments.

\cite{pmlr-v97-tan19b} proposed decomposing of the overall learning problem into many smaller problems, which are coordinated by the hierarchical mixture, represented by a sum-product network (SPN) and showed that their model outperform classical VAEs on almost all of their experimental benchmarks.

In a recent work on HMM-VAE \cite{Raj_2017}, the Hidden Markov Models (HMMs) was being used as latent models to perform acoustic unit discovery (AUD) in a zero resource scenario, and showed significant improvement in the accuracy of the acoustic unit discovery. The kernel Kalman rule has been proven as an improvement over the Kernel Bayes rule \cite{Gebhardt2017}. The Extended Kalman Filter being superior in modeling dynamic state, applied the Extended Kalman Filter with Variational Auto-encoder to bring further improvement in accuracy in acoustic unit discovery.

\par
\cite{pagnoni2018conditional} augmented the encoder-decoder NMT paradigm by introducing a continuous latent variable to model features of the translation process by extending this model with a co-attention mechanism motivated by \cite{parikh2016} in the inference network to show that the conditional variational model improves upon both discriminating attention-based translation and the conditional variational language model for machine translation presented in \cite{zhang2016}. \cite{pagnoni2018conditional} presented some exploration of the learned latent space to illustrate what the latent variable is capable of capturing by utilizing the latent variable without weakening the translation model.

\par
The contrastive variational autoencoder (cVAE) \cite{abid2019contrastive} was designed to identify and enhance salient latent features by explicitly modeling latent features that are shared between the MNIST dataset \cite{lecun-mnisthandwrittendigit-2010}, as well as those that are enriched in one dataset relative to the other.

\par
\cite{tjandra2020transformer} built a Transformer-based Vector Quantised Variational AutoEncoder (VQ-VAE) for unsupervised unit discovery system that addresses two major components such as 1) given speech audio, extract subword units in an unsupervised way and 2) re-synthesize the audio from novel speakers. In VQ-VAE, the focus was on the discrete latent representation as opposed to continuous representations costing it to achieve likelihood close but not as good as the continuous representation.

The work proposed in this paper bears some resemblance to the HMM-VAE \cite{Raj_2017} or VQ-VAE \cite{tjandra2020transformer}  which used VAE for acoustic discovery but it is an improvement over VQ-VAE model as it leveraged the continuous latent space representation along with handling the non-linearities of the system. Also, EKF-VAE is an enhancement over HMM-VAE as it reduces the error and loss by taking care of the non-linearities of the system by updating the state, weights and covariance effectively.

\section{Background}
\label{section:background}
First, let me give a brief overview of Variational Autoencoder (VAE)s along with Recurrent Neural Network (RNN) based Encoder-Decoder.

\subsection{Variational Autoencoder}
Variational Autoencoders \cite{Kingma2014} (VAEs) got popularity in unsupervised learning \cite{varadarajan-etal-2008-unsupervised} of complicated distributions.

For every datapoint Z in the dataset, \cite{doersch2021tutorial} there is one (or many) settings of the latent variables which causes the model to generate something very similar to Z. The objective is to optimize $\theta$ such that we can sample x from p(x) and, with to maximize probability, f(x; $\theta$) with the aim maximize the probability of each Z in the training set under the entire generative process,
\begin{equation} \label{eq:vaeEq}
    P(Z) = \int P(Z | x; \theta)P(x) dx
\end{equation}

Here, $f(x; \theta)$ has been replaced by a distribution $p(Z | x; \theta)$, which allows us to make the dependence of Z on x explicit by using the law of total probability. Based on the principle of "Maximum Likelihood" if the model is likely to produce training set samples, then it is also likely to produce similar samples, and unlikely to produce dissimilar ones.

\begin{equation}
    \hat{x}_{ml} = arg \: \underset{x}{max} \:  p(z | x)
\end{equation}

In VAEs, the choice of this output distribution is Gaussian, i.e., $p( Z | x; \theta) = \mathcal{N} (Z | f(x; \theta), \sigma^{2}I)$.

\begin{figure}[H]
  \centering
  \includegraphics[scale=.4]{img/VAE.png}
  \caption{ Generative Model in solid lines for $p_{\theta}(x)p_{\theta}(z|x)$, dashed lines denote the variational approximation $q_{\phi}(x|z)$ to intractable posterior $p_{\theta}(x|z)$.  }
  \label{fig:fig1}
\end{figure}

The Kullback-Leibler divergence (KL divergence) between $p(x | Z)$ and Q(x) for some arbitrary Q, can be written while applying Bayes rule to $p(x | Z)$ as follows:
\begin{equation}
    \begin{split}
    K&L[Q(x)\: ||\: p(x | Z)] = \\
  & E_{x\sim Q}[log\: Q(x) - log\: p(Z | x) - log\: p(x)] + log\: p(Z)
    \end{split}
\end{equation}

Here, log p(Z) comes out of the expectation because it does not depend on latent variable x. The objective is to construct a Q which does depend on Z, and in particular, one which makes $KL[Q(x)\: ||\: p(x | z)]$ small:
\begin{equation}\label{eq:vaeOpt}
    \begin{split}
        log\: p(Z) &- KL[Q(x | Z)\: ||\: p(x | Z)] = \\
        &E_{x\sim Q}[log\: p(Z | x)] - KL[Q(x | Z)\: ||\: p(x)]
    \end{split}
\end{equation}
the left hand side of Equation \ref{eq:vaeOpt} has the quantity which needs to be maximized: log p(Z) (plus an error term, which makes Q produce x’s that can reproduce a given Z. The right hand side is optimized by Extended Kalman Filter (or stochastic gradient descent) given the right choice of Q.

\par
The first term in Equation \ref{eq:vaeOpt} is a bit more tricky. A possible option is to sample to estimate $E_{x\sim Q}[log\: p(Z | x)]$, but getting a good estimate would require passing many samples of x through f , which would be expensive. The full equation to optimize is:
\begin{equation}\label{vae2}
    \begin{split}
        E_{Z\sim KL}&[log\: p(Z) - KL[Q(x|Z)\: ||\: p(x | Z)] = \\
        & E_{Z\sim KL}[E_{x\sim Q}[log\: p(Z | x)] - KL[Q(x|Z)\: ||\: p(x)]]
    \end{split}
\end{equation}

Sample a single value of Z and a single value of x from the distribution $Q(x|Z)$, and compute the gradient of the right-hand side. We can then average the gradient of this function over arbitrarily many samples of X and z, and the result converges to the gradient of Equation \ref{vae2}.
\subsection{RNN Based Encoder-Decoder}
A novel architecture is built leveraging the {\it RNN Encoder-Decoder} proposed by \citet{cho2014learning} and \citet{sutskever2014sequence} to perform acoustic discovery.

In the Encoder-Decoder framework, an encoder reads the input sentence, a
sequence of vectors $x=\left( x_1, \cdots, x_{T_x} \right)$, into a vector
$c$. The most common approach is to use an RNN such that
\begin{align}
    \label{eq:forward_state}
    h_t = f\left( x_{t}, h_{t-1} \right)
\end{align}
and
\begin{align*}
    c = q\left(\left\{ h_1, \cdots, h_{T_x} \right\}\right),
\end{align*}
where $h_t \in \mathcal{R}^{n}$ is a hidden state at time $t$, and $c$ is a vector
generated from the sequence of the hidden states. $f$ and $q$ are some
nonlinear functions. \citet{sutskever2014sequence} used an LSTM as $f$ and
$q\left(\left\{ h_1, \cdots, h_T \right\}\right)=h_T$, for instance.

The decoder \cite{bahdanau2016neural} is trained to predict the next word $y_{t'}$ given the context vector $c$ and all the previously predicted words $\left\{ y_1, \cdots, y_{t'-1}
\right\}$. In other words, the decoder defines a probability over the translation by decomposing the joint probability into the ordered conditionals:
\begin{align}
    \label{eq:decoder_prob}
    p(y) = \prod_{t=1}^T p(y_t \mid \left\{ y_1, \cdots, y_{t-1} \right\}, c),
\end{align}
where $y = \left( y_1, \cdots, y_{T_y} \right)$. With an RNN, each conditional probability is modeled as
\begin{align}
    \label{eq:output_rnn}
    p(y_t \mid \left\{ y_1, \cdots, y_{t-1} \right\}, c) = g(y_{t-1}, x_{t}, c),
\end{align}
where $g$ is a nonlinear, multi-layered, function that outputs the probability of $y_t$, and $x_t$ is the hidden state of the RNN.

\section{Model}
\label{section:model}

Built the model using RNN, GRU Cell of PyTorch and developed Encoder and Decoder RNN as part of the VAE model for comparing the performance of the proposed model EKF-VAE against the HMM-VAE as baseline.

\subsection{Kalman Filter \& Extended Kalman Filter}
The Kalman filter \cite{SSS06Oxford} is being popular in predicting the next state of dynamic systems. It uses an iterative approach to tune the model to rectify the error.

\begin{figure}[H]
  \centering
  \includegraphics[scale=.22]{img/kalman.png}
  \caption{ Kalman Filter showing latent and observed variables}
  \label{fig:fig2}
\end{figure}

The Kalman filter and smoother are based on the following probabilistic model \cite{JWMiller16}.
\begin{itemize}
\item Like a discrete-state HMM as shown in the Figure \ref{fig:fig2}, the sequence of observations $z_{1},z_{2}, . . . , z_{n}$ is modeled jointly along with a sequence of hidden latent states $x_{1}, x_{2}, ....., x_{n}$ with the assumption that:
\begin{equation}
p(z_{1:n}, x_{1:n}) = p(x_{1})p(z_{1}|x_{1}) \displaystyle\prod_{j=2}^{n} p(x_{j}|x_{j-1})p(z_{j}|x_{j})
\end{equation}
\item Difference from a discrete-state HMM is that each hidden state $x_j$ is modeled as a continuous random variable in $\mathcal{R}^d$ with a multivariate normal distribution.

\item The initial distribution $p(x_{1})$, the transition distributions $p(x_{j} | x_{j-1})$ (a.k.a. the "process model") and the emission distributions $p(z_{j} | x_{j} )$ (a.k.a. the "measurement model") are assumed to be
\begin{equation}
\begin{split}
p(x_{1}) = &\mathcal{N} (x_{1} | \mu_{0}, P_{0}) \\
p(x_j | x_{j - 1}) = &\mathcal{N}  (x_{j}| F x_{j - 1}, Q) \\
p(z_j | x_j) = &\mathcal{N}  (z_j| Hx_j, R)
\end{split}
\end{equation}
where
\begin{itemize}
    \item $x_{j} \in \mathcal{R}^{d} $ (the state of the system at time step j),
    \item $z_{j} \in \mathcal{R}^d$ (the measurements at time step j),
    \item $\mu_{0} \in \mathcal{R}^d$ is an arbitrary vector (the initial “best guess” at the initial state),
    \item $P_{0} \in \mathcal{R}^{d \times d}$ is a symmetric positive definite matrix (the initial covariance matrix, quantifying the uncertainty about the initial state),
    \item $F \in \mathcal{R}^{d \times d}$ is an arbitrary matrix (modeling the physics of the process nonlinear vector function, or a linear approximation thereof),
    \item $Q \in \mathcal{R}^{d \times d}$ is a symmetric positive definite matrix (quantifying the noise/error in the process that is not captured by F),
    \item $H \in \mathcal{R}^{D \times d}$ is an arbitrary matrix (relating the measurements to the state),
    \item $R \in \mathcal{R}^{D \times D}$is a symmetric positive definite matrix (quantifying the noise/error of the measurements).
\end{itemize}
\item The model can easily be extended to handle time-dependence in F, Q, H, and R, by simply replacing them with $F_{j}, Q_{j}, H_{j}, and R_{j}$ in the expressions above.
\end{itemize}

The Kalman filter (KF) is a method based on recursive Bayesian filtering where the noise in the system is assumed to be Gaussian. The \textbf{Extended Kalman Filter} (EKF) is an extension of the classic Kalman Filter for non-linear systems where non-linearity are approximated using the first or second order derivative.

\subsection{Extended Kalman Filter: Forward Algorithm}
The Extended Kalman filter (EKF) is the nonlinear version of the Kalman filter which linearizes about an estimate of the current mean and covariance.

\textbf{Model and Observation:}\\
Consider the nonlinear system, described by the difference equation and the observation
model with additive noise:
\begin{equation}
    x_{j} = f(x_{j-1}) + w_{j-1}
\end{equation}
\begin{equation}
    z_{j} = h(x_{j}) + v_{j}
\end{equation}

\textbf{Initialization:}\\
The initial state $x_{0}$ is a random vector with known mean $\mu_{0} = E[x_{0}]$ and covariance $P_{0} = E[(x_{0} - \mu_{0})(x_{0} - \mu_{0})^{T} ]$.

In the Extended Kalman Filter forward algorithm, Compute $p(x_j | z_{1:j})$ sequentially for j = 1, 2, ...., n in that order. Here is the generalized form for step j \cite{JWMiller16}, $p(x_{j-1} | z_{1 : j-1}) = \mathcal{N}(x_{j - 1} | \mu_{j - 1}, V_{j - 1})$

\textbf{Model Forecast Step/Predictor:}\\
The forecast value for $x_{j}$ is $x_{j}^{f}$, which can be expressed as:
\begin{equation}
    x_{j}^{f} \approx f(x_{j-1}^{a})
\end{equation}

\textbf{Data Assimilation Step/Corrector:}\\
The state-estimate $x_{j}^{a}$ can be expressed as:
\begin{equation}
    x_{j}^{a} \approx x_{j}^{f} + K_{j}(z_{j} - h(x_{j}^{f}))
\end{equation}
Kalman Gain at step j $K_{j}$ can be expressed as:
\begin{equation}
    K_{j} = P_{j-1}H^{T}(H P_{j-1}H^{T} + R)^{-1}
\end{equation},

Update the matrix P as it captures the uncertainty about the initial state and improve based on the Kalman Gain $K_{j}$
\begin{equation}
    P_{j} = (I - K_{j}H)P_{j-1}
\end{equation}
The update of weights of the neural network $dW_{j}$ can be computed based on the difference of the actual output y and the approximate Jacobian H factored by the Kalman Gain. Approximated Jacobian (H) based on weight, $d\sigma$.

Putting the above equations altogether, the algorithm \ref{alg:ekfalgo} can be described as follows.

\begin{algorithm}[tbh]
   \caption{Extended Kalman Filter Forward Algorithm}
   \label{alg:ekfalgo}
\begin{algorithmic}
   \STATE {\bfseries Input:} Input state $x_{j}$, Observed data $z_j$, size $n$ and model parameters $P_{0}$, F, Q, H, R, step
   \STATE {Initialization:}
   \STATE $ K_{1} = P_{0}H^{T}(HP_{0}H^{T} + R)^{-1} $ \\
   \STATE $ P_{1} = (I - K_{1}H)P_{0} $ \\
   \FOR{$j = 2$ {\bfseries to} $n$}
      \STATE Approximate Jacobian H
      \STATE $ K_{j} = P_{j-1}H^{T}(HP_{j-1}H^{T} + R)^{-1} $ \\
      \STATE $P_{j} = (I - K_{j}H)P_{j-1} $  \\
      \STATE $ dW_{j} = step K_{j} (z_{j} - H)$
      \STATE $W_{j} = W_{j-1} + dW_{j}$
      \IF{Q != 0}
        \STATE $P_{j} = P_{j} + Q$
      \ENDIF
   \ENDFOR
\end{algorithmic}
\end{algorithm}
In the above algorithm \ref{alg:ekfalgo}, P is the variance of the state estimation, Q is the variance of the process noise, R captures the variance of the measurement noise.

The Kalman filter is identical to the forward algorithm for discrete-state HMMs, except that it is expressed in terms of $\mu_{j}, V_{j}$ instead of $s_{j} (z_{j})$ (and the derivation involves an integral instead of a sum).

\subsection{EKF-VAE}
The Extended Kalman Filter based Variational Autoencoder (EKF-VAE) is built on a Sequence-to-Sequence model made up of an EncoderRNN module and a DecoderRNN module which leverage Extended Kalman Filter.

The forward behavior depends on whether ground-truth is being provided or not. When ground-truth is provided it returns cross-entropy loss, else, it returns predicted word (id).

\begin{figure}[H]
  \centering
  \includegraphics[scale=.20]{img/EKF-VAE-Loss.png}
  \caption{EKF-VAE uses EKF for updating weights and state-estimates. A RNN based Encoder-Decoder to reconstruct by generative process.}
  \label{fig:fig3}
\end{figure}

The Figure \ref{fig:fig3} shows a schematic diagram of the EKF based VAE which consists of the following:
\begin{itemize}
    \item EKF is a neural network to update state-estimation and covariance matrix to provide better prediction.
    \item RNN Based Encoder-Decoder to perform regenerative actions
    \item Check against ground-truth (when provided) to compute loss, else, provide reconstructed input.
    \item Observed and latent variables.
\end{itemize}
The EKF updates the weight and covariance matrix to improve the state-estimation. The Encoder RNN is a Bi-directional multi-layer gated recurrent unit (GRU) RNN. The Attention decoder is based on \textbf{Listen, Attend and Spell (LAS)} \cite{chan2015listen}.

The Decoder RNN applies two simple and effective classes of attentional mechanism: a global approach which always attends to all source words and a local one that only looks at a subset of source words at a time similar to the attentional mechanism proposed by \cite{luong2015effective}.

The novel EKF-VAE algorithm calls EKF (described in the algorithm \ref{alg:ekfalgo}) to update the weights of the neural network to provide better convergence by predicting the state variables and covariance matrix. Adam optimizer is being used with the $torch.optim.lr\_scheduler.ReduceLROnPlateau$ scheduler as it allows dynamic learning rate reducing based on some validation measurements. The Kalman Filter based Neural Net (EKF) is a feed-forward neural network (NN). The EKF implements the Extended Kalman Filter algorithm to train. Technically, it could potentially be possible to be trained by stochastic gradient descent (SGD). Trained the NN using the feed-forward function to compute the NN output, and the classify function to round a feed-forward to the nearest class values. Also check-pointed the EKF object in the working directory.

\begin{algorithm}[tbh]
   \caption{EKF Feed Forward Algorithm}
   \label{alg:ekfFeedAlgo}
\begin{algorithmic}
   \STATE {\bfseries Input:} Input training data x, activation function $\sigma$, current weight $W_{j}$ of the neural network \\
   \STATE $l = \sigma (W_{j} x) $ \\
   \STATE $ h = W_{j} l$  \\
   \STATE  return h, l
\end{algorithmic}
\end{algorithm}

Updating the weights of the neural network using EKF allows us to predict and update the states. The hypothesis behind weight update using EKF is to capture the non-linearities of the underlying system.

\section{Experiments}
\label{section:experiments}
For experimenting Extended Kalman Filter (EKF) based Variational Autoencoder (VAE), applied Extended Kalman Filter based forward prediction specified in algorithm \ref{alg:ekfalgo} along with Feed Forward algorithm \ref{alg:ekfFeedAlgo} at each step of the training to improve the prediction of the dynamic state. As the EKF improves the prediction at each step the error (Root Mean Square) reduces as compared to HMM-VAE).

Applied the EKF-VAE model on TIMIT \cite{timit-article} data and computed the error by computing the distance between the actual and predicted data and learning from it.

\subsection{Dataset: TIMIT}
The Texas Instruments/Massachusetts Institute of Technology (TIMIT) \cite{timit-article} corpus of read
speech has been designed to provide speech data for the acquisition of acoustic-phonetic knowledge and for the development and evaluation of automatic speech recognition systems. TIMIT contains speech from 630 speakers representing 8 major dialect divisions of American English, each speaking 10 phonetically-rich sentences. The TIMIT corpus includes time-aligned orthographic, phonetic, and word transcriptions, as well as speech waveform data for each spoken sentence.

The acoustic features are 80-dimensional filter banks. They are stacked every 3 consecutive frames, so the time resolution is reduced. Following the standard recipe, used a 462-speaker training set with all SA records removed. Outputs are mapped to 39 phonemes when evaluating.

\subsection{Experiment Settings}
The EKF-VAE consists of an Encoder Recurrent Neural Network (RNN) and an Decoder Recurrent Neural Network (RNN). The Encoder RNN consists of three encoder layers and the Decoder RNN consists of two decoder layers with Relu activation.
The encoder has an input size of 240. Both encoder and decoder have a hidden unit size of 256. The target size is equal to the vocabulary size of the tokenizer.For training purposes, used a batch size of 64 with a dropout percentage of 0.5.

\subsection{Results}
Trained the EKF-VAE algorithm to perform the acoustic discovery on TIMIT data. The algorithm was trained for 50 iterations and then 100 iterations. Observed that the Root Mean Square Error (RMSE) goes down very fast and but the dev loss plateaued for both EKF-VAE as well as HMM-VAE, but the rate of reduction of RMSE is faster for ELF-VAE and remained even as the number of iterations have been increased.

\begin{figure*}[h]
  \centering
  \begin{minipage}[b]{0.4\textwidth}
    \includegraphics[width=\textwidth]{img/TrainLoss.pdf}
    \caption{Training Loss}
    \label{fig:fig4}
  \end{minipage}
  \hfill
  \begin{minipage}[b]{0.4\textwidth}
    \includegraphics[width=\textwidth]{img/DevLoss.pdf}
    \caption{Evaluation/Dev Loss}
    \label{fig:fig5}
  \end{minipage}
\end{figure*}
As shown in Figure \ref{fig:fig4}, the EKF-VAE achieved more than 47\% reduction in training loss at the end of 100 iterations (epochs) and experienced a maximum of 49\% reduction in dev loss individual epoch as shown in Figure \ref{fig:fig5}. Although both the HMM and EKF are Gaussian based, the training loss reduction is better for EKF-VAE (shown in green) as compared to HMM-VAE as EKF handles the non-linearity and predicts the dynamic state better. As shown in Figure \ref{fig:fig5}, the evaluation (dev) loss has increased in EKF-VAE in later iterations and HMM over-performed by 10\%.

\begin{figure}[H]
    \includegraphics[scale=.55]{img/ErrorRate.pdf}
    \caption{Error Rate}
    \label{fig:fig6}
\end{figure}

The Kalman filter seems to be effective in modeling language as word utterances can be modeled as discrete occurrences of words. Applying Kalman Filter at each step forced the current state uncertainty $P_{j}$ to converge faster causing error rate to go down. For acoustic discovery use-case, the error rate has been reduced up to 37\% by Extended Kalman Filter based VAE (EKF-VAE) over HMM VAE as shown in Figure \ref{fig:fig6}.

\begin{figure}[htb]
    \includegraphics[scale=.5]{img/RMSE.pdf}
    \caption{Root Mean Square Error}
    \label{fig:fig7}
\end{figure}

The error has been computed based on distance of expected and predicted word. The root-mean square error (RMSE) as shown in Figure \ref{fig:fig7} reduced by 57\% at individual epoch in EKF-VAE as compared to its HMM-VAE counterpart.

\begin{figure}[htb]
    \includegraphics[scale=.5]{img/EpochDuration.pdf}
    \caption{Epoch Duration Comparison between HMM-based and EKF-based VAE}
    \label{fig:fig8}
\end{figure}

But, as the EKF is being called in each iteration, it increases the training time. It is essential to understand the trade-off between incurring an additional cost of time for extended kalman filter based prediction to improve convergence and the error reduction rate. The additional Extended Kalman Filter computation increases the training time at every iteration which resulted in less than 14\% increase in epoch duration as shown in Figure \ref{fig:fig8}.

\begin{figure}[H]
    \includegraphics[scale=.5]{img/lr.pdf}
    \caption{Learning Rate Comparison between HMM-VAE and EKF-VAE}
    \label{fig:fig9}
\end{figure}

As shown in Figure \ref{fig:fig9} the learning rate between HMM-VAE and EKF-VAE, the learning rate for HMM-VAE dropped to 0.00005 while the learning rate for EKF-VAE remained at 0.00015. The HMM-VAE kept learning at a higher rate potentially can cause an oscillation in predicting the words. The lower the learning rate, the lesser is the oscillations and hence can expect to have a better convergence.

The EKF-VAE has predicted the words more accurately than its HMM rivals as it predicted the discrete state more accurately. EKF kept computing the Kalman Gain at each step to decide how much to tune/update the weights to provide a better prediction resulting in superior acoustic discovery.

\begin{figure}[H]
  \centering
  \includegraphics[scale=.25]{img/EKF-Predict.png}
  \caption{ Extended Kalman Filter based VAE Prediction}
  \label{fig:fig10}
\end{figure}
The EKF based predictions as shown in Figure \ref{fig:fig10} and Figure \ref{fig:fig11} demonstrates the actual word prediction.
\begin{figure}[H]
  \centering
  \includegraphics[scale=.42]{img/TIMIT_EKF.png}
  \caption{Acoustic Discovery}
  \label{fig:fig11}
\end{figure}
Using RNN-Based Encoder-Decoder along with the attention model allowed us to have a better word prediction.

\section{Conclusions}
\label{section:conclusions}
The proposed an Extended Kalman Filter (EKF) Powered Variational Autoencoder (VAE) for acoustic discovery has achieved a faster convergence as it models the latent space better. From the results, it is evident that the EKF based Variational Model outperforms the HMM based Variational model. The EKF-VAE provides significant improvement over HMM based VAE due to better prediction of the dynamic state by the Kalman filter. Finally, it is shown that the EKF-VAE can reduce the error significantly (~49\%) to assure superior prediction accuracy.

%%%%%%%%%%%%% Acknowledgements %%%%%%%%%%%%%
%\footnotesize
\section{Acknowledgements}
Thanks to Prof Bhiksha Raj Ramakrishnan of the Carnegie Mellon University for his immense encouragement and suggestions. Thanks to Nihit Purwar for his support. Thanks to my beautiful wife Mahaswata for motivating me to pursue my passion and buying an NVIDIA RTX2080 based laptop on which all the experiments were performed.

%%%%%%%%%%%%%%   Bibliography   %%%%%%%%%%%%%%
\normalsize
\bibliography{references}
\bibliographystyle{icml2020}

% In the unusual situation where you want a paper to appear in the
% references without citing it in the main text, use \nocite
%\nocite{langley00}

\end{document}

% This document was modified from the file originally made available by
% Pat Langley and Andrea Danyluk for ICML-2K. This version was created
% by Iain Murray in 2018, and modified by Alexandre Bouchard in
% 2019 and 2020. Previous contributors include Dan Roy, Lise Getoor and Tobias
% Scheffer, which was slightly modified from the 2010 version by
% Thorsten Joachims & Johannes Fuernkranz, slightly modified from the
% 2009 version by Kiri Wagstaff and Sam Roweis's 2008 version, which is
% slightly modified from Prasad Tadepalli's 2007 version which is a
% lightly changed version of the previous year's version by Andrew
% Moore, which was in turn edited from those of Kristian Kersting and
% Codrina Lauth. Alex Smola contributed to the algorithmic style files.


% --------------------------- %
% Bakery Start
% --------------------------- %
\section{\textbf{Bakery Test}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Particular Case}
\par
In this experiment, we are dealing with the problem of mutual exclusion with a
number of participants $>2$.
\par
We have already seen that Peterson Algorithm works for two threads. It guarantees
mutual exclusion and is starvation free. Therefore, the algorithm is deadlock
free. Now let us focus in an algorithm that can be used to coordinate more
threads.
\par
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Solution}
\par
The algorithm that we will play with is called \textit{Bakery}. Its name comes from the
fact that it is similar to the protocol used in bakeries where one enters the
store and picks a number that indicates the order in which each client will be
attended.
\par
In the algorithm, the fact of entering the store is done by setting a flag.
After that, the thread calculates the next number in the machine.
\par
The algorithm then chooses the next number to be attended. When that happens,
the thread is allowed to enter the critical section.
\par
One thing that we ought to mention is that threads can have the same number
assigned. If that is the case, the next in the line is decided using a
lexicographical ordering of the threads ids.
\par
Here we show the interesting methods used in this algorithm:
\par
\hfill
\begin{lstlisting}[style=numbers]
  public void lock() {
    int me = ThreadID.get();
    flag[me]  = true;
    int max = Label.max(label);
    label[me] = new Label(max + 1);
    while (conflict(me)) {};  // spin
  }

  public void unlock() {
    flag[ThreadID.get()] = false;
  }

  private boolean conflict(int me) {
    for (int i = 0; i < label.length; i++) {
      if (i != me && flag[i] && label[me].compareTo(label[i]) < 0) {
        return true;
      }
    }
    return false;
  }

  static class Label implements Comparable<Label> {
    int counter;
    int id;
    Label() {
      counter = 0;
      id = ThreadID.get();
    }
    Label(int c) {
      counter = c;
      id = ThreadID.get();
    }
    static int max(Label[] labels) {
      int c = 0;
      for (Label label : labels) {
        c = Math.max(c, label.counter);
      }
      return c;
    }

    public int compareTo(Bakery.Label other) {
      if (this.counter < other.counter
          || (this.counter == other.counter && this.id < other.id)) {
        return -1;
      } else if (this.counter > other.counter) {
        return 1;
      } else {
        return 0;
      }
    }
  }
\end{lstlisting}
\hfill
\par
In the \textit{lock()} method, we observe that the thread signals its intention
of accessing the critical section by setting its flag to true. After that, the
thread picks its number and then it sits and waits for its turn. The thread has
to wait if another thread which announced its intention to use the resource has
a smaller label.
\par
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Experiment Description}
\par
Now let us explain how the experiments work. In this case we have 8 threads
increasing a counter from 0 to 1024. Each thread will increase the counter 128
times. At the end, the counter must remain in 1024. If that is not the case,
then there is a problem with the mutual exclusion algorithm.
\par
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Sample Results}
\par
In this exercise we observed that in the machine that we have been using, the
test for the Bakery algorithm fails consistently with the following error:
\par
\begin{verbatim}
.F
Time: 0.019
There was 1 failure:
1) testParallel(mutex.BakeryTest)junit.framework.AssertionFailedError:
expected:<1019> but was:<1024>
        at mutex.BakeryTest.testParallel(BakeryTest.java:47)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

FAILURES!!!
Tests run: 1,  Failures: 1,  Errors: 0
\end{verbatim}
\par
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Interpretation}
\par
Let us elaborate on the meaning of the error that we got. The assertion failure
says that at the end of the algorithm, our counter didn't reach the expected
1024. This problem might indicate that our counter wasn't actually being
protected by the lock because it looks like multiple threads found the counter
with a value of $i$ and increased it to $i+1$. This is wrong.
\par
After investigating the problem in more detail, we found out that the problem in
this code has to do with the way the \textit{label} array is accessed and
modified. For example, in the lock method, multiple threads can reach the point
where the max is calculated and hence these mutiple threads can take the same
turn. The algorithm takes care of this problem by introducing a lexicographical
ordering in the comparison: if two threads have the same number, the decision is
made based on the $threadID$.
\par
Also, as explained in the textbook, we cannot be sure that we have a correct
memory consistency: it is possible that the processor modified the order in
which operations in the \textit{lock()} method are executed. It is also possible
that the processor decided to execute an instruction before another.
\par
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Proposed solution}
\par
The following fragment of code shows one possible way of fixing the code:
\par
\hfill
\begin{lstlisting}[style=numbers]
  public void lock() {
    int me = ThreadID.get();
    flag[me]  = true;
    synchronized (label){
      int max = Label.max(label);
      label[me] = new Label(max + 1);
      while (conflict(me)) {};  // spin
    }
  }
\end{lstlisting}
\hfill
Note that this solution is not quite satisfactory as we are basically using a
Java mechanism to ensure synchronized access in an algorithm that promises to
provide synchronized access.
\par
In any case, let us show the output of the program after this fix:
\begin{verbatim}
.
Time: 0.015

OK (1 test)
\end{verbatim}
\hfill
\par
After the fix the results in the proposed system were all OK. In all cases, the
threads were able to cooperate to increase the counter to 1024.
\par
One thing that is worth mentioning is that unlike the Peterson Algorithm, for
example, the Bakery algorithm is able to coordinate multiple threads (at least
in theory). This fact was shown in the test case were 8 threads were coordinated
to increase the counter.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --------------------------- %
% Bakery End
% --------------------------- %