function [b0, m] = sample_beta0(X, z, beta, sigma2, omega2)
%SAMPLE_BETA0 samples the intercept parameter beta0.
%   [b0, m] = sample_beta0(...) samples the intercept parameter
%   beta0 from the conditional posterior distribution.
%
%
%   The input arguments are:
%       X       - [n x p] data matrix 
%       z       - [n x 1] target vector
%       beta    - [p x 1] regression parameters
%       sigma2  - [1 x 1] noise variance
%       omega2  - [n x 1] hyperparameters
%
%   Return values:
%       b0      - a beta0 sample from the posterior distribution
%       m       - posterior mean
%
%   (c) Copyright Enes Makalic and Daniel F. Schmidt, 2016

W = sum(1./omega2);
m = sum((z - X*beta) ./ omega2) / W;
v = sigma2 / W;

b0 = m + randn(1)*sqrt(v);

end