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.
Glossary
Growing Spheres

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.
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.
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.
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
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
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.
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
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
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
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.
| Feature | Growing Spheres | Gradient-Based | Genetic/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) |
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.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
The Growing Spheres algorithm operates within a broader landscape of counterfactual explanation methods. Understanding these adjacent concepts is critical for designing robust recourse and fairness systems.
Counterfactual Proximity
The fundamental objective that Growing Spheres directly optimizes. It quantifies the distance between the original query instance and the generated counterfactual, typically using L1 or L2 norms. The algorithm's core mechanism—expanding a hypersphere until it intersects the decision boundary—guarantees finding the closest counterfactual in Euclidean space, minimizing this metric by construction.
Decision Boundary
The hypersurface in feature space that separates prediction classes. Growing Spheres explicitly searches for this boundary by sampling points on the surface of an expanding hypersphere. The algorithm's termination condition is the first sphere that contains at least one point classified as the target outcome, effectively identifying the local geometry of the model's decision frontier.
Model-Agnostic Explainability
Growing Spheres is a model-agnostic method, meaning it treats the predictive model as a black box. It requires only query access to the model's prediction function, making it applicable to any classifier—from logistic regression to deep neural networks. This contrasts with gradient-based counterfactual methods that require differentiable model internals.
Sparse Counterfactual
While Growing Spheres finds the closest counterfactual by distance, the resulting instance may alter many features. A sparse counterfactual changes only a small number of features, improving human interpretability. Post-processing steps are often applied to Growing Spheres outputs to enforce sparsity, trading off minimal distance for actionable simplicity.
Algorithmic Recourse
The practical application of counterfactual explanations. Growing Spheres provides the minimal change vector required to flip a negative decision to a positive one. This vector can be translated into actionable recommendations for end-users—for example, specifying exactly how much to increase income or reduce debt to qualify for a loan, enabling concrete recourse.
Plausible Counterfactual
A counterfactual that lies within the high-density region of the training data distribution. Growing Spheres may generate instances on the decision boundary that are unrealistic outliers. Evaluating plausibility using Mahalanobis distance or density estimation is a critical validation step to ensure the generated explanation represents a feasible real-world scenario, not an adversarial artifact.

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us