Skip to main content

StochOPy (STOCHastic OPtimization for PYthon) provides user-friendly routines to sample or optimize objective functions with the most popular algorithms.

Project description

StochOPy (STOCHastic OPtimization for PYthon) provides user-friendly routines to sample or optimize objective functions with the most popular algorithms.

Version:

1.1.1

Author:

Keurfon Luu

Web site:

https://github.com/keurfonluu/stochopy

Copyright:

This document has been placed in the public domain.

License:

StochOPy is released under the MIT License.

NOTE: StochOPy has been implemented in the frame of my Ph. D. thesis. If you find any error or bug, or if you have any suggestion, please don’t hesitate to contact me.

Features

StochOPy provides routines for sampling of a model parameter space:

  • Pure Monte-Carlo

  • Metropolis-Hastings algorithm

  • Hamiltonian (Hybrid) Monte-Carlo [1,2]

or optimization of an objective function:

  • Differential Evolution [3]

  • Particle Swarm Optimization [4,5]

  • Covariance Matrix Adaptation - Evolution Strategy [6]

Installation

The recommended way to install StochOPy is through pip:

pip install stochopy

Usage

First, import StochOPy and define an objective function (here Rosenbrock):

import numpy as np
from stochopy import MonteCarlo, Evolutionary

f = lambda x: 100*np.sum((x[1:]-x[:-1]**2)**2)+np.sum((1-x[:-1])**2)

You can define the search space boundaries if necessary:

n_dim = 2
lower = np.full(n_dim, -5.12)
upper = np.full(n_dim, 5.12)

Initialize the Monte-Carlo sampler:

max_iter = 1000
mc = MonteCarlo(f, lower = lower, upper = upper, max_iter = max_iter)

Now, you can start sampling with the simple method ‘sample’:

mc.sample(sampler = "hamiltonian", stepsize = 0.005, n_leap = 20, xstart = [ 2., 2. ])

Note that sampler can be set to “pure” or “hastings” too. The models sampled and their corresponding energies are stored in:

print(mc.models)
print(mc.energy)

Optimization is just as easy:

n_dim = 10
lower = np.full(n_dim, -5.12)
upper = np.full(n_dim, 5.12)
popsize = 4 + np.floor(3.*np.log(n_dim))
ea = Evolutionary(f, lower = lower, upper = upper, popsize = popsize, max_iter = max_iter)
xopt, gfit = ea.optimize(solver = "cmaes")
print(xopt)
print(gfit)

References

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

stochopy-1.1.1.tar.gz (24.3 kB view details)

Uploaded Source

File details

Details for the file stochopy-1.1.1.tar.gz.

File metadata

  • Download URL: stochopy-1.1.1.tar.gz
  • Upload date:
  • Size: 24.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for stochopy-1.1.1.tar.gz
Algorithm Hash digest
SHA256 95b93f2597eb261833b6616b0a3eac31bfa48ad0c39d9d611638fff4ce45a1c8
MD5 ce382e78011438c6b929c3c4bd1ab664
BLAKE2b-256 6ce407307cf03ac7146d75467fe4394efc4eee767aef792be2de55d0d3f2dfc1

See more details on using hashes here.

Supported by

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