\chapter{Idealized rates and instruments}
\label{chap:instuments}

\added[comment={Added}]{In this chapter we review idealized versions of interest rates and derivatives of interest rates. By idealized, we mean that the instruments are simplied for analytical purposes. For example, there is no lag between trade and spot date or expiry and delivery date. Neither we do not use funding rate that is separate form the market rates. The treatment is standard and is based mainly on} \textcite[pp. 1--22]{brigo2007interest} unless otherwise noted.

In the following, we assume that $0 < t < T$ are points of time and $\dayc(t,T) \in [ 0, \infty )$ is the day count convention between the points $t$ and $T$. We explicitly assume that $\dayc(t,T) \approx T-t$ when $t \approx T$.

\section{Fundamental rates and instruments}

\subsection{Short-rate, idealized bank account and stochastic discount factor}

When making calculations with idealized bank account, it is customary to assume that the day count-convention $\dayc(t,T) = T-t$ as this will simplify the notation. An idealized bank account is an instrument with the value
\begin{align}
\Bank(t) = \exp \left( \int\limits_0^t r(s) \dx s \right)
\end{align}
where $r(t)$ is the short-rate rate. The short-rate $r(t)$ may be non-deterministic but we assume that it is smooth enough so that the integral can be defined in some useful sense. We note that $\Bank(0)=1$. If $\delta > 0$ is very small and $r(t)$ is a smooth function, then
\begin{align}
\int\limits_t^{t+\delta} r(s) \dx s \approx r(t) \delta
\end{align}
and we see that the first-order expansion of exponential function yields
\begin{align}
\Bank(t+\delta) \approx \Bank(t) ( 1+r(t) \delta ) .
\end{align}
Thus the short-rate can be seen as continuous interest rate intensity. Short-rate is purely theoretical construction which can be used to price financial instruments.

Now we may define a stochastic discount factor $\DF(t,T)$ from time $t$ to $T$ as
\begin{align}
\DF(t,T) = \frac{\Bank(t)}{\Bank(T)} = \exp \left( - \int\limits_t^T r(s) \dx s \right) .
\end{align}
If $r(t)$ is a random variable, then $\Bank(t)$ and $\DF(t,T)$ are stochastic too.

\subsection{Zero-coupon bond}

A promise to pay one unit of currency at time $T$ is called a $T$-bond. We shall assume that there is no credit risk for these bonds. We further assume that the market is liquid and bond may be freely bought and sold at the same price, furthermore short selling is allowed without limits or extra fees. The price of this bond at time $t$ is denoted by $\Bond(t,T)$ and so $\Bond(t,T) > 0$ and $\Bond(T,T) = 1$.

As $\DF(0,t)$ is guaranteed to pay one unit of currency at the time $t$, we see that in this case $\DF(0,t) = \Bond(0,t)$. We note that if the short-rate $r(t)$ is deterministic, then
\begin{align}
\DF(0,t) = \frac{1}{\Bank(t)}
\end{align}
is deterministic too.  We see that if short-rate $r(t)$ is deterministic, then $\DF(t,T) = \Bond(t,T)$ for all $0 \leq t \leq T$. But this does not hold if $r(t)$ is truly stochastic.

\subsection{Simple spot $\Rflt(t,T)$ and $k$-times compounded simple spot rate}

The simple spot rate $\Rflt(t,T)$ is defined by
	\begin{align}
		\Rflt(t,T) = \frac{1 - \Bond(t,T)}{\dayc(t,T)\Bond(t,T)} ,
	\end{align}
which is equivalent to
	\begin{align}
		\label{discountandrate}
		1 + \dayc(t,T) \Rflt(t,T) = \frac{1}{\Bond(t,T)} .
	\end{align}

For $k \geq 1$, the $k$-times compounded interest rate from $t$ to $T$ is
\begin{align}
\Rflt^k(t,T) = \frac{k}{\Bond(t,T)^{\frac{1}{k \dayc(t,T)}}} - k,
\end{align}
which is equivalent to
\begin{align}
\Bond(t,T) \left( 1 + \frac{\Rflt^k(t,T)}{k} \right)^{k \dayc(t,T)} = 1 .
\end{align}
As
\begin{align}
(1 + \frac{x}{k})^k \longrightarrow \e^x
\end{align}
when $k \longrightarrow \infty$, then
\begin{align}
\left( 1 + \frac{L^k(t,T)}{k} \right)^{k\dayc(t,T)} \longrightarrow \e^{\dayc(t,T)r(t,T)} ,
\end{align}
where $\Rflt^k(t,T) \longrightarrow r(t,T)$ when $k \longrightarrow \infty$.

\subsection{Forward rate agreement}

A forward rate agreement (FRA) is a contract that pays
\begin{align}
\dayc_K(t,T) K - \dayc(t,T) \Rflt(t,T)
\end{align}
at the time $T$. Here we assume that the contact is made at the present time $0$ and $0 < t < T$, but this assumption is made just to keep the notation simplier. Here $K$ is an interest rate that is fixed at time $0$, $\dayc_K$ is the day count convention for the this fixed rate and $\Rflt(t,T)$ is the spot rate from time $t$ to $T$ (which might not be know at the present). The price of a FRA at the time $s \leq t$ is denoted by $\FRA(s,t,T,K)$. Now
\begin{align}
\FRA(t,t,T,K) = \Bond(t,T) \left( \dayc_K(t,T) K - \dayc(t,T) \Rflt(t,T) \right) .
\end{align}

In order to price a FRA at different times, we consider a portfolio of one long $T$-bond and $x$ short $t$-bonds. The value of this portfolio at the present is $V(0) = \Bond(0,T) - x \Bond(0,t)$ and we note that the portfolio has zero value if
\begin{align}
\label{FRAzeroprice}
x = \frac{\Bond(0,T)}{\Bond(0,t)}.
\end{align}

At the time $t$, the portfolio has value
\begin{align}
V(t) &= \Bond(t,T) - x \\
&= \Bond(t,T) \left( 1 - \frac{x}{\Bond(t,T)} \right)
\end{align}
where $\Bond(t,T)$ is known and
\begin{align}
1 + \dayc(t,T) \Rflt(t,T) = \frac{1}{\Bond(t,T)} = y(t,T).
\end{align}
We define $K^*(x) = x^{-1}$. Thus
\begin{align}
1 - \frac{x}{\Bond(t,T)} &= x \left( \frac{1}{x} - \frac{1}{\Bond(t,T)} \right) \\
&= x \left( K^*(x) -  y(t,T)  \right)
\end{align}
and this implies that
\begin{align}
\label{forwardpricelemmaquation}
V(t) &= x \Bond(t,T) \left( K^*(x) - y(t,T) \right) .
\end{align}
Without arbitrage
\begin{align}
\label{forwardpricelemmaquation1}
V(T) &= x \left( K^*(x) - y(t,T) \right) \\
&= x \left( K^*(x) - 1 - \dayc(t,T) \Rflt(t,T) \right)
\end{align}
We note that at the time $0$, $K^*(x)$ is a known yield but $y(t,T)$ is an unknown yield if $\Bond(t,T)$ is not deterministic. Now if
\begin{align}
K &= \frac{1}{\dayc_K(t,T)} \left( K^*(x) - 1 \right) \\
&= \frac{1}{\dayc_K(t,T)} \left( \frac{1}{x} - 1 \right)
\end{align}
the given portfolio can be used to replicate the cash flows of the FRA and
\begin{align}
x \FRA(s,t,T,K) = V(s) .
\end{align}
If
\begin{align}
x &= \frac{\Bond(0,T)}{p(0,t)}
\end{align}
then $V(0) = 0$ and
\begin{align}
K &= \frac{1}{\dayc_K(t,T)} \left( \frac{\Bond(0,t)}{\Bond(0,T)} - 1 \right) \\
&= \frac{\dayc(t,T)}{\dayc_K(t,T)}  \Rflt(t,T) .
\end{align}
We see that the forward rate and the rate that defines FRA with zero present value are essentially the same. Thus we define that the forward rate at the time $t$ from time $T$ to $S$ is
\begin{align}
	\Rflt(t,T,S) &= \frac{1}{\dayc(T,S)} \left( \frac{\Bond(t,T)}{\Bond(t,S)} - 1 \right) .
