Inferensys

Glossary

Growing Spheres

A model-agnostic counterfactual generation algorithm that searches for the closest counterfactual by expanding a hypersphere around the query instance until the decision boundary is crossed.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
MODEL-AGNOSTIC COUNTERFACTUAL SEARCH

What is Growing Spheres?

A foundational algorithm for generating counterfactual explanations by systematically expanding a hypersphere in the feature space until a decision boundary is crossed.

Growing Spheres is a model-agnostic counterfactual generation algorithm that identifies the closest counterfactual instance by iteratively expanding a hypersphere centered on the query point. The algorithm samples points on the surface of an ever-widening sphere until it finds one that crosses the model's decision boundary, guaranteeing a minimal-change explanation without requiring access to model gradients or internal parameters.

Unlike gradient-based methods, Growing Spheres operates purely through black-box query access, making it suitable for any classifier. The process begins with a small radius and incrementally grows it, sampling uniformly from the sphere's surface. Once a valid counterfactual is found inside the sphere, a local search refines it toward the boundary to minimize counterfactual proximity, balancing computational cost with the guarantee of finding a nearby valid instance.

ALGORITHM MECHANICS

Key Features of Growing Spheres

The Growing Spheres algorithm generates counterfactuals by systematically expanding a hypersphere around the query instance until the decision boundary is crossed, ensuring minimal change.

01

Model-Agnostic Operation

Growing Spheres treats the target model as a black box, requiring only the ability to query for predictions. It does not rely on gradients, internal weights, or architectural assumptions.

  • Works with any classifier: neural networks, tree ensembles, SVMs
  • No access to model internals required
  • Ideal for auditing proprietary or third-party APIs
  • Operates purely on input-output pairs
Zero
Model Assumptions
02

Hypersphere Expansion Search

The algorithm initializes a hypersphere centered on the query instance and iteratively expands its radius. At each step, it samples points on the sphere's surface until a point with the target prediction is found.

  • Begins with radius = 0 at the query point
  • Uniformly samples from the sphere surface at each expansion step
  • Guarantees the first valid counterfactual found is close to optimal
  • Uses L2 distance as the default proximity metric
03

Two-Phase Generation Process

Growing Spheres operates in two distinct phases to balance exploration and refinement:

Phase 1 — Expansion: The sphere radius grows until a counterfactual is found, establishing an upper bound on the minimal distance.

Phase 2 — Refinement: A binary search between the last unsuccessful radius and the successful radius narrows the distance, producing a counterfactual that lies precisely on the decision boundary.

04

Sparse Feature Selection

After identifying a boundary-crossing point, Growing Spheres applies a feature selection step to reduce the number of changed features, improving interpretability.

  • Uses a greedy algorithm to drop feature changes that do not revert the prediction
  • Prioritizes explanations with fewer altered dimensions
  • Balances proximity with sparsity for human comprehension
  • Outputs a minimal set of actionable changes
05

Handling Immutable Constraints

The algorithm can be extended to respect feasibility constraints by projecting sampled points onto a valid subspace before querying the model.

  • Immutable features are held constant at their original values
  • Categorical features can be handled via one-hot encoding and constrained sampling
  • Ensures generated counterfactuals respect real-world actionability
  • Integrates with formal action set specifications
06

Distance Metric Flexibility

While L2 distance is the default, Growing Spheres supports alternative distance functions to align with domain-specific notions of similarity.

  • Mahalanobis distance accounts for feature correlations and variances
  • Weighted L2 allows domain experts to encode feature importance
  • Custom metrics can reflect cost or difficulty of changing each feature
  • Metric choice directly impacts the plausibility of generated counterfactuals
METHOD COMPARISON

Growing Spheres vs. Other Counterfactual Methods

A technical comparison of the Growing Spheres algorithm against gradient-based and genetic counterfactual generation methods across key operational dimensions.

FeatureGrowing SpheresGradient-BasedGenetic/Evolutionary

Model Access Required

Black-box (predictions only)

White-box (gradient access)

Black-box (predictions only)

Handles Non-Differentiable Models

Handles Categorical Features

Generates Sparse Counterfactuals

Distance Metric

L2 (Euclidean)

L1, L2, or custom loss

Configurable fitness function

Proximity Guarantee

Local minimum (first boundary crossing)

Local minimum (gradient descent)

Global search (population-based)

Computational Cost

Moderate (iterative sampling)

Low (direct optimization)

High (generational evolution)

Risk of Adversarial Examples

Low (expanding from instance)

Moderate (loss minimization)

Low (diversity pressure)

GROWING SPHERES EXPLAINED

Frequently Asked Questions

Clear, technical answers to the most common questions about the Growing Spheres algorithm for generating counterfactual explanations in machine learning models.

The Growing Spheres algorithm is a model-agnostic counterfactual generation method that identifies the closest counterfactual instance by iteratively expanding a hypersphere in the feature space around the query point until the decision boundary of the target class is crossed. The process operates in two distinct phases. First, a growing phase performs a binary search on the radius of a hypersphere centered on the factual instance, sampling points on the sphere's surface until at least one point is classified with the desired target label. This establishes the minimal distance to the decision boundary. Second, a shrinking phase refines the candidate by moving from the initial boundary-crossing point back toward the query instance along a straight line, using a bisection method to find the exact point where the decision flips. Because it relies solely on querying the model's prediction function and requires no gradient information, Growing Spheres works on any classifier, including black-box APIs and non-differentiable models like random forests.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.