bayes_yplus

Submodules

bayes_yplus.utils

utils.py Model Utilities

Copyright(C) 2024-2025 by Trey V. Wenger; tvwenger@gmail.com This code is licensed under MIT license (see LICENSE for details)

bayes_yplus.utils.gaussian(x: float, center: float, fwhm: float) float

Evaluate a normalized Gaussian function

Parameters:
xfloat

Position at which to evaluate

centerfloat

Gaussian centroid

fwhmfloat

Gaussian FWHM line width

Returns:
float

Gaussian evaluated at x

bayes_yplus.yplus_model

yplus_model.py YPlusModel definition

Copyright(C) 2024-2025 by Trey V. Wenger; tvwenger@gmail.com This code is licensed under MIT license (see LICENSE for details)

class bayes_yplus.yplus_model.YPlusModel(*args, **kwargs)

Bases: BaseModel

Definition of the YPlusModel

Attributes:
baseline_deterministics

Get the deterministic baseline parameter names.

baseline_freeRVs

Get the free baseline parameter names.

cloud_deterministics

Get the deterministic cloud parameter names.

cloud_freeRVs

Get the free cloud parameter names.

hyper_deterministics

Get the deterministic hyper parameter names.

hyper_freeRVs

Get the free hyper parameter names.

labeller

Get the arviz labeller.

unique_solution

Check if posterior samples suggest a unique solution.

Methods

add_baseline_priors([prior_baseline_coeffs])

Add baseline priors to the model.

add_likelihood()

Add likelihood to the model.

add_priors([prior_H_area, prior_H_center, ...])

Add priors and deterministics to the model

bic([chain, solution])

Calculate the Bayesian information criterion at the mean point estimate.

fit([n, draws, rel_tolerance, ...])

Approximate posterior distribution using Variational Inference (VI).

graph()

Generate visualization of the model graph.

mean_lnlike([chain, solution])

Evaluate mean log-likelihood over posterior samples.

null_bic()

Evaluate the Bayesian Information Criterion for the null hypothesis (baseline only, no clouds)

predict_baseline([baseline_params])

Predict the un-normalized baseline model.

reset_results()

Reset results and convergence checks.

sample([init, n_init, chains, init_kwargs, ...])

Sample posterior distribution using MCMC.

sample_posterior_predictive([solution, thin])

Generate posterior predictive samples

sample_prior_predictive([samples])

Generate prior predictive samples

sample_smc(**kwargs)

Sample posterior distribution using Sequential Monte Carlo.

solve([num_gmm_samples, kl_div_threshold])

Identify unique solutions and break the labeling degeneracy.

add_likelihood()

Add likelihood to the model.

add_priors(prior_H_area: float = 1000.0, prior_H_center: Iterable[float] = [0.0, 25.0], prior_H_fwhm: Iterable[float] = [25.0, 10.0], prior_He_H_fwhm_ratio: Iterable[float] = [1.0, 0.1], prior_yplus: float = 0.05, prior_rms: dict[str, float] | None = None, prior_baseline_coeffs: dict[str, Iterable[float]] | None = None, ordered: bool = False)

Add priors and deterministics to the model

Parameters:
prior_H_areafloat, optional

Prior distribution on H RRL line area (mK km s-1), by default 1000.0, where H_area ~ HalfNormal(sigma=prior)

prior_H_centerIterable[float], optional

Prior distribution on H RRL center velocity (km s-1), by default [0, 25.0], where H_center ~ Normal(mu=prior[0], sigma=prior[1])

prior_H_fwhmIterable[float], optional

Prior distribution on H RRL FWHM line width (km s-1), by default [25.0, 10.0], where H_fwhm ~ Gamma(mu=prior[0], sigma=prior[1])

prior_He_H_fwhm_ratioIterable[float], optional

Prior distribution on He/H RRL FWHM line width ratio, by default [1.0, 0.1], where He_H_fwhm_ratio ~ Gamma(mu=prior[0], sigma=prior[1])

prior_yplusfloat, optional

Prior distribution on y+, by default 0.05, where yplus ~ HalfNormal(sigma=prior_yplus)

prior_rmsOptional[dict[str, float]], optional

Prior distribution on spectral rms (K), by default None, where rms ~ HalfNormal(sigma=prior) Keys are dataset names and values are priors. If None, then the spectral rms is taken from dataset.noise and not inferred.

prior_baseline_coeffsOptional[dict[str, Iterable[float]]], optional

