Skip to main content

OR4CLE: Out-of-sample Bayesian Reconstruction for Complex Networks

Project description

OR4CLE

OR4CLE - Out-of-sample bayesian Recostruction 4 CompLex nEtworks.

This repository provides a reproducible reference implementation of rolling, out-of-sample reconstruction workflows.

Installation

pip install or4cle

For development:

pip install -e .

Bundled example dataset

The repository ships a synthetic monthly dataset in datasets/synthetic_networks/ (already generated). It is used by the notebook in notebooks/ and by the automated tests.

Quickstart: BFM rolling experiment

import json
import numpy as np
from pathlib import Path

from or4cle import BFM_solver

DATA_ROOT = Path("datasets/synthetic_networks")
META_PATH = DATA_ROOT / "metadata.json"
SNAP_DIR  = DATA_ROOT / "snapshots"

meta = json.load(open(META_PATH, "r"))
snap_index = {d["label"]: d for d in meta["snapshots"]}

def load_snapshot(label: str) -> BFM_solver.Snapshot:
    z = np.load(SNAP_DIR / snap_index[label]["file"], allow_pickle=False)
    return BFM_solver.Snapshot(
        A=z["A"].astype(float),
        s=z["s"].astype(float),
        node_ids=z["node_ids"].astype(np.int64),
    )

rm = BFM_solver.BFMRollingModel(
    agg="monthly",
    seed_period=("2000-01", "2002-12"),
    analysis_period=("2002-12", "2008-04"),
    loader=load_snapshot,
    update="observed",          # or "counterfactual" for self-sustained
    cal_window_years=3,          # rolling calibration window length
    prior_family="auto",        # choose among supported families
)

res = rm.run()
print(len(res.steps), res.steps[0]["label_t"], "->", res.steps[0]["label_t1"])

Quickstart: BERM rolling experiment

import json
import numpy as np
from pathlib import Path

from or4cle import BERM_solver

DATA_ROOT = Path("datasets/synthetic_networks")
META_PATH = DATA_ROOT / "metadata.json"
SNAP_DIR  = DATA_ROOT / "snapshots"

meta = json.load(open(META_PATH, "r"))
snap_index = {d["label"]: d for d in meta["snapshots"]}

def load_snapshot(label: str) -> BERM_solver.Snapshot:
    z = np.load(SNAP_DIR / snap_index[label]["file"], allow_pickle=False)
    return BERM_solver.Snapshot(
        A=z["A"].astype(float),
        node_ids=z["node_ids"].astype(np.int64),
    )

rm = BERM_solver.BERMRollingModel(
    agg="monthly",
    seed_period=("2000-01", "2002-12"),
    analysis_period=("2002-12", "2008-04"),
    loader=load_snapshot,
    update="observed",
    cal_window_years=3,
    prior_family="auto",
)

res = rm.run()
print(len(res.steps), res.steps[0]["label_t"], "->", res.steps[0]["label_t1"])

Prior diagnostics (fit + KS test) on a seed window

The rolling models fit priors internally. For diagnostics on a chosen seed window you can use utils.plot_prior_from_window:

from or4cle import utils

ks_info = utils.plot_prior_from_window(
    rm,
    start_label="2000-01",
    end_label="2003-12",
    alpha=0.05,
    save_fig=False,
    plot_ecdf=False,
    return_table=True,
)

print(ks_info["table"])      # formatted table string
print(ks_info["passed"])     # list of families passing the KS threshold
print(ks_info["best_name"])  # best family by p-value (tie-broken by D)

Documentation

See docs/:

  • docs/usage.md
  • docs/api_quick_reference.md
  • docs/math.md

License

MIT (see LICENSE).

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

or4cle-0.1.0.tar.gz (61.5 kB view details)

Uploaded Source

Built Distribution

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

or4cle-0.1.0-py3-none-any.whl (62.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: or4cle-0.1.0.tar.gz
  • Upload date:
  • Size: 61.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.13

File hashes

Hashes for or4cle-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e54c8d8794c80b3abdb880b6408e98136d5c8db8ff70503ac87b48e9d479c4cb
MD5 f5f4bbc19dc003d702ab4ee89fa67d0c
BLAKE2b-256 62a3faa6aa94ea4aa30efc2c711cc298ea59826042c35cdcde241caeada35ee4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: or4cle-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 62.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.13

File hashes

Hashes for or4cle-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd8873bbd0e03755b2e41c0c73d46603d7b3257f26519a0a27064442e9a23952
MD5 a0fa7b9903670301b937815ae238a2f1
BLAKE2b-256 81389d10c1ca84e95dceb830a73311afae15fa4da053e7a0948aad311acebd28

See more details on using hashes here.

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