Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time-series forecasting (Exponential Smoothing) with Darts Integration #1851

Merged
merged 40 commits into from
May 24, 2022
Merged

Time-series forecasting (Exponential Smoothing) with Darts Integration #1851

merged 40 commits into from
May 24, 2022

Conversation

martinb-ai
Copy link
Contributor

@martinb-ai martinb-ai commented May 21, 2022

Description

Newly updated exponential smoothing with probabilistic forecasting utilizing Darts library
(Found inside stocks/pred/expo)

  • automated historical backtesting and retraining
  • modifiable sliding window and forecasting look-ahead
  • mean average precision error (MAPE) over backtest
  • supports N number of forecasting days; best results are 3-7 days in the future.

appl
image

  • Relevant motivation and context.
    Standardization, flexibility and readability of a common library.
    Improved performance and features.

  • List any dependencies that are required for this change.
    torch, darts

How has this been tested?

  • Please describe the tests that you ran to verify your changes.
stocks
load aapl
pred
expo -w 0.9 -n 6 -t M -p 5 -d T -f 4 -s M
  • Please also list any relevant details for your test configuration.

Checklist:

Others

  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • My code passes all the checks pylint, flake8, black, ... To speed up development you should run pre-commit install.
  • New and existing unit tests pass locally with my changes. You can test this locally using pytest tests/....
    --> No, breaks on covid scripts.

@jmaslek
Copy link
Collaborator

jmaslek commented May 21, 2022

Gonna wanna also update requirements and requirements-full, which can be done as

poetry export -f requirements.txt --without-hashes  --output requirements.txt 
poetry export \
       -f requirements.txt \
       --dev \
       -E prediction \
       --without-hashes \
       --output requirements-full.txt

@jmaslek jmaslek added the feat S Small T-Shirt size Feature label May 21, 2022
@jmaslek
Copy link
Collaborator

jmaslek commented May 21, 2022

Only other thing missing is the Hugo website stuff. If you look in the website folder, you can find common/prediction_techniques then follow the same pattern for editing content/ as well as data/main.yml

@jmaslek
Copy link
Collaborator

jmaslek commented May 21, 2022

So on my not M1 Mac, I seem to be able to install everything except pystan. Ill spare the errors, but they are the usual clang stuff:

    error: Command "clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O2 -Wall -fPIC -O2 -isystem /opt/anaconda3/envs/openbb/include -fPIC -O2 -isystem /opt/anaconda3/envs/openbb/include -I/opt/anaconda3/envs/openbb/include/python3.10 -c pystan/_misc.cpp -o build/temp.macosx-10.9-x86_64-3.10/pystan/_misc.o -Os -ftemplate-depth-256 -Wno-unused-function -Wno-uninitialized -std=c++1y" failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /opt/anaconda3/envs/openbb/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/44/whtr9jk97n9638w2kt7ys1pr0000gn/T/pip-install-3ilzvueq/pystan_e1d671dcfc12414b8ea473f679cc8273/setup.py'"'"'; __file__='"'"'/private/var/folders/44/whtr9jk97n9638w2kt7ys1pr0000gn/T/pip-install-3ilzvueq/pystan_e1d671dcfc12414b8ea473f679cc8273/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/44/whtr9jk97n9638w2kt7ys1pr0000gn/T/pip-record-vzyjam8o/install-record.txt --single-version-externally-managed --compile --install-headers /opt/anaconda3/envs/openbb/include/python3.10/pystan Check the logs for full command output.

Can any other non-m1 folks confirm this (cough @DidierRLopes )

@DidierRLopes
Copy link
Collaborator

Screenshot 2022-05-21 at 12 30 03

This is what I'm getting 😞

@martinb-ai
Copy link
Contributor Author

martinb-ai commented May 21, 2022

Screenshot 2022-05-21 at 12 30 03

This is what I'm getting 😞

@DidierRLopes

MacOS Issues with LightGBM

At the time of writing, there is an issue with libomp 12.0.1 that results in
segmentation fault on Mac OS Big Sur.
Here's the procedure to downgrade the libomp library (from the
original Github issue):

  • Install brew if you don't already have it.
  • Install wget if you don't already have it : brew install wget.
  • Run the commands below:
wget https://raw.githubusercontent.com/Homebrew/homebrew-core/fb8323f2b170bd4ae97e1bac9bf3e2983af3fdb0/Formula/libomp.rb
brew unlink libomp
brew install libomp.rb

If this works for you, I will have to update the install README for our prediction menu with regards to apple products.
I had to do this also, but figured it might only be an M1 issue...

@martinb-ai
Copy link
Contributor Author

