Skip to main content

Use Python like a spreadsheet!

https://github.com/fumitoh/modelx/actions/workflows/python-package.yml/badge.svg https://img.shields.io/pypi/pyversions/modelx https://img.shields.io/pypi/v/modelx https://img.shields.io/pypi/l/modelx

What is modelx?

modelx is a numerical computing tool that enables you to use Python like a spreadsheet by quickly defining cached functions. modelx is best suited for implementing mathematical models expressed in a large system of recursive formulas, in such fields as actuarial science, quantitative finance and risk management.

See also the GitHub Copilot instructions for modelx.

Feature highlights

modelx enables you to interactively develop, run and debug complex models in smart ways. modelx allows you to:

  • Define cached functions as Cells objects by writing Python functions

  • Quickly build object-oriented models, utilizing prototype-based inheritance and composition

  • Quickly parameterize a set of formulas and get results for different parameters

  • Trace formula dependency

  • Import and use any Python modules, such as Numpy, pandas, SciPy, scikit-learn, etc..

  • See formula traceback upon error and inspect local variables

  • Save models to text files and version-control with Git

  • Save data such as pandas DataFrames in Excel or CSV files within models

  • Auto-document saved models by Python documentation generators, such as Sphinx

  • Use Spyder with a plugin for modelx (spyder-modelx) to interface with modelx through GUI

  • Export models as Python modules independent of modelx

  • Translate exported models to Cython optimized code and compile them for performance improvement using Cython through modelx-cython

modelx sites

Home page

https://modelx.io

Blog

https://modelx.io/allposts

Documentation site

https://docs.modelx.io

Development

https://github.com/fumitoh/modelx

Discussion Forum

https://github.com/fumitoh/modelx/discussions

modelx on PyPI

https://pypi.org/project/modelx/

Who is modelx for?

modelx is designed to be domain agnostic, so it’s useful for anyone in any field. Especially, modelx is suited for modeling in such fields such as:

  • Quantitative finance

  • Risk management

  • Actuarial science

