σμ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
  • 日本語
  • 中文
Apache Arrow skill›Module 1 · Arrays, tables and kernels›Topic 1.3·~45 minIntermediate

Compute kernels

Skill positionModule 1 of 3 · Topic 3 of 3
1.3 Compute kernels
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 2

  1. 01The idea in plain words
  2. 02Whole columns in, whole columns out
  3. 03Filter, group and sort are kernels too
  4. 04What the tasks ask

A column of 6,700 prices sits in one block of memory. The slow way to compute returns is to pull the prices out into Python objects and loop. The fast way is to hand the whole block to a function written in compiled code that walks it in one go. Arrow ships hundreds of those functions, called kernels, and using them is the difference between code that keeps up with the data and code that does not.

01

The idea in plain words

Where we are. Topic 2 produced tables and batches; this one computes on them. The kernels here are the same ones DuckDB, Polars and pandas call underneath when they hold Arrow data, so what you learn is also what those libraries do for you. Topic 4 looks at the memory that makes these loops so fast.

DefinitionCompute kernel

A function in compiled code that takes whole arrays and returns a whole array or a scalar, handling nulls itself. One call replaces a Python loop and runs over contiguous memory, so it is typically far faster.

02

Whole columns in, whole columns out

To get a return you need each price next to the previous one. Slicing does that without copying: close.slice(1) is the prices from the second on, close.slice(0, n - 1) is the prices up to the second to last, and the two line up element by element. One divide and one subtract then produce the whole return column. The result is one element shorter than the input, because the first price has no predecessor.

main.pyPython
# KO's closing prices in, KO's daily returns out, in a handful of kernel callsreturns = daily_returns(ko["close"])print("prices:", len(ko["close"]), "| returns:", len(returns))print("best day :", round(pc.max(returns).as_py(), 4))print("worst day:", round(pc.min(returns).as_py(), 4))print("mean     :", round(pc.mean(returns).as_py(), 6))
prices: 6699 | returns: 6698
best day : 0.1387
worst day: -0.1006
mean     : 0.000313
03

Filter, group and sort are kernels too

Selecting rows is a two-step: a comparison kernel makes a boolean mask, and filter keeps the rows where it is true. Grouping and sorting follow the same style. above finds the heavy trading days, total_by adds volume per symbol, and top_rows picks the biggest prices. Each is a few lines and none of them touches a Python value until the small answer is read at the end.

main.pyPython
# Heavy days, volume per symbol, and the three highest closesprint("days above 500M shares:", above(table, "volume", 500_000_000).num_rows)print(total_by(table, "symbol", "volume"))top = top_rows(table, "close", 3)print(list(zip(top["symbol"].to_pylist(), top["close"].to_pylist())))
days above 500M shares: 2055
{'AAPL': 2863818765894, 'JNJ': 76321726041, 'JPM': 163036003862, 'KO': 127779316463, 'XOM': 167184796142}
[('JPM', 365.18), ('JPM', 363.25), ('JPM', 363.11)]
A Python loop undoes the gain

for p in close.to_pylist() copies every value into a Python object first, so even a correct loop pays for the copy and then the loop. If a calculation seems to need one, look for the kernel that does it, or build it from slices and arithmetic as daily_returns does. Check pc. completions before writing a loop.

04

What the tasks ask

  1. 1
    `daily_returns`pc.subtract(pc.divide(close.slice(1), close.slice(0, n - 1)), 1.0).
  2. 2
    `above` and `top_rows`table.filter(pc.greater(table[column], threshold)); table.sort_by([(column, "descending")]).slice(0, n).
  3. 3
    `total_by`table.group_by(key).aggregate([(value, "sum")]), sort by the key, then read the two columns into a dict.

Key takeaways

  • Whole arrays, one call A kernel replaces a Python loop and runs on contiguous memory.
  • Slices are free Shifted slices line up neighbours without copying a value.
  • Stay in Arrow until the answer Convert to Python only for the small result. Next: the buffers underneath.
Quick check
Why is daily_returns written with close.slice(1) and close.slice(0, n - 1) instead of a loop over positions?
Pick an answer.

Lab · what's graded

4 tasks · 100 pts
  • Returns from prices: daily_returns25 pts
  • Filter with a mask: above25 pts
  • Group and total: total_by25 pts
  • The largest rows: top_rows25 pts
Explorer · lab-3-1
▾ lab-3-1
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-3-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-3-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