Metadata-Version: 2.4
Name: rjdoa
Version: 0.2.0
Summary: Bayesian wideband DoA estimation and detection with reversible-jump MCMC and SMC samplers.
Author: Finley Boulton
License-Expression: MIT
Project-URL: Repository, https://github.com/fincb/rjdoa
Project-URL: Bug Tracker, https://github.com/fincb/rjdoa/issues
Keywords: direction of arrival,passive sonar,source detection,Bayesian inference,reversible jump MCMC,sequential Monte Carlo
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.26
Requires-Dist: scipy>=1.11
Requires-Dist: numba>=0.59
Requires-Dist: tqdm>=4
Provides-Extra: plot
Requires-Dist: matplotlib>=3.7; extra == "plot"
Provides-Extra: scripts
Requires-Dist: matplotlib>=3.7; extra == "scripts"
Requires-Dist: h5py>=3; extra == "scripts"
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Requires-Dist: h5py>=3; extra == "test"
Provides-Extra: dev
Requires-Dist: rjdoa[scripts,test]; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Dynamic: license-file

# rjdoa

Bayesian wideband direction-of-arrival (DoA) estimation and source detection with
reversible-jump samplers. The number of sources and their bearings are inferred jointly from
time-domain array data, with the source signals and noise power marginalised out [1], [2].

