RATS 10.1
RATS 10.1

Examples /

SHILLERGIBBS.RPF

Home Page

← Previous Next →

SHILLERGIBBS.RPF demonstrates Gibbs sampling analysis of Shiller Smoothness Prior (for a distributed lag). SHILLER.RPF shows the same model done using mixed estimation—see the other example for the description of how the matrices for describing the prior are created.

 

Mixed estimation is a relatively simple two-step procedure which estimates by least squares, then adds "dummy observations" which combine the data and prior information to form a final estimator. This has the technical problem that it, in effect, rescales the prior based upon an estimate of the variance of the residuals. (See the calculation of SCALEFAC in the SHILLER.RPF example). Gibbs sampling uses a similar calculation for the regression coefficients, but treats the variance as random, rescaling the data contribution rather than the prior.

 

Full Program


 

open data haversample.rat
calendar(m) 1947
data(format=rats) 1947:1 2007:4 fltg ftb3
set shortrate = ftb3
set longrate  = fltg
*
* Set the number of draws.
* Set the prior degrees of freedom (NU) and mean (S2) for the precision
* (reciprocal of the variance) for the regression residuals.
* Set the prior precision for the second difference in the coefficients.
*
compute nburn=100
compute ndraws=5000
compute s2=.50^2.0
compute nu=4.0
compute hb=1.0/(.03^2)
*
* Generate the precision matrix for the prior. For this example, the
* prior is flat on the constant and zero lag coefficient, and the second
* differences on the remainder of the lag polynomial are given
* independent Normal priors with common precision. In general, PRIORH
* will be the (generalized) inverse of your prior variance.
*
declare rect dummy(22,26)
declare symm priorh(26,26)
fmatrix(diff=2) dummy 1 3
compute priorh=hb*(tr(dummy)*dummy)
compute priorb=%zeros(26,1)
*
* Compute the cross-moment matrix
*
cmom
# constant shortrate{0 to 24} longrate
*
* Compute the OLS regression to provide an initial value for the beta
* vector.
*
linreg(cmom) longrate
# constant shortrate{0 to 24}
compute beta=betaols=%beta
*
* Specialized code for the bookkeeping is needed in this example We are
* keeping track of the following:
*
*  1. Full distribution on the intercept
*  2. Full distribution on the zero lag coefficient
*  3. Full distribution of the sum of the lag coefficients
*  4. The first and second moments for all coefficients
*
* 1,2 and 3 each require a series with length equal to the number of
* draws. To allow us to compute quickly the sum of the lag coefficients,
* we generate a vector (called SUMMER) which, when dotted with a draw
* for the coefficients, will give the sum of the lag coefficients.
*
* 4 requires simply a pair of series with length equal to the number of
* coefficients, all elements initialized to zero.
*
dec vect summer(%nreg)
ewise summer(i)=%if(i>=2,1.0,0.0)
*
set inter  1 ndraws = 0.0
set coeff1 1 ndraws = 0.0
set sums   1 ndraws = 0.0
*
set comoment1 1 %nreg = 0.0
set comoment2 1 %nreg = 0.0
*
infobox(action=define,progress,lower=-nburn,upper=ndraws) "Gibbs Sampler"
do draw=-nburn,ndraws
   infobox(current=draw)
   *
   * Draw residual precision conditional on previous beta
   *
   compute rssplus=nu*s2+%rsscmom(%cmom,beta)
   compute hu     =%ranchisqr(nu+%nobs)/rssplus
   *
   * Draw betas given hu
   *
   compute beta=%ranmvpostcmom(%cmom,hu,priorh,priorb)
   if draw<=0
      next
   *
   * Do the bookkeeping here. For 1,2,3, stuff the function of the
   * current draw into the "DRAW" element of the result series.
   *
   compute inter(draw)=beta(1)
   compute coeff1(draw)=beta(2)
   compute sums(draw)=%dot(beta,summer)
   *
   * Accumulate the sum and sum of squares of the coefficients
   *
   set comoment1 1 %nreg = comoment1+beta(t)
   set comoment2 1 %nreg = comoment2+beta(t)^2
end do draw
infobox(action=remove)

