VotingHelper Module

Aggregation and voting utilities for ensemble predictions.

class HMB.VotingHelper.VotingHelper[source]

Bases: object

VotingHelper: Collection of voting and aggregation utilities.

Each method takes a list of predictions and optionally a list of weights (same length). Methods return a single aggregated scalar or label depending on the strategy.

WeightedMajorityVoting(predictions, weights)[source]

Compute a weighted majority vote over labels.

Parameters:
  • predictions (iterable) – Iterable of labels.

  • weights (iterable) – Iterable of numeric weights (same length as predictions).

Returns:

The label with the highest cumulative weight.

Return type:

label

MajorityVoting(predictions)[source]

Simple majority vote (unweighted).

Parameters:

predictions (iterable) – Iterable of labels.

Returns:

The most common label.

Return type:

label

WeightedAverageVoting(predictions, weights)[source]

Weighted average of numeric predictions.

Parameters:
  • predictions (iterable) – Iterable of numeric predictions.

  • weights (iterable) – Iterable of numeric weights (same length as predictions).

Returns:

Weighted mean.

Return type:

float

AverageVoting(predictions)[source]

Simple arithmetic mean of numeric predictions.

Parameters:

predictions (iterable) – Iterable of numeric predictions.

Returns:

Arithmetic mean.

Return type:

float

WeightedMedianVoting(predictions, weights)[source]

Weighted median: returns a scalar median taking weights into account.

Parameters:
  • predictions (iterable) – Iterable of numeric predictions.

  • weights (iterable) – Iterable of numeric weights (same length as predictions).

Returns:

Weighted median.

Return type:

float

MedianVoting(predictions)[source]

Unweighted median for numeric predictions.

Parameters:

predictions (iterable) – Iterable of numeric predictions.

Returns:

Median value.

Return type:

float

WeightedModeVoting(predictions, weights)[source]

Weighted mode: returns the label with the highest cumulative weight.

For numeric or non-numeric labels, this method aggregates weights per unique label and returns the label with the maximum total weight. This is equivalent to WeightedMajorityVoting.

Parameters:
  • predictions (iterable) – Iterable of labels (numeric or non-numeric).

  • weights (iterable) – Iterable of numeric weights (same length as predictions).

Returns:

Weighted mode label or numeric value.

Return type:

label or float

ModeVoting(predictions)[source]

Unweighted mode (most common element).

Parameters:

predictions (iterable) – Iterable of labels.

Returns:

Most common label.

Return type:

label

WeightedGeometricMeanVoting(predictions, weights)[source]

Weighted geometric mean for positive numeric predictions.

Parameters:
  • predictions (iterable) – Iterable of positive numeric predictions.

  • weights (iterable) – Iterable of numeric weights (same length as predictions).

Returns:

Weighted geometric mean.

Return type:

float

GeometricMeanVoting(predictions)[source]

Geometric mean for positive numeric predictions.

Parameters:

predictions (iterable) – Iterable of positive numeric predictions.

Returns:

Geometric mean.

Return type:

float

WeightedHarmonicMeanVoting(predictions, weights)[source]

Weighted harmonic mean for positive numeric predictions.

Parameters:
  • predictions (iterable) – Iterable of positive numeric predictions.

  • weights (iterable) – Iterable of numeric weights (same length as predictions).

Returns:

Weighted harmonic mean.

Return type:

float

HarmonicMeanVoting(predictions)[source]

Unweighted harmonic mean for positive numeric predictions.

Parameters:

predictions (iterable) – Iterable of positive numeric predictions.

Returns:

Harmonic mean.

Return type:

float

WeightedQuadraticMeanVoting(predictions, weights)[source]

Weighted root-mean-square (quadratic mean).

Parameters:
  • predictions (iterable) – Iterable of numeric predictions.

  • weights (iterable) – Iterable of numeric weights (same length as predictions).

Returns:

Weighted root-mean-square.

Return type:

float

QuadraticMeanVoting(predictions)[source]

