Get startedGet started for free

Asset price prediction

Now you can use a neural network to predict an asset price, which is a large component of quantitative financial analysis as well as risk management.

You'll use the 2005-2010 stock prices of Citibank, Goldman Sachs and J. P. Morgan to train a network to predict the price of Morgan Stanley's stock.

You'll create and train a neural network with one input layer, one output layer and two hidden layers.

Then a scatter plot will be shown to see how far the predicted Morgan Stanley prices are from their actual values over 2005-2010. (Recall that if the predictions are perfect, the resulting scatter plot will lie on the 45-degree line of the plot.)

The Sequential and Dense objects are available, as well as the prices DataFrame with investment bank prices from 2005-2010.

This exercise is part of the course

Quantitative Risk Management in Python

View Course

Exercise instructions

  • Set the input data to be all bank prices except Morgan Stanley, and the output data to be only Morgan Stanley's prices.
  • Create a Sequential neural network model with two Dense hidden layers: the first with 16 neurons (and three input neurons), and the second with 8 neurons.
  • Add a single Dense output layer of 1 neuron to represent Morgan Stanley's price.
  • Compile the neural network, and train it by fitting the model.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Set the input and output data
training_input = prices.____('Morgan Stanley', axis=1)
training_output = prices['Morgan Stanley']

# Create and train the neural network with two hidden layers
model = ____()
model.add(Dense(16, input_dim=____, activation='sigmoid'))
model.add(____(8, activation='relu'))
model.add(____(1))

model.____(loss='mean_squared_logarithmic_error', optimizer='rmsprop')
model.____(training_input, training_output, epochs=100)

# Scatter plot of the resulting model prediction
axis.scatter(training_output, model.predict(training_input)); plt.show()
Edit and Run Code