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.
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.
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.
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.
# 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
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.
# 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)]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.
# 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%
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.
# 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
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.
What the tasks ask
- 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`register_views`Create
lakewithread_parquet(..., hive_partitioning = true), thendaily_returnswithlag(); return its row count. - 3`best_months`Compound by symbol and month, then
QUALIFY row_number() OVER (...) = 1; sort by symbol. - 4`market_beta`Monthly returns of one symbol (
quoteit), joined tofactors;regr_slope(ret - rf, mkt_rf)andcount(*).
best_months compound with exp(sum(ln(1 + ret))) - 1 instead of sum(ret)?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