Skip to main content

Tunafish

Project description

Tunafish

Autotuner For Python Functions

Tunafish is an autotuner for plain ol' Python functions. Under the hood, it uses genetic algorithms to determine which arguments maximize a function's return value (i.e. it's fitness); however, Tunafish eliminates the need to think about genetic algorithms. Instead, you can focus more on just writing code.

To use Tunafish, your objective function must satisfy the following two conditions:

  1. Arguments must be annotated as primitives types, like float, int, str, and bool, typing.Text, etc.
  2. The return value must be expressed as a single float (i.e. a fitness value).

Example: Automated Trading

Consider a function whose objective is to place orders to buy and sell stocks. The input arguments are aggression, which regulates the a minimum amount of time between orders, and window, which determines how far back the trading algorithm should looks when deciding when take action. The return value is simply the net gain or loss generated while trading, which is used as a proxy for fitness.

Define The Objective Function

from example_project import create_trader, load_historical_trading_data

trader = create_trader()
training_data = load_historical_trading_data(start, stop, interval)

def trade(aggression: float, window: int) -> float:
  gains = trader.trade(training_data, aggression, window)
  return gains  # AKA fitness

Tune it...

from tunafish import FunctionTuner

tuner = FunctionTuner()
arguments = tuner.tune(trade, options={
  'aggression': {'min': 0.01, 'max': 1.0},
  'window': {'min': 5, 'max': 20}
})

More Examples

Working examples can be found in tunafish.examples. The "basic" and "early_stopping" examples differ only in that "early_stopping" shows you how to control a bit more of the internals of the genetic algorithm. In particular, we tell it to exit the training loop early if we reach a fitness goal before all 500 epochs have run. Running these examples should generate a plot, showing convergence of fitness versus time.

Max Fitness Per Epoch Graph

Running Examples

Just do python -m tunafish.examples.basic!

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

tunafish-0.0.2.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

tunafish-0.0.2-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file tunafish-0.0.2.tar.gz.

File metadata

  • Download URL: tunafish-0.0.2.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.4

File hashes

Hashes for tunafish-0.0.2.tar.gz
Algorithm Hash digest
SHA256 4759fb6f10787f98f648948b551a0da76c91f1ef81d189bae5d6dd668d72dddb
MD5 c9f218e9d71c67d087d898e86a47f7c5
BLAKE2b-256 a59a0a492dfd027a5da850a6d509e427c97c0ab8de833d7f04c916a4d880af25

See more details on using hashes here.

File details

Details for the file tunafish-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: tunafish-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.4

File hashes

Hashes for tunafish-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3dd58addfeb650b3b5448053da30c21d2c8ba4d7104b8aed93b3b508b3371530
MD5 92137e3f9af68af644888797a5971f31
BLAKE2b-256 6245d13e521e45e3b6282dd9fee7e223b695cf2151b703f35e0523bba7d2c0ed

See more details on using hashes here.

Supported by

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