montecarlo

    Reproducible financial Monte Carlo simulation framework in MoonBit

    monte-carlo
    finance
    pricing
    risk-management
    stochastic-simulation
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    2 months ago
    Downloads
    31

    No dependencies

    #MoonBit Financial Monte Carlo

    License CI

    moonbit-montecarlo is a deterministic financial simulation toolkit written in MoonBit. It combines pseudo-random and quasi-random sampling, stochastic processes, derivative pricing, portfolio analytics, risk measurement, calibration, and auditable result export in a modular library layout.

    The design goal is a small, inspectable quantitative core: inputs are validated at package boundaries, numerical algorithms expose explicit parameters, and deterministic seeds make simulation results easy to reproduce in tests and operational reports.

    #Capabilities

    • Deterministic Xorshift128+, PCG32, and SplitMix64 random-number engines.
    • Halton, Sobol (up to 32 dimensions), and rank-1 lattice quasi-Monte Carlo sequences.
    • GBM, Vasicek, CIR, Ornstein-Uhlenbeck, Heston, SABR, Merton jump diffusion, and Milstein process primitives.
    • Antithetic, control-variate, importance, stratified, and Latin-hypercube variance reduction.
    • European, Asian, barrier, lookback, cliquet, rainbow, and Longstaff–Schwartz American option pricing.
    • Historical, parametric, and Monte Carlo VaR; expected shortfall; backtesting; stress testing; credit risk; and risk attribution.
    • Return analytics, rolling statistics, drawdown, covariance/correlation, portfolio valuation, scenario shocks, and target-weight rebalancing.
    • Discount-curve and implied-volatility calibration with validation and diagnostics.
    • JSON, CSV, Markdown, HTML, and deterministic checksum-based audit output.

    #Architecture

    core ───────────────┬── qmc ────────────────┐ ├── stochastic ─────────┤ └── multi_asset ────────┤ ▼ variance_reduction ─────────────────────── pricing │ analytics ─── portfolio ─── calibration ─── risk │ ▼ audit ── cli

    The repository currently contains 6,064 non-empty lines of production MoonBit source across 69 .mbt files and 1,071 non-empty lines of test source across 25 test files. Generated files under _build are excluded from these figures, and test files are reported separately. Physical line counts are 6,526 and 1,143 respectively.

    PackageResponsibilityProduction LOC
    lib/coreRandom engines, distributions, statistics, paths, matrices1,138
    lib/stochasticStochastic differential-process simulators609
    lib/pricingDerivative contracts and Monte Carlo/LSM pricing619
    lib/analyticsReturns, rolling statistics, risk-adjusted metrics614
    lib/riskVaR, CVaR, backtesting, stress, credit, attribution463
    lib/calibrationCurves, implied volatility, GBM calibration, bootstrap411
    lib/portfolioPosition validation, valuation, scenarios, rebalancing391
    lib/qmcLow-discrepancy sequence generators321
    lib/engineValidated simulation summaries and streaming ledgers506
    lib/auditReproducible audit and report exporters308
    lib/multi_assetCorrelation, Cholesky, and copula utilities181
    lib/variance_reductionSampling variance reduction methods195
    benchmarksDeterministic release workload suite205
    lib/cliExecutable batch runner103

    #Quick start

    Install the current stable MoonBit toolchain, then run the project checks:

    moon version --all moon update moon check --target all moon test --target all moon test --target native --deny-warn moon fmt --check

    Run the example batch simulation:

    moon run lib/cli

    Run the deterministic release benchmark suite:

    moon run --target native --release benchmarks

    The benchmark workload and a captured local result are documented in docs/benchmarks.md. Numerical algorithm provenance, including the Sobol parameter sources, is documented in docs/algorithm-notes.md.

    #Reproducibility

    Simulation APIs accept an explicit RngEngine seed. The audit package records workload dimensions and FNV-1a-style 64-bit checksums for path data and result samples. For numerical comparisons, use the same toolchain, target, seed, and time grid; floating-point price estimates should be compared with a tolerance while checksums can be compared exactly.

    The lib/engine package also provides convergence reports, pooled estimates, and an incremental EstimateLedger for simulations that are split across workers or checkpoints.

    #Testing and continuous integration

    The test suite covers normal workflows, invalid inputs, empty and singleton samples, degenerate matrices, zero-volatility processes, deterministic seed replay, and native-target execution. GitHub Actions runs formatting, interface-generation, type checks, WebAssembly tests, native tests, and release benchmark smoke checks on Linux, macOS, and Windows.

    #Repository layout

    lib/ MoonBit library packages benchmarks/ Deterministic executable benchmark workloads docs/ User-facing benchmark and design notes .github/workflows/ Continuous integration LICENSE Apache License 2.0

    #License

    This project is distributed under the Apache License 2.0.