---
title: "Computations that differentiate, debug, and document themselves"
description: "Jane Street's OCaml library Gradient_calculator represents numerical cost computations (margin, financing, capital) as expression trees, so one definition can be evaluated, automatically differentiated, debugged and documented."
url: "https://swe.quest/articles/computations-that-differentiate-debug-and-document-themselves-027e0f3894353813"
canonical: "https://blog.janestreet.com/computations-that-differentiate-debug-and-document-themselves/"
source: "Jane Street Tech Blog"
published: "2022-11-17"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 14
tags: ["functional-programming","algorithms","testing","debugging"]
---

# Computations that differentiate, debug, and document themselves

A post on [Jane Street Tech Blog](https://swe.quest/sources/jane-street.md), published 2022-11-17, 14 min read, Deep dive. Original: [https://blog.janestreet.com/computations-that-differentiate-debug-and-document-themselves/](https://blog.janestreet.com/computations-that-differentiate-debug-and-document-themselves/)

## Summary

Jane Street's OCaml library Gradient\_calculator represents numerical cost computations (margin, financing, capital) as expression trees, so one definition can be evaluated, automatically differentiated, debugged and documented. It uses forward-mode automatic differentiation. The evaluation cost stays roughly proportional to the cost of the function itself, and the team chose it over reverse-mode because their computations involve few nested chain-rule applications. The same tree structure produces expect-test-friendly printouts of every intermediate value, which catch bugs on unexercised branches, and generates LaTeX formulas so documentation stays in sync with the code. Existing tools like JAX, OWL and ocaml-torch were rejected for poor OCaml interop or a tensor-centric model that didn't fit equation-style computations.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Functional programming](https://swe.quest/tags/functional-programming.md) (Languages)
- [Algorithms](https://swe.quest/tags/algorithms.md) (Architecture & design)
- [Testing](https://swe.quest/tags/testing.md) (Engineering practices)
- [Debugging](https://swe.quest/tags/debugging.md) (Engineering practices)

## Prerequisites

- OCaml basics ([easy posts on Functional programming](https://swe.quest/tags/functional-programming.md?difficulty=easy))
- partial derivatives
- gradient descent

## Keywords

Gradient\_calculator, OCaml, automatic differentiation, forward-mode AD, LaTeX, expect tests, Jane Street

## Related posts

- [Writing a DSL for high performance parallel programming in OxCaml](https://swe.quest/articles/writing-a-dsl-for-high-performance-parallel-programming-in-oxcaml-5f7bd51c2038a2c3.md): Jane Street Tech Blog · 2026-11-06 · Hard · 17 min read · Compilers, Functional programming, Language design, Performance engineering, Hardware
  A Jane Street intern built a JIT compiler for an SPMD-style DSL in OxCaml so SIMD kernels for options pricing and risk checks can be written once as ordinary-looking OxCaml functions instead of architecture-specific C intrinsics.
- [What the interns have wrought, 2021 edition](https://swe.quest/articles/what-the-interns-have-wrought-2021-edition-0f76a8f694a74c7c.md): Jane Street Tech Blog · 2021-08-09 · Medium · 10 min read · Functional programming, Engineering culture, Developer tools, Observability
  Jane Street's 2021 dev internship (87 interns across New York, London and Hong Kong) is showcased through three projects.
- [What the interns have wrought, 2025 edition](https://swe.quest/articles/what-the-interns-have-wrought-2025-edition-eda7f47e78fb3ca5.md): Jane Street Tech Blog · 2025-08-27 · Hard · 12 min read · Functional programming, Memory management, Query engines, Type systems, Performance engineering
  Jane Street's 2025 intern projects include an index-aware evaluator for JSQL, the company's internal SQL dialect, which over-approximates key ranges from WHERE clauses and specializes execution to Map, Hashtbl or multi-index containers, giving ~700x and ~30x speedups on 3.8M-row benchmarks.
- [Flex Model: an event-driven feature engineering system](https://swe.quest/articles/flex-model-an-event-driven-feature-engineering-system-b101b9ddf0c5a739.md): Jane Street Tech Blog · 2026-10-13 · Medium · 13 min read · Functional programming, System design, Performance engineering, Machine learning, Memory management
  Flex Model is Jane Street's OCaml framework that turns market events into feature vectors for neural-network trading models, replacing a 17k-line monolith where adding one feature touched nineteen files.
- [What the interns have wrought, 2022 edition](https://swe.quest/articles/what-the-interns-have-wrought-2022-edition-0f4dcf7e53f3e8d4.md): Jane Street Tech Blog · 2022-08-25 · Medium · 8 min read · Functional programming, Testing, Performance engineering, Developer tools, Engineering culture
  Three Jane Street intern projects from 2022 show substantial tooling work in OCaml. magic-trace gained a sampling mode that can cover 20 seconds to 20 minutes on Intel or AMD hardware, plus multi-process tracing, CPU-frequency transition reporting and a perf dlfilter backend that speeds trace decoding by 10–50%.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
