Files
OpenBB/gamestonk_terminal/common/prediction_techniques
DidierRLopes 595a94faf6 Updated Hugo Server (#752)
* Push some changes towards updated Hugo Server

* More hugo server refactoring

* Update Hugo Server website with stock context and economy context

* Small bugs fixes

* Add ETF, options and PO to Hugo Server

* More bugfixes

* Add crypto hugo server

* Improve docs

* merge bugs fixed
2021-09-16 18:43:42 +01:00
..
2021-09-16 18:43:42 +01:00

PREDICTION TECHNIQUES

This menu aims to predict the share price of a pre-loaded stock, and the usage of the following commands along with an example will be exploited below. See How to fune-tuning a NN model.

  • ets
    • Exponential Smoothing (e.g. Holt-Winters)
  • knn
    • k-Nearest Neighbors
  • linear
    • linear regression (polynomial 1)
  • quadratic
    • quadratic regression (polynomial 2)
  • cubic
    • cubic regression (polynomial 3)
  • regression
    • regression (other polynomial)
  • arima
    • autoregressive integrated moving average
  • mlp
    • MultiLayer Perceptron
  • rnn
  • lstm
  • conv1d
    • 1D Convolutional Neural Net

Note: Use this at your own discretion. All of these prediciton techniques rely solely on the closing price of the stock. This means that there are several factors that the models aren't aware of at the time of prediction, and may - drastically - move the price up or down. Examples are: news, analyst price targets, reddit post, tweets from Elon Musk, and so on.

Note 2: Enabling GPU acceleration for TensorFlow requires CUDA setup and will probably not provide any speedup unless you are building large custom models.

ets

usage: ets [-t TREND] [-s SEASONAL] [-p SEASONAL_PERIODS] [-d N_DAYS]

Exponential Smoothing (based on trend+seasonality, see https://otexts.com/fpp2/taxonomy.html):

  • -t : trend component: N: None, A: Additive, Ad: Additive Damped. Default N.
  • -s : seasonality component: N: None, A: Additive, M: Multiplicative. Default N.
  • -p : seasonal periods. Default 5.
  • -d : prediction days. Default 5.
  • -e : end date (format YYYY-MM-DD) of the stock - Backtesting. Default None.

ets_pltr

knn

usage: knn [-i N_INPUTS] [-d N_DAYS] [-n N_NEIGHBORS]

k-Nearest Neighbors:

  • -i : number of days to use for prediction. Default 40.
  • -d : prediction days. Default 5.
  • -n : number of neighbors to use on the algorithm. Default 20.
  • -e : end date (format YYYY-MM-DD) of the stock - Backtesting. Default None.

knn

linear

usage: linear [-i N_INPUTS] [-d N_DAYS]
usage: regression -p 1 [-i N_INPUTS] [-d N_DAYS]

Linear Regression (p=1):

  • -i : number of days to use for prediction. Default 40.
  • -d : prediction days. Default 5.
  • -e : end date (format YYYY-MM-DD) of the stock - Backtesting. Default None.

linear

quadratic

usage: quadratic [-i N_INPUTS] [-d N_DAYS]
usage: regression -p 2 [-i N_INPUTS] [-d N_DAYS]

Quadratic Regression (p=2):

  • -i : number of days to use for prediction. Default 40.
  • -d : prediction days. Default 5.
  • -e : end date (format YYYY-MM-DD) of the stock - Backtesting. Default None.

quadratic

cubic

usage: cubic [-i N_INPUTS] [-d N_DAYS]
usage: regression -p 3 [-i N_INPUTS] [-d N_DAYS]

Cubic Regression (p=3):

  • -i : number of days to use for prediction. Default 40.
  • -d : prediction days. Default 5.
  • -e : end date (format YYYY-MM-DD) of the stock - Backtesting. Default None.

cubic

regression

usage: regression -p N_POLYNOMIAL [-i N_INPUTS] [-d N_DAYS]

Regression:

  • -i : number of days to use for prediction. Default 40.
  • -d : prediction days. Default 5.
  • -p : polynomial associated with regression. Required.
  • -e : end date (format YYYY-MM-DD) of the stock - Backtesting. Default None.

regression

arima

usage: arima [-d N_DAYS] [-i {aic,aicc,bic,hqic,oob}] [-s] [-r] [-o S_ORDER]

Auto-Regressive Integrated Moving Average:

  • -d : prediciton days. Default 5.
  • -i : information criteria - used if auto_arima library is invoked. Default aic.
  • -s : weekly seasonality flag. Default False.
  • -r : results about ARIMA summary flag. Default False.
  • -o : arima model order. If the model order is defined, auto_arima is not invoked, deeming information criteria useless.
    Example: -o 5,1,4 where:
    • p = 5 : order (number of time lags) of the autoregressive model.
    • d = 1 : degree of differencing (the number of times the data have had past values subtracted).
    • q = 4 : order of the moving-average model.
  • -e : end date (format YYYY-MM-DD) of the stock - Backtesting. Default None.

arima

mlp

usage: mlp [-d N_DAYS] [-i N_INPUTS] [--epochs N_EPOCHS] [-e S_END_DATE] [--loops N_LOOPS] [-v VALID]
 [--lr LEARNING_RATE] [--no_shuffle]

MulitLayer Perceptron:

  • -d : prediciton days. Default 5.
  • -i : number of days to use for prediction. Default 40.
  • --epochs : number of training epochs. Default 200.
  • -v : validation split. Default 0.1
  • -e : end date (format YYYY-MM-DD) of the stock - Backtesting. Default None.
  • --xla_cpu: if present, will enable XLA for CPU (overrides environment variables during run).
  • --xla_gpu: if present, will enable XLA for GPU (overrides environment variables during run).
  • --force_allow_gpu_growth: if true, will force TensorFlow to allow GPU memory usage to grow as needed. Otherwise will allocate 100% of available GPU memory when CUDA is set up. Default true.
  • --batch_size: batch size for model training, should not be used unless advanced user. Default None.
  • --lr : learning rate for optimizer
  • --loops: number of loops to iterate and train models. Default 1.
  • --no_shuffle : split validation data in time_ordered way instead of random.

Due to the complexity of defining a model through command line, one can define it in: config_neural_network_models.txt

MultiLayer_Perceptron \
    = [ {'Dense':
                {'units':50, 'activation':'relu'} },
        {'Dense':
                {'units':100, 'activation':'relu'} },
        {'Dense':
                {'units':80, 'activation':'relu'} },
        {'Dense':
                {'units':30, 'activation':'relu'} },
        {'Dense':
                {'activation':'relu'} }]

mlp

rnn

usage: rnn [-d N_DAYS] [-i N_INPUTS] [--epochs N_EPOCHS] [-p {normalization,standardization,none}]
 [-l {mae,mape,mse,msle}] [-e S_END_DATE] [--loops N_LOOPS] [-v VALID] [--lr LEARNING_RATE] [--no_shuffle]

Recurrent Neural Network:

  • -d : prediciton days. Default 5.
  • -i : number of days to use for prediction. Default 40.
  • --epochs : number of training epochs. Default 200.
  • -v : validation split. Default 0.1
  • -e : end date (format YYYY-MM-DD) of the stock - Backtesting. Default None.
  • --xla_cpu: if present, will enable XLA for CPU (overrides environment variables during run).
  • --xla_gpu: if present, will enable XLA for GPU (overrides environment variables during run).
  • --force_allow_gpu_growth: if true, will force TensorFlow to allow GPU memory usage to grow as needed. Otherwise will allocate 100% of available GPU memory when CUDA is set up. Default true.
  • --batch_size: batch size for model training, should not be used unless advanced user. Default None.
  • --loops: number of loops to iterate and train models. Default 1.
  • --lr : learning rate for optimizer
  • --no_shuffle : split validation data in time_ordered way instead of random.

Due to the complexity of defining a model through command line, one can define it in: config_neural_network_models.txt

Recurrent_Neural_Network \
    = [ {'SimpleRNN':
                {'units':100, 'activation':'linear', 'return_sequences':True} },
        {'SimpleRNN':
                {'units':50, 'activation':'linear', 'return_sequences':True} },
        {'Dropout':
                {'rate':0.2} },
        {'SimpleRNN':
                {'units':21, 'activation':'linear', 'return_sequences':False} },
        {'Dense':
                {'activation':'relu'} }]

rnn

Looping Example

loops

Captura de ecrã 2021-03-22, às 02 14 38

lstm

usage: lstm [-d N_DAYS] [-i N_INPUTS]  [--epochs N_EPOCHS] [-e S_END_DATE] [--loops N_LOOPS] [-v VALID]
 [--lr LEARNING_RATE] [--no_shuffle]

Long-Short Term Memory:

  • -d : prediction days. Default 5.
  • -i : number of days to use for prediction. Default 40.
  • --epochs : number of training epochs. Default 200.
  • -v : validation split. Default 0.1
  • -e : end date (format YYYY-MM-DD) of the stock - Backtesting. Default None.
  • --xla_cpu: if present, will enable XLA for CPU (overrides environment variables during run).
  • --xla_gpu: if present, will enable XLA for GPU (overrides environment variables during run).
  • --force_allow_gpu_growth: if true, will force TensorFlow to allow GPU memory usage to grow as needed. Otherwise will allocate 100% of available GPU memory when CUDA is set up. Default true.
  • --batch_size: batch size for model training, should not be used unless advanced user. Default None.
  • --loops: number of loops to iterate and train models. Default 1
  • --lr : learning rate for optimizer
  • --no_shuffle : split validation data in time_ordered way instead of random.

Due to the complexity of defining a model through command line, one can define it in: config_neural_network_models.py

Long_Short_Term_Memory \
    = [ {'LSTM':
                {'units':25, 'activation':'tanh', 'return_sequences':True} },
        {'LSTM':
                {'units':15, 'activation':'tanh', 'return_sequences':False} },
        {'Dense':
                {'activation':'relu'} }]

lstm

Backtesting Example

appl_pred

apple_error

Captura de ecrã 2021-03-14, às 00 06 51

conv1d

usage: lstm [-d N_DAYS] [-i N_INPUTS] [--epochs N_EPOCHS] [-e S_END_DATE] [--loops N_LOOPS] [-v VALID]
[--lr LEARNING_RATE] [--no_shuffle]

1D Convolutional Neural Net:

  • -d : prediction days. Default 5.
  • -i : number of days to use for prediction. Default 40.
  • --epochs : number of training epochs. Default 50.
  • -v : validation split. Default 0.1
  • -e : end date (format YYYY-MM-DD) of the stock - Backtesting. Default None.
  • --xla_cpu: if present, will enable XLA for CPU (overrides environment variables during run).
  • --xla_gpu: if present, will enable XLA for GPU (overrides environment variables during run).
  • --force_allow_gpu_growth: if true, will force TensorFlow to allow GPU memory usage to grow as needed. Otherwise will allocate 100% of available GPU memory when CUDA is set up. Default true.
  • --batch_size: batch size for model training, should not be used unless advanced user. Default None.
  • --loops: number of loops to iterate and train models. Default 1.
  • --lr : learning rate for optimizer
  • --no_shuffle : split validation data in time_ordered way instead of random.