## CIR model

We derive the CIR term structure model formulae.

### Derivation of bond price formula

We assume that the price of a T-maturity zero coupon bond at time t given the then current rate of $$r_{t}$$ can be represented as:

$$P \left( t, T \right)= A \left( t, T \right) e^{ -r_{t} B \left(t, T \right) }$$ $$dP \left( t, T\right)= \frac {\partial P}{\partial t}dt + \frac {\partial P}{\partial r}dr + \frac{1}{2} \frac{\partial^2 P}{\partial r^2}{dr^2}$$

Now:

$$\frac {\partial P}{\partial t}= \frac {\partial }{\partial t} \left( A e^{ -r B}\right)=e^{ -r B}\frac {\partial A}{\partial t} + A e^{ -r B}\frac {\partial}{\partial t} \left( -r B \right)$$ $$= e^{ -r B}\frac {\partial A}{\partial t} -r A e^{ -r B}\frac {\partial B}{\partial t}=\frac{P}{A} \frac {\partial A}{\partial t} -r P\frac {\partial B}{\partial t}$$ $$\frac {\partial P}{\partial r}= \frac {\partial }{\partial r} \left( A e^{ -r B}\right)= A e^{ -r B}\frac {\partial}{\partial r} \left( -r B \right)=-P B$$ $$\frac {\partial^2 P}{\partial r^2}= \frac {\partial }{\partial r} \left( -P B\right)= -B\frac {\partial}{\partial r} \left( P \right)=P B^2$$

Thus:

$$dP \left( t, T\right)= \frac {\partial P}{\partial t}dt + \frac {\partial P}{\partial r}dr + \frac{1}{2} \frac{\partial^2 P}{\partial r^2}{dr^2}$$ $$dP \left( t, T\right)= \left( \frac{P}{A} \frac {\partial A}{\partial t} -r P\frac {\partial B}{\partial t} \right) dt + \left( -P B \right) dr + \frac{1}{2} \left( P B^2 \right) {dr^2}$$ $$\frac{dP}{P}= \left( \frac{1}{A} \frac {\partial A}{\partial t} -r \frac {\partial B}{\partial t} \right) dt - B dr + \frac{1}{2} B^2 {dr^2}$$ $$\frac{dP}{P}= \left(\frac{1}{A} \frac {\partial A}{\partial t} -r \frac {\partial B}{\partial t} \right) dt - B \left( \kappa \theta dt -\kappa r dt + \sigma \sqrt{r}d w_{t} \right) + \frac{1}{2} B^2 {\sigma}^2 r dt$$ $$\frac{dP}{P}= \left(\frac{1}{A} \frac {\partial A}{\partial t} -r \frac {\partial B}{\partial t} - \kappa \theta B + \kappa r B+ \frac{1}{2} r B^2 {\sigma}^2\right) dt - \sigma \sqrt{r} B d w_{t}$$

The expected return of this under the risk neutral measure must be equal to the risk free rate:

$$\frac{1}{A} \frac {\partial A}{\partial t} -r \frac {\partial B}{\partial t} - \kappa \theta B + \kappa r B + \frac{1}{2} r B^2 {\sigma}^2=r$$ $$\frac{1}{A} \frac {\partial A}{\partial t} - \kappa \theta B =r \left( 1+ \frac {\partial B}{\partial t} -\kappa B -\frac{1}{2} B^2 {\sigma}^2 \right)$$

As this equality holds for all values of r, which does not appear on the left hand side, we infer that

$$\frac{1}{A} \frac {\partial A}{\partial t} - \kappa \theta B = 0$$ $$1+ \frac {\partial B}{\partial t} -\kappa B - \frac{{\sigma}^2}{2} B^2 =0$$

And given that the price of a zero coupon bond at maturity P(T,T)=1, the function $$P=A e^{-r B}$$ implies B(T,T)=0 and A(T,T)=1. Now, the second equation is Riccati equation, and to solve it, we first transform it into a ordinary differential equation. let $$c=-\frac{\sigma^2}{2}$$:

$$B = \frac{1}{c u} \frac{d u}{d t}$$ $$\Rightarrow \frac{d B}{d t} = \frac{1}{c u} \frac{d^2 u}{d t^2}+ \frac{1}{c}\frac{d u}{d t} \frac{d}{d t} \left( \frac{1}{u} \right)$$ $$\quad \quad \frac{d B}{d t} = \frac{1}{c u} \frac{d^2 u}{d t^2} - \frac{1}{c u^2} {\left( \frac{d u}{d t} \right)}^2$$

Making the substitutions into the Riccati equation, we get a second order linear ODE:

