pcntoolkit.regression_model.hbr#
Classes#
Hierarchical Bayesian Regression model implementation. |
Module Contents#
- class HBR(name: str = 'template', likelihood: pcntoolkit.math_functions.likelihood.Likelihood = get_default_normal_likelihood(), draws: int = 1500, tune: int = 500, cores: int = 4, chains: int = 4, nuts_sampler: str = 'nutpie', init: str = 'jitter+adapt_diag', progressbar: bool = True, is_fitted: bool = False, is_from_dict: bool = False)#
Bases:
pcntoolkit.regression_model.regression_model.RegressionModelHierarchical Bayesian Regression model implementation.
This class implements a Bayesian hierarchical regression model using PyMC for posterior sampling. It supports multiple likelihood functions and provides methods for model fitting, prediction, and analysis.
This class implements a Bayesian hierarchical regression model using PyMC for posterior sampling.
- Parameters:
name (
str) – Unique identifier for the model instancelikelihood (
Likelihood) – Likelihood function to use for the modeldraws (
int, optional) – Number of samples to draw from the posterior distribution per chain, by default 1000tune (
int, optional) – Number of tuning samples to draw from the posterior distribution per chain, by default 500cores (
int, optional) – Number of cores to use for parallel sampling, by default 4chains (
int, optional) – Number of chains to use for parallel sampling, by default 4nuts_sampler (
str, optional) – NUTS sampler to use for parallel sampling, by default “nutpie”init (
str, optional) – Initialization method for the model, by default “jitter+adapt_diag”progressbar (
bool, optional) – Whether to display a progress bar during sampling, by default Trueis_fitted (
bool, optional) – Whether the model has been fitted, by default Falseis_from_dict (
bool, optional) – Whether the model was created from a dictionary, by default False
- backward(X, be, Z) xarray.DataArray#
Map Z values to Y space using MCMC samples
- Parameters:
X (
xr.DataArray) – Covariate databe (
xr.) – Batch effect dataZ (
xr.DataArray) – Z-score data
- Returns:
Z-values mapped to Y space
- Return type:
xr.DataArray
- compute_yhat(data, responsevar, X, be)#
- elemwise_logp(X, be, Y) xarray.DataArray#
Compute log-probabilities for each observation in the data.
- Parameters:
X (
xr.DataArray) – Covariate databe (
xr.DataArray) – Batch effect databe_maps (
dict[str,dict[str,int]]) – Batch effect mapsY (
xr.DataArray) – Response variable data
- Returns:
Log-probabilities of the data
- Return type:
xr.DataArray
- fit(X: xarray.DataArray, be: xarray.DataArray, be_maps: dict[str, dict[str, int]], Y: xarray.DataArray) None#
Fit the model to training data using MCMC sampling.
- Parameters:
X (
xr.DataArray) – Covariate databe (
xr.DataArray) – Batch effect databe_maps (
dict[str,dict[str,int]]) – Batch effect mapsY (
xr.DataArray) – Response variable data
- Return type:
- forward(X: xarray.DataArray, be: xarray.DataArray, Y: xarray.DataArray) xarray.DataArray#
Map Y values to Z space using MCMC samples
- Parameters:
X (
xr.DataArray) – Covariate databe (
xr.DataArray) – Batch effect dataY (
xr.DataArray) – Response variable data
- Returns:
Z-values mapped to Y space
- Return type:
xr.DataArray
- classmethod from_args(name: str, args: Dict[str, Any]) HBR#
Create model instance from command line arguments.
- classmethod from_dict(my_dict: Dict[str, Any], path: str | None = None) HBR#
Create model instance from serialized dictionary.
- Parameters:
dict (
Dict[str,Any]) – Dictionary containing serialized modelpath (
Optional[str], optional) – Path to load inference data from, by default None
- Returns:
New model instance
- Return type:
- generic_MCMC_apply(X, be, Y, fn, kwargs)#
Apply a generic function to likelihood parameters
- has_batch_effect() bool#
Check if model includes batch effects.
- Returns:
True if model includes batch effects, False otherwise
- Return type:
- load_idata(path: str) None#
Load inference data from NetCDF file.
- Parameters:
path (
str) – Path to load inference data from. Should end in ‘.nc’- Return type:
- Raises:
RuntimeError – If model is fitted but inference data cannot be loaded from path
- save_idata(path: str) None#
Save inference data to NetCDF file.
- Parameters:
path (
str) – Path to save inference data to. Should end in ‘.nc’- Return type:
- Raises:
RuntimeError – If model is fitted but does not have inference data
- to_dict(path: str | None = None) Dict[str, Any]#
Serialize model to dictionary format.
- Parameters:
path (
Optional[str], optional) – Path to save inference data, by default None- Returns:
Dictionary containing serialized model
- Return type:
Dict[str,Any]
- transfer(X: xarray.DataArray, be: xarray.DataArray, be_maps: dict[str, dict[str, int]], Y: xarray.DataArray, **kwargs) HBR#
Perform transfer learning using existing model as prior.
- hbrconfHBRConf
Configuration for new model
- transferdataHBRData
Data for transfer learning
- freedomfloat
Parameter controlling influence of prior model (0-1)
- x
- HBR
New model instance with transferred knowledge
- chains = 4#
- cores = 4#
- draws = 1500#
- idata: arviz.InferenceData = None#
- init = 'jitter+adapt_diag'#
- likelihood#
- nuts_sampler = 'nutpie'#
- progressbar = True#
- pymc_model: pymc.Model = None#
- tune = 500#