% 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]{beamer}
%\usetheme[everytitleformat=uppercase]{m}
\usetheme[everytitleformat=regular]{m}
\usepackage[absolute,overlay]{textpos}
\usepackage{booktabs}
\usepackage[utf8]{inputenc}


\usepackage[official]{eurosym}

\usepackage[scale=2]{ccicons}


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


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

\def\l{\lambda}
\def\m{\mu}
\def\d{\partial}
\def\cL{\mathcal{L}}
\def\co2{CO${}_2$}



% 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*}{4cm}(8.0cm,8.0cm)
    \begin{beamercolorbox}[ht=0.5cm,right]{framesource}
        \usebeamerfont{framesource}\usebeamercolor[fg]{framesource} Source: {#1}
    \end{beamercolorbox}
\end{textblock*}}

\usepackage{hyperref}


%\usepackage[pdftex]{graphicx}


\graphicspath{{graphics/}}

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



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



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

\title{Electricity Markets: Summer Semester 2016, Lecture 2}
%\subtitle{---}
\author{
  Tom Brown, Mirko Schäfer
}

\institute{
  Frankfurt Institute of Advanced Studies (FIAS), Goethe-Universit\"at Frankfurt \\
  FIAS Renewable Energy System and Network Analysis (FRESNA) \\
  \\
  \url{{brown, schaefer}@fias.uni-frankfurt.de}
}


\date{18th April 2016}


\titlegraphic{%
  \vspace{7cm}
    \includegraphics[width=4cm]{FIAS-logo}
}

\begin{document}

\maketitle


\begin{frame}

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



\section{Short-run Efficient Operation of Electricity Markets}

\begin{frame}{Efficient Markets for the short-run}


  Assume investments already made in generators and and consumption assets (factories, machines, etc.).

  Assume all actors are price takers (i.e. nobody can exercise market power) and we have perfect competition.

  How do we allocate production and consumption in the most efficient way?


  I.e. we are interested in the short-run ``static'' efficiency.
\end{frame}


\section{Electricity Markets from the Consumer Perspective}


\begin{frame}{Consumer behaviour: Theory}

  Suppose for some given period a consumer consumes electricity at a rate of
  $Q$~MW.


  Their \alert{utility or value function} $U(Q)$ in \euro/h is a
  measure of their benefit for a given consumption rate $Q$.

  For a firm this could be the profit related to this electricity
  consumption from manufacturing goods.


  Typical the consumer has a higher utility for higher $Q$, i.e. the
  first derivative is positive $U'(Q) > 0$. By assumption, the rate of
  value increase with consumption decreases the higher the rate of
  consumption, i.e. $U''(Q) < 0$.

\end{frame}


\begin{frame}{Utility: Example}


  A widget manufacturer has a utility function which depends on the
  rate of electricity consumption $Q$ [\euro/h] as
  \begin{equation*}
    U(Q) = 0.0667~Q^3  - 8~Q^2 + 300~Q
  \end{equation*}


  \centering
  \includegraphics[width=6cm]{utility}


  Note that the slope is always positive, but becomes less positive
  for increasing $Q$.
\end{frame}


\begin{frame}{Optimal consumer behaviour}



  We assume to begin with that the consumer is a price-taker,
  i.e. they cannot influence the price by changing the amount they
  consume.

  Suppose the market price is $\lambda$ \euro/MWh. The consumer should
  adjust their consumption rate $Q$ to maximise their \alert{net surplus}
  \begin{equation*}
    \max_Q \left[U(Q) - \l Q \right]
  \end{equation*}

  This optimisation problem is optimised for $Q=Q^*$ where
  \begin{equation*}
    U'(Q^*) \equiv \frac{dU}{dQ} (Q^*) =  \l
  \end{equation*}

  [Check units: $\frac{dU}{dQ}$ has units $\frac{\textrm{\euro/h}}{\textrm{MW}} = $ \euro/MWh.]

  I.e. the consumer increases their consumption until they make a net loss for any increase of consumption.

  $U'(Q)$ is known as the \alert{inverse demand curve} or \alert{marginal utility curve}, which shows,
  for each rate of consumption $Q$ what price $\l$ the consumer should be
  willing to pay.