lifelib (https://lifelib.io) is a library of actuarial and financial models that are built on top of modelx.

How modelx works

Below is an example showing how to build a simple model using modelx. The model performs a Monte Carlo simulation to generate 10,000 stochastic paths of a stock price that follow a geometric Brownian motion and to price an European call option on the stock.

import modelx as mx
import numpy as np

model = mx.new_model()                  # Create a new Model named "Model1"
space = model.new_space("MonteCarlo")   # Create a UserSpace named "MonteCralo"

# Define names in MonteCarlo
space.np = np
space.M = 10000     # Number of scenarios
space.T = 3         # Time to maturity in years
space.N = 36        # Number of time steps
space.S0 = 100      # S(0): Stock price at t=0
space.r = 0.05      # Risk Free Rate
space.sigma = 0.2   # Volatility
space.K = 110       # Option Strike


# Define Cells objects in MonteCarlo from function definitions
@mx.defcells
def std_norm_rand():
    gen = np.random.default_rng(1234)
    return gen.standard_normal(size=(N, M))


@mx.defcells
def stock(i):
    """Stock price at time t_i"""
    dt = T/N; t = dt * i
    if i == 0:
        return np.full(shape=M, fill_value=S0)
    else:
        epsilon = std_norm_rand()[i-1]
        return stock(i-1) * np.exp((r - 0.5 * sigma**2) * dt + sigma * epsilon * dt**0.5)


@mx.defcells
def call_opt():
    """Call option price by Monte Carlo"""
    return np.average(np.maximum(stock(N) - K, 0)) * np.exp(-r*T)

Running the model from IPython is as simple as calling a function:

>>> stock(space.N)      # Stock price at i=N i.e. t=T
array([ 78.58406132,  59.01504804, 115.148291  , ..., 155.39335662,
        74.7907511 , 137.82730703])

>>> call_opt()
16.26919556999345

Changing a parameter is as simple as assigning a value to a name:

>>> space.K = 100   # Cache is cleared by this assignment

>>> call_opt()    # New option price for the updated strike
20.96156962064

You can even dynamically create multiple copies of MonteCarlo with different combinations of r and sigma, by parameterizing MonteCarlo with r and sigma:

>>> space.parameters = ("r", "sigma")   # Parameterize MonteCarlo with r and sigma

>>> space[0.03, 0.15].call_opt()  # Dynamically create a copy of MonteCarlo with r=3% and sigma=15%
14.812014828333284

>>> space[0.06, 0.4].call_opt()   # Dynamically create another copy with r=6% and sigma=40%
33.90481014639403

License

Copyright 2017-2026, Fumito Hamamura

modelx is free software; you can redistribute it and/or modify it under the terms of GNU Lesser General Public License v3 (LGPLv3).

Contributions, productive comments, requests and feedback from the community are always welcome. Information on modelx development is found at Github https://github.com/fumitoh/modelx

Requirements

  • Python 3.7+

  • NetwrkX 2.0+

  • asttokens

  • LibCST

  • Pandas

  • OpenPyXL

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modelx-0.33.0.tar.gz (241.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modelx-0.33.0-py3-none-any.whl (280.0 kB view details)

Uploaded Python 3

File details

Details for the file modelx-0.33.0.tar.gz.

File metadata

  • Download URL: modelx-0.33.0.tar.gz
  • Upload date:
  • Size: 241.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for modelx-0.33.0.tar.gz
Algorithm Hash digest
SHA256 caff39e239bf395dd7f96fdf360d7af48b855d751c4a68bee3fb35482180d3de
MD5 108ad53122f0c377c7d41a7fbcf7d51b
BLAKE2b-256 1e820df7665bf60eb60d34ee29e058ecd0c1486e256f0fe72787668dd0f75c4f

See more details on using hashes here.

File details

Details for the file modelx-0.33.0-py3-none-any.whl.

File metadata

  • Download URL: modelx-0.33.0-py3-none-any.whl
  • Upload date:
  • Size: 280.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for modelx-0.33.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0931c547af0c04136817516bc3f26640a3226af0c92f363400e90d348aab5100
MD5 2ca698233a8391344d4d05ff0b6f0df2
BLAKE2b-256 dd6fb0af7a380d134055427ee20c398ccb41c78d58783407ebe96429e5ce6675

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.33.0 This release

2 files

0.32.0

2 files

0.31.1

2 files

0.31.0

2 files

0.30.1

2 files

0.30.0

2 files

0.29.2

2 files

0.29.1

2 files

0.29.0

2 files

0.28.1

2 files

0.28.0

2 files

0.27.0

2 files

0.26.0

2 files

0.25.1

2 files

0.25.0

2 files

0.24.0

2 files

0.23.0

2 files

0.22.0

2 files

0.21.0

2 files

0.20.0

2 files

0.19.1

2 files

0.19.0

2 files

0.18.0

2 files

0.17.0

2 files

0.16.1

2 files

0.16.0

2 files

0.15.0

2 files

0.14.0

2 files

0.13.1

2 files

0.13.0

2 files

0.12.1

2 files

0.12.0

2 files

0.11.0

2 files

0.10.0

2 files

0.9.0

2 files

0.8.0

2 files

0.7.0

2 files

0.6.1

2 files

0.6.0

2 files

0.5.0

2 files

0.4.0

2 files

0.3.0

2 files

0.2.0

2 files

0.1.0

2 files

0.0.25

2 files

0.0.24

2 files

0.0.23

2 files

0.0.22

2 files

0.0.21

2 files

0.0.20

2 files

0.0.19

2 files

0.0.18

2 files

0.0.17

2 files

0.0.16

2 files

0.0.15

2 files

0.0.14

2 files

0.0.13

2 files

0.0.12

2 files

0.0.11

2 files

0.0.10

2 files

0.0.9

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page