modelx: Actuarial Models as Code
modelx is the open-source actuarial modeling system for Python. Build transparent, auditable models with spreadsheet-like formulas — dependency-traced, version-controlled with Git, and readable by humans and AI agents alike. Deploy them as self-contained pure-Python packages that run without modelx, and compile them to native code with modelx-cython.
General-purpose at its core, modelx is developed with actuarial and financial modeling as its primary domain. It powers lifelib, the open-source library of actuarial model examples. A GUI for modelx, spyder-modelx, is available as a plugin for the Spyder IDE.
In continuous development since 2017 · LGPLv3 · Distributed on PyPI and conda-forge
Build a model with formulas
import modelx as mx
model = mx.new_model(name="TermLife")
proj = mx.new_space(name="Projection")
proj.premium_pp = 120 # annual premium per policy
proj.sum_assured = 50_000
proj.q = 0.002 # mortality rate
proj.lapse = 0.05 # lapse rate
@mx.defcells
def pols_if(t):
"""Policies in force at t"""
return 1.0 if t == 0 else pols_if(t - 1) * (1 - q - lapse)
@mx.defcells
def premiums(t):
return premium_pp * pols_if(t)
@mx.defcells
def claims(t):
return sum_assured * q * pols_if(t)
@mx.defcells
def net_cf(t):
return premiums(t) - claims(t)Run it, trace it, ship it
>>> net_cf(10) # calculation order resolved automatically
11.725013975627874
>>> net_cf.preds(10) # trace what this value depends on
[TermLife.Projection.premiums(t=10)=70.3500838537673,
TermLife.Projection.claims(t=10)=58.62506987813943]
>>> mx.write_model(model, "TermLife") # plain text: diff it, Git it
>>> model.export("TermLife_ex") # self-contained pure-Python package
>>> from TermLife_ex import mx_model # runs without modelx
>>> mx_model.Projection.net_cf(10)
11.725013975627874- Automatic dependency resolution — no run scripts to write.
- Full tracing for validation — every value knows its precedents and dependents.
- Models saved as plain text — ready for Git and AI agents.
- Exports as a pure-Python package with no modelx dependency.
- Exported models compile to native code with modelx-cython.
Install
pip install modelx
# or
conda install -c conda-forge modelxPrefer a zero-setup Windows bundle? Get the lifelib + modelx distribution →
Features
Governance & auditability
Dependency tracing
Dependency tracing is an essential feature for checking and validating models. You can check what other values each calculated value is using, and also what other values it is used by. Read More>>
Version control
modelx models can be saved as text files written in Python syntax, which means you can take full advantage of modern version control systems, such as Git. Read More>>
Document integration
Document model components, such as spaces and cells by their docstrings within their definitions, render the docstrings nicely by Sphinx, a documentation generator, into html, pdf or other formats. No need to maintain a separate model document. Read More>>
Model design & productivity
Automatic calculation order
Build models just by writing formulas like you do on spreadsheets. No run scripts or orchestration code to write—modelx resolves the execution order from formula dependencies. Read More>>
Readable formulas
Define formulas by writing Python functions and assign them to Cells. Formulas are evaluated when they are called for the first time. Lambda expression is also supported. Read More>>
Object-oriented
modelx is object-oriented, which means you create, access or make changes to objects, such as models, spaces and cells. modelx features composition and inheritance mechanisms common in Object-oriented programming (OOP). Read More>>
Parameterization
You can apply the same set of calculations to multiple data sets associated with parameters. This can be achieved by parameterizing spaces. Read More>>
Excel Interface
Excel files are great for storing data of relatively small sizes. You can create new spaces and populate new cells in the space with data from Excel files. Read More>>
GUI as Spyder plugin
Spyder is a popular open-source Python IDE. Spyder plugin for modelx adds custom IPython consoles and GUI widgets to use modelx with Spyder more intuitively. Read More>>
Deployment & performance
Export to pure Python
Export any model as a self-contained Python package with no runtime dependency on modelx. Build in modelx, deploy as plain Python wherever Python runs. Read More>>
Native compilation with modelx-cython
Exported models can be cythonized and compiled to native code with modelx-cython for faster execution. Read More>>