Maximum entropy inference Keras implementation
Project description
Maximum Entropy Inference
This provides a Keras implementation of maximum entropy simulation based inference. The point of this package is to reweight outcomes from a simulator to agree with observations, rather than trying to optimize your simulators input parameters. The simulator must necessarily give multiple outcomes - either because you're trying multiple sets of input parameters or it has intrinsic noise. The assumption of this model is that your simulator is approximately correct. The observations being fit could have come the distribution of outcomes of your simulator.
About maximum entropy
Maximum entropy reweighting is a straightforward black box method that can be applied to arbitrary simulators with few observations. Its runtime is independent of the number of parameters used by the simulator, and it has been shown analytically to minimally change the prior to agree with observations. This method fills a niche in the small-data, high-complexity regime of SBI parameter inference, because it accurately and minimally biases a prior to match observations and does not scale in runtime with the number of model parameters.
Installation
pip install maxent-infer
Quick Start
A Pandas Data Frame
Consider a data frame representing outcomes from our prior model/simulator. We would like to regress these outcomes to data.
import pandas as pd
import numpy as np
import maxent
data = pd.read_csv('data.csv')
Perhaps we have a single observation we would like to match. We can define it with a restraint. Let's say the observation corresponds to the values in column 3.
def observe(single_row):
return single_row[3]
r = maxent.Restraint(observe, target=1.5)
Now we'll fit using MaxEnt
model = maxent.MaxentModel(r)
model.compile()
model.fit(data.values)
We now have a set of weights -- one per row -- that we can use to compute other expressions. For example, here is the most likely outcome (mode)
i = np.argmax(model.traj_weights)
mode = data.iloc[i, :]
Here are the new column averages
col_avg = np.sum(data.values * model.traj_weights[:, np.newaxis], axis=0)
A simulator
Here we show how to take a random walk simulator and use maxent
to have reweight the random walk so that the average end is at x = 2, y= 1.
# simulate
def random_walk_simulator(T=10):
x = [0,0]
traj = np.empty((T,2))
for i in range(T):
traj[i] = x
x += np.random.normal(size=2)
return traj
N = 500
trajs = [random_walk_simulator() for _ in range(N)]
# now have N x T x 2 tensor
trajs = np.array(trajs)
# here is a plot of these trajectories
# we want the random walk to have average end of 2,1
rx = maxent.Restraint(lambda traj: traj[-1,0], target=2)
ry = maxent.Restraint(lambda traj: traj[-1,1], target=1)
# create model by passing in restraints
model = maxent.MaxentModel([rx, ry])
# convert model to be differentiable/GPU (if available)
model.compile()
# fit to data
h = model.fit(trajs)
# can now compute other averages properties
# with new weights
model.traj_weights
# plot showing weights of trajectories:
Further Examples
You can find the examples used in the manuscript, including comparisons with competing methods: here. These examples use the latest package versions, so the figures will not exactly match those in the manuscript. If you would like to reproduce the manuscript exactly, install the packages in paper/requirements.txt
and execute the notebooks in paper
(this is the output from the paper
workflow above).
Citation
See preprint and the citation:
@article{barrett2022simulation,
title={Simulation-Based Inference with Approximately Correct Parameters via Maximum Entropy},
author={Barrett, Rainier and Ansari, Mehrad and Ghoshal, Gourab and White, Andrew D},
journal={Machine Learning: Science and Technology},
year={2022}
}
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file maxent-infer-1.0.0.tar.gz
.
File metadata
- Download URL: maxent-infer-1.0.0.tar.gz
- Upload date:
- Size: 17.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 801120a1071f9adda5fa17ebd7b0040cefd6781b42a3629ec68fba897a70605b |
|
MD5 | 964150cc087eca125e1356844f4ad2c7 |
|
BLAKE2b-256 | 54ba27930da0ee1e5654c9f1d77c9f7c51945f53d291e48bc0bad39eb5da46e8 |
File details
Details for the file maxent_infer-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: maxent_infer-1.0.0-py3-none-any.whl
- Upload date:
- Size: 17.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c86cfac8e18155c5a6727e71daa7ad2f978d6ae0a21ec8b257e9f3883a64b427 |
|
MD5 | 51c1b9e40629b4701620c49526508f0b |
|
BLAKE2b-256 | a10b5984081f138f639fe119b8d13ad8864f00c7f361189efd85cfcdde01ef2e |