\end{align}
Since $\dayc(T,S) \approx S-T$ when $T \approx S$, we have that
	\begin{align}
		\Rflt(t,T,S) &= \frac{1}{\dayc(T,S)} \left( \frac{\Bond(t,T)}{\Bond(t,S)} - 1 \right) \\
				   &\approx \frac{1}{\Bond(t,T)} \frac{\Bond(t,T) - \Bond(T,S)}{S-T}
	\end{align}
and therefore
	\begin{align}
		\Rflt(t,T,S) & \longrightarrow - \frac{1}{\Bond(t,T)} \frac{ \partial \Bond(t,T) }{ \partial t} \\
			&= - \frac{\partial \log \Bond(t,T)}{\partial T}
	\end{align}
when $S \rightarrow T^+$ under the assumption that the zero curve $\Bond(t,T)$ is differentiable. We now define that the instantaneous forward rate at the time $t$ is
	\begin{align}
		\Forwardrate(t,T) = - \frac{\partial \log \Bond(t,T)}{\partial T}.
	\end{align}
Now since $\Bond(t,t) = 1$,
	\begin{align}
		- \int_t^T f(t,s) \dx s &= \int_t^T \partial \log \Bond(t,s) \dx s \\
			&=  \log \Bond(t,T) - \log \Bond(t,t) \\ &= \log \Bond(t,T)
	\end{align}
meaning that
	\begin{align}
		\Bond(t,T) = \exp \left( - \int\limits_t^T \Forwardrate (t,s) \dx s \right) .
	\end{align}

\section{Interest rate instruments}

\subsection{Fixed leg and floating leg}

A leg with tenor $t_0 < t_1 < t_2 < \ldots < t_n = T$ and coupons $c_1, c_2, \ldots, c_n$ is an instruments that pays $c_i$ at the time $t_i$ for all $1 \leq i \leq n$. The coupons may be functions of some variables. Thus a is a portfolio of $n$ zero-coupon bonds with maturities coinciding with tenor. It has has present value of
  \begin{align}
    \sum_{i=1}^n c_i \Bond (t, t_i) \1_{ \{ t \geq t_i \} }
  \end{align}
at the time $t$.

A floating leg with a unit principal has coupons defined by $c_i = \dayc_1(t_{i-1}, t_i) \Rflt(t_{i-1}, t_i)$, where $\Rflt$ is a reference rate for a floating. It has a present value of
\begin{align}
PV_{\text{float}}(t) &= \sum_{i=1}^n \Bond (t,t_i) \dayc_1(t_{i-1}, t_i) \Rflt(t_{i-1}, t_i) \\
	&= \sum_{i=1}^n \Bond (t,t_i) \left( \frac{1}{\Bond(t_{i-1},t_i)} -1 \right) \\
	&= \sum_{i=1}^n \Bond (t,t_{i-1}) \Bond (t_{i-1},t_i) \left( \frac{1}{\Bond(t_{i-1},t_i)} -1 \right) \\
	&= \sum_{i=1}^n \left( \Bond (t,t_{i-1}) - \Bond (t,t_{i-1})\Bond (t_{i-1},t_i) \right) \\
	&= \sum_{i=1}^n \left( \Bond (t,t_{i-1}) - \Bond (t,t_i) \right) \\
	&= \Bond (t,t_{0}) - \Bond (t,t_n)
\end{align}
and especially $PV_{\text{float}}(t_0) = 1 - \Bond (t,t_n)$.

If the coupons are $c_i = K \dayc_0(t_{i-1}, t_i)$ for a fixed rate $K$, then we call it as a fixed leg with a unit principal. It has a present value
  	\begin{align}
		PV_{\text{fixed}}(t) &= K \sum_{i=1}^n  \dayc_0(t_{i-1}, t_i) \Bond (t,t_i) .
	\end{align}

\subsection{Coupon bearing bond}

A coupon bearing bond with floating coupons and a unit principal is combination of a floating leg and payment of one currency unit coinciding with the last tenor date. Thus it has present value of
\begin{align}
PV_{\text{floating bond}}(t) = \Bond (t,t_{0})
\end{align}
and especially $PV_{\text{floating bond}}(t_0) = 1$.

Similarly a coupon bearing bond with fixed coupons and a unit principal is combination of a fixed leg and payment of one currency unit coinciding with the last tenor date. It has a present value of
	\begin{align}
		PV_{\text{fixed bond}}(t) &= \Bond (t,t_n) + K \sum_{i=1}^n  \dayc_0(t_{i-1}, t_i) \Bond (t,t_i) \\
		&= \Bond (t,t_n) + PV_{\text{fixed}}(t) .
	\end{align}

\subsection{Vanilla interest rate swap}

