executing-plans
executing-plans
Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available
ds-bayesian-inference"Applies Bayesian methods for prior selection, posterior estimation and probabilistic inference in machine learning models"
Independent GitHub snapshot · Automated classification · Original documentation governs
name: ds-bayesian-inference compatibility: opencode completeness: 95 content-types:
Comprehensive guide to bayesian inference in machine learning and data science workflows.
Bayesian Inference is a critical component of the machine learning workflow. This skill covers:
import numpy as np
import pandas as pd
from scipy import stats
from typing import Tuple, Dict
def bayesian_linear_regression(
X: np.ndarray,
y: np.ndarray,
prior_precision: float = 1.0
) -> Dict[str, np.ndarray]:
"""
Perform Bayesian Linear Regression using conjugate Normal-Gamma priors.
Computes posterior distribution over regression weights analytically.
Args:
X: Feature matrix of shape (n_samples, n_features)
y: Target vector of shape (n_samples,)
prior_precision: Precision (inverse variance) of the prior on weights
Returns:
Dictionary containing posterior mean, covariance, and noise variance
"""
if X.ndim == 1:
X = X.reshape(-1, 1)
n_samples, n_features = X.shape
X_b = np.column_stack([np.ones(n_samples), X])
# Initialize prior covariance matrix
prior_cov = np.eye(n_features + 1) / prior_precision
# Estimate initial noise variance from data
beta_mle = np.linalg.lstsq(X_b, y, rcond=None)[0]
residuals = y - X_b @ beta_mle
sigma2_init = np.sum(residuals**2) / n_samples
# Compute posterior precision and covariance
posterior_precision = np.linalg.inv(prior_cov) + (1.0 / sigma2_init) * (X_b.T @ X_b)
posterior_cov = np.linalg.inv(posterior_precision)
# Compute posterior mean
posterior_mean = posterior_cov @ (
np.linalg.inv(prior_cov) @ np.zeros(n_features + 1) +
(1.0 / sigma2_init) * (X_b.T @ y)
)
return {
'posterior_mean': posterior_mean
'posterior_cov': posterior_cov
'noise_variance': sigma2_init
'n_samples': n_samples
'n_features': n_features
}
import logging
import numpy as np
import pandas as pd
from typing import Any, Dict, Optional
from scipy import stats
logger = logging.getLogger(__name__)
class BayesianInferenceEngine:
"""Production-grade Bayesian Inference engine for regression tasks."""
def __init__(self, prior_precision: float = 1.0, confidence_level: float = 0.95) -> None:
self.prior_precision = prior_precision
self.confidence_level = confidence_level
logger.info("BayesianInferenceEngine initialized with prior_precision=%.2f, confidence=%.2f",
prior_precision, confidence_level)
def _validate_input(self, data: pd.DataFrame) -> None:
if data is None or data.empty:
raise ValueError("Input data cannot be None or empty")
if not isinstance(data, pd.DataFrame):
raise TypeError("Input must be a pandas DataFrame")
if data.shape[0] < 2:
raise ValueError("Insufficient data for inference: requires at least 2 samples")
def execute(self, data: pd.DataFrame, target_col: str = 'y') -> Dict[str, Any]:
"""Execute Bayesian inference on provided data and return structured results."""
self._validate_input(data)
logger.info("Running Bayesian inference on %d samples", len(data))
X = data.drop(columns=[target_col]).values
y = data[target_col].values
n, d = X.shape
X_b = np.column_stack([np.ones(n), X])
prior_cov = np.eye(d + 1) / self.prior_precision
sigma2 = np.var(y - np.mean(y))
post_prec = np.linalg.inv(prior_cov) + (1.0 / sigma2) * (X_b.T @ X_b)
post_cov = np.linalg.inv(post_prec)
post_mean = post_cov @ (np.linalg.inv(prior_cov) @ np.zeros(d + 1) +
(1.0 / sigma2) * (X_b.T @ y))
y_pred = X_b @ post_mean
pred_var = sigma2 * (1 + np.sum(X_b * (post_cov @ X_b.T), axis=1))
ci_half_width = stats.norm.ppf((1 + self.confidence_level) / 2) * np.sqrt(pred_var)
return {
'posterior_mean': post_mean
'posterior_cov': post_cov
'predictions': y_pred
'lower_bound': y_pred - ci_half_width
'upper_bound': y_pred + ci_half_width
'noise_variance': sigma2
'status': 'success'
}
| Pitfall | Problem | Solution | |
Authoritative documentation links for this skill's domain. The model follows markdown links at load time to resolve external references and inline content.