Runnable Python tutorials for RiskLabAI.py
(≥ 2.0.1), the financial-machine-learning library based on López de Prado's
Advances in Financial Machine Learning and Machine Learning for Asset Managers.
Each notebook is exhaustive over its module, runs top-to-bottom against a
pinned environment, and mirrors the matching Julia tutorial in
Notebooks.jl.
New to this? This gets you from a fresh computer to running any tutorial. You only do Step 1 once.
1a. VS Code. Download and install Visual Studio Code (Windows or macOS) and open it once. Then install these extensions (left sidebar → Extensions icon → search → Install):
- Python (publisher: Microsoft)
- Jupyter (publisher: Microsoft)
1b. Anaconda (Python + Jupyter). Download and install
Anaconda (Windows: run the installer with defaults;
macOS: run the .pkg). This gives you Python and Jupyter ready to go.
- Get the notebooks. Go to https://github.com/RiskLabAI/Notebooks.py → green
Code button → Download ZIP → unzip somewhere easy. (Or, with git:
git clone https://github.com/RiskLabAI/Notebooks.py.) - Open the folder in VS Code: File → Open Folder → choose the
Notebooks.pyfolder. - Install the packages. Open a terminal in VS Code (Terminal → New Terminal) and run:
This installs RiskLabAI (from PyPI) plus Jupyter, pandas, matplotlib, and the data tools. (First time takes a few minutes.)
pip install -r requirements.txt
- Open a notebook (e.g.
optimization/portfolio_construction.ipynb), click Select Kernel (top-right) → Python Environments → choose the Anaconda/basePython, then click Run All.
The fractional differentiation and triple-barrier labeling tutorials pull real data from FRED (the Federal Reserve's free database). To run them:
- Get a free API key (instant): https://fredaccount.stlouisfed.org/apikey
- Set it as an environment variable, then open a new terminal so it takes effect:
- Windows:
setx FRED_API_KEY "your_key_here" - macOS: add
export FRED_API_KEY="your_key_here"to~/.zshrc, thensource ~/.zshrc
- Windows:
- Verify in a new terminal:
echo %FRED_API_KEY%(Windows) orecho $FRED_API_KEY(macOS).
All other tutorials run fully offline and need no key.
The Deep-BSDE notebook (pde/deep_bsde.ipynb) needs PyTorch (a large download). Only if
you want to run it:
pip install -r requirements-pde.txt| # | Notebook | Topic |
|---|---|---|
| 1 | optimization/portfolio_construction.ipynb |
Robust portfolio construction — covariance denoising (Marčenko–Pastur), HRP, and NCO vs naive Markowitz |
| 2 | data/structures/financial_data_structures.ipynb |
Financial data structures — time, tick, volume, dollar, imbalance & run bars (offline AAPL data) |
| 3 | data/differentiation/fractional_differentiation.ipynb |
Fractional differentiation — the minimum d for stationarity via an ADF sweep (FRED S&P 500) |
| 4 | data/labeling/triple_barrier_labeling.ipynb |
Triple-barrier labeling + meta-labeling + trend scanning (FRED S&P 500) |
| 5 | backtest/cross_validation_and_pbo.ipynb |
Cross-validation — purged K-Fold, CPCV, walk-forward, and the Probability of Backtest Overfitting (PBO) |
| 6 | features/feature_importance/feature_importance.ipynb |
Feature importance — MDI, MDA, SFI, orthogonal (PCA) features, and weighted Kendall-τ |
| 7 | pde/deep_bsde.ipynb |
Deep-BSDE PDE solver (Han, Jentzen & E 2018) — four financial PDEs, checked vs Monte-Carlo / closed-form references |
RiskLabAI installs straight from PyPI — no clone of the library needed.
git clone https://github.com/RiskLabAI/Notebooks.py.git
cd Notebooks.py
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txt # RiskLabAI (PyPI) + notebook deps
jupyter notebook # launch, then open any tutorialRun a notebook headless:
jupyter nbconvert --to notebook --execute --inplace <path-to-notebook>Optional — Deep-BSDE PDE notebook only. Tutorial 7 (pde/deep_bsde.ipynb) needs
PyTorch, kept out of the light core environment:
pip install -r requirements-pde.txtTutorials 3–4 read the S&P 500 from FRED; set the FRED_API_KEY environment
variable (a free key from https://fred.stlouisfed.org). All other data is either
synthetic or the committed input_data/AAPL_OHLCV_1m_Data.csv snapshot, so the
notebooks stay reproducible and offline.
