\documentclass[10pt]{article}

\usepackage{dsfont}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{geometry}
\geometry{a4paper}
\usepackage[parfill]{parskip}
\usepackage{graphicx}
\usepackage{epstopdf}
\usepackage{listings}
\lstset{language = C++}
\usepackage{url}

\newcommand{\re}[1]{\ensuremath{\operatorname{Re}(#1)}}
\newcommand{\im}[1]{\ensuremath{\operatorname{Im}(#1)}}

%\newcommand{\Vr}{\re{V}}
%\newcommand{\Vi}{\im{V}}
%\newcommand{\Ir}{\re{I}}
%\newcommand{\Ii}{\im{I}}
%\newcommand{\Sr}{\ensuremath{P}}
%\newcommand{\Si}{\ensuremath{Q}}

\newcommand{\Vr}{{V_R}}
\newcommand{\Vi}{{V_I}}
\newcommand{\Ir}{{I_R}}
\newcommand{\Ii}{{I_I}}
\newcommand{\Sr}{\ensuremath{P}}
\newcommand{\Si}{\ensuremath{Q}}

\newcommand{\Id}{\mathds{1}}

\title{Power Flow Theory}
\author{Dan Gordon}
\date{}

\begin{document}
\maketitle

\section{Nomenclature}
\begin{align*}
V &= \Vr + \Vi = \text{complex voltage.} \\
I &= \Ir + j\Ii = \text{complex current.} \\
S &= P + jQ = \text{complex power.} \\
y &= g + jb = \text{complex admittance} \\
Z &= R + jX = \text{complex impedance} \\
y_{ik} &= \text{complex admittance between buses $i$ and $k$.} \\
Y_{ik} &= G_{ik} + jB_{ik} = \text{nodal admittance matrix element $i, k$.} \\
\theta_{i} &= \text{the voltage angle of bus $i$.} \\
\theta_{ik} &= \theta_i - \theta_k = \text{the voltage angle difference between buses $i$ and $k$.} \\
I_{\text{ZIP},i} &= \text{total complex current injection from load at bus $i$.} \\
I_{\text{gen},i} &= \text{complex current injection due to the voltage controlled generation at bus $i$.} \\
S_{\text{gen},i} &= \text{complex power injection due to the voltage controlled generation at bus $i$.} \\
S_{ci} &= \text{constant power injection ZIP component of load at bus $i$.} \\
I_{ci} &= \text{constant current injection ZIP component of load.} \\
y_{ci} &= \text{constant impedance ZIP component of load.} \\
\delta_{ik} &= \text{the Kronecker delta, $\delta_{ik} = 1$ if $i = k$, 0 otherwise.}
\end{align*}

\section{AC power}
For AC power, we can write $V(t) = V_\text{max} \cos(\theta_V + \omega t)$ and $I(t) = I_\text{max} \cos(\theta_I + \omega t)$, where $V_\text{max}$ and $I_\text{max}$ are the maximum power and voltage over a cycle. However, it is more usual to use RMS quantities $V_\text{RMS} = V_\text{max}/\sqrt{2}$ and $I_\text{RMS} = I_\text{max}/\sqrt{2}$, because the average power over a cycle then mimics the usual relationships such as $P = V^2/R$\footnote{For a resistive load, the average power over a cycle works out to be $P_\text{avg} = I_\text{RMS}V_\text{RMS}$, replicating the usual relationship for DC power. If we used the maximum rather than RMS values of $V$ and $I$, then this relationship would have an extra factor of 1/2. All the other usual DC relationships also hold for the RMS values, such as $V_\text{RMS} = I_\text{RMS}R$. We are not often concerned with knowing the instantaneous voltage and current, so the factors of $\sqrt{2}$ in Eq. \ref{EQ_V_I_RMS} are not too inconvenient.}, so,
\begin{align}
	V(t) &= \sqrt{2}V_\text{RMS} \cos(\theta_V + \omega t), \notag \\
	I(t) &= \sqrt{2}I_\text{RMS} \cos(\theta_I + \omega t)
	\label{EQ_V_I_RMS}
\end{align}

It is convenient to use the complex phasor notation $V := V_\text{RMS} \exp(j\theta_V), \, I := I_\text{RMS} \exp(j\theta_I)$, which allows us to unambiguously code the voltage magnitude and phase shift in a single complex number. If we ever need to recover, for example, the instantaneous voltage, we can use
\begin{align}
	V(t) = \sqrt{2}\re{V \exp(j\omega t)}
\end{align}

The instantaneous power is
\begin{align}
	P(t) &= 2I_\text{RMS}V_\text{RMS}\cos(\theta_I + \omega t)\cos(\theta_V + \omega t) \notag \\
	     &= I_\text{RMS}V_\text{RMS}\left[\cos(\theta_V - \theta_I) + \cos(\theta_V + \theta_I + 2 \omega t)\right] \notag \\
		 &= \re{I^*V + IV\exp(2j \omega t)}
\end{align}
This inspires us to define the instantaneous complex power,
\begin{align}
	S(t) &= I^*V + IV\exp(2j \omega t)
\end{align}
whose average over a cycle is
\begin{align}
	S &= I^*V,
\end{align}
We define the real power and reactive power phasors to be
\begin{align}
	P := \re{S} = I_\text{RMS}V_\text{RMS}\cos(\theta_V - \theta_I), \notag \\
	Q := \im{S} = I_\text{RMS}V_\text{RMS}\sin(\theta_V - \theta_I)
\end{align}
which shows that $P$ and $Q$ are ``in phase'' and ``out of phase'' components of the product of $I$ and $V$. $P$ is just the average power dissipated by the resistive component of the load, while $Q$ is the magnitude of the instantaneous power absorbed by the reactive part of the load.

\subsection{Reactive power, voltage and line losses}
The loss of real power over a section of transmission line is given by
\begin{align}
	P_\text{loss} &= \left|I\right|^2 R
\end{align}
We can express $I$ as a function of the voltage and power injection to the downstream (or upstream) bus, e.g.
\begin{align}
	I &= \frac{S^*_\text{out}}{V^*_\text{out}}
\end{align}
Therefore, we have
\begin{align}
	P_\text{loss} &= \frac{\left|S_\text{out}\right|^2}{\left|V_\text{out}\right|^2}R
\end{align}

This equation explains, firstly, why transmission lines are more efficient at high voltage (because, e.g. doubling the voltage means the line loss is reduced by a factor of four), and secondly, why utilities are keen to reduce the reactive power flowing through lines (because doubling the reactive power multiplies the line losses by four). Line losses are bad for three reasons: they are a waste of energy, they correspond to dissipated heat and therefore thermal limits, and they also correspond to voltage drops in the line.

We have
\begin{align}
	\left|\frac{V_\text{out}}{V_\text{in}}\right|^2 &= \left|\frac{V_\text{in} - IZ}{V_\text{in}}\right|^2 \notag \\
	&= \left|1 - \frac{S^*_\text{in}Z}{\left|V_\text{in}\right|^2}\right|^2 \notag \\
	&= 1 - \frac{2(PR + QX)}{\left|V_\text{in}\right|^2} + \frac{\left|S\right|^2\left|Z\right|^2}{\left|V_\text{in}\right|^4}
\end{align}
At high voltages, the middle term in the sum dominates the voltage drop. High voltage lines normally also have a larger reactance than resistance, and in such cases we see that reactive power is a more significant contributor to the voltage drop than is real power.
\section{Three phase balanced power}
In balanced positive sequence three-phase systems, the phase to neutral voltages (or, simply, the phase voltages), at a bus, are
\begin{align}
(V_A, V_B, V_C) = (V_A, V_A\exp(-j2\pi/3), V_A\exp(j2\pi/3)).
\end{align}
The line to line voltages, also just called the line voltages, are
\begin{align}
(V_{AB}, V_{BC}, V_{CA}) &= (V_A - V_B, V_B - V_C, V_C - V_A) \notag \\
&= \sqrt{3}\exp(j\pi/6)(V_A, V_B, V_C).
\end{align}
Thus, the line to line voltages have a magnitude $\sqrt{3}$ times that of the phase voltages, and they also form a positive sequence set.

Loads and sources can be connected in either $\Delta$ or $Y$ configuration. Fig. \ref{FIG_YY_DD} shows these connections.
\begin{figure}[!h]
	\begin{center}
		\includegraphics[width=(12cm)]{YSrcYLd.png}
		\includegraphics[width=(12cm)]{DSrcDLd.png}
	\end{center}
	\caption{
		Top: A Y-connected source connected to a Y-connected load. Bottom: A $\Delta$-connected source connected to a $\Delta$-connected load.
	}
	\label{FIG_YY_DD}
\end{figure}

The currents along the phase wires, $I_A, I_B, I_C$ are known as the ``line currents''. Note that this can be confusing when compared to the use of ``line voltage'' to mean line to line voltage.

For a $\Delta$-connected load or source operating under balanced positive sequence conditions, we can easily show, using Ohm's law, that $I_{BC} = \exp(-j2\pi/3) I_{AB}$ and $I_{CA} = \exp(j2\pi/3) I_{AB}$, i.e. the delta currents also form a positive sequence set. Kirchoff's current law can then be used to show that,
\begin{align}
(I_{AB}, I_{BC}, I_{CA}) &= \frac{1}{\sqrt{3}}\exp\left(j\pi/6\right)(I_A, I_B, I_C)
\end{align}
meaning the $\Delta$ currents are a factor of $\sqrt{3}$ less than the line currents. Note that this is opposite to the relationship that holds for phase voltages vs line to line voltages.

Note that for every $\Delta$ connected load, there is an equivalent $Y$-connected load. For balanced power flow, if the impedance on each arm of the $Y$ is $Z_Y$, then the equivalent impedance for each side of the $\Delta$ is $Z_\Delta = 3Z_Y$.

\section{Symmetric components}
Let
\begin{align}
	A &= \left[
		\begin{array}{lll}
			1 & 1 & 1 \\
			1 & \alpha^2 & \alpha \\
			1 & \alpha & \alpha^2
		\end{array}
	\right],\;
	A^{-1} = \frac{1}{3}\left[
		\begin{array}{lll}
			1 & 1 & 1 \\
			1 & \alpha & \alpha^2 \\
			1 & \alpha^2 & \alpha
		\end{array}
	\right]
\end{align}
where $\alpha := \exp(j2\pi/3)$.

We define a transformation to symmetric components:
\begin{align}
	V_{012} = A^{-1}V_{ABC}
\end{align}
which applies for all vectors e.g. to $I$ as well as $V$, and
\begin{align}
	Z_{012} = A^{-1}Z_{ABC}A
\end{align}
which applies for matrices, for example, impedance matrices $Z$, as shown above.

Suppose we have a balanced positive sequence voltage $V^+_{ABC} = V_A[1, \alpha^2, \alpha]^T$. Then, under the transformation to symmetric components, we find $V_{012} = V_A[0, 1, 0]^T$. Similarly, a negative sequence voltage $V^-_{ABC} = V_A[1, \alpha, \alpha^2]^T$ transforms to $V_{012} = V_A[0, 0, 1]^T$. Finally, the unbalanced \emph{zero sequence} voltage $V^-_{ABC} = V_A[1, 1, 1]^T$ transforms to $V_{012} = V_A[1, 0, 0]^T$. Therefore, the transformation $A^{-1}$ decomposes $V_{ABC}$ into a positive sequence component $V_1$, a negative sequence component $V_2$, and a zero sequence component $V_0$. In summary:
\begin{align}
V_{ABC}^+ = V_A\left[\begin{array}{l}
	1 \\ \alpha^2 \\ \alpha
\end{array}\right] &\Longleftrightarrow
V_{012}^+ = V_A\left[\begin{array}{l}
	0 \\ 1 \\ 0
\end{array}\right] & \text{(positive sequence voltage)}\notag \\
V_{ABC}^- = V_A\left[\begin{array}{l}
	1 \\ \alpha \\ \alpha^2
\end{array}\right] &\Longleftrightarrow
V_{012}^- = V_A\left[\begin{array}{l}
	0 \\ 0 \\ 1
\end{array}\right] & \text{(negative sequence voltage)}\notag \\
V_{ABC}^0 = V_A\left[\begin{array}{l}
	1 \\ 1 \\ 1
\end{array}\right] &\Longleftrightarrow
V_{012}^0 = V_A\left[\begin{array}{l}
	1 \\ 0 \\ 0
\end{array}\right] & \text{(zero sequence voltage)}\notag \\
\end{align}

\subsection{Reduction of a three-phase balanced problem to single phase}

In balanced power flow, there is only a positive sequence component, which suggests that symmetric components could be used to reduce a three-phase problem to an equivalent single-phase problem, since the negative and zero sequence components will always be zero. However, using the treatment above, it turns out that we then need to remember to multiply by an extra factor of three when calculating the total power. This is not ideal, because total power is an important physical quantity that corresponds to the amount of actual energy consumed or generated. Therefore, we shall develop a non-standard treatment of symmetric components.

Let the scaled sequence transformation matrix $B$ be defined as
\begin{align}
	B := \frac{1}{\sqrt{3}}A &= \frac{1}{\sqrt{3}}\left[
		\begin{array}{lll}
			1 & 1 & 1 \\
			1 & \alpha^2 & \alpha \\
			1 & \alpha & \alpha^2
		\end{array}	\right], \notag \\
	B^{-1} := \sqrt{3} A^{-1} &=\frac{1}{\sqrt{3}}\left[
		\begin{array}{lll}
			1 & 1 & 1 \\
			1 & \alpha & \alpha^2 \\
			1 & \alpha^2 & \alpha
		\end{array} \right]
\end{align}
Note that $B^{-1} = B^\dag$, so $B$ is a unitary matrix\footnote{$B$ being unitary implies that the transformation that it describes will have nice properties like preserving scalar (dot) products. The total power, $S$, is a scalar product: $S = I^\dag V$, so this transformation preserves the value obtained for the total power.}. The transformation to scaled symmetric components is:
\begin{align}
	V_{012'} &= B^{-1}V_{ABC} \notag \\
	Z_{012'} &= B^{-1}Z_{ABC}B
\end{align}

Under this transformation, positive, negative and zero sequence voltages transform as follows
\begin{align}
V_{ABC}^+ = V_A\left[\begin{array}{l}
	1 \\ \alpha^2 \\ \alpha
\end{array}\right] &\Longleftrightarrow
V_{012'}^+ = \sqrt{3}V_A\left[\begin{array}{l}
	0 \\ 1 \\ 0
\end{array}\right] & \text{(positive sequence voltage)}\notag \\
V_{ABC}^- = V_A\left[\begin{array}{l}
	1 \\ \alpha \\ \alpha^2
\end{array}\right] &\Longleftrightarrow
V_{012'}^- = \sqrt{3}V_A\left[\begin{array}{l}
	0 \\ 0 \\ 1
\end{array}\right] & \text{(negative sequence voltage)}\notag \\
V_{ABC}^0 = V_A\left[\begin{array}{l}
	1 \\ 1 \\ 1
\end{array}\right] &\Longleftrightarrow
V_{012'}^0 = \sqrt{3}V_A\left[\begin{array}{l}
	1 \\ 0 \\ 0
\end{array}\right] & \text{(zero sequence voltage)}\notag \\
\end{align}
So for example, a positive sequence balanced three phase voltage will have a single nonzero sequence component, the positive sequence component $V_1 = \sqrt{3}V_A$, with magnitude equal to the line voltage. A similar relationship holds for the line currents: a positive sequence balanced three phase current will have $I_1 = \sqrt{3}I_A$, however, this time, there is no simple interpretation of $\sqrt{3}I_A$ that is analogous to $\sqrt{3}V_A$ being the line to line voltage.

Now consider a three phase transmission line. The equation for the voltage drop in each wire is:
\begin{align}
	\Delta V = ZI
\end{align}
where $Z$ is the phase impedance matrix for the line. If the power flow is to be perfectly balanced, then there must be a symmetry under exchange of any two phases, meaning all wires must either be equidistant from each other, or else transposed\footnote{Line transposition occurs in transmission systems, where the physical locations of the phases on the poles on are interchanged at regular intervals, so for example the lines are arranged as ABC--BCA--CAB.}, and must have equal impedances and symmetrical mutual impedances. $Z$ must therefore have the form:
\begin{align}
	Z_{ABC} = \left[
			\begin{array}{lll}
				z & m & m \\
				m & z & m \\
				m & m & z
			\end{array}
		\right]
		\label{EQ_ZABC}
\end{align}
where $z$ is the single line impedance, and $m$ is the mutual coupling impedance. Under the transformation to scaled symmetric components, this becomes
\begin{align}
	Z_{012} = \left[
			\begin{array}{lll}
				z+2m & 0 & 0 \\
				0 & z-m & 0 \\
				0 & 0 & z-m
			\end{array}
		\right]
		\label{EQ_Z012}
\end{align}
This matrix is diagonal, which confirms that a perfectly symmetric network can be decomposed into three independent networks: a positive sequence network, a negative sequence network, and a zero sequence network. Note also that the positive and negative sequences are equal. In balanced power flow, a single positive sequence impedance only needs to be specified for normal operation, whereas the zero-sequence impedance needs to be additionally specified for unbalanced conditions such as non-symmetric faults. Shunt impedances or loads can be represented in a similar manner, but have $m = 0$. $\Delta$ connected shunt impedances or loads can be treated using the equivalent $Y$ impedance: each impedance in the $\Delta$ load must be divided by 3 to get the equivalent $Y$-impedance.

In summary, we can model a balanced three phase power problem as a single phase problem by making the following interpretations:
\begin{itemize}
	\item The voltage in the equivalent single phase system is the line to line voltage in the three phase system (but, to be pedantic, with the same angle as A). To get the phase A voltage, divide the single phase voltage by $\sqrt{3}$.
	\item To get the current along each phase of a three phase line, divide the single phase current by $\sqrt{3}$. This is most important when considering line limits.
	\item Power in the equivalent single phase system is the \emph{total} power of all phases in the three phase system. Thus, for example, the power at an equivalent single phase $PQ$ bus is the sum of the power for all phases in the three phase system. This is regardless of whether the load or source is $\Delta$ or $Y$-connected.
	\item Three-phase lines represented as in Eq. \ref{EQ_ZABC} have an equivalent single phase impedance of $z - m$.
	\item A Y-connected load/shunt impedance, with impedance $z$ on each phase, becomes a single impedance $z$ in the equivalent single phase system.
	\item A $\Delta$-connected load/shunt impedance, with impedance $z$ between each pair of phases, becomes a single load impedance $z/3$ in the equivalent single phase system.

\end{itemize}

\section{The powerflow problem}

We start with a collection \emph{buses} -- terminal-like conductors that have a single associated voltage, $V_i$, where $i$ is the bus index. We can consider \emph{ground} to be a special bus (to which we assign index 0), at which $V$ is always zero.

Current can flow between buses, via the network. The current flowing from bus $i$ to $k$ is $I_{ik}$.

Current that flows into a bus from ground is an \emph{injection} into the bus. We use the notation $I_{\text{inj},i}$ to denote a current injection from ground to bus $i$. The generated power is $S_{\text{inj}, i} = I_{\text{inj}, i}^*(V_i - V_i)$. The generated power -- that is, the power \emph{injection} into the bus -- is the negative of this: $S_{\text{inj},i} := I_{\text{inj},i}^*V_i$.

\emph{Kirchoff's current law} states that the total current flowing into a bus (from other buses and ground) is zero -- in other words, current in equals current out; electrical charges don't accumulate at buses. Thus
\begin{align}
	I_{i} - \sum_{k \ne i}{I_{ik}} &= 0
	\label{EQ_KCL}
\end{align}

The aim of the powerflow problem is, essentially, to find all bus voltages and currents\footnote{This will also trivially allow us to calculate power flows in the system -- we will discuss this later.}. To do so, we need to define the properties of the network to provide a functional relationship between current and voltage at the buses. We start with a simple formulation, where the current flow between any two buses is ohmic: it is defined by a fixed impedance between them. This describes a network of passive \emph{lines}. Later we will generalise this relationship, so that, for example, we can include transformers.

Suppose that $y_{ik} = y_{ki}$ is an impedance between buses $i$ and $k$. Then Ohm's law states that
\begin{align}
	I_{ik} &= y_{ik}(V_i - V_k)
\end{align}
Substituting this into KCL, Eq. (\ref{EQ_KCL}), gives
\begin{align}
	I_{\text{inj},i} - \sum_{k \ne i}{y_{ik}(V_i - V_k)} &= 0
	\label{EQ_PF_1}
\end{align}
which can be written as the matrix equation
\begin{align}
	I_\text{inj} - YV &= 0
\end{align}
where
\begin{align}
	Y_{ik} &=
		\begin{cases}
			-y_{ik}&\text{if $i \ne k$} \\
			\sum_{l \ne i} y_{il}& \text{if $i = k$}
		\end{cases}
	\label{EQ_YNODE_OHMIC}
\end{align}
However, in what follows, we do not make further use of the particular form of $Y$ expressed in Eq. (\ref{EQ_YNODE_OHMIC}) above. We simply require that $Y$ be a constant matrix, so that there is a linear relationship between $I$ and $V$. We shall see later that including components such as shunts and transformers allows us to retain the linear relationship while modifying the particular form of $Y$.

Due to the basic relation $S = I^*V$, the power loss between bus $i$ and $k$ is $S_{ik} = I_{ik}^*(V_i - V_k)$. Note that power is a scalar: if we reverse the order of the buses, then the power loss is unchanged, in contrast to, say, a current, where reversing the order of the buses will negate the current.

More commonly, though, we deal with the power entering or leaving a bus rather than the power lost in a line. Consider a current leaving a bus. If the current is a draw to ground, or if the system is radial and the current is in the direction of the downstream loads, all of this current will eventually end up at ground. The voltage drop in this process will just be the voltage of the bus. Thus, the power eventually transferred to ground will be
\begin{align}
	S_\text{draw} = I_\text{draw}^*V_\text{bus}
\end{align}
or, equivalently, for injections
\begin{align}
	S_\text{inj} = I_\text{inj}^*V_\text{bus}
\end{align}
Note the way in which this concept of power is different to the concept of power loss in a line: the voltage in the equation is always the voltage of a bus relative to ground, and the current is always an injection or draw on a bus (rather than between two buses). There is an implied directionality: if the power draw from a bus is positive, then power is considered to be flowing from the bus to ground and we have a load.

The one piece of the puzzle we haven't yet dealt with is the current injections $I_\text{inj}$ into the buses. If the current injections were all constant, then the powerflow equations could be immediately solved by standard linear algebra. However, typically, the current injections from ground will depend on the bus voltage, and it is here that the real complexity of powerflow modelling comes into play.

Current injections take the form of \emph{loads} or \emph{generation}. Load currents can often be modeled using the \emph{ZIP} model:
\begin{align}
	I_\text{ZIP} = \frac{I_cV}{|V|} + \frac{S^*_c}{V^*} - y_cV
\end{align}
Where $I_\text{ZIP}$ is the total current injection of the ZIP, $I_c$ and $S_c$ are constant current\footnote{Note the expression $I_cV/|V|$ for the constant current component. The voltage dependence ensures that the complex power $S$ does not depend on the absolute phase of the voltage. Instead we have $S = I^*_c |V|$, which is much more reasonable for realistic devices.} and power injections and $y_c$ is a constant impedance. The ZIP model is a good model for many types of load, but could also be used for certain types of generation - e.g. fixed power generation. If the real power injection $P = \re{I_\text{ZIP}^*V}$ is negative, then we have a load; if it is positive, then we have a generator.

In addition to the ZIP loads at a bus, we may include generators that apply some kind of extra voltage control on the bus to maintain power stability in the system. A bus that only has a ZIP load is termed a PQ bus. The term PQ means constant P, constant Q.\footnote{Confusingly, though, the presence of fixed current and fixed impedance components of the ZIP means that the total power injection may not be constant. This confusion arises because ZIP loads provide a generalisation to the original concept of PQ buses.}

We consider two other types of generator bus: SL (slack/swing/infinite/reference) buses, and PV buses. SL generators keep the total complex voltage of the bus fixed by injecting variable $P$ and $Q$. $PV$ buses keep the voltage magnitude $V$ and power $P$ fixed, while injecting a variable current\footnote{Note that the voltage angle is relative to the slack bus, and so is a non-local property of the network, hence it usually does not make sense to control it anywhere except at the slack bus.}. We write the additional generator current as a power $S_{gen}$ to simplify the later analysis.
\begin{align}
I_{gen} &= \frac{S_{gen}^*}{V^*}
\end{align}
Note that SL and PV buses may also have ZIP loads attached.

We now write the final form of the powerflow equations:
\begin{align}
I_{\text{inj},i} &= \frac{S^*_{ci} + S^*_{\text{gen},i}}{V^*_i} + \frac{I_{ci}V_i}{|V_i|} - y_{ci}V_i - \sum_{k=0}^NY_{ik}V_k = 0
\label{EQ_POWERFLOW_COMPLEX}
\end{align}
The term $-y_{ci}V_i$ may in practice be absorbed into $Y$ by adding $y_{ci}$ to $Y_{ii}$; we retain it here as an explicit bus shunt for bookkeeping purposes.

Real and imaginary components are:
\begin{align}
	\Ir_i &= \frac{(P_{ci} + P_{\text{gen},i})\Vr_i + (Q_{ci} + Q_{\text{gen},i})\Vi_i}{|V|^2_i} \notag \\
	      &+ \frac{\Ir_{ci}\Vr_{i} - \Ii_{ci}\Vi_i}{|V_i|} -g_{ci}\Vr_i + b_{ci}\Vi_i \notag \\
	      &+ \sum_{k=0}^N\left(-G_{ik}\Vr_k + B_{ik}\Vi_k\right) = 0\\
	\Ii_i &= \frac{(P_{ci} + P_{\text{gen},i})\Vi_i - (Q_{ci} + Q_{\text{gen},i})\Vr_i}{|V|^2_i} \notag \\
	      &+ \frac{\Ir_{ci}\Vi_{i} + \Ii_{ci}\Vr_{i}}{|V_i|} -g_{ci}\Vi_i - b_{ci}\Vr_i \notag \\
	&+ \sum_{k=0}^N\left(-G_{ik}\Vi_k - B_{ik}\Vr_k\right) = 0
\end{align}

The possible unknowns are the generator power $P_\text{gen}, Q_\text{gen}$ and the voltages $\Vr, \Vi$.

\subsection{PQ buses}
For PQ buses, there is no additional (non-ZIP) generation, and hence $S_{\text{gen}}$ can be set to zero. The unknown variables are the components of $V$.
\subsection{Slack buses}
For SL buses, $V$ is constant and the power flow equations give an explicit expression for $S$. All quantities at slack buses can immediately be found and are therefore considered constants in the powerflow equations.
\subsection{PV buses}
For PV buses, the power flow equations also hold, with both components of $V$ being unknown as was the case for PQ buses. $Q_{\text{gen}}$ is an additional variable, and an extra constraint also applies:
\begin{align}
\Delta |V|^2 = \Vr^2 + \Vi^2 - V_\text{PV}^2 = 0
\label{EQ_POWERFLOW_PV_CONSTRAINT}
\end{align}
where $V_{\text{PV}}$ is the voltage magnitude setpoint for the PV bus.

\subsection{Newton-Raphson equations}
Considering PQ buses only for the moment, the unknowns are the real and imaginary parts of $V$, and these equations can be solved using the Newton-Raphson method. Letting the function to which we want to find the zero be $f = [\Ir, \Ii]$, the unknows be $x = [\Vr, \Vi]$, we wish to solve $f(x) = 0$. Using the Jacobian
\begin{align}
J_{ik}(x) = \frac{\partial f_i(x)}{\partial x_k}
\end{align}
the NR method calculates the update to $x$ at each iteration as the solution to the linear equations
\begin{align}
-f_{(n)} &= J(x_{(n)})(x_{(n+1)}-x_{(n)}) = J(x_{(n)})\Delta x_{(n,n+1)}
\label{EQ_NR}
\end{align}
The Jacobian elements for variables $V$ are given by:
\begin{align}
	\frac{\partial \Ir_i}{\partial \Vr_{k}}
		&= \left[-\frac{2\Vr_k[(P_{ck} + P_{\text{gen},k})\Vr_k + (Q_{ck} + Q_{\text{gen},k})\Vi_k]}{|V|_k^4} + \frac{(P_{ck} + P_{\text{gen},k})}{|V|_k^2}\right]\delta_{ik} \notag \\
		&+ \frac{\Vi_i(\Ii_i\Vr_i + \Ir_i\Vi_i)}{|V_i|^3}\delta_{ik} \notag \\
		&- (G_{ik} + g_{ci}\delta_{ik}) \\
	\frac{\partial \Ir_i}{\partial \Vi_{k}}
		&= \left[-\frac{2\Vi_k[(P_{ck} + P_{\text{gen},k})\Vr_k + (Q_{ck} + Q_{\text{gen},k})\Vi_k]}{|V|_k^4} + \frac{(Q_{ck} + Q_{\text{gen},k})}{|V|_k^2} \right]\delta_{ik} \notag \\
		&- \frac{\Vr_i(\Ii_i\Vr_i + \Ir_i\Vi_i)}{|V_i|^3}\delta_{ik} \notag \\
		&+ (B_{ik} + b_{ci}\delta_{ik}) \\
	\frac{\partial \Ii_i}{\partial \Vr_{k}}
		&= \left[-\frac{2\Vr_k[(P_{ck} + P_{\text{gen},k})\Vi_k - (Q_{ck} + Q_{\text{gen},k})\Vr_k]}{|V|_k^4} - \frac{(Q_{ck} + Q_{\text{gen},k})}{|V|_k^2} \right]\delta_{ik} \notag \\
		&+ \frac{\Vi_i(\Ii_i\Vi_i - \Ir_i\Vr_i)}{|V_i|^3}\delta_{ik} \notag \\
		&- (B_{ik} + b_{ci}\delta_{ik}) \\
	\frac{\partial \Ii_i}{\partial \Vi_{k}}
		&= \left[-\frac{2\Vi_k[(P_{ck} + P_{\text{gen},k})\Vi_k - (Q_{ck} + Q_{\text{gen},k})\Vr_k]}{|V|_k^4} + \frac{(P_{ck} + P_{\text{gen},k})}{|V|_k^2} \right] \delta_{ik} \notag \\
		&+ \frac{\Vr_i(\Ir_i\Vr_i - \Ii_i\Vi_i)}{|V_i|^3}\delta_{ik} \notag \\
		&- (G_{ik} + g_{ci}\delta_{ik})
\end{align}

\subsubsection{Treatment of the slack bus}
There are no variables associated with the slack bus. The voltage is constant, and the power can be found after the rest of the buses are solved.

\subsubsection{Treatment of PV buses}
As explained earlier, $PV$ buses add an extra variable, $Q_g$. This adds the following non-zero elements to the Jacobian:
\begin{align}
\frac{\partial \Ir_k}{\partial Q_{\text{gen},k}} &= \frac{\Vi_k}{|V|_k^2} \\
\frac{\partial \Ii_k}{\partial Q_{\text{gen},k}} &= -\frac{\Vr_k}{|V|_k^2}
\end{align}
There is also an extra constraint, Eq. (\ref{EQ_POWERFLOW_PV_CONSTRAINT}), reproduced here:
\begin{align}
\Delta |V|^2 = \Vr^2 + \Vi^2 - V_\text{PV}^2 = 0
\label{EQ_POWERFLOW_PV_CONSTRAINT_AGAIN}
\end{align}
with corresponding elements in the Jacobian being:
\begin{align}
\frac{\partial \Delta|V|^2_k}{\partial \Vr_k} &= 2 \Vr_k \notag \\
\frac{\partial \Delta|V|^2_k}{\partial \Vi_k} &= 2 \Vi_k
\label{EQ_PV_JAC_Q}
\end{align}
The NR update corresponding to this constraint row is:
\begin{align}
	-\Vr_k^2 - \Vi_k^2 + V_{\text{PV},k}^2 &= 2\Vr_k\Delta\Vr_k + 2\Vi_k\Delta\Vi_k
\end{align}
which gives
\begin{align}
\Delta \Vr_k &= \frac{V^2_{\text{PV},k} - \Vr_k^2 - \Vi_k^2- 2\Vi_k\Delta \Vi_k}{2\Vr_k}
\end{align}

Using these expressions, $\Delta \Vr$ may be eliminated from the NR equations for the PV bus. First write the Jacobian as if all buses were PQ. Let $k$ be any PV bus. Take the column corresponding to $\Delta \Vr_k$, and add its product with $-\Vi_k/\Vr_k$ to the matching column for  $\Delta \Vi_k$. Add its product with $(|V|^2_{\text{PV},k} - \Vr_k^2 - \Vi_k^2)/(2\Vr_k)$ to $f$ in Eq. (\ref{EQ_NR}). The column and the corresponding element of $x$ for $\Delta \Vr_k$ will now be replaced with a column and elment for $\Delta Q_{\text{gen},k}$. Set the column to zero, and then set the block diagonal elements, using Eq. (\ref{EQ_PV_JAC_Q}). Reinterpret the corresponding element of $\Delta x$ in Eq. (\ref{EQ_NR}) as now corresponding to $\Delta Q_{\text{gen},k}$ instead of $\Delta \Vr_k$.

\section{Power flow in polar coordinates}
The preceding section treated the power flow problem in rectangular coordinates, in which the mathematical expressions are simpler and the Newton-Raphson method works well in rectangular coordinates. In fact, it is more common to work in polar coordinates. Doing so can give a clearer physical insight, and is conducive to the development of approximations such as are used in the DC approximation or fast decoupled power flow. Taking the complex conjugate of Eq. \ref{EQ_POWERFLOW_COMPLEX} and multiplying by $V_i$ gives
\newcommand{\M}[1]{\ensuremath{|V_{#1}|}}
\newcommand{\cs}[1]{\ensuremath{\cos(\theta_{#1})}}
\newcommand{\sn}[1]{\ensuremath{\sin(\theta_{#1})}}
\begin{align}
	S_i &= S_{ci} + S_{gi} + I^*_{ci}\M{i} - y^*_{ci}\M{i}^2 - V_i\sum_{k = 0}^N{Y^*_{ik}V^*_k} \notag \\
	&= S_{ci} + S_{gi} + I^*_{ci}M_i - y^*_{ci}\M{i}^2 - \M{i}\sum_{k = 0}^N{Y^*_{ik}\M{k}\exp{(j\theta_{ik})}}
\end{align}
where we introduce the polar notation $V_i := \M{i} \exp{(j\theta_i)}$ and $\theta_{ik} := \theta_i - \theta_k$. $S_i$ is the \emph{total} power injection into a bus, including all loads, branch flows and generation, and should be zero in a correct power flow solution. The real and reactive components of this are:
\begin{align}
	P_i &= P_{ci} + P_{gi} + \Ir_{ci}\M{i} - g_i\M{i}^2 \notag \\
	&- \M{i}\sum_{l = 0}^N{\left[G_{il}\cs{il} + B_{il}\sn{il}\right]\M{l}} \notag \\
	Q_i &= Q_{ci} + Q_{gi} - \Ii_{ci}\M{i} + b_i\M{i}^2 \notag \\
	&- \M{i}\sum_{l = 0}^N{\left[G_{il}\sn{il} - B_{il}\cs{il}\right]\M{l}} \notag \\
\end{align}
However, for PV buses, $Q_{g}$ is a free variable, and can immediately be set to the value that makes the error $Q_i$ zero.

We first solve:
\begin{align}
	P_i &= P_{ci} + P_{gi} + \Ir_{ci}\M{i} - g_i\M{i}^2 \notag \\
	&- \M{i}\sum_{l = 0}^N{\left[G_{il}\cs{il} + B_{il}\sn{il}\right]\M{l}}  , & i \in \mathrm{PQ} \cup \mathrm{PV}\notag \\
	Q_i &= Q_{ci} - \Ii_{ci}\M{i} + b_i\M{i}^2 \notag \\
	&- \M{i}\sum_{l = 0}^N{\left[G_{il}\sn{il} - B_{il}\cs{il}\right]\M{l}}, & i \in \mathrm{PQ}
\end{align}
for variables $\{\M{\text{PQ}}, \theta_\text{PQ, PV}\}$. These equations are independent of $Q_g$, which can be therefore be calculated in a single final step as:
\begin{align}
	Q_{gi} &= -Q_{ci} + \Ii_{ci}\M{i} - b_i\M{i}^2 \notag \\
	&+ \M{i}\sum_{l = 0}^N{\left[G_{il}\sn{il} - B_{il}\cs{il}\right]\M{l}}, & i \in \mathrm{PV}
\end{align}

Note for computations (particularly in what follows), it may be more efficient to cache $\sin(\theta)$ and $\cos(\theta)$, and to use
\begin{align}
	\sin(\theta_{il}) &:= \sin(\theta_i)\cos(\theta_l) - \cos(\theta_i)\sin(\theta_l) \notag \\
	\cos(\theta_{il}) &:= \cos(\theta_i)\cos(\theta_l) + \sin(\theta_i)\sin(\theta_l)
\end{align}

\subsection{The NR method in polar coordinates}
For PQ buses, the possible unknowns are $\M{}$ and $\theta$. For PV buses, the possible unknowns are $Q_g$ and $\theta$. The Jacobian is therefore
\begin{align}
	\frac{\partial P_i}{\partial \M{k}} &=
		\begin{cases}
			-\M{i}\left[G_{ik}\cs{ik} + B_{ik}\sn{ik}\right] & k \ne i \\
			\Ir_{ci} - 2g_i\M{i} - 2 \M{i}G_{ii} - \sum_{\stackrel{l = 0}{l \ne i}}^N{\left[G_{il}\cs{il} + B_{il}\sn{il}\right]\M{l}} & k = i
		\end{cases} & k \in \text{PQ} \notag \\
	\frac{\partial P_i}{\partial \theta_k} &=
		\begin{cases}
			\M{i}\left[-G_{ik}\sn{ik} + B_{ik}\cs{ik}\right]\M{k}, &k \ne i \\
			\M{i}\sum_{\stackrel{l = 0}{l \ne i}}^N{\left[G_{il}\sn{il} - B_{il}\cs{il}\right]\M{l}}, &k = i
		\end{cases} \notag \\
	\frac{\partial Q_i}{\partial \M{k}} &=
		\begin{cases}
			-\M{i}\left[G_{ik}\sn{ik} - B_{ik}\cs{ik}\right], & k \ne i \\
			-\Ii_{ci} + 2b_i\M{i} + 2\M{i}B_{ii} - \sum_{l = 0}^N{\left[G_{il}\sn{il} - B_{il}\cs{il}\right]\M{l}}, & k = i, \\
		\end{cases} &  i, k \in \text{PQ} \notag \\
	\frac{\partial Q_i}{\partial \theta_k} &=
		\begin{cases}
			\M{i}\left[G_{ik}\cs{ik} + B_{ik}\sn{ik}\right]\M{k}, & k \ne i \\
			-\M{i}\sum_{\stackrel{l = 0}{l \ne i}}^N{\left[G_{il}\cs{il} + B_{il}\sn{il}\right]\M{l}}, & k = i \\
		\end{cases}, & i \in PQ
\end{align}

This gives us enough information to carry out the NR method for polar coordinates. Note that the value of $Q_g$ is not needed for any of the iterations, and so can be calculated once-only after convergence is achieved.

\subsection{Fast-decoupled load flow}
Especially in transmission networks, we have that $G_{ik} \ll B_{ik}$ and $\theta_{ik} \approx 0$. We also assume that shunts are mainly reactive (i.e. there are no large resistive loads), so that $g_i \approx 0$, and that there are no large constant current loads, so that $I_c \approx 0$. Under these conditions, the Jacobian above can be simplified:
\begin{align}
	\frac{\partial P_i}{\partial \M{k}} &= 0, & k \in \text{PQ} \notag \\
	\frac{\partial P_i}{\partial \theta_k} &=
		\begin{cases}
			\M{i}B_{ik}\M{k}, & i \ne k \\
			-\M{i}\sum_{\stackrel{l = 0}{l \ne i}}^NB_{il}\M{l}, &i = k
		\end{cases} \notag \\
	\frac{\partial Q_i}{\partial \M{k}} &=
		\begin{cases}
			\M{i}B_{ik}, & k \ne i \\
			-\Ii_{ci} + 2b_i\M{i} + 2 \M{i}B_{ii} + \sum_{\stackrel{l = 0}{l \ne i}}^N{B_{il}\M{l}}, & k = i,
		\end{cases} & i, k \in \text{PQ} \notag \\
	\frac{\partial Q_i}{\partial \theta_k} &= 0, & i \in PQ
\end{align}
Note that the problem then separates into two independent problems: one for $\{P, \theta\}$ and one for $\{Q, \M{}\}$.

For a flat-start, we have $\M{k} \approx 1$, and the Jacobian simplifies further. The fast decoupled load flow method normally uses this flat start Jacobian throughout the iteration process. The error function $f$ is assumed to be unchanged from the full NR method, so if it converges, it will converge to the correct solution to the full AC power flow problem. In practice it requires many more iterations than the full NR method, but each iteration is much faster, and the convergence properties can be much better.

\section{Generalised Power Flow Theory}
Let the voltage at bus $i$ be $V_i$, and let the voltage difference between buses $i$ and $k$ be $V_{ik} := V_i - V_k$. We represent ground as a special bus, with index zero, which must always have a voltage of zero: $V_0 = 0$. Let $S^*_{g,i}$ be the power injected into bus $i$. Let $y_{c,ik}$ be a constant admittance between buses $i$ and $k$, $S_{c,ik}$ be a constant power flow between these buses and $I_{c,ik}$ be a constant magnitude current flow whose phase varies with $V$, so that the resulting actual current is\footnote{This ensures that the controlled current loads can't have a power that depends on the local absolute phase.} $I_{c,ik}V_{ik}/|V_{ik}|$.

The generated current injection at bus $i$ is then equal to the sum of the current flowing from $i$ to all other connected buses:
\begin{align}
	\frac{S^*_{g,i}}{V^*_i} - \sum_{k \ne i} \left[y_{c,ik}V_{ik} + \frac{S^*_{c,ik}}{V^*_{ik}} + \frac{I_{c,ik}V_{ik}}{|V_{ik}|}\right] = 0
	\label{EQ_POWERFLOW_B}
\end{align}

To simplify the calculation for the terms proportional to $y$, we define the bus admittance matrix, $Y$:
\begin{align}
	Y_{c,ik} &=
		\begin{cases}
			-y_{c,ik}&\text{if $i \ne k$} \\
			\sum_{l \ne i} y_{c,il}& \text{if $i = k$}
		\end{cases}
	\label{EQ_YNODE_OHMIC_B}
\end{align}
The sum over $k$ in this equation includes the special ground bus, $k = 0$. Such terms represent shunt admittances to ground. Equation \ref{EQ_POWERFLOW_B} then becomes
\begin{align}
	\frac{S^*_{g,i}}{V^*_i} - \sum_{k \ne 0}{Y_{c,ik}V_{k}} - \sum_{k \ne i}\left[\frac{S^*_{c,ik}}{V^*_{ik}} + \frac{I_{c,ik}V_{ik}}{|V_{ik}|}\right] = 0
	\label{EQ_POWERFLOW_B1}
\end{align}
Since ground is treated implicitly, there is no need to consider terms where $i = 0$, and, because $V_0$ = 0, we may safely omit terms in the first sum where $k = 0$, as shown in the equation. Note that the second sum \emph{does} include terms with $k = 0$; these are usual constant current and constant power components of a ZIP load model. However, this equation allows more general loads than the usual ZIP model, as constant current and power components are also possible \emph{between} buses, not just from a single bus to ground. This allows us to consider FACTS devices, as well as various types of delta loads, for example.

\subsection{NR method in rectangular coordinates}
Let the mismatch, $\Delta I$, equal the LHS of Eq. \ref{EQ_POWERFLOW_B1}:
\begin{align}
\Delta I_i = \frac{S^*_{g,i}}{V^*_i} - \sum_{k \ne 0}{Y_{c,ik}V_{k}} - \sum_{k \ne i}\left[\frac{S^*_{c,ik}}{V^*_{ik}} + \frac{I_{c,ik}V_{ik}}{|V_{ik}|}\right]
\label{EQ_MISMATCH}
\end{align}

Considering for the moment PQ buses only, the unknowns are the real and imaginary voltage components, $x = [\Vr, \Vi]$. We wish to find values for these variables such that the mismatch, $\Delta I$, is zero. Using the Jacobian
\begin{align}
J_{ik}(x) = \frac{\partial \Delta I_i(x)}{\partial x_k},
\end{align}
the NR method calculates the update to $x$ at each iteration as the solution to the linear equations
\begin{align}
-\Delta I_{(n)} &= J(x_{(n)})(x_{(n+1)}-x_{(n)}) \notag \\
                &= J(x_{(n)})\Delta x_{(n,n+1)}
\label{EQ_NR}
\end{align}

The Jacobian is given below:
\begin{align}
	\frac{\partial \Delta I_i}{\partial \Vr_l}
		&= \delta_{il}\frac{-S^*_{g,i}}{{V^*_i}^2} \notag \\
		&- Y_{c,il} \notag \\
		&+ \delta_{il} \sum_{k \ne i}{\frac{S^*_{c,ik}}{{V^*_{ik}}^2}} \notag \\
		&+ (1-\delta_{il})\frac{-S^*_{c,il}}{{V^*_{il}}^2} \notag \\
		&+ \delta_{il} \sum_{k \ne i}{\frac{I_{c,ik}(-|V_{ik}|^2 + \Vr_{ik}V_{ik})}{|V_{ik}|^3}} \notag \\
		&+ (1 - \delta_{il})\frac{I_{c,il}(|V_{il}|^2 - \Vr_{il}V_{il})}{|V_{il}|^3} \notag \\
	\frac{\partial \Delta I_i}{\partial \Vi_l}
		&= \delta_{il}\frac{j S^*_{g,i}}{{V^*_i}^2} \notag \\
		&- j Y_{c,il} \notag \\
		&+ \delta_{il} \sum_{k \ne i}{\frac{-j S^*_{c,ik}}{{V^*_{ik}}^2}} \notag \\
		&+ (1-\delta_{il}) \frac{j S^*_{c,il}}{{V^*_{il}}^2} \notag \\
		&+ \delta_{il} \sum_{k \ne i}{\frac{I_{c,ik}(-j|V_{ik}|^2 + \Vi_{ik}V_{ik})}{|V_{ik}|^3}} \notag \\
		&+ (1 - \delta_{il})\frac{I_{c,il}(j|V_{il}|^2 - \Vi_{il}V_{il})}{|V_{il}|^3}
\end{align}
To solve these equations, we transform each complex row of the mismatch vector and the Jacobian into separate real and imaginary rows, and then use a sparse solver on the resulting system of real equations.

\subsubsection{Treatment of the slack bus}
There are no variables associated with the slack bus. The voltage is constant, and the power can be found after the rest of the buses are solved.

\subsubsection{Treatment of the ground bus}
Similarly, there are no variables associated with the special ground bus, and indeed this bus may be treated completely implicitly. Its voltage is, of course, zero. We have already seen how constant admittance shunts to ground are absorbed into the definition of the bus admittance matrix, $Y$. Similarly, there are columns in $S_c$ and $I_c$ involving the ground bus that need to be taken into account in various sums.

\subsubsection{Treatment of PV buses}
As explained earlier, $PV$ buses add an extra variable, $Q_g$. This adds the following non-zero elements to the Jacobian:
\begin{align}
\frac{\partial \Delta I_i}{\partial Q_{g,l}} &= \delta_{il}\frac{-i}{V^*_i}
\end{align}
There is also an extra constraint, Eq. (\ref{EQ_POWERFLOW_PV_CONSTRAINT}), reproduced here:
\begin{align}
\Delta |V|^2 = |V|^2 - V_\text{PV}^2 = 0
\label{EQ_POWERFLOW_PV_CONSTRAINT_AGAIN}
\end{align}
with corresponding elements in the Jacobian being:
\begin{align}
\frac{\partial \Delta|V|^2_k}{\partial \Vr_k} &= 2 \Vr_k \notag \\
\frac{\partial \Delta|V|^2_k}{\partial \Vi_k} &= 2 \Vi_k
\label{EQ_PV_JAC_Q}
\end{align}
The NR update corresponding to this constraint row is:
\begin{align}
	-|V_k|^2 + V_{\text{PV},k}^2 &= 2\Vr_k\Delta\Vr_k + 2\Vi_k\Delta\Vi_k
\end{align}
which gives
\begin{align}
\Delta \Vr_k &= \frac{V^2_{\text{PV},k} - |V_k|^2- 2\Vi_k\Delta \Vi_k}{2\Vr_k}
\end{align}

Using these expressions, $\Delta \Vr$ may be eliminated from the NR equations for the PV bus. First write the Jacobian as if all buses were PQ. Let $k$ be any PV bus. Take the column corresponding to $\Delta \Vr_k$, and add its product with $-\Vi_k/\Vr_k$ to the matching column for  $\Delta \Vi_k$. Add its product with $(|V|^2_{\text{PV},k} - \Vr_k^2 - \Vi_k^2)/(2\Vr_k)$ to $f$ in Eq. (\ref{EQ_NR}). The column and the corresponding element of $x$ for $\Delta \Vr_k$ will now be replaced with a column and elment for $\Delta Q_{g,k}$. Set the column to zero, and then set the block diagonal elements, using Eq. (\ref{EQ_PV_JAC_Q}). Reinterpret the corresponding element of $\Delta x$ in Eq. (\ref{EQ_NR}) as now corresponding to $\Delta Q_{g,k}$ instead of $\Delta \Vr_k$.

\section{Formalism for modelling branches}
\subsection{Branch admittance ($Y$) parameters}
Consider a network consisting of three buses, $a$, $b$ and $c$. Due to the linear nature of the nodal admittance relation, the nodal admittance matrix for the network $Y$, can be decomposed as follows:
\begin{align}
	Y = \begin{bmatrix}
		Y^{ab}_{00} & Y^{ab}_{01} & 0 \\ Y^{ab}_{10} & Y^{ab}_{11} & 0 \\ 0 & 0 & 0
	\end{bmatrix} + \begin{bmatrix}
		Y^{ac}_{00} & 0 & Y^{ac}_{01} \\ 0 & 0 & 0 \\ Y^{ac}_{10} & 0 &  Y^{ac}_{11}
	\end{bmatrix} + \begin{bmatrix}
		0 & 0 & 0 \\ 0 & Y^{bc}_{00} & Y^{bc}_{01} \\ 0 & Y^{bc}_{10} &  Y^{bc}_{11}
	\end{bmatrix}
	\label{EQ_BRANCH_DECOMP}
\end{align}
where, e.g., $Y^{ab}$, is the nodal admittance matrix for buses $a$ and $b$ in isolation from the rest of the network. If nodes $a$ and $b$ were connected by a line with admittance $y_{ab}$, then we would have
\begin{align}
	Y^{ab} &=
	\begin{bmatrix}
		y_{ab} & -y_{ab} \\
		-y_{ab} & y_{ab}
	\end{bmatrix}
\end{align}
In this kind of decomposition, the matrix $Y^{ab}$ represents the properties of the \emph{branch} between nodes $a$ and $b$ and is termed the \emph{branch admittance matrix}. The global properties of the full $Y$ matrix are reduced to the elements of the $2 \times 2$ branch admittance matrices, which can be derived in isolation from each other.

What about three phase systems? A three-phase bus can be represented as a set of three single phase buses. A three phase branch links two three phase buses, and thus involves six single phase buses; it is represented by a $6 \times 6$ branch admittance matrix. Considering the network for these six buses in isolation from all other buses allows us to derive its elements, which could, for example, represent the properties of a three-wire overhead transmission line.

The modelling task is then to derive expressions for the branch admittance matrices of all the different kinds of lines and transformers. The full $Y$ matrix may then always be reconstructed from these components.

Sometimes, instead of using the branch admittance matrix representation, alternate expressions are used. Often, the inverse of the branch matrix, the $Z$ matrix, is used
\begin{align}
	Z = Y^{-1}
\end{align}

Another common representation is the ``ABCD'' representation, where
\begin{align}
	\begin{bmatrix}
		V_1 \\ I_1
	\end{bmatrix} &= \begin{bmatrix}
		A & -B \\ C & -D
	\end{bmatrix}\begin{bmatrix}
		V_2 \\ I_2
	\end{bmatrix}
\end{align}
$V_1, I_1$ and $V_2, I_2$ are the current and voltage at buses $1$ and $2$ respectively.\footnote{Take care: in most treatments, you will not see the negative signs on $B$ and $D$. This is because in these treatments $I_1$ is the current \emph{into} bus 1 and $I_2$ is the current \emph{out of} bus 2. To avoid choosing arbitrary directions of current flow, we have instead been working with the convention that all currents and powers are \emph{injections into} a bus.} For multi-phase buses, these are vector quantities, and $A, B, C, D$ are matrices. We can reconstruct the $Y$ matrix as follows:
\begin{align}
	Y = \begin{bmatrix}
		DB^{-1} & C - DB^{-1}A \\
		-B^{-1} & B^{-1}A
	\end{bmatrix}
\end{align}
Given a $Y$ matrix, we can also construct the $ABCD$ matrix:
\begin{align}
	\begin{bmatrix} A & -B \\ C & -D \end{bmatrix} &=
	\begin{bmatrix}
		-Y_{21}^{-1}Y_{22} & -Y_{21}^{-1} \\
		Y_{12} - Y_{11}Y_{21}^{-1}Y_{22} & -Y_{11}Y_{21}^{-1}
	\end{bmatrix}
\end{align}
where, again, all elements may be considered to be block submatrices.

The main point of the ABCD representation is that branches may be ``cascaded'' together. For example, consider two n-phase lines that are joined head to tail at a central n-phase bus. We can eliminate the central bus by multiplying the ABCD matrices:
\begin{align}
	\begin{bmatrix}
		V_1 \\ I_1
	\end{bmatrix} &=
	\begin{bmatrix}
		A_{12} & -B_{12} \\ C_{12} & -D_{12}
	\end{bmatrix}
	\begin{bmatrix}
		V_2 \\ I_2
	\end{bmatrix} \notag \\
	&=
	\begin{bmatrix}
		A_{12} & -B_{12} \\ C_{12} & -D_{12}
	\end{bmatrix}
	\begin{bmatrix}
		A_{23} & -B_{23} \\ C_{23} & -D_{23}
	\end{bmatrix}
	\begin{bmatrix}
		V_3 \\ I_3
	\end{bmatrix}
\end{align}
This defines a composition law for $ABCD$ parameters.
\section{Transmission Lines}
\subsection{Single phase transmission lines}
Short transmission lines (below 80 km in length) can be modelled as a single admittance $y$ (or equivalently an impedance $z = 1/y$) between two buses:
\begin{align}
	Y = \begin{bmatrix}
		y & -y \\ -y & y
	\end{bmatrix}
\end{align}
The real part of $z$ is positive (a resistance), and the imaginary part is also positive (an inductive reactance). Both the resistance and the inductive reactance are proportional to the length of the line. The inductive reactance is usually larger than the resistance.

Longer transmission lines ($> 80$ km) also develop a capacitance between the line and ground. If the length is less than about 240 km, then this is often modelled using the $\pi$-model, see Fig. \ref{FIG_PI_LINE}. The $Y$-matrix is:
\begin{align}
	Y = \begin{bmatrix}y_s + \frac{j b_c}{2} & -y_s \\ -y_s & y_s + \frac{j b_c}{2} \end{bmatrix}
\end{align}
\begin{figure}[!h]
	\begin{center}
		\includegraphics[width=(5cm)]{pi_line}
	\end{center}
	\caption{
		The $\pi$ model of a transmission line.
	}
	\label{FIG_PI_LINE}
\end{figure}
Very long lines require a more complex \emph{distributed model}.

\subsection{Multi-phase transmission lines}
The equations above apply also to multi-phase lines, except that the scalar elements become block submatrices whose size is the number of phases.

\subsection{Line parameters}
We have discussed above the structure of the $Y$ matrix for transmission lines -- but we still need to know how to find the values of the admittances in our equations. \emph{Carson's Equations} allow us to do this, for N-phase lines which may also contain one or more multi-grounded neutral wires. They take into account the geometry of the transmission line and the conductivity of the ground. We do not, however, go into these equations in this document.

\section{Transformers}
\subsection{Single phase transformers}
For an ideal transformer with a single (possibly complex) turns ratio $n = n_1/n_0$, we have, using the ABCD parameters:
\begin{align}
\begin{bmatrix}V_1 \\ I_1\end{bmatrix} &= \begin{bmatrix}n & 0 \\ 0 & 1/n^*\end{bmatrix}\begin{bmatrix}V_0 \\ I_0 \end{bmatrix}
\end{align}
This can't be modelled correctly in the formalism of nodal admittance matrices, in the same way that a line with zero impedance can't. Two equivalent circuits for an ideal transformer are shown in Fig. \ref{FIG_IDEAL_TRANS_EQUIV}.
\begin{figure}[!h]
	\begin{center}
		\includegraphics[width=(\textwidth-2cm)]{ideal_transformer_equiv.png}
	\end{center}
	\caption{
		Two equivalent circuits for an ideal transformer.
	}
	\label{FIG_IDEAL_TRANS_EQUIV}
\end{figure}
The relationships could be included in the power flow equations specially, but more commonly real transformers are used, as described below; these may be expressed in the nodal admittance formalism.

A real transformer includes a leakage impedance (due to finite resistance of copper windings and core losses) and a shunt magnetising impedance. There are various ways of drawing an equivalent circuit. We use the general branch model described in Section \ref{SEC_GEN_BRANCH_MODEL}, with the series admittance set to a leakage admittance $y_l$ (mainly inductive) and the shunt admittances forming the magnetising admittance $y_m$, see Fig. \ref{FIG_REAL_TRANS}.
\begin{figure}[!h]
	\begin{center}
		\includegraphics[width=(\textwidth-2cm)]{transformer.png}
	\end{center}
	\caption{
		An equivalent circuit for a real transformer.
	}
	\label{FIG_REAL_TRANS}
\end{figure}

The nodal admittance matrix is:
\begin{align}
\begin{bmatrix}I_0 \\ I_1 \end{bmatrix} &=
\begin{bmatrix}\frac{y_l + \frac{1}{2}y_m}{|n|^2} & -\frac{y_l}{n^*} \\ -\frac{y_l}{n} & y_l + \frac{1}{2}y_m\end{bmatrix}
\begin{bmatrix}V_0 \\ V_1 \end{bmatrix}
\label{EQ_REAL_TRANS}
\end{align}
where $n = n_1/n_0$ is the ideal turns ratio, $y_l$ is the leakage impedance and $y_m$ is the magnetising admittance. Often, the magnetising admittance is very small and is neglected. $y_l$ can be found by short circuiting the secondary, while $y_m$ can be found using an open circuit test.

\subsection{Three-phase transformers}
The nodal admittance matrices of three-phase transformers may be derived from the single-phase expression, above, combined with information about the connections between phases.
\subsubsection{Wye-Wye}
We have
\begin{align}
I_{A} &= \frac{y_l}{|n|^2}V_A - \frac{y_l}{n^*}V_a \\
I_{B} &= \frac{y_l}{|n|^2}V_B - \frac{y_l}{n^*}V_b \\
I_{C} &= \frac{y_l}{|n|^2}V_C - \frac{y_l}{n^*}V_c \\
I_a &= -\frac{y_l}{n}V_A + y_l V_a \\
I_b &= -\frac{y_l}{n}V_B + y_l V_b \\
I_c &= -\frac{y_l}{n}V_C + y_l V_c \\
\end{align}
so we can immediately write down the nodal admittance relationship:
\begin{align}
\begin{bmatrix}I_A \\ I_B \\ I_C \\ I_a \\ I_b \\ I_c\end{bmatrix} &=
y_l \begin{bmatrix}
	1/|n|^2 & 0 & 0 & -1/n^* & 0 & 0 \\
	0 & 1/|n|^2 & 0 & 0 & -1/n^* & 0 \\
	0 & 0 & 1/|n|^2 & 0 & 0 & -1/n^* \\
	-1/n & 0 & 0 & 1 & 0 & 0 \\
	0 & -1/n & 0 & 0 & 1 & 0 \\
	0 & 0 & -1/n & 0 & 0 & 1
\end{bmatrix}
\begin{bmatrix}V_A \\ V_B \\ V_C \\ V_a \\ V_b \\ V_c\end{bmatrix}
\end{align}
with the nodal admittance matrix being specified by the matrix on the right, including the factor of $y_l$.

\subsubsection{Delta-Delta}
We have
\begin{align}
I_{AB} &= \frac{y_l}{|n|^2}(V_A - V_B) - \frac{y_l}{n^*}(V_a - V_b) \\
I_{BC} &= \frac{y_l}{|n|^2}(V_B - V_C) - \frac{y_l}{n^*}(V_b - V_c) \\
I_{CA} &= \frac{y_l}{|n|^2}(V_C - V_A) - \frac{y_l}{n^*}(V_c - V_a) \\
I_{ab} &= -\frac{y_l}{n}(V_A - V_B) + y_l (V_a - V_b) \\
I_{bc} &= -\frac{y_l}{n}(V_B - V_C) + y_l (V_b - V_c) \\
I_{ca} &= -\frac{y_l}{n}(V_C - V_A) + y_l (V_c - V_a)
\end{align}
Also, by the KCL, we have
\begin{align}
I_A &= I_{AB} - I_{CA} \\ &= \frac{y_l}{|n|^2}(2V_A - V_B - V_C) - \frac{y_l}{n^*}(2V_a - V_b - V_c) \\
I_B &= I_{BC} - I_{AB} \\ &= \frac{y_l}{|n|^2}(2V_B - V_C - V_A) - \frac{y_l}{n^*}(2V_b - V_c - V_a) \\
I_C &= I_{CA} - I_{BC} \\ &= \frac{y_l}{|n|^2}(2V_C - V_A - V_B) - \frac{y_l}{n^*}(2V_c - V_a - V_b) \\
I_a &= I_{ab} - I_{ca} \\ &= -\frac{y_l}{n}(2V_A - V_B - V_C) + y_l(2V_a - V_b - V_c) \\
I_b &= I_{bc} - I_{ab} \\ &= -\frac{y_l}{n}(2V_B - V_C - V_A) + y_l(2V_b - V_c - V_a) \\
I_c &= I_{ca} - I_{bc} \\ &= -\frac{y_l}{n}(2V_C - V_A - V_B) + y_l(2V_c - V_a - V_b)
\end{align}
so we can immediately write down the nodal admittance relationship:
\begin{align}
\begin{bmatrix}I_A \\ I_B \\ I_C \\ I_a \\ I_b \\ I_c\end{bmatrix} &=
y_l \begin{bmatrix}
	2/|n|^2 & -1/|n|^2 & -1/|n|^2 & -2/n^* & 1/n^* & 1/n^* \\
	-1/|n|^2 & 2/|n|^2 & -1/|n|^2 & 1/n^* & -2/n^* & 1/n^* \\
	-1/|n|^2 & -1/|n|^2 & 2/|n|^2 & 1/n^* & 1/n^* & -2/n^* \\
	-2/n & 1/n & 1/n & 2 & -1 & -1 \\
	1/n & -2/n & 1/n & -1 & 2 & -1 \\
	1/n & 1/n & -2/n & -1 & -1 & 2
\end{bmatrix}
\begin{bmatrix}V_A \\ V_B \\ V_C \\ V_a \\ V_b \\ V_c\end{bmatrix}
\end{align}
with the nodal admittance matrix being specified by the matrix on the right, including the factor of $y_l$.

\subsubsection{Delta-GWye}
\begin{figure}
\begin{center}
\includegraphics[width=7cm]{DeltaGWye.pdf}
\caption{Schematic of a Delta-GWye transformer}
\label{FIG_DELTA_GWYE}
\end{center}
\end{figure}
Considering Fig. \ref{FIG_DELTA_GWYE} and neglecting the magnetising impedance, we have,
\begin{align}
I_{AB} &= \frac{y_l}{|n|^2}(V_A - V_B) - \frac{y_l}{n^*}V_a \\
I_{BC} &= \frac{y_l}{|n|^2}(V_B - V_C) - \frac{y_l}{n^*}V_b \\
I_{CA} &= \frac{y_l}{|n|^2}(V_C - V_A) - \frac{y_l}{n^*}V_c \\
I_a &= -\frac{y_l}{n}(V_A - V_B) + y_l V_a \\
I_b &= -\frac{y_l}{n}(V_B - V_C) + y_l V_b \\
I_c &= -\frac{y_l}{n}(V_C - V_A) + y_l V_c \\
\end{align}
Also, by the KCL, we have
\begin{align}
I_A &= I_{AB} - I_{CA} \\
&= \frac{y_l}{|n|^2}(2V_A - V_B - V_C) + \frac{y_l}{n^*}(V_c - V_a) \\
I_B &= I_{BC} - I_{AB} \\
&= \frac{y_l}{|n|^2}(2V_B - V_C - V_A) + \frac{y_l}{n^*}(V_a - V_b) \\
I_C &= I_{CA} - I_{BC} \\
&= \frac{y_l}{|n|^2}(2V_C - V_A - V_B) + \frac{y_l}{n^*}(V_b - V_c)
\end{align}
so we can immediately write down the nodal admittance relationship:
\begin{align}
\begin{bmatrix}I_A \\ I_B \\ I_C \\ I_a \\ I_b \\ I_c\end{bmatrix} &=
y_l \begin{bmatrix}
	2/|n|^2 & -1/|n|^2 & -1/|n|^2 & -1/n^* & 0 & 1/n^* \\
	-1/|n|^2 & 2/|n|^2 &  -1/|n|^2 & 1/n^*  & -1/n^* & 0 \\
	-1/|n|^2 &  -1/|n|^2 & 2/|n|^2 & 0 & 1/n^*  & -1/n^* \\
	-1/n & 1/n & 0 & 1 & 0 & 0 \\
	0 & -1/n & 1/n & 0 & 1 & 0 \\
	1/n & 0 & -1/n & 0 & 0 & 1
\end{bmatrix}
\begin{bmatrix}V_A \\ V_B \\ V_C \\ V_a \\ V_b \\ V_c\end{bmatrix}
\end{align}
with the nodal admittance matrix being specified by the matrix on the right, including the factor of $y_l$.

\subsubsection{Open Delta-open Delta}
\begin{figure}
\begin{center}
\includegraphics[width=7cm]{OpenDelta.png}
\caption{Schematic of an open-delta transformer connection. The complex turns ratios (primary/secondary) are $n_A$ and $n_C$, and the leakage admittances are $y_{LA}$ and $y_{LC}$. There is a tie admittance $y_\text{tie}$ between the middle terminals.}
\label{FIG_OPEN_DELTA}
\end{center}
\end{figure}
We use the open delta model shown in \ref{FIG_OPEN_DELTA}. Note that the two middle phases are tied together using an admittance, to prevent a floating neutral. There are other grounding options available, for example, to ground the middle phase, and the model can be easily modified for these. Using Eq. (\ref{EQ_REAL_TRANS}) (and neglecting magnetising impedance),  we can derive the following equations:
\begin{align}
	I_A &= \frac{y_{LA}}{|n_A|^2}(V_A - V_B) - \frac{y_{LA}}{n_A^*}(V_a - V_b) \notag \\
	&= V_A\left(\frac{y_{LA}}{|n_A|^2}\right) + V_B\left(-\frac{y_{LA}}{|n_A|^2}\right) + V_a\left(-\frac{y_{LA}}{n_A^*}\right) + V_b\left(\frac{y_{LA}}{n_A^*}\right) \notag \\
	%
	I_B &= y_\text{tie}(V_B - V_b) - I_A - I_C \notag \\
	&= V_A\left(-\frac{y_{LA}}{|n_A|^2}\right) + V_B \left(y_\text{tie} + \frac{y_{LA}}{|n_A|^2} + \frac{y_{LC}}{|n_C|^2}\right) + V_C\left(-\frac{y_{C}}{|n_C|^2}\right) \notag \\
	&+ V_a\left(\frac{y_{LA}}{n_A^*}\right) + V_b\left(-y_\text{tie} -\frac{y_{LA}}{n_A^*} - \frac{y_{LC}}{n_C^*}\right) + V_c\left(\frac{y_{LC}}{n_C^*}\right) \notag \\
	%
	I_C &= \frac{y_{LC}}{|n_C|^2}(V_C - V_B) - \frac{y_{LC}}{n_C^*}(V_c - V_b) \notag \\
	&= V_B\left(-\frac{y_{LC}}{|n_C|^2}\right) + V_C\left(\frac{y_{LC}}{|n_C|^2}\right) + V_b\left(\frac{y_{LC}}{n_C^*}\right) +  V_c\left(-\frac{y_{LC}}{n_C^*}\right) \notag \\
	%
	I_a &= -\frac{y_{LA}}{n_A}(V_A - V_B) + y_{LA}(V_a - V_b) \notag \\
	&= V_A\left(-\frac{y_{LA}}{n_A}\right) + V_B\left(\frac{y_{LA}}{n_A}\right) + V_a\left(y_{LA}\right) + V_b\left(-y_{LA}\right)\notag \\
	%
	I_b &= -y_\text{tie}(V_B - V_b) - I_a - I_c \notag \\
	&= V_A\left( \frac{y_{LA}}{n_A}\right) + V_B\left(-y_\text{tie} - \frac{y_{LA}}{n_A} - \frac{y_{LC}}{n_C}\right) + V_C\left( \frac{y_{LC}}{n_C}\right) \notag \\
	&+ V_a\left(-y_{LA}\right) + V_b\left(y_\text{tie} + y_{LA} + y_{LC}\right) + V_c\left(-y_{LC}\right) \notag \\
	%
	I_c &= -\frac{y_{LC}}{n_C}(V_C - V_B) + y_{LC}(V_c - V_b) \notag \\
	&= V_B\left(\frac{y_{LC}}{n_C}\right) + V_C\left( -\frac{y_{LC}}{n_C}\right) + V_b\left(-y_{LC}\right)  + V_c\left(y_{LC}\right)
\end{align}
Since $I = YV$, we can deduce the bus admittance matrix $Y$ as:
\begin{align}
Y &=
\begin{bmatrix}
	\frac{y_{LA}}{|n_A|^2} & -\frac{y_{LA}}{|n_A|^2} & 0 & -\frac{y_{LA}}{n_A^*} & \frac{y_{LA}}{n_A^*} & 0 \\
	%
	-\frac{y_{LA}}{|n_A|^2} & \left(y_\text{tie} + \frac{y_{LA}}{|n_A|^2} + \frac{y_{LC}}{|n_C|^2}\right) & -\frac{y_{C}}{|n_C|^2} & \frac{y_{LA}}{n_A^*} & \left(-y_\text{tie} -\frac{y_{LA}}{n_A^*} - \frac{y_{LC}}{n_C^*}\right) & \frac{y_{LC}}{n_C^*} \\
	%
	0 & -\frac{y_{LC}}{|n_C|^2} & \frac{y_{LC}}{|n_C|^2} & 0 & \frac{y_{LC}}{n_C^*} &  -\frac{y_{LC}}{n_C^*} \\
	%
	-\frac{y_{LA}}{n_A} & \frac{y_{LA}}{n_A} & 0 & y_{LA} & -y_{LA} & 0 \\
	%
	\frac{y_{LA}}{n_A} & \left(-y_\text{tie} - \frac{y_{LA}}{n_A} - \frac{y_{LC}}{n_C}\right) & \frac{y_{LC}}{n_C} & -y_{LA} & \left(y_\text{tie} + y_{LA} + y_{LC}\right) & -y_{LC} \\
	%
	0 & \frac{y_{LC}}{n_C} & -\frac{y_{LC}}{n_C} & 0 & -y_{LC}\ & y_{LC}
\end{bmatrix}
\end{align}

\section{General single phase branch model}
\label{SEC_GEN_BRANCH_MODEL}
Single phase transformers and transmission lines can both be incorporated within a single general branch model, shown in Fig. \ref{FIG_GEN_BRANCH_MODEL}.
\begin{figure}[!h]
	\begin{center}
		\includegraphics[width=(9cm)]{branch.png}
	\end{center}
	\caption{
		A general branch model, similar but more general than that used by {\sc Matpower}.
	}
	\label{FIG_GEN_BRANCH_MODEL}
\end{figure}
The $Y$ matrix for this model is defined by the following equation:
\begin{align}
	\begin{bmatrix}I_0 \\ I_1\end{bmatrix} &=
	\begin{bmatrix}\frac{y_a + y_b}{|n|^2} & -\frac{y_a}{n^*} \\ -\frac{y_a}{n} & y_a + y_c \end{bmatrix}
	\begin{bmatrix}V_0 \\ V_1\end{bmatrix}
\end{align}
Normally, $y_c = y_b$ for both transformers and transmission lines, as per the {\sc Matpower} manual. For a transmission line, $y_b$ is capacitative, and for a transformer it has a resistive and an inductive component and is associated with the ``no load'' current needed in the primary windings. For both transformers and lines, $y_a$ has a resistive component and an inductive component.

\section{Units and the per-unit system}
The following self-consistent system of units is often used in modelling electricity networks:
\begin{description}
	\item[Voltage:]kV (kilovolts)
	\item[Power:]MW/MVAR/MVA (megawatts etc.)
	\item[Current:]kA (kiloamps)
	\item[Resistance:]$\Omega$ (ohms)
	\item[Admittance:]$S$ (siemens)
\end{description}
Under this system, relationships like Ohm's Law hold without the need to include any additional constants. The alternative is to use SI units ($V/W/A/\Omega/S$\ldots).

The \emph{per-unit} system is also often used. The idea is to express electrical quantities as a \emph{per-unit} dimensionless quantity times a \emph{base unit}, e.g.
\begin{align}
V &= V_\text{pu}V_\text{base} \notag \\
S &= S_\text{pu}P_\text{base} \notag \\
I &= I_\text{pu}I_\text{base} \notag \\
&\text{etc.}
\end{align}

Base units $V_\text{base}$ and $P_\text{base}$ are normally first defined for the network. $V_\text{base}$ is set to the nominal operating voltage of a bus (e.g. 11~kV), which means that $V_\text{base}$ may vary from bus to bus. On the other hand, a single network-wide value for $P_\text{base}$ is usually chosen to be a convenient value such as 100~MVA. Other base quantities may then be derived:
\begin{align}
	I_\text{base} &= P_\text{base}/V_\text{base} \notag \\
	Z_\text{base} &= V_\text{base}^2 / P_\text{base} \notag \\
	Y_\text{base} &= P_\text{base} / V_\text{base}^2
\end{align}
Note that there is some ambiguity about which bus $V_\text{base}$ is referenced to in the expressions above. Consider for example a line that runs between two buses: logically there could be several ways to define the per-unit current: using the voltage at the first bus, the voltage at the second bus, or the voltage drop on the line. Depending on how things are done, different equations will need to be used for the network. The ambiguity is removed if we distinguish the power entering bus A along a line AB as being distinct from the power entering bus B along the same line, i.e. we only work with bus injections as opposed to line flows. A set of conventions consistent with \textsc{Matpower} is given below:
\begin{itemize}
\item Per-unit quantities in lines are calculated using the base-voltage at the second (to) bus. This makes sense in Matpower, since a common branch model is used where all transformers occur at the first (from) bus, with the bulk of the line occurring between the transformer and the second (to) bus.
\item Per-unit quantities associated with buses (such as bus shunt admittance, current loads etc. are calculated, naturally enough, using the base voltage of the bus.
\end{itemize}

\section{The Swing Equations}
A synchronous generator has a flywheel with moment of inertia $J$. There is a mechanical torque (e.g. from the turbine) $T_m$ pushing the rotor and an electrical torque $T_e$ that comes from the alternator and usually acts against the mechanical torque. We define the torque imbalance $\Delta T = T_m - T_e$. When $\Delta T$ is positive the rotor will speed up and when it is negative it will slow down. We define the rotor angle $\theta$ which is assumed to be zero at $t = 0$. Its first time derivative is the angular velocity $\omega = \dot{\theta}$.

The equation \footnote{For those not familiar with the notation, a dot above a variable indicates a time derivative, e.g. $\dot{\theta} := \partial \theta / \partial t$. Similarly, a double dot indicates a second time derivative, etc.} governing the motion of the rotor is
\begin{align}
	J \ddot{\theta} &= \Delta T
\end{align}
which is analogous to Newton's $F = ma$.

For a rotor running at the constant mandated network angular frequency $\omega_\text{nom}$, we have $\theta = \omega_\text{nom} t$. Because the rotor may deviate from this frequency, we define the angular error
\begin{align}
	\Delta \theta &= \theta - \omega_\text{nom} t. \label{EQ_DEFN_DELTA_THETA}
\end{align}
This quantity is basically equal to the voltage phase of the generator internal bus. In terms of this new variable, the generator equation is:
\begin{align}
	J \Delta \ddot{\theta} &= \Delta T
\end{align}

Since the power $P = \omega T$, we multiply both sides of the second equation by $\omega$ to obtain a relation for the power mismatch:
\begin{align}
	J\omega \Delta \ddot{\theta} &= \Delta P \notag \\
	J\left(\omega_\text{nom} + \Delta \dot{\theta}\right) \Delta\ddot{\theta} &= \Delta P
\end{align}
where $\Delta P$ is the input mechanical power minus the electrical power supplied to the network. When the rotor is running far from the mandated frequency, then this equation would need to be applied as is. However, at frequencies near the network frequency, $\omega_\text{nom} \gg \dot{\theta}$, and so we can write
\begin{align}
	J\omega_\text{nom} \Delta\ddot{\theta} &= L_\text{nom} \Delta\ddot{\theta} = \Delta P \label{EQ_SWING}
\end{align}
where $L_\text{nom} = J \omega_\text{nom}$ is the angular momentum of the rotor at the network frequency. This is the swing equation, and when solved will give the bus internal voltage angle as a function of time.

A note about units: if, as is common, we measure power in $\mathrm{MVA}$, then $L$ will need to be measured in commensurate units of $\mathrm{Mkg\cdot m^{-2}}$, i.e. we will need to divide the SI value by $10^6$.

\subsection{Multiple-pole generators}
Generators may have multiple pairs of north-south magnetic dipoles. In such cases, the rotor angular frequency will be scaled with respect to the network frequency. For example, a generator with two dipoles running on a 50~Hz network will turn at 25~Hz, because the north-south fields will pass the coils at twice the rate of the rotor's rotation.

Such cases are easily taken care of by absorbing the scaling factor into an effective moment of inertia $J_\text{eff} = NJ$, where N is the number of dipoles.

\subsection{The swing equations and the power flow equations}
We derive the electrical power at all generators using the power flow equations. The phase angle $\phi$ is fixed at the generator buses by the swing equations. The voltage magnitude $V$ could also be fixed (either to its fixed value, or scaled according to the current frequency), making all of the generator buses into effective slack buses. To ensure feasibility of the AC power flow equations, we could then convert all or some ZIP loads into constant impedance loads, or add a high constant impedance component to all loads to ensure feasibility.

\subsection{The swing equations at a three phase bus}
For three phase problems, the three terminals of the bus are connected to a single rotor, with a given angular momentum. The phase relationship between the bus terminals is enforced by the windings in the stator. The problem is what to do with this situation, in regards to the swing equations etc.

The answer is the obvious: that there is a single machine at each generator, which is input for the swing equations.
\end{document}


%!TEX root = paper.tex
% \section{Synthesizing \DIs}

\subsubsection{PCA-inspired \View Derivation}\label{candidatesec}

\setlength{\textfloatsep}{0mm}
\begin{algorithm}[t]
	\caption{Procedure to generate linear \views.}
	\label{fig:pca}
	\LinesNumbered
	\small{
    	\SetKwInOut{Inputs}{Inputs}
		\SetKwInOut{Output}{Output}
		\Inputs{
			A dataset $D \subset \DDom^m$
		}
		\Output{
                    A set $\{(F_1,\gamma_1),\ldots,(F_K,\gamma_K)\}$ of \views and importance factors
    	}
                $D_N  \gets D {\mbox{ after dropping non-numerical attributes}}$  \label{algo2:line1}\\
                $D'_N  \gets [\vec{1} ; D_N]$ \label{algo2:line2} \\
                $\{\vec{w}_1,\ldots,\vec{w}_K\} \gets {\mbox{ eigenvectors of }} {D_N'}^T D_N'$ \label{algo2:line31} \\
				\ForEach{$1 \le k \le K$}{
					$\vec{w}_k' \gets {\mbox{ $\vec{w}_k$ with first element removed}}$\label{algo2:line32} \\
                                        $F_k \gets \lambda\vec{A}: \frac{\vec{A}^T\vec{w}_k'}{||\vec{w}_k'||}$\label{algo2:line33} \\
					%{\mbox{ for $i=1,\ldots,k$}}$ \label{algo2:line3} \\
					$\gamma_k \gets \frac{1}{\log(2+\stddev{F_k(D_N)})}$ \label{algo2:line34}
					%\af{I fixed it with $\log$ since that is what we do.  Any argument needed for that? Also, I fixed $\stddev{F_i(D)}$ to $\stddev{F_i(D_N)}$. Check if that's correct.}
					% {\mbox{ for $i=1,\ldots,k$}}$ \label{algo2:line4} \\
				}
                \Return $\{(F_1,\frac{\gamma_1}{Z}),\ldots,(F_K,\frac{\gamma_K}{Z})\}$, where
                $Z = \sum_k \gamma_k$\label{algo2:line35}
	}
\end{algorithm}
\setlength{\textfloatsep}{5pt}

Theorem~\ref{THM:MAIN} sets the requirements for good \views (see
also~\cite{tveten2019principal, tveten2019tailored,
DBLP:journals/tnn/KunchevaF14} that make similar observations in different
ways). It indicates that we can start with any arbitrary \views and then
iteratively improve them. However, we can get the desired set of best \views in
one shot using an algorithm inspired by principal component analysis (PCA).
\revisetwo{PCA relies on computing eigenvectors. \label{pcadetails} There exist
different algorithms for computing eigenvectors (from the infinite space of
possible vectors). The general mechanism involves applying numerical approaches
to iteratively converge to the eigenvectors (up to a desired precision) as no
analytical solution exists in general.} Algorithm~\ref{fig:pca}  returns \views that
correspond to the principal components of a
slightly modified version of the given dataset: % (Fig.~\ref{fig:pcadetails}).
% Algorithm~\ref{fig:pca} details our approach for discovering \views for
% constructing \dis:

% The
% only additional technical point is the addition of a dummy attribute (column)
% to the dataset, which is set to the constant $1$ for each tuple. This is done
% because we are not normalizing the data (e.g., to make the mean of each column
% zero). It turns out that adding a constant dummy column
% lets us avoid normalizing the data. % achieves the same effect.
% We avoid normalizing just to keep the values unchanged so that the generated \di is
% interpretable on original values.  In detail, we have:
\smallskip

\begin{description}
	%
    \looseness-1
    \item[Line~\ref{algo2:line1}] Drop all non-numerical attributes from $D$ to
    get the numeric dataset $D_N$. This is necessary because PCA only applies
    to numerical values. Instead of dropping, one can also consider embedding
    techniques to convert non-numerical attributes to numerical ones.

    \item[Line~\ref{algo2:line2}] Add a new column to $D_N$ that consists of
    the constant $1$, to obtain the modified dataset $D_N' := [\vec{1} ; D_N]$,
    where $\vec{1}$ denotes the column vector with $1$ everywhere. We do this
    transformation to capture the additive constant within principal
    components, which ensures that the approach works even for unnormalized
    data.

    \item[Line~\ref{algo2:line31}] Compute $K$ eigenvectors of the square
    matrix ${D_N'}^{T}D_N'$, where $K$ denotes the number of columns in $D'_N$.
	These eigenvectors provide coefficients to construct \views.

    \item[Lines~\ref{algo2:line32}--\ref{algo2:line33}] Remove the first
    element (coefficient for the newly added constant column) of all
    eigenvectors and normalize them to generate \views. Note that we no longer
    need the constant element of the eigenvectors since we can appropriately
    adjust the bounds, $\lb$ and $\ub$, for each \view by evaluating it on
    $D_N$.
        %% bounds of
    %% the \dis by shifting the mean by the corresponding constant amount.
        % (The
 %    2-norm $||\vec{w}'_k||$ is $\sqrt{{\vec{w}_k}^{'T} \vec{w}'_k}$.)

    \item[Line~\ref{algo2:line34}] Compute importance factor for each \view.
    Since \views with smaller standard deviations are more discerning (i.e.,
    stronger), we assign each \view an importance factor ($\gamma$) that is
    inversely proportional to its standard deviation over $D_N$.

    \item[Line~\ref{algo2:line35}] Return the linear \views with corresponding
    normalized importance factors.

\end{description}
% \\
% (a) Drop all non-numerical attributes from $D$ to get $D_N$ (line~\ref{algo2:line1}).
% %Let $\vec{w}$ denote the unknown weights that define
% %the linear \view $F(\vec{A}) = \vec{A}^T\vec{w}$ on $D_N$. We prefer $\vec{w}$
% %that minimizes $\stddev{D_N\vec{w}}$.
% %
% %
% %
% %We are interested in learning linear functionals $F(A_1,\ldots,A_k)$ of the form
% %$w_0 + w_1 A_1  + w_2 A_2 + \cdots + w_k A_k$, where $x1,\ldots,A_k$ are the $k$ numerical columns
% %in $D_N$ and $w_0, w_1,\ldots,w_k$ are $k+1$ unknown weights.
% %%Since data can be noisy, we want to allow for the equality relation to be not necessarily an exact equality.
% %
% %Without loss of generality, we ignore the constant term $w_0$ in the \invariant. This is because
% %We find $\vec{w}$ as follows:
% \\
% %
% (b) Add a new column to $D_N$ that consists of the constant $1$, that is,
% $D_N' := [\vec{1} ; D_N]$, where $\vec{1}$ denotes the column vector with $1$
% everywhere (line~\ref{algo2:line2}).
% %If $D_N$ has $k-1$ columns, then $D_N'$ has $k$ columns.
% %
% \\
% %
% (c) Compute $k$ eigenvectors of the square matrix ${D_N'}^{T}D_N'$, where $k$
% denotes the number of columns in $D'_N$
% (line~\ref{algo2:line31}). % $\vec{v} := [v_1;v_2;\ldots;v_k]$ corresponding to the lowest
% %eigenvalue,
% %
% \\
% %
% (d) Remove the first element from each eigenvector (line~\ref{algo2:line32})
% and normalize them to generate \views (line~\ref{algo2:line33}). (The 2-norm
% $||\vec{v}||$ of $\vec{v}$ is $\sqrt{\vec{v}^T\vec{v}}$.)
% \\ (e) Compute
% importance factor for each \view (line~\ref{algo2:line34}).
% \\ (f) Return the
% linear \views with corresponding normalized importance factors
% (line~\ref{algo2:line35}).

\smallskip

We now claim that the \views returned by Algorithm~\ref{fig:pca} include the
\view with minimum standard deviation and
%. Furthermore, if $D$ is sufficiently large, then
the correlation between any two \views
%returned Algorithm~\ref{fig:pca}
is 0. This indicates that we cannot further improve
the \views, and, thus they are optimal.
%,and $[a;b]$ denotes $\left[\begin{matrix}a \\ b\end{matrix}\right]$.

\begin{theorem}[Correctness of Algorithm~\ref{fig:pca}]\label{THM:ALGO2-CORRECTNESS}
	%
    Given a numerical dataset $D$ over the schema $\mathcal{R}$, let
    $\mathcal{F} = \{F_1,F_2,\ldots, F_K\}$ be the set of linear \views
    returned by Algorithm~\ref{fig:pca}. Let $\sigma^* =
    \min_k^{K}\stddev{F_k(D)}$. If $\mu(A_k(D)) = 0$ for all attribute $A_k$ in
    $\mathcal{R}$, then,\footnote{When the condition $\forall A_k \;
    \mu(A_k(D)) = 0$ does not hold, slightly modified variants of the claim
    hold. However, by normalizing $D$ (i.e., by subtracting attribute mean
    $\mu(A_k(D))$ from each $A_k(D)$), it is always possible to satisfy the
    condition.}
%
	\begin{enumerate}[label=(\arabic*)]
            \item $\sigma^* \leq \stddev{F(D)}$ $\forall F=\vec{A}^T\vec{w}$ where $||\vec{w}||\geq 1$, and
    \item $\forall F_j, F_k \in \mathcal{F}$ s.t.\ $F_j\neq F_k$,
        % the corresponding eigenvalues $\varepsilon_j \neq \varepsilon_k$,
        $\rho_{F_j, F_k} = 0$.
		% (assuming, wlog, Algorithm~\ref{fig:pca} always picks                orthogonal eigenvectors).
        % $\lim_{|D| \to \infty} \rho_{F_j, F_k} = 0$, \textcolor{red}{under the assumption that
                % the columns of $D$ converge to some mean value and the
                % eigenvalues of $D^TD$ converge to some fixed values.}
	\end{enumerate}
    % Furthermore, any linear \view $F$ s.t.\ $\stddev{F(D)} = \sigma^*$
    % is equal to a linear
    % combination of $F_j$'s whose standard deviation is $\sigma^*$ on $D$.
\end{theorem}
% Let $\vecw$ be the (column) vector consisting of the unknown weights $w_1,\ldots,w_k$.
% Since $d\vecw$ may not identically be $0$ for all $d\in D_N$,
% we can cast the problem of finding $w_1,\ldots,w_k$ as an optimization problem
% that tries to minimize the sum $\sum_{d\in D_N} (d\vecw)^2$, which we shall call {\em{error}}.
% In matrix notation, this error can be written
% as $\vecw^T D_N^TD_N \vecw$.
% We restrict $\vecw$ to unit vectors to make the optimization problem well posed.

\ignore{
In Algorithm~\ref{fig:pca}, we use the above procedure to compute linear \views.
\af{I will pull this before, and will describe the procedure while referring
to the corresponding lines in the algorithm. I can do it if you agree.}
Algorithm~\ref{fig:pca} computes \views using {\em{all}} eigenvectors, rather
than just the eigenvector corresponding to the lowest eigenvalue.
\endignore}

Using \views $F_1, \ldots, F_K$, and importance factors
$\gamma_1,\ldots,\gamma_K$, returned by Algorithm~\ref{fig:pca}, we generate
the simple (conjunctive) \invariant with $K$ conjuncts:
%
$
 \bigwedge_k  \lb_k \leq F_k(\vec{A}) \leq \ub_k
$.
%
We compute the bounds $\lb_k$ and $\ub_k$ following Section~\ref{synth-bounds}
and use the importance factor $\gamma_k$ for the $k^{th}$ conjunct in the
quantitative semantics.

\begin{example}\label{ex:int} Algorithm~\ref{fig:pca} finds the projection of the \di of
Example~\ref{ex:tml}, but in a different form. The actual airlines dataset has
an attribute $\mathtt{distance}\; (\mathtt{DIS})$ that represents miles travelled by a flight. In
our experiments, we found the following \di\footnote{For ease of exposition, we
use $F(\vec{A}) \approx 0$ to denote $\epsilon_1 \le F(\vec{A}) \le
\epsilon_2$, where $\epsilon_i \approx 0$.} over the dataset of daytime flights:
%
{
\begin{align}\label{eq:one}
0.7 \times \mathtt{AT} - 0.7 \times \mathtt{DT} - 0.14 \times \mathtt{DUR} - 0.07 \times \mathtt{DIS} \approx 0
\end{align}
}
%
This \invariant is not quite interpretable by itself, but it is in fact a
linear combination of two expected and interpretable
\invariants:~\footnote{\revisetwo{We developed a tool~\cite{DBLP:conf/sigmod/FarihaTRG20} to explain causes of
non-conformance.~\citeTechRep}}
%
{
\begin{align}
	\mathtt{AT} - \mathtt{DT} - \mathtt{DUR} &\approx 0 \label{eq:two}\\
	\mathtt{DUR} - 0.12 \times \mathtt{DIS} &\approx 0 \label{eq:three}
\end{align}
}
%
Here, (\ref{eq:two}) is the one mentioned in Example~\ref{ex:tml} and
(\ref{eq:three}) follows from the fact that average aircraft speed is about
$500$ mph implying that it requires $0.12$ minutes per mile. $0.7$ $\times$
(\ref{eq:two}) + $0.56$ $\times$ (\ref{eq:three}) yields:
%
{
\begin{align*}
&0.7 \times (\mathtt{AT} - \mathtt{DT} - \mathtt{DUR}) + 0.56 \times \mathtt{DUR} - 0.56 \times 0.12 \times \mathtt{DIS} \approx 0 \\
\implies & 0.7 \times \mathtt{AT} - 0.7 \times \mathtt{DT} - 0.14 \times \mathtt{DUR} - 0.07 \times \mathtt{DIS} \approx 0
\end{align*}
}
%
Which is exactly the \di~(\ref{eq:one}). Algorithm~\ref{fig:pca} found the
optimal \view of~(\ref{eq:one}), which is a linear combination of the \views
of~(\ref{eq:two}) and~(\ref{eq:three}). The reason is: there is a correlation
between the \views of~(\ref{eq:two}) and~(\ref{eq:three}) over the dataset
(Theorem~\ref{THM:MAIN}). One possible explanation of this correlation is:
whenever there is an error in the reported duration of a tuple, it violates
both~(\ref{eq:two}) and~(\ref{eq:three}). Due to this natural correlation,
Algorithm~\ref{fig:pca} returned the optimal \view of~(\ref{eq:one}), that
``covers'' both \views of~(\ref{eq:two}) or~(\ref{eq:three}).

%
\end{example}

\ignore{

The reason for including \views corresponding to all eigenvalues in the
simple \invariant---in addition to the one with smallest standard
deviation---is that it only strengthens the \invariant. Note that we use importance factors $\gamma_i$ to give
more importance to the \views with smaller standard deviations. One obvious
question is: can our procedure miss some linear \views with small standard
deviations? The following result states that we do not miss any linear \views
corresponding to the {\em{smallest}} achievable standard deviation.

% Consider the eigenvalues and eigenvectors of the positive semi-definite matrix $D_N^TD_N$.
% Let $\eig_1,\ldots,\eig_k$ be the $k$ eigenvalues, and
% $\vecv_1, \ldots,\vecv_k$ be the corresponding $k$ eigenvectors of $D_N^TD_N$. Assuming that the eigenvectors are normalized to have norm $1$, we have
% $$
 % \vecv_j^T D_N^TD_N \vecv_j = \vecv_j^T \eig_j\vecv_j = \eig_j
% $$
% Since $D_N^TD_N$ is real-valued, symmetric, and positive semi-definite, we know that $\eig_j$'s are all non-negative reals.
% WLOG, assume $\eig_1 \leq \eig_2 \leq \cdots \leq \eig_k$.
% Define $k$ linear functionals $f_1,f_2,\ldots,f_k$,
% where $f_i$ is defined as $f_i(A_1,\ldots,A_k) = \vecv_{j1}A_1 + \cdots + \vecv_{jk}A_k$.
% For these $k$ choices,
% the spectrum, $\eig_1, \ldots, \eig_k$, of the matrix $D_N^TD_N$, provides us with different choices for the error value.
% %
% Since we want to minimize error \af{I don't think this argument is correct. As
% mentioned in Section 2, we want neither overfit nor underfit \invariants. If we
% wanted to minimize error, you could always learn a NULL \invariant, why even
% just one with lowest eigenvalue?}, we should pick $\vecv_1$, the eigenvector
% corresponding to $\eig_1$, to construct our \invariant. In fact, we should
% use all eigenvectors corresponding to small eigenvalues, since some prior
% work~\cite{tveten2019principal, tveten2019tailored,
% DBLP:journals/tnn/KunchevaF14} showed that low eigenvalue principal components
% are more sensitive to a general change. Rather than use some heuristic to
% determine which eigenvalues are ``small'' and which are not, in our
% implementation, we just use all eigenvectors \af{I think we can get rid of
% defer it to the Experiment section where I would keep a subsection for
% implementation specifics.}, but {\em{weigh them differently}} according to the
% standard deviation of $f_i(D_N)$, as we stated before. \todo{Talk about
% $\gamma$ being $\frac{1}{\log(2 + \stddev{D})}$. And then talk about
% normalizing them}. Note that the above derivation shows that $\stddev{f_j}$ is
% related to the value $\eig_j$ of the corresponding eigenvalue.
% Algorithm~\ref{fig:pca} summarizes the procedure for computing the linear
% functionals from a data set $D$, which are then used to define the actual
% \invariants as described in previous section.\af{I am not sure if the algorithm
% is very useful, as PCA is a very well known technique.}\af{This is the heart of
% this section, and one of our core novelty. I think we should revise this with
% better justification and intuition. At present, it looks very weak.}

\begin{theorem}
    Let $D$ be a dataset and
    $F_1,F_2,\ldots$ be the linear \views returned by the procedure in Algorithm~\ref{fig:pca}
    when on $D$.
    Let $\sigma^* = \min(\{\stddev{F_i(D)} | i=1,2,\ldots\})$.
    If there is a linear \view $F$ s.t. $\stddev{F(D)} = \sigma^*$, then $F$ will be in the linear subspace spanned by all the linear
     $F_j$ with standard deviation $\sigma^*$ on $D$.
\end{theorem}
\af{I now have high level idea of what this theorem is trying to say, but I still
think we can omit it in this paper.}
% .. Dw.Dw = Dv.Dv and ||w||=||v||=1
% ||av + bw||^2 = a^2 + b^2 + 2ab cos(theta)
% D(av+bw).D(av+bw) = (aDv + bDw).(aDv + bDw) = a^2 S + b^2 S + 2ab (Dv.Dw) = aaS + bbS + 2abS v^Tw = S!!

We observe that the most common way in which PCA is used involves {\em{throwing
away}} the low variance components, whereas we give {\em{most importance}} to
these components. This role reversal is one of our key insights in synthesizing good
\dis.

\endignore}

\subsection{Compound \DIs}\label{sec:disjunctive}
% We now describe computation of \emph{disjunctive} linear \invariants.
The quality of our PCA-based simple linear \invariants relies on how many low
variance linear \views we are able to find on the given dataset. For many
datasets, it is possible we find very few, or even none, such linear \views. In
these cases, it is fruitful to search for compound \invariants; we first focus
on {\em{disjunctive \invariants}} (defined by $\psi_A$ in our language grammar).

The PCA-based approach fails in cases where there exist different piecewise linear
trends within the data, as it will result into low-quality \invariants, with
very high variances. In such cases, partitioning the dataset and then learning
\invariants separately on each partition will result in significant improvement
of the learned \invariants. A disjunctive \invariant is a compound \invariant
of the form $\bigvee_k((A = c_k) \maxand \phi_k)$, where each $\phi_k$ is a
\invariant for a specific partition of $D$. Finding disjunctive \invariants
involves horizontally partitioning the dataset $D$ into smaller disjoint
datasets $D_1, D_2, \ldots, D_L$. Our strategy for partitioning $D$ is to use
categorical attributes with a small domain in $D$; in our implementation, we
use those attributes $A_j$ for which $|\{t.A_j | t\in D\}|\le50$. If $A_j$ is
such an attribute with values $v_1, v_2, \ldots, v_L$, we partition $D$ into
$L$ disjoint datasets $D_1, D_2,\ldots, D_L$, where $D_l = \{t \in D | t.A_j =
v_l\}$. Let $\phi_1, \phi_2, \ldots, \phi_L$ be the $L$ simple \dis we learn
for $D_1, D_2, \ldots, D_L$ using Algorithm~\ref{fig:pca}, respectively. We
compute the following disjunctive \di for $D$:\\
%  (recall that the quantitative semantics
% of \dis does not satisfy the typical Boolean algebra laws for $\vee$
% and $\wedge$)
%
\indent $
 ((A_j = v_1) \maxand \phi_1) \vee
 ((A_j = v_2) \maxand \phi_2) \vee
\cdots \vee
 ((A_j = v_L) \maxand \phi_L)\\[-0.7em]
$

\looseness-1 We repeat this process and partition $D$ across multiple
attributes and generate a compound disjunctive \invariant for each attribute.
Then we generate the final compound conjunctive \di ($\Psi$) for $D$, which is
the conjunction of all these disjunctive \invariants. Intuitively, this final
\di forms a set of \emph{overlapping} hyper-boxes around the data tuples.

\subsection{Theoretical Analysis}\label{sec:complexity}

\subsubsection{Runtime Complexity}\looseness-1 Computing simple \invariants
involves two computational steps: (1)~computing $X^TX$, where $X$ is an
$n\times m$ matrix with $n$ tuples and $m$ attributes, which~takes
$\mathcal{O}(nm^2)$ time, and (2)~computing the eigenvalues and eigenvectors of
an $m\times m$ positive definite matrix, which has complexity
$\mathcal{O}(m^3)$~\cite{DBLP:conf/stoc/PanC99}. Once we obtain the linear
\views using the above two steps, we need to compute the mean and variance of
these \views on the original dataset, which takes $\mathcal{O}(nm^2)$ time. In
summary, the overall procedure is cubic in the number of attributes and linear
in the number of tuples.
%
For computing disjunctive \invariants, we greedily pick attributes that take at
most $L$ (typically small) distinct values, and then run the above procedure
for simple \invariants at most $L$ times. This adds just a constant factor
overhead per attribute.

\subsubsection{Memory Complexity} The procedure can be implemented in
$\mathcal{O}(m^2)$ space. The key observation is that $X^T X$ can be computed
as $\sum_{i=1}^{n} t_i t_i^T$, where $t_i$ is the $i^{th}$ tuple in the
dataset. Thus, $X^TX$ can be computed incrementally by loading only one tuple
at a time into memory, computing $t_i t_i^T$, and then adding that to a running
sum, which can be stored in $\mathcal{O}(m^2)$ space. Note that instead of such
an incremental computation, this can also be done in an embarrassingly parallel
way where we horizontally partition the data (row-wise) and each partition is
computed in parallel.

% Due to such low time and memory complexity, our approach
% scales gracefully to large datasets.

\revisetwo{\subsubsection{Implication, Redundancy, and Minimality}\label{sec:impl}
Definition~\ref{def:stronger} gives us the notion of \emph{implication} on
\dis: for a dataset $D$, satisfying $\phi_1$ that is stronger than $\phi_2$
implies that $D$ would satisfy $\phi_2$ as well.
%
Lemma~\ref{LEMMA:MAIN} and Theorem~\ref{THM:MAIN} associate \emph{redundancy}
with correlation: correlated projections can be combined to construct a new
projection that makes the correlated projections redundant.
Theorem~\ref{THM:ALGO2-CORRECTNESS} shows that our PCA-based procedure finds
a non-redundant (orthogonal and uncorrelated) set of projections. For disjunctive
\invariants, it is possible to observe redundancy across partitions. However,
our quantitative semantics ensures that redundancy does not affect the
violation score.
%
Another notion relevant to data profiles (e.g., FDs) is \emph{minimality}. In
this work, we do not focus on finding the minimal set of \dis. Towards
achieving minimality for \dis, a future direction is to explore techniques for
optimal data partitioning. However, our approach computes only $m$ \dis for
each partition. Further, for a single tuple, only $m_N \cdot m_C$ \dis are applicable,
where $m_{N}$ and $m_C$ are the number of numerical and categorical attributes
in $D$ (i.e., $m = m_{N} + m_C$). The quantity $m_N \cdot m_C$ is upper-bounded by
$\frac{m^2}{4}$. }


\section{Asymptotic Theory}

Asymptotic theory, large sample theory, or limit theory, deals with the question of the limiting behavior of sequences of random variables.

The notion of convergence for random variables is more involved than is usual for, for example, a sequence of numbers or functions in calculus. There are different types of convergence, and they do not necessarilty imply one another.

The chain of implication is:

\begin{equation}
\mathrm{quadratic\ mean} \rightarrow \mathrm{probability} \rightarrow \mathrm{distribution}
\end{equation}

For point mass distributions only, convergence in distribution implies convergence in probability. Similarlty, implication rules exist for functions of random variables when convergence for the underlying random variables is given. See Theorems 5.4, 5.5 and 5.17 in \citeasnoun{wasserman2013all} (pages 73-74, 81).

\subsection{Preasymptotics}
The behavior in the intermediate regime, for example where $n$ large but not yet asymptotic. Asymptotic behavior kicks in at very different rates for different underlying processes. The ``baseline" in terms of speed is represented by normally distributed random variables.

%convergence in probability
\subsection{Convergence in Probability}

\begin{equation}
\mathbb{P}(|X_n - X| > \epsilon) \rightarrow 0
\end{equation}

as $n\rightarrow \infty$. Convergence in Probability means that the distribution of $X_n$ becomes sharper and sharper around $X$ as $n\rightarrow \infty$. At $n=\infty$, it has point mass distribution concentrated at $X$.

% convergence in distribution
\subsection{Convergence in Distribution}
Where $F$ is the CDF of $X_n$,

\begin{equation}
\lim_{n\rightarrow \infty} F_n(t) = F(t)
\end{equation}

For all $t$ for which $F$ is continuous. That means, convergence is satisfied even when the equality is violated at points of discontinuity.

% convergence in quadratic mean
\subsection{Convergence in Quadratic Mean, Convergence in $L_2$}

\begin{equation}
\mathbb{E}(X_n - X)^2 \rightarrow 0
\end{equation}

as $n\rightarrow \infty$.

% almost sure convergence
\subsection{Almost Sure Convergence}

$X_n$ converges \textit{almost surely} to $X$ if:

\begin{equation}
\mathbb{P}(\{\omega: X_n(\omega) \rightarrow X(\omega) \}) = 1
\end{equation}

Which I would read as the value of the measurable map $X_n$ converging to the value of the measurable map $X$ everywhere on the probability space except possibly on a set of probability measure $0$.

% l1 convergence
\subsection{$L_1$ Convergence}

$L_1$ convergence requires $\mathbb{E}|X_n - X| \rightarrow 0$ as $n\rightarrow 0$.

% WLLN
\subsection{Weak Law of Large Numbers}

The sample mean of i.i.d. variables  $\overline{X}_n$ converges in probability to the mean of the distribution $\mu$i. It can be proven with Chebyshev's inequality that:

\begin{equation}
\mathbb{P}(|\overline{X}_n-\mu|>\epsilon) \leq \frac{\mathbb{V}(\overline{X}_n)}{\epsilon^2}=\frac{\sigma^2}{n \epsilon^2}
\end{equation}

In words, the probability that the sample mean deviates from the population mean by more than $\epsilon$ has an upper bound that decreases inversely proportional to $n\epsilon^2$. The probability becomes more and more centered around the mean $\mu$.

% SLLN
\subsection{Strong Law of Large Numbers}
The strong law of large number gives almost surely convergence of the sample mean to the population mean.

If $\mathbb{E}|X_1| < \infty$, then $\overline{X_n}\xrightarrow{as}\mu$.

% CLT
\subsection{Central Limit Theorem}
The distribution of the sample mean converges in distribution to a normal distribution with variance $\sigma^2/n$ and mean $\mu$.

If $Z_n = \frac{\overline{X}_n - \mu}{\sqrt{\mathbb{V}(\overline{X}_n)}}$ then $\lim_{n\rightarrow \infty} \mathbb{P}(Z_n \leq z) = \Phi(z)$, where $\Phi(z)$ is the CDF of a standard normal distribution.

It turns out that when $Z_n$ is obtained by normalizing not by the (most likely unknown) population variance $\sigma$ but by the sample variance $S_n^2$, the CLT still holds. The accuracy of this is given by the Berry-Ess\'een Inequality.

% multivariate CLT
\subsection{Multivariate Central Limit Theorem}
Given $\mathbf{X_1, ... ,X_n}$ i.i.d random vectors where each vector:

\begin{equation}
\mathbf{X_i} = \left(\begin{array}{c}X_{1i}\\ X_{2i} \\ \vdots \\ X_{ki} \end{array}\right)
\end{equation}

Then the population mean:

\begin{equation}
\mathbf{\mu} = \left(\begin{array}{c} \mu_1 \\ \mu_2 \\ \vdots \\ \mu_k \end{array} \right) =  \left(\begin{array}{c} \mathbb{E}(X_{i1}) \\ \mathbb{E}(X_{i2}) \\ \vdots \\ \mathbb{E}(X_{ik}) \end{array} \right)
\end{equation}

The variance is given by the matrix $\Sigma$ as before. The sample mean:

\begin{equation}
\overline{\mathbf{X}} = \left(\begin{array}{c}\overline{X_{1}}\\ \overline{X_{2}} \\ \vdots \\ \overline{X_{k}} \end{array}\right)
\end{equation}

Then $\sigma^{-\frac{1}{2}} (\overline{X}-\mu)$ converges in distribution ot $\mathscr{N}(0,1)$.

% proof
\subsection{Proof of the Central Limit Theorem}
\citeasnoun{wasserman2013all}, page 81.

Given i.i.d random variables $X_i$, the transformation $Y_i = \frac{X_i-\mu}{\sigma}$ gives i.i.d. random variables with zero mean and unit variance. Let $\psi(t)$ be the MGF of $Y_i$. Since $Y_i$ are i.i.d., the sum $\sum_{i=1}^n Y_i$ has MGF $\psi(t)^n$. The normalized sample mean $Z_n = \frac{1}{\sqrt{n}}\sum_{i=1}^n Y_i$ has MGF $\Xi_n(t)=\psi(t/\sqrt{n})^n$. Two random variables that have the same MGF in an open interval about the point $t=0$ have the same distribution, probably because the Laplace transform is injective. Therefore, if $\psi_n(t)\rightarrow \psi(t)$ in some open interval around $t=0$, then their underlying random variables $Z_n \xrightarrow{dist}Z_n$ converge in distribution. Taking the Taylor expansion of $\epsilon_n(t)$:

\begin{equation}
\epsilon_n(t) = \left(1+0+\frac{t^2}{2! n} + ... \right)^n \rightarrow e^{t^2/2}
\end{equation}

Which is the MGF of $\mathscr{N}(0,1)$

% Delta Method
\subsection{Delta Method}

The delta method allows statements regarding the convergence of functions of random variables, whenever the input random variable converges in distribution to a normal distribution.

If $Y_n$ has a limiting normal distribution, and $g(Y_n )$ is a smooth function so that $g'(\mu) \neq 0$, then if:

\begin{equation}
\frac{\sqrt{n}(Y_n - \mu)}{\sigma} \xrightarrow{dist} \mathscr{N}(0,1)
\end{equation}

Then:

\begin{equation}
\frac{\sqrt{n}(g(Y_n)-g(\mu))}{|g'(\mu)|\sigma}\xrightarrow{dist}\mathscr{N}(0,1)
\end{equation}

Rewriting, if $Y_n \xrightarrow{dist}\mathscr{N}(\mu,\frac{\sigma^2}{n})$ then $g(Y_n)\xrightarrow{dist}\mathscr{N}(g(\mu),(g'(\mu))^2\frac{\sigma^2}{n})$.

\subsubsection{Multivariate Delta Method}

If $\mathbf{Y_n} \xrightarrow{dist}\mathscr{\mathbf{\mu},\mathbf{\Sigma}}$ then the scalar-valued function $g(\mathbf{Y_n}) \xrightarrow{dist}\mathscr{N}(g(\mathbf{\mu}),\frac{1}{n} (\nabla g(\mu))^T \mathbf{\Sigma} (\nabla g(\mu)) )$.

Use case would be functions of several sample means, where the underlying samples have non-trivial covariance (cf. \cite{wasserman2013all}, p. 80).


\section{Technical Cryptography}
\label{sec:tech_crypto}

In this section we present in detail the specifics of the
cryptography we will be using for MadNetwork.
At times we will be verbose in our algorithmic details
and design choices to allow for others to understand our decisions.

We will begin by comparing this work with the
Ethereum Distributed Key Generation whitepaper~\cite{ethdkg}
in Sec.~\ref{ssec:ethdkg_comparison};
our design is based this paper.
Some of the implementation-specific details are covered in
Sec.~\ref{ssec:pk_curve_specifics}.
In Sec.~\ref{ssec:math_def}, we discuss the mathematics
related to pairing-based cryptography; this is integral to
our work as it is required for our group signatures.
We describe the distributed key generation protocol in
Sec.~\ref{ssec:dkg};
the specific method of shared secret encryption is
described in Sec.~\ref{ssec:secret_enc}.
We discuss how to construct group signatures in
Sec.~\ref{ssec:crypto_group_sig}.
Group signatures from pairing-based cryptography require
a hash-to-curve function, and we talk about our construction
based on~\cite{ft2012bnhashtocurve,boneh2019h2cBLS12}
in Sec.~\ref{ssec:hash-to-curve}.

We follow~\cite{ethdkg} in our definition of
$\parens{t,n}$-thresholded system, where we need $t+1$ actors
for consensus.
Unfortunately, this is different than what was used previously,
where $\parens{t,n}$-thresholded system meant $t$ actors were
needed to agree.
We keep this difference for ease of comparison with the referenced paper.

\input{tex/tcrypt_comparison.tex}
\input{tex/tcrypt_spec_imp.tex}
\input{tex/tcrypt_math_def.tex}
\input{tex/tcrypt_dkg.tex}
\input{tex/tcrypt_sse.tex}
\input{tex/tcrypt_grpsig.tex}
\input{tex/tcrypt_h2c.tex}