import tensorflow as tf
import matplotlib.pyplot as plt
from scipy.stats import norm
my_input = []
premium = tf.keras.layers.Input(shape=(1,), name="premium")
my_input = my_input + [premium]
hedge_cost = tf.keras.layers.Input(shape=(1,), name='hedge_cost')
my_input = my_input + [hedge_cost]
price = tf.keras.layers.Input(shape=(1,), name="price")
my_input = my_input + [price]
for j in range(N):
delta = tf.keras.layers.Dense(32, activation='tanh')(price)
# delta = tf.keras.layers.BatchNormalization()(delta)
# #delta = tf.keras.layers.Dropout(0.5)(delta)
# delta = tf.keras.layers.Dense(32, activation='relu')(delta)
# delta = tf.keras.layers.BatchNormalization()(delta)
delta = tf.keras.layers.Dense(32, activation='leaky_relu')(delta)
delta = tf.keras.layers.Dense(1)(delta)
new_price = tf.keras.layers.Input(shape=(1,), name='S'+str(j))
my_input = my_input + [new_price]
price_inc = tf.keras.layers.Subtract(name='price_inc_'+str(j))([price, new_price])
cost = tf.keras.layers.Multiply(name="multiply_"+str(j))([delta, price_inc])
hedge_cost = tf.keras.layers.Add(name='cost_'+str(j))([hedge_cost, cost])
price = new_price
payoff = tf.keras.layers.Lambda(lambda x : 0.5*(tf.abs_mean(x-K)+x-K))(price)
cum_cost = tf.keras.layers.Add(name="final")([hedge_cost, payoff])
cum_cost = tf.keras.layers.Subtract(name="final_")([cum_cost, premium])
model = tf.keras.Model(inputs=my_input, outputs=cum_cost)
p = 1.4116955785329064 * np.ones([M,1])
c = np.zeros([M,1])
SS = [S[:,i].reshape(M,1) for i in range(N+1)]
x = [p]+[c]+[SS]
y = np.zeros([M,1])
I should change lambda from just one value to average price but don’t know how to