Why modelx

An actuarial modeling system has to deliver four things at once: transparency, auditability, performance and maintainability. modelx approaches all four with a single principle: actuarial models are code — plain, readable Python.

Deployment & performance

Build in modelx; ship plain Python; compile it native when you need speed. No step requires a proprietary runtime.


graph LR
A(Build in modelx) -- "model.export()" --> B(Pure-Python package
runs without modelx) B -- mx2cy --> C(Native-compiled model)

Export to pure Python

Since modelx 0.22.0, Model.export turns any model into a self-contained Python package—plain code with no runtime dependency on modelx, deployable wherever Python runs:

>>> model.export("TermLife_ex")

>>> from TermLife_ex import mx_model    # no modelx needed

>>> mx_model.Projection.net_cf(10)
11.725013975627874

Some features are not supported in exported models—see Model.export for current limitations and Introducing the Export Feature for background.

Native compilation with modelx-cython

modelx-cython translates exported models to Cython and compiles them to native code with a single command:

$ mx2cy TermLife_ex

modelx-cython is at an early, experimental stage—see the modelx-cython repository for status, and Introducing modelx-cython for background and measurements.

Modeling in a GUI

GUI as Spyder plugin

spyder-modelx embeds modelx in Spyder, the open-source Python IDE: MxExplorer shows the model as a tree with each cell’s formula a click away, MxDataViewer inspects results as DataFrames, and the precedents/dependents pane traces values interactively.

spyder-modelx in Spyder: MxExplorer model tree, formula pane, MxDataViewer and dependency tracing

Watch the demo video to see it in action.

Governance & auditability

Dependency tracing

Every calculated value knows its precedents and dependents, so you can trace an entire projection from result to assumptions:

>>> Balance.preds(5)
[Model1.Space1.Balance(t=4)=400, Model1.Space1.Cashflow(t=5)=100]

>>> Balance.succs(4)
[Model1.Space1.Balance(t=5)=500]
Trace precedents
Trace dependents

Version control

Models are saved as plain Python text, so Git gives you meaningful diffs, history, branches and code review. Plain text is also what AI coding agents read—see modelx and AI agents.

Document integration

Docstrings written on models, spaces and cells render into full model documentation with Sphinx—HTML, PDF and other formats, with math, images and code samples. See lifelib for samples.

Model design & productivity

Automatic calculation order

Define formulas like you do on spreadsheets; modelx resolves the calculation order from their dependencies and caches results until they need recalculating. No run scripts to write:

>>> import modelx as mx

>>> @mx.defcells
... def Cashflow(t):
...     return 100

>>> @mx.defcells
... def Balance(t):
...     if t > 0:
...         return Balance(t-1) + Cashflow(t)
...     else:
...         return 0
    
>>> Balance(5)
500

Readable formulas

Formulas are ordinary Python functions—far more readable than spreadsheet formulas, with Python’s control flow, data structures and lambda expressions available.

Object-oriented

You build models from objects—Models, Spaces and Cells—with composition and inheritance as in object-oriented programming:


graph TD
A(Model1) --- B[Space1]
B --- C[Cells1]
B --- D[Space2]
D --- E(Cells2)

Parameterization

Write a space once, then apply it to arbitrary combinations of inputs without changing formula signatures:

>>> space.parameters = ("Rate", "Term")

>>> space[3, 10].Payment()
117.23050660515952

Parameterization

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.

Get started

pip install modelx
# or
conda install -c conda-forge modelx

Then follow the tutorial, or explore working actuarial models at lifelib.