{"id":20968557,"url":"https://github.com/rnburn/bbai","last_synced_at":"2026-02-01T18:03:24.909Z","repository":{"id":57451574,"uuid":"258955777","full_name":"rnburn/bbai","owner":"rnburn","description":"Deterministic algorithms for objective Bayesian inference and hyperparameter optimization","archived":false,"fork":false,"pushed_at":"2026-01-30T17:25:21.000Z","size":1323832,"stargazers_count":63,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2026-01-31T10:28:25.294Z","etag":null,"topics":["bayesian-statistics","gaussian-processes","hyperparameter-optimization","machine-learning","python","regression-models","statistics"],"latest_commit_sha":null,"homepage":"https://buildingblock.ai/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc-by-4.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rnburn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2020-04-26T06:21:36.000Z","updated_at":"2026-01-30T17:25:26.000Z","dependencies_parsed_at":"2024-04-05T02:30:19.272Z","dependency_job_id":"307cede4-642f-4dd5-90fd-b59a6fafa093","html_url":"https://github.com/rnburn/bbai","commit_stats":null,"previous_names":["rnburn/peak-engines"],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/rnburn/bbai","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnburn%2Fbbai","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnburn%2Fbbai/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnburn%2Fbbai/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnburn%2Fbbai/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rnburn","download_url":"https://codeload.github.com/rnburn/bbai/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rnburn%2Fbbai/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28984833,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-01T17:52:09.146Z","status":"ssl_error","status_checked_at":"2026-02-01T17:49:53.529Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bayesian-statistics","gaussian-processes","hyperparameter-optimization","machine-learning","python","regression-models","statistics"],"created_at":"2024-11-19T03:14:07.263Z","updated_at":"2026-02-01T18:03:24.903Z","avatar_url":"https://github.com/rnburn.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"## bbai\n![](https://github.com/rnburn/peak-engines/workflows/CI/badge.svg) [![PyPI version](https://img.shields.io/pypi/v/bbai.svg)](https://badge.fury.io/py/bbai) [![License: CC BY 4.0](https://img.shields.io/badge/License-CC%20BY%204.0-lightgrey.svg)](https://creativecommons.org/licenses/by/4.0/) [![API Reference](http://img.shields.io/badge/api-reference-blue.svg)](https://buildingblock.ai/bbai.glm)\n\nDeterministic, exact algorithms for objective Bayesian inference and hyperparameter optimization.\n\n## Installation\n\n**bbai** supports Linux on x86-64.\n\n```\npip install bbai\n```\n\n## Usage\n\n### Fit Cubic Bezier Paths to Parametric Functions\nUse a trust-region optimizer and orthogonal distance fitting to fit cubic Bezier curves\nto a function. This can be used to produce compact, accurate vector graphic plots of \nfunctions for SVG or TikZ:\n\n```python\nfrom bbai.graphics import BezierPath\nimport numpy as np\ndef f(t):\n    return np.sin(6 * t) + np.sin(60 * np.exp(t))\npath = BezierPath(\n    dst_xmin=0, dst_xmax=9.5,\n    dst_ymin=0, dst_ymax=2)\npath.fit(f, -1, 1)\nprint(path.tikz_) \n    # prints a TikZ path for the curve that fits into the window\n    # defined by the points (0, 0) and (9.5, 2)\n```\nSee [doc/bezier-fitting.pdf](doc/bezier-fitting.pdf) for more details.\n\n### Fit Lasso Regression to Optimize LOOCV\nEfficiently fit a lasso regression model with λ set so as to optimize performance on \na leave-one-out cross-validation of the data set.\n\n```python\nfrom sklearn.datasets import load_diabetes\nfrom bbai.glm import Lasso\nX, y = load_diabetes(return_X_y=True)\nmodel = Lasso(loo_mode='lambda').fit(X, y) \n            # Fit lasso regression using hyperparameters that maximize performance on\n            # leave-one-out cross-validation.\nprint(model.lambda_) # print out the hyperparameter that maximizes\n                     # leave-one-out cross validation performance\n  # prints: 22.179\nprint(model.intercept_, model.coef_) # print out the coefficients that maximizes\n                                     # leave-one-out cross validation performance\n  # prints: 152.13 [0, -193.9, 521.8, 295.15, -99.28, 0, -222.67, 0, 511.95, 52.85]\n```\nSee [24-lasso-diabetes](example/24-lasso-diabetes.ipynb) for the full example and [doc/lo-lars.pdf](doc/lo-lars.pdf)\nfor details on the algorithm.\n\n### Fully Bayesian Single-variable Logistic Regression with Reference Prior\nhttps://www.objectivebayesian.com/p/election-2024\n\nBuild a fully Bayesian logistic regression model with a single unknown weight using Jeffreys\nprior (or reference prior, which are the same for only a single parameter).\n\n```python\nfrom bbai.glm import BayesianLogisticRegression1\n\nx = [-5, 2, 8, 1]\ny = [0, 1, 0, 1]\nmodel = BayesianLogisticRegression1()\n\n# Fit a posterior distribution for w with the logistic\n# regression reference prior\nmodel.fit(x, y)\n\n# Print the posterior probability that w \u003c 0.123\nprint(model.cdf(0.123))\n```\n\n\n### Hypothesis testing using Expected Encompassing Intrinsic Bayes Factors (EEIBF)\nhttps://www.objectivebayesian.com/p/hypothesis-testing\n\nThe EEIBF method is described in the paper *Default Bayes Factors for Nonnested Hypothesis Testing*\nby James Berger and Julia Mortera ([postscript](http://www2.stat.duke.edu/~berger/papers/mortera.ps)).\n\nThe python code below shows how to test these three hypotheses for the mean of normally distributed \ndata with unknown variance.\n```\nH_equal: mean = 0\nH_left: mean \u003c 0\nH_right: mean \u003e 0\n```\n```python\nfrom bbai.stat import NormalMeanHypothesis\nimport numpy as np\n\nnp.random.seed(0)\ndata = np.random.normal(0.123, 1.5, size=9)\nprobs = NormalMeanHypothesis().test(data)\nprint(probs.equal) # posterior probability for H_equal 0.235\nprint(probs.left) # posterior probability for H_left 0.0512\nprint(probs.right) # posterior probability for H_right 0.713\n```\nSee [example/19-hypothesis-first-t.ipynb](example/19-hypothesis-first-t.ipynb) for an example and\n[example/18-hypothesis-eeibf-validation.ipynb](example/18-hypothesis-eeibf-validation.ipynb) for a\nstep-by-step validation of the method against the paper.\n\n### Objective Bayesian inference for comparing binomial proportions\nhttps://www.objectivebayesian.com/p/binomial-comparison\n\nFit a posterior distribution with a reference prior to compare binomial proportions:\n```python\nfrom bbai.model import DeltaBinomialModel\n\n# Some example data\na1, b1, a2, b2 = 5, 3, 2, 7\n\n# Fit a posterior distribution with likelihood function\n#     L(theta, x) = (theta + x)^a1 * (1 - theta - x)^b1 * x^a2 (1-x)^b2\n# where theta represents the difference of the two binomial distribution probabilities\nmodel = DeltaBinomialModel(prior='reference')\nmodel.fit(a1, b1, a2, b2)\n\n# Print the probability that theta \u003c 0.123\nprint(model.cdf(0.123))\n     # Prints 0.10907436812863071\n```\n\n### Efficient approximation of multivariable functions using adaptive sparse grids at Chebyshev nodes.\n```python\nfrom bbai.numeric import SparseGridInterpolator\nimport numpy as np\n\n# A test function\ndef f(x, y, z):\n    t1 = 0.68 * np.abs(x - 0.3)\n    t2 = 1.25 * np.abs(y - 0.15)\n    t3 = 1.86 * np.abs(z - 0.09)\n    return np.exp(-t1 - t2 - t3)\n\n# Fit a sparse grid to approximate f\nranges = [(-2, 5), (1, 3), (-2, 2)]\ninterp = SparseGridInterpolator(tolerance=1.0e-4, ranges=ranges)\ninterp.fit(f)\nprint('num_pts =', interp.points.shape[1])\n    # prints 10851\n\n# Test the accuracy at a random point of the domain\nprint(interp.evaluate(1.84, 2.43, 0.41), f(1.84, 2.43, 0.41))\n#    prints 0.011190847391188667 0.011193746554063376\n\n# Integrate the approximation over the range\nprint(interp.integral)\n#    prints 0.6847335267327939\n```\n### Objective Bayesian Inference for Gaussian Process Models\nConstruct prediction distributions for Gaussian process models using full integration over the\nparameter space with a noninformative, reference prior.\n```python\nimport numpy as np\nfrom bbai.gp import BayesianGaussianProcessRegression, RbfCovarianceFunction\n\n# Make an example data set\ndef make_location_matrix(N):\n    res = np.zeros((N, 1))\n    step = 1.0 / (N - 1)\n    for i in range(N):\n        res[i, 0] = i * step\n    return res\ndef make_covariance_matrix(S, sigma2, theta, eta):\n    N = len(S)\n    res = np.zeros((N, N))\n    for i in range(N):\n        si = S[i]\n        for j in range(N):\n            sj = S[j]\n            d = np.linalg.norm(si - sj)\n            res[i, j] = np.exp(-0.5*(d/theta)**2)\n        res[i, i] += eta\n    return sigma2 * res\ndef make_target_vector(K):\n    return np.random.multivariate_normal(np.zeros(K.shape[0]), K)\nnp.random.seed(0)\nN = 20\nsigma2 = 25\ntheta = 0.01\neta = 0.1\nparams = (sigma2, theta, eta)\nS = make_location_matrix(N)\nK = make_covariance_matrix(S, sigma2, theta, eta)\ny = make_target_vector(K)\n\n# Fit a Gaussian process model to the data\nmodel = BayesianGaussianProcessRegression(kernel=RbfCovarianceFunction())\nmodel.fit(S, y)\n\n# Construct the prediction distribution for x=0.1\npreds, pred_pdfs = model.predict([[0.1]], with_pdf=True)\nhigh, low = pred_pdfs.ppf(0.75), pred_pdfs.ppf(0.25)\n\n# Print the mean and %25-%75 credible set of the prediction distribution\nprint(preds[0], '(%f to %f)' % (low, high))\n```\n\n### Ridge Regression\nFit a ridge regression model with the regularization parameter *exactly* set so as to minimize mean squared error on a leave-one-out cross-validation of the training data set\n```python\n# load example data set\nfrom sklearn.datasets import load_boston\nfrom sklearn.preprocessing import StandardScaler\nX, y = load_boston(return_X_y=True)\nX = StandardScaler().fit_transform(X)\n\n# fit model\nfrom bbai.glm import RidgeRegression\nmodel = RidgeRegression()\nmodel.fit(X, y)\n```\n\n### Logistic Regression\nFit a logistic regression model with the regularization parameter *exactly* set so as to maximize likelihood on an approximate leave-one-out cross-validation of the training data set\n```python\n# load example data set\nfrom sklearn.datasets import load_breast_cancer\nfrom sklearn.preprocessing import StandardScaler\nX, y = load_breast_cancer(return_X_y=True)\nX = StandardScaler().fit_transform(X)\n\n# fit model\nfrom bbai.glm import LogisticRegression\nmodel = LogisticRegression()\nmodel.fit(X, y)\n```\n\n### Bayesian Ridge Regression\nFit a Bayesian ridge regression model where the hyperparameter controlling the regularization strength is integrated over.\n```python\n# load example data set\nfrom sklearn.datasets import load_boston\nfrom sklearn.preprocessing import StandardScaler\nX, y = load_boston(return_X_y=True)\nX = StandardScaler().fit_transform(X)\n\n# fit model\nfrom bbai.glm import BayesianRidgeRegression\nmodel = BayesianRidgeRegression()\nmodel.fit(X, y)\n```\n\n### Logistic Regression MAP with Jeffreys Prior\nFit a logistic regression MAP model with Jeffreys prior.\n```python\n# load example data set\nfrom sklearn.datasets import load_breast_cancer\nfrom sklearn.preprocessing import StandardScaler\nX, y = load_breast_cancer(return_X_y=True)\nX = StandardScaler().fit_transform(X)\n\n# fit model\nfrom bbai.glm import LogisticRegressionMAP\nmodel = LogisticRegressionMAP()\nmodel.fit(X, y)\n```\n\n## How it works\n* [Deterministic Objective Bayesian Inference for Spatial Models](https://buildingblock.ai/bayesian-gaussian-process.pdf)\n* [Optimizing Approximate Leave-one-out Cross-validation to Tune Hyperparameters](https://arxiv.org/abs/2011.10218)\n* [An Algorithm for Bayesian Ridge Regression with Full Hyperparameter Integration](https://buildingblock.ai/bayesian-ridge-regression)\n* [How to Fit Logistic Regression with a Noninformative Prior](https://buildingblock.ai/logistic-regression-jeffreys)\n\n## Examples\n\n* [01-digits](https://buildingblock.ai/multinomial-logistic-regression-example): Fit a multinomial logistic regression model to predict digits.\n* [02-iris](example/02-iris.py): Fit a multinomial logistic regression model to the Iris data set.\n* [03-bayesian](example/03-bayesian.py): Fit a Bayesian ridge regression model with hyperparameter integration.\n* [04-curve-fitting](example/04-curve-fitting.ipynb): Fit a Bayesian ridge regression model with hyperparameter integration.\n* [05-jeffreys1](example/05-jeffreys1.ipynb): Fit a logistic regression MAP model with Jeffreys prior and a single regressor.\n* [06-jeffreys2](example/06-jeffreys2.ipynb): Fit a logistic regression MAP model with Jeffreys prior and two regressors.\n* [07-jeffreys-breast-cancer](example/07-jeffreys-breast-cancer.py): Fit a logistic regression MAP model with Jeffreys prior to the breast cancer data set.\n* [08-soil-cn](example/08-soil-cn.ipynb): Fit a Bayesian Gaussian process with a non-informative prior to a data set of soil carbon-to-nitrogen samples.\n* [11-meuse-zinc](example/11-meuse-zinc.ipynb): Fit a Bayesian Gaussian process with a non-informative prior to a data set of zinc concetrations taken in a flood plain of the Meuse river.\n* [13-sparse-grid](example/13-sparse-grid.ipynb): Build adaptive sparse grids for interpolation and integration.\n\n## Documentation\n\n* [buildingblock.ai](https://buildingblock.ai/)\n* [Getting Started](https://buildingblock.ai/get-started)\n* [Logistic Regression Guide](https://buildingblock.ai/logistic-regression-guide)\n* [Reference](https://buildingblock.ai/bbai.glm)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frnburn%2Fbbai","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frnburn%2Fbbai","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frnburn%2Fbbai/lists"}