σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
DuckDB skill›Module 3 · Fast and at scale›Topic 3.3·~1 hr 30 minAdvanced

Capstone: a notebook-grade research warehouse

Skill positionModule 3 of 3 · Topic 3 of 3
3.3 Capstone: a notebook-grade research warehouse
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 4

  1. 01The idea in plain words
  2. 02Step 1: the lake
  3. 03Step 2: views, so the definition lives once
  4. 04Step 3: QUALIFY for the best month
  5. 05Step 4: a beta, estimated in SQL
  6. 06What the tasks ask

Every earlier topic was one tool. Research is the chain: raw files in, a layout you can query cheaply, named views every report shares, and a few queries that turn it into numbers you trust. This capstone is that chain in one notebook-sized warehouse, with no server and nothing to install beyond DuckDB.

01

The idea in plain words

Where we are. The last topic of the DuckDB skill. It reuses partitioned Parquet (topic 8), views and QUALIFY (topics 3 to 6), safe quoting (topic 1) and the factor data from the SQL skill. After it comes the Parquet skill, which opens the file format itself.

DefinitionResearch warehouse

A small, reproducible store of cleaned data with named views on top, so every analysis starts from the same definitions. In DuckDB it is a folder of Parquet files plus a handful of CREATE VIEW statements that anyone can rebuild from the raw files.

02

Step 1: the lake

The raw data is five CSV files loaded into the bars table. build_lake writes it back as Parquet, partitioned by symbol: smaller on disk, typed, and a query for one stock opens one folder.

main.pyPython
# Write the lake from the bars table and see what landed on diskprint(build_lake(con, "lake"))print("raw rows:", con.sql("SELECT count(*) FROM bars").fetchall()[0][0])print("lake rows:", con.sql("SELECT count(*) FROM read_parquet('lake/*/*.parquet')").fetchall()[0][0])
['symbol=AAPL', 'symbol=JNJ', 'symbol=JPM', 'symbol=KO', 'symbol=XOM']
raw rows: 33493
lake rows: 33493
03

Step 2: views, so the definition lives once

A return is close / lag(close) - 1 within each symbol, and it is easy to write it slightly differently in every notebook. A view fixes it once. lake exposes the folder name as symbol; daily_returns is built on it, and every later query just reads daily_returns.

main.pyPython
# Create the views, then query the returns without repeating the formulaprint("daily returns:", register_views(con, "lake"))print(con.sql("SELECT symbol, date, round(ret, 4) AS ret FROM daily_returns WHERE symbol = 'KO' ORDER BY date DESC LIMIT 3").fetchall())
daily returns: 33488
[('KO', datetime.date(2026, 8, 21), 0.0066), ('KO', datetime.date(2026, 8, 20), 0.0017), ('KO', datetime.date(2026, 8, 19), 0.0172)]
04

Step 3: QUALIFY for the best month

Daily returns compound into a month with exp(sum(ln(1 + ret))) - 1, a product written as a sum of logs. Adding the returns instead would be wrong by the interest on the interest. Then QUALIFY row_number() OVER (PARTITION BY symbol ORDER BY month_ret DESC) = 1 keeps one row per symbol without a subquery, and the month name is the tie-break so the answer is deterministic.

main.pyPython
# Each symbol's best calendar month, compounded from the daily returns viewfor symbol, month, month_ret in best_months(con):    print(symbol, month, f"{month_ret:+.1%}")
AAPL 2001-01 +45.3%
JNJ 2000-04 +17.4%
JPM 2009-04 +24.2%
KO 2009-05 +14.2%
XOM 2022-10 +26.9%
05

Step 4: a beta, estimated in SQL

Beta is the slope of a stock's excess return on the market's excess return. Compound each stock into months, join the months to the Fama-French factors table, and let regr_slope(excess, mkt_rf) do the regression. The result comes with the number of months used, because a beta from eight months is not a beta from three hundred.

main.pyPython
# The market beta of each stock, with the months behind itfor symbol in SYMBOLS:    beta, months = market_beta(con, symbol)    print(f"{symbol}: beta {beta:.2f} over {months} months")
AAPL: beta 1.31 over 320 months
JPM: beta 1.25 over 320 months
XOM: beta 0.58 over 320 months
JNJ: beta 0.38 over 320 months
KO: beta 0.40 over 320 months
Rebuild, do not patch

The lake and the views are cheap to recreate, so keep one script that builds them from the raw files and run it whenever the inputs change. A warehouse that only exists as the result of last month's manual edits cannot be audited, and a research result you cannot rebuild is a rumour.

06

What the tasks ask

  1. 1
    `build_lake`COPY (SELECT * FROM bars) TO folder (FORMAT parquet, PARTITION_BY (symbol), OVERWRITE_OR_IGNORE), then return the sorted sub-folder names.
  2. 2
    `register_views`Create lake with read_parquet(..., hive_partitioning = true), then daily_returns with lag(); return its row count.
  3. 3
    `best_months`Compound by symbol and month, then QUALIFY row_number() OVER (...) = 1; sort by symbol.
  4. 4
    `market_beta`Monthly returns of one symbol (quote it), joined to factors; regr_slope(ret - rf, mkt_rf) and count(*).

Key takeaways

  • Lake, views, queries Three layers, each cheap to rebuild, make research repeatable.
  • Compound, do not add Monthly return is the product of daily growth factors.
  • Report the sample size A beta is only as good as the months behind it. Next: the Parquet skill.
Quick check
Why does best_months compound with exp(sum(ln(1 + ret))) - 1 instead of sum(ret)?
Pick an answer.

Lab · what's graded

4 tasks · 100 pts
  • Write the lake: build_lake25 pts
  • Name the queries as views: register_views25 pts
  • Each symbol's best month: best_months25 pts
  • Beta to the market factor: market_beta25 pts
Explorer · lab-9-1
▾ lab-9-1
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-9-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-9-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes