Skip to content

Commit

Permalink
SVM
Browse files Browse the repository at this point in the history
  • Loading branch information
romanmichaelpaolucci committed Apr 24, 2021
1 parent 5f1fae9 commit 7219541
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions QFin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from qfin.options import BlackScholesCall
from qfin.options import BlackScholesPut
from qfin.simulations import GeometricBrownianMotion
from qfin.simulations import StochasticVarianceModel
from qfin.simulations import MonteCarloBinaryCall
from qfin.simulations import MonteCarloBinaryPut
from qfin.simulations import MonteCarloCall
Expand Down
43 changes: 43 additions & 0 deletions QFin/simulations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
from scipy.stats import norm


class GeometricBrownianMotion:
Expand All @@ -18,6 +19,35 @@ def __init__(self, S, mu, sigma, dt, T):
self.simulated_path = self.simulate_path(S, mu, sigma, dt, T)


class StochasticVarianceModel:

def simulate_path(self, S, mu, r, div, alpha, beta, rho, vol_var, inst_var, dt, T):
prices = []
price_now = S
inst_var_now = inst_var
prev_inst_var = inst_var_now
step = 0
while step < T:
e1 = norm.ppf(np.random.random())
e2 = e1*rho + np.sqrt(1-(rho**2))*norm.ppf(np.random.random())

price_now = price_now + (r - div) * price_now * dt + price_now * np.sqrt(prev_inst_var * dt) * e1
prev_inst_var = inst_var_now
inst_var_now = prev_inst_var + (alpha - beta*prev_inst_var)*dt + vol_var*np.sqrt(prev_inst_var*dt)*e2

# Avoid negative cases and floor variance at zero
if inst_var_now > .0000001:
pass
else:
inst_var_now = .0000001
prices.append(price_now)
step += dt
return prices

def __init__(self, S, mu, r, div, alpha, beta, rho, vol_var, inst_var, dt, T):
self.simulated_path = self.simulate_path(S, mu, r, div, alpha, beta, rho, vol_var, inst_var, dt, T)


class MonteCarloCall:

def simulate_price(self, strike, n, r, S, mu, sigma, dt, T):
Expand Down Expand Up @@ -92,3 +122,16 @@ class MonteCarloBarrierPut:

def __init__(self):
pass

# 100 - initial underlying asset price
# 0 - underlying asset drift (mu)
# .01 - risk free rate of interest
# .05 - continuous dividend
# .5, 2 - (alpha, beta) mean reverting parameters
# -.7 - (rho) correlation of errors generated
# .3 - Variance's volatility
# 1/52 - time steps (dt)
# 1 - time to maturity (annum)
svm = StochasticVarianceModel(100, 0, .01, .05, .5, 2, -.7, .3, .09, 1/52, 1)

print(svm.simulated_path)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
long_description = f.read()

VERSION = '0.0.16'
DESCRIPTION = 'A Python package for mathematical finance.'
DESCRIPTION = 'A Python library for mathematical finance.'

# Setting up
setup(
Expand Down

0 comments on commit 7219541

Please sign in to comment.