0

Just a toy example. Suppose we have 5 stocks and we want to find the best portfolio structure (linear weights) maximizing our PnL on history. Weights are used to build portfolio invested in equities.

weights = tf.Variable(np.random.random((5, 1)), dtype=tf.double)
returns = tf.placeholder(dtype=tf.double)
portfolio = tf.matmul(returns, weights)
pnl = portfolio[-1]

optimizer = tf.train.AdamOptimizer().minimize(-1*pnl)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    train_data = {returns: returns_data}  
    for i in range(100):
        sess.run(optimizer, feed_dict=train_data)

I want to find the best decision on history with following constraints:

  • each stock individual weight (min: 0.05, max: 0.5)
  • weights vector sum = 1 (portfolio is always invested)

How can I implement weights constraints to the optimizer?

1 Answers1

0

For your first question, you can clip values into your matrice :

weights = tf.Variable(np.random.random((5, 1)), dtype=tf.double)
weights = tf.clip_by_value(weights, 0.05, 0.5)

The softmax function can be an answer to your second question (https://en.wikipedia.org/wiki/Softmax_function)

By the way, optimizer = tf.train.AdamOptimizer().minimize(-1*pnl) won't work. The optimizer needs to know the variable list from which it needs to minimize the loss. Therefore it is : optimizer = tf.train.AdamOptimizer().minimize(-1*pnl, weights)

Adrien Logut
  • 812
  • 5
  • 13