BayesianFitter

class BayesianFitter(model, *, likelihood_kind=None, likelihood_params=None, feature_sigmas=None, **kwargs)[source]

Bases: BaseFitter, ABC

A base class for Bayesian inference fitters.

This class extends BaseFitter to support probabilistic modeling. It appends likelihood noise parameters (like variance or sigma) to the underlying model’s parameters and provides lazily compiled functions for the prior and likelihood probabilities.

Main methods

run

Execute the Bayesian fitting routine.

execute

Implemented by subclasses to run the specific optimization algorithm.

log_likelihood

Evaluate the log-likelihood of the target data.

Parameters:
  • model (Model) – The ParamRF model containing the parameters to optimize.

  • likelihood_kind (str, optional) – The type of likelihood function to use. Options typically include ‘gaussian’ or ‘multivariate_gaussian’. If None, it is inferred automatically based on the provided measurement data during run().

  • likelihood_params (dict[str, Parameter], optional) – A dictionary defining the prior distributions for the likelihood noise parameters (e.g., standard deviations). If None, standard uniform priors are generated automatically.

  • feature_sigmas (list[str], optional) – A list mapping the extracted features to their specific noise parameter names when using a ‘multivariate_gaussian’ likelihood.

  • **kwargs – Additional arguments passed up to BaseFitter (such as frequency).

run(measured, **kwargs)[source]

Execute the Bayesian fitting routine.

This method intercepts the standard run sequence to automatically resolve the target features, likelihood kind, and noise priors based on the shape and type of the provided measurement data before passing execution to the backend.

Parameters:
  • measured (str or skrf.Network or NetworkCollection) – The measurement data to condition the likelihood on.

  • **kwargs – Additional arguments forwarded to the specific backend solver.

Returns:

The fitted model and the raw results object.

Return type:

tuple[Model, FitResults]

property num_params: int

Total number of active parameters (model free parameters + likelihood noise parameters).

Type:

int

cdf(theta)[source]

Evaluate the combined cumulative distribution function (CDF).

Parameters:

theta (jax.numpy.ndarray) – The parameter values. Note that this 1D array must contain the model parameters followed sequentially by the likelihood noise parameters.

Returns:

The combined CDF probabilities mapped between \(0\) and \(1\).

Return type:

jax.numpy.ndarray

icdf(u)[source]

Evaluate the combined inverse cumulative distribution function (ICDF).

Parameters:

u (jax.numpy.ndarray) – The probability values. Note that this 1D array corresponds to the probabilities for the model parameters followed by the likelihood noise parameters.

Returns:

The physical parameter values evaluated from the prior distributions.

Return type:

jax.numpy.ndarray

log_prior(theta)[source]

Evaluate the total log-prior probability.

This lazily compiles the JAX graph to sum the log-prior probabilities of both the underlying model parameters and the added likelihood noise parameters.

Parameters:

theta (jax.numpy.ndarray) – The concatenated 1D array containing the model parameters followed by the likelihood parameters.

Returns:

The scalar log-prior probability.

Return type:

jax.numpy.ndarray

log_likelihood(theta, target)[source]

Evaluate the log-likelihood of the target data.

This handles expanding 1D parameters into the 2D format expected by the vmapped feature extractor, and computes the probability density of the target data against the selected Gaussian or Multivariate Gaussian distribution.

Parameters:
  • theta (jax.numpy.ndarray) – The concatenated 1D array containing the model parameters followed by the likelihood noise standard deviations (\(\sigma\)).

  • target (jax.numpy.ndarray) – The extracted target features (measurement data) to evaluate against.

Returns:

The scalar log-likelihood probability.

Return type:

jax.numpy.ndarray

abstractmethod execute(target, **kwargs)

Implemented by subclasses to run the specific optimization algorithm.

Parameters:
  • target (jax.numpy.ndarray) – The extracted target features to fit against.

  • **kwargs – Backend-specific algorithm parameters passed down from run().

Returns:

The fitted model and the raw results from the solver.

Return type:

tuple[Model, Any]

model_features(theta)

Extract the RF features from the model for a given set of parameters.

This function maps the parameters into the model, simulates it over the defined frequency band, and extracts the target specifications. The entire extraction pipeline is vectorized over the batch dimension and lazily compiled via jax.jit(jax.vmap(...)).

Parameters:

theta (jax.numpy.ndarray) – A 1D array of a single parameter set, or a 2D array representing a batch of parameters.

Returns:

The extracted model features. Matches the batch dimension of theta.

Return type:

jax.numpy.ndarray

Raises:

RuntimeError – If frequency or features were not provided during initialization.

static read_results(stream)

Reconstruct backend optimization or sampling results from a bytes stream.

Parameters:

stream (BytesIO) – The open byte stream to read from.

Returns:

The deserialized Python objects.

Return type:

Any

static write_results(stream, results)

Encode backend optimization or sampling results into a bytes stream.

Parameters:
  • stream (BytesIO) – The open byte stream to write to.

  • results (Any) – The data structure containing the backend results.

Notes

The default implementation uses jsonpickle for robust, research-grade serialization of complex Python objects.