CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-cmdstanpy

Python interface to CmdStan that provides comprehensive access to the Stan compiler and all Bayesian inference algorithms.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

variational-results.mddocs/

Variational Inference Results

Container for Automatic Differentiation Variational Inference (ADVI) results and approximate posterior samples. The CmdStanVB class provides access to variational parameters and generated posterior samples.

Capabilities

Variational Parameters

Access the mean-field or full-rank variational parameters that define the approximate posterior.

def variational_params_np(self):
    """
    Get variational parameters as NumPy array.
    
    Returns:
    np.ndarray: Variational parameter values (means and standard deviations)
    """

def variational_params_pd(self):
    """
    Get variational parameters as pandas DataFrame.
    
    Returns:
    pd.DataFrame: Parameters with names as index
    """

def variational_params_dict(self):
    """
    Get variational parameters as dictionary.
    
    Returns:
    dict: Mapping from parameter names to arrays
    """

Posterior Samples

Access samples drawn from the variational approximation.

def variational_sample(self):
    """
    Get posterior sample from variational approximation.
    
    Returns:
    np.ndarray: Draws from approximate posterior (draws, parameters)
    """

def variational_sample_pd(self):
    """
    Get posterior sample as pandas DataFrame.
    
    Returns:
    pd.DataFrame: Draws with parameter names as columns
    """

Variable Access

def stan_variable(self, var):
    """
    Get posterior sample for specific Stan variable.
    
    Parameters:
    - var (str): Variable name
    
    Returns:
    np.ndarray: Draws for the variable
    """

def stan_variables(self):
    """
    Get all Stan variables as dictionary.
    
    Returns:
    dict: Mapping from variable names to draw arrays
    """

File Operations

def save_csvfiles(self, dir=None):
    """
    Save CSV output files to directory.
    
    Parameters:
    - dir (str or PathLike, optional): Target directory
    
    Returns:
    None
    """

Properties

# Variational information
vb.columns              # int: Number of parameters
vb.column_names         # Tuple[str, ...]: Parameter names
vb.eta                  # float: Final step size
vb.metadata            # InferenceMetadata: Run configuration and timing

Usage Examples

# Run variational inference
vb = model.variational(data=data, algorithm="meanfield", draws=2000)

# Access variational parameters
var_params = vb.variational_params_dict()
print("Variational means:", var_params)

# Get posterior samples
posterior_samples = vb.variational_sample()
theta_draws = vb.stan_variable("theta")

print(f"Posterior mean for theta: {theta_draws.mean()}")
print(f"Posterior std for theta: {theta_draws.std()}")

Install with Tessl CLI

npx tessl i tessl/pypi-cmdstanpy

docs

advanced-variational.md

data-io-utilities.md

generated-quantities.md

index.md

installation-setup.md

mcmc-results.md

model-compilation.md

model-interface.md

optimization-results.md

variational-results.md

tile.json