So on my not M1 Mac, I seem to be able to install everything except pystan. Ill spare the errors, but they are the usual clang stuff:

    error: Command "clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O2 -Wall -fPIC -O2 -isystem /opt/anaconda3/envs/openbb/include -fPIC -O2 -isystem /opt/anaconda3/envs/openbb/include -I/opt/anaconda3/envs/openbb/include/python3.10 -c pystan/_misc.cpp -o build/temp.macosx-10.9-x86_64-3.10/pystan/_misc.o -Os -ftemplate-depth-256 -Wno-unused-function -Wno-uninitialized -std=c++1y" failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /opt/anaconda3/envs/openbb/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/44/whtr9jk97n9638w2kt7ys1pr0000gn/T/pip-install-3ilzvueq/pystan_e1d671dcfc12414b8ea473f679cc8273/setup.py'"'"'; __file__='"'"'/private/var/folders/44/whtr9jk97n9638w2kt7ys1pr0000gn/T/pip-install-3ilzvueq/pystan_e1d671dcfc12414b8ea473f679cc8273/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/var/folders/44/whtr9jk97n9638w2kt7ys1pr0000gn/T/pip-record-vzyjam8o/install-record.txt --single-version-externally-managed --compile --install-headers /opt/anaconda3/envs/openbb/include/python3.10/pystan Check the logs for full command output.

Can any other non-m1 folks confirm this (cough @DidierRLopes )

I think this might be an issue with using anaconda3 as opposed to miniconda. (Still confirming though)

@hrzn
Copy link

hrzn commented May 21, 2022

The clang issue is due to pystan, which is a dependency of prophet, which provides the Prophet model in Darts by default. Prophet and a couple of other dependencies of Darts can be notoriously painful to install. For these reasons, we also provide versions of Darts that do not include all models. In your case, it could be easier to depend on "u8darts[torch]" (PyPi) or u8darts-torch (conda) than the full darts (which contains all models). "u8darts[torch]" contains all models of Darts except Prophet and pmdarima-based models, which you are unlikely to need. You can see a list of available flavours here

time_col="date",
value_cols=["AdjClose"],
freq="B",
fill_missing_dates=True,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is fine to leave this here, but it might incur a small performance penalty. If you happen to already have the guarantee beforehand that your DataFrame does not have missing dates (even if it has missing values), you can consider setting this to False.

)

# Show forecast over validation # and then +n_predict afterwards sampled 10 times per point
probabilistic_forecast = model_es.predict(n_predict, num_samples=10)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10 samples is somewhat on the low side - something like 500 would seem more reasonable to estimate the distribution. Generating these samples is fast in Numpy so it shouldn't incur any noticeable penalty, and it'll improve precision (especially as later you need the 10th and 90th percentiles).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, will update. Thanks for the insight.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback @hrzn ❤️

We are going to revamp our entire prediction menu with Darts and create a release around it 🚀 🚀

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome :)

@jmaslek
Copy link
Collaborator

jmaslek commented May 21, 2022

