Skip to main content

A simple interface for solving systems of linear equations

Project description

Solving systems of linear equations

The purpose of this code is to aid in expressing and solving sets of equations using Python.

This tool will take a textual description of the equations and then run the solver iteratively until it converges to a solution.

The solver provides the following choices for solving: - Gauss-Seidel - Newton-Raphson - Broyden

It also uses parts of sympy to aid in parsing the equations.

The initial motivation for this tool was to solve economic models based on Stock Flow Consistent (SFC) models.

Installation

::

pip3 install pysolve

Usage

from pysolve3.model import Model
from pysolve3.utils import round_solution,is_close

model = Model()

model.set_var_default(0)
model.var('Cd', desc='Consumption goods demand by households')
model.var('Cs', desc='Consumption goods supply')
model.var('Gs', desc='Government goods, supply')
model.var('Hh', desc='Cash money held by households')
model.var('Hs', desc='Cash money supplied by the government')
model.var('Nd', desc='Demand for labor')
model.var('Ns', desc='Supply of labor')
model.var('Td', desc='Taxes, demand')
model.var('Ts', desc='Taxes, supply')
model.var('Y', desc='Income = GDP')
model.var('YD', desc='Disposable income of households')

# This is a shorter way to declare multiple variables
# model.vars('Y', 'YD', 'Ts', 'Td', 'Hs', 'Hh', 'Gs', 'Cs',
#            'Cd', 'Ns', 'Nd')
model.param('Gd', desc='Government goods, demand', initial=20)
model.param('W', desc='Wage rate', initial=1)
model.param('alpha1', desc='Propensity to consume out of income', initial=0.6)
model.param('alpha2', desc='Propensity to consume o of wealth', initial=0.4)
model.param('theta', desc='Tax rate', initial=0.2)

model.add('Cs = Cd')
model.add('Gs = Gd')
model.add('Ts = Td')
model.add('Ns = Nd')
model.add('YD = (W*Ns) - Ts')
model.add('Td = theta * W * Ns')
model.add('Cd = alpha1*YD + alpha2*Hh(-1)')
model.add('Hs - Hs(-1) =  Gd - Td')
model.add('Hh - Hh(-1) = YD - Cd')
model.add('Y = Cs + Gs')
model.add('Nd = Y/W')

# solve until convergence
for _ in xrange(100):
    model.solve(iterations=100, threshold=1e-3)

    prev_soln = model.solutions[-2]
    soln = model.solutions[-1]
    if is_close(prev_soln, soln, rtol=1e-3):
        break

print round_solution(model.solutions[-1], decimals=1)
For additional examples, view the iPython notebooks at

http://nbviewer.ipython.org/github/kennt/monetary-economics/tree/master/

Tutorial

A short tutorial with more explanation is available at

http://nbviewer.ipython.org/github/kennt/monetary-economics/blob/master/extra/pysolve%20tutorial.ipynb

Project details


Download files

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

Source Distribution

pysolve3-0.1.5.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

pysolve3-0.1.5-py3-none-any.whl (28.0 kB view details)

Uploaded Python 3

File details

Details for the file pysolve3-0.1.5.tar.gz.

File metadata

  • Download URL: pysolve3-0.1.5.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for pysolve3-0.1.5.tar.gz
Algorithm Hash digest
SHA256 2d7402f362f2320e070fad3a8524f5533718e8e85bc984dea6c0cb7a88b30962
MD5 20882a06ddb90c15685d1739d69b8fdf
BLAKE2b-256 6912280b2e4817199e157260b7221e3530d35ee0714482139637d9054d882bb9

See more details on using hashes here.

File details

Details for the file pysolve3-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: pysolve3-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 28.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for pysolve3-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d5fb940657594198bdc66955ea97fbc51fffc578a01bd818e2c5f6923cc19fbb
MD5 01d359022d00446d953d964db0c9455b
BLAKE2b-256 274130b9b4c14a3b242fde11a2b36844b25ae59a48acd407e39bb35e8c350a2e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page