modelx
======
*Use Python like a spreadsheet!*
.. image:: https://github.com/fumitoh/modelx/actions/workflows/python-package.yml/badge.svg
:target: https://github.com/fumitoh/modelx/actions/workflows/python-package.yml
.. image:: https://img.shields.io/pypi/pyversions/modelx
:target: https://pypi.org/project/modelx/
.. image:: https://img.shields.io/pypi/v/modelx
:target: https://pypi.org/project/modelx/
.. image:: https://img.shields.io/pypi/l/modelx
:target: https://github.com/fumitoh/modelx/blob/master/LICENSE.LESSER.txt
.. Overview Begin
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.
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
.. _Numpy: https://numpy.org/
.. _pandas: https://pandas.pydata.org/
.. _SciPy: https://scipy.org/
.. _scikit-learn: https://scikit-learn.org/
.. _Git: https://git-scm.com/
.. _Sphinx: https://www.sphinx-doc.org
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.
.. code-block:: python
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:
.. code-block:: pycon
>>> 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:
.. code-block:: pycon
>>> 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``:
.. code-block:: pycon
>>> 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-2024, 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)
<https://github.com/fumitoh/modelx/blob/master/LICENSE.LESSER.txt>`_.
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
.. Overview End
Requirements
------------
* Python 3.7+
* NetwrkX 2.0+
* asttokens
* LibCST
* Pandas
* OpenPyXL
Raw data
{
"_id": null,
"home_page": "https://github.com/fumitoh/modelx",
"name": "modelx",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "model development",
"author": "Fumito Hamamura",
"author_email": "fumito.ham@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/c7/fb/ef5ec4a2bb2429f74448fba501a0901cb1402d9770c461044e6ebe6f87b7/modelx-0.28.0.tar.gz",
"platform": null,
"description": "modelx\n======\n*Use Python like a spreadsheet!*\n\n.. image:: https://github.com/fumitoh/modelx/actions/workflows/python-package.yml/badge.svg\n :target: https://github.com/fumitoh/modelx/actions/workflows/python-package.yml\n\n.. image:: https://img.shields.io/pypi/pyversions/modelx\n :target: https://pypi.org/project/modelx/\n\n.. image:: https://img.shields.io/pypi/v/modelx\n :target: https://pypi.org/project/modelx/\n\n.. image:: https://img.shields.io/pypi/l/modelx\n :target: https://github.com/fumitoh/modelx/blob/master/LICENSE.LESSER.txt\n\n\n.. Overview Begin\n\nWhat is modelx?\n---------------\n**modelx** is a numerical computing tool that enables you to\nuse Python like a spreadsheet by quickly defining cached functions.\nmodelx is best suited for implementing mathematical models expressed\nin a large system of recursive formulas,\nin such fields as actuarial science, quantitative finance and risk management.\n\nFeature highlights\n------------------\n**modelx** enables you to interactively\ndevelop, run and debug complex models in smart ways.\nmodelx allows you to:\n\n- Define cached functions as *Cells* objects by writing Python functions\n- Quickly build object-oriented models, utilizing prototype-based inheritance and composition\n- Quickly parameterize a set of formulas and get results for different parameters\n- Trace formula dependency\n- Import and use any Python modules, such as `Numpy`_, `pandas`_, `SciPy`_, `scikit-learn`_, etc..\n- See formula traceback upon error and inspect local variables\n- Save models to text files and version-control with `Git`_\n- Save data such as pandas DataFrames in Excel or CSV files within models\n- Auto-document saved models by Python documentation generators, such as `Sphinx`_\n- Use Spyder with a plugin for modelx (spyder-modelx) to interface with modelx through GUI\n\n.. _Numpy: https://numpy.org/\n.. _pandas: https://pandas.pydata.org/\n.. _SciPy: https://scipy.org/\n.. _scikit-learn: https://scikit-learn.org/\n.. _Git: https://git-scm.com/\n.. _Sphinx: https://www.sphinx-doc.org\n\n\nmodelx sites\n-------------\n\n========================== ===============================================\nHome page https://modelx.io\nBlog https://modelx.io/allposts\nDocumentation site https://docs.modelx.io\nDevelopment https://github.com/fumitoh/modelx\nDiscussion Forum https://github.com/fumitoh/modelx/discussions\nmodelx on PyPI https://pypi.org/project/modelx/\n========================== ===============================================\n\n\nWho is modelx for?\n------------------\n**modelx** is designed to be domain agnostic, \nso it's useful for anyone in any field.\nEspecially, modelx is suited for modeling in such fields such as:\n\n- Quantitative finance\n- Risk management\n- Actuarial science\n\n**lifelib** (https://lifelib.io) is a library of actuarial and\nfinancial models that are built on top of modelx.\n\nHow modelx works\n----------------\n\nBelow is an example showing how to build a simple model using modelx.\nThe model performs a Monte Carlo simulation to generate 10,000\nstochastic paths of a stock price that follow a geometric Brownian motion\nand to price an European call option on the stock.\n\n.. code-block:: python\n\n import modelx as mx\n import numpy as np\n\n model = mx.new_model() # Create a new Model named \"Model1\"\n space = model.new_space(\"MonteCarlo\") # Create a UserSpace named \"MonteCralo\"\n\n # Define names in MonteCarlo\n space.np = np\n space.M = 10000 # Number of scenarios\n space.T = 3 # Time to maturity in years\n space.N = 36 # Number of time steps\n space.S0 = 100 # S(0): Stock price at t=0\n space.r = 0.05 # Risk Free Rate\n space.sigma = 0.2 # Volatility\n space.K = 110 # Option Strike\n\n\n # Define Cells objects in MonteCarlo from function definitions\n @mx.defcells\n def std_norm_rand():\n gen = np.random.default_rng(1234)\n return gen.standard_normal(size=(N, M))\n\n\n @mx.defcells\n def stock(i):\n \"\"\"Stock price at time t_i\"\"\"\n dt = T/N; t = dt * i\n if i == 0:\n return np.full(shape=M, fill_value=S0)\n else:\n epsilon = std_norm_rand()[i-1]\n return stock(i-1) * np.exp((r - 0.5 * sigma**2) * dt + sigma * epsilon * dt**0.5)\n\n\n @mx.defcells\n def call_opt():\n \"\"\"Call option price by Monte Carlo\"\"\"\n return np.average(np.maximum(stock(N) - K, 0)) * np.exp(-r*T)\n\nRunning the model from IPython is as simple as calling a function:\n\n.. code-block:: pycon\n\n >>> stock(space.N) # Stock price at i=N i.e. t=T\n array([ 78.58406132, 59.01504804, 115.148291 , ..., 155.39335662,\n 74.7907511 , 137.82730703])\n\n >>> call_opt()\n 16.26919556999345\n\nChanging a parameter is as simple as assigning a value to a name:\n\n.. code-block:: pycon\n\n >>> space.K = 100 # Cache is cleared by this assignment\n\n >>> call_opt() # New option price for the updated strike\n 20.96156962064\n\nYou can even dynamically create multiple copies of *MonteCarlo*\nwith different combinations of ``r`` and ``sigma``,\nby parameterizing *MonteCarlo* with ``r`` and ``sigma``:\n\n.. code-block:: pycon\n\n >>> space.parameters = (\"r\", \"sigma\") # Parameterize MonteCarlo with r and sigma\n\n >>> space[0.03, 0.15].call_opt() # Dynamically create a copy of MonteCarlo with r=3% and sigma=15%\n 14.812014828333284\n\n >>> space[0.06, 0.4].call_opt() # Dynamically create another copy with r=6% and sigma=40%\n 33.90481014639403\n\n\nLicense\n-------\nCopyright 2017-2024, Fumito Hamamura\n\nmodelx is free software; you can redistribute it and/or\nmodify it under the terms of\n`GNU Lesser General Public License v3 (LGPLv3)\n<https://github.com/fumitoh/modelx/blob/master/LICENSE.LESSER.txt>`_.\n\nContributions, productive comments, requests and feedback from the community\nare always welcome. Information on modelx development is found at Github\nhttps://github.com/fumitoh/modelx\n\n\n.. Overview End\n\n\nRequirements\n------------\n* Python 3.7+\n* NetwrkX 2.0+\n* asttokens\n* LibCST\n* Pandas\n* OpenPyXL\n",
"bugtrack_url": null,
"license": "LGPLv3",
"summary": "Build and run complex models composed of formulas and data",
"version": "0.28.0",
"project_urls": {
"Homepage": "https://github.com/fumitoh/modelx"
},
"split_keywords": [
"model",
"development"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "587eb63147b588f1e8c22e69703a397f4fb093257615ef7a5ac72d39ef8d736b",
"md5": "1db53f00f5d30d4821ab08e337ddfab2",
"sha256": "ce2cca9ae8594fe39630eda9801af558eea033d702afee492e5431a914cf387e"
},
"downloads": -1,
"filename": "modelx-0.28.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1db53f00f5d30d4821ab08e337ddfab2",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 203964,
"upload_time": "2024-12-08T09:20:54",
"upload_time_iso_8601": "2024-12-08T09:20:54.279013Z",
"url": "https://files.pythonhosted.org/packages/58/7e/b63147b588f1e8c22e69703a397f4fb093257615ef7a5ac72d39ef8d736b/modelx-0.28.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c7fbef5ec4a2bb2429f74448fba501a0901cb1402d9770c461044e6ebe6f87b7",
"md5": "707a40881eaed3d0b840f4abb43ba45e",
"sha256": "91af1db5d76f121a4f35c21044741afcdebd4bbba6a19737bf55a591f123dbe4"
},
"downloads": -1,
"filename": "modelx-0.28.0.tar.gz",
"has_sig": false,
"md5_digest": "707a40881eaed3d0b840f4abb43ba45e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 180133,
"upload_time": "2024-12-08T09:20:55",
"upload_time_iso_8601": "2024-12-08T09:20:55.625058Z",
"url": "https://files.pythonhosted.org/packages/c7/fb/ef5ec4a2bb2429f74448fba501a0901cb1402d9770c461044e6ebe6f87b7/modelx-0.28.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-08 09:20:55",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "fumitoh",
"github_project": "modelx",
"travis_ci": true,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "modelx"
}