# Least-Squares-Monte-Carlo by Neural Network Estimator for pricing American Option Python [closed]

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

lr = 0.005

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


## closed as unclear what you're asking by Gordon, skoestlmeier, Attack68♦Jun 5 at 15:09

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• All aspects of your code seem very confused. There was not enough space in comments so i put in answer but see below. It seems you are just guessing how to implement all these things instead of following some standard logic presented in papers. – Ezy Jan 3 at 1:35
• I'm voting to close this question as off-topic because this a question about technical use of a python package (keras) – lehalle Jan 10 at 21:32
• I think this question belongs on StackOverflow with a python and keras tag. Has the OP found a solution or workaround? – rajah9 Jun 4 at 13:28

In the non nn case your code does not implement the longstaff-schwartz algorithm so i am not sure what makes why you think it does. Longstaff-Schwartz is a Monte-Carlo method and you seem to be implementing some backward pricing scheme so this does not make much sense at all to me.

Longstaff-Schwartz has 2 phases: 1 backward pricing step to calibrate the continuation value estimator and a regular MC forward pricing step to actually price the option.

You seem to have tested that for some value of the input your procedure returns some double value but this is far from being a test that your procedure is correct! You should benchmark your code against known results for european and american payoffs in order to take any sort of conclusion regarding the “ok-ness” of your code.

So overall, you should spend more time checking that your pricing algo actually makes sense before using NN as an alternative fitting algo for the continuation value. It is not even clear what you are trying to achieve here because the continuation value of an american is not so non-linear as to justify using a NN algo to fit it so it is not even clear what your motivation is here.

NB: you keras procedure seem not even to make sense as you seem to use the continuation value as “X” and the spot value as “Y” so it is not even clear what you are trying to do at all here. There is not even any pricing logic.

In addition you seem to be hitting an error where a basic input array having the wrong shape for your keras fit to work as you are asking for 13 features but provide a single dimenion array as input for X so you must have a basic input incorrectly setup. So probably you want to make some basic tests that you know how to use this fitter before asking how to use it inside some sort of american option pricing algo.

• okay thanks, but there is something which is not so clear for me. if it isn't the continuation value for x , which one I need for the input x ? For the target value I can use regular mc forward pricing step – joey Jan 5 at 13:22

ok, sorry but i am not understanding what u are trying. Here is a paper, i think it can be helpful for u. But there is one problem what is not clear for me. The features Matrix X ("feature_matrix_from_current_state), he used the chebyshev polynomials with degree 4. So if u want to use neural network , i think u must change this feature matrix , but i dont know how u can do it. https://ipythonquant.wordpress.com/2018/06/01/pricing-bermudan-options-in-tensorflow-learning-an-optimal-early-exercise-strategy/