I am trying to code the functions that financial Greeks calculate for the BS model.
I started with something like this:
def greeks_vanilla(S, K, r, q, t, T, sigma):
import numpy as np
from scipy.stats import norm
tau = np.linspace(t, T, num = T)
d1 = ( np.log(S/K) + (r - q + sigma**2 /2)*tau ) / (sigma * np.sqrt(tau))
d2 = d1 - sigma*np.sqrt(tau)
delta_call = np.exp(-q*tau) * norm.cdf(d1)
delta_put = -np.exp(-q*tau) * norm.cdf(-d1)
...
return {'d1':d1, 'd2': d2, 'delta_call': delta_call, 'delta_put': delta_put, ...}
'...' means that more Greeks are calculated, but this is not important here.
It worked fine, I had reasonable values, good stories, etc .; however, my teacher told me that he wants to see these values not only compared to time (tau on the x axis), but also compared to S (S is the price of shares on the x axis). In other words, I have to calculate the Greeks to change tau and S.
I tried the following:
def greeks_vanilla(S, K, r, q, t, T, sigma):
import numpy as np
from scipy.stats import norm
S = np.linspace(1, S, num = S)
tau = np.linspace(t, T, num = T)
d1 = ( np.log(S/K) + (r - q + sigma**2 /2)*tau ) / (sigma * np.sqrt(tau))
d2 = d1 - sigma*np.sqrt(tau)
delta_call = np.exp(-q*tau) * norm.cdf(d1)
delta_put = -np.exp(-q*tau) * norm.cdf(-d1)
...
For both versions, I initialized the following parameters and run (a-variable):
S = 30.0
K = 50.0
r = 0.05
q = 0.01
t = 1.0
T = 100.0
sigma = 0.15
a = greeks_vanilla(S, K, r, q, t, T, sigma)
It works fine in the first case, but when I want to change S (second function), I get the following error:
File "greeks.py", line 11, in greeks_vanilla
d1 = ( np.log(S/K) + (r - q + sigma**2 /2)*tau ) / (sigma * np.sqrt(tau))
ValueError: operands could not be broadcast together with shapes (30,) (100,)
Google, , - Numpy (). ( ), .
, S == 100 (S = T), .
:
S = list(S)
:
File "greeks.py", line 11, in greeks_vanilla
d1 = ( np.log(S/K) + (r - q + sigma**2 /2)*tau ) / (sigma * np.sqrt(tau))
TypeError: unsupported operand type(s) for /: 'list' and 'float'
, . , S ( ...), , - (?) - .