Skip to main content

Python bindings for shrew — random variable arithmetic

Project description

Shrew

Static C++ library with python bindings for random variable arithmetic.

Currently featured:

  • Arithmetic of normal random variables (e.g. X * Y - Z)
  • Probability density and cumulative density of arithmetic expressions with lazy numerical evaluation if no exact solution exists
  • Conditionals and marginals of multivariate normal random vectors
  • Gaussian process model with hyperparameter optimization

Installation

Python

pip install pyshrew

C++ (CMake)

Install the required dependencies first:

Ubuntu/Debian

sudo apt-get -y install libboost-dev libeigen3-dev libnlopt-cxx-dev

macOS

brew install eigen boost nlopt

Then build and install:

cd <PathToRepo>/shrew
cmake -S . -B build -DSHREW_PYTHON=OFF -DSHREW_TESTS=OFF
cmake --build build
cmake --install build

The library installs to the install directory. Use find_package(shrew) in your CMakeLists.txt (you may need to add the install path to CMAKE_PREFIX_PATH), then link with Shrew::shrew.

Development

Install system dependencies (see C++ section above), then either:

Editable install — reinstall after C++ changes:

pip install --no-build-isolation -e .
python -c "import pyshrew; print(dir(pyshrew))"

Python Examples

Random variable arithmetic

import pyshrew as ps

rv1 = ps.RandomVariable(ps.NormalDistribution(mean=-1.0, stddev=1.0))
rv2 = ps.RandomVariable(ps.NormalDistribution(mean=1.0, stddev=1.0))

# Cumulative distribution of compound random variable at 0.0
print((rv1 * rv2).pdist.cdf(0.0))

# Probability density distribution of compound random variable at 2.0
print((0.5 / rv1).pdist.cdf(2.0))

Random vector operations

import pyshrew as ps
import numpy as np

mu = np.array([1, 2])  # Mean
K = np.array([[4, 1.5], [1.5, 1]])  # Covariance matrix

rvec = ps.RandomVector(mu, K)

# Define conditional indices, condition type, and condition values
# below, we define the 0th index to be equal (=) to 3
indices = [0]
condition = '='
values = np.array([3])

# Get conditional random vector
conditional_rvec = ps.get_conditional(rvec, indices, condition, values)

print(f'Conditional mean: {conditional_rvec.mu}')
print(f'Conditional variance: {conditional_rvec.K}')

Gaussian Process optimization

import pyshrew as ps
import numpy as np
import matplotlib.pyplot as plt

# Generate synthetic data
n_data, n_eval = 50, 100
range_x = 10
noise = 0.2

x_eval = np.array([x * range_x / n_eval for x in range(0, n_eval)])
x_data = np.array([x * range_x / n_data for x in range(0, n_data)])
x = np.append(x_eval, x_data)
y = np.array([np.sin(xi) + np.random.normal(0, noise) for xi in x_data])
conditional_indices = [x for x in range(n_eval, n_eval+n_data)]

# Define hyperparameter initial values and their lower and upper bounds
hyperparameters_iv = ps.SEHyperparams(signal_stdv=0.5, lengthscale=0.5, noise_stdv=0.3)
hyperparameters_lb = ps.SEHyperparams(signal_stdv=0, lengthscale=0, noise_stdv=0)
hyperparameters_ub = ps.SEHyperparams(signal_stdv=100, lengthscale=100, noise_stdv=100)

kernel = ps.SquaredExponential(
    hyperparameters_iv, hyperparameters_lb, hyperparameters_ub, 
    conditional_indices)

# Create Gaussian process and optimize hyperparameters
gp = ps.GaussianProcess(x, y, conditional_indices, kernel)
print("Initial log marginal likelihood:", gp.log_marginal_likelihood())
gp.optimize()

# Get posterior distribution
mu, stdv = gp.get_posterior()

# Plot predictions
ylow = [zp[0]-1.96*zp[1] for zp in zip(mu, stdv)]
yhi = [zp[0]+1.96*zp[1] for zp in zip(mu, stdv)]

plt.plot(x_eval, mu, label=r'GP Posterior Mean')
plt.fill_between(x_eval, ylow, yhi, alpha=0.3, label=r'GP 95% Confidence')
plt.plot(x_data, y, 'k.', label='Data')
plt.legend()
plt.title('Gaussian Process Regression for noisy sine function')
plt.show()

GP Optimization Output

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyshrew-0.1.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

pyshrew-0.1.0-cp313-cp313-macosx_13_0_arm64.whl (3.0 MB view details)

Uploaded CPython 3.13macOS 13.0+ ARM64

pyshrew-0.1.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

pyshrew-0.1.0-cp312-cp312-macosx_13_0_arm64.whl (3.0 MB view details)

Uploaded CPython 3.12macOS 13.0+ ARM64

pyshrew-0.1.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

pyshrew-0.1.0-cp311-cp311-macosx_13_0_arm64.whl (3.0 MB view details)

Uploaded CPython 3.11macOS 13.0+ ARM64

pyshrew-0.1.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

pyshrew-0.1.0-cp310-cp310-macosx_13_0_arm64.whl (3.0 MB view details)

Uploaded CPython 3.10macOS 13.0+ ARM64

pyshrew-0.1.0-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

