## CIR model

We derive the CIR term structure model formulae.

### Short rate dynamics: Mean and Variance

The short rate under the CIR model has the following dynamics:

$$d r_{t}= \kappa \left( \theta-r_{t} \right) dt + \sigma \sqrt{r_t} d w_{t}$$

Rearranging the equation, multiplying both sides by the integrating factor, and integrating from s to t, we get:

$$d r_{t} =\kappa \theta dt -\kappa r_{t} dt + \sigma \sqrt{r_t} d w_{t}$$ $$d r_{t}+ \kappa r_{t} dt = \kappa \theta dt + \sigma \sqrt{r_t} d w_{t}$$ $$e^{\kappa t} d r_{t} + \kappa e^{\kappa t} r_{t} dt =e^{\kappa t}\kappa\theta dt + e^{\kappa t} \sigma \sqrt{r_t} d w_{t}$$ $$d \left( e^{\kappa t} r_{t} \right) = e^{\kappa t}\kappa\theta dt + e^{\kappa t} \sigma \sqrt{r_t} d w_{t}$$ $$\int_{s}^{t} {d \left( e^{k u} r_{u} \right)} = \kappa \theta \int_{s}^{t}{e^{\kappa u} du} +\sigma\int_{s}^{t} {e^{k u} \sqrt{r_u} d w_{u}}$$ $$e^{\kappa t} r_{t} -e^{\kappa s} r_{s} = \kappa \theta \frac{\left( e^{\kappa t}-e^{\kappa s}\right)}{\kappa} + \sigma\int_{s}^{t} {e^{k u} \sqrt{r_u} d w_{u}}$$ $$r_{t} = r_{s}e^{-\kappa \left( t-s\right)}+ \theta \left( 1-e^{-\kappa \left( t-s \right) }\right) +\sigma\int_{s}^{t} {e^{-k \left( t- u \right) } \sqrt{r_u} d w_{u}}$$

The mean and variance of which are given by,

$$E \left[ r_{t} \mid r_{s} \right]= r_{s}e^{-\kappa \left( t-s\right)}+ \theta \left( 1-e^{-\kappa \left( t-s \right) }\right)$$ $$V \left[ r_{t} \mid r_{s} \right]= V \left[ \sigma \int_{s}^{t} {e^{-\kappa \left(t-u \right)} \sqrt{r_u} d w_{u}} \right]$$ $$\quad \quad = E \left[{\sigma}^{2} \int_{s}^{t} {e^{-2 \kappa \left(t-u \right)} r_u du} \right]$$ $$\quad \quad = {\sigma}^{2} \int_{s}^{t} {e^{-2 \kappa \left(t-u \right)} E \left[ r_u \mid r_{s} \right] du}$$ $$\quad \quad = {\sigma}^{2} \int_{s}^{t} {e^{-2 \kappa \left(t-u \right)} \left[ r_{s}e^{-\kappa \left( u-s\right)}+ \theta \left( 1-e^{-\kappa \left( u-s \right) }\right) \right] du}$$ $$\quad \quad = {\sigma}^{2} \left(r_{s} -\theta \right) e^{-2\kappa t+\kappa s} \int_{s}^{t} {e^{\kappa u}du} +{\sigma}^{2} \theta e^{-2 \kappa t} \int_{s}^{t} {e^{2 \kappa u} du}$$ $$\quad \quad = \frac{{\sigma}^{2}}{\kappa} \left(r_{s} -\theta \right) \left( e^{-\kappa \left(t- s \right)}-e^{-2\kappa \left(t- s \right)} \right) + \frac{{\sigma}^2}{2{\kappa}} \theta \left( 1- e^{-2 \kappa \left( t -s \right)}\right)$$ $$\quad \quad = \frac{{\sigma}^{2} r_{s}}{\kappa} \left( e^{-\kappa \left(t- s \right)}-e^{-2\kappa \left(t- s \right)} \right) + \frac{{\sigma}^2 \theta }{2{\kappa}} \left( 1- e^{-2 \kappa \left( t -s \right)} - 2 e^{-\kappa \left(t- s \right)}+2 e^{-2\kappa \left(t- s \right)}\right)$$ $$\quad \quad = \frac{{\sigma}^{2} r_{s}}{\kappa} \left( e^{-\kappa \left(t- s \right)}-e^{-2\kappa \left(t- s \right)} \right) + \frac{{\sigma}^2 \theta }{2{\kappa}} {\left( 1- e^{-\kappa \left( t -s \right)} \right)}^2$$