我正在通过tensorflow创建一个蒙特卡洛模拟到价格选项。当我尝试调用第二个梯度来计算伽玛时,它抛出了一个错误:
Fetch argument None has invalid type <class 'NoneType'>
当我使用blstfPricer运行代码时:
pricerobj = OptionPricer()
blspricer = pricerobj.blstfPricer()
price = blspricer(100,110,2,0.2,0.03,0)
一切正常。但是当我打电话时:
pricer = pricerobj.tfmcPricer(TFMonteCarloSimulatorType.GBM,enable_greeks = True)
price = pricer(100,110,2.0,0.2,0.03,0,0,100000,365 * 2)
它将引发以下错误:
Fetch argument None has invalid type <class 'NoneType'>
我检查了问题是由
引起的gamma = tf.gradients(greeks[0],[spot])
有人可以帮我吗?
请在下面查看我的代码:
class TFMonteCarloSimulator(object):
def __init__(self, SimulatorType):
if not isinstance(SimulatorType, TFMonteCarloSimulatorType):
raise Exception("Please use defined Simulator Type from class TFMonteCarloSimulatorType")
self.simulatorType = SimulatorType
def _GBMSimulation(self):
spot = tf.placeholder(tf.float32)
strike = tf.placeholder(tf.float32)
dt = tf.placeholder(tf.float32)
vol = tf.placeholder(tf.float32)
timeToMaturity = tf.placeholder(tf.float32)
dw = tf.placeholder(tf.float32)
miu = tf.placeholder(tf.float32)
div = tf.placeholder(tf.float32)
pricer = spot * tf.cumprod(tf.exp((miu - div - (vol ** 2) / 2) * dt + vol * tf.sqrt(dt) * dw), axis=1)
return (spot, strike, dt, vol, miu, div, dw, pricer,timeToMaturity)
def createSimulator(self):
if self.simulatorType == TFMonteCarloSimulatorType.GBM:
(spot, strike, dt, vol, miu, div, dw, pricer,timeToMaturity) = self._GBMSimulation()
def simulator(S, K, TTM, volatility, drift, dividend, seed, Nsimulation, Ndays):
if seed != 0:
np.random.seed(seed)
rndMatrix = np.random.randn(Nsimulation, Ndays)
with tf.Session() as sess:
timeDelta = TTM / Ndays
res = sess.run(pricer,{
spot: S,
strike: K,
miu:drift,
div:dividend,
vol:volatility,
dt:timeDelta,
timeToMaturity:TTM,
dw:rndMatrix
})
return res
return simulator
else:
return None
class OptionPricer(TFMonteCarloSimulator):
def __init__(self):
pass
def blstfPricer(self, enable_greeks = True):
spot = tf.placeholder(tf.float32)
strike = tf.placeholder(tf.float32)
dt = tf.placeholder(tf.float32)
vol = tf.placeholder(tf.float32)
ir = tf.placeholder(tf.float32)
div = tf.placeholder(tf.float32)
ndf = tfp.distributions.Normal(0,1).cdf
d1 = (tf.log(spot/strike) + (ir + (vol ** 2)/2) * dt) / (vol * tf.sqrt(dt))
d2 = d1 - vol * tf.sqrt(dt)
price = spot * tf.exp(-div * dt) * ndf(d1) - strike * tf.exp(-ir * dt) * ndf(d2)
targets = [price]
if enable_greeks:
greeks = tf.gradients(price, [spot, vol, ir, dt])
gamma = tf.gradients(ys = greeks[0],xs = [spot])
targets += [greeks, gamma]
def execute(S,K, TTM, imVol, IR, Div):
with tf.Session() as sess:
res = sess.run(targets,
{spot:S,
strike:K,
ir:IR,
vol:imVol,
div:Div,
dt:TTM
})
return res
return execute
def tfmcPricer(self, simType,enable_greeks = True):
if simType == TFMonteCarloSimulatorType.GBM:
(spot, strike, dt, vol, miu, div, dw, pricer, timeToMaturity) = super(OptionPricer, self)._GBMSimulation()
payoff = tf.maximum(pricer[:,-1] - strike, 0)
price = tf.exp(-miu * timeToMaturity) * tf.reduce_mean(payoff)
targets = [price]
if enable_greeks:
greeks = tf.gradients(price, [spot, vol, miu,timeToMaturity])
gamma = tf.gradients(greeks[0],[spot])
targets += [greeks]
targets += [gamma]
def execute(S, K, TTM, volatility, drift, dividend,seed, Nsimulation,Ndays):
if seed != 0:
np.random.seed(seed)
rndMatrix = np.random.randn(Nsimulation, Ndays)
with tf.Session() as sess:
timedelta = TTM / Ndays
res = sess.run(targets,
{
timeToMaturity:TTM,
spot:S,
strike:K,
dt:timedelta,
vol:volatility,
miu:drift,
div:dividend,
dw:rndMatrix
}
)
return res
return execute
else:
return None