Derivatives analytics with python ---- Part 1

Derivatives analytics with python ---- Part 1

Market-based Valuation

This is a book that I think really helpful and practical to our quant work. But the python code in it is outdated, and some of them are missing the important parts, so from now on, I will use the mindmap to structure the knowledge in it and rewrite the codes in python 3.7. I hope this can help more people.

This is the overall structure of this book and it is also the information from the first chapter.

Mindmap

Derivatives analytics with python ---- Part 1

European Call Option Inner Value Plot

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['font.family'] = 'serif'

# Option Strike
K = 8000
# Graphical Output
S = np.linspace(7000, 9000, 100) # index level values
h = np.maximum(S - K, 0) # inner values of call option
plt.figure()
plt.plot(S, h, lw=2.5) # plot inner values at maturity
plt.xlabel('index level $S_t$ at maturity')
plt.ylabel('inner value of European call option')
plt.grid(True)

Derivatives analytics with python ---- Part 1

European Call Option Value Plot

# first, define the valuation formula for European Call option
import numpy as np
import scipy.stats as si
import sympy as sy
from sympy.stats import Normal, cdf
def BSM_call_value(S0, K, q, T, r, vol):
    # q denotes the dividend rate
    d1 = (np.log(S / K) + (r - q + 0.5 * vol ** 2) * T) / (vol * np.sqrt(T))
    d2 = (np.log(S / K) + (r - q - 0.5 * vol ** 2) * T) / (vol * np.sqrt(T))
    call = (S * np.exp(-q * T) * si.norm.cdf(d1, 0.0, 1.0) - K * np.exp(-r * T) * si.norm.cdf(d2, 0.0, 1.0))
    return call
import sys
sys.path.append('05_com')
# Model and Option Parameters
K = 8000 # strike price
T = 1.0 # time-to-maturity
r = 0.025 # constant, risk-less short rate
vol = 0.2 # constant volatility
# Sample Data Generation
S = np.linspace(4000, 12000, 150) # vector of index level values
h = np.maximum(S - K, 0) # inner value of option
C = [BSM_call_value(S0, K, 0, T, r, vol) for S0 in S][0]
# calculate call option values
# Graphical Output
plt.figure()
plt.plot(S, h, 'b-.', lw=2.5, label='inner value')
# plot inner value at maturity
plt.plot(S, C, 'r', lw=2.5, label='present value')
# plot option present value
plt.grid(True)
plt.legend(loc=0)
plt.xlabel('index level $S_0$')
plt.ylabel('present value $C(t=0)$')
plt.show()

Derivatives analytics with python ---- Part 1

上一篇:全网最新最全首届“陇剑杯”网络安全大赛完整WIRTEUP --- 内存分析(2题)


下一篇:2021年“莲城杯”网络安全大赛-Misc-解密试试