Unweighted root-mean-square.

Parameters:

predictions (iterable) – Iterable of numeric predictions.

Returns:

Root-mean-square.

Return type:

float

WeightedCubicMeanVoting(predictions, weights)[source]

Weighted cubic mean (signed) -> (mean of cubes)^(1/3).

Parameters:
  • predictions (iterable) – Iterable of numeric predictions.

  • weights (iterable) – Iterable of numeric weights (same length as predictions).

Returns:

Weighted cubic mean.

Return type:

float

CubicMeanVoting(predictions)[source]

Unweighted cubic mean.

Parameters:

predictions (iterable) – Iterable of numeric predictions.

Returns:

Cubic mean.

Return type:

float

WeightedQuarticMeanVoting(predictions, weights)[source]

Weighted quartic mean -> 4th root of mean of 4th powers.

Parameters:
  • predictions (iterable) – Iterable of numeric predictions.

  • weights (iterable) – Iterable of numeric weights (same length as predictions).

Returns:

Weighted quartic mean.

Return type:

float

QuarticMeanVoting(predictions)[source]

Unweighted quartic mean.

Parameters:

predictions (iterable) – Iterable of numeric predictions.

Returns:

Quartic mean.

Return type:

float

SoftVoting(probabilityPredictions)[source]

Soft voting: average predicted probabilities across models.

Parameters:

probabilityPredictions (list of dict) – Each dict maps class labels to predicted probabilities.

Returns:

Class with highest average probability.

Return type:

label

ConfidenceWeightedVoting(predictions, confidences)[source]

Weighted voting using model confidence scores.

Parameters:
  • predictions (iterable) – Predicted labels.

  • confidences (iterable) – Confidence scores in [0, 1] or positive reals.

Returns:

Label with highest cumulative confidence-weighted vote.

Return type:

label

BayesianModelAveraging(predictions, modelPosteriors)[source]

Bayesian Model Averaging: weight predictions by posterior model probabilities.

Parameters:
  • predictions (iterable) – Predicted labels from each model.

  • modelPosteriors (iterable) – Posterior probabilities for each model (sum to 1).

Returns:

Label with highest posterior-weighted cumulative probability.

Return type:

label

BordaCountVoting(rankedPredictions)[source]

Borda Count rank aggregation: higher ranks receive more points.

Parameters:

rankedPredictions (list of lists) – Each inner list is a ranking of labels from most to least preferred.

Returns:

Label with the highest cumulative Borda score.

Return type:

label

UncertaintyAwareVoting(predictions, uncertainties, uncertaintyType='inverse')[source]

Aggregate predictions weighted by inverse uncertainty.

Parameters:
  • predictions (iterable) – Predicted labels.

  • uncertainties (iterable) – Uncertainty estimates (lower = more certain).

  • uncertaintyType (str) – “inverse” or “exponential” weighting scheme.

Returns:

Label with highest uncertainty-adjusted cumulative weight.

Return type:

label

EntropyWeightedVoting(probabilityPredictions)[source]

Weight voting by inverse Shannon entropy of probability distributions.

Parameters:

probabilityPredictions (list of dict) – Each dict maps class labels to predicted probabilities.

Returns:

Class with highest entropy-adjusted cumulative weight.

Return type:

label

DiversityWeightedVoting(predictions)[source]

Weight predictions by model diversity (pairwise disagreement).

Parameters:

predictions (list of lists) – Each inner list contains predictions from one model across samples.

Returns:

Aggregated prediction for each sample position.

Return type:

list

CondorcetVoting(rankedPredictions)[source]

Condorcet voting: winner defeats all others in pairwise comparisons.

Parameters:

rankedPredictions (list of lists) – Each inner list is a ranking of labels from most to least preferred.

Returns:

Condorcet winner if one exists, else fallback to Borda winner.

Return type:

label

CalibrationAwareVoting(predictions, calibrationScores)[source]

Weight predictions by model calibration quality (higher = better calibrated).

