Work with Numpy arrays, getting an error

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 #Stock price
K = 50.0 #Strike price
r = 0.05 #Risk-free rate
q = 0.01 #also called delta, annual dividend yield
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 ( ...), , - (?) - .

+4
1

, , 1D, np.linspace. 2D np.linspace.

Numpy -. , linspace , , linspace . , , , (s, t) s in S t in tau, 2D . , , np.meshgrid.

, , #s.

#...................
#...................
#...................
#...................
#...................
####################

numpy - , , tau S. , , tau S, , len(tau)==len(S), . , , - , np.meshgrid .

Numpy meshgrid , . (, linspaces), 2D-, , . , , , - :

def greeks_vanilla(S, K, r, q, t, T, sigma):
    import numpy as np
    from scipy.stats import norm

    v1 = np.linspace(1, S, num = S)
    v2 = np.linspace(t, T, num = T)

    S, TAU = np.meshgrid(v1, v2)

    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)
    ...

.

+1

Source: https://habr.com/ru/post/1611334/


All Articles