使用TensorFlow神经网络对美国股票期权定价,由Monte Carlo模拟

时间:2019-01-30 16:57:31

标签: python tensorflow neural-network montecarlo stock

所以我想做的是用Monte Carlo模拟美国期权(股票)并使用TensorFlow对其定价。

我使用两个帮助函数 get_continuation_function 创建TF运算符。然后 pricing_function 创建定价的计算图。

npv 运算符是最佳运动决策的总和。每次我检查行使值是否大于预测的连续值(换句话说,期权是否在货币中)。

而实际的定价功能是 american_tf 。我执行该功能以创建路径,即训练路径的运动值。然后,我向后遍历training_functions,并在每个练习日期学习其价值和决定。

def get_continuation_function():
    X = tf.placeholder(tf.float32, (None,1),name="X")
    y = tf.placeholder(tf.float32, (None,1),name="y")
    w = tf.Variable(tf.random_uniform((1,1))*0.1,,name="w")
    b = tf.Variable(initial_value = tf.ones(1)*1,name="b")
    y_hat = tf.add(tf.matmul(X, w), b)
    pre_error = tf.pow(y-y_hat,2)
    error = tf.reduce_mean(pre_error)
    train = tf.train.AdamOptimizer(0.1).minimize(error)
    return(X, y, train, w, b, y_hat)


def pricing_function(number_call_dates):
    S = tf.placeholder(tf.float32,name="S")
    # First excerise date
    dts = tf.placeholder(tf.float32,name="dts")
    # 2nd exersice date
    K = tf.placeholder(tf.float32,name="K")
    r = tf.placeholder(tf.float32,,name="r")
    sigma = tf.placeholder(tf.float32,name="sigma")
    dW = tf.placeholder(tf.float32,name="dW") 

    S_t = S * tf.cumprod(tf.exp((r-sigma**2/2) * dts + sigma * tf.sqrt(dts) * dW), axis=1)
    E_t = tf.exp(-r * tf.cumsum(dts)) * tf.maximum(K-S_t, 0)

    continuationValues = []
    training_functions = []

    previous_exersies = 0
    npv = 0
    for i in range(number_call_dates-1):
        (input_x, input_y, train, w, b, y_hat) = get_continuation_function()
        training_functions.append((input_x, input_y, train, w, b, y_hat))
        X = tf.keras.activations.relu(S_t[:, i])
        contValue = tf.add(tf.matmul(X, w),b)
        continuationValues.append(contValue)
        inMoney = tf.cast(tf.greater(E_t[:,i], 0.), tf.float32)
        exercise = tf.cast(tf.greater(E_t[:,i], contValue[:,0]), tf.float32) * inMoney * (1-previous_exersies)
        previous_exersies += exercise
        npv += exercise*E_t[:,i]

    # Last exercise date
    inMoney = tf.cast(tf.greater(E_t[:,-1], 0.), tf.float32)
    exercise =  inMoney * (1-previous_exersies)
    npv += exercise*E_t[:,-1]
    npv = tf.reduce_mean(npv)
    return([S, dts, K, r, sigma,dW, S_t, E_t, npv, training_functions])


def american_tf(S_0, strike, M, impliedvol, riskfree_r, random_train, random_pricing):
    n_exercise = len(M)
    with tf.Session() as sess:

        S,dts,K,r,sigma,dW,S_t,E_t,npv,training_functions = pricing_function(n_exercise)
        sess.run(tf.global_variables_initializer())
        paths, exercise_values = sess.run([S_t,E_t], {
            S: S_0,
            dts: M,
            K: strike,
            r: riskfree_r,
            sigma: impliedvol,
            dW: random_train
        })

        for i in range(n_exercise-1)[::-1]:
            (input_x,input_y,train,w,b,y_hat) = training_functions[i]
            y= exercise_values[:,i+1:i+2]
            X = paths[:,i]
            print(input_x.shape)
            print((exercise_values[:,i]>0).shape)
            for epochs in range(100):
                _ = sess.run(train, {input_x:X[exercise_values[:,i]>0], 
                                     input_y:y[exercise_values[:,i]>0]})
                cont_value = sess.run(y_hat, {input_x:X, input_y:y})   
                exercise_values[:,i+1:i+2] = np.maximum(exercise_values[:,i+1:i+2], cont_value)

        npv = sess.run(npv, {S: S_0, K: strike, r: riskfree_r, sigma: impliedvol, dW: N_pricing})

        return npv


N_samples_learn = 1000
N_samples_pricing = 1000
calldates = 12
N = np.random.randn(N_samples_learn,calldates)
N_pricing = np.random.randn(N_samples_pricing,calldates)

american_tf(100., 90., [1.]*calldates, 0.25, 0.05, N, N_pricing)

Calldates是步骤数
训练样本集= 1000
测试样本大小= 1000

但是我的错误很奇怪

 ---> 23                 nput_y:y[exercise_values[:,i]>0]})

 ValueError: Cannot feed value of shape (358,) for Tensor 'Placeholder_441:0', which has shape '(?, 1)'

