---
title: "Hilpisch Code Resources (Python for Finance, 2nd ed.)"
subtitle: "Curated, commit‑pinned links mapped to our weeks"
---

This page links to the official companion notebooks for Hilpisch (2019) :  Python for Finance (2nd ed., O’Reilly).

- Upstream repo: https://github.com/yhilpisch/py4fi2nd  
- Commit pinned (for stability): 5995cc903bf55c079158e93e96c572c042e44811

Tip: Use the Colab links to open read‑only copies quickly. For local use, clone the repo or our submodule.

## How to run in Colab (one‑time setup cell)

Some notebooks expect a local data file at `../../source/tr_eikon_eod_data.csv` relative to the notebook path (e.g., from `code/ch13/` up to `source/`). In Colab the working folder is `/content`, so run this cell first to create the expected folder and download the data from this course repo (mirrored), with automatic fallback to the author’s repo if needed:

```python
# One‑time setup for Hilpisch notebooks in Colab
import os, requests
os.makedirs('source', exist_ok=True)  # /content/source

urls = [
    # Course mirror (preferred)
    'https://raw.githubusercontent.com/quinfer/financial-data-science/main/resources/hilpisch-data/tr_eikon_eod_data.csv',
    # Upstream (pinned commit) fallback
    'https://raw.githubusercontent.com/yhilpisch/py4fi2nd/5995cc9/source/tr_eikon_eod_data.csv',
]

last_err = None
for u in urls:
    try:
        r = requests.get(u, timeout=30)
        r.raise_for_status()
        open('source/tr_eikon_eod_data.csv', 'wb').write(r.content)
        print('Saved /content/source/tr_eikon_eod_data.csv from', u, '(bytes:', len(r.content), ')')
        last_err = None
        break
    except Exception as e:
        last_err = e
        print('Fetch failed from', u, '->', e)

if last_err:
    raise last_err
```

## Week 0 :  Primer (squared‑loss, diagnostics)

- Chapter 13 :  Statistics and ML workflows (squared‑loss examples)
  - GitHub:  
    - code/ch13/13_a_statistics.ipynb  
      https://github.com/yhilpisch/py4fi2nd/blob/5995cc9/code/ch13/13_a_statistics.ipynb  
    - code/ch13/13_b_statistics.ipynb  
      https://github.com/yhilpisch/py4fi2nd/blob/5995cc9/code/ch13/13_b_statistics.ipynb  
    - code/ch13/13_c_machine_learning.ipynb  
      https://github.com/yhilpisch/py4fi2nd/blob/5995cc9/code/ch13/13_c_machine_learning.ipynb
  - Colab:  
    - [Open in Colab](https://colab.research.google.com/github/yhilpisch/py4fi2nd/blob/5995cc9/code/ch13/13_a_statistics.ipynb) :  descriptive stats, returns, aggregations, hypothesis tests
    - [Open in Colab](https://colab.research.google.com/github/yhilpisch/py4fi2nd/blob/5995cc9/code/ch13/13_b_statistics.ipynb) :  regression basics, residual diagnostics, rolling windows
    - [Open in Colab](https://colab.research.google.com/github/yhilpisch/py4fi2nd/blob/5995cc9/code/ch13/13_c_machine_learning.ipynb) :  scikit‑learn pipeline, scaling, train/test split, CV, model selection

## Week 1 :  FinTech evolution & plotting

- Chapter 03 :  Data structures and plotting
  - GitHub: https://github.com/yhilpisch/py4fi2nd/blob/5995cc9/code/ch03/03_data_structures.ipynb  
  - Colab: [Open in Colab](https://colab.research.google.com/github/yhilpisch/py4fi2nd/blob/5995cc9/code/ch03/03_data_structures.ipynb) :  pandas Series/DataFrame, joins, resampling, basic plotting

## Week 2 :  Data acquisition, features, pipelines

- Chapter 13 :  ML pipelines and evaluation (time‑aware splits as adaptation)  
  - GitHub: https://github.com/yhilpisch/py4fi2nd/blob/5995cc9/code/ch13/13_c_machine_learning.ipynb  
  - Colab: [Open in Colab](https://colab.research.google.com/github/yhilpisch/py4fi2nd/blob/5995cc9/code/ch13/13_c_machine_learning.ipynb) :  feature engineering, scaling, cross‑validation, model comparison

## Week 4 :  Portfolio analytics (context)

- Chapter 13 :  Statistics (risk/return basics for context)  
  - GitHub: https://github.com/yhilpisch/py4fi2nd/blob/5995cc9/code/ch13/13_a_statistics.ipynb  
  - Colab: [Open in Colab](https://colab.research.google.com/github/yhilpisch/py4fi2nd/blob/5995cc9/code/ch13/13_a_statistics.ipynb) :  return distributions, risk measures, rolling/annualised metrics

These examples complement our own labs. Use them to deepen practise with squared‑loss workflows, residual diagnostics, and model evaluation/model governance consistent with @hilpisch2019. Remember to run the setup cell first in Colab to stage the expected data file.
