Package: pyblip
pyblip: A python implementation of BLiPIntroductionIn many applications, we can tell that a signal of interest exists but cannot perfectly "localize" it. For example, when regressing an outcomeYon highly correlated covariates (X1, X2), the data may suggest thatat leastone of (X1, X2) influencesY, but it may be challenging to tell which of (X1, X2) is important. Likewise, in genetic fine-mapping, biologists may have high confidence that a gene influences a disease without knowing precisely which genetic variants cause the disease. Similar problems arise in many settings with spatial or temporal structure, including change-point detection and astronomical point-source detection.Bayesian Linear Programming(BLiP) is a method which jointly detects as many signals as possible while localizing them as precisely as possible. BLiP can wrap on top of nearly any Bayesian model or algorithm, and it will return a set of regions which each contain at least one signal with high confidence. For example, in regression problems, BLiP might return the region (X1, X2), which suggests that at least one of (X1, X2) is an important variable. BLiP controls the false discovery rate while also making these regions as narrow as possible, meaning that (roughly speaking) it will perfectly localize signals whenever this is possible!pyblipis an efficient python implementation of BLiP, which is designed so that BLiP can wrap on top of the Bayesian model in only one or two lines of code. For convenience, it also includes fast Bayesian samplers for linear regression, probit regresion, and change point detection, since BLiP can wrap on top of any of these methods.InstallationYou can installpyblipusing pip:``pip install pyblip``Performance improvementBLiP usescvxpyto efficiently solve linear programs.pyblipwill install and run correctly using the defaultcvxpysolvers, but to improve performance, we also recommend installing the Cbc solver. Seethis linkfor system-specific instructions to install Cbc.Installation issuespipshould automatically installpyblip's dependencies. However, if installation fails, it is likely for one of three reasons:You are having trouble installingcvxpyBy default, installingcvxpyalso installs several heavy-duty convex solvers, and installation of these solvers can fail. However,pybliponly requires a few solvers. As a result, it can be easier to install a lightweight version ofcvxpywith the following command:pip install cvxpy-baseand then install theSCSorCBCsolvers. Please see thecvxpy installation instructionsfor more details.You are having trouble installingcython.The Bayesian samplers inpyblipare written in cython to improve performance. If your system is having trouble installing cython, see thecython websitefor instructions.You are having trouble installingcvxopt.pybliprequirescvxoptbecause installingcvxoptis often the easiest way to get access to a solver for mixed-integer linear programs. If you are having trouble installingcvxopt, you can avoid this problem by specifyingdeterministic=Falsewhenever running BLiP, and you will not need a mixed-integer solver. Alternatively, you can follow the instructions on thecvxpy websiteto install any other mixed-integer solver. For example, if you installCBCas described earlier, you do not needcvxopt.Quick startHere, we apply BLiP to perform variable selection in a sparse linear regression problem. The first step is to generate synthetic data and fit the Bayesian model.# Synthetic regression data with AR1 design matrix
	import numpy as np
	import scipy.linalg
	n, p, nsignals, rho = 100, 500, 20, 0.95
	c = np.cumsum(np.zeros(p) + np.log(rho)) - np.log(rho)
	cov = scipy.linalg.toeplitz(np.exp(c))
	X = np.dot(np.random.randn(n, p), np.linalg.cholesky(cov).T)

	# Sparse coefficients for linear model
	beta = np.zeros(p)
	signal_locations = np.random.choice(np.arange(p), nsignals)
	beta[signal_locations] = np.random.randn(nsignals)
	y = np.dot(X, beta) + np.random.randn(n)

	# Spike-and-slab bayesian sampling
	import pyblip
	lm = pyblip.linear.LinearSpikeSlab(
		X=X, y=y, 
	)
	lm.sample(N=1000, chains=10)The second step is to apply BLiP directly on top of the posterior samples of the linear coefficients:detections = pyblip.blip.BLiP(
		samples=lm.betas,
		q=0.1,
		error='fdr'
	)
	for x in detections:
		print("BLiP has detected a signal among {x.group}!")Please seeamspector100.github.io/pyblip/usage.htmlfor examples ranging from variable selection to change-point detection to astronomical point-source detection.DocumentationDocumentation is available atamspector100.github.io/pyblip.ReferenceIf you usepyblipor BLiP in an academic publication, please consider citing Spector and Janson (2022). The bibtex entry is below:@article{AS-LJ:2022,
  title={Controlled Discovery and Localization of Signals via Bayesian Linear Programming},
  author={Spector, Asher and Janson, Lucas},
  journal={arXiv preprint arXiv:2203.17208},
  url={https://arxiv.org/abs/2203.17208},
  year={2022}
}