Skip to main content

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!

Metadata

Release files for tunafish 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tunafish 0.0.3
File Size Uploaded
tunafish-0.0.3.tar.gz 17.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tunafish 0.0.3
File Interpreter ABI Platform
tunafish-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 40.3 kB

Release files / tunafish-0.0.3.tar.gz

Download URL tunafish-0.0.3.tar.gz
Size 17.9 kB
Tags Source
SHA-256 checksum
How to use checksums
5b51a79da8c90648f1c109de65c1a421468467d162c443ef939f65753f0117c2
BLAKE2b-256 checksum
How to use checksums
811eaec1f85bd928e2f1ee92517b051da076dc6a5d9e213156cd529c0146e9af
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / tunafish-0.0.3-py3-none-any.whl

Download URL tunafish-0.0.3-py3-none-any.whl
Size 22.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
da4f49cbdc7a7e46d53360679597dd59807a6f06cecd10841aba1de81f4ee001
BLAKE2b-256 checksum
How to use checksums
6c97b17ff61f56795c15c567f791d4fc7b70d390734d054f4abbad113825e30b
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.3 This release

2 release files

0.0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page