Custom loss functions

Machine Learning para finanzas con Python

Nathan George

Data Science Professor

direction mismatch

Machine Learning para finanzas con Python

MSE with directional penalty

If prediction and target direction match:

  • $ \sum(y - \hat{y})^2$

If not:

  • $\sum (y - \hat{y})^2 * \text{penalty} $
Machine Learning para finanzas con Python

Implementing custom loss functions

import tensorflow as tf
Machine Learning para finanzas con Python

Creating a function

import tensorflow as tf

# create loss function def mean_squared_error(y_true, y_pred):
Machine Learning para finanzas con Python

Mean squared error loss

import tensorflow as tf

# create loss function
def mean_squared_error(y_true, y_pred):

loss = tf.square(y_true - y_pred) return tf.reduce_mean(loss, axis=-1)
Machine Learning para finanzas con Python

Add custom loss to keras

import tensorflow as tf

# create loss function
def mean_squared_error(y_true, y_pred):
    loss = tf.square(y_true - y_pred)
    return tf.reduce_mean(loss, axis=-1)

# enable use of loss with keras import keras.losses keras.losses.mean_squared_error = mean_squared_error
# fit the model with our mse loss function
model.compile(optimizer='adam', loss=mean_squared_error)
history = model.fit(scaled_train_features, train_targets, epochs=50)
Machine Learning para finanzas con Python

Checking for correct direction

tf.less(y_true * y_pred, 0)

Correct direction:

  • neg * neg = pos
  • pos * pos = pos

Wrong direction:

  • neg * pos = neg
  • pos * neg = neg
Machine Learning para finanzas con Python

Using tf.where()

# create loss function
def sign_penalty(y_true, y_pred):
    penalty = 10.
    loss = tf.where(tf.less(y_true * y_pred, 0), 
                    penalty * tf.square(y_true - y_pred), 
                    tf.square(y_true - y_pred))
Machine Learning para finanzas con Python

Tying it together

# create loss function
def sign_penalty(y_true, y_pred):
    penalty = 100.
    loss = tf.where(tf.less(y_true * y_pred, 0),
                    penalty * tf.square(y_true - y_pred), 
                    tf.square(y_true - y_pred))

    return tf.reduce_mean(loss, axis=-1)

# enable use of loss with keras keras.losses.sign_penalty = sign_penalty
Machine Learning para finanzas con Python

Using the custom loss

# create the model
model = Sequential()
model.add(Dense(50,
                input_dim=scaled_train_features.shape[1],
                activation='relu'))
model.add(Dense(10, activation='relu'))
model.add(Dense(1, activation='linear'))
# fit the model with our custom 'sign_penalty' loss function
model.compile(optimizer='adam', loss=sign_penalty)
history = model.fit(scaled_train_features, train_targets, epochs=50)
Machine Learning para finanzas con Python

The bow-tie shape

train_preds = model.predict(scaled_train_features)
# scatter the predictions vs actual
plt.scatter(train_preds, train_targets)
plt.xlabel('predictions')
plt.ylabel('actual')
plt.show()

bow-tie plot

Machine Learning para finanzas con Python

Create your own loss function!

Machine Learning para finanzas con Python

Preparing Video For Download...