Skip to content
Sarthak Bagaria
All model code

quant/src/lib.rs

Pricing models behind the figures in the stochastic calculus notes.

//! Pricing models behind the figures in the stochastic calculus notes.//!//! The crate is compiled twice from the same source, which is the whole point//! of it existing://!//!   * natively, by `src/bin/figures.rs`, which evaluates every figure once and//!     writes TikZ coordinates for the PDF build and a JSON snapshot for the web//!     build;//!   * to `wasm32-unknown-unknown`, whose exports (see [`wasm`]) the site calls//!     when a reader moves a slider.//!//! So a figure in the PDF and the same figure on the site are guaranteed to be//! the same numbers out of the same code, and a model only has to be written,//! checked and got right once.//!//! Nothing here imports a numerics crate. Every routine a figure needs is short//! enough to write out, and writing it out is the point — a reader who wants to//! know what the picture is actually doing can read it. pub mod arbitrage;pub mod black;pub mod calibration;pub mod cms;pub mod credit;pub mod crosscurrency;pub mod curve;pub mod dependence;pub mod estimation;pub mod events;pub mod generator;pub mod heston;pub mod hjm;pub mod hullwhite;pub mod localvol;pub mod lsv;pub mod marketmaking;pub mod marketmodels;pub mod measure;pub mod numerics;pub mod pathwise;pub mod quasigaussian;pub mod risk;pub mod sabr;pub mod smile;pub mod svi;pub mod transform;pub mod special; #[cfg(target_family = "wasm")]pub mod wasm; #[cfg(not(target_family = "wasm"))]pub mod source; #[cfg(not(target_family = "wasm"))]pub mod tikz; #[cfg(test)]mod chapter_reference_tests {    /// No chapter numbers in this crate's prose.    ///    /// The notes get reordered — chapters are inserted, split and moved — and    /// every chapter number written by hand silently starts pointing    /// somewhere else when they are. The failure is invisible: the sentence still reads    /// perfectly. An audit of these files after one such reordering found    /// roughly forty references that had drifted.    ///    /// The notes solve this with a generated macro, `\chapref{slug}`, because    /// LaTeX can look a number up. Doc comments cannot, so they take the other    /// route and name the chapter instead: "the local volatility chapter" is    /// still correct after any reordering, and a reader can tell at a glance    /// whether it is right, which is not true of a number.    #[test]    fn no_doc_comment_names_a_chapter_by_number() {        let mut offenders = Vec::new();         for entry in std::fs::read_dir(concat!(env!("CARGO_MANIFEST_DIR"), "/src")).unwrap() {            let path = entry.unwrap().path();            if path.extension().and_then(|e| e.to_str()) != Some("rs") {                continue;            }            let body = std::fs::read_to_string(&path).unwrap();            for (n, line) in body.lines().enumerate() {                if !line.trim_start().starts_with("//") {                    continue;                }                let lower = line.to_lowercase();                for (at, _) in lower.match_indices("chapter") {                    let rest = lower[at + "chapter".len()..].trim_start_matches('s');                    if rest.trim_start().starts_with(|c: char| c.is_ascii_digit())                        && rest.len() != rest.trim_start().len()                    {                        offenders.push(format!(                            "{}:{}: {}",                            path.file_name().unwrap().to_string_lossy(),                            n + 1,                            line.trim()                        ));                    }                }            }        }         assert!(            offenders.is_empty(),            "chapter numbers written by hand in {} place(s):\n{}\n\nName the \             chapter instead — \"the local volatility chapter\" — so that \             reordering the notes cannot make it wrong.",            offenders.len(),            offenders.join("\n")        );    }}