lmfinance guide

This guide explains how lmfinance is put together and how to think in its domain model: the evaluation date, curves, instruments, models, engines and results, the market conventions that are fixed inside the host, and its limits. For commands and copyable recipes see the skill page; for every signature see the reference.

Overview

lmfinance is one statically linked executable that contains QuantLib 1.44 and the lmcode-cpp virtual machine. It has no user-facing subcommands. An LMScript program contains a block that starts with the _lmcode_ marker naming cli="lmfinance"; lmscript compiles the block against the schema embedded in the binary, and run_lmcode hands the compiled program to the host, which executes it and returns one result envelope. Because the schema is typed, a wrong method name or argument type is a compile error before any pricing happens.

The host is part of a family of scripted scientific hosts. It computes numbers and writes text tables; drawing is left to other hosts such as lmplot and lmdraw, which a script can call from further _lmcode_ blocks in the same file. The gallery in the repository works this way: lmfinance writes data files, a second block draws them.

Workflow

  1. Write a .lms script with one _lmcode_ block for lmfinance, declaring install="github:lmctl-ai/lmfinance@latest" so the release binary is fetched when it is missing.
  2. Inside the block, set the evaluation date if the default is not wanted, build market objects, instruments, models and engines, and compute values.
  3. Assert expected relations with lmf.assert_close and lmf.check, so a wrong number stops the run.
  4. Export what the rest of the script needs: text tables through save_csv, save_profile or lmf.save_text, or values through lmcode_return_set and lmcode_return_put.
  5. After the block, call run_lmcode, test ok, and continue in LMScript: read the files, hand them to a plotting host, or report the returned values.

Domain model

The seven context objects are namespaces over one shared host instance. Objects they return are plain values: every pricing call builds the QuantLib objects it needs and discards them, so each call is a pure function of its arguments and the evaluation date. Only yield curves keep QuantLib state, as immutable term structures.

ConceptTypeCreated byRole
Evaluation datehost statemarket.set_dateThe valuation date for everything; default 2024-01-02
Yield curveCurvemarket.flat_curve, or a CurveBuilder from market.zero_curve or market.bootstrap_curveDiscounting and forward rates for bonds, swaps, caps, floors and swaptions
Payoff and exerciseOptioninstrument.european_*, american_*, barrier_*, asian_*What the option pays; carries no market data
Equity marketModelmodel.black_scholes, model.heston, HestonFit.fitSpot, rate, dividend yield and volatility dynamics
Numerical methodEnginepricing.analytic, binomial, binomial_tree, finite_diff, monte_carlo, bawHow the option is priced
ValuationResultpricing.price(option, model, engine)Price plus the Greeks that engine provides
Fixed incomeBond, Swap, CapFloor, Swaptioninstrument.bond, zero_bond, swap, cap, floor, swaptionValued by pricing functions that take a Curve (and a flat Black volatility for caps, floors and swaptions)
Sample setSeriesrisk.series(path)Empirical quantiles, value at risk and expected shortfall of a P/L or return file
Calibration jobHestonFitcalibration.heston(spot, rate, dividend)Collects volatility quotes and fits the five Heston parameters

Option, Model and Engine are deliberately separate. The same option can be priced under two models or by several engines, and Model.with_spot, with_rate and with_vol return modified copies, which is how scenario grids and profiles are written in a loop.

Market conventions

Engines and Greeks

EnginePricesGreeks on the Result
analytic()European options under Black-Scholes and Heston; barrier options (Reiner-Rubinstein closed form, continuous monitoring, no rebate, Black-Scholes only); geometric Asian optionsFor European Black-Scholes options: delta, gamma, vega, theta, rho. Barrier options have no engine Greeks
binomial(steps), binomial_tree(tree, steps)European and American options; trees crr, jr, tian and lr (Leisen-Reimer, which uses an odd step count); 2 to 20000 stepsdelta, gamma, theta
finite_diff(time_steps, grid_points)European and American optionsdelta, gamma, theta
monte_carlo(paths, seed)European options and arithmetic Asian options (with the geometric option as control variate); 16 to 100000000 pathsNone; stderr gives the standard error
baw()Barone-Adesi-Whaley approximation for American optionsNone

Asking a Result for a Greek its engine did not produce is a run error that points to the risk functions. risk.delta, gamma, vega, rho and theta are central finite differences that work with any engine on a Black-Scholes model: the spot is bumped by 1%, volatility by 0.001, the rate by 0.0001 and theta uses one day. With Monte Carlo the bumped runs reuse the same seed, so they share random numbers. Units are vega per 1.00 of volatility, rho per 1.00 of rate and theta per year.

Monte Carlo uses QuantLib pseudo-random numbers (Mersenne Twister with the inverse cumulative normal), antithetic paths and one time step. The same seed reproduces the same result; the seed must be at least 1.

Calibration

calibration.implied_vol(option, model, engine, price) solves for the Black-Scholes volatility with Brent's method in the range 0.0001 to 5, using the chosen engine; a price outside the reachable range is an error. calibration.heston collects quotes of strike, maturity and implied volatility (at least five, since the model has five parameters), optionally takes a starting guess, and fit() runs Levenberg-Marquardt on implied-volatility errors with the analytic Heston engine. fit() returns a Heston Model and error() the remaining error. A fit that stops without converging is a run error, not a silent result.

Architecture

Limits

Related pages