First I did the LSM (Longstaff-Schwartz) to understand how its work to price an American option.
code for standard_normal
def gen_sn(M,I,anti_paths=True,mo_match=True):
if anti_paths is True:
sn= npr.standard_normal((M+1,np.int(I/2) ))
sn= np.concatenate((sn,-sn),axis=1)
else:
sn=npr.standard_normal((M+1,I))
if mo_match is True:
sn = (sn-sn.mean())/sn.std()
return sn
then, for the simulation and the pricing
def amer(K,S0,r,sigma,T,M,I):
dt = T/M
df = np.exp(-r*dt)
S=np.zeros((M+1 ,I))
S[0]=S0
sn= gen_sn(M,I)
for t in range(1,M+1):
S[t] = S[t-1] * np.exp((r-0.5*sigma**2)*dt + sigma * np.sqrt(dt) * sn[t]) #brown
h = np.maximum(K-S,0)
#LSM Algo
V=np.copy(h)
for t in range(M-1,0,-1): #von 12 bis 0 in -1 schritten -> 12,11,10,9,..,0
reg = np.polyfit(S[t], V[t+1] * df, 3) #least-squares-polynomial-fit /sum(Y-X)^2
C = np.polyval(reg,S[t])
V[t] = np.where(C > h[t], V[t+1]* df, h[t])
#MCS estimator
C0 = df * 1/I *np.sum(V[1])
return C0
So I use this parameters and get this estimated price
amer(90.,100,0.05,0.25,1.0,12,2000) -> 3.6793077
I think this code is ok
So now I will use the Keras Library for neural network regression estimator My results make no sense for me , I am very confused now
def amer(K,S0,r,sigma,T,M,I):
dt = T/M
df = np.exp(-r*dt)
S=np.zeros((M+1 ,I))
S[0]=S0
sn= gen_sn(M,I)
for t in range(1,M+1):
S[t] = S[t-1] * np.exp((r-0.5*sigma**2)*dt + sigma * np.sqrt(dt) * sn[t])
h = np.maximum(K-S,0)
V = np.copy(h)
num_features=13
model=Sequential()
init_w= RandomUniform(minval=-1.0, maxval=1.0) #weight
init_b = Constant(value=0.0) #bias
for t in range(M-1,0,-1):
model.add(Dense(4, kernel_initializer=init_w, bias_initializer=init_b, input_shape=(num_features,)))
model.add(Activation("sigmoid"))
model.add(Dense(2, kernel_initializer=init_w, bias_initializer=init_b))
model.summary()
lr = 0.005
optimizer = Adam(lr=lr)
model.compile(loss="mse", optimizer=optimizer, metrics=['accuracy'])
X= V[t+1] * df
x_train=X[:1000]
x_test = X[1000:2000]
y= S[t]
y_train = y[:1000]
y_test = y[1000:2000]
model.fit(x=x_train, y=y_train, verbose=1, epochs=100, validation_data=[x_test,y_test])
score = model.evaluate(,y)
return score