Skip to content

Add analytical solutions, fast mean and variance calculations#50

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:feature-issue-2-analytical-solutions
Feb 12, 2026
Merged

Add analytical solutions, fast mean and variance calculations#50
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:feature-issue-2-analytical-solutions

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Contributor

Summary

This PR addresses issue #2 by implementing analytical solutions, fast mean calculations, and fast variance calculations for the financial models in DiffEqFinancial.jl.

New Functions Added

Geometric Brownian Motion (GBM):

  • gbm_mean(μ, u0, t) - E[X(t)] = x₀ exp(μt)
  • gbm_variance(μ, σ, u0, t) - Var[X(t)] = x₀² exp(2μt)(exp(σ²t) - 1)
  • gbm_std(μ, σ, u0, t) - Standard deviation

Ornstein-Uhlenbeck (OU):

  • ou_mean(a, r, u0, t) - E[X(t)] = r + (x₀ - r)exp(-at)
  • ou_variance(a, σ, t) - Var[X(t)] = (σ²/2a)(1 - exp(-2at))
  • ou_std(a, σ, t) - Standard deviation
  • ou_stationary_mean(r) - Long-run mean = r
  • ou_stationary_variance(a, σ) - Long-run variance = σ²/2a

Cox-Ingersoll-Ross (CIR):

  • cir_mean(κ, θ, u0, t) - E[r(t)] = θ + (r₀ - θ)exp(-κt)
  • cir_variance(κ, θ, σ, u0, t) - Full CIR variance formula
  • cir_std(κ, θ, σ, u0, t) - Standard deviation
  • cir_stationary_mean(θ) - Long-run mean = θ
  • cir_stationary_variance(κ, θ, σ) - Long-run variance = θσ²/2κ

Black-Scholes Log-Price:

  • bs_log_mean(r, σ, u0, t) - E[ln S(t)] = ln S₀ + (r - σ²/2)t
  • bs_log_variance(σ, t) - Var[ln S(t)] = σ²t
  • bs_log_std(σ, t) - Standard deviation

Heston Model:

  • heston_mean(μ, u0, t) - E[S(t)] = S₀ exp(μt) (independent of volatility path)
  • heston_variance_mean(κ, Θ, v0, t) - Mean of variance process (follows CIR)
  • heston_variance_variance(κ, Θ, σ, v0, t) - Variance of variance process

Benefits

  • Performance: These analytical functions provide exact results without Monte Carlo simulation
  • Accuracy: No discretization or sampling error
  • Convenience: Easy-to-use API for common moment calculations
  • Type Stability: All functions are type-stable (verified with JET.jl)

Tests Added

  • Unit tests verifying analytical formulas
  • Monte Carlo validation tests confirming convergence to analytical values
  • JET.jl static analysis tests for type stability

Test plan

  • All existing tests pass
  • New analytical solution tests pass
  • Monte Carlo validation tests verify formulas match simulation
  • JET.jl type stability tests pass

Closes #2

cc @ChrisRackauckas

🤖 Generated with Claude Code

@ChrisRackauckas-Claude ChrisRackauckas-Claude force-pushed the feature-issue-2-analytical-solutions branch from 0832ed9 to 66573d8 Compare February 12, 2026 13:20
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Contributor Author

Rebase and Documentation Update

Rebased onto current master (b4eed69) and made the following fixes:

Conflict Resolution

  • Resolved merge conflict in test/runtests.jl — kept upstream's GROUP-based test structure and integrated the analytical solution tests into the core test group
  • Moved JET type stability tests for analytical functions into test/nopre/jet_tests.jl (following upstream's nopre test separation pattern)
  • Kept upstream's explicit import style in src/DiffEqFinancial.jl (using specific imports from packages)

Formatting

  • Applied Runic.jl formatting to all changed files (fixes the Format Check CI failure)

Documentation

  • Added docs/src/analytics.md with dedicated documentation page for all analytical functions (GBM, OU, CIR, Black-Scholes, Heston)
  • Updated docs/pages.jl to include the new "Analytical Solutions" page in the docs navigation
  • The @autodocs in diffeqfinancial.md also picks up all exported functions automatically

Tests

  • All 106 tests pass locally (Interface Compatibility + Core Tests + Analytical Solutions + Explicit Imports + JET Tests + AllocCheck)
  • Docs build succeeds locally

Co-Authored-By: Chris Rackauckas accounts@chrisrackauckas.com

@ChrisRackauckas-Claude ChrisRackauckas-Claude force-pushed the feature-issue-2-analytical-solutions branch from 66573d8 to fbf30b5 Compare February 12, 2026 13:54
…models

This PR addresses issue SciML#2 by adding analytical solutions and fast moment
calculation functions for the supported financial models:

- GBM: gbm_mean, gbm_variance, gbm_std
- Ornstein-Uhlenbeck: ou_mean, ou_variance, ou_std, ou_stationary_mean, ou_stationary_variance
- CIR: cir_mean, cir_variance, cir_std, cir_stationary_mean, cir_stationary_variance
- Black-Scholes log-price: bs_log_mean, bs_log_variance, bs_log_std
- Heston: heston_mean, heston_variance_mean, heston_variance_variance

These functions provide fast, exact analytical calculations without requiring
Monte Carlo simulation, enabling significant performance improvements for
applications that only need moment estimates.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@ChrisRackauckas-Claude ChrisRackauckas-Claude force-pushed the feature-issue-2-analytical-solutions branch from fbf30b5 to fd61687 Compare February 12, 2026 14:30
@ChrisRackauckas ChrisRackauckas merged commit a1785a9 into SciML:master Feb 12, 2026
11 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add analytical solutions, fast mean calculations, fast variance calculations

2 participants