Skip to content

Instantly share code, notes, and snippets.

@StuartGordonReid
Created June 15, 2015 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save StuartGordonReid/c5ba147d9e90cacef2f2 to your computer and use it in GitHub Desktop.
Save StuartGordonReid/c5ba147d9e90cacef2f2 to your computer and use it in GitHub Desktop.
Cox Ingersoll Ross Stochastic Process
import math
import numpy
import random
import decimal
import scipy.linalg
import numpy.random as nrand
import matplotlib.pyplot as plt
"""
Note that this Gist uses the Model Parameters class found here - https://gist.github.com/StuartGordonReid/f01f479c783dd40cc21e
"""
def cox_ingersoll_ross_levels(param):
"""
This method returns the rate levels of a mean-reverting cox ingersoll ross process. It is used to model interest
rates as well as stochastic volatility in the Heston model. Because the returns between the underlying and the
stochastic volatility should be correlated we pass a correlated Brownian motion process into the method from which
the interest rate levels are constructed. The other correlated process is used in the Heston model
:param param: the model parameters object
:return: the interest rate levels for the CIR process
"""
brownian_motion = brownian_motion_log_returns(param)
# Setup the parameters for interest rates
a, mu, zero = param.cir_a, param.cir_mu, param.all_r0
# Assumes output is in levels
levels = [zero]
for i in range(1, param.all_time):
drift = a * (mu - levels[i-1]) * param.all_delta
# The main difference between this and the Ornstein Uhlenbeck model is that we multiply the 'random'
# component by the square-root of the previous level i.e. the process has level dependent interest rates.
randomness = math.sqrt(levels[i - 1]) * brownian_motion[i - 1]
levels.append(levels[i - 1] + drift + randomness)
return numpy.array(levels)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment