Neural Networks

Machine Learning for Finance in Python

Nathan George

Data Science Professor

GPU moore's law

Machine Learning for Finance in Python

Neural networks have potential

Neural nets have:

  • non-linearity
  • variable interactions
  • customizability
Machine Learning for Finance in Python

basic neural net diagram

Machine Learning for Finance in Python

net diagram with basic math

Machine Learning for Finance in Python

neural net math

Machine Learning for Finance in Python

activation neural net diagram

Machine Learning for Finance in Python

activation function

Machine Learning for Finance in Python

loss function

Machine Learning for Finance in Python

forward pass

Machine Learning for Finance in Python

backwards pass

Machine Learning for Finance in Python

keras and tensorflow logos

Machine Learning for Finance in Python

Implementing a neural net with keras

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
Machine Learning for Finance in Python

Implementing a neural net with keras

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense


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'))
Machine Learning for Finance in Python

Fitting the model

model.compile(optimizer='adam', loss='mse')
history = model.fit(scaled_train_features, 
                    train_targets, 
                    epochs=50)
Machine Learning for Finance in Python
plt.plot(history.history['loss'])
plt.title('loss:' + str(round(history.history['loss'][-1], 6)))
plt.xlabel('epoch')
plt.ylabel('loss')
plt.show()

loss plot

Machine Learning for Finance in Python

Checking out performance

from sklearn.metrics import r2_score

# calculate R^2 score
train_preds = model.predict(scaled_train_features)
print(r2_score(train_targets, train_preds))
0.4771387560719418
Machine Learning for Finance in Python

Plot performance

# plot predictions vs actual
plt.scatter(train_preds, train_targets)
plt.xlabel('predictions')
plt.ylabel('actual')
plt.show()

preds vs actual

Machine Learning for Finance in Python

Make a neural net!

Machine Learning for Finance in Python

Preparing Video For Download...