Prior distribution on the normalized baseline polynomial coefficients, by default None. Keys are dataset names and values are lists of length baseline_degree+1. If None, use [1.0]*(baseline_degree+1) for each dataset.

orderedbool, optional

If True, assume ordered velocities (optically thin assumption), by default False. If True, the prior distribution on the velocity becomes H_center(cloud = n) ~

prior[0] + sum_i(H_center[i < n]) + Gamma(alpha=2.0, beta=1.0/prior[1])

Module contents

class bayes_yplus.YPlusModel(*args, **kwargs)

Bases: BaseModel

Definition of the YPlusModel

Attributes:
baseline_deterministics

Get the deterministic baseline parameter names.

baseline_freeRVs

Get the free baseline parameter names.

cloud_deterministics

Get the deterministic cloud parameter names.

cloud_freeRVs

Get the free cloud parameter names.

hyper_deterministics

Get the deterministic hyper parameter names.

hyper_freeRVs

Get the free hyper parameter names.

labeller

Get the arviz labeller.

unique_solution

Check if posterior samples suggest a unique solution.

Methods

add_baseline_priors([prior_baseline_coeffs])

Add baseline priors to the model.

add_likelihood()

Add likelihood to the model.

add_priors([prior_H_area, prior_H_center, ...])

Add priors and deterministics to the model

bic([chain, solution])

Calculate the Bayesian information criterion at the mean point estimate.

fit([n, draws, rel_tolerance, ...])

Approximate posterior distribution using Variational Inference (VI).

graph()

Generate visualization of the model graph.

mean_lnlike([chain, solution])

Evaluate mean log-likelihood over posterior samples.

null_bic()

Evaluate the Bayesian Information Criterion for the null hypothesis (baseline only, no clouds)

predict_baseline([baseline_params])

Predict the un-normalized baseline model.

reset_results()

Reset results and convergence checks.

sample([init, n_init, chains, init_kwargs, ...])

Sample posterior distribution using MCMC.

sample_posterior_predictive([solution, thin])

Generate posterior predictive samples

sample_prior_predictive([samples])

Generate prior predictive samples

sample_smc(**kwargs)

Sample posterior distribution using Sequential Monte Carlo.

solve([num_gmm_samples, kl_div_threshold])

Identify unique solutions and break the labeling degeneracy.

add_likelihood()

Add likelihood to the model.

add_priors(prior_H_area: float = 1000.0, prior_H_center: Iterable[float] = [0.0, 25.0], prior_H_fwhm: Iterable[float] = [25.0, 10.0], prior_He_H_fwhm_ratio: Iterable[float] = [1.0, 0.1], prior_yplus: float = 0.05, prior_rms: dict[str, float] | None = None, prior_baseline_coeffs: dict[str, Iterable[float]] | None = None, ordered: bool = False)

Add priors and deterministics to the model

Parameters:
prior_H_areafloat, optional

Prior distribution on H RRL line area (mK km s-1), by default 1000.0, where H_area ~ HalfNormal(sigma=prior)

prior_H_centerIterable[float], optional

Prior distribution on H RRL center velocity (km s-1), by default [0, 25.0], where H_center ~ Normal(mu=prior[0], sigma=prior[1])

prior_H_fwhmIterable[float], optional

Prior distribution on H RRL FWHM line width (km s-1), by default [25.0, 10.0], where H_fwhm ~ Gamma(mu=prior[0], sigma=prior[1])

prior_He_H_fwhm_ratioIterable[float], optional

Prior distribution on He/H RRL FWHM line width ratio, by default [1.0, 0.1], where He_H_fwhm_ratio ~ Gamma(mu=prior[0], sigma=prior[1])

prior_yplusfloat, optional

Prior distribution on y+, by default 0.05, where yplus ~ HalfNormal(sigma=prior_yplus)

prior_rmsOptional[dict[str, float]], optional

Prior distribution on spectral rms (K), by default None, where rms ~ HalfNormal(sigma=prior) Keys are dataset names and values are priors. If None, then the spectral rms is taken from dataset.noise and not inferred.

prior_baseline_coeffsOptional[dict[str, Iterable[float]]], optional

Prior distribution on the normalized baseline polynomial coefficients, by default None. Keys are dataset names and values are lists of length baseline_degree+1. If None, use [1.0]*(baseline_degree+1) for each dataset.

orderedbool, optional

If True, assume ordered velocities (optically thin assumption), by default False. If True, the prior distribution on the velocity becomes H_center(cloud = n) ~

prior[0] + sum_i(H_center[i < n]) + Gamma(alpha=2.0, beta=1.0/prior[1])