So after installing with conda install -c conda-forge -c pytorch u8darts-torch, I broke it :(

2022 May 21, 15:21 (🦋) /stocks/ $ pred
[2022-05-21 15:21:29,852] WARNING | darts.models | Support for AutoARIMA, BATS and TBATS is not available.To enable it, install u8darts[pmdarima] or u8darts[all].
[2022-05-21 15:21:29,853] WARNING | darts.models | Support for Facebook Prophet is not available. To enable it, install "darts", "u8darts[prophet]" or "u8darts[all]" (with pip);or "u8darts-all" (with conda).
libc++abi: terminating with uncaught exception of type std::length_error: vector
zsh: abort      openbb

@jmaslek
Copy link
Collaborator

jmaslek commented May 21, 2022

It works!

Had to clean up my env from goofing around yesterday with the full install, but conda install -c conda-forge -c pytorch u8darts-torch installed on an intel Mac.

Screen Shot 2022-05-21 at 4 00 04 PM

I still get these messages on import, but I assume nothing we can do

[2022-05-21 15:21:29,852] WARNING | darts.models | Support for AutoARIMA, BATS and TBATS is not available.To enable it, install u8darts[pmdarima] or u8darts[all].
[2022-05-21 15:21:29,853] WARNING | darts.models | Support for Facebook Prophet is not available. To enable it, install "darts", "u8darts[prophet]" or "u8darts[all]" (with pip);or "u8darts-all" (with conda).

@martinb-ai
Copy link
Contributor Author

I still get these messages on import, but I assume nothing we can do

[2022-05-21 15:21:29,852] WARNING | darts.models | Support for AutoARIMA, BATS and TBATS is not available.To enable it, install u8darts[pmdarima] or u8darts[all].
[2022-05-21 15:21:29,853] WARNING | darts.models | Support for Facebook Prophet is not available. To enable it, install "darts", "u8darts[prophet]" or "u8darts[all]" (with pip);or "u8darts-all" (with conda).

This is being worked on. unit8co/darts#927

@DidierRLopes
Copy link
Collaborator

I still get this:
Screenshot 2022-05-22 at 00 39 06

My poetry seems to be f*cked somewhere, but unrelated. But with pip install -r requirements-full.txt all is good!

@jmaslek
Copy link
Collaborator

jmaslek commented May 21, 2022

My poetry seems to be f*cked somewhere, but unrelated. But with pip install -r requirements-full.txt all is good!

Yeah in order to get it I had to nuke the env and recreate. I don't think we should add this to poetry.

@martinb-ai
Copy link
Contributor Author

Yeah in order to get it I had to nuke the env and recreate. I don't think we should add this to poetry.

Agreed. I am trying to find the proper steps to have it installed on all system architectures to then create a simple setup script. Poetry does not play nicely with all library versions.

For now I am updating the install README on setup that works for M1 and Intel Macs. (5f5c618)

@hrzn
Copy link

hrzn commented May 22, 2022

I still get these messages on import, but I assume nothing we can do

[2022-05-21 15:21:29,852] WARNING | darts.models | Support for AutoARIMA, BATS and TBATS is not available.To enable it, install u8darts[pmdarima] or u8darts[all].
[2022-05-21 15:21:29,853] WARNING | darts.models | Support for Facebook Prophet is not available. To enable it, install "darts", "u8darts[prophet]" or "u8darts[all]" (with pip);or "u8darts-all" (with conda).

This is being worked on. unit8co/darts#927

I'll put this one higher on our backlog so we address it rather sooner than later.

@martinb-ai
Copy link
Contributor Author

Can confirm this branch runs smoothly on my linux seup.

conda env create -n gst --file build/conda/conda-3-8-env.yaml
conda activate gst
poetry install -E prediction
conda install -c conda-forge -c pytorch u8darts-torch

image

@jmaslek
Copy link
Collaborator

jmaslek commented May 22, 2022

Looks like a side effect of loading in these libraries. When loading in stocks

Loading Daily AAPL stock with starting period 2019-05-20 for analysis.
/Users/james/PycharmProjects/OpenBBTerminal/openbb_terminal/stocks/stocks_helper.py:1128: FutureWarning: DatetimeIndex.union_many is deprecated and will be removed in a future version. Use obj.union instead.
  sch = calendar.schedule(
/Users/james/PycharmProjects/OpenBBTerminal/openbb_terminal/stocks/stocks_helper.py:1128: FutureWarning: DatetimeIndex.union_many is deprecated and will be removed in a future version. Use obj.union instead.
  sch = calendar.schedule(
/Users/james/PycharmProjects/OpenBBTerminal/openbb_terminal/stocks/stocks_helper.py:1128: FutureWarning: DatetimeIndex.union_many is deprecated and will be removed in a future version. Use obj.union instead.
  sch = calendar.schedule(

Can you confirm or is this just me. Its just a warning so we can suppress.

@martinb-ai
Copy link
Contributor Author

Looks like a side effect of loading in these libraries. When loading in stocks

Loading Daily AAPL stock with starting period 2019-05-20 for analysis.
/Users/james/PycharmProjects/OpenBBTerminal/openbb_terminal/stocks/stocks_helper.py:1128: FutureWarning: DatetimeIndex.union_many is deprecated and will be removed in a future version. Use obj.union instead.
  sch = calendar.schedule(
/Users/james/PycharmProjects/OpenBBTerminal/openbb_terminal/stocks/stocks_helper.py:1128: FutureWarning: DatetimeIndex.union_many is deprecated and will be removed in a future version. Use obj.union instead.
  sch = calendar.schedule(
/Users/james/PycharmProjects/OpenBBTerminal/openbb_terminal/stocks/stocks_helper.py:1128: FutureWarning: DatetimeIndex.union_many is deprecated and will be removed in a future version. Use obj.union instead.
  sch = calendar.schedule(

Can you confirm or is this just me. Its just a warning so we can suppress.

Can confirm, see it on my side too - definitely a lib version issue. This warning is not present on the main branch when using a normal env just from poetry install .

@DidierRLopes DidierRLopes merged commit 5f8eb43 into OpenBB-finance:main May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat S Small T-Shirt size Feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants