Debian Codegraph @

tests/estimation/univariate/bayesian.mod

raw
1 // --+ options: json=compute +--
2 // https://www3.nd.edu/~rwilliam/stats1/OLS-Stata9.pdf
3
4 var income educ jobexp race;
5
6 varexo e;
7
8 parameters b1, b2, b3, b4;
9
10 model;
11
12 // The following three lines are required because the variables are declared as endogenous, but the equations
13 // have no consequences on the estimation of the last equation in the block.
14 educ = educ;
15 jobexp = jobexp;
16 race = race;
17
18 [name='eqols']
19 income = b1 + b2*educ + b3*jobexp + b4*race + e;
20
21 end;
22
23 // Load data.
24 ds = dseries('data.csv');
25
26 // Estimate last equatioon by OLS (ds is updated with the fitted values).
27 ds = dyn_ols(ds, {}, {'eqols'});
28
29 /*
30 ** BAYESIAN ESTIMATION OF 'eqols'
31 */
32
33 // Set the number of estimated parameters.
34 n = length(oo_.ols.eqols.beta);
35
36 // We consider a kind of non informative prior by assuming that the inverse of the prior variance for β is zero.
37 V0 = diag(Inf(n, 1));
38
39 // We choose the prior mean for β randomly (around the OLS estimate).
40 beta0 = oo_.ols.eqols.beta + randn(n, 1);
41
42 // Set prior for the variance of the error
43 s2priormean = 17;
44 s2df = 1;
45
46 // Run bayesian estimation with normal-gamma prior. Because there is no closed
47 // form expression for the joint posterior marginal distribution of β, a Gibbs
48 // sampling algorithm is used (the prior for β and the inverse of σ² are independent).
49
50 gibbslength = 300000; // Set the number of iterations in Gibbs
51 burnin = 10000; // Set the number of iterations to be discarded (try to remove the effects of the initial condition).
52 steps = 10; // Do not record all iterations (try to remove the dependence between the draws).
53
54 ds = olsgibbs(ds, 'eqols', beta0, V0, s2priormean, s2df, gibbslength, burnin, steps, {'eqols', 'eqols_olsgibbs_fit'});
55
56 // Since we use a diffuse prior for β, the posterior mean of β should be close to the OLS estimate.
57 if max(abs(oo_.ols.eqols.beta-oo_.olsgibbs.eqols.posterior.mean.beta))>.1
58 error('Something is wrong in the Gibbs sampling routine (univariate model)')
59 end
60
61 // We can plot the posterior distribution of the parameters:
62 post = oo_.olsgibbs.eqols.posterior.density.b2;
63 plot(post(:,1), post(:,2), '-k', 'linewidth', 2);
64 axis tight
65 box on
66 hold on
67 plot([oo_.olsgibbs.eqols.posterior.mean.beta(2), oo_.olsgibbs.eqols.posterior.mean.beta(2)], [0 max(post(:,2))], '-r')
68 hold off