Parameters:
  • predictions (iterable) – Predicted labels.

  • calibrationScores (iterable) – Calibration metrics in [0, 1] where 1 = perfectly calibrated.

Returns:

Label with highest calibration-weighted cumulative vote.

Return type:

label

MetaWeightLearning(basePredictions, trueLabels)[source]

Learn optimal linear combination weights from validation data.

Parameters:
  • basePredictions (2D list) – Shape (n_models, n_samples) of predictions.

  • trueLabels (list) – Ground truth labels for weight learning.

Returns:

Contains learned weights and aggregation function closure.

Return type:

dict

CopelandVoting(rankedPredictions)[source]

Copeland voting: score candidates by net pairwise wins (wins minus losses).

Parameters:

rankedPredictions (list of lists) – Each inner list is a ranking of labels from most to least preferred.

Returns:

Candidate with highest Copeland score (net pairwise wins).

Return type:

label

RobustMeanVoting(predictions, trimFraction=0.1)[source]

Trimmed mean aggregation: exclude extreme values before averaging.

Parameters:
  • predictions (iterable) – Iterable of numeric predictions.

  • trimFraction (float) – Fraction of extreme values to exclude from each tail (0 to 0.5).

Returns:

Trimmed arithmetic mean.

Return type:

float

ProductOfExpertsVoting(probabilityPredictions, expertWeights=None)[source]

Log-linear pooling: combine probabilities via weighted product (normalized).

Parameters:
  • probabilityPredictions (list of dict) – Each dict maps class labels to predicted probabilities.

  • expertWeights (iterable, optional) – Weight for each expert model (default: uniform weighting).

Returns:

Class with highest pooled probability.

Return type:

label

CorrelationAwareWeightedVoting(predictions, baseWeights=None)[source]

Weight predictions by inverse correlation to promote ensemble diversity.

Parameters:
  • predictions (list of lists) – Each inner list contains predictions from one model across samples.

  • baseWeights (iterable, optional) – Initial weight for each model before correlation adjustment.

Returns:

Aggregated prediction for each sample position.

Return type:

list

AttentionWeightedVoting(predictions, contextFeatures, attentionDim=16)[source]

Attention-based aggregation: compute dynamic weights via scaled dot-product attention.

Parameters:
  • predictions (list of lists) – Shape (n_models, n_samples) of predicted labels.

  • contextFeatures (list of lists) – Shape (n_models, n_features) of model context vectors.

  • attentionDim (int) – Dimensionality of attention projection (default: 16).

Returns:

Aggregated prediction for each sample position using attention-derived weights.

Return type:

list

HedgeAdaptiveVoting(predictions, initialWeights=None, learningRate=0.1, feedback=None)[source]

Online adaptive voting using Hedge algorithm with exponential weighting and regret bounds.

Parameters:
  • predictions (list of lists) – Shape (n_models, n_samples) of predicted labels.

  • initialWeights (iterable, optional) – Initial weight for each model (default: uniform).

  • learningRate (float) – Learning rate for weight updates (0 to 1).

  • feedback (list of lists, optional) – Shape (n_models, n_samples) of per-sample losses.

Returns:

Contains aggregated predictions and final adaptive weights.

Return type:

dict

FedAvgVoting(localPredictions, clientWeights=None, clientDataSizes=None)[source]

Federated averaging aggregator: combine local model predictions weighted by client data size.

Parameters:
  • localPredictions (list of lists) – Shape (n_clients, n_samples) of local predictions.

  • clientWeights (iterable, optional) – Explicit weight for each client (default: data-size proportional).

  • clientDataSizes (iterable, optional) – Number of samples per client for proportional weighting.

Returns:

Aggregated prediction for each sample position using federated weighting.

Return type:

list

MedianOfMeansVoting(predictions, nBuckets=5, randomSeed=42)[source]

Median-of-means aggregation: robust to Byzantine faults and adversarial model contributions.

