Skip to main content

Build a surrogate for your LnL(d|θ)± ΔLnL(d|θ)

Project description

COMPAS LnL Surrogate

Coverage Status

This python package helps make an LnL surrogate for COMPAS SF-params given a set of BBH mergers. The surrogate is trained using active learning/bayesian optimisation techniques. This package acts as a bridge between the COMPAS LnL computer, surrogate modelling packages, and Bayesian optimisation packages.

Installation

pip install lnl_surrogate@git+https://github.com/COMPAS-Surrogate/lnl_surrogate.git

Example

from trieste.acquisition.function import PredictiveVariance, ExpectedImprovement


import numpy as np
from lnl_surrogate.surrogate import train
from scipy.stats import norm
from lnl_surrogate.surrogate.setup_optimizer import McZGrid
from lnl_computer.mock_data import generate_mock_data
from typing import Dict
import matplotlib.pyplot as plt
import os

np.random.seed(0)

MINX, MAXX = 0.005, 0.015
MIDX = (MINX + MAXX) / 2
NORM = norm(MIDX, 0.003)
OUTDIR = 'outdir'
os.makedirs(OUTDIR, exist_ok=True)

def mock_lnl(*args, **kwargs):
    sf_sample: Dict = kwargs.get('sf_sample')
    sf_sample = np.array(list(sf_sample.values()))
    return NORM.logpdf(sf_sample), 0

def plot_res(model, data, search_space):
    x = np.linspace(MINX, MAXX, 100).reshape(-1, 1)
    true_y = NORM.logpdf(x) * -1.0
    model_y, model_yunc = model.predict(x)
    x_obs = data.query_points
    y_obs = data.observations

    tf_to_np = lambda x: x.numpy().flatten() if hasattr(x, 'numpy') else x
    # make new fig
    plt.figure()
    plt.plot(x, true_y, label='True', color='black')
    plt.plot(x, model_y, label='Model', color="tab:orange")
    plt.scatter(x_obs, y_obs, label='Observed', color='black')
    yup, ydown = tf_to_np(model_y + model_yunc), tf_to_np(model_y - model_yunc)
    plt.fill_between(x.flatten(), yup.flatten(), ydown.flatten(), alpha=0.2 , color="tab:orange")
    plt.legend(loc='upper right')
    return plt.gcf()

McZGrid.lnl = mock_lnl
mock_data = generate_mock_data(OUTDIR)

acq_fns = [PredictiveVariance(), ExpectedImprovement()]
for acq_fn in acq_fns:
    res = train(
        model_type='gp',
        mcz_obs=mock_data.observations.mcz,
        compas_h5_filename=mock_data.compas_filename,
        acquisition_fns=[acq_fn],
        params=['aSF'],
        n_init=2,
        n_rounds=10,
        n_pts_per_round=1,
        outdir=f"{OUTDIR}/gp",
        truth=dict(
            aSF=MIDX,
            lnl=mock_lnl(sf_sample={'aSF': MIDX})[0]*-1.0
        ),
        model_plotter=plot_res,
        noise_level=1e-3
    )
Exploratory Acquisition Exploitative Acquisition
Exploratory Exploitative

Regret

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

compas_surrogate_lnl_surrogate-0.1.1.tar.gz (587.9 kB view details)

Uploaded Source

Built Distribution

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

compas_surrogate.lnl_surrogate-0.1.1-py3-none-any.whl (23.5 kB view details)

Uploaded Python 3

File details

Details for the file compas_surrogate_lnl_surrogate-0.1.1.tar.gz.

File metadata

File hashes

Hashes for compas_surrogate_lnl_surrogate-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ea91991f19e538e205309407d25ede46d220a00e866b1e9bb05b24ce09fbc001
MD5 54a87eb65297d6614ea6f381eb8ca6c3
BLAKE2b-256 64480526d302234c18f36414609200f98aa4791dfef8b324a2c8fe726ff5e5c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for compas_surrogate_lnl_surrogate-0.1.1.tar.gz:

Publisher: release.yml on COMPAS-Surrogate/lnl_surrogate

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

File details

Details for the file compas_surrogate.lnl_surrogate-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for compas_surrogate.lnl_surrogate-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fe459bc094098561e23dc130c62a1d2c1e7fe9ea42b9ffca02dc586431ef7c93
MD5 26a808a3fb5a0c1cd63ec1639c90fee4
BLAKE2b-256 11a5909f0566353c49100a6139c9a97ada766a6b8634d3daaa3ee1b696ac8e24

See more details on using hashes here.

Provenance

The following attestation bundles were made for compas_surrogate.lnl_surrogate-0.1.1-py3-none-any.whl:

Publisher: release.yml on COMPAS-Surrogate/lnl_surrogate

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