Skip to main content

Numerical optimization/minimization

Project description

optipy

CircleCI codecov Codacy grade PyPi Version GitHub stars

optipy contains a generic optimization/minimization method. Its creation was motivated by the absence of an implementation of Newton's method with a custom Hessian solver in SciPy (see this bug).

The mandatory Rosenbrock example:

a = 1.0
b = 100.0

def fun(x):
    return (a-x[0])**2 + b*(x[1] - x[0]**2)**2

def jac(x):
    return numpy.array([
        -2*(a-x[0]) - 4*b*(x[1] - x[0]**2) * x[0],
        2*b*(x[1] - x[0]**2)
        ])

def hess_inv(x, grad):
    hess = numpy.array([
        [2 + 8*b*x[0]**2 - 4*b*(x[1] - x[0]**2), -4*b*x[0]],
        [-4*b*x[0], 2*b]
        ])
    return -numpy.linalg.solve(hess, grad)

sol = optipy.minimize(
    fun=fun,
    x0=[-1.0, 3.5],
    jac=jac,
    get_search_direction=hess_inv,
    atol=1.0e-5
    )

This is basically the exact Newton method. When setting get_search_direction to lambda x, grad: -grad, one gets the steepest descent method. For larger computations, one will typically replace this with a tailored solver, e.g., a preconditioned Krylov solver.

The return type is largely compatible with SciPy's generic return type, OptmizeResult.

Installation

optipy is available from the Python Package Index, so simply do

pip install -U optipy

to install or upgrade. Use sudo -H to install as root or the --user option of pip to install in $HOME.

Testing

To run the optipy unit tests, check out this repository and type

pytest

Distribution

To create a new release

  1. bump the __version__ number,

  2. publish to PyPi and tag on GitHub:

    $ make publish
    

License

optipy is published under the MIT license.

Project details


Release history Release notifications

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
optipy-0.1.0-py2.py3-none-any.whl (3.9 kB) Copy SHA256 hash SHA256 Wheel py2.py3
optipy-0.1.0.tar.gz (3.9 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page