Skip to main content

Simple probability density function estimation for existing code

Project description

Randify

Randomize your existing code with randify. Evaluate how your code reacts to randomly distributed inputs and evaluate the probability distributions of your code outputs. Randify performs Monte Carlo simulations to estimate probability density distributions of your outputs. Designed for simple syntax.

Full API documentation at https://jonasnebl.github.io/randify/randify.html

Installation

Install randify from PyPI:

$ pip install randify

Quick guide

Randify works on functions with any number and type of input and output arguments. In this quick example we have a function y1, x2 = f(x1, x2) returning the sum x1 + x2 and the product x1 * x2 of two arguments x1 and x2. For more in-depth examples check-out the Jupyter-Notebooks in examples.

def f(x1, x2):
    return x1 + x2, x1 * x2

x1 = 1
x2 = 2
y1, x2 = f(x1, x2)

Now we want to evaluate how random inputs x1 and x2 influence the results y1and y2 using randify. We can do this using two steps.

  1. Define x1and/or x2as a RandomVariable. For defining the RandomVariable you need to pass a function that generates random samples of this RandomVariables. In this example, we use functions from numpy's random module, but you can also define custom functions (see the examples for that).
  2. Call the randify function wrapper with the RandomVariables as arguments.
from randify import randify, RandomVariable
import numpy as np

def f(x1, x2):
    return x1 + x2, x1 * x2

x1 = RandomVariable(np.random.normal, loc=0, scale=1)
x2 = RandomVariable(np.random.uniform, low=-1, high=1)
y1, y2 = randify(f)(x1, x2)

y1and y2 are now also RandomVariables. You can calculate the resulting statistical measure like expected value or variance. To display the estimated probability distributions, you can use randify's plot_pdf function:

print(f"E[y1] = {y1.expected_value}")
print(f"Var[y1] = {y1.variance}")

from randify import plot_pdf
plot_pdf(x1=x1, x2=x2, y1=y1, y2=y2)

Documentation

Full API documentation can be found at https://jonasnebl.github.io/randify/randify.html. The documentation is generated automatically using pdoc.

$ pip install pdoc
$ pdoc randify --math

Formatting

ruff is used to format randify.

$ pip install ruff
$ ruff format

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

randify-0.1.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

randify-0.1.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file randify-0.1.0.tar.gz.

File metadata

  • Download URL: randify-0.1.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.4

File hashes

Hashes for randify-0.1.0.tar.gz
Algorithm Hash digest
SHA256 be551900f5f34eaabb0f26cb7cdb85074473917e9dbac31764de6899880f8771
MD5 e828e3de71372d4c897e889f76a576a7
BLAKE2b-256 9a9dbcda06fb5bd4f65400872941d14d6d679105a92838e9d8ba3bcb489ae07c

See more details on using hashes here.

File details

Details for the file randify-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: randify-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.11.4

File hashes

Hashes for randify-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 16b3b91b7c751dd5c98493139423a0fcede72eb2c39100be1f5cb5c3cc81fb59
MD5 103981cbb550cc8b13482ee569c87900
BLAKE2b-256 086e698a3fd346256df049b062d6bbc2840771ca1cfd38e8d142f963ac84f020

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