Parameters:
  • predictions (list of lists) – Shape (n_models, n_samples) of predicted labels.

  • nBuckets (int) – Number of random buckets for partitioning models (default: 5).

  • randomSeed (int) – Seed for reproducible bucket assignment (default: 42).

Returns:

Aggregated prediction for each sample position using robust median-of-means.

Return type:

list

QuantileAggregationVoting(quantilePredictions, targetQuantile=0.5)[source]

Quantile-based aggregation: combine predictive distributions via quantile averaging.

Parameters:
  • quantilePredictions (list of dicts) – Each dict maps quantile levels to predicted values. Example: [{“Q0.1”: 1.0, “Q0.5”: 2.0, “Q0.9”: 3.0}, …]

  • targetQuantile (float) – Target quantile level for aggregation (0 to 1, default: 0.5 for median).

Returns:

Aggregated prediction at the target quantile level.

Return type:

float or label

ConformalPredictionVoting(probabilityPredictions, calibrationScores, targetCoverage=0.95)[source]

Conformal prediction aggregation: return prediction sets with guaranteed marginal coverage.

Parameters:
  • probabilityPredictions (list of dict) – Each dict maps class labels to predicted probabilities.

  • calibrationScores (list of float) – Nonconformity scores from calibration data (lower = better fit).

  • targetCoverage (float) – Desired coverage level in [0, 1] (default: 0.95).

Returns:

Prediction set containing labels that satisfy conformal coverage guarantee.

Return type:

set

DynamicEnsembleSelectionVoting(predictions, competenceScores=None, neighborhoodSize=3)[source]

Dynamic ensemble selection: weight models by local competence estimates per sample.

Parameters:
  • predictions (list of lists) – Shape (n_models, n_samples) of predicted labels.

  • competenceScores (list of lists, optional) – Shape (n_models, n_samples) of competence estimates.

  • neighborhoodSize (int) – Number of nearest neighbors for local competence estimation (default: 3).

Returns:

Aggregated prediction for each sample position using dynamic competence weighting.

Return type:

list

WassersteinBarycenterVoting(distributionPredictions, weights=None, nGrid=100)[source]

Wasserstein barycenter aggregation: combine predictive distributions via optimal transport.

Parameters:
  • distributionPredictions (list of dicts) – Each dict maps numeric values to probability mass. Example: [{“1.0”: 0.2, “2.0”: 0.5, “3.0”: 0.3}, …]

  • weights (iterable, optional) – Weight for each distribution (default: uniform).

  • nGrid (int) – Number of grid points for discretized Wasserstein computation (default: 100).

Returns:

Aggregated prediction at the Wasserstein barycenter (median of barycenter distribution).

Return type:

float

CausalInvariantVoting(predictions, environmentLabels, groundTruth=None)[source]

Causal aggregation: weight models by invariant predictive performance across environments.

Parameters:
  • predictions (list of lists) – Shape (n_models, n_samples) of predicted labels.

  • environmentLabels (list) – Environment identifier for each sample (e.g., [“train”, “test”, “shifted”]).

  • groundTruth (list, optional) – Ground truth labels for computing invariant performance metrics.

Returns:

Aggregated prediction for each sample position using environment-invariant weighting.

Return type:

list

GraphNeuralAggregation(predictions, adjacencyMatrix, aggregationSteps=2, activationType='relu', fixedLabels=None)[source]

GNN-style aggregation: propagate predictions through model dependency graph via message passing.

Parameters:
  • predictions (list of lists) – Shape (n_models, n_samples) of predicted labels.

  • adjacencyMatrix (list of lists) – Shape (n_models, n_models) adjacency matrix for model graph.

  • aggregationSteps (int) – Number of message-passing iterations (default: 2).

  • activationType (str) – Activation function for message aggregation: “relu”, “tanh”, or “identity”.

  • fixedLabels (list, optional) – Fixed ordered list of all possible labels for consistent embedding dimensions.

Returns:

Aggregated prediction for each sample position using graph-propagated weights.

Return type:

list