Skip to content

Read a larger calculation

After the five lessons, choose a calculation you recognize. Read its function signatures first, then follow one input through the body. The hello-chelis source files are modules: their exported functions need callers, and running a file of definitions alone does not produce a worked calculation.

Linear regression: shapes through a prediction

Section titled “Linear regression: shapes through a prediction”

Open linreg.ch. Start with predict, which combines a matrix multiplication with a bias. Its input matrix has shape 64 by 64, the weight matrix 64 by 1, and the result 64 by 1. Trace those shapes through matmul.

The bias has only one element. Find the explicit insert that makes its shape fit the prediction before addition. This is the larger-program counterpart of the shape lesson: the function states the shape change instead of relying on implicit broadcasting.

Next read mse_loss. Follow pred into the subtraction, multiplication, and two reductions. The body sums squared errors; despite its name, it does not divide by the number of observations. When reading numerical code, check the expression as well as the function name.

Exercise: write down the shape after every operation in predict and mse_loss. Then locate the grad calls in sgd_step and identify which parameter each one differentiates.

Open blackscholes.ch and its test. This module uses Nautilus's normal cumulative distribution function to calculate a Black-Scholes call price.

Trace s (spot price), k (strike), r (interest rate), sigma (volatility), and t (time to expiry) through d1 and d2 into call_price. The delta function differentiates that price with respect to s; vega differentiates it with respect to sigma. This uses the same grad(f, wrt=parameter) form as the small squared-loss lesson.

Exercise: find the test inputs and each comparison's tolerance. Explain which input delta varies and which input vega varies, keeping the other four inputs fixed.

Open returnsrisk.ch and its test. Begin with simple_returns, which pairs each price with the next one and calculates their relative change.

Follow returns_frame to see how a tensor becomes a numeric column beside a ticker column in a Coral dataframe. Then read portfolio_sharpe, which extracts that numeric column and passes it to sharpe_ratio. The latter subtracts the risk-free return from the mean and divides by the sample standard deviation, using Nautilus functions.

Exercise: use the three prices in test_simple_returns to calculate both returns by hand. Then follow the same data path in the test that compares the dataframe calculation with the tensor calculation.

The lessons on this site are standalone files tested with the current compiler. The hello-chelis corpus has its own compiler and dependency pins in reef.toml and reef.lock. Those pins select the environment for the package; a newer compiler installation alone does not establish that the entire corpus works with it.

If you want to run the package's tests, use the setup instructions in its repository guide and preserve the declared pins. Read the tests alongside the modules first to choose the calculation you want to explore.