density(smoothing=1.5) inter  1 ndraws ginter  finter
density(smoothing=1.5) coeff1 1 ndraws gcoeff1 fcoeff1
density(smoothing=1.5) sums   1 ndraws gsums   fsums

scatter(style=lines,window="Posterior for Intercept")
# ginter finter
scatter(style=lines,window="Posterior for Lag 0")
# gcoeff1 fcoeff1
scatter(style=lines,window="Posterior for Sum")
# gsums fsums
*
set comoment1 1 %nreg = comoment1/ndraws
set comoment2 1 %nreg = sqrt(comoment2/ndraws-comoment1^2)
set upper     1 %nreg = comoment1+2.0*comoment2
set lower     1 %nreg = comoment1-2.0*comoment2
set ols       1 %nreg = betaols(t)
*
graph(number=0,window="Graph of Lag Distribution") 4
# comoment1 2 %nreg
# upper     2 %nreg 2
# lower     2 %nreg 2
# ols       2 %nreg

Output


 

Linear Regression - Estimation by Least Squares

Dependent Variable LONGRATE

Monthly Data From 1949:01 To 2007:04

Usable Observations                       700

Degrees of Freedom                        674

Centered R^2                        0.8951770

R-Bar^2                             0.8912889

Uncentered R^2                      0.9839108

Mean of Dependent Variable       6.1440571429

Std Error of Dependent Variable  2.6181144800

Standard Error of Estimate       0.8632282458

Sum of Squared Residuals         502.23986490

Regression F(25,674)                 230.2354

Significance Level of F             0.0000000

Log Likelihood                      -877.0561

Durbin-Watson Statistic                0.0896
 

    Variable                        Coeff      Std Error      T-Stat      Signif

************************************************************************************

1.  Constant                      1.672475104  0.067473794     24.78703  0.00000000

2.  SHORTRATE                     0.453439132  0.086136629      5.26418  0.00000019

3.  SHORTRATE{1}                 -0.094668190  0.144896493     -0.65335  0.51375318

4.  SHORTRATE{2}                  0.052983512  0.152244376      0.34802  0.72793667

5.  SHORTRATE{3}                 -0.018857374  0.153371945     -0.12295  0.90218183

6.  SHORTRATE{4}                  0.063790400  0.153616599      0.41526  0.67808578

7.  SHORTRATE{5}                  0.084543569  0.154480458      0.54728  0.58436975

8.  SHORTRATE{6}                 -0.004286232  0.156341085     -0.02742  0.97813613

9.  SHORTRATE{7}                  0.046222046  0.156782514      0.29482  0.76822497

10. SHORTRATE{8}                 -0.066795220  0.156624402     -0.42647  0.66990338

11. SHORTRATE{9}                  0.020265529  0.153969413      0.13162  0.89532376

12. SHORTRATE{10}                -0.058462752  0.150514569     -0.38842  0.69782848

13. SHORTRATE{11}                 0.056806104  0.149407094      0.38021  0.70390916

14. SHORTRATE{12}                 0.044969624  0.150030177      0.29974  0.76447004

15. SHORTRATE{13}                 0.015649051  0.149403284      0.10474  0.91661036

16. SHORTRATE{14}                -0.078691033  0.150520030     -0.52279  0.60128912

17. SHORTRATE{15}                -0.023182556  0.153947718     -0.15059  0.88034641

18. SHORTRATE{16}                -0.068641873  0.156619259     -0.43827  0.66132938

19. SHORTRATE{17}                 0.050922524  0.156858591      0.32464  0.74555452

20. SHORTRATE{18}                 0.119791197  0.156413095      0.76586  0.44402520

21. SHORTRATE{19}                -0.022025194  0.154557282     -0.14251  0.88672368

22. SHORTRATE{20}                 0.024980054  0.153787232      0.16243  0.87101391

23. SHORTRATE{21}                -0.023470380  0.153579909     -0.15282  0.87858443

24. SHORTRATE{22}                 0.090537387  0.152488292      0.59373  0.55288966

25. SHORTRATE{23}                -0.157174438  0.145139493     -1.08292  0.27923126

26. SHORTRATE{24}                 0.417377352  0.086179819      4.84310  0.00000159


 

Graphs


 


 


 


 


 


 

 


Copyright © 2025 Thomas A. Doan