pyshrew-0.1.0-cp39-cp39-macosx_13_0_arm64.whl (3.0 MB view details)

Uploaded CPython 3.9macOS 13.0+ ARM64

File details

Details for the file pyshrew-0.1.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pyshrew-0.1.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c55d9a1110801915cec1ca315a1413dcf601e1db1345aa608eab9112997e9c8d
MD5 d99df7dd6c49c9fa0b8ac38dad279c04
BLAKE2b-256 e25ab4aaf92e4ec2aa4838b7127525412153915ad77ce1308e9823464560f490

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyshrew-0.1.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build-wheels.yml on ffgiardina/shrew

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyshrew-0.1.0-cp313-cp313-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for pyshrew-0.1.0-cp313-cp313-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 710cb6f9a6e3599519c2720e7746ee7f782a84f4323fb1a3e6fad2bee930eac2
MD5 90514758a2f07a211eb890b4c51150ee
BLAKE2b-256 7a83cab4e1d8443f2d6fc825e42fb8bd0c2ca95c92eec8af2a8fea14c884fe30

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyshrew-0.1.0-cp313-cp313-macosx_13_0_arm64.whl:

Publisher: build-wheels.yml on ffgiardina/shrew

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyshrew-0.1.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pyshrew-0.1.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0b1eab895e256cb5828e347528072ea9e131e6c760f2161ce49e7ebab8d0fe1d
MD5 c26994d5315b99e7a58073c2490a1ec1
BLAKE2b-256 395a03075218a4295092b3cf7953e22399e44ecf1e783f32b4cd2ac1055aad1d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyshrew-0.1.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build-wheels.yml on ffgiardina/shrew

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyshrew-0.1.0-cp312-cp312-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for pyshrew-0.1.0-cp312-cp312-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 b8b18dda7df432b1d57839dd1c402788304aadb312e9e8ef6e0837c322ccc61e
MD5 fcd5a75ad155277d2dc6b8ff092496a8
BLAKE2b-256 97bbe5ddb0f39a2bb005536542de1da1812167bcc177fdc95f286dc178b756da

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyshrew-0.1.0-cp312-cp312-macosx_13_0_arm64.whl:

Publisher: build-wheels.yml on ffgiardina/shrew

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyshrew-0.1.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pyshrew-0.1.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 faee25c116a7aa7eeaf25958e67cb2764685f68834053f6e36f495bd6e56d1e1
MD5 d056eb409ab6799b131a8082fb9ca063
BLAKE2b-256 d517851b69614e31c03e4ff78cb2b871d55cc7fea92e12ff681724b8f97593b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyshrew-0.1.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build-wheels.yml on ffgiardina/shrew

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyshrew-0.1.0-cp311-cp311-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for pyshrew-0.1.0-cp311-cp311-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 6001509801f093d16fd5abce9af824c4d187ea6a955f2afa2323d72f5c4436c0
MD5 f389a2a6608887d8632d2d9ce6ddd3ee
BLAKE2b-256 666fb4211f98e37a2a525e0a31dc5ed5ac241f61cd55c48412364872a05b76c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyshrew-0.1.0-cp311-cp311-macosx_13_0_arm64.whl:

Publisher: build-wheels.yml on ffgiardina/shrew

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyshrew-0.1.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pyshrew-0.1.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b27b8770b2e2cc9d7c5d4031065f5a8b4f9f01a6441573b6d33b56a915c67582
MD5 bfaf965840d86b6d736bcf27736ca163
BLAKE2b-256 d391ed555740905f1a7b7d8c07d109db5e8ea9d8a980e902873a7e4ec717fc67

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyshrew-0.1.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build-wheels.yml on ffgiardina/shrew

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyshrew-0.1.0-cp310-cp310-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for pyshrew-0.1.0-cp310-cp310-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 371ddf5573cceb947cd597b593882ef26538d7a8e89a77e6be8231244c254a5d
MD5 7e3c3018fb513df1ece79b3b8f9877e3
BLAKE2b-256 2cf2912dc6ab5ba9f1abf306bff139d303e3c53b333a072119232c2d6b5f2cfa

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyshrew-0.1.0-cp310-cp310-macosx_13_0_arm64.whl:

Publisher: build-wheels.yml on ffgiardina/shrew

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyshrew-0.1.0-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pyshrew-0.1.0-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4b67fb9e3e271e7d0acc33c346e57cc2d85bb662580edae24c1f8d1803d10766
MD5 5acc112991f991402f558407ace8858b
BLAKE2b-256 18f6ca991d8a46d26673a03bfb5a8de9edabab4dd17255851a870509649bdbd4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyshrew-0.1.0-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: build-wheels.yml on ffgiardina/shrew

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyshrew-0.1.0-cp39-cp39-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for pyshrew-0.1.0-cp39-cp39-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 73db71e558e7d2565f6d3854381539b9466453e6027b0d4ea2805954d172e63c
MD5 530ac18464dbdadff4d9be3b336112c6
BLAKE2b-256 681c2893c2bddae0b2e0707082049e995dddfcbd3932a59180dbddf9f22d91da

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyshrew-0.1.0-cp39-cp39-macosx_13_0_arm64.whl:

Publisher: build-wheels.yml on ffgiardina/shrew

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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