A vanilla payer interest rate swap (IRS) is a contract defined by paying a fixed leg and receiving a floating leg. A vanilla receiver interest rate swap (IRS) is a contract defined by paying a floating leg and receiving a fixed leg. The legs may have different amount of coupons. Also the coupons dates and day count conventions may not coincide. If $K$ is the common rate for the fixed leg and both legs have the same notional value, then a payer IRS has the present value of
  	\begin{align}
		 \sum_{i=1}^m \Bond (t,t'_i) \dayc_1(t'_{i-1}, t'_i) \Rflt(t'_{i-1}, t'_i) - K \sum_{i=1}^n \Bond (t,t_i) \dayc_0(t_{i-1}, t_i)
	\end{align}
where $t'_0 < t'_1 < t'_2 < \ldots < t'_m$ are the coupon times for the floating leg. A par swap is a swap with present value of zero and the fixed rate for a par swap is
  	\begin{align}
		K = \frac{ \sum\limits_{i=1}^m \Bond (t,t'_i) \dayc_1(t'_{i-1}, t'_i) \Rflt(t'_{i-1}, t'_i) }{ \sum\limits_{i=1}^n \Bond (t,t_i) \dayc_0(t_{i-1}, t_i) }
	\end{align}
It is easy to see that if the both legs have same underlying notional principal and coupon dates are the same, then the swap is just a collection of forward rate agreements with a fixed strike price. A vanilla payer IRS let the payer to hedge interest rate risk by converting a liability with floating rate payments into fixed payments.

\subsection{Overnight indexed swap}

At the end of a banking day, banks and other financial institutions may face surplus or shortage of funds. They may lend the excess or borrow the shortfall on overnight market. Overnight lending rate is often regarded as a proxy for risk-free rate. In Euro area, European Central Bank calculates Eonia, which is a weighted average of all overnight unsecured lending transactions in the interbank market.

Overnight indexed swap (OIS) is a swap where a compounded reference overnight lending rate is exchanged to a fixed rate.

\subsection{Call and put option and call-put parity}

A European call (put) option gives the buyer the right but not an obligation to buy (sell) a designated underlying instrument from the option seller with a fixed price at expiry date. Thus a call option on $T$-bond with strike price $K$ and maturity $S < T$ has the final value
	\begin{align}
		\ZBC(S,S,T,K) = \left( \Bond(S,T) - K \right)^+
	\end{align}
and the corresponding put option has the final value
	\begin{align}
		\ZBP(S,S,T,K) = \left( K - \Bond(S,T) \right)^+ .
	\end{align}

A portfolio of long one call and short one put option on a same $T$-bond with identical strike price $K$ and maturity $S$ has final value of
	\begin{align}
		\left( \Bond(S,T) - K \right)^+ - \left( K - \Bond(S,T) \right)^+ = \Bond(S,T) - K.
	\end{align}
Therefore, without any arbitrage, we have the so called call-put--parity
	\begin{align}
		\ZBC(t,S,T,K) - \ZBP(t,S,T,K) = \Bond(t,T) - \Bond(t,S) K
	\end{align}
holds for all $t \leq S$.

\subsection{Caplet, cap, floorlet and floor}

In order to keep notation simplier, we assume that the present is $0$ and $0 < t < T$. A caplet is an interest rate derivative in which the buyer receives
	\begin{align}
		\left( \Rflt(t,T) - K \right)^+
	\end{align}
at the time $T$, where $\Rflt(t,T)$ is some reference rate and $K$ is the fixed strike price. The fixing is done at when the contract is made.

Suppose that a firm must pay a floating rate $L$. By buying a cap with strike $K$ against $L$, the firm is paying
	\begin{align}
		L - \left( L - K \right)^+  = \min (L,K)
	\end{align}
meaning that the highest rate will pay will be the strike rate $K$. Thus caps may be used to hedge interest rate risk.

Now
	\begin{align}
		\Rflt(t,T) - K &= \frac{1}{\dayc(t,T)} \left( 1+\dayc(t,T)\Rflt(t,T) - K^* \right) \\
			&= \frac{1}{\dayc(t,T)} \left( \frac{1}{p(t,T)} - K^* \right)
	\end{align}
where $K^* = 1+ \dayc(t,T)K$. Thus the value of a caplet at the time $t$ is
	\begin{align}
		p(t,T) \left( \Rflt(t,T) - K \right)^+ &= \frac{p(t,T)}{\dayc(t,T)} \left( \frac{1}{p(t,T)} - K^* \right)^+ \\
			&= \frac{1}{\dayc(t,T)} \left( 1 - p(t,T) K^* \right)^+ \\
			&= \frac{K^*}{\dayc(t,T)} \left( \frac{1}{K^*} - p(t,T) \right)^+ .
	\end{align}
But this is the price of $\frac{K^*}{\dayc(t,T)}$ put options on a $T$-bond with strike price $\frac{1}{K^*}$ at the time of strike $t$. Thus we can price a caplet as a put option on a bond. As the price of a cap contains optionality, we must model the interest rates in order to price it.

A cap is a linear collection of caplets with the same strike price.

A floorlet is an derivate with the payment
	\begin{align}
\dayc(t,T) \left( K - \Rflt(t,T) \right)^+
\end{align}
at the time $T$, where $\Rflt(t,T)$ is some reference rate with day-count convention $\dayc(t,T)$ and $K$ is the fixed strike price. Similarly a floor is a linear collection of floorlets with the same strike price. We can price a floorlet is the price of $\frac{K^*}{\dayc(t,T)}$ call options on a $T$-bond with strike price $\frac{1}{K^*}$ at the time of strike $t$.

\subsection{Swaption}

A swaption is an interest rate derivative that allows the owner the right but not an obligation to enter into an IRS. A payer swaption gives the owner the right to enter a payer swap (a swap paying a fixed rate while receiving floating rate). A receiver swaption gives the owner the option to initiate a receiver swaption (a swap paying a floating rate while receiving a fixed rate).

A European payer swaption is equivalent to a European put option on a coupon bearing bond. The underlying swap have the value of
  	\begin{align}
		\Swap(S) = PV_{\text{float}}(S) - PV_{\text{fixed}}(S) .
	\end{align}
at the time of the strike $S$. Thus
	\begin{align}
		\Swaption(S) &= \left( PV_{\text{float}}(S) - PV_{\text{fixed}}(S) \right)^+ \\
			&= \left( 1 - \Bond (t,t_n) - PV_{\text{fixed}}(S) \right)^+ \\
			&= \left( 1 - PV_{\text{fixed bond}}(S) \right)^+ .
	\end{align}
We see that a swaption is a european put option on a fixed rate coupon bond. The coupon rate is the fixed rate of the underlying swap and strike price is the principal of the bond and the underlying swap.

In some cases we may price a swaption as a portfolio of options on zero-coupon bond. This trick was introduced in \cite{jamshidian1989anexactbondoptionformula}. We now denote the price of a zero coupon bond as a function of a short rate $\Bond(t,T,r)$. We consider a put option with maturity $S$ and strike price $K$ on a bond with coupond $c_i$ occuring at times $t_i$, $i=1,2, \ldots, n$. Let $r^*$ be the rate with the property
	\begin{align}
		K &= \sum_{i=1}^n c_i \Bond(S, t_i, r^*)  .
	\end{align}
Now the put option has a value
	\begin{align}
		\left( K - \sum_{i=1}^n c_i \Bond(S,t_i) \right)^+ &= \left( \sum_{i=1}^n c_i \left( \Bond(S, t_i, r^*) - \Bond(S,t_i,r(S)) \right) \right)^+ .
	\end{align}
If we assume that the bond prices are uniformly decreasing function on the initial short rate, then the options will be exercised if and only if $r^* < r(S)$ and now
	\begin{align}
		\Bond(S, t_i, r^*) > \Bond(S,t_i,r(S)) .
	\end{align}
for all $i$ Otherwise all $\Bond(S, t_i, r^*) \leq \Bond(S,t_i,r(S))$ for all $i$. Thus the put option has value
	\begin{align}
		\sum_{i=1}^n c_i \left( \Bond(S, t_i, r^*) - \Bond(S,t_i,r(S)) \right)^+
	\end{align}
which is a portfolio of put options with maturities $S$ on a zero coupon bonds with strike prices of $\Bond(S, t_i, r^*)$. The assumption behind this trick assumes in essence that the prices of the zero coupon bonds moves in unison. This is satisfied by one-factor models but the assumption does not hold for multi-factor models.

Similarly, a European receiver swaption is equivalent to a European call option on a coupon bearing bond. Under the same assumption, we may disassemble a receiver swaption as a portfolio of call options on zero coupon bonds.

\iffalse

\subsection{Eurodollar futures}

Eurodollar futures is a contract that will swap
	\begin{align}
		1- \Rflt(T,S)
	\end{align}
with
	\begin{align}
		1 - \FUT(t,T,S)
	\end{align}
at the time $T$, where $t < T < S$ and the futures rate $\FUT(t,T,S)$ is set so that entering futures contract at the time $t$ costs nothing. Futures contracts are resettled continuously, meaning that for a small time horizon $\delta > 0$, the owner of a futures contract will have a cash flow of
	\begin{align}
		\FUT(s,T,S) - \FUT(s+\delta,T,S) .
	\end{align}
In practice, resettlement is done daily. As $\FUT(T,T,S) = \Rflt (T,S)$, the holder of a futures contract has experienced undiscounted net cash flow of
	\begin{align}
		\sum\limits_{t \leq s < T} \left( \FUT(s,T,S) - \FUT(s+\delta,T,S) \right) = \FUT(t,T,S) - \Rflt (T,S) ,
	\end{align}
but we note this number contains undiscounted cash flows from different point of times. If interest rates raises, the futures contract will lose value. On the other hand, falling interest rates will make contract more valuable. Assuming that the rates of different tenor move in unison, the refinancing cost of settling a move of $n>0$ basis points is higher than the benefits of reinvesting gains from a movement of $-n$ basis points.

\fi

\section{Defaultable instruments and credit default swaps}

\subsection{Defaultable $T$-bond}

A defaultable $T$-bond with no recovery (NR) is an instrument that pays
	\begin{align}
		\DBond(T,T) = \begin{cases} 1, & T < \default \\ 0, & T \geq \default \end{cases}
	\end{align}
at the time $T$, where $\default$ is the time of a default of the underlying. The price of a defaultable $T$-bond at the time $t < T$ is denoted by $\DBond(t,T)$.

A defaultable $T$-bond with recovery of treasury (RT) has the same final payout is a defaultable $T$-bond with no recovery but in addition it pays $\delta \Bond(\default, T)$ if $\default \leq T$, where $0 < \delta < 1$. Thus it was a terminal value of
	\begin{align}
		\DBond(T,T) = \1_{ \{ \default > T \} } + \delta \1_{ \{ \default \leq T \} } .
	\end{align}

A defaultable $T$-bond with recovery of face value (RFV) has the same final payout is a defaultable $T$-bond with no recovery but in addition it pays $\delta$ at the default if $\default \leq T$, where $0 < \delta < 1$. Thus it was a terminal value of
	\begin{align}
		\DBond(T,T) = \1_{ \{ \default > T \} } + \delta \1_{ \{ \default \leq T \} } \Bond(\default,T) .
	\end{align}

A defaultable $T$-bond with recovery of market value (RMV) has the same final payout is a defaultable $T$-bond with no recovery but in addition it pays $\delta\DBond(\default,T)$ at the default if $\default \leq T$, where $0 < \delta < 1$. Thus it was a terminal value of
\begin{align}
\DBond(T,T) = \1_{ \{ \default > T \} } + \delta \1_{ \{ \default \leq T \} } \Bond(\default,T) .
\end{align}

\subsection{Credit default swap}

A credit default swap (CDS) is an instrument where the seller of the contract will compensate the buyer if the reference instrument or entity has a credit event such as a default. In exchange, the buyer will make periodic payments to the seller until the end of the contract or the default event. The buyer of CDS will be hedged against the credit risk of the reference entity. Originally physical settlement was used. If the credit event occurs before the maturity of the CDS, then the seller is obligated to buy the underlying reference debt for face value. Since the notional value of credit default swaps may be greater than the underlying debt, physical settlement is a cumbersome process and cash settlements are held instead. In order to determine the value of a contract after the default, a credit event auction is held to determine the recovery value $\Rec$ (\cite{ISDAbigbang}, \cite{BISquarterlyreview2010}).

Suppose that the CDS will offer protection from $S$ to $T$ and $\default$ is the time of the credit event. The protection seller has agreed to pay the buyer $\LGD = 1 - \Rec$ at the time $\default$ if $S \leq \default \leq T$. The protection leg of CDS has a value of
	\begin{align}
		\Protection(t) = \Bond(t, \default) \LGD \1_{ \{ S \leq \default \leq T \} }
	\end{align}
at the time $t$. Let $S = t_0 < t_1 < t_2 < \ldots <t_n = T$. The premium leg will pay a coupon rate $C$ at the times $t_1 < t_2 < \ldots < t_n$ if the credit event has not occurred. If the credit event happes, then the buyer will pay the accrued premium rate at the time of the default. The premium leg has a value of
	\begin{align}
		\Premium(t, C) =\sum_{i=1}^n \Bond(t, t_i) \dayc(t_{i-1},t_i) C \1_{ \{ \default > t_i \} } + \Bond(t, \default) \dayc(t,\default) C \1_{ \{ t_s \leq \default \leq t_{s+1} \} }
	\end{align}
where $t_s$ is the last date from $t_0 < t_1 < \ldots < t_n$ before the credit event (if it occurs).

Standardized CDS contracts have quarterly coupon payments and rates are usually set to be either $25$, $100$, $500$ or $1000$ basis points. So when traded the buyer will pay
	\begin{align}
		\Premium(0, C) - \Protection(0) .
	\end{align}
Earlier the coupon rate $C$ was set so that $\Premium(0, C) = \Protection(0)$ and no money was exchanged at the trade.


\section{Hierarchical Models}

\begin{frame}{Structure of HM - Setup}
  \Large{
  \emphcol{Hierarchical Data:}\pause
  \begin{itemize}
    \item[] \emphcol{Individual Level:} $(y_i, x_i)$ for $i = 1,\mydots,n$\pause
    \item[] \emphcol{Group Level:} $u_j$ for $j = 1,\mydots,J$
  \end{itemize}\pause
  \emphcol{Example:}
  \begin{itemize}
    \item[] \emphcol{Test Outcome:} $y_i$
    \item[] \emphcol{Parental Income:} $x_i$
    \item[] \emphcol{Num. of Teachers:} $u_j$
  \end{itemize}
  }
\end{frame}

\begin{frame}{Structure of HM}
\begin{figure}
\begin{center}
\begin{tikzpicture}%
  [vertex/.style={circle,draw=black,fill=white, minimum size=1cm},
  node distance=2.5cm,
  >=latex,
  on grid]
  \node[vertex] (phi) {$\phi$};
  \node[vertex,below left=1.5cm and 2cm of phi] (theta1) {$\theta_1$};
  \node[vertex,below right=1.5cm and 2cm of phi] (thetaJ) {$\theta_J$};
  \node[below=1.5cm of phi] (dots1) {$\dots$};
  \node[rectangle, draw=black, minimum size=0.9cm, left=2cm of theta1] (u1) {$u_1$};
  \node[rectangle, draw=black, minimum size=0.9cm, right=2cm of thetaJ] (uJ) {$u_J$};
  \node[vertex,below=2cm of theta1] (y1) {$y(1)$};
  \node[vertex,below=2cm of thetaJ] (yJ) {$y(J)$};
  \node[rectangle, draw=black, minimum size=0.9cm, left=2cm of y1] (x1) {$x(1)$};
  \node[rectangle, draw=black, minimum size=0.9cm, right=2cm of yJ] (xJ) {$x(J)$};
  \node[below=3.5cm of phi] (dots1) {$\dots$};
  \draw[->]
    (phi) edge (theta1)
    (phi) edge (thetaJ)
    (u1) edge (theta1)
    (uJ) edge (thetaJ)
    (theta1) edge (y1)
    (thetaJ) edge (yJ)
    (x1) edge (y1)
    (xJ) edge (yJ);
\end{tikzpicture}
\end{center}
\end{figure}
\end{frame}

\begin{frame}{The Prior Revisited}
  \Large{
    \emphcol{Before:}
    \begin{itemize}
        \item[] \emphcol{Model:} $p(\text{data} \mid \theta)$
        \item[] \emphcol{Prior:} $p(\theta)$
      \end{itemize}\pause
    \emphcol{Now:}
    \begin{itemize}
      \item[] \emphcol{Model:} $p(\text{data} \mid \theta, \phi) = p(\text{data} \mid \theta)$
      \item[] \emphcol{Prior:} $p(\theta \mid \phi)$
      \item[] \emphcol{Hyperprior:} $p(\phi)$
    \end{itemize}
  }
\end{frame}

\begin{frame}{The Posterior Revisited}
  \Large{
    \emphcol{Posterior:}
    \begin{align*}
    \action<+->{p(\theta, \phi \mid \text{data}) &\propto p(\text{data} \mid \theta) p(\theta, \phi)\\}
    \action<+->{&\propto p(\text{data} \mid \theta) p(\theta \mid \phi) p(\phi)\\}
    \action<+->{p(\phi \mid \text{data}) &\propto \int p(\theta, \phi \mid \text{data}) \mathrm{d} \theta}
    \end{align*}
  }
\end{frame}

\begin{frame}{Varying Slopes, Varying Intercepts}
  \Large{
    \begin{itemize}
      \item[] \emphcol{Setup:} Individual $i$ in group $j$\pause
      \item[] \emphcol{Individual Level:} $y_i = \alpha_{j[i]} + \beta_{j[i]} x_i + \epsilon_i$\pause
      \item[] \emphcol{Group Level:} $\sqmat{\alpha_j\\\beta_j} = \gamma_0 + \gamma u_j + \eta_j$\pause
      \item[] \emphcol{Priors on:} $\gamma_0, \gamma, \epsilon_i, \eta_j$\pause
    \end{itemize}
  }
\end{frame}


\documentclass[11pt]{article}

\usepackage{graphicx}
\usepackage{courier}
\usepackage{underscore}
\setlength\parindent{0pt}

\title{Laconic Operation Documentation}
\author{Adam Yedidia}

\begin{document}

\maketitle

This document enumerates all the primitive operations in Laconic. It is recommended that users start by reading \texttt{laconic_quick_start.pdf} to get a sense for how Laconic works. \\

Remember that operations can be combined into complex expressions (i.e. \texttt{(a+b)*c}) but that full parenthesization is required! Also, recall that \texttt{int}s are signed integers with no maximum or minimum value, \texttt{list}s are Python-style lists of \texttt{int}s, and \texttt{list2}s are Python-style lists of \texttt{list}s.

\section{Integer Operations}

Assume in the operations below that \texttt{x} and \texttt{y} are variables of type \texttt{int} with values $x$ and $y$, respectively. \\

All the operations below yield values of type \texttt{int}.

\subsection{Addition}

The expression \texttt{x+y} yields the value $x+y$.

\subsection{Subtraction}

The expression \texttt{x-y} yields the value $x-y$.

\subsection{Multiplication}

The expression \texttt{x*y} yields the value $xy$.

\subsection{Integer Division}

The expression \texttt{x/y} yields the value $s(xy)\left\lfloor|\frac{x}{y}|\right\rfloor$, where $s()$ is the sign function. In plain English, integer division in Laconic rounds numbers to the lowest-magnitude adjacent number. This means that \texttt{3/2} would yield the value 1, and \texttt{(0-3)/2} would yield the value $-1$. \\

If \texttt{y} is 0, Laconic will throw a runtime error if interpreted, and the compiled TMD or Turing machine will enter an infinite loop.

\subsection{Negation}

The expression \texttt{{\raise.17ex\hbox{$\scriptstyle\sim$}}x} yields the value $-x$. Note the strange negation operator.

\subsection{Equality}

The expression \texttt{x==y} yields the value 1 if $x=y$, and the value 0 otherwise.

\subsection{Inequality}

The expression \texttt{x!=y} yields the value 1 if $x\not=y$, and the value 0 otherwise.

\subsection{Greater Than}

The expression \texttt{x>y} yields the value 1 if $x>y$, and the value 0 otherwise.

\subsection{Less Than}

The expression \texttt{x<y} yields the value 1 if $x<y$, and the value 0 otherwise.

\subsection{Greater or Equal}

The expression \texttt{x>=y} yields the value 1 if $x\ge y$, and the value 0 otherwise.

\subsection{Less Than or Equal}

The expression \texttt{x<=y} yields the value 1 if $x\le y$, and the value 0 otherwise.

\subsection{And}

The expression \texttt{x\&y} yields the value 1 if $x>0$ and $y>0$, and the value 0 otherwise. Note that negative values of $x$ and $y$ are interpreted as ``false'' values for the purposes of ``boolean'' operations.

\subsection{Or}

The expression \texttt{x|y} yields the value 1 if $x>0$ or $y>0$, and the value 0 otherwise.

\subsection{Not}

The expression \texttt{!x} yields the value 1 if $x\le 0$, and the value 0 otherwise.

\section{List and List2 Operations}

Assume in the operations below that \texttt{x} is a variable of type \texttt{int}, \texttt{l} is a variable of type \texttt{list}, and that \texttt{L} is a variable of type \texttt{list2}. Assume that these variables have values of $x$, $l$, and $L$, respectively.

\subsection{Indexing}

The expression \texttt{l@x} yields the \texttt{int} value of the $x^{\textrm{th}}$ element of $l$, assuming 0-indexing. If $x\le |l|$, a runtime error is thrown in both the interpreted and compiled versions of the code.\\

The expression \texttt{L@*x} yields the \texttt{list} value of the $x^{\textrm{th}}$ element of $L$, assuming 0-indexing. If $x\le |L|$, a runtime error is thrown in both the interpreted and compiled versions of the code. \\

In general, \texttt{list2} operations use the same symbol as the corresponding \texttt{list} operations, but with a \texttt{*} at the end.

\subsection{Appending}

The expression \texttt{l\char`\^x} yields the \texttt{list} value of $l||[x]$, where $||$ denotes the concatenation operation. \\

The expression \texttt{L\char`\^*l} yields the \texttt{list2} value of $L||[l]$, where $||$ denotes the concatenation operation.

\subsection{Length}

The expression \texttt{\#l} yields the \texttt{int} value of $|l|$. \\

The expression \texttt{\#*L} yields the \texttt{int} value of $|L|$.

\subsection{Concatenation}

In the expressions below, the variables \texttt{l1}, \texttt{l2}, \texttt{L1}, and \texttt{L2} have types \texttt{list}, \texttt{list}, \texttt{list2}, and \texttt{list2}, with values of $l_1$, $l_2$, $L_1$, and $L_2$, respectively. \\

The expression \texttt{l1||l2} yields the \texttt{list} value of $l_1||l_2$, where $||$ denotes the concatenation operation. \\

The expression \texttt{L1||*L2} yields the \texttt{list2} value of $L_1||L_2$, where $||$ denotes the concatenation operation.

\subsection{Explicit Description}

To explicitly enumerate a \texttt{list}, put the elements of the \texttt{list} between brackets and separated by commas. For example, \texttt{[x, 1, y]} would yield the \texttt{list} value $[x, 1, y]$. \\

To explicitly enumerate a \texttt{list2}, put the elements of the \texttt{list2} between colons and separated by commas. For example, \texttt{:[x, 1, y], l1, []:} would yield the \texttt{list2} value $[[x, 1, y], l_1, []]$.

\end{document}


\subsection{Relations}\label{subsec:relations}

\begin{definition}\label{def:cartesian_product}\mcite[def. 1.23]{OpenLogicFull}
  We are now in a vicious cycle where we need binary Cartesian products in order to define arbitrary Cartesian products. We will do this as quickly as possible, without introducing relations and functions. The latter two will be discussed in detail in \fullref{subsec:relations} and \fullref{subsec:functions}, respectively.

  \begin{thmenum}
    \thmitem{def:cartesian_product/kuratowski_pair} The \term{Kuratowski pair} or simply \term{ordered pair} \( \braket{ x, y } \) of the sets \( x \) and \( y \) is defined as
    \begin{equation*}
      \braket{ x, y } \coloneqq \set{ \set{ x }, \set{ x, y } }.
    \end{equation*}

    This is a simple and widespread definition that encodes the order of \( x \) and \( y \), unlike the set \( \set{ x, y } \) for example.

    We will later use the notation \( (x, y) \), but until \fullref{rem:kuratowski_pairs_and_tuples}, we want to distinguish between Kuratowski pairs and \( 2 \)-tuples.

    We will use the following \hyperref[rem:predicate_formula]{predicate formula} in \( \ref{eq:def:function/predicate}[\rho, \tau, \sigma] \):
    \begin{equation*}\taglabel[\op{IsPair}]{eq:def:cartesian_product/kuratowski_pair_predicate}
      \ref{eq:def:cartesian_product/kuratowski_pair_predicate}[\rho, \tau, \sigma] \coloneqq \qforall \xi \parens[\Bigg]{ \xi \in \rho \leftrightarrow \parens[\Big]{ \parens[\Big]{ \qforall {\eta \in \xi} \eta \doteq \tau } \vee \parens[\Big]{ \qforall {\eta \in \xi} (\eta \doteq \tau \vee \eta \doteq \sigma) } } }
    \end{equation*}

    \thmitem{def:cartesian_product/indexed_family} A set \( i \) of Kuratowski pairs is called an \term{indexed family} if whenever \( \braket{ k, A } \in i \) and \( \braket{ k, B } \in i \), we have \( A = B \). It is conventional to denote this unique set corresponding to \( k \) as \( A_k \) without an explicit reference to \( i \).

    The \term{index set} of the family is
    \begin{equation*}
      \mscrK \coloneqq \set{ k \given \qexists A \braket{ k, A } \in i }.
    \end{equation*}

    The family itself is then denoted as
    \begin{equation*}
      \seq{ A_k }_{k \in \mscrK}.
    \end{equation*}

    If \( A_k \in \mscrA \) for every \( k \in \mscrK \), we sometimes write
    \begin{equation*}
      \seq{ A_k }_{k \in \mscrK} \subseteq \mscrA,
    \end{equation*}
    although the latter is an embedding rather than set inclusion.

    \thmitem{def:cartesian_product/tuple} A \term{tuple} from the indexed family \( \seq{ A_k }_{k \in \mscrK} \) is another indexed family \( \seq{ x_k }_{k \in \mscrK} \) with the same index set satisfying the condition that for every \( k \in \mscrK \), the value \( x_k \) belongs to \( A_k \).

    We will see later that this is precisely a \hyperref[def:choice_function]{choice function}.

    \thmitem{def:cartesian_product/product} The \term{Cartesian product} of an indexed family \( \seq{ A_k }_{k \in \mscrK} \) is the set of all tuples from this family. We denote the Cartesian product by
    \begin{equation*}
      \prod_{k \in \mscrK} A_k.
    \end{equation*}

    With the availability of \hyperref[def:function]{functions} in \fullref{subsec:functions}, we can define the Cartesian product as the set
    \begin{equation*}
      \prod_{k \in \mscrK} A_k = \set{ f: A \to \bigcup_{k \in \mscrK} A_k \given \qforall {k \in \mscrK} k \in A_k }.
    \end{equation*}
  \end{thmenum}
\end{definition}

\begin{definition}\label{def:sequence}
  Families indexed by \( \omega \) are called \term{infinite sequences} or simply \term{sequences}. We will use several notations, depending on the context.
  \begin{itemize}
    \item \( \seq{ A_k }_{k \in \BbbN} \), which is the conventional notation for indexed families.
    \item \( \seq{ A_k }_{k=0}^\infty \), which easily extends to cases such as \( \seq{ A_k }_{k=m}^n \) when the index set is \( \set{ m, \ldots, n } \).
    \item \( (A_0, A_1, \ldots) \), which is used when explicitly enumerating members of the sequence.
  \end{itemize}

  It is conventional to write a family \( \seq{ A_k }_{k \in n} \) indexed by a natural number \( n \) using the notation \( (A_1, \cdots, A_n) \), with or without the outer parentheses. Families indexed by natural numbers are called \term{finite sequences}.

  For \( n = 2 \), finite sequences are called \term{pairs}, for \( n = 3 \) --- \term{triples} and for \( n = 4 \) --- \term{quadruples}.

  We say that \( A_{k_1}, A_{k_2}, \ldots \) is a \term{subsequence} of \( A_1, A_2, \ldots \) if \( k_1 < k_2 < \ldots \), i.e. if the sequence of indices is monotone in the sense of \eqref{eq:def:partially_ordered_set/homomorphism/sequence}.
\end{definition}

\begin{remark}\label{rem:kuratowski_pairs_and_tuples}
  Note that the tuple \( (A, B) \)
  \begin{equation*}
    (A, B) = \set[\Bigg]{ \set[\Big]{ \set{ 0 }, \set{ 0, A } }, \set[\Big]{ \set{ 1 }, \set{ 1, B } } }
  \end{equation*}
  is formally different from the \hyperref[def:cartesian_product/kuratowski_pair]{Kuratowski pair}
  \begin{equation*}
    \braket{ A, B } = \set[\Big]{ \set{ A }, \set{ A, B } }.
  \end{equation*}

  This is one reason we hurried to define general Cartesian products --- we wanted to avoid working with tuples defined in terms of Kuratowski pairs. We even introduced a special notation for them, just so we can avoid any confusion. Nevertheless, it is conventional to conflate Kuratowski pairs with \( \set{ 0, 1 } \)-indexed tuples.

  We also conflate the tuples \( (A, (B, C)) \), \( ((A, B), C) \) and \( (A, B, C) \).
\end{remark}

\begin{definition}\label{def:relation}
  Let \( A_1, \ldots, A_n \) be a \hyperref[def:sequence]{finite sequence} of sets and let
  \begin{equation*}
    R \subseteq A_1 \times \cdots \times A_n
  \end{equation*}
  be a subset of their \hyperref[def:cartesian_product/product]{Cartesian product}.

  The sequence \( (R, A_1, \ldots, A_n) \) is called an \( n \)-ary \term{relation}. We say that the tuple \( (x_1, \ldots, x_n) \in A_1 \times \cdots \times A_n \) is related with respect to \( R \) if \( (x_1, \ldots, x_n) \in R \).

  Relations are the semantical counterpart to \hyperref[def:first_order_structure/interpretation/predicate]{first-order predicates} and are equivalent to Boolean-valued functions, as discussed in \fullref{rem:boolean_valued_functions_and_predicates}.

  We generalize only the following notions from \hyperref[def:binary_relation]{binary relations}:
  \begin{thmenum}[series=def:relation]
    \thmitem{def:relation/graph} The set \( R \) of tuples is called the \term{graph} of the relation. In case the sequence \( A_1, \ldots, A_n \) is clear from the context, we can identify the relation \( (R, A_1, \ldots, A_n) \) with its graph \( R \). We occasionally use the notation \( \gph(R) \) for explicitly denoting the graph.

    \thmitem{def:relation/signature} The \term{signature} of the relation is the sequence \( (A_1, \ldots, A_n) \). Obviously this definition only makes sense if we know what the signature is, either from the context or from the definition of the relation as the sequence \( (R, A_1, \ldots, A_n) \) rather than only via its graph \( R \).

    As a matter of fact, it is common to ignore the signature when defining relations --- see e.g. \cite[7]{Kelley1955} or \cite[def. 2.1]{OpenLogicFull}. If we do identify a relation only with its graph, however some notions like \hyperref[def:multi_valued_function/range]{ranges} and \hyperref[def:multi_valued_function/image]{images} coincide despite being different and other notions like \hyperref[def:function_invertibility/surjective]{function surjectivity} make no sense.

    Furthermore, two relations whose graphs are equal may have different signatures, which further highlights how important it is to distinguish between a relation and its graph.

    \thmitem{def:relation/arity} For some small values of \( n \), \( n \)-ary relations have established names:
    \begin{itemize}
      \item \term{Nullary} if \( n = 0 \).
      \item \term{Unary} if \( n = 1 \).
      \item \term{Binary} if \( n = 2 \).
      \item \term{Ternary} if \( n = 3 \).
    \end{itemize}

    This is not to be confused with \hyperref[def:multi_valued_function/arity]{function arity} --- functions are always binary relations.

    \thmitem{def:relation/single_set} If all \( A_k \) for \( k = 1, \ldots, n \) are equal to the set \( A \), we say that \( R \subseteq A^n \) is a relation \hi{on \( A \)}.
  \end{thmenum}
\end{definition}

\begin{definition}\label{def:binary_relation}
  An important special case of \hyperref[def:relation]{relations} are \term{binary relations}. Given two sets \( A \) and \( B \), a binary relation between them is a triple \( (R, A, B) \).

  In addition to the terminology for \fullref{def:relation}, we also introduce the following terms:
  \begin{thmenum}[series=def:binary_relation]
    \thmitem{def:binary_relation/empty} The relation is \term{empty} if its graph is the empty set, i.e. if no two elements are related.

    It is important to highlight that the graphs of all empty relations are equal, but two empty relations are only equal if their signatures are.

    \thmitem{def:binary_relation/converse} The \term{converse relation} of \( R \) is
    \begin{equation*}
      R^{-1} \coloneqq \set{ (y, x) \given (x, y) \in R }.
    \end{equation*}

    \thmitem{def:binary_relation/restriction} If \( A = B \), the \term{restriction} of \( R \) to \( X \subseteq A \) is the binary relation \( (R\restr_{X}, X, X) \) is
    \begin{equation*}
      R\restr_{X} \coloneqq R \cap (X \times X) = \set{ (x, y) \in R \given x \in X \T{and} y \in X }.
    \end{equation*}

    We say that \( R \) is an \term{extension} of \( R\restr_{X} \).

    \thmitem{def:binary_relation/diagonal} A special relation is the \term{diagonal relation} on a set \( A \):
    \begin{equation*}
      \increment_A \coloneqq \set{ (x, x) \given x \in A }.
    \end{equation*}

    \thmitem{def:binary_relation/composition} Given two binary relations \( R \subseteq A \times B \) and \( T \subseteq B \times C \), we define their composition as
    \begin{equation*}
      T \bincirc R \coloneqq \set*{ (x, z) \in A \times C \given* \qexists {y \in B} \parens[\Big]{ (x, y) \in R \T{and} (y, z) \in T } }.
    \end{equation*}
  \end{thmenum}

  Whenever \( A = B \) and \( R \) is simply a binary relation over \( A \), the following are commonly used conditions that are often as axioms to some theory:
  \begin{thmenum}[resume=def:binary_relation]
    \thmitem{def:binary_relation/reflexive} \( R \) is \term{reflexive} if \( \Delta_A \subseteq R \), i.e. if every element of \( A \) is related with itself.

    The following formula is used as an axiom for \hyperref[def:partially_ordered_set/nonstrict]{nonstrict partial orders} and \hyperref[def:entourage]{entourages}:
    \begin{equation}\label{eq:def:binary_relation/reflexive}
      \qforall \xi (\xi \mathbin{R} \xi).
    \end{equation}

    Note that we use \fullref{rem:first_order_formula_conventions/infix} notation in the latter case. Using either infix or prefix notation is actually a necessity since we do not actually have a concept of an ordered tuple in general (not set-based) first-order theories --- see \fullref{rem:first_order_formula_conventions/infix}.

    \thmitem{def:binary_relation/irreflexive} \( R \) is \term{irreflexive} if \( \Delta_A \cap R = \varnothing \), i.e. if no element of \( A \) is related with itself.

    The following formula is used as an axiom for \hyperref[def:partially_ordered_set/strict]{strict partial orders}:
    \begin{equation}\label{eq:def:binary_relation/irreflexive}
      \neg \qexists \xi (\xi \mathbin{R} \xi).
    \end{equation}

    \thmitem{def:binary_relation/symmetric} \( R \) is \term{symmetric} if \( R = R^{-1} \).

    The following formula is used as an axiom for \hyperref[def:equivalence_relation]{equivalence relations}, \hyperref[def:undirected_multigraph]{undirected graphs} and \hyperref[def:entourage]{entourages}:
    \begin{equation}\label{eq:def:binary_relation/symmetric}
      \xi \mathbin{R} \eta \rightarrow \eta \mathbin{R} \xi.
    \end{equation}

    \thmitem{def:binary_relation/antisymmetric} \( R \) is \term{antisymmetric} if \( R \cap R^{-1} = \Delta_X \).

    The following formula is used as an axiom for \hyperref[def:partially_ordered_set]{partial orders}:
    \begin{equation}\label{eq:def:binary_relation/antisymmetric}
      (\xi \mathbin{R} \eta \wedge \eta \mathbin{R} \xi) \rightarrow \xi \doteq \eta.
    \end{equation}

    \thmitem{def:binary_relation/transitive} \( R \) is \term{transitive} if \( R = R \bincirc R \).

    The following formula is used as an axiom for \hyperref[def:partially_ordered_set]{preorders}:
    \begin{equation}\label{eq:def:binary_relation/transitive}
      (\xi \mathbin{R} \eta \wedge \eta \mathbin{R} \zeta) \rightarrow \xi \mathbin{R} \zeta.
    \end{equation}

    \thmitem{def:binary_relation/total} \( R \) is \term{total} if any two member of \( A \) are related.

    The following formula is used as an axiom for \hyperref[def:totally_ordered_set]{nonstrict total orders}:
    \begin{equation}\label{eq:def:binary_relation/total}
      \qforall \xi \qforall \eta (\xi \mathbin{R} \eta \vee \eta \mathbin{R} \xi).
    \end{equation}

    Total relations are also called \term{connected} because of \fullref{thm:order_category_isomorphism/totally_ordered}.

    \thmitem{def:binary_relation/trichotomic} \( R \) is \term{trichotomic} if every two elements of \( A \) are either related or equal.

    The following formula is used as an axiom for \hyperref[def:totally_ordered_set]{strict total orders}:
    \begin{equation}\label{eq:def:binary_relation/trichotomic}
      \qforall \xi \qforall \eta (\xi \mathbin{R} \eta \vee \eta \mathbin{R} \xi \vee \eta = \xi).
    \end{equation}
  \end{thmenum}
\end{definition}

\begin{example}\label{ex:def:binary_relation}
  \hyperref[def:binary_relation]{Binary relations} are used in vastly different contexts:
  \begin{itemize}
    \item \hyperref[def:function]{Functions} are special binary relations.
    \item \hyperref[sec:order_theory]{Orders} are also special binary relations.
    \item \hyperref[def:quiver/simple]{Directed graphs} are commonly defined as binary relations.
    \item \hyperref[def:entourage]{Entourages} are binary relations in \hyperref[def:uniform_space]{uniform spaces}.
    \item Relations are equivalent to Boolean-valued functions as shown in \fullref{rem:boolean_valued_functions_and_predicates}, and are often used for defining semantics of predicate symbols in \hyperref[subsec:first_order_logic]{first-order logic}.
  \end{itemize}
\end{example}

\begin{definition}\label{def:equivalence_relation}
  A binary relation on the set \( A \) that is \hyperref[def:binary_relation/reflexive]{reflexive}, \hyperref[def:binary_relation/symmetric]{symmetric} and \hyperref[def:binary_relation/transitive]{transitive} is called an \term{equivalence relation}. In other words, an equivalence relation is a symmetric \hyperref[def:preordered_set]{preorder}.

  We often denote equivalence relations via the symbol \( \sim \).

  \begin{thmenum}
    \thmitem{def:equivalence_relation/coset} The \term{equivalence class} of \( x \in A \), also called its \term{coset}, is the set
    \begin{equation*}
      [x] \coloneqq \set{ y \in A \given x \sim y }
    \end{equation*}
    of all elements of \( A \) that are related to \( x \).

    \thmitem{def:equivalence_relation/quotient} The \term{quotient set} of \( A \) by \( \sim \) is the set
    \begin{equation*}
      A / {\sim} \ \coloneqq \set{ [x] \given x \in A }.
    \end{equation*}

     If we have an easy way to choose a representative from each coset, then \( A / {\sim} \) may be regarded as a subset of \( A \). In general, this is not a subset relation by only an \hyperref[def:first_order_homomorphism_invertibility/embedding]{embedding}.

    \thmitem{def:equivalence_relation/projection} Using forward references to \fullref{subsec:functions}, we define the \term{canonical projection} as the function
    \begin{equation*}
      \begin{aligned}
        &\pi: A \to A / {\sim}  \\
        &\pi(x) \coloneqq [x].
      \end{aligned}
    \end{equation*}

    If we have a fixed \hyperref[def:choice_function]{choice function} \( c: A / {\sim} \to A \), we also define the canonical embedding
    \begin{equation*}
      \begin{aligned}
        &\iota: A / {\sim} \to A \\
        &\iota([x]) \coloneqq c(x).
      \end{aligned}
    \end{equation*}

    We sometimes have an obvious choice function, for example in \fullref{thm:representatives_in_univariate_polynomial_quotient_set}. In this case, the canonical projection may be regarded as a function from \( A \) to the subset \( c(A / {\sim}) \) of \( A \). Otherwise, the function \( \pi \) can be regarded as a \hyperref[def:multi_valued_function]{multi-valued function} from \( A \) to \( A \).
  \end{thmenum}
\end{definition}

\begin{remark}\label{rem:congruence_modulo_relation}
  If \( x \sim y \) for some \hyperref[def:equivalence_relation]{equivalence relation}, we say that they are \term{congruent modulo} \( \sim \). This concept specializes to congruence modulo normal subgroups defined in \fullref{def:congruence_modulo_normal_subgroup}.
\end{remark}

\begin{proposition}\label{thm:equality_is_smallest_equivalence_relation}
  The equality \hyperref[def:binary_relation]{relation} \( = \) is the intersection of all equivalence relations.
\end{proposition}
\begin{proof}
  It is equivalent to the \hyperref[def:binary_relation/diagonal]{diagonal relation} \( \Delta_X \). It is the smallest reflexive (resp. symmetric and transitive) relation on \( A \), i.e. the intersection of all reflexive (resp. symmetric and transitive) relations.
\end{proof}

\begin{definition}\label{def:set_partition}
  Let \( A \) be a set. A \term{cover} of \( A \) is a \hyperref[rem:family_of_sets]{family} \( \mscrA \subseteq \pow(A) \) of nonempty subsets of \( A \) such that \( A = \bigcup \mscrA \). We sometimes use the term more loosely and say that an arbitrary family of sets \( \mscrA \) is a cover of \( A \) if \( A \subseteq \bigcup \mscrA \). The two definitions are identical if we intersect each set in \( \mscrA \) with \( A \) and exclude the empty sets.

  A \term{partition} of \( A \) is a pairwise \hyperref[def:subset]{disjoint} cover. In other words, the cover \( \mscrA \) is a partition if and only if each element of \( A \) belong to exactly one set in \( \mscrA \).
\end{definition}

\begin{proposition}\label{thm:equivalence_partition}
  Fix a set \( A \). Let \( {\sim} \) be a binary relation of \( A \). The following are equivalent:
  \begin{thmenum}
    \thmitem{thm:equivalence_partition/equivalence} \( {\sim} \) is an \hyperref[def:equivalence_relation]{equivalence relation}.

    \thmitem{thm:equivalence_partition/partition} There exists a \hyperref[def:set_partition]{partition} \( \mscrA \) of \( A \) such that \( x \sim y \) if and only if they belong to the same set in the partition \( \mscrA \).
  \end{thmenum}
\end{proposition}
\begin{proof}
  \ImplicationSubProof{thm:equivalence_partition/equivalence}{thm:equivalence_partition/partition} Let \( {\sim} \) be an equivalence relation on \( A \). The quotient set \( A / {\sim} \) is a partition. Indeed:
  \begin{itemize}
    \item Every element \( x \in A \) belongs exactly one equivalence class \( [x] \) by definition.

    \item The equivalence classes are disjoint. Indeed, assume the contrary. Then there exist \( x \) and \( y \) such that \( [x] \cap [y] \neq \varnothing \) and yet \( x \not\sim y \).

    Let \( z \in [x] \cap [y] \). Then \( z \sim x \) and \( z \sim y \), thus from transitivity of \( {\sim} \) we have \( x \sim z \sim y \) and hence \( x \sim y \), which contradicts our assumption that \( x \not\sim y \).

    Hence, either \( [x] = [x] \) or \( [x] \cap [y] = \varnothing \). That is, different equivalence classes are disjoint.
  \end{itemize}

  \ImplicationSubProof{thm:equivalence_partition/partition}{thm:equivalence_partition/equivalence} Let \( \mscrA \) be a partition of \( A \) such that \( x \sim y \) if and only if they both belong to the same set in \( \mscrA \).

  Given \( x \in A \), denote by \( A_x \) the set in \( \mscrA \) which contains \( x \). The family \( \seq{ A_x }_{x \in A} \) is well-defined since \( \mscrA \) is a partition, which means that \( x \) belongs to exactly one set in \( \mscrA \).

  \SubProofOf*[def:binary_relation/reflexive]{reflexivity} Clearly \( A_x = A_x \), hence \( x \sim x \).

  \SubProofOf*[def:binary_relation/symmetric]{symmetry} If \( x \sim y \), then \( A_x = A_y \), which implies \( A_y = A_x \) and thus \( x \sim y \).

  \SubProofOf*[def:binary_relation/transitive]{transitivity} If \( x \sim y \) and \( y \in z \), then \( A_x = A_y = A_z \) and thus \( x \sim z \).
\end{proof}

\begin{definition}\label{def:relation_closures}
  Let \( R \subseteq A^2 \) be a binary relation on the set \( A \). We define several \hyperref[def:closure_operator]{closure operators}:
  \begin{thmenum}
    \thmitem{def:relation_closures/reflexive} The \term{reflexive closure} of \( R \) is
    \begin{equation*}
      \cl^R(R) \coloneqq R \cup \Delta_X.
    \end{equation*}

    \thmitem{def:relation_closures/symmetric} The \term{symmetric closure} of \( R \) is
    \begin{equation*}
      \cl^S(R) \coloneqq R \cup R^{-1}.
    \end{equation*}

    \thmitem{def:relation_closures/transitive} The \term{transitive closure} \( \cl^T(R) \) of \( R \) is
    \begin{equation*}
      \cl^T(R) \coloneqq \bigcup \set{ R^k \given k = 1, 2, \ldots },
    \end{equation*}
    where \( R^k \) is iterated \hyperref[def:binary_relation/composition]{composition} of \( R \).

    Note that this is very different from the transitive closure of a set defined in \fullref{def:transitive_closure_of_a_set}.

    A \term{transitive reduction} of \( R \) is a \hyperref[def:partially_ordered_set_extremal_points/maximal_and_minimal_element]{minimal} relation \( Q \subseteq R \) such that \( \cl^T(Q) = \cl^T(R) \). If there exists a smallest such relation, it is the unique transitive reduction, and we denote it by \( \red^T(R) \).
  \end{thmenum}
\end{definition}

\begin{proposition}\label{thm:def:relation_closures}
  \hyperref[def:relation_closures]{Binary relation closures} have the following basic properties:
  \begin{thmenum}
    \thmitem{thm:def:relation_closures/reflexive_relation} The symmetric and transitive closures of a reflexive relation are symmetric.
    \thmitem{thm:def:relation_closures/symmetric_relation} The reflexive and transitive closures of a symmetric relation are symmetric.
    \thmitem{thm:def:relation_closures/transitive_relation} The reflexive closure of a transitive relation is transitive. The symmetric closure of a transitive relation may not be transitive --- see \fullref{ex:thm:def:relation_closures/symmetric_and_transitive}.

    \thmitem{thm:def:relation_closures/reflexive_and_symmetric} The reflexive and symmetric closures commute:
    \begin{equation}\label{eq:thm:def:relation_closures/reflexive_and_symmetric}
      \cl^S \cl^R(R) = \cl^R \cl^S(R).
    \end{equation}

    \thmitem{thm:def:relation_closures/reflexive_and_transitive} The transitive and reflexive closures commute:
    \begin{equation}\label{eq:thm:def:relation_closures/reflexive_and_transitive}
      \cl^R \cl^T(R) = \cl^T \cl^R(R).
    \end{equation}

    \thmitem{thm:def:relation_closures/symmetric_and_transitive} For the transitive and symmetric closures of \( R \) we have
    \begin{equation}\label{eq:thm:def:relation_closures/symmetric_and_transitive}
      \cl^S \cl^T(R) \subseteq \cl^T \cl^S(R).
    \end{equation}

    The converse holds if \( R \) is symmetric but not in general --- see \fullref{ex:thm:def:relation_closures/symmetric_and_transitive}.
  \end{thmenum}
\end{proposition}
\begin{proof}
  \SubProofOf{thm:def:relation_closures/reflexive_relation} Trivial.
  \SubProofOf{thm:def:relation_closures/symmetric_relation} Trivial.
  \SubProofOf{thm:def:relation_closures/transitive_relation} Trivial.
  \SubProofOf{thm:def:relation_closures/reflexive_and_symmetric} Trivial.
  \SubProofOf{thm:def:relation_closures/reflexive_and_transitive} The reflexive closure only adds pairs of the form \( (x, x) \). Thus, if \( (x, y) \in \cl^T(\cl^R(R)) \) for \( x \neq y \), then \( (x, y) \in \cl^T(R) \subseteq \cl^R(\cl^T(R)) \).

  Conversely, if \( (x, y) \in \cl^R(\cl^T(R)) \) for \( x \neq y \), then \( (x, y) \in \cl^T(R) \subseteq \cl^R(\cl^T(R)) \).

  \SubProofOf{thm:def:relation_closures/symmetric_and_transitive} If \( (x, y) \in \cl^S(\cl^T(R)) \), then we have the following possibilities:
  \begin{itemize}
    \item If \( (x, y) \in R \), obviously \( (x, y) \in \cl^T(\cl^S(R)) \).
    \item If \( (x, y) \in \cl^T(R) \setminus R \), then there exists some natural number \( k > 1 \) such that \( (x, y) \in R^k \).

    Since \( R^k \subseteq [\cl^S(R)]^k \), as can be shown by induction, we have \( (x, y) \in [\cl^S(R)]^k \). We thus conclude that \( (x, y) \in \cl^T(\cl^S(R)) \).

    \item Finally, if \( (x, y) \not\in \cl^T(R) \), then \( (y, x) \in \cl^T(R) \). As in the previous step, we can show that \( (y, x) \in \cl^T(\cl^S(R))) \). The latter set is symmetric, hence \( (x, y) \in \cl^T(\cl^S(R)) \).
  \end{itemize}

  Since \( (x, y) \) was arbitrary, we conclude that \eqref{eq:thm:def:relation_closures/symmetric_and_transitive} holds.

  Furthermore, if \( R \) is symmetric, then
  \begin{equation*}
    \cl^T \cl^S(R)
    =
    \cl^T(R)
    \reloset {\ref{thm:def:relation_closures/reflexive_relation}} =
    \cl^S \cl^T(R).
  \end{equation*}
\end{proof}

\begin{example}\label{ex:thm:def:relation_closures/symmetric_and_transitive}
  We will show that symmetric and transitive closures of relations do not commute. This is also a consequence of the difference between \hyperref[def:quiver_connectedness/weak]{weak} and \hyperref[def:quiver_connectedness/strong]{strong} connectedness of quivers.

  Consider the set \( A = \set{ a, b, c } \) and the relation \( R = \set{ (a, b), (c, b) } \).

  It should be noted that \( R \) is \hyperref[def:binary_relation/transitive]{transitive}. Thus,
  \begin{equation*}
    \cl^S(\cl^T(R)) = \cl^S(R) = R \cup \set{ (b, a), (b, c) }.
  \end{equation*}

  The latter set is not transitive because \( (a, b) \) and \( (b, a) \) both belong to \( \cl^S(R) \) and neither \( (a, a) \) nor \( (b, b) \) do not.

  This shows that the converse of \eqref{eq:thm:def:relation_closures/symmetric_and_transitive} does not hold in general.
\end{example}

\begin{proposition}\label{thm:equivalence_closure}
  The \hyperref[def:relation_closures/reflexive]{reflexive}, \hyperref[def:relation_closures/symmetric]{symmetric} and \hyperref[def:relation_closures/transitive]{transitive} closure \( \cl^T \cl^S \cl^R (R) \) of any relation \( R \) is an \hyperref[def:equivalence_relation]{equivalence relation}.

  This holds for any permutation of the closures as long as \( \cl^T \) is applied \hi{after} \( \cl^S \). This latter restriction is due to \fullref{thm:def:relation_closures/symmetric_and_transitive}.
\end{proposition}
\begin{proof}
  Let \( R \subseteq A \times B \) be an arbitrary relation. By \fullref{thm:def:relation_closures/reflexive_relation}, \( \cl^S \cl^R (R) \) is reflexive. It is also symmetric as the symmetric closure of \( \cl^R(R) \).

  Then the transitive closure \( \cl^T \cl^S \cl^R (R) \) is also symmetric and reflexive by \fullref{thm:def:relation_closures/reflexive_relation} and \fullref{thm:def:relation_closures/symmetric_relation}.

  Therefore, \( \cl^T \cl^S \cl^R (R) \) is an equivalence relation.
\end{proof}