The package is a Python port of [ReversibleJump.jl](https://github.com/Red-Portal/ReversibleJump.jl) (v0.2.0) and
[WidebandDoA.jl](https://github.com/Red-Portal/WidebandDoA.jl), verified against the Julia code. It adds the
reversible-jump SMC samplers of Bright and Maskell [3].

The primary and intended purpose for this repository is compatibility for inclusion into the
[blue-pebble](https://github.com/UoL-SignalProcessingGroup/blue-pebble) sonar signal processing simulator
plugin for [Stone Soup](https://github.com/dstl/Stone-Soup).

```
pip install -e ".[dev]"
pytest -m "not slow"            # unit + Julia fixture parity (~1.5 min)
pytest -m slow                  # Julia chain parity + Geweke tests (several min, all cores)
```

## Layout

`rjdoa.estimate(signals, fs, positions, c, method=...)` is the one-call entry point. It takes
one window of array data in blue-pebble's layout, runs nRJMCMC (optionally as several chains in
parallel), RJSMC or wiRJSMC and returns the posterior number of sources, bearings, their
uncertainty, how often each appears and, for nRJMCMC, convergence diagnostics.

All modules are under `rjdoa`:

- `reversible_jump` has RJMCMC and nRJMCMC with independent and annealed (AIS) jump proposals,
  a runner for independent chains in parallel, and R-hat and ESS diagnostics.
- `reversible_jump.smc` has RJSMC and wiRJSMC, run serially or in parallel processes.
- `doa` has the wideband model, the collapsed likelihood (numba and numpy), array geometries,
  relabelling, simulation and plotting.
- `doa.inference` has the slice and Metropolis kernels.
- `mixture` has the Richardson-Green Gaussian mixture used to reproduce [3].
- `dists` has fast scalar log-densities with Distributions.jl parametrisations.

DoA parameters are `(k, 2)` arrays of `[phi, loglambda]`, and example scripts are in
`scripts/`.

## Results

### Performance

On the demo configuration (M = 20, N = 64, n_fft = 128) the log-density is 1.6-6.3x faster
than Julia for k = 1-6, and the nRJMCMC demo sampler takes 267 us per iteration against
Julia's 1165 us. Only the single-source slice sweep is slower (261 us against 157 us). Timings
come from `scripts/bench_python.py` and `verification/julia/bench.jl`.

### DoA: nRJMCMC vs RJSMC

These results come from `scripts/rjsmc_doa.py` on the demo scenario, with true DoAs of 30, 45,
-15 and -60 degrees, where the 30 degree source is weak. All four samplers place the four
bearings within 0.1 degrees of each other.

| method | wall (s) | P(k=3) | P(k=4) | mean k | R-hat of k |
|---|---|---|---|---|---|
| nRJMCMC (1 chain x 100k) | 30.8 | 0.60 | 0.39 | 3.40 | 1.00 |
| nRJMCMC (16 chains x 10k, in parallel) | 4.2 | 0.63 | 0.37 | 3.37 | 1.04 |
| RJSMC (N=128, T=300) | 9.8 | 0.40 | 0.58 | 3.62 | - |
| wiRJSMC | 9.5 | 0.20 | 0.80 | 3.80 | - |

The 16 parallel chains start from random draws of the prior, yet agree with the single chain
and the Julia reference chains in about a seventh of the time, while the SMC order posteriors
disagree. This needs further testing (see [Notes / TODO](#notes--todo)). Currently nRJMCMC
appears to be more accurate for P(k).

<img src="https://github.com/fincb/rjdoa/raw/master/scripts/out/doa_overview.png" width="800" alt="DoA overview">

*Model-order trace, order posterior and DoA posterior (left to right) for each sampler.*

### Sampler benchmark

`scripts/benchmark/` compares each sampler's error, for a fixed wall time, against long
reference runs on four synthetic scenarios (detailed in `scripts/benchmark/README.md`).
Parallel nRJMCMC chains are the most accurate in every scenario, and 4 or more chains give
similar results. RJSMC and wiRJSMC overestimate the number of sources on the two harder
scenarios, by 0.08 to 0.55, and more time or particles do not reduce this.

<img src="https://github.com/fincb/rjdoa/raw/master/scripts/benchmark/out/error_vs_time.png" width="800" alt="Error against time">

*TV error of P(k) against wall time, mean and range over 5 seeds, for each scenario.*

### RJSMC paper reproduction

These results come from `scripts/rjsmc_mixture.py`, with N = 128 particles, T = 1000
iterations and 32 cores.

| data | method | wall (s) | BIC variance | mode k |
|---|---|---|---|---|
| toy | nRJMCMC (128k iterations) | 18.5 | 54.7 | 2 |
| toy | RJSMC | 6.8 (39.4 on 1 core) | 79.3 | 2 |
| toy | wiRJSMC | 6.4 | 31.1 | 2 |
| galaxy | nRJMCMC | 24.1 | 263 | 3 |
| galaxy | RJSMC | 9.2 (62.4 on 1 core) | 387 | 4 |
| galaxy | wiRJSMC | 7.5 | 49.6 | 4 |

As in [3], wiRJSMC has the lowest BIC variance, but this compares single nRJMCMC draws with
per-iteration SMC averages. If nRJMCMC is averaged similarly, its variance is lowest. RJSMC
still runs 2.5-3x faster than nRJMCMC here. The larger 30x gain in [3] is measured against a
much slower baseline (5417 s for 128k nRJMCMC iterations, against 18.5 s here) on 64 cores
rather than 32. Models with costlier particles than this mixture should scale closer to [3].

The SMC BIC variances on the toy data closely match [3] (79 and 31 here, against 74 and 28).
Other values, such as BIC levels and numbers of components, differ because [3] does not fully
specify its priors.

<img src="https://github.com/fincb/rjdoa/raw/master/scripts/out/mixture_galaxy_traces.png" width="800" alt="Galaxy traces">

*Weighted-mean BIC and number of components for the galaxy data, against model evaluations.*

## Verification and deviations from the originals

The port matches the Julia code to 1e-9 on deterministic outputs and agrees with Julia chains
on order posteriors, DoA means and acceptance rates (|z| < 2). Every sampler also passes
exact-target and Geweke joint-distribution tests. `verification/julia/setup.sh` builds the
Julia reference environment.

The port deliberately differs from the Julia code in three places:

- The Julia simulator draws the noise level from a different prior than the one the likelihood
  assumes. The port's simulator uses the same prior as the likelihood.
- In arithmetic-path AIS, Julia lets proposed DoAs leave the range the proposal can reach,
  which biases P(k) low. The port rejects such proposals.
- When nRJMCMC is moving down at k = 0, Julia proposes a birth, which samples the wrong
  distribution. The port reverses direction instead.

## Notes / TODO

- The likelihood treats the zero padding as measured silence, so a signal present at a window
  edge leaves about D samples per edge that no plane wave explains (D is the delay across the
  array). At high SNR this adds weak spurious sources (P(k>4) = 0.61 / 0.18 / 0.06 at
  N = 64 / 256 / 1024, +10 dB). Integrating the padded samples out as missing data would fix
  it; until then, keep D/N small.
- RJSMC and wiRJSMC overestimate the number of sources. Their weights collapse every iteration
  (ESS about 10 % of N, as almost every jump proposal is poor). More particles do not help,
  so the cause is not clear. Need to test: much longer SMC runs and SMC started from posterior draws.
- Data-driven birth proposals (bearings drawn from where the data has unexplained power) would
  raise jump acceptance for every sampler, and SMC most.

## References

[1] K. Kim, P. T. Clemson, J. P. Reilly, J. F. Ralph, and S. Maskell, "Fully Bayesian wideband
direction-of-arrival estimation and detection via RJMCMC," arXiv:2412.08895, Dec. 2024.

[2] K. Kim, P. T. Clemson, J. P. Reilly, J. F. Ralph, and S. Maskell, "Bayesian wideband signal
detection via source signal marginalization and RJMCMC," *IEEE Trans. Signal Process.*,
vol. 74, pp. 3773-3788, 2026, doi: 10.1109/TSP.2026.3721253.

[3] M. Bright and S. Maskell, "Reversible jump SMC," in *Proc. IEEE 10th Int. Workshop Comput.
Adv. Multi-Sensor Adaptive Process. (CAMSAP)*, 2025, pp. 271-275,
doi: 10.1109/CAMSAP66162.2025.11423972.