$$1+ \frac {\partial B}{\partial t} -\kappa B - \frac{{\sigma}^2}{2} B^2 =0$$ $$1+ \frac{1}{c u} \frac{d^2 u}{d t^2} - \frac{1}{c u^2} {\left( \frac{d u}{d t} \right)}^2- \frac{\kappa}{c u} \frac{d u}{d t} + c \frac{1}{c^2 u^2} {\left( \frac{d u}{d t} \right)}^2 =0$$ $$c u+ \frac{d^2 u}{d t^2} - \kappa \frac{d u}{d t} =0$$ $$\frac{d^2 u}{d t^2} - \kappa \frac{d u}{d t} - \frac{\sigma^2}{2}u =0$$

The characteristic equation of the above ODE is $$m^2 -\kappa m -\frac{\sigma^2}{2}=0$$, which gives $$m= \frac{\kappa \pm \sqrt{k^2+2\sigma^2}}{2}$$, and hence, the solution of the ODE is:

$$u = C_1 e^{\frac{\kappa + \sqrt{k^2+2\sigma^2}}{2} t}+C_2 e^{\frac{\kappa - \sqrt{k^2+2\sigma^2}}{2} t}$$ $$u = C_1 e^{\frac{\kappa + \gamma}{2} t} + C_2 e^{\frac{\kappa - \gamma}{2} t}$$ $$\frac{d u}{d t} = C_1\frac{\kappa + \gamma}{2} e^{\frac{\kappa+ \gamma}{2} t} +C_2 \frac{\kappa-\gamma}{2} e^{\frac{\kappa - \gamma}{2} t}$$ $$B = \frac{1}{c u} \frac{d u}{d t}$$ $$B = - \frac{2}{\sigma^2} \frac{ C_1 \frac{\kappa+\gamma}{2} e^{\frac{\kappa+ \gamma}{2} t}+ C_2 \frac{\kappa-\gamma}{2} e^{\frac{\kappa-\gamma}{2}t}}{C_1 e^{\frac{\kappa+\gamma}{2}t}+C_2 e^{\frac{\kappa-\gamma}{2}t}}$$ $$B = - \frac{1}{\sigma^2} \frac{ C_3 {\left(\kappa+\gamma \right)} e^{\frac{\gamma}{2} t}+ {\left(\kappa-\gamma \right)} e^{\frac{-\gamma}{2} t}}{C_3 e^{\frac{\gamma}{2}t}+ e^{\frac{-\gamma}{2}t}}$$

We now have one unknown constant $$C_3$$ and recall that we have one boundary condition:

$$B(T,T)=0$$ $$C_3 \frac{\kappa+\gamma}{2} e^{\frac{\gamma}{2} T}+ \frac{\kappa-\gamma}{2} e^{\frac{-\gamma}{2} T}=0$$ $$C_3 = \frac {-\kappa+\gamma}{\kappa+\gamma} e^{-\gamma T}$$

Thus:

$$B = - \frac{1}{\sigma^2} \frac{ C_3 \left(\kappa+\gamma \right) e^{\frac{\gamma}{2} t}+ \left(\kappa-\gamma \right) e^{\frac{-\gamma}{2} t}}{C_3 e^{\frac{\gamma}{2}t}+ e^{\frac{-\gamma}{2}t}}$$ $$B = - \frac{1}{\sigma^2} \frac{ \left(-\kappa+\gamma \right) e^{-\gamma T+\frac{\gamma}{2} t}+ \left(\kappa-\gamma \right) e^{\frac{-\gamma}{2} t}}{ \frac{-\kappa+\gamma}{\kappa+\gamma} e^{-\gamma T + \frac{\gamma}{2}t}+ e^{\frac{-\gamma}{2}t}}$$ $$B = - \frac{1}{\sigma^2} \frac{ \left(-\kappa+\gamma \right) e^{-\frac{\gamma \left(T- t \right)}{2}}+ \left(\kappa-\gamma \right) e^{\frac{\gamma \left(T- t \right)}{2}}}{ \frac{-\kappa+\gamma}{\kappa+\gamma} e^{-\frac{\gamma \left(T- t \right)}{2}}+ e^{\frac{\gamma \left(T- t \right)}{2}}}$$ $$B = - \frac{1}{\sigma^2} \frac{ \left(-\kappa+\gamma \right) + \left(\kappa-\gamma \right) e^{\gamma \left(T- t \right)}}{ \frac{-\kappa+\gamma}{\kappa+\gamma} + e^{\gamma \left(T- t \right)}}$$ $$B = - \frac{1}{\sigma^2} \frac{ 1- e^{\gamma \left(T- t \right)}}{ \frac{1}{\kappa+\gamma} +\frac{1}{-\kappa+\gamma} e^{\gamma \left(T- t \right)}}$$ $$B = - \frac{1}{\sigma^2} \frac{ 1- e^{\gamma \left(T- t \right)}}{ \frac{\left(-\kappa+\gamma\right) + \left( \kappa+\gamma\right) e^{\gamma \left(T- t \right)} }{\gamma^2-\kappa^2}}$$ $$B = - \frac{1}{\sigma^2} \frac{ 1- e^{\gamma \left(T- t \right)}}{ \frac{\left(-\kappa+\gamma\right) + \left(\kappa+\gamma\right) - \left(\kappa+\gamma\right) + \left( \kappa+\gamma\right) e^{\gamma \left(T- t \right)} }{\kappa^2+2 \sigma^2-\kappa^2}}$$ $$B = 2 \frac{ e^{\gamma \left(T- t \right)}-1}{ 2 \gamma + \left(\kappa+\gamma\right) \left( e^{\gamma \left(T- t \right)}-1\right)}$$

