**Review 3**

**程$
% text size
% tiny scriptsize small normalsize large Large LARGE huge Huge
% color
% Aquamarine, black, blue, brown, cyan, darkgray, gray, green, lightgray, lime, magenta, olive, orange, pink, purple, red, teal, violet, white, yellow
\DeclareMathOperator*{\argmin}{argmin}
\DeclareMathOperator*{\argmax}{argmax}
\DeclareMathOperator*{\plim}{plim}
\newcommand{\ffrac}{\displaystyle \frac}
\newcommand{\d}[1]{\displaystyle{#1}}
\newcommand{\space}{\text{ }}
\newcommand{\bspace}{\;\;\;\;}
\newcommand{\bbspace}{\;\;\;\;\;\;\;\;}
\newcommand{\QQQ}{\boxed{?\:}}
\newcommand{\void}{\left.\right.}
\newcommand{\Tran}[1]{{#1}^{\mathrm{T}}}
\newcommand{\CB}[1]{\left\{ #1 \right\}}
\newcommand{\SB}[1]{\left[ #1 \right]}
\newcommand{\P}[1]{\left( #1 \right)}
\newcommand{\abs}[1]{\left| #1 \right|}
\newcommand{\norm}[1]{\left\| #1 \right\|}
\newcommand{\given}[1]{\left. #1 \right|}
\newcommand{\using}[1]{\stackrel{\mathrm{#1}}{=}}
\newcommand{\asim}{\overset{\text{a}}{\sim}}
\newcommand{\RR}{\mathbb{R}}
\newcommand{\EE}{\mathbb{E}}
\newcommand{\II}{\mathbb{I}}
\newcommand{\NN}{\mathbb{N}}
\newcommand{\ZZ}{\mathbb{Z}}
\newcommand{\QQ}{\mathbb{Q}}
\newcommand{\PP}{\mathbb{P}}
\newcommand{\AcA}{\mathcal{A}}
\newcommand{\FcF}{\mathcal{F}}
\newcommand{\AsA}{\mathscr{A}}
\newcommand{\FsF}{\mathscr{F}}
\newcommand{\dd}{\mathrm{d}}
\newcommand{\I}[1]{\mathrm{I}\left( #1 \right)}
\newcommand{\N}[1]{\mathcal{N}\left( #1 \right)}
\newcommand{\Exp}[1]{\mathrm{E}\left[ #1 \right]}
\newcommand{\Var}[1]{\mathrm{Var}\left[ #1 \right]}
\newcommand{\Avar}[1]{\mathrm{Avar}\left[ #1 \right]}
\newcommand{\Cov}[1]{\mathrm{Cov}\left( #1 \right)}
\newcommand{\Corr}[1]{\mathrm{Corr}\left( #1 \right)}
\newcommand{\ExpH}{\mathrm{E}}
\newcommand{\VarH}{\mathrm{Var}}
\newcommand{\AVarH}{\mathrm{Avar}}
\newcommand{\CovH}{\mathrm{Cov}}
\newcommand{\CorrH}{\mathrm{Corr}}
\newcommand{\ow}{\text{otherwise}}
\newcommand{\wp}{\text{with probability }}
\newcommand{\FSD}{\text{FSD}}
\newcommand{\SSD}{\text{SSD}}$远星$^\dagger$**

## Options

The bounds:

$$\begin{align}
&\max\P{S_0-Ke^{-rT},0}\leq c\leq C \leq S_0\\
&\max\P{Ke^{-rT}-S_0,0}\leq p\leq P \leq K
\end{align}$$

The put-call parity, $c+Ke^{-rT} = p+S_0$; and if there's dividend paid, it becomes

$$c+Ke^{-rT} = p+S_0e^{-qT}\Rightarrow c\P{t,S_t} +Ke^{-r\P{T-t}} = p\P{t,S_t}+S_te^{-q\P{T-t}}$$

If American options are involved, we have

$$c+Ke^{-rT}-S_0 = p \leq P\leq c+K-S_0 = C+K-S_0$$

All preceding can be proved by constructing two portfolios with certain payoffs at time $T$.

## Binomial Tree

**Arbitrage**: NOTHING at time $0$, all possible payoffs at $T$ is non-negative, the expectation of the payoff under the actual probability measure, is positive; or mathematically

- $V_0^h = 0\\[0.5em]$
- $V_T^h\P\omega \geq0\\[0.5em]$
- $\Exp{V_T^h\P\omega}>0$

**Dynamic replication**: or **No arbitrage**: Let $c$ denote a European call option price at time $0$ (or $V\P{0,S_0}$ if necessary); and $c_u$, $c_d$ the call option price at time $T$. Then we construct the portfolio $h$ that replicates the *long position* of the call option (derivative), with $x$ units of bonds and $y$ shares of stocks. Denote $c_u = \P{S_T-K}^+$ as the payoff from the call (derivative) if the price moves up

$$\begin{cases}
x \cdot e^{rT} + y\cdot uS_0 = c_u\\
x \cdot e^{rT} + y\cdot dS_0 = c_d
\end{cases} \Rightarrow \begin{cases}
x = \ffrac{u\cdot c_d - d\cdot c_u}{\P{u-d}e^{rT}}\\[0.6em]
y = \ffrac{c_u - c_d}{\P{u-d}S_0}
\end{cases}$$

then $c = x+yS_0 = e^{-rT}\P{\ffrac{e^{rT}-d}{u-d}c_u + \ffrac{u - e^{rT}}{u-d}c_d}$

***

**Riskless hedging principle**: we now need a portfolio $h$ with a *short* position of $\Delta$ shares of stocks and $1$ units of the call option (derivative), so that the *terminal payoff* of $h$ is a constant (riskless).

$$c_u - \Delta \cdot u\cdot S_0 = c_d - \Delta \cdot d\cdot S_0 = \P{c-\Delta \cdot S_0}e^{rT}$$

***

**Risk-neutral valuation**: As we can assume the existence of the risk-neutral probability value $q_u$ and $q_d$, we first find them.

$$S_0 = e^{-rT}\ExpH^\QQ\SB{S_T} = e^{-rT}\P{q_u\cdot uS_0 + \P{1-q_u}dS_0},\bspace q_d = 1-q_u$$

After obtaining the two we can calculate the price of the call option (derivative)

$$c = e^{-rT} \P{q_u\cdot c_u + q_d \cdot c_d}\equiv \ExpH^\QQ\SB{c\P{T,S_T}}$$

**Relationship**

- Let $\Delta = y$ and we'll see that the first two methods are equivalent
- Write $\ffrac{e^{rT}-d}{u-d} = q_u$ and we'll see the first and the last methods are equivalent

***

**Multiperiod binomial model**

Let $\P{t,k}$ denote the node at time $t$ with $k$ steps up and $\tau$ the time unit. Totally $T$ units of time.

1. step by step: $u = \ffrac{S_t}{S_{t-1}}$, then $c_t\P{k}=e^{-r\tau}\ExpH^\QQ\SB{q \cdot c_{t+1}\P{k+1}+\P{1-q} \cdot c_{t+1}\P{k}}$
2. path probability:

$$\begin{align}
c &= e^{-rT}\ExpH^\QQ\SB{c\P{T,S_T}}\\
&= e^{-rT}\SB{\binom{T}{T}q^T c_T\P{T} + \binom{T}{T-1}q^{T-1}\P{1-q}c_T\P{T-1} + \binom{T}{T-2}q^{T-2}\P{1-q}^2c_T\P{T-2}+\cdots}
\end{align}$$

and a little bit modification for European put option. As for American options, be careful.

***

**Replication**: or **Hedging**. For *Multiperiod binomial model*, we need to dynamically replicate at each time period. Here's a $Proposition$: Consider a claim $U = \Phi\P{S_T}$. Then this claim can be replicated using a self-financing portfolio. Still $V_t\P{k}$ denotes the option price at the node $\P{t,k}$, where $k$ is the number of up-moves that have occurred, then $V_t\P{k}$ can be computed recursively by the scheme:

$$\begin{align}
V_T\P{k} &= \phi\P{S_T} \\
V_t\P{k} &= e^{-r \times 1}\SB{q V_{t+1}\P{t+1} + \P{1-q} V_{t+1}\P{k}}
\end{align}$$

and to hedge this, we construct the portfolio as

$$y_t\P{k} = \ffrac{V_{t+1}\P{k+1} - V_{t+1}\P{k}} {\P{u-d}S_t},\bspace x_t\P{k} = V_t\P k - y_t\P k \cdot S_t = V_{t}\P{k}-\ffrac{V_{t+1}\P{k+1} - V_{t+1}\P{k}} {u-d}$$

here $\P{x_t\P{k},y_t\P{k}}$ for the amount of stock and bond held in the portfolio at node $(t, k)$ over the time interval $[t, t + 1)$

## Stochastic Process

**Martingale**

1. $\forall\, t$, $\ExpH^\PP\SB{\abs{X_t}}<\infty$. Try using the fundamental inequlity: $\Exp{\abs{W_t}}\leq\Exp{\ffrac{1+W_t^2}{2}} = \ffrac{1+t}{2}$
2. $\forall\, s\leq t$, $\ExpH^\PP\SB{X_{t}\mid\FcF_s}\equiv \ExpH^\QQ_s\SB{X_t} = X_s$
3. $\CB{X_t}_{t\geq 0}$ is a stochastic process adapted to $\CB{\FcF_t}_{t\geq 0}$

***

**Brownian motion**

1. $W_t-W_s\sim\N{0,t-s}$, $\P{t-s}^{-1}\P{W_t-W_s}^2\sim\chi^2\P1$, $\Exp{\chi^2\P k} = k$ and $\Var{\chi^2\P k} = 2k$
2. $\Exp{\exp\CB{\mu t + \sigma W_t}} = \exp\CB{\mu t + \ffrac{1}{2}\sigma^2 \cdot t}$
3. $\ExpH_s\SB{W_t} = W_s + \ExpH_s\SB{W_{t-s}} = W_s + \Exp{W_{t-s}} = W_s$

***

**quadratic variation**

$$\SB{W}_t = \lim_{\norm{\Pi}\to0}Q_\Pi = \lim_{\norm{\Pi}\to0}\sum_{j=0}^{n-1} \P{W_{t_{\void_{j+1}}} - W_{t_{\void_j}}}^2 $$

1. $\SB{W}_t = t$, by verifying the expectation is $t$ and variance is $0$
2. Write $\d{t = \lim_{\norm{\Pi}\to0}Q_\Pi = \lim_{\norm{\Pi}\to0}\sum_{j=0}^{n-1} \P{W_{t_{\void_{j+1}}} - W_{t_{\void_j}}}^2 = \int_0^t \;\P{\dd W_s}^2}$, then $\P{\dd W_t }^2 = \dd t$
3. Similarly, $\d{0 = \lim_{\norm{\Pi}\to0} \sum_{j=0}^{n-1} \P{t_j - t_{j-1}}^2 = \int_0^t \;\P{\dd s}^2}$ and thus $\dd t \dd t = 0$
4. Using Cauchy-Schwartz inequality, $\dd W_t\dd t = 0$

***

**Itô**

$$I\P t = \int_0^t h\P{s,W_s}\;\dd W_s,\bspace I_n\P t =\sum_{j=0}^{n-1} h\P{t_j,W_{t_{\void_j}}}\P{W_{t_{\void_{j+1}}} - W_{t_{\void_j}}}$$

1. $\ExpH_s\SB{I\P t-I\P s} = \ExpH_s\SB{\d{\lim_{n\to\infty} \sum_{j=0}^{n-1} h\P{t_j,W_{t_{\void_j}}}\P{W_{t_{\void_{j+1}}} - W_{t_{\void_j}}}}} = 0$ and thus $I\P t$ is a martingale
2. Itô isometry: $\Exp{I\P t^2} = \d{\int_0^t \Exp{h^2\P{s,W_s}}\;\dd s}$
3. **Itô process**: $\dd X_t = \mu\P{t,X_t}\dd t + \sigma\P{t,X_t}\dd W_t$
4. **Itô's formula**: for $Y_t = f\P{t,X_t}$ we have
$$\begin{align}\dd f &= \ffrac{\partial f}{\partial t}\dd t + \ffrac{\partial f}{\partial X_t}\dd X_t + \ffrac{1}{2}\ffrac{\partial^2 f}{\partial X_t^2}\P{\dd X_t}^2\\
&= \P{\ffrac{\partial f}{\partial t} + \mu\P{t,X_t}\ffrac{\partial f}{\partial X_t} + \ffrac{1}{2} \sigma^2 \P{t,X_t} \ffrac{\partial^2 f}{\partial X_t^2}}\dd t + \sigma\P{t,X_t}\ffrac{\partial f}{\partial X_t}\dd W_t
\end{align}$$

**e.g.**

The basic reference model for stock price in continuous time is

$$\ffrac{\dd S_t}{S_t} = \mu \dd t + \sigma \dd W_t \P{\neq \dd \log S_t}$$

First write $\dd S_t = \mu \cdot S_t \dd t + \sigma \cdot S_t \dd W_t \equiv \mu_t \dd t + \sigma_t \dd W_t$. Then using the Itô's formula we have

$$\begin{align}
\dd \log S_t &= \dd f\P{S_t} \\
&= \P{0 + \P{\mu S_t}\ffrac{1}{S_t} + \ffrac{1}{2}\P{\sigma S_t}^2 \P{-\ffrac{1}{S_t^2}}}\dd t + \P{\sigma S_t} \ffrac{1}{S_t} \dd W_t \\
&= \P{\mu - \ffrac{1}{2}\sigma^2}\dd t + \sigma \dd W_t
\end{align}$$
***
Here we can also apply the first form of Itô's formula, which yields

$$\begin{align}
\dd \log S_t &= \ffrac{1}{S_t} \dd S_t - \ffrac{1}{2S_t^2} \P{\dd S_t}^2 \\
&= \mu \dd t + \sigma \dd W_t - \ffrac{1}{2}\P{\ffrac{\dd S_t}{S_t}}^2\\
&= \mu \dd t + \sigma \dd W_t - \ffrac{1}{2}\sigma^2\dd t = \P{\mu - \ffrac{1}{2}\sigma^2}\dd t + \sigma \dd W_t\\
\end{align}$$

take the intergral, we have

$$\log\ffrac{S_t}{S_0} = \P{\mu - \ffrac{\sigma^2}{2}}t + \sigma W_t$$

$$\begin{align}
S_t &= S_0 \exp\CB{\P{\mu - \ffrac{\sigma^2}{2}}t + \sigma W_t}\\
&= S_s \exp\CB{\P{\mu - \ffrac{\sigma^2}{2}}\P{t-s} + \sigma \P{W_t-W_s}}
\end{align}$$

***

**Feynman-Kac**

Assume the function $f$ solves the boundary value problem:

$$\begin{cases}
-rf\P{t,x} +\ffrac{\partial f} {\partial t}\P{t,x} + \mu\P{t,x} \ffrac{\partial f} {\partial x}\P{t,x} + \ffrac{1}{2}\sigma^2 \P{t,x} \ffrac{\partial^2 f} {\partial x^2}\P{t,x} = 0\\[0.6em]
f\P{T,x} = \Phi\P{x}
\end{cases}$$

And define $\CB{X_t}_{0\leq t\leq T}$ to be the solution of the ***SDE***: $\dd{X_t} = \mu\P{t,X_t} \dd{t}+\sigma\P{t,X_t} \dd{W_t}$. Then under some regularity condition, we have

$$f\P{t,x} = \Exp{e^{-r\P{T-t}}f\P{T,X_T}\mid X_t = x} = \Exp{e^{-r\P{T-t}}\Phi\P{X_T}\mid X_t = x}$$

The two equation, are equivalent.

**e.g.**

Use the Feynman-Kac stochastic representation formula to solve

$$\ffrac{\partial F\P{t,x}}{\partial t} + \ffrac{1}{2}\sigma^2 \ffrac{\partial^2 F\P{t,x}}{\partial x^2} = 0$$

subject to the *terminal value condition* $F\P{T,x} = e^x$

>By F-K formula, we have the solution of the corresponding stochastic differential equation
>
>$$\dd X_t = \mu_t\dd t + \sigma_t \dd W_t$$
>
>where $\mu_t = 0$, $r = 0$, and $\sigma_t = \sigma$. So that the solution is, in integral form,
>
>$$X_t = \sigma W_t + X_0$$
>
>So that the solution $F\P{t,x}$
>
>$$\begin{align}
F\P{t,x} &= \Exp{e^{-r\P{T-t}} e^{X_T} \mid X_t = x} \\
&= \Exp{e^{-0\P{T-t}} e^{\sigma W_T} \mid \sigma W_t = x} \\
&= \Exp{e^{\sigma\P{ W_T - W_t} +x} \mid \sigma W_t = x}\\
&= e^{\sigma x}\Exp{e^{\sigma\P{ W_T - W_t}}}\\
&= \exp\CB{ x + \ffrac{\sigma^2}{2}\P{T-t}}
\end{align}$$

***

**Girsanov's Theorem**

Suppose that $\CB{W_t}_{t \geq0}$ is a $\PP$-Brownian motion with the natural filtration $\CB{\FcF_t}_{t\geq 0}$ and $\CB{\theta_t}_{t\geq0}$ is an $\CB{\FcF_t}_{t\geq0}$-adapted process such that

$$\Exp{\exp\CB{\ffrac{1}{2}\int_{0}^{T} \theta_t^2 \;\dd{t}}}<\infty$$

Then we define $L_t = \exp\CB{\d{\mathbf-\int_{0}^{t} \theta_s \;\dd{W_s} \mathbf-\ffrac{1}{2}\int_{0}^{t}\theta_s^2 \;\dd{s}}}$ and let $\tilde P$ be the probability measure defined by $\tilde P\SB{A} = \d{\int_A L_t\P{\omega} \PP\P{\dd{\omega}}}$. Then under the new probability measure $\tilde P$, the process defined by $\widetilde W_t = W_t \mathbf +\d{\int_{0}^{t}\theta_s\;\dd{s}}$ is a standard Brownian motion.

1. $\tilde P\SB{A} = \d{\int_A L_t\P{\omega} \PP\P{\dd{\omega}}} \iff L_t = \given{\ffrac{\dd \tilde P}{\dd \PP}}_{\FcF_t}$, and $L_t$ is a martingale
2. $\ExpH^{\tilde P} \SB{\Phi\P{t}\mid \FcF_s} = \ExpH^\PP\SB{\Phi\P{t}\ffrac{L_t}{L_s}\mid\FcF_s}$, or shortly $\ExpH^\tilde P_s\SB{\Phi\P t} = \ExpH^\PP_s\SB{\Phi\P t\ffrac{L_t}{L_s}}$
3. $\ExpH^\tilde P_s\SB{\Phi\P t\ffrac{L_s}{L_t}} = \ExpH^\PP_s\SB{\Phi\P t}$

**e.g.**

The basic reference model for stock price in continuous time is

$$\dd S_t = \mu S_t \dd t + \sigma S_t \dd W_t$$

where $\CB{W_t}_{t\geq 0}$ is a $\PP$-Brownian motion. Find a measure under which the discounted stock price $\CB{\tilde S_t}_{t\geq 0}$ is a martingale. Here, $\tilde S_t = e^{-rt}S_t$.

>By Itô's formula, under $\PP$ measure, we have
>
>$$\dd \tilde S_t =\tilde S_t \P{-r + \mu} \dd t + \sigma \tilde S_t \dd W_t$$
>
>With the intention to *zero* the **drift** $\mu_t$, we let $\widetilde W_t = W_t + \ffrac{\mu - r}{\sigma}t$:
>
>$$\dd \tilde S_t =  \sigma \tilde S_t \dd \widetilde W_t$$
>
>According to Girsanov’s Theorem, we can define, by letting $\theta = \ffrac{\mu-r}{\sigma}$
>
>$$\newcommand{\QQ}{\mathbb{Q}}
\left.\ffrac{\dd\QQ}{\dd\PP}\right|_{\FcF_t} = \exp\CB{-\theta W_t - \ffrac{1}{2}\theta^2 t}$$
>
>Then $\CB{\widetilde W_t}_{t\geq0}$ is a $\QQ$-Brownian motion and the discounted stock price $\CB{\tilde S_t}_{t\geq 0 }$ is a $\QQ$-martingale.

$Remark$

>When $S_t$ is multiplied by $e^{rt}$, the resulting process can be remembered as a conclusion that
>
>$$\dd \tilde S_t =\tilde S_t \P{r + \mu} \dd t + \sigma \tilde S_t \dd W_t$$
>
>The **drift** is increased by $r$, while the **diffusion** (volatility) remains the same.
>
>And this can be used to prove $S_t$ is martingale, given the adaptivity and finite expecation of the absolute value.

***

## Black-Scholes-Merton model

$$\dd S_t = \mu S_t \dd t + \sigma S_t \dd W_t,\bspace S_t = S_0 \cdot \exp\CB{\P{\mu - \ffrac{\sigma^2}{2}}t +\sigma W_t}$$

- **Dynamic Replication**: or **No arbitrage**, first, the wealth process$\\[1em]$
$$\dd V_t = \ffrac{\pi_t}{S_t} \dd S_t + \ffrac{V_t - \pi_t}{B_t}\dd B_t = \SB{rV_t + \P{\mu-r} \pi_t}\dd t + \sigma\pi_t \dd W_t\\[0.5em]$$
then use **Itô formula** on $c\\[0.5em]$
$$\dd c = \P{\ffrac{\partial c}{\partial t} + \mu S_t \ffrac{\partial c}{\partial S_t} + \ffrac{1}{2}\sigma^2 S_t^2 \ffrac{\partial^2 c}{\partial S_t^2}}\dd t + \sigma S_t\ffrac{\partial c}{\partial S_t}\;\dd W_t\\[1em]$$
Comparing the $\dd W_t$ term, we have $\ffrac{\pi_t}{S_t} = \ffrac{\partial c}{\partial S_t}$ and comparing the $\dd t$ term we have$\\[1.3em]$
$$\ffrac{\partial c}{\partial t} + \mu S_t \ffrac{\partial c}{\partial S_t} + \ffrac{1}{2}\sigma^2 S_t^2 \ffrac{\partial^2 c}{\partial S_t^2} = rc + \P{\mu-r} \pi_t \Rightarrow \ffrac{\partial c}{\partial t} + r S_t \ffrac{\partial c}{\partial S_t} + \ffrac{1}{2}\sigma^2 S_t^2 \ffrac{\partial^2 c}{\partial S_t^2} - rc = 0\\[2em]$$

- **Riskless Hedging Principle**: $\Pi\P{t,S_t} = c\P{t,S_t}-\Delta S_t$. Using Itô,$\\[1em]$
$$\begin{align}
\dd\Pi\P{t,S_t}&=\P{\ffrac{\partial c}{\partial t} + \mu S_t \ffrac{\partial c}{\partial S_t} + \ffrac{1}{2}\sigma^2 S_t^2 \ffrac{\partial^2 c}{\partial S_t^2}}\dd t + \sigma S_t\ffrac{\partial c}{\partial S_t}\;\dd W_t - \Delta \P{\mu S_t\dd t + \sigma S_t \dd W_t}\\
&= \P{ \ffrac{\partial c}{\partial t} + \mu S_t\P{\ffrac{\partial c}{\partial S_t} - \Delta} + \ffrac{1}{2}\sigma^2 S_t^2 \ffrac{\partial^2 c}{\partial S_t^2}}\dd t + \sigma S_t\P{\ffrac{\partial c}{\partial S_t} - \Delta}\dd W_t
\end{align}\\[1.2em]$$
Then from the **riskless hedging principle**, we have $\dd\Pi\P{t,S_t} = r\cdot\Pi\P{t,S_t}\dd t$ and thus$\\[1em]$
$$\P{\ffrac{\partial c}{\partial t} + \mu S_t\P{\ffrac{\partial c}{\partial S_t} - \Delta} + \ffrac{1}{2}\sigma^2 S_t^2 \ffrac{\partial^2 c}{\partial S_t^2}}\dd t + \sigma S_t\P{\ffrac{\partial c}{\partial S_t} - \Delta}\dd W_t = r\cdot\Pi\P{t,S_t}\dd t = r\cdot\P{c-\Delta S_t}\dd t\\[1em]$$
Mathing the terms, we have
$$\ffrac{\partial c}{\partial t} + \ffrac{1}{2} \sigma^2 S_t^2 \ffrac{\partial^2 c}{\partial S_t^2} + rS_t \ffrac{\partial c}{\partial S_t} - rc = 0,\bspace \Delta = \ffrac{\partial c}{\partial S_t}\\[2em]$$

- **Risk-Neutral Valuation**: first find the risk-neutral probabilities where the discounted stock price $\CB{\tilde S_t}_{t\geq0}$ is a martingale, by **Gisanov's Thoerem**.
    - $\given{\ffrac{\dd\QQ}{\dd\PP}}_{\FcF_t} = \exp\CB{-\theta W_t - \ffrac{1}{2}\theta^2 t}$, where $\theta = \ffrac{\mu - r}{\sigma}$
    - Under $\QQ$ measure, $\dd S_t = rS_t\dd t + \sigma S_t \dd \widetilde W_t$, $\dd \tilde S_t = \sigma \tilde S_t \dd \widetilde W_t$
    - then the risk-neutral valuation formula$\\[1em]$
$$\begin{align}
c\P{t,S_t} &= \ExpH_t^\QQ\SB{e^{-r\P{T-t}}c\P{T,S_T}}\\
&= \ExpH_t^\QQ\SB{e^{-r\P{T-t}} %\max
\P{S_T - K}^+}\\
&= \ExpH^\QQ\SB{e^{-r\P{T-t}} %\max
\P{S_T - K}^+ \mid S_t}
\end{align}$$
    - then the **Feynman-Kac theorem**, $\\[1em]$
$$\ffrac{\partial c}{\partial t} + \ffrac{1}{2} \sigma^2 S_t^2 \ffrac{\partial^2 c}{\partial S_t^2} + rS_t \ffrac{\partial c}{\partial S_t} - rc = 0$$

**BS equation**:

- for European call option without dividend: $\ffrac{\partial c}{\partial t} + \ffrac{1}{2} \sigma^2 S_t^2 \ffrac{\partial^2 c}{\partial S_t^2} + rS_t \ffrac{\partial c}{\partial S_t} - rc = 0$

$$\begin{align}
c\P{t,S_t} &= S_t\Phi^{-1}\P{d_1} - Ke^{-r\P{T-t}} \Phi^{-1}\P{d_2}\equiv S_t\N{d_1} - Ke^{-r\P{T-t}} \N{d_2}\\
d_1 &= \ffrac{1}{\sigma\sqrt{T-t}} \P{\log\ffrac{S_t}{K} + \P{r\,\mathbf+\,\ffrac{1}{2}\sigma^2}\P{T-t}}\\
d_2 &= \ffrac{1}{\sigma\sqrt{T-t}} \P{\log\ffrac{S_t}{K} + \P{r\,\mathbf-\,\ffrac{1}{2}\sigma^2}\P{T-t}}= d_1 - \sigma \sqrt{T-t}
\end{align}$$

>We solve this using probabilistic method. Under $\QQ$,
>
>$$
c\P{t,S_t} = \ExpH^\QQ\SB{e^{-r\P{T-t}}} = e^{-r\P{T-t}}\P{\ExpH^\QQ_t\SB{S_T \cdot \mathbf{1}_{S_{\void_T}>K}}-K\cdot\ExpH^\QQ_t\SB{ \mathbf{1}_{S_{ \void_T} >K}}}
$$
>
>Note that $S_T = S_t \exp\CB{\P{r- \ffrac{1}{2}\sigma^2}\P{T-t} + \sigma\P{\widetilde W_T -\widetilde W_t}}$

>$$S_T\mid \FcF_t \sim S_t \exp\CB{\P{r- \ffrac{1}{2}\sigma^2}\P{T-t} + \sigma\sqrt{T-t}\cdot Z},\bspace Z\sim \N{0,1}$$
>
>$$\begin{align}
\ExpH^\QQ_t\SB{\mathbf{1}_{S_{\void_T}>K}} &= \QQ\P{S_t \exp\CB{\P{r- \ffrac{1}{2}\sigma^2}\P{T-t} + \sigma\sqrt{T-t}\cdot Z} >K}\\
&= \QQ\P{Z>\ffrac{1}{\sigma\sqrt{T-t}} \P{\log\ffrac{K}{S_t} - \P{r-\ffrac{1}{2}\sigma^2}\P{T-t}}}\\
&= \QQ\P{Z>-d_2} = \QQ\P{Z<d_2} = \N{d_2}
\end{align}$$
>
>$$\begin{align}
\ExpH^\QQ_t\SB{S_T \cdot \mathbf{1}_{S_{\void_T}>K}} &= \ExpH^\QQ_t\SB{S_T \cdot \mathbf{1}_{Z>-d_{\void_2}}}\\
&= S_t\int_{-d_{\void_2}}^{+\infty} \exp\CB{\P{r-\ffrac{1}{2}\sigma^2}\P{T-t} + \sigma \sqrt{T-t}\cdot z} \ffrac{1}{\sqrt{2\pi}} \exp\CB{-\ffrac{z^2}{2}}\;\dd z\\
&= S_t e^{r\P{T-t}}\int_{-d_{\void_2}}^{+\infty}\ffrac{1}{\sqrt{2\pi}}\exp\CB{-\ffrac{ \P{z - \sigma\sqrt{T-t}}^2 }{2}}\;\dd z\\
&\bspace\text{let } u = z-\sigma\sqrt{T-t},\text{ and the lower bond or the integral:}\\
&\bspace-d_2 \to -\P{d_2 - \sigma\sqrt{T-t}} = -d_1,\text{ so that}\\
&= S_t e^{r\P{T-t}}\int_{-d_{\void_1}}^{+\infty}\ffrac{1}{\sqrt{2\pi}}\exp\CB{-\ffrac{u^2 }{2}}\;\dd u\\
&= S_t e^{r\P{T-t}} \N{d_1}
\end{align}$$
>
>So finally the formula: $c\P{t,S_t} = S_t\N{d_1} - Ke^{-r\P{T-t}} \N{d_2}$

- for European call option with dividend: first $G_t := S_t + \d{\int_0^t qS_u\;\dd u}$, then
$$\begin{align}
\dd V_t &= \ffrac{\pi_t}{S_t}\dd G_t + \ffrac{V_t - \pi_t}{B_t}\dd B_t\neq \ffrac{\pi_t}{S_t}\dd S_t + \ffrac{V_t - \pi_t}{B_t}\dd B_t\\
&= \P{r V_t + \P{\mu+q-r}\pi_t}\dd t + \sigma \pi_t \dd W_t
\end{align}\\[1em]$$
then $\dd S_t = \P{r-q} S_t \dd t + \sigma S_t \dd \widetilde W_t = \mu S_t \dd t + \sigma S_t \dd \widetilde W_t$, so

$$\ffrac{\partial c}{\partial t} + \ffrac{1}{2} \sigma^2 S_t^2 \ffrac{\partial^2 c}{\partial S_t^2} + \P{r\color{brown}{-q}}S_t \ffrac{\partial c}{\partial S_t} - rc = 0$$

$$
\begin{align}
c_{\text{dividend}}\P{t,S_t} &= S_t \color{brown}{e^{-q\P{T-t}}}\N{d_1} -Ke^{-r\P{T-t}}\N{d_2}\\
d_1 &= \ffrac{1}{\sigma\sqrt{T-t}} \P{\log\P{\ffrac{S_t}{K}} + \P{r\color{brown}{-q}\,\mathbf+\,\ffrac{\sigma^2}{2}} \P{T-t}}\\
d_2 &= \ffrac{1}{\sigma\sqrt{T-t}} \P{\log\P{\ffrac{S_t}{K}} + \P{r\color{brown}{-q}\,\mathbf-\,\ffrac{\sigma^2}{2}} \P{T-t}} = d_1 - \sigma\sqrt{T-t}
\end{align}$$

- **Currency Option**: Consider a currency option that give its owner the right to buy $1$ unit of a foreign currency for $K$ units of the domestic currency at maturity $T$. Thus the payoff would be, as evaluated in the domestic currency, is euqal to $\P{R_T - K}^+$, where $R_T$ is the exchange rate at time $T$, $i.e.$, the domestic value of one unit of foreign currency at time $T$. We now assume the **exchange rate process** $\CB{R_t}_{t\geq 0}$ satisfies the **Black-Scholes model**, $\dd R_t = R_t \SB{\mu_R \dd t + \sigma_R \dd W_t} = \mu_R R_t \dd t + \sigma_R R_t \dd W_t$. The wealth dynamic (in domestic currency) of a portfolio with $\pi_t$ in the foreign account and the rest in the domestic account, is$\\[0.8em]$
$$\dd V_t = \ffrac{\pi_t}{R_t^{\,f}} \dd R_t^{\,f} + \ffrac{V_t - \pi_t}{B_t} \dd B_t= \P{r_d V_t + \pi_t \P{\mu_R + r_f - r_d}}\dd t + \pi_t \sigma_R \dd W_t\\[1em]$$
where $r_d$ is the domestic risk-free rate. And this dynamics is the the same as the case of a dividend-paying with $q$ replaced by $r_f$. Thus we have the solution of the option price, by applying the risk-neutral method then,

$$\begin{align}
c\P{t,R_t} &= R_t \exp\CB{-r_f\P{T-t}}\N{d_1} - K\exp\CB{-r_d\P{T-t}}\N{d_2}\\
d_{1,2} &= \ffrac{1}{\sigma_R\sqrt{T-t}} \P{\log\P{\ffrac{R_t}{K}} + \P{r_d - r_f \pm \ffrac{\sigma^2}{2}}\P{T-t}}
\end{align}$$

## Exotic options

- **Forward start option**: see Review_2_Question1.extended and HW_05_Question3
- **Lookback option**:
    - **floating strike**: the *optimal value* of the underlying asset’s price during the option life.
        - **floating lookback call option**: with payoff $S_T - \d{\min_{0 \leq t \leq T}}S_t$
        - **floating lookback put option**: with payoff $\d{\max_{0 \leq t \leq T}}S_t - S_T$
    - **fixed strike**: $K$, while the price will be considered NOT floating
        - **fixed lookback call option**: with payoff $\big(\d{\max_{0 \leq t \leq T}}S_t - K\big)^+$
        - **fixed lookback put option**: with payoff $\big(K - \d{\min_{0 \leq t \leq T}}S_t\big)^+$
- **Exchange option**: The holder has the right to exchange asset $S_t^\P{1}$ for $S_t^\P{2}$ and thus its payoff is $\P{S_T^\P{2} - S_T^\P 1}^+$.

$$c\P{t,S_t^{\P1},S_t^{\P2}} = S_t^{\P1}\P{Z_t\N{\hat d_1} - 1\cdot e^0 N\P{\hat d_2}} = S_t^{\P2} \N{\hat d_1} - S_t^{\P1} \N{\hat d_2}$$

$$\begin{align}
\hat d_1 &= \ffrac{1}{\sigma\sqrt{T-t}} \SB{\log\P{Z_t}+ \ffrac{1}{2}\sigma^2\P{T-t}} = \ffrac{1}{\sigma\sqrt{T-t}} \SB{\log\P{\ffrac{S_t^{\P2}}{S_t^{\P1}}}+ \ffrac{1}{2}\sigma^2\P{T-t}}\\
\hat d_2 &= \ffrac{1}{\sigma\sqrt{T-t}} \SB{\log\P{\ffrac{S_t^{\P2}}{S_t^{\P1}}} - \ffrac{1}{2}\sigma^2\P{T-t}},\bspace \sigma = \sqrt{\sigma_1^2+\sigma_2^2 - \rho\sigma_1\sigma_2}
\end{align}$$

**Implied volatility**

$$\left.\begin{align}
p_{\text{BS}} + S_0 e^{-qT} &= c_{\text{BS}} + Ke^{-rT}\\
p_{\text{imp}} + S_0 e^{-qT} &= c_{\text{imp}} + Ke^{-rT}\\
\end{align}\right\}\Rightarrow p_{\text{BS}} - p_{\text{imp}} = c_{\text{BS}}- c_{\text{imp}}$$

and this implies that European put and call options have the same implied volatility. Suppose that now put option has a higher $\sigma_{\text{img}}$, then since $\large{\nu} = p_{\sigma}>0$, $p_{\text{img}}$ is higher and so is $p_{\text{BS}}$. Thus to arbitrage we should sell the put, buy the call, then short the stock

## Hedging

- The hedger takes a *long position* in $\delta$ units of the futures $F_2\P{t,U}$. Here $\delta$ is called the **hedge ratio** and $F_2\P{t,U}$ is the **futures price** at time $t$ of the contract maturing at time $U$ on the asset $S^\P2$.
- **risk exposure** $H = S_t^\P1 - S_T^\P1 + \delta\P{F_2\P{T,U} - F_2\P{t,U}}$
    - $S_t^\P1 - S_T^\P1$: the change in the value of the asset to be delivered at time $T$
    - $\delta\P{F_2\P{T,U} - F_2\P{t,U}}$: the payoff of holding the *long position* in the futures contract between $t$ and $T$.
- The **basis risk**: its variance conditional at time $t$

$$\VarH_t\SB{H} = \VarH_t\SB{S_T^\P 1}+ \delta^2 \VarH_t \SB{F_2\P{T,U}}-2\delta \CovH_t\P{S_T^\P1,F_2\P{T,U}}$$

- **Optimal $\delta$**: taking the derivative with respect to $\delta$

$$\delta = \ffrac{\CovH_t\P{S_T^\P1,F_2\P{T,U}}}{\VarH_t \SB{F_2\P{T,U}}} = \rho\ffrac{\sigma_S}{\sigma_F}$$

- **Greeks neutral**: A position with a $\Delta$ ($\Gamma$) of zero is called **Delta** (**Gamma**) neutral. And pratically we first reach **Gamma Neutral** then the **Delta Neutral**.

***

FinMath/Models and Pricing of Financial Derivatives/Review_3.ipynb


# 質的変数と回帰分析

If you come here without expecting Japanese, please click [Google translated version](https://translate.google.com/translate?hl=&sl=ja&tl=en&u=https%3A%2F%2Fpy4etrics.github.io%2F13_Dummies.html) in English or the language of your choice.

---

```python
import numpy as np
import pandas as pd
from statsmodels.formula.api import ols
import wooldridge
```

実証分析で使う変数を次の２種類に区別することができる。
1. 量的変数（体重，賃金など）
2. 質的変数（性別，人種，地域など）

今までは量的変数を考えたが，ここでは質的変数について議論する。まず男女のような２つの属性に分けるダミー変数を考える。その後に，より一般的な3つ以上の属性をに特徴付けられるカテゴリー変数を扱う場合を説明する。

## ダミー変数

### ケース１：定数項だけの回帰分析

#### ダミー変数なし

直感的な説明にするために説明変数が定数項だけの回帰分析から始める。
具体的には次の回帰式を考える。

$y=\beta_0+u$

実は，この場合のOLS推定量$\hat{\beta}_0$は被説明変数$y$の平均と等しいことになる。この結果を確認するために以下では`wooldridge`パッケージの`wage1`のデータを使う。

```python
wage1 = wooldridge.data('wage1')
wooldridge.data('wage1',description=True)
```

    name of dataset: wage1
    no of variables: 24
    no of observations: 526

    +----------+---------------------------------+
    | variable | label                           |
    +----------+---------------------------------+
    | wage     | average hourly earnings         |
    | educ     | years of education              |
    | exper    | years potential experience      |
    | tenure   | years with current employer     |
    | nonwhite | =1 if nonwhite                  |
    | female   | =1 if female                    |
    | married  | =1 if married                   |
    | numdep   | number of dependents            |
    | smsa     | =1 if live in SMSA              |
    | northcen | =1 if live in north central U.S |
    | south    | =1 if live in southern region   |
    | west     | =1 if live in western region    |
    | construc | =1 if work in construc. indus.  |
    | ndurman  | =1 if in nondur. manuf. indus.  |
    | trcommpu | =1 if in trans, commun, pub ut  |
    | trade    | =1 if in wholesale or retail    |
    | services | =1 if in services indus.        |
    | profserv | =1 if in prof. serv. indus.     |
    | profocc  | =1 if in profess. occupation    |
    | clerocc  | =1 if in clerical occupation    |
    | servocc  | =1 if in service occupation     |
    | lwage    | log(wage)                       |
    | expersq  | exper^2                         |
    | tenursq  | tenure^2                        |
    +----------+---------------------------------+

    These are data from the 1976 Current Population Survey, collected by
    Henry Farber when he and I were colleagues at MIT in 1988.

時間平均賃金`wage`を被説明変数に設定する。`statsmodels`では，定数項だけの回帰式を考える場合，`1`をつか加える必要がある。

```python
form_const = 'wage ~ 1'  # 定数項だけの場合は１が必要

res_const = ols(form_const, data=wage1).fit()

res_const.params
```

    Intercept    5.896103
    dtype: float64

この推定値が賃金の平均と等しいこをを確認するために，直接平均を計算する。

```python
wage1.loc[:,'wage'].mean()
```

    5.896102674787035

この結果はGM仮定4の$\text{E}(u|X)=0$から簡単に導出できる。この仮定を標本回帰式で考えると次式となる。

$$\frac{1}{N}\sum_{i=1}^Nu_i=0$$

この式の左辺にに回帰式を代入すると

$$\frac{1}{N}\sum_{i=1}^N\left( y_i-\beta_0\right)=\bar{y}-\beta_0$$

この式が0となる$\hat{\beta_0}$がOLS推定量なので

$$\bar{y}=\hat{\beta_0}$$

となる。

#### ダミー変数あり：２つのケース

同じデータを使って$\{0,1\}$の値を取るダミー変数を考える。データセット`wage1`の中の`female`という変数があり，以下の値を取る。
```
女性の場合：female = 1
男性の場合：female = 0
```
値が０のカテゴリーを**基本カテゴリー**という。

$D$を`female`のダミー変数とすると回帰式は以下のようになる。

$$
y=\beta_0+\beta_1D
$$

さらに，この式は$D=\{0,1\}$の値によって以下のように表すことができる。

男性：$D=0\quad\Rightarrow\quad y=\beta_0+u$

女性：$D=1\quad\Rightarrow\quad y=\beta_0+\beta_1+u$

即ち，OLS推定量は以下を表すことになる。

$\hat{\beta}_0$：男性の平均賃金

$\hat{\beta}_0+\hat{\beta}_1$：女性の平均賃金

この回帰式を使い，時間賃金の男女間の差について考察する。

```python
form_const_2 = 'wage ~ female'

res_const_2 = ols(form_const_2, data=wage1).fit()

res_const_2.params
```

    Intercept    7.099489
    female      -2.511830
    dtype: float64

* `female=0`の場合は男性なので，定数項の値（約7.10）が男性の時間賃金の平均である。
* `female=1`の場合は女性なので，女性の時間賃金の平均は

    $$7.10-2.51\approx 4.59$$

即ち，男性（基本カテゴリー）と比べて女性の平均賃金は2.51ドル低い。

データを使い直接男女の平均を計算して確認する。

＜女性の場合＞

```python
# 女性だけを抽出するTrue/False条件の作成
cond_female = (wage1['female']==1)

wage1.loc[cond_female,'wage'].mean()
```

    4.587658740225292

＜男性の場合＞

```python
# 男性だけを抽出するTrue/False条件の作成
cond_male = (wage1['female']==0)

wage1.loc[cond_male,'wage'].mean()
```

    7.099489067157689

**（解釈）**
* 女性の時間賃金は約2.51ドル低い
* しかし比較する場合，同じ条件で比べるべきではないだろうか。例えば，未婚・既婚，教育年数や就労期間が賃金に影響する場合，この差を考慮すべきである。しかし，この回帰式にはそのような変数は含まれていないため，含まれていない変数の違いが賃金の違いに反映されている可能性が高い。

#### ダミー変数あり：４つのケース

データセット`wage1`には`married`という変数が含まれており，以下の値をとる。
```
既婚者の場合：married = 1
未婚者の場合：married = 0
```
`female`と組み合わせることにより，次の４つのケースを分けることができる。
```
未婚男性：female=0, married=0
未婚女性：female=1, married=0
既婚女性：female=1, married=1
既婚男性：female=0, married=1
```
この４つのケースを捉えるために、`female`と`married`の値によって`0`もしくは`1`の値になるダミー変数を作成するが，２つのステップに分けて説明する。

---
＜ステップ１＞

新たなダミー変数の作成ルールを定義する関数を作成する。この関数では`DataFrame`の**行**を引数とし，ステップ２ではこの関数を各行に適用しダミー変数を作成することになる。

```python
# 以下では row をDataFrameの行と考える。

# 未婚男性の関数
def singmale(row):
    if row['female'] == 0 and row['married'] == 0:
        return 1
    else:
        return 0

# 既婚男性の関数
def marmale(row):
    if row['female'] == 0 and row['married'] == 1:
        return 1
    else:
        return 0

# 未婚女性の関数
def singfem(row):
    if row['female'] == 1 and row['married'] == 0:
        return 1
    else:
        return 0

# 既婚女性の関数
def marfem(row):
    if row['female'] == 1 and row['married'] == 1:
        return 1
    else:
        return 0
```

`singmale`を考えてみる。引数の`row`は行を表すので、例として`wage1`の0番目の行を考えてみよう。その行には`female`と`married`の列が含まれており、それらの列の情報に基づいて返り値を設定している。もし`female`の値が0であり、さらに`married`の値も0であれば1を返し、そうでなければ0を返す関数となっている。他の関数も同様に考えることができる。

＜ステップ２＞

上の関数を使い，`wage1`に新たな列を追加する。

以下のコードで使う`.apply()`は第１引数の関数を行または列に適用するメソッドである。`axis=1`は「列」を指定する引数であり、列が縦にあるように「上から下に向かって各行に関数を適用する」ことを指定している（`axis='columns'`でもOK）。また、引数の関数に`()`は書かないことに注意しよう。即ち，引数に関数名（関数を参照する「参照記号」）を引数とすることにより，関数のオブジェクトを参照していることになる。即ち、`.apply()`は第１引数の関数を参照し、それを実行するメソッドである。

```python
wage1.loc[:,'singmale'] = wage1.apply(singmale, axis=1)  # axis='columns'でもOK
wage1.loc[:,'marmale'] = wage1.apply(marmale, axis=1)    # axis='columns'でもOK
wage1.loc[:,'singfem'] = wage1.apply(singfem, axis=1)    # axis='columns'でもOK
wage1.loc[:,'marfem'] = wage1.apply(marfem, axis=1)      # axis='columns'でもOK

wage1.head(3)
```

      wage
      educ
      exper
      tenure
      nonwhite
      female
      married
      numdep
      smsa
      northcen
      ...
      profocc
      clerocc
      servocc
      lwage
      expersq
      tenursq
      singmale
      marmale
      singfem
      marfem

      0
      3.10
      11
      2
      0
      0
      1
      0
      2
      1
      0
      ...
      0
      0
      0
      1.131402
      4
      0
      0
      0
      1
      0

      1
      3.24
      12
      22
      2
      0
      1
      1
      3
      1
      0
      ...
      0
      0
      1
      1.175573
      484
      4
      0
      0
      0
      1

      2
      3.00
      11
      2
      0
      0
      0
      0
      2
      0
      0
      ...
      0
      0
      0
      1.098612
      4
      0
      1
      0
      0
      0

3 rows × 28 columns

表示された`DataFrame`を右にスクロールすると`singmale`、`marmale`、`singfem`、`marfem`の4つの列が追加されていることが確認できる。

一方でコードを書く際、似たものをコピペして少しだけ変更することを繰り返すとエラーや意図しない結果につながる可能性が高くなる。その場合は、次のように`for`ループで書くことを薦める。

まず辞書を作成する：
* キーを列のラベル
* 値を関数名

```python
func_dict = {'singmale':singmale,
             'marmale':marmale,
             'singfem':singfem,
             'marfem':marfem}
```

次に`func_dict`を使い`for`ループで列を追加する。ここで`key`は辞書のキーを指す。

```python
for key in func_dict:
    wage1.loc[:,key] = wage1.apply(func_dict[key], axis=1)

wage1.head(3)
```

      wage
      educ
      exper
      tenure
      nonwhite
      female
      married
      numdep
      smsa
      northcen
      ...
      profocc
      clerocc
      servocc
      lwage
      expersq
      tenursq
      singmale
      marmale
      singfem
      marfem

      0
      3.10
      11
      2
      0
      0
      1
      0
      2
      1
      0
      ...
      0
      0
      0
      1.131402
      4
      0
      0
      0
      1
      0

      1
      3.24
      12
      22
      2
      0
      1
      1
      3
      1
      0
      ...
      0
      0
      1
      1.175573
      484
      4
      0
      0
      0
      1

      2
      3.00
      11
      2
      0
      0
      0
      0
      2
      0
      0
      ...
      0
      0
      0
      1.098612
      4
      0
      1
      0
      0
      0

3 rows × 28 columns

---
これら４つのケースを捉えるために次の回帰式を使う。

$y=\beta_0+\beta_1D_1+\beta_2D_2+\beta_3D_3+u$

* 基本カテゴリー：`singmale`
* $D_1$=`marmale`
* $D_2$=`singfem`
* $D_3$=`marfem`

---
$D_1=\{0,1\}$、$D_2=\{0,1\}$、$D_3=\{0,1\}$の取る値を考慮すると，以下の４つのパターンに分けることができる。

$D_1=0$ & $D_2=0$ & $D_3=0\quad\Rightarrow\quad$
$y=\beta_0+u$

$D_1=1$ & $D_2=0$ & $D_3=0\quad\Rightarrow\quad$
$y=\beta_0+\beta_1+u$

$D_1=0$ & $D_2=1$ & $D_3=0\quad\Rightarrow\quad$
$y=\beta_0+\beta_2+u$

$D_1=0$ & $D_2=0$ & $D_3=1\quad\Rightarrow\quad$
$y=\beta_0+\beta_3+u$

即ち，OLS推定量は以下を表すことになる。

$\hat{\beta}_0$：未婚男性の平均賃金

$\hat{\beta}_0+\hat{\beta}_1$：既婚男性の平均賃金

$\hat{\beta}_0+\hat{\beta}_2$：未婚女性の平均賃金

$\hat{\beta}_0+\hat{\beta}_3$：既婚女性の平均賃金

```python
form_const_4 = 'wage ~ marmale + singfem + marfem'

res_const_4 = ols(form_const_4, data=wage1).fit()

para4 = res_const_4.params
para4
```

    Intercept    5.168023
    marmale      2.815009
    singfem     -0.556440
    marfem      -0.602114
    dtype: float64

（結果）
* 未婚男性の平均賃金は約5.16
* 未婚男性に比べて既婚男性の平均賃金は約2.82高い
* 未婚男性に比べて未婚女性の平均賃金は約0.56低い
* 未婚男性に比べて既婚女性の平均賃金は約0.60低い

`wage1`のデータから直接計算して確認する。

未婚男性の平均賃金

```python
wage1.query('female==0 & married==0')['wage'].mean()
```

    5.168023282705351

```python
para4[0]
```

    5.1680232827053505

既婚男性の平均賃金

```python
wage1.query('female==0 & married==1')['wage'].mean()
```

    7.983031926002909

```python
para4[0]+para4[1]
```

    7.98303192600291

未婚女性の平均賃金

```python
wage1.query('female==1 & married==0')['wage'].mean()
```

    4.611583345135053

```python
para4[0]+para4[2]
```

    4.6115833451350525

既婚女性の平均賃金

```python
wage1.query('female==1 & married==1')['wage'].mean()
```

    4.565909099398238

```python
para4[0]+para4[3]
```

    4.5659090993982385

### ケース２：定量的変数の導入

１つのダミー変数`female`だけが入るケースに次の変数を加えた回帰式を考える。
* `educ`：教育年数
* `exper`：雇用経験年数
* `tenure`：勤続年数

```python
form_1 = 'wage ~ female + educ + exper+ tenure'

res_1 = ols(form_1, data=wage1).fit()

res_1.params
```

    Intercept   -1.567939
    female      -1.810852
    educ         0.571505
    exper        0.025396
    tenure       0.141005
    dtype: float64

（解釈）
* 賃金格差は約-1.81に減少した。これは`educ`, `exper`, `tenure`の影響を取り除いた結果である。言い換えると，教育，経験，就労期間を所与とすると（それらの変数が同じである場合という意味），女性の時間賃金は約1.8ドル低い。
* 女性差別を捉えているのだろうか？回帰式にない変数（その影響は誤差項に入っている）が残っている可能性があるので，必ずしもそうではない。またその場合，欠落変数バイアスも発生し得る。

### ケース３：ダミー変数の交差項

ケース１と２の被説明変数は`wage`をそのまま使ったが，ここでは対数を取り賃金方程式にダミー変数の交差項を入れて考察する。

以下の回帰式を考える。

$$y=\beta_0+\beta_1D+ \beta_2Dx+\beta_3x + u$$

ここで$D$がダミー変数，$x=$は定量的変数であり，$Dx$がダミー変数の交差項である。ダミー変数が取る値$D=\{0,1\}$に分けて考えると，以下を推定することになる。

$D=0\quad\Rightarrow\quad
y=\beta_0+\beta_3x + u$

$D=1\quad\Rightarrow\quad
y=\left(\beta_0+\beta_1\right)+ \left(\beta_2+\beta_3\right)x + u$

---
具体例として$D=$
`female`，$x=$`educ`とするとOLS推定量は以下を表すことになる。

$\hat{\beta}_0$：（教育の効果を取り除いた）男性の平均賃金（対数）

$\hat{\beta}_3$：男性の賃金に対する教育の効果（％）

$\hat{\beta}_0+\hat{\beta}_1$：（教育の効果を取り除いた）女性の平均賃金（対数）

$\hat{\beta}_2+\hat{\beta}_3$：女性の賃金に対する教育の効果（％）

（注意）

`statsmodels`の回帰式では$\text{female}\times \text{educ}$を$\text{female}:\text{educ}$と書く。

```python
form_2 = 'np.log(wage) ~ female + female:educ + educ + exper + tenure'

res_2 = ols(form_2, data=wage1).fit()
```

```python
print(res_2.summary().tables[1])
```

    ===============================================================================
                      coef    std err          t      P>|t|      [0.025      0.975]
    -------------------------------------------------------------------------------
    Intercept       0.4647      0.123      3.781      0.000       0.223       0.706
    female         -0.2104      0.174     -1.209      0.227      -0.552       0.131
    female:educ    -0.0072      0.014     -0.534      0.593      -0.034       0.019
    educ            0.0903      0.009     10.359      0.000       0.073       0.107
    exper           0.0046      0.002      2.850      0.005       0.001       0.008
    tenure          0.0174      0.003      5.849      0.000       0.012       0.023
    ===============================================================================

**$t$検定**
* `female`
    * 教育などの影響を省いた後の平均賃金の差
    * 5%有意水準で$\text{H}_0$`female`=0は棄却できない。
* `female:educ`

    * 教育などの影響を省いた後の教育の収益率の差
    * 5%有意水準で$\text{H}_0$
    `female:educ`=0は棄却できない。

**$F$検定**

$\text{H}_0$: `female`$=$
`female:educ`$=0$の制約を考えよう。

```python
hypotheses = 'female=0, female:educ=0'

res_2.f_test(hypotheses).pvalue
```

    array(3.89455469e-14)

$\text{H}_0$は棄却される。

$t$検定では，`female`と`female:educ`はそれぞれの帰無仮説が棄却されなかったが，$F$検定では制約が棄却された。一貫性がなく説明変数が不足している可能性がある。

## カテゴリー変数

カテゴリー変数とは定性的な変数であり，男女もカテゴリー変数の一種である。カテゴリー変数をダミー変数に変換するには2つの方法がある。

1. `statsmodels`にはカテゴリー変数に自動的にダミー変数を割り当てる機能がある。操作は簡単で，単に回帰式の中で`C()`の中にカテゴリー変数を入れるだけである。
1. `C()`を使わない方法。

### `C()`を使う方法

式の中にダミー変数を入れるためには，変数が`pandas`のカテゴリー型変数に変換される必要がある。回帰式の中で`C()`を使うと，文字型データなどは自動的に変換される。

#### 例１

例として，男女のカテゴリーがある`wage1`のデータセットをもう一度使う。

```python
df = wage1.loc[:,['wage', 'female', 'educ']]
```

`df`のメソッド`replace()`を使って`female`の列の値を以下のルールに沿って変換し，それを`df`に`sex`という列として入れ直す。
* 1 $\Rightarrow$ `female`
* 0 $\Rightarrow$ `male`

`replace()`の中は辞書になっている（`replace()`の代わりに`map()`でもOK）。

```python
df.loc[:,'sex'] = df['female'].replace({1:'female',0:'male'})
```

```python
df.head(3)
```

      wage
      female
      educ
      sex

      0
      3.10
      1
      11
      female

      1
      3.24
      1
      12
      female

      2
      3.00
      0
      11
      male

`sex`の変数を`C()`に入れて回帰式を書いて計算。

```python
form_c = 'wage ~  C(sex) + educ'

res_c = ols(form_c, data=df).fit()

res_c.params
```

    Intercept        -1.650545
    C(sex)[T.male]    2.273362
    educ              0.506452
    dtype: float64

---
`C(sex)[T.male]`について
* `T`は`Treatment`の頭文字で，通常のダミー変数を設定することを示している。
* `male`は`male`の変数であることを表しており，自動的に`female`が基本カテゴリーに設定されたことが分かる。

（結果）

`C(sex)[T.male]`は`female`に比べて`male`の賃金は約2.27ドル高いことを示している。

---
一方で，基本カテゴリーを手動で設定したい場合がある。その場合には`C()`に`Treatment("＜設定したいカテゴリー＞")`を引数として追加する。

（注意）`Treatment()`の中は double quotations `" "`を使用すること。

例えば，`male`を基本カテゴリーとする。

```python
form_cm = 'wage ~  C(sex,Treatment("male")) + educ'

res_cm = ols(form_cm, data=df).fit()

res_cm.params
```

    Intercept                              0.622817
    C(sex, Treatment("male"))[T.female]   -2.273362
    educ                                   0.506452
    dtype: float64

この結果は，`male`基本カテゴリーとする以下の結果と同じである。

```python
form_ca = 'wage ~  female + educ'

res_ca = ols(form_ca, data=df).fit()

res_ca.params
```

    Intercept    0.622817
    female      -2.273362
    educ         0.506452
    dtype: float64

#### 例２

米国ニュージャージーとペンシルベニアのファースト・フード店で人種と所得によって価格差別をおこなっているかを検証する例を取り上げる。`wooldridge`パッケージのデータセット`discrim`には以下の変数が含まれている。
* フライド・ポテトの平均価格（`pfries`）
* 人口における黒人の比率（`prpblck`）
* 平均所得（`income`）
* ４つのファースト・フード店（`chain`; カテゴリー変数で1~4の数字が使われている）
    * `1`: Berger King
    * `2`: KFC
    * `3`: Roy Rogers
    * `4`: Wendy's

```python
discrim = wooldridge.data('discrim')
wooldridge.data('discrim',description=True)
```

    name of dataset: discrim
    no of variables: 37
    no of observations: 410

    +----------+----------------------------------------------+
    | variable | label                                        |
    +----------+----------------------------------------------+
    | psoda    | price of medium soda, 1st wave               |
    | pfries   | price of small fries, 1st wave               |
    | pentree  | price entree (burger or chicken), 1st wave   |
    | wagest   | starting wage, 1st wave                      |
    | nmgrs    | number of managers, 1st wave                 |
    | nregs    | number of registers, 1st wave                |
    | hrsopen  | hours open, 1st wave                         |
    | emp      | number of employees, 1st wave                |
    | psoda2   | price of medium soday, 2nd wave              |
    | pfries2  | price of small fries, 2nd wave               |
    | pentree2 | price entree, 2nd wave                       |
    | wagest2  | starting wage, 2nd wave                      |
    | nmgrs2   | number of managers, 2nd wave                 |
    | nregs2   | number of registers, 2nd wave                |
    | hrsopen2 | hours open, 2nd wave                         |
    | emp2     | number of employees, 2nd wave                |
    | compown  | =1 if company owned                          |
    | chain    | BK = 1, KFC = 2, Roy Rogers = 3, Wendy's = 4 |
    | density  | population density, town                     |
    | crmrte   | crime rate, town                             |
    | state    | NJ = 1, PA = 2                               |
    | prpblck  | proportion black, zipcode                    |
    | prppov   | proportion in poverty, zipcode               |
    | prpncar  | proportion no car, zipcode                   |
    | hseval   | median housing value, zipcode                |
    | nstores  | number of stores, zipcode                    |
    | income   | median family income, zipcode                |
    | county   | county label                                 |
    | lpsoda   | log(psoda)                                   |
    | lpfries  | log(pfries)                                  |
    | lhseval  | log(hseval)                                  |
    | lincome  | log(income)                                  |
    | ldensity | log(density)                                 |
    | NJ       | =1 for New Jersey                            |
    | BK       | =1 if Burger King                            |
    | KFC      | =1 if Kentucky Fried Chicken                 |
    | RR       | =1 if Roy Rogers                             |
    +----------+----------------------------------------------+

    K. Graddy (1997), “Do Fast-Food Chains Price Discriminate on the Race
    and Income Characteristics of an Area?” Journal of Business and
    Economic Statistics 15, 391-401. Professor Graddy kindly provided the
    data set.

それぞれのファースト・フード店の数を確認する。

```python
discrim['chain'].value_counts()
```

    1    171
    3     99
    2     80
    4     60
    Name: chain, dtype: int64

OLS推定をおこなう。

```python
form_p = 'np.log(pfries) ~ prpblck + np.log(income) + C(chain)'

res_p = ols(form_p, data=discrim).fit()

print(res_p.summary().tables[1])
```

    ==================================================================================
                         coef    std err          t      P>|t|      [0.025      0.975]
    ----------------------------------------------------------------------------------
    Intercept         -1.0812      0.222     -4.860      0.000      -1.519      -0.644
    C(chain)[T.2]     -0.0682      0.014     -4.943      0.000      -0.095      -0.041
    C(chain)[T.3]      0.0811      0.013      6.215      0.000       0.055       0.107
    C(chain)[T.4]     -0.0434      0.015     -2.892      0.004      -0.073      -0.014
    prpblck            0.1317      0.031      4.185      0.000       0.070       0.194
    np.log(income)     0.0914      0.021      4.441      0.000       0.051       0.132
    ==================================================================================

自動的にBerger Kingが基本カテゴリーに設定されている。

（結果）
* BKとKFCの価格比較

    $$
    \begin{align}
    \ln P_{\text{KFC}}&=\ln P_{\text{BK}}-0.0682 \\
    &\Downarrow \\
    \dfrac{P_{\text{KFC}}}{P_{\text{BK}}}-1&=e^{-0.0682}-1\approx-0.06593
    \end{align}
    $$

  Berger Kingと比べてKFCの価格は約6.6%低いことがわかる。

* `prpblck`と`np.log(income)`の$p$値は`0`に近く，帰無仮説は１％有意水準でも棄却できる。

### `C()`を使わない方法

例２を使って説明する。まず説明の前準備として`discrim`の中で使う変数だけを取り出そう。

```python
df_c = discrim.loc[:,['pfries', 'prpblck', 'income', 'chain']]
df_c.head()
```

      pfries
      prpblck
      income
      chain

      0
      1.06
      0.171154
      44534.0
      3

      1
      0.91
      0.171154
      44534.0
      1

      2
      0.91
      0.047360
      41164.0
      1

      3
      1.02
      0.052839
      50366.0
      3

      4
      NaN
      0.034480
      72287.0
      1

`replace()`を使って`chain`の列の値を以下のルールに沿って変換する。
* `1` $\Rightarrow$ Berger King
* `2` $\Rightarrow$ KFC
* `3` $\Rightarrow$ Roy Rogers
* `4` $\Rightarrow$ Wendy's

```python
df_c.loc[:,'chain'] = df_c['chain'].replace(
                        {1:'Berger_King',2:'KFC',3:'Roy_Rogers',4:'Wendys'})

df_c.head()
```

      pfries
      prpblck
      income
      chain

      0
      1.06
      0.171154
      44534.0
      Roy_Rogers

      1
      0.91
      0.171154
      44534.0
      Berger_King

      2
      0.91
      0.047360
      41164.0
      Berger_King

      3
      1.02
      0.052839
      50366.0
      Roy_Rogers

      4
      NaN
      0.034480
      72287.0
      Berger_King

`DataFrame`の特徴を確認する。

```python
df_c.info()
```

    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 410 entries, 0 to 409
    Data columns (total 4 columns):
     #   Column   Non-Null Count  Dtype
    ---  ------   --------------  -----
     0   pfries   393 non-null    float64
     1   prpblck  409 non-null    float64
     2   income   409 non-null    float64
     3   chain    410 non-null    object
    dtypes: float64(3), object(1)
    memory usage: 12.9+ KB

列`chain`は`object`となっており文字型であることがわかる。

＜`C()`を使わない方法＞
* `pandas`の関数である`pd.Categorical()`を使って列`chain`を`pandas`のカテゴリー型に変換する。

```python
df_c['chain'] = pd.Categorical(df_c['chain'])
df_c.info()
```

    <class 'pandas.core.frame.DataFrame'>
    RangeIndex: 410 entries, 0 to 409
    Data columns (total 4 columns):
     #   Column   Non-Null Count  Dtype
    ---  ------   --------------  -----
     0   pfries   393 non-null    float64
     1   prpblck  409 non-null    float64
     2   income   409 non-null    float64
     3   chain    410 non-null    category
    dtypes: category(1), float64(3)
    memory usage: 10.3 KB

列`chain`は`category`になっている。後は，`chain`をそのまま回帰式に使うだけである。

```python
form_c = 'np.log(pfries) ~ prpblck + np.log(income) + chain'

res_c = ols(form_c, data=df_c).fit()

print(res_c.summary().tables[1])
```

    =======================================================================================
                              coef    std err          t      P>|t|      [0.025      0.975]
    ---------------------------------------------------------------------------------------
    Intercept              -1.0812      0.222     -4.860      0.000      -1.519      -0.644
    chain[T.KFC]           -0.0682      0.014     -4.943      0.000      -0.095      -0.041
    chain[T.Roy_Rogers]     0.0811      0.013      6.215      0.000       0.055       0.107
    chain[T.Wendys]        -0.0434      0.015     -2.892      0.004      -0.073      -0.014
    prpblck                 0.1317      0.031      4.185      0.000       0.070       0.194
    np.log(income)          0.0914      0.021      4.441      0.000       0.051       0.132
    =======================================================================================

`C()`を使って推定した結果と同じである。