\end{frame}



\begin{frame}{Inverse demand function: Example}
  For our example the inverse demand function is given by
  \begin{equation*}
    U'(Q) = 0.2~Q^2 - 16~Q + 300
  \end{equation*}

  \centering
  \includegraphics[width=6cm]{inverse_demand_function}

  It's called the \emph{inverse} demand function, because the demand function is the function you get from reversing the axes.
\end{frame}


\begin{frame}{Inverse demand function: Example}


  The \alert{demand function} $D(\l)$ gives the demand $Q$ as a function of the price $\l$. $D(U'(Q)) = Q$.

  For our example the demand function is given by
  \begin{equation*}
    D(\l) =  -((\l+20 )/0.2)^{0.5} +40
  \end{equation*}

  \centering
  \includegraphics[width=6cm]{demand_function}

\end{frame}



\begin{frame}{Gross consumer surplus}

  The area under the inverse demand curve is the \alert{gross consumer surplus},
  which as the integral of a derivative, just gives the utility
  function $U(Q)$ again, up to a constant.

  \centering
  \includegraphics[width=6cm]{gross_consumer_surplus}

\end{frame}



\begin{frame}{Net consumer surplus}

  The more relevant \alert{net consumer surplus}, or just
  \alert{consumer surplus} is the net gain the consumer makes by
  having utility above the electricity price.

  \centering
  \includegraphics[width=6cm]{net_consumer_surplus}

\end{frame}


\begin{frame}{Limits to consumption}


  Note that it is quite common for consumption to be limited by other
  factors before the electricity price becomes too expensive, e.g. due
  to the size of electrical machinery. This gives an upper bound
  \begin{equation*}
    Q \leq Q^{\textrm{max}}
  \end{equation*}
  In the following case the optimal consumption is at
  $Q^* = Q^{\textrm{max}} =$  10~MW. We have a \alert{binding} constraint and can define a \alert{shadow price} $\m$, which indicates the benefit of relaxing the constraint $\m^* = U'(Q^*) - \l$.

  \centering
  \includegraphics[width=6cm]{net_consumer_surplus-limited}

\end{frame}



\begin{frame}
  \frametitle{Consumers can delay their consumption}

  Besides changing the amount of electricity consumption, consumers
  can also shift their consumption in time.

  For example electric storage heaters use cheap electricity at night
  to generate heat and then store it for daytime.

  The LHC particle accelerator does not run in the winter,
  when prices are higher (see
  \url{http://home.cern/about/engineering/powering-cern}).  Summer
  demand: 200~MW, corresponds to a third of Geneva, equal to peak
  demand of Rwanda (!); winter only 80~MW.


  \centering
  \includegraphics[width=6cm]{csm_1998_LHCUeberblick_CERN_02_ed178e98ea}


  \source{CERN}



\end{frame}


\begin{frame}
  \frametitle{Consumers can also move location}

  Aluminium smelting is an electricity-intensive process. Aluminium
  smelters will often move to locations with cheap and stable
  electricity supplies, such as countries with lots of hydroelectric
  power.  For example, 73\% of Iceland's total power consumption in
  2010 came from aluminium smelting.

  Aluminium costs around US\$ 1500/ tonne to produce. %http://www.lme.com/metals/non-ferrous/aluminium/

  Electricity consumption: 15 MWh/tonne.

  At Germany consumer price of \euro 300 / MWh, this is \euro 4500 / tonne. Uh-oh!!!

  If electricity is 50\% of cost, then need \$750/tonne to go on electricity $\Rightarrow$ 750/15 \$/MWh = 50 \$/MWh.

  %https://arcticecon.wordpress.com/2012/02/15/aluminium-smelting-in-iceland-alcoa-rio-tinto-alcan-century-aluminum-corp/
  %15.7 kWh of electricity to produce 1 kg of aluminium.  Al203 (alumina) -> Al

  % The Hall-Héroult electrolysis process is the major production route for primary aluminium.

  %Due to the nature of the process, power outages have the potential
  %to cause damage to production cells as the molten liquids could
  %solidify in absence of adequate current.  For this reason,
  %production facilities need to be near secure and reliable sources
  %of energy.  step before smelting is bauxite -> alumina. Bauxite is
  %gibbsite Al(OH)3, boehmite γ-AlO(OH), and diaspore α-AlO(OH).


%What is notable is that the aluminium industry’s total power usage
  %amounted to roughly 73% of Iceland’s total power consumption in 2010.

  %https://en.wikipedia.org/wiki/Economy_of_Iceland#Aluminium

  %https://agmetalminer.com/2015/11/24/power-costs-the-production-primary-aluminum/

  %making domestic electricity costs some 44% of the sales price of
  %ingot and may explain why Chinese smelters combined with low
  %capacity utilization due to reduced demand are widely reported

  %http://www.crugroup.com/about-cru/cruinsight/Aluminium_Smelter_Power_Tariffs_Winners_and_Losers

%Power typically represents more than a third of an aluminium
%smelter's operating costs and is the single most important cost
%variant between smelting assets. As a result access to cheap power is
%one of the key sources of competitive advantage in primary aluminium
%smelting.

  % https://en.wikipedia.org/wiki/List_of_aluminium_smelters

  % THere are some in Germany

  \end{frame}

\section{Electricity Markets from the Generator Perspective}


\begin{frame}
  \frametitle{Generator Cost Function}


  Optimal generator behaviour follows a similar schema to that for consumers.

  A generator has a \alert{cost or supply function} $C(Q)$ in \euro/h, which
  gives the costs (of fuel, etc.) for a given rate of electricity
  generation $Q$~MW.

  Typical the generator has a higher cost for a higher rate of generation $Q$,
  i.e. the first derivative is positive $C'(Q) > 0$. For most generators the rate at which cost increases with rate of production itself increases as the rate of production increases, i.e. $C''(Q) > 0$.

\end{frame}


\begin{frame}{Cost Function: Example}


  A gas generator has a cost function which depends on the
  rate of electricity generation $Q$ [\euro/h] according to
  \begin{equation*}
    C(Q) = 0.005~Q^2  + 9.3~Q + 120
  \end{equation*}


  \centering
  \includegraphics[width=7cm]{generator_cost_function}


  Note that the slope is always positive and becomes more positive
  for increasing $Q$. The curve does not start at the origin because of startup costs, no load costs, etc.
\end{frame}



\begin{frame}{Real Example: California Day Ahead (DA) Market}

  Generators in California provide supply curves to the market operator as a piecewise linear function with ten segments:

  \centering
  \includegraphics[width=9cm]{california-ten-segment}

  \source{CAISO}

\end{frame}

\begin{frame}{Optimal generator behaviour}

  We assume to begin with that the generator is a price-taker,
  i.e. they cannot influence the price by changing the amount they
  generate.

  Suppose the market price is $\lambda$ \euro/MWh. For a generation
  rate $Q$, the revenue is $\l Q$ and the generator should adjust their
generation rate $Q$ to maximise their \alert{net generation surplus}, i.e. their profit:
  \begin{equation*}
    \max_Q \left[ \l Q - C(Q) \right]
  \end{equation*}

  This optimisation problem is optimised for $Q=Q^*$ where
  \begin{equation*}
    C'(Q^*) \equiv \frac{dC}{dQ} (Q^*) =  \l
  \end{equation*}

  [Check units: $\frac{dC}{dQ}$ has units $\frac{\textrm{\euro/h}}{\textrm{MW}} = $ \euro/MWh.]

  I.e. the generator increases their output until they make a net loss for any increase of generation.

  $C'(Q)$ is known as the \alert{marginal cost curve}, which shows,
  for each rate of generation $Q$ what price $\l$ the generator should be
  willing to supply at.


\end{frame}



\begin{frame}{Marginal cost function: Example}

  For our example the marginal function is given by
  \begin{equation*}
    C'(Q) = 0.001~Q + 9.3
  \end{equation*}

  \centering
  \includegraphics[width=7cm]{marginal_cost_function}

\end{frame}





\begin{frame}{Generator surplus}

  The area under the curve is generator costs, which as the integral
  of a derivative, just gives the cost function $C(Q)$ again, up to a
  constant.


  The \alert{generator surplus} is the profit the generator makes by
  having costs below the electricity price.

  \centering
  \includegraphics[width=7cm]{generator_surplus}

\end{frame}



\begin{frame}{Limits to generation}


  Note that it is quite common for generators to be limited by
  e.g. their capacity, which may become a \alert{binding}, i.e. limiting factor before the price plays a role, e.g.
  \begin{equation*}
    Q \leq Q^{\textrm{max}}
  \end{equation*}
  In the following case the optimal generation is at
  $Q^* = Q^{\textrm{max}} = $ 250~MW.   We have a \alert{binding} constraint and can define a \alert{shadow price} $\m$, which indicates the benefit of relaxing the constraint $\m^* = \l - C'(Q^*)$.


  \centering
  \includegraphics[width=7cm]{generator_surplus-limited}

\end{frame}



\section{Putting generators and consumers together}



\begin{frame}{Spoiler: Setting the quantity and price}

  Total welfare (consumer and generator surplus) is maximised if the
  total quantity is set where the marginal cost and marginal utility curves meet.

  If the price is also set from this point, then the individual
  optimal actions of each actor will achieve this result in a perfect
  decentralised market.

  \centering
  \includegraphics[width=8cm]{consumer_producer_surplus.pdf}




\end{frame}



\begin{frame}{Spoiler: The result of optimisation}

  This is the result of maximising the total economic welfare, the sum of the consumer and the producer surplus for consumers with consumption $Q_i^B$ and generators generating with rate $Q_i^S$:
  \begin{align*}
    \max_{\{Q_i^B\}, \{Q_i^S\}}\left[ \sum_i U_i (Q_i^B)  -  \sum_i C_i (Q_i^S) \right]
  \end{align*}
  subject to the supply equalling the demand in the balance constraint:
  \begin{align*}
    \sum_i Q_i^B -  \sum_i Q_i^S  = 0 \hspace{1cm} \leftrightarrow \hspace{1cm} \l
  \end{align*}
  and any other constraints (e.g. limits on generator capacity, etc.).

  Market price $\l$ is the shadow price of the balance constraint,
  i.e. the cost of supply an extra increment 1~MW of demand.

\end{frame}


\begin{frame}{Why decentralised markets work (in theory)}

  We will now show our main result:

      \begin{alertblock}{Welfare-maximisation through decentralised markets}
        The welfare-maximising combination of production and consumption can be achieved by the decentralised profit-maximising decisions of producers and the utility-maximising decisions of consumers, provided that:
        \begin{itemize}
        \item The market price is equal to the constraint marginal value of the overall supply-balance constraint in the welfare maximisation problem
          \item All producers and consumers are price-takers
        \end{itemize}
      \end{alertblock}

\end{frame}




\begin{frame}{Supply-demand example: Generator bids}

  Example from Kirschen and Strbac pages 56-58.

  The following generators bid 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] &  Price [\$/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 example: Consumer offers}

  The following consumers make offers for the same period:
  \ra{1.1}
  \begin{table}[!t]
    \begin{tabular}{lrr}
      \toprule
      Company & Quantity [MW] &  Price [\$/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}{Several generators with fixed demand}


  Consider a simplified situation where we have just two generators 1
  and 2 which have to meet a fixed demand $Q$ (could be e.g. many
  consumers with inelastic demand and inverse demand curve much higher than the generation costs, i.e. $U(Q) = d\cdot Q$ where $d >> c_i$).

  They have generation rate $Q_1$ and $Q_2$ respectively, so that we have
  \begin{equation*}
    Q = Q_1 + Q_2
  \end{equation*}

  The generators have simple linear cost functions
  \begin{align*}
      C_1(Q_1) & = c_1\cdot Q_1 \\
      C_2(Q_2) & = c_2\cdot Q_2
  \end{align*}
  where we assume $c_1 < c_2$. The dispatch must be positive, but below the capacity:
  \begin{align*}
    0 \leq Q_1 & \leq \hat Q_1\\
    0 \leq Q_2 & \leq \hat Q_2
  \end{align*}

\end{frame}




\begin{frame}{Several generators with fixed demand}

  What is the most efficient dispatch of the two generators?

  We want to maximise the total economic welfare, which is the sum of
  generator and consumer surplus. For this example, we have no control
  of the consumer side, so we must seek to maximise the generator
  surplus. This is equivalent to minimising the costs. We can write an optimisation problem with \alert{objective function}:
  \begin{equation*}
    \max_{Q_1,Q_2} f(Q_1,Q_2) = - c_1\cdot Q_1 - c_2\cdot Q_2
  \end{equation*}
  subject to one equality and four inequality \alert{constraints}:
  \begin{align*}
    Q -Q_1 - Q_2 & = 0 \\
    0 \leq Q_1 & \leq \hat Q_1\\
    0 \leq      Q_2 & \leq \hat Q_2
      \end{align*}

\end{frame}


\begin{frame}{Several generators with fixed demand: Example 1}

  Suppose $c_1 = 10$ \euro/MWh, $c_2 = 30$ \euro/MWh,
  $Q = 200$ MW, $\hat{Q}_1 = 300$ MW, $\hat{Q}_2 = 400$ MW.

  What is the optimal power plant dispatch, i.e. what values of $Q_1,Q_2$ maximise efficiency?

  Answer is clear: supply all of the demand with the cheapest generator 1.

  The optimal dispatch is $Q_1^* = 200 < \hat{Q}_1$ and $Q_2 ^*= 0 < \hat{Q}_2$.

  In this example the inequalities for generator 1 are
  \alert{non-binding}.


  \centering
  \includegraphics[width=6cm]{supply-demand-low.pdf}


\end{frame}



\begin{frame}{Equality Constraints and Shadow Prices}

  We have an equality constraint:
  \begin{align*}
    Q - Q_1 - Q_2 = 0
  \end{align*}

  We can ask: what is the change in the objective function, i.e. the
  increase in total cost, if we increase the load $Q$ by one unit?


  In this case, the extra load would be supplied by generator 1, which
  still has capacity, so the shadow price of the balance constraint is
  $\l^* = c_1$.

\end{frame}


\begin{frame}{Several generators with fixed demand: Example 2}

  Suppose now we raise the load to $Q=$ 500~MW.  What values of $Q_1,Q_2$ maximise efficiency?  Answer is clear: max out cheapest generator 1, then supply the
  remaining demand with the more expensive generator 2.

  The optimal dispatch is $Q_1^* = 300 = \hat{Q}_1$ and $Q_2^* = 200 < \hat{Q}_2$.

  The balance constraint here has a shadow price of $\l^* = c_2$.

  In this example the inequality for generator 1 is \alert{binding} but the inequality for generator 2 is \alert{non-binding}.


  \centering
  \includegraphics[width=6cm]{supply-demand-high.pdf}


\end{frame}




\begin{frame}{Binding Inequality Constraints and Shadow Prices}

  If we associate shadow prices $\m_i$ with the four generation inequality constraints
  \begin{align*}
      Q_1 & \leq \hat Q_1  \hspace{1cm}\leftrightarrow\hspace{1cm} \m_1 \\
      Q_2 & \leq \hat Q_2  \hspace{1cm}\leftrightarrow\hspace{1cm} \m_2 \\
      -Q_1 & \leq \hat 0  \hspace{1cm}\leftrightarrow\hspace{1cm} \m_3 \\
      -Q_2 & \leq \hat 0  \hspace{1cm}\leftrightarrow\hspace{1cm} \m_4
      \end{align*}

  Then we can ask: what is the change in the objective function if we
  relax the first constraint, i.e. allow one more unit of capacity $\hat Q_1$ for
  generator 1.

  We substitute generation from generator 2 with generation from generator 1, thus increasing total welfare by
  \begin{equation*}
    \m_1^* = c_2 - c_1
  \end{equation*}

  Of course this would have to be balanced against the capital costs of the addition of capacity to generator 1.


\end{frame}



\section{General constrained optimisation theory: Lagrangians and Karush-Kuhn-Tucker conditions}


\begin{frame}
  \frametitle{Optimisation problem}


We have an \emph{objective function} $f: \R^k \to \R$
\begin{equation*}
  \max_{x} f(x)
\end{equation*}
[$x = (x_1, \dots x_k)$] subject to some 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 KKT `Lagrange' multipliers we introduce for
each constraint equation; their meaning and interpretation will be
explained in the next slide.


\end{frame}




\begin{frame}
  \frametitle{Lagrangian}


We now study the \emph{Lagrangian function}
\begin{equation*}
   \cL(x,\l,\m) = f(x) - \sum_i \l_i \left[g_i(x) - c_i\right]  - \sum_j \m_j \left[h_j(x) - d_j\right]
\end{equation*}

We've built this function using the variables $\l_i$ and $\m_j$ to
better understand the optimal solution of $f(x)$ given the
constraints.

The optima of $\cL(x,\mathbf{\l},\m)$ tell us important information
about the optima of $f(x)$ given the constraints.

We can already see that if $\frac{\d \cL}{\d \l_i} = 0$ then the
equality constraint $g_i(x) = c$ will be satisfied.

[Beware: $\pm$ signs appear differently in literature, but have been chosen here such that $\l_i = \frac{\d \cL}{\d c_i}$ and $\m_j
  = \frac{\d \cL}{\d d_j}$.]


\end{frame}




\begin{frame}
  \frametitle{KKT conditions}

The 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 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 Primal feasibility:
      \begin{align*}
        g_i(x^*) & = c_i \\
        h_j(x^*) &\leq d_j
      \end{align*}
    \item Dual feasibility: $\m_j^* \geq 0$
    \item Complementary slackness: $\m_j^* (h_j(x^*) - d_j) = 0$
\end{enumerate}

\end{frame}





\begin{frame}
  \frametitle{Complementarity slackness for inequality constraints}


  We have for each inequality constraint
  \begin{align*}
    \m_j^* & \geq  0 \\
    \m_j^*(h_j(x^*) - d_j) & = 0
  \end{align*}

  So \alert{either} the inequality constraint is binding
  \begin{align*}
    h_j(x^*)  = d_j
  \end{align*}
  and we have $\m_j^* \geq 0$.

  \alert{Or} the inequality constraint is NOT binding
  \begin{align*}
    h_j(x^*)  < d_j
  \end{align*}
  and we therefore MUST have $\m_j^* = 0$.

  If the inequality constraint is non-binding, we can remove it from the optimisation problem, since it has no effect on the optimal solution.
\end{frame}





\begin{frame}
  \frametitle{Nota Bene}

  \begin{enumerate}
  \item The KKT conditions are only \alert{sufficient} for optimality
    of the solution under certain conditions, e.g. linearity of the
    problem.
  \item Since at the optimal solution we have $g_i(x^*) = c_i$ for equality constraints and
    $\m_j^*(h_j(x^*) - d_j) = 0$, we have
    \begin{equation*}
         \cL(x^*,\l^*,\m^*) = f(x^*)
    \end{equation*}


  \end{enumerate}

\end{frame}



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

  Apply KKT now to maximisation of total economic welfare:
  \begin{align*}
    \max_{\{Q_i^B\}, \{Q_i^S\}} f(\{Q_i^B\}, \{Q_i^S\}) = \left[ \sum_i U_i (Q_i^B)  -  \sum_i C_i (Q_i^S) \right]
  \end{align*}
  subject to the balance constraint:
  \begin{align*}
    g(\{Q_i^B\}, \{Q_i^S\}) = \sum_i Q_i^B -  \sum_i Q_i^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\} = \{Q_i^B\} \cup \{Q_i^S\}$.

  We get from stationarity:
  \begin{align*}
    0 & =   \frac{\d f}{\d Q_i^B} - \sum_i \l^* \frac{\d g}{\d Q_i^B} = U_i'(Q_i^B) - \l^* = 0 \\
    0 & =   \frac{\d f}{\d Q_i^S} - \sum_i \l^* \frac{\d g}{\d Q_i^S} = -C_i'(Q_i^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:
  \begin{align*}
 U_i'(Q_i^B) =  \l^*  \\
 C_i'(Q_i^S) = \l^*
  \end{align*}

  This is the CENTRAL result of microeconomics.

  If we have further inequality constraints that are binding, then
  these equations will receive additions with $\m_i^* > 0$, as we will
  see in the next examples...


\end{frame}




\begin{frame}
  \frametitle{Application to 2-generator example with fixed demand}

  Our optimisation variables are $\{x\} = \{Q_1,Q_2\}$ with objective function

    \begin{equation*}
    \max_{Q_1,Q_2} f(Q_1,Q_2) = - c_1\cdot Q_1 - c_2\cdot Q_2
  \end{equation*}
  subject to one equality and four inequality \alert{constraints}:
  \begin{align*}
    Q -Q_1 - Q_2 & = 0  \hspace{1cm}\leftrightarrow\hspace{1cm} \l \\
    Q_1 & \leq \hat Q_1  \hspace{1cm}\leftrightarrow\hspace{1cm} \m_1 \\
    Q_2 & \leq \hat Q_2  \hspace{1cm}\leftrightarrow\hspace{1cm} \m_2 \\
    -Q_1 & \leq \hat 0  \hspace{1cm}\leftrightarrow\hspace{1cm} \m_3 \\
    -Q_2 & \leq \hat 0  \hspace{1cm}\leftrightarrow\hspace{1cm} \m_4
  \end{align*}

\end{frame}


\begin{frame}
  \frametitle{KKT for example}


  Stationarity gives us:
  \begin{align*}
    -c_1 + \l^* -\m_1^* + \m_3^* & = 0 \\
    -c_2 + \l^* -\m_2^* + \m_4^* & = 0
  \end{align*}

  Dual feasibility and complementary slackness give us:
  \begin{align*}
    \m_i^* & \geq 0 \hspace{1cm} \forall i \in {1,2,3,4} \\
    \m_i^*(Q_i^*-\hat Q_i) & = 0 \hspace{1cm} \forall i \in {1,2} \\
    \m_i^*(-Q_i^*) & = 0 \hspace{1cm} \forall i \in {3,4} \\
  \end{align*}

\end{frame}



\begin{frame}
  \frametitle{Application to Example 1}

  Here we have no binding constraints for generator 1 at the optimal
  point, $0 < Q_1^* < \hat Q_1$, and generator 2 is at the lower
  boundary $Q_2^* = 0$.

  Thus we have $\m_1^* = \m_2^* = \m_3^* = 0$ and $\m_4^* \neq 0$.

  We get from stationarity
  \begin{align*}
    -c_1 + \l^*& = 0 \\
    -c_2 + \l^*+ \m_4^* & = 0
  \end{align*}


  Thus the non-zero Lagrange multipiers are:
  \begin{align*}
   \l^*& = c_1 \\
  \m_4^* & = c_2 - c_1
  \end{align*}


\end{frame}



\begin{frame}
  \frametitle{Application to Example 2}

  Here we have one binding constraint for generator 1 at the optimal
  point, $Q_1^*  = \hat Q_1$, and generator 2 has no binding constraints $0 < Q_2^* < \hat Q_2$.

  Thus we have $\m_1^* \neq 0$, $ \m_2^* = \m_3^* =\m_4^* = 0$.

  We get from stationarity
  \begin{align*}
    -c_1 + \l^* - \m_1^* & = 0 \\
    -c_2 + \l^* & = 0
  \end{align*}

  Thus the non-zero Lagrange multipiers are:
  \begin{align*}
   \l^*& = c_2 \\
  \m_1^* & = c_2 - c_1
  \end{align*}


\end{frame}






\begin{frame}
  \frametitle{Extension of Example for Demand Higher than Generation}

  In the previous examples there was always one generator setting the
  price, getting revenue at their marginal cost. How can such
  generators every recoup their capital costs?

  Suppose now we promote the demand $Q$ to an optimisation variable with utility
  \begin{equation*}
    U(Q) = \left\{ \begin{array}{ll}
      200 Q & \textrm{ for } Q\leq 600 \\
      -180000 + 800 Q - \frac{1}{2} Q^2 & \textrm{ for } 600 \leq Q\leq 800 \\
    \end{array}
    \right.
  \end{equation*}


  \centering
  \includegraphics[width=6cm]{supply-demand-higher-wo.pdf}


\end{frame}





\begin{frame}
  \frametitle{Application to 2-generator example with fixed demand}

  Our optimisation variables are $\{x\} = \{Q, Q_1,Q_2\}$ with objective function

    \begin{equation*}
    \max_{Q, Q_1,Q_2} f(Q, Q_1,Q_2) = U(Q) - c_1\cdot Q_1 - c_2\cdot Q_2
  \end{equation*}
  subject to one equality and four inequality \alert{constraints}:
  \begin{align*}
    Q -Q_1 - Q_2 & = 0  \hspace{1cm}\leftrightarrow\hspace{1cm} \l \\
    Q_1 & \leq \hat Q_1  \hspace{1cm}\leftrightarrow\hspace{1cm} \m_1 \\
    Q_2 & \leq \hat Q_2  \hspace{1cm}\leftrightarrow\hspace{1cm} \m_2 \\
    -Q_1 & \leq \hat 0  \hspace{1cm}\leftrightarrow\hspace{1cm} \m_3 \\
    -Q_2 & \leq \hat 0  \hspace{1cm}\leftrightarrow\hspace{1cm} \m_4
  \end{align*}

\end{frame}


\begin{frame}
  \frametitle{Extension of Example for Demand Higher than Generation}


  We max out both generators $Q_1^* = 300, Q_2^* = 400, Q^* = 700$.

  From KKT we get $\m_3^* = \m_4^* = 0$ because the generators are
  both at their \emph{upper limits}.

  KKT stationarity:
  \begin{align*}
    U'(Q^*) - \l^* & = 0 \hspace{1cm} \Rightarrow \l^* = 100 \\
    -c_1 + \l^* - \m_1^* & = 0  \hspace{1cm} \Rightarrow \m_1^* = \l^* - c_1 = 90\\
    -c_2 + \l^* - \m_2^* & = 0 \hspace{1cm} \Rightarrow \m_2^* = \l^* - c_2 = 70
  \end{align*}
  \centering
  \includegraphics[width=6cm]{supply-demand-higher.pdf}


\end{frame}


\begin{frame}{Copyright}


  Unless otherwise stated the graphics and text is Copyright
  \copyright Tom Brown, 2016.

  We hope the graphics borrowed from others have been attributed
  correctly; if not, drop a line to the authors and we will correct
  this.

  The source \LaTeX, self-made graphics and Python code used to
  generate the self-made graphics are available on the course website:

  \url{http://fias.uni-frankfurt.de/~brown/courses/electricity_markets/}

  The graphics and text for which no other attribution are given are licensed under a
  \href{http://creativecommons.org/licenses/by-sa/4.0/}{Creative Commons
  Attribution-ShareAlike 4.0 International License}.

  \begin{center}\ccbysa\end{center}

\end{frame}

\end{document}