We now proceed to solve the other equation:

$$\frac{1}{A} \frac {\partial A}{\partial t} - \kappa \theta B =0$$ $$\frac{1}{A} \frac {\partial A}{\partial t} = \kappa \theta B$$ $$\int_{t}^{T}{ \frac {d A \left( u,T \right)}{A \left( u,T \right)} } = \kappa \theta \int_{t}^{T}{ B\left( u, T \right) du}$$ $$ln {A \left( T,T \right) } - ln {A \left( t,T \right) }= 2 \kappa \theta \int_{t}^{T}{ \frac{ e^{\gamma \left(T- u\right)}-1}{ 2 \gamma + \left(\kappa+\gamma\right) \left( e^{\gamma \left(T- u \right)}-1\right)} du}$$ $$- ln {A \left( t,T \right) }= 2 \kappa \theta \int_{t}^{T}{ \frac{ e^{\gamma \left(T- u\right)}-1}{ 2 \gamma + \left(\kappa+\gamma\right) \left( e^{\gamma \left(T- u \right)}-1\right)} du}$$

To simplify the integral on the right hand side, we let $$y= e^{\gamma (T-u)}$$, which implies $$dy= - \gamma e^{\gamma (T-u)} du$$, or $$du= -\frac{dy}{\gamma y}$$, and $$u=T \Rightarrow y=1, u=t \Rightarrow y= e^{\gamma (T-t)}$$:

$$- ln {A \left( t,T \right) }= -\frac{2 \kappa \theta}{\gamma} \int_{e^{\gamma (T-t)}}^{1}{ \frac{y-1}{ 2 \gamma + \left(\kappa+\gamma\right) \left(y -1\right)} \frac{dy}{y} }$$

We evaluate the integral via partial fraction decomposition:

$$\frac{y-1}{ 2 \gamma + \left(\kappa+\gamma\right) \left(y -1\right)} \frac{1}{y}= \frac{y-1}{ \left( \gamma -\kappa + \left( \kappa+\gamma \right)y \right) y}$$ $$\frac{y-1}{ \left( \gamma -\kappa + \left( \kappa+\gamma \right)y \right) y}=\frac{D}{y}+\frac{E}{\gamma -\kappa + \left( \kappa+\gamma \right)y}$$ $$\Rightarrow y-1 =D \left[\gamma -\kappa + \left( \kappa+\gamma \right) y \right]+ E y$$ $$\quad y-1 = \left[ D\left(\kappa+\gamma \right)+ E \right] y+ D \left( \gamma -\kappa \right)$$

Equating the coefficients, we get:

$$-1 = D \left( \gamma -\kappa \right) \Rightarrow D= \frac{1}{\kappa-\gamma}$$ $$1 = D\left(\kappa+\gamma \right)+ E \Rightarrow E = 1- \frac{\kappa+\gamma}{\kappa-\gamma}=\frac{2\gamma}{\gamma-\kappa}$$

Hence:

