Metadata-Version: 2.4
Name: BayesFlux
Version: 0.10.0
Summary: Bayesian Fast Linear algebra sUbspace eXtraction in JAX
Author: Joshua Chen, Michael Brennan, Thomas O'Leary-Roseberry
Author-email: Joshua Chen <joshuawchen@icloud.com>, Michael Brennan <mcbrenn@mit.edu>, Thomas O'Leary-Roseberry <tom.olearyroseberry@utexas.edu>
License-Expression: MIT
License-File: LICENSE
Requires-Dist: jax
Requires-Dist: jaxlib
Requires-Dist: randlax>=0.4.0
Requires-Dist: pytest ; extra == 'dev'
Requires-Dist: black ; extra == 'dev'
Requires-Dist: isort ; extra == 'dev'
Requires-Dist: flake8 ; extra == 'dev'
Requires-Dist: flake8-pyproject ; extra == 'dev'
Requires-Dist: hickle ; extra == 'hippylib'
Requires-Dist: hippylib>=3.1.0 ; extra == 'hippylib'
Requires-Python: >=3.9
Project-URL: Homepage, https://github.com/joshuawchen/BayesFlux
Project-URL: Repository, https://github.com/joshuawchen/BayesFlux
Provides-Extra: dev
Provides-Extra: hippylib
Description-Content-Type: text/markdown

# BayesFlux

BayesFlux is a JAX Python package that provides Fast Linear algebra sUbspace eXtraction for Bayesian inverse problems built off https://github.com/joshuawchen/randLAX.

## Features

- Active Subspace for parameter dimension reduction
- Informative Output Subspace for data dimension reduction
- Coupled parameter and data subspaces fitted to a specified pair of ranks

## Coupled dimension reduction

Given Jacobians of a forward model at prior samples, `coupled_dimension_reduction`
alternates between the parameter eigenspace conditional on the current data
subspace and the data eigenspace conditional on the updated parameter subspace.
It implements Algorithm 1 of Chen, Arnaud, Baptista and Zahm. The existing
`information_theoretic_dimension_reduction` computes the independent diagnostics
of Baptista, Marzouk and Zahm. [Coupled algorithm](https://doi.org/10.1137/24M1670421)
· [Authors' implementation](https://github.com/qchen95/Coupled-input-output-DR/blob/master/dr.py)
· [Independent diagnostics](https://arxiv.org/abs/2207.08670)

```python
from bayesflux.subspace_detection import coupled_dimension_reduction

# J_samples has shape (samples, observations, parameters).
bases = coupled_dimension_reduction(
    key=42,
    J_samples=J_samples,
    noise_precision=noise_precision,
    prior_precision=prior_precision,
    max_input_dimension=4,
    max_output_dimension=2,
)
X_reduced = (X - prior_mean) @ bases["input"]["encoder"]
Y_reduced = Y @ bases["output"]["encoder"]
J_reduced = bases["output"]["encoder"].T @ J_samples @ bases["input"]["decoder"]
```

Each precision can be a positive scalar or a dense positive definite matrix.
The seed initializes the data subspace. To start from a known subspace, pass
its orthonormal columns in whitened data coordinates as `initial_output_basis`.
Each sweep increases or preserves the
mean squared Frobenius norm of the reduced Jacobians. Inspect
`bases["diagnostics"]["objective_history"]` to track this objective. The
default relative tolerance is `1e-6` over at most 20 full sweeps. Set
`tolerance=None` to run a fixed number of sweeps. Fit each desired pair of
ranks independently since either rank can change both optimal subspaces.

## Installation

The core JAX package supports Python 3.9 and newer.

```sh
python -m pip install bayesflux
```

For PDE sampling and likelihoods, install legacy FEniCS/DOLFIN 2019.1 with PETSc
first. The following commands create a Python 3.11 environment and install
hIPPYlib and hickle through the BayesFlux extra.

```sh
conda create -n bayesflux-fenics -c conda-forge python=3.11 fenics=2019.1.0 pip
conda activate bayesflux-fenics
python -m pip install 'bayesflux[hippylib]'
```

Projects that use the PDE functionality can declare `bayesflux[hippylib]` as a
dependency and direct users to the FEniCS environment setup above.

### Encoded Gaussian PDE likelihood

The local hIPPYlib wrapper evaluates the Gaussian likelihood of the retained observations. For an output encoder `E`, physical noise covariance `Gamma`, and PDE prediction `G(m)`, it uses residual `y_reduced - E.T @ G(m)` and covariance `E.T @ Gamma @ E`. With the coupled noise-whitened basis the reduced covariance is identity. The gradient uses one Jacobian-transpose action for each sample. The input is `parameter_offset + input_decoder @ z`. For standard-normal centered coordinates, pass `parameter_offset=prior_mean`. For preprojected training data, `projection_center=prior_mean` evaluates the forward model on the same affine subspace and stores the usual raw dinox input coefficients. The wrapper accepts stock hIPPYlib noise variance or a custom noise precision and checks their consistency when both are supplied. Solve failures propagate to the caller.
