
Algorithm & Biological Foundations
Nick Borcherding
Washington University in St.Β Louis, School of Medicine, St.Β Louis, MO, USAncborch@gmail.com
2026-06-26
Source:vignettes/articles/foundations.Rmd
foundations.RmdOverview
This article describes the mathematical formulation and biological motivation for every component of bHIVE. It is intended as a reference companion to the tutorial vignettes and is organized to mirror the algorithmβs execution flow: initialization, affinity computation, clonal selection, mutation, regulation, and assignment.
Throughout, we use the following notation:
| Symbol | Meaning |
|---|---|
| Data matrix ( observations, features) | |
| Antibody (prototype) matrix ( antibodies) | |
| The -th data point (row of ) | |
| The -th antibody (row of ) | |
| Affinity between a data point and an antibody | |
| Distance between a data point and an antibody |
Shape Space and Affinity
Biological context
In immunology, shape space (Perelson & Oster 1979) is the abstract space in which an antibodyβs binding site (paratope) and an antigenβs epitope are represented as points. Affinity is a function of distance in this space, with closer shapes bind more tightly. The shape space framework provides the geometric foundation for all AIS algorithms: data points are antigens, prototypes are antibodies, and learning is the process of moving antibodies through shape space to maximize affinity to the data.
Affinity kernels
bHIVE computes an affinity matrix where . Five kernels are available:
Gaussian (RBF)
The default kernel. The bandwidth parameter controls how quickly affinity decays with distance. Larger produces sharper, more localized affinity peaks.
Laplace
Similar to Gaussian but decays with L1-like (linear) distance rather than squared distance. Produces heavier tails, making it more robust to outliers.
Polynomial
A dot-product kernel where is a bias term and is the polynomial degree. Captures non-Euclidean similarity structure. Useful when the relevant signal is in the angle and magnitude of feature vectors rather than their Euclidean distance.
Distance functions
For clustering assignment and network suppression, bHIVE uses distance rather than affinity. Six metrics are available:
| Distance | Formula | Notes |
|---|---|---|
| Euclidean | Default; L2 norm | |
| Manhattan | L1 norm; robust to outliers | |
| Minkowski | Generalized; delegates to Euclidean | |
| Cosine | Angular distance | |
| Mahalanobis | Accounts for feature covariance | |
| Hamming | Categorical features |
Initialization
Standard methods
bHIVE provides four initialization strategies for the starting antibody population:
| Method | Algorithm | When to use |
|---|---|---|
"sample" |
Random rows from | Default; fast, data-representative |
"random" |
When you want diversity beyond data support | |
"random_uniform" |
Uniform coverage of feature ranges | |
"kmeans++" |
D2-weighted sampling | Better coverage; cost |
The kmeans++ initialization (Arthur & Vassilvitskii 2007) selects the first antibody uniformly at random, then each subsequent antibody with probability proportional to its squared distance to the nearest existing antibody: .
V(D)J Gene Library (VDJLibrary)
Biological context
Real antibody diversity is generated by V(D)J recombination: the variable region of an immunoglobulin gene is assembled by randomly combining one V (variable), one D (diversity), and one J (joining) gene segment from a germline library. This combinatorial mechanism generates approximately distinct antibodies from only $$300 gene segments.
Algorithm
VDJLibrary translates this to feature space:
-
Segment the feature space into three groups (V, D, J) by splitting the dimensions. Three methods are available:
- PCA: The first principal components form V, the next form D, the remainder form J
- Cluster: k-means clustering within each dimension group creates alleles
- Random partition: Dimensions randomly assigned to V, D, J
Create alleles for each segment by clustering the data projected onto that segmentβs dimensions (using k-means with
nV,nD,nJcenters respectively)Generate antibodies by combinatorial sampling: select one V allele, one D allele, and one J allele, then concatenate to form a complete -dimensional antibody vector
This produces structured coverage of the data manifold with potential combinations, analogous to the combinatorial diversity of the real immune system.
The Core Loop: Clonal Selection
Biological context
Clonal selection theory (Burnet 1959) states that when an antigen enters the body, the B cells whose receptors best match that antigen are selected to proliferate (clonal expansion) and undergo mutation (somatic hypermutation). The result is an iterative refinement process: the population of antibodies evolves toward higher affinity for the antigen.
This is the mechanism that de Castro & Von Zuben (2001) formalized in the AI-Net algorithm and that bHIVE implements with C++ acceleration.
Algorithm
For each iteration :
Step 1: Affinity computation. Compute the affinity matrix using the chosen kernel. This is the most expensive step and is dispatched to BLAS via RcppArmadillo.
Step 2: Clonal expansion and mutation. For each data point :
Select the antibodies with highest affinity:
For each selected antibody , generate clones where:
Mutate each clone: where is drawn from the active SHM strategy (see below). If the mutant has higher affinity than the parent, it replaces the parent.
Step 3: Network regulation. Suppress redundant antibodies (see Idiotypic Network section) or apply simple -threshold suppression: remove if for any .
Step 4: Task-specific assignment.
- Clustering: each data point assigned to the nearest antibody by distance
- Classification: each antibody labeled by majority vote of its assigned data points; predictions via affinity-weighted nearest antibody
Step 5: Convergence check. Early stopping if the
antibody population size has not changed by more than
stopTolerance for noImprovementLimit
consecutive iterations.
Somatic Hypermutation (SHM)
Biological context
Somatic hypermutation is the process by which activated B cells introduce point mutations into their antibody genes at a rate approximately times higher than the background mutation rate. The enzyme activation-induced cytidine deaminase (AID) preferentially targets WRCY/RGYW DNA motifs (hotspots), creating a non-uniform mutation landscape. Mutations that improve antigen binding are selected for; those that reduce it lead to cell death. Over multiple rounds of mutation and selection (affinity maturation), the antibody population converges on high-affinity binders.
Five strategies
1. Uniform
where
is the affinity of the parent antibody,
is mutationDecay, and
is the iteration. The mutation rate decreases with both affinity (better
antibodies mutate less) and time (the population stabilizes).
When to use: Default baseline. Simple and robust.
2. AIRS (Affinity-Proportional)
where
is a scaling constant (c_rate) and
is a temperature parameter. From Watkins & Timmis (2004), this
achieves approximately 50% better data reduction than uniform mutation
by concentrating mutation on low-affinity antibodies.
When to use: When you want the mutation rate to be strictly controlled by affinity, with a tunable temperature.
3. Hotspot (Feature-Weighted)
where is the per-feature βgradientβ toward the matched data point. Features with larger discrepancies mutate more, analogous to AID targeting specific DNA motifs.
When to use: When some features are more informative than others and you want the algorithm to discover this automatically.
4. Energy-Budget
The total mutation magnitude is bounded by an energy budget that shrinks as affinity increases. Inspired by Kleinsteinβs observation that the energetic cost of SHM scales quadratically with the number of mutations (). Individual feature perturbations are sampled uniformly, then rescaled to satisfy the budget constraint.
When to use: When you want hard bounds on how much any single mutation event can perturb an antibody.
5. Adaptive (Adam-Like)
Implementation of a convergence hypothesis. For each feature of each antibody:
This is identical to the Adam optimizer (Kingma & Ba 2015) applied per-feature. The biological interpretation: represents the cellβs βmemoryβ of which direction to mutate (accumulated selection pressure), while tracks the variance of past selection signals (stability of the gradient). Features with consistent directional pressure get larger mutations; features with noisy signals are damped.
When to use: Complex landscapes where different features have different scales and signal-to-noise ratios. Best general-purpose choice.
Idiotypic Network Regulation
Biological context
Jerneβs idiotypic network theory (1974) proposes that antibodies donβt just recognize antigens, but also recognize each other. The variable region of one antibody (its idiotype) can serve as an epitope for another antibody (an anti-idiotype). This creates a regulatory network with emergent properties: memory, tolerance, and self-organized repertoire structure.
Varela & Coutinho (1991) formalized this as a βsecond generationβ immune network with a bell-shaped activation function: below a lower threshold , insufficient stimulation leads to cell death (neglect); between thresholds, moderate stimulation leads to activation; above , excessive stimulation leads to suppression.
This is biologically more realistic than the simple -threshold suppression used in classical AIS, and it produces qualitatively different repertoire dynamics.
Algorithm
Given the antibody matrix with rows:
Step 1: Compute the pairwise affinity matrix where .
Step 2: For each pair , compute the activation signal:
Step 3: Euler-integrate the population dynamics ODE for steps with step size :
where: -
= source_rate (basal production of new cells) -
= decay_rate (natural death) - The activation and
suppression terms are weighted by the actual affinity values
Step 4: Remove antibodies whose population level
falls below survival_threshold.
Parameter guidance
| Parameter | Default | Effect of increasing |
|---|---|---|
theta_low |
0.01 | More antibodies die from neglect |
theta_high |
0.5 | Narrower activation window; more suppression |
source_rate |
0.5 | More new cells enter; larger final repertoire |
decay_rate |
0.1 | Faster natural death; smaller repertoire |
dt |
0.1 | Larger integration steps (less stable if too large) |
timeSteps |
20 | Longer simulation; more time for dynamics to settle |
survival_threshold |
0.5 | Stricter survival; fewer antibodies remain |
Germinal Center Selection
Biological context
The germinal center (GC) is a specialized microstructure within lymph nodes where B cells undergo iterative rounds of mutation (in the dark zone) and selection (in the light zone). In the light zone, B cells compete for help from T follicular helper (Tfh) cells. A Tfh cell evaluates the quality of the B cellβs antigen presentation and provides survival signals only to the best competitors. B cells that fail to receive Tfh help undergo apoptosis.
This is a quality-control bottleneck: it ensures that only improved antibodies survive each round.
Algorithm
The GerminalCenter module implements this as resource
competition:
-
Compute a quality score for each antibody . The scoring is task-aware:
- Classification: = proportion of assigned data points whose label matches the antibodyβs majority label
- Clustering: = mean affinity to assigned data points (cohesion)
Apply selection pressure to compute a survival threshold:
For each of
nTfhhelper cells, the Tfh selects the antibody with highest quality among those not yet helped. Antibodies below the threshold that do not receive Tfh help are removed.Repeat for
roundscycles.
Microenvironment
Biological context
B cell fate is strongly influenced by the tissue microenvironment: chemokine gradients direct migration, cytokines influence differentiation, and local cell density affects competition for resources. In the germinal center, the dark zone (proliferation/mutation) and light zone (selection) create spatially distinct functional regions.
Algorithm
The Microenvironment module classifies each antibodyβs
local context by computing kernel density estimates from the data:
Based on the percentile of within the antibody population:
| Zone | Condition | Mutation modifier | Biological analog |
|---|---|---|---|
| Stable | (reduce) | Light zone; protect good solutions | |
| Explore | (increase) | Dark zone; search sparse regions | |
| Boundary | otherwise | (no change) | Transitional; potential class switch |
where
and
are the high_density_threshold and
low_density_threshold percentiles, respectively.
Two-Signal Activation Gate
Biological context
In real immunity, B cell activation requires two signals:
- Signal 1: Specific antigen recognition by the B cell receptor (affinity-dependent)
- Signal 2: Costimulatory signals from helper T cells, danger signals (DAMPs/PAMPs), or cytokines
This two-signal model (Bretscher & Cohn 1970, formalized for AIS by Freitas 2006) prevents autoimmune activation: a B cell that binds self-antigen without costimulation is anergized (silenced) rather than activated. It serves as a biologically-principled regularization mechanism.
Algorithm
An antibody-antigen interaction is activated only if both signals exceed their thresholds:
Three options for Signal 2:
| Type | Computation | Use case |
|---|---|---|
"density" |
Local data density around | Default; prevents activation in sparse regions |
"danger" |
User-provided per-data-point danger scores | When you have external quality annotations |
"entropy" |
Local label entropy among βs neighbors | Classification; high entropy = uncertain region |
Class Switching
Biological context
During an immune response, B cells can switch the constant region of their antibody (isotype) while retaining the same antigen-binding variable region. The functional consequence is a change in effector properties:
- IgM: Pentameric; broad, low-affinity binding. First responder.
- IgG: Monomeric; high-affinity, highly specific. Dominant in secondary responses.
- IgA: Dimeric; secreted at mucosal surfaces. Boundary patrol.
Algorithm
In bHIVE, class switching changes the effective kernel width of the affinity function, modifying the matching breadth without changing the antibodyβs position in feature space:
| Isotype | Matching behavior | |
|---|---|---|
| IgM | 0.1 (broad) | Large receptive field; captures general patterns |
| IgG | 5.0 (narrow) | Small receptive field; fine-grained discrimination |
| IgA | 1.0 (medium) | Intermediate; boundary patrol |
Switching rules are driven by the Microenvironment zone
assignments:
- Stable zone IgG: data-dense regions benefit from specific matching
- Explore zone IgM: sparse regions need broad coverage to find signal
- Boundary zone IgA: intermediate regions need balanced matching
Memory Pool
Biological context
After an immune response resolves, a subset of activated B cells differentiate into long-lived memory B cells that persist for years. Upon re-encounter with the same antigen, memory cells mount a faster and stronger secondary response.
Algorithm
The MemoryPool module maintains an archive of
high-performing antibodies:
Archive: After each iteration, antibodies whose mean
affinity to their assigned data exceeds archive_threshold
are copied into the memory pool, up to max_memory total
cells. If the pool is full, the lowest- affinity memories are
evicted.
Recall: When new data arrive (or when the data
distribution shifts), memory antibodies whose affinity to the new data
exceeds recall_threshold are reintroduced into the active
repertoire. This provides a warm start for the next round of
adaptation.
Convergent Selection
Biological context
In adaptive immunology, certain TCR/BCR sequences appear in multiple unrelated individuals responding to the same pathogen. These public clonotypes are driven by convergent selection: the structure of the antigen imposes such strong selective pressure that independent immune systems arrive at the same solution.
Algorithm
The ConvergentSelector module finds public antibodies
across multiple independent bHIVE runs:
- Run independent bHIVE analyses (with different random seeds) on the same data
- For each antibody from run 1, check how many other runs contain an
antibody within distance
tolerance - Antibodies that appear in
min_appearancesruns are declared public and retained as consensus prototypes
This implements a biologically-motivated ensemble: rather than averaging predictions, it identifies the prototypes that multiple independent optimization trajectories converge on.
Parameter Guidance
The tables below provide practical starting points and tuning advice for the most important parameters. These are rules of thumb β the optimal settings depend on your data.
Core Parameters
| Parameter | Default | Start here | Tune if⦠|
|---|---|---|---|
nAntibodies |
20 | to | Underfitting (too few) or slow (too many) |
beta |
5 | 3β10 | Low affinity at convergence (increase) |
epsilon |
0.01 | 0.01β0.1 | Too many or too few final antibodies |
maxIter |
50 | 20β100 | Not converged (increase) or slow (decrease) |
k |
3 | 2β5 | Underfitting (increase) or overfitting (decrease) |
affinityFunc |
"gaussian" |
"gaussian" |
Non-Euclidean data (try cosine, polynomial) |
alpha |
1 | All affinities (decrease) or (increase) |
SHM Parameters
| Parameter | Default | When to change |
|---|---|---|
method |
"uniform" |
Try "adaptive" for complex landscapes |
decay |
1.0 | Set if population oscillates and wonβt converge |
mutationMin |
0.01 | Increase if algorithm stalls at local optima |
temperature |
0.5 (airs) | Lower = less mutation on high-affinity antibodies |
base_rate |
0.1 (adaptive) | Scale with feature magnitudes |
Module Interactions
| Combination | Effect | Recommendation |
|---|---|---|
| SHM adaptive + Microenvironment | Zone-dependent adaptive mutation rates | Strong synergy; recommended for complex data |
| Idiotypic + GerminalCenter | Network regulation + quality selection | Complementary; idiotypic controls diversity, GC controls quality |
| VDJLibrary + ActivationGate | Structured init + regularized activation | Good for high-dimensional data |
| ClassSwitcher + Microenvironment | Zone-driven kernel width changes | Requires Microenvironment to define zones |
References
- Arthur, D. & Vassilvitskii, S. (2007). k-means++: The advantages of careful seeding. SODA.
- Bretscher, P. & Cohn, M. (1970). A theory of self-nonself discrimination. Science, 169, 1042β1049.
- Burnet, F.M. (1959). The Clonal Selection Theory of Acquired Immunity. Cambridge University Press.
- de Castro, L.N. & Von Zuben, F.J. (2001). aiNet: Artificial immune network for data analysis. In Data Mining: A Heuristic Approach, 231β260.
- de Castro, L.N. & Timmis, J. (2003). Artificial immune systems as a novel soft computing paradigm. Soft Computing, 7, 526β544.
- Freitas, A.A. (2006). Immunoinformatics. Springer.
- Jerne, N.K. (1974). Towards a network theory of the immune system. Annales dβImmunologie (Institut Pasteur), 125C, 373β389.
- Kingma, D.P. & Ba, J. (2015). Adam: A method for stochastic optimization. ICLR.
- Perelson, A.S. & Oster, G.F. (1979). Theoretical studies of clonal selection: Minimal antibody repertoire size and reliability of self-non-self discrimination. Journal of Theoretical Biology, 81, 645β670.
- Varela, F.J. & Coutinho, A. (1991). Second generation immune networks. Immunology Today, 12, 159β166.
- Watkins, A. & Timmis, J. (2004). Artificial immune recognition system (AIRS): An immune-inspired supervised learning algorithm. Genetic Programming and Evolvable Machines, 5, 291β317.
## R version 4.6.1 (2026-06-24)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.4 LTS
##
## Matrix products: default
## BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
## [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
## [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
## [10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
##
## time zone: UTC
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] viridis_0.6.5 viridisLite_0.4.3 ggplot2_4.0.3 bHIVE_0.99.6
## [5] BiocStyle_2.40.0
##
## loaded via a namespace (and not attached):
## [1] sass_0.4.10 generics_0.1.4 lattice_0.22-9
## [4] digest_0.6.39 magrittr_2.0.5 evaluate_1.0.5
## [7] grid_4.6.1 RColorBrewer_1.1-3 bookdown_0.47
## [10] fastmap_1.2.0 jsonlite_2.0.0 Matrix_1.7-5
## [13] umap_0.2.10.0 RSpectra_0.16-2 gridExtra_2.3.1
## [16] BiocManager_1.30.27 scales_1.4.0 codetools_0.2-20
## [19] textshaping_1.0.5 jquerylib_0.1.4 cli_3.6.6
## [22] rlang_1.2.0 withr_3.0.3 cachem_1.1.0
## [25] yaml_2.3.12 otel_0.2.0 Rtsne_0.17
## [28] tools_4.6.1 parallel_4.6.1 BiocParallel_1.46.0
## [31] dplyr_1.2.1 reticulate_1.46.0 png_0.1-9
## [34] vctrs_0.7.3 R6_2.6.1 lifecycle_1.0.5
## [37] fs_2.1.0 htmlwidgets_1.6.4 ragg_1.5.2
## [40] cluster_2.1.8.2 pkgconfig_2.0.3 desc_1.4.3
## [43] pkgdown_2.2.0 pillar_1.11.1 bslib_0.11.0
## [46] gtable_0.3.6 glue_1.8.1 Rcpp_1.1.1-1.1
## [49] systemfonts_1.3.2 xfun_0.59 tibble_3.3.1
## [52] tidyselect_1.2.1 knitr_1.51 farver_2.1.2
## [55] htmltools_0.5.9 rmarkdown_2.31 clusterCrit_1.3.0
## [58] compiler_4.6.1 S7_0.2.2 askpass_1.2.1
## [61] openssl_2.4.2