Skip to content

Capstones

The capstone projects live in src/capstones/. Each one combines several language features and shells into a realistic, self-contained program.

This capstone computes option price sensitivities (delta, gamma, vega, theta) by applying grad to the Black-Scholes pricing formula. It demonstrates:

  • Reverse-mode AD applied to a financial model.
  • nautilus special functions (the normal CDF via erf).
  • Higher-order derivatives via nested grad calls (gamma is the second derivative of price with respect to spot).
import nautilus.distributions.Normal
fn black_scholes(S: f64, K: f64, T: f64, r: f64, sigma: f64) -> f64 =
let d1 = (ln(S / K) + (r + 0.5 * sigma * sigma) * T) / (sigma * sqrt(T))
let d2 = d1 - sigma * sqrt(T)
S * Normal.cdf(d1) - K * exp(-r * T) * Normal.cdf(d2)
let delta = grad(black_scholes, wrt=S)
let gamma = grad(delta, wrt=S)
let vega = grad(black_scholes, wrt=sigma)

A minimal training loop that fits a linear model using stochastic gradient descent. It demonstrates:

  • school's training loop and optimizer primitives.
  • grad for computing parameter gradients.
  • coral for loading and batching data.
import school.{Linear, SGD, train_step}
import coral.{read_csv, Frame}
let data: Frame[X: f64, Y: f64] = read_csv("regression.csv")
let model = Linear[In: 1, Out: 1]
let opt = SGD(lr=0.01)
for batch in data.batches(size=32):
let loss_fn = fn(m) => mse(m.forward(batch.X), batch.Y)
train_step(model, opt, loss_fn)

A single transformer block (multi-head attention, layer norm, feed-forward) composed entirely from chelis-std primitives. It demonstrates:

  • Named dimensions for heads, sequence length, and embedding size.
  • vmap over attention heads.
  • Linearity discipline: explicit copies where tensors are reused (residual connections).
fn attention[Heads, Seq, Emb](
q: Tensor[Heads, Seq, Emb],
k: Tensor[Heads, Seq, Emb],
v: Tensor[Heads, Seq, Emb]
) -> Tensor[Heads, Seq, Emb] =
let scale = sqrt(f64(Emb))
let scores = (q @ k.transpose(Seq, Emb)) / scale
let weights = softmax(scores, dim=Seq)
weights @ v
fn transformer_block[Seq, Emb](x: Tensor[Seq, Emb]) -> Tensor[Seq, Emb] =
let normed = layernorm(copy(x), dim=Emb)
let attn_out = multi_head_attention(normed)
let residual1 = copy(x) + attn_out
let ff_out = feed_forward(layernorm(copy(residual1), dim=Emb))
residual1 + ff_out

This capstone crosses shell boundaries to build a complete pipeline: data ingestion with coral, feature engineering, model definition with school, training, and evaluation. It demonstrates:

  • Importing from multiple shells in one project.
  • AD propagating through coral frame operations into school model parameters.
  • Named dimensions maintained consistently across the data and model layers.
import coral.{read_csv, Frame}
import school.{Linear, Adam, train_step}
import nautilus.stats.{mean, std}
// Load and normalize
let raw = read_csv("dataset.csv")
let features = (raw.select(Feature_cols) - mean(raw, dim=Rows)) / std(raw, dim=Rows)
let labels = raw.select(Label)
// Define model
let model = Linear[In: Features, Out: Classes]
let opt = Adam(lr=0.001)
// Train
for epoch in 0..50:
for batch in features.batches(size=64):
let loss_fn = fn(m) => cross_entropy(m.forward(batch.x), batch.y)
train_step(model, opt, loss_fn)

After working through all four capstones, you have exercised the major integration surfaces of the Chelis ecosystem: AD, typed dataframes, scientific computing, ML training, and cross-shell composition.