1 个答案:

答案 0 :(得分:0)

有一堆在评论与@ hallo12讨论的事情。我只想上传一个包含所有更改的工作版本。该代码经过测试,可以正常运行。但是,为了确保最终的培训输出是正确的,你可能需要比较对一些标杆。

一般注释:在这种类型的应用程序中,最好将变量和时间维度分开,尤其是当您只有1个变量时。例如,您的输入数组应为

的3D
[time, training sample, input variable]

而不是使用[训练样本,时间]进行2D。这样,当你遍历时间维度,尺寸的其余部分保持不变。

import tensorflow as tf
import numpy as np

def get_continuation_function():
    X = tf.placeholder(tf.float32, (None,1),name="X")
    y = tf.placeholder(tf.float32, (None,1),name="y")
    w = tf.Variable(tf.random_uniform((1,1))*0.1,name="w")
    b = tf.Variable(initial_value = tf.ones(1)*1,name="b")
    y_hat = tf.add(tf.matmul(X, w), b)
    pre_error = tf.pow(y-y_hat,2)
    error = tf.reduce_mean(pre_error)
    train = tf.train.AdamOptimizer(0.1).minimize(error)
    return(X, y, train, w, b, y_hat)


def pricing_function(number_call_dates):
    S = tf.placeholder(tf.float32,name="S")
    # First excerise date
    dts = tf.placeholder(tf.float32,name="dts")
    # 2nd exersice date
    K = tf.placeholder(tf.float32,name="K")
    r = tf.placeholder(tf.float32,name="r")
    sigma = tf.placeholder(tf.float32,name="sigma")
    dW = tf.placeholder(tf.float32,name="dW")

    S_t = S * tf.cumprod(tf.exp((r-sigma**2/2) * dts + sigma * tf.sqrt(dts) * dW), axis=1)
    E_t = tf.exp(-r * tf.cumsum(dts)) * tf.maximum(K-S_t, 0)

    continuationValues = []
    training_functions = []

    previous_exersies = 0
    npv = 0
    for i in range(number_call_dates-1):
        (input_x, input_y, train, w, b, y_hat) = get_continuation_function()
        training_functions.append((input_x, input_y, train, w, b, y_hat))
        X = tf.keras.activations.relu(S_t[:, i:i+1])
        contValue = tf.add(tf.matmul(X, w),b)
        continuationValues.append(contValue)
        inMoney = tf.cast(tf.greater(E_t[:,i], 0.), tf.float32)
        exercise = tf.cast(tf.greater(E_t[:,i], contValue[:,0]), tf.float32) * inMoney * (1-previous_exersies)
        previous_exersies += exercise
        npv += exercise*E_t[:,i]

    # Last exercise date
    inMoney = tf.cast(tf.greater(E_t[:,-1], 0.), tf.float32)
    exercise =  inMoney * (1-previous_exersies)
    npv += exercise*E_t[:,-1]
    npv = tf.reduce_mean(npv)
    return([S, dts, K, r, sigma,dW, S_t, E_t, npv, training_functions])


def american_tf(S_0, strike, M, impliedvol, riskfree_r, random_train, random_pricing):
    n_exercise = len(M)
    with tf.Session() as sess:

        S,dts,K,r,sigma,dW,S_t,E_t,npv,training_functions = pricing_function(n_exercise)
        sess.run(tf.global_variables_initializer())
        paths, exercise_values = sess.run([S_t,E_t], {
            S: S_0,
            dts: M,
            K: strike,
            r: riskfree_r,
            sigma: impliedvol,
            dW: random_train
        })

        for i in range(n_exercise-1)[::-1]:
            (input_x,input_y,train,w,b,y_hat) = training_functions[i]
            y= exercise_values[:,i+1:i+2]
            X = paths[:,i]
            print(input_x.shape)
            print((exercise_values[:,i]>0).shape)
            for epochs in range(100):
                _ = sess.run(train, {input_x:(X[exercise_values[:,i]>0]).reshape(len(X[exercise_values[:,i]>0]),1),
                                     input_y:(y[exercise_values[:,i]>0]).reshape(len(y[exercise_values[:,i]>0]),1)})
                cont_value = sess.run(y_hat, {input_x:X.reshape(len(X),1), input_y:y.reshape(len(y),1)})
                exercise_values[:,i+1:i+2] = np.maximum(exercise_values[:,i+1:i+2], cont_value)

        npv = sess.run(npv, {S: S_0, K: strike, dts:M, r: riskfree_r, sigma: impliedvol, dW: N_pricing})

        return npv


N_samples_learn = 1000
N_samples_pricing = 1000
calldates = 12
N = np.random.randn(N_samples_learn,calldates)
N_pricing = np.random.randn(N_samples_pricing,calldates)

print(american_tf(100., 90., [1.]*calldates, 0.25, 0.05, N, N_pricing))