Morph-JAX
Morph-JAX fits a normalized probability density to samples. Use it to evaluate log densities and their gradients with JAX or generate independent draws from the fitted approximation.
Morph represents the joint density as a product of kernel density estimates over disjoint blocks of parameters. It selects blocks using estimates of total correlation and Seeded Greedy Maximization (SGM). The method follows Zahraoui et al., Phys. Rev. D 113, 083014 (2026).
Installation
Morph-JAX requires Python 3.10 or newer. Install from this checkout with:
python -m pip install .
When the PyPI release is available, install it with python -m pip install morph-jax.
The default JAX installation uses CPU. For accelerators, install the appropriate JAX build using the official installation guide. For Linux environments managed with uv, see the CPU and CUDA setup guide.
Density approximation
Input arrays use rows for draws: (n_samples, n_parameters).
import jax
import jax.numpy as jnp
from morph_jax import fit, log_prob, sample
training_draws = jax.random.multivariate_normal(
jax.random.key(0),
jnp.zeros(2),
jnp.array([[1.0, 0.8], [0.8, 1.0]]),
shape=(500,),
)
fitted = fit(training_draws, order=2)
state = fitted.state
draws = sample(jax.random.key(1), state, n=1000)
log_densities = log_prob(state, draws)
gradient = jax.grad(lambda point: log_prob(state, point))(draws[0])
print(state.blocks)
print(fitted.diagnostics.retained_total_correlation)
fit(samples, weights=...) accepts nonnegative weights. Silverman's bandwidth
is the default. order=2 fits pairs and any residual singleton; order=1 fits
independent marginals. Factors must have fewer than ten dimensions. All candidate
blocks are enumerated and scored without a candidate-count limit.
SGM is a heuristic, not an exact matching algorithm.
It tries five seeds by default. Set n_seeds to override the count, or
n_seeds=None to try every candidate.
sgm_early_stop=True enables an equivalent early-exit scan; benchmark it on your
device before enabling it, since it is not consistently faster across JAX versions.
For cheaper block scoring, explicitly set max_tc_samples=256 and provide a
key. This uses a reproducible subset for total correlation only; the selected
KDE factors still use all fitting samples. It can change the selected partition.
Set candidate_progress_every=100 to show a live tqdm bar while scoring
candidates, including completion counts, elapsed time, and scoring rate.
Bayesian evidence estimation with MorphZ
Morph-JAX also includes a MorphZ workflow for estimating marginal likelihoods from posterior draws. It fits a Morph density on one subset of the chain and uses draws from that density as proposals for bridge sampling on a held-out subset. Enable 64-bit precision before constructing the arrays used for evidence estimation.
import jax
from morph_jax import estimate_evidence
jax.config.update("jax_enable_x64", True)
def log_density(theta):
return log_likelihood(theta) + log_prior(theta)
result = estimate_evidence(
posterior_samples,
log_density,
jax.random.key(2),
order=2,
n_proposal=2000,
)
print(result.bridge.log_evidence)
print(result.bridge.relative_error)
print(result.bridge.converged)
The Gaussian evidence example checks the estimate against an analytical result.
The callback must be JAX-compatible, return a scalar, and include all likelihood
and proper-prior normalization constants. This workflow requires equal-weight
posterior draws. It splits consecutive rows into disjoint fitting and bridge
batches. It estimates autocorrelation from the ordered held-out bridge sequence;
use posterior_iat=1 for independent draws. For multiple chains, supply a suitable
IAT explicitly rather than treating a concatenated array as one chain.
Provide posterior_log_density with one cached value per original row to reduce
additional target evaluations to the number of proposal draws. Bounded-prior
proposals outside the support must return -inf; those draws are retained.
For non-JAX likelihoods, evaluate densities externally and call bridge_sampling
with the four log-density vectors. See the numerical method.
Both APIs default to geometric_restart=True: after every 1,000 unconverged
bridge updates, restart at the midpoint of the last two log-Z values (their
geometric mean on the Z scale). Set restart_interval=N (at least 2) to change
the cycle length, or geometric_restart=False to recover standard uninterrupted
bridge iteration. max_iterations remains a total cap across all cycles.
The final averaging window uses only updates since the latest restart.
This repeats the restart remedy of
Gronau, Heathcote and Matzke (2020);
the bridge equation and convergence tolerance are unchanged.
Validation and implementation records
python -m pip install -e '.[test,benchmark]'
pytest -q
ruff check .
ruff format --check .
python examples/gaussian_evidence.py
python benchmarks/sgm.py --dimension 50 --seeds 128
- Method and equations: mathematical conventions and scope.
- JAX execution enhancements: changes, compatibility, and measurements.
- Implementation record: modules, optimizations, deviations, diagnostics, and limitations.
- Validation record: checks and execution results.
- Changelog: versioned changes.
Research benchmarks
- Block recovery and density accuracy
- CPU and GPU total-correlation benchmarks
- NANOGrav evidence with IRN, CURN, and HD models
- NANOGrav flow comparisons
Benchmark scripts save generated results and plots under benchmark-results/.
Metadata
Release files for morph-jax 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| morph_jax-0.1.0.tar.gz | 49.0 kB | Without provenance | Details for morph_jax-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| morph_jax-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for morph_jax-0.1.0-py3-none-any.whl |
Total release size: 72.8 kB
Release files / morph_jax-0.1.0.tar.gz
| Download URL | morph_jax-0.1.0.tar.gz |
|---|---|
| Size | 49.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
49569a247d805300fe57712dc1a1106afceb0796fb9441699ea3ae25167ec0af
|
|
BLAKE2b-256 checksum How to use checksums |
2cbada423657d7d9f5c96b500521adc240a1f56150237594ecb2735ad896c4e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / morph_jax-0.1.0-py3-none-any.whl
| Download URL | morph_jax-0.1.0-py3-none-any.whl |
|---|---|
| Size | 23.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f989fee93f9b21f40477b0adb2df1bf8d78a29945be4c7a3b7e9f508bae46f55
|
|
BLAKE2b-256 checksum How to use checksums |
dc2fba44a9d29d97cffea295618ef3da79f047a7f0e8a0f54ff93281fde662c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for morph-jax 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.