$$\frac{y-1}{ \left( \gamma -\kappa + \left( \kappa+\gamma \right)y \right) y}=\frac{D}{y}+\frac{E}{\gamma -\kappa + \left( \kappa+\gamma \right)y}$$ $$=\frac{\frac{1}{\kappa-\gamma}}{y}+\frac{\frac{2\gamma}{\gamma-\kappa}}{\gamma -\kappa + \left( \kappa+\gamma \right)y}$$ $$=\frac{1}{\kappa-\gamma} \int_{e^{\gamma (T-t)}}^{1}{\frac{dy}{y}}+\frac{2\gamma}{\gamma-\kappa} \int_{e^{\gamma (T-t)}}^{1}{\frac{dy}{\gamma -\kappa + \left( \kappa+\gamma \right)y}}$$ $$=\frac{1}{\kappa-\gamma} \left( ln(1)- ln(e^{\gamma (T-t)}) \right) + \frac{2\gamma}{\gamma-\kappa} \frac{1}{\gamma+\kappa} \left[ ln(\gamma -\kappa + \kappa+\gamma) - ln (\gamma -\kappa + (\kappa+\gamma)e^{\gamma (T-t)}) \right]$$ $$=\frac{1}{\gamma-\kappa}ln(e^{\gamma (T-t)}) + \frac{2\gamma}{\gamma-\kappa} \frac{1}{\gamma+\kappa} \left[ ln(2\gamma) - ln (\gamma -\kappa + (\kappa+\gamma)e^{\gamma (T-t)}) \right]$$ $$= \frac{2\gamma}{\gamma-\kappa} \frac{1}{\gamma+\kappa} \left[ ln(2\gamma) +\frac{(\gamma+\kappa)}{2\gamma}ln(e^{\gamma (T-t)})- ln (\gamma -\kappa + (\kappa+\gamma)e^{\gamma (T-t)}) \right]$$ $$= \frac{2\gamma}{\gamma^2-\kappa^2} \left[ ln(2\gamma)+ln( e^{\gamma (T-t) \frac{(\gamma+\kappa)}{2\gamma} }) - ln (\gamma -\kappa + (\kappa+\gamma)e^{\gamma (T-t)}) \right]$$ $$= \frac{2\gamma}{2\sigma^2} ln \left( \frac{2\gamma e^{\frac{(\gamma+\kappa)(T-t)}{2}}}{\gamma -\kappa + (\kappa+\gamma)e^{\gamma (T-t)}} \right)$$ $$= \frac{\gamma}{\sigma^2} ln \left( \frac{2\gamma e^{\frac{(\gamma+\kappa)(T-t)}{2}}}{ 2\gamma + (\kappa+\gamma) \left( e^{\gamma (T-t)}-1 \right)} \right)$$

Hence:

$$ln {A \left( t,T \right) }= \frac{2 \kappa \theta}{\gamma} \int_{e^{\gamma (T-t)}}^{1}{ \frac{y-1}{ 2 \gamma + \left(\kappa+\gamma\right) \left(y -1\right)} \frac{dy}{y} }$$ $$ln {A \left( t,T \right) }= \frac{2 \kappa \theta}{\gamma} \frac{\gamma}{\sigma^2} ln \left( \frac{2\gamma e^{\frac{(\gamma+\kappa)(T-t)}{2}}}{ 2\gamma + (\kappa+\gamma) \left( e^{\gamma (T-t)}-1 \right)} \right)$$ $$ln {A \left( t,T \right) }= \frac{2 \kappa \theta}{\sigma^2} ln \left( \frac{2\gamma e^{\frac{(\gamma+\kappa)(T-t)}{2}}}{ 2\gamma + (\kappa+\gamma) \left( e^{\gamma (T-t)}-1 \right)} \right)$$ $$ln {A \left( t,T \right) }= ln {\left( \frac{2\gamma e^{\frac{(\gamma+\kappa)(T-t)}{2}}}{ 2\gamma + (\kappa+\gamma) \left( e^{\gamma (T-t)}-1 \right)} \right)} ^{\frac{2 \kappa \theta}{\sigma^2}}$$ $$A \left( t,T \right) = {\left( \frac{2\gamma e^{\frac{(\gamma+\kappa)(T-t)}{2}}}{ 2\gamma + (\kappa+\gamma) \left( e^{\gamma (T-t)}-1 \right)} \right)} ^{\frac{2 \kappa \theta}{\sigma^2}}$$

Thus:

$$P \left( t, T \right)= A \left( t, T \right) e^{ -r_{t} B \left(t, T \right) }$$ $$P \left( t, T \right)= {\left( \frac{2\gamma e^{\frac{(\gamma+\kappa)(T-t)}{2}}}{ 2\gamma + (\kappa+\gamma) \left( e^{\gamma (T-t)}-1 \right)} \right)} ^{\frac{2 \kappa \theta}{\sigma^2}} e^{ -2 r_{t} \frac{ e^{\gamma \left(T- t \right)}-1}{ 2 \gamma + \left(\kappa+\gamma\right) \left( e^{\gamma \left(T- t \right)}-1\right)} }$$