Original link:tecdat.cn/?p=5263

Original source:Tuo End number according to the tribe public number

 

In this article, we will introduce regression modeling in a Bayesian framework and use the PyMC3 MCMC library for reasoning.

 

We will first review the classical frequency-theoretic multiple linear regression method. Then we’ll talk about how Bayes considers linear regression.

Bayesian linear regression was performed with PyMC3

In this section, we will take a classic approach to statistical examples, which is to simulate the data of some properties we know, and then fit a model to extrapolate these original properties.

What is a generalized linear model?

Before we start talking about Bayesian linear regression, I want to briefly outline the concepts of generalized linear models (GLM), as we will use them to build our models in PyMC3.

Generalized linear models are flexible methods for extending ordinary linear regression to more general forms of regression, including logistic regression (classification) and Poisson regression (for counting data), as well as linear regression itself.

GLM allows a dependent variable with an error distribution other than the normal distribution.

PyMC3 was used to simulate the data and fit the model

Before we use PyMC3 to specify and sample the Bayesian model, we need to simulate some noise linear data.

The output is shown below:

 

Simulate noisy linear data with Numpy, PANDAS, and Seaborn

Now that we’ve run the simulation, we want to fit bayesian linear regression to the data. This is the GLM method.

Then we will find the maximum posteriori probability (MAP) estimate for the MCMC sampler. Finally, we will use the no-U-turn Sampler (NUTS) for practical reasoning, then plot the curve of the model, discarding the first 500 samples as a “burn in” pre-firing process.

Traceplot is shown below:

 

The Bayesian GLM linear regression model was fitted to simulated data using PyMC3

 

Firstly, the seaborn LMplot method is used, fit_reg parameter is set to False, and the frequency regression curve is not drawn. We then plot a posterior prediction regression line for 100 samples. Finally, we draw using the original “real” regression line and the parameters of β1=2.

We can see the sampling range of the regression line in the figure below:

 

 

If you have any questions, please contact us!

 


Most welcome insight

1. Matlab uses Bayesian optimization for deep learning

2. Matlab Bayesian hidden Markov HMM model implementation

3. Simple Bayesian linear regression simulation of Gibbs sampling in R language

4. Block Gibbs Sampling Bayesian multiple linear regression in R language

5. Bayesian model of MCMC sampling for Stan probabilistic programming in R language

6.Python implements Bayesian linear regression model with PyMC3

7.R language uses Bayesian hierarchical model for spatial data analysis

8.R language random search variable selection SSVS estimation Bayesian vector autoregression (BVAR) model

9. Matlab Bayesian hidden Markov HMM model implementation