KernelMix
KernelMix fits Bayesian kernel machine regression (BKMR) for exposure mixtures in Python. It models how an outcome varies with several exposures together, allowing curved associations and interactions between exposures. Other measured variables enter as linear adjustment terms.
To handle different outcome types, the package combines this exposure function with continuous, binary, count or survival models. NumPy and SciPy handle fitting and prediction; R is not required.
| Task | Start here |
|---|---|
| Choose an outcome model and priors | Models and assumptions |
| Fit, inspect, predict and compare exposures | Analysis tutorial |
| Model clusters, kernels or detailed survival records | Structured models |
| Find arguments and result fields | API reference |
| Check numerical evidence and limitations | Evidence |
Example
import numpy as np
from kernelmix import fit, univariate_response, plot_response
rng = np.random.default_rng(17)
Z = rng.normal(size=(24, 2))
y = np.sin(Z[:, 0]) + 0.3 * Z[:, 1] + rng.normal(0, 0.5, 24)
result = fit(y, Z, draws=200, warmup=200, chains=2, seed=17)
print(result.summary())
curve = univariate_response(result, variable=0, points=25, seed=17)
ax = plot_response(curve)
Example output (first rows):
BKMRFit(family='gaussian', n=24, exposures=2, chains=2, draws=200)
95% pointwise credible intervals
Parameter Mean SD Lower Upper
lambda 16.93 9.047 4.145 38.61
sigma2 0.2159 0.08346 0.1142 0.4188
These short chains illustrate the output and trigger mixing warnings. For an analysis, use longer runs and inspect agreement between chains and effective sample sizes, which account for dependence between draws.

The curve varies the first exposure with the second at its median, relative to the all-median exposure profile. It describes a fitted association; a causal interpretation requires additional assumptions.
Documentation
Browse the searchable documentation.
The common questions explain prediction scales, missing values, selection and diagnostics. The observed ozone example includes source data and saved results. Download the reference manual for offline reading. Contributors can find development checks separately.
Citation
Chapagain, N. (2026). KernelMix: Bayesian kernel machine regression in Python. Preprint.
Cite the statistical methods used and record the KernelMix version.
@misc{chapagain2026kernelmix,
author = {Chapagain, Nilson},
title = {{KernelMix: Bayesian kernel machine regression in Python}},
year = {2026},
howpublished = {Preprint, ResearchGate},
url = {https://www.researchgate.net/publication/416822910_KernelMix_Bayesian_kernel_machine_regression_in_Python}
}
References and license
Bobb et al. (2015) introduced BKMR for exposure-mixture analysis. Bobb et al. (2018) described the R software and its binary-outcome extension. KernelMix implements these methods separately in Python and adds count and survival likelihoods, defined in the model guide.
We compare selected Gaussian and probit posterior summaries and conditional calculations, including random intercepts, with installed R bkmr 0.2.2. The evidence summary records results and limits. See NOTICE for attribution and LICENSE for GPL-2.0-only terms.
Acknowledgements
Computations were performed on the Arseven Computing Cluster, Department of Statistics, Texas A&M University.
This work received no grant funding.