Skip to content

Commit

Permalink
version 0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick T. Brandt authored and gaborcsardi committed Aug 6, 2010
1 parent 4fb76c2 commit 8e7ecf4
Show file tree
Hide file tree
Showing 72 changed files with 2,514 additions and 3,777 deletions.
14 changes: 7 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Package: MSBVAR
Version: 0.4.0
Date: 2009-06-12
Version: 0.5.0
Date: 2010-08-06
Title: Markov-Switching, Bayesian, Vector Autoregression Models
Author: Patrick T. Brandt <pbrandt@utdallas.edu>
Maintainer: Patrick T. Brandt <pbrandt@utdallas.edu>
Depends: R (>= 2.8.0), KernSmooth, xtable, coda, bit, mvtnorm
Depends: R (>= 2.11.0), KernSmooth, xtable, coda, bit, mvtnorm
Description: Provides methods for estimating frequentist and Bayesian
Vector Autoregression (VAR) models. Functions for reduced form
and structural VAR models are also available. Includes methods
Expand All @@ -13,11 +13,11 @@ Description: Provides methods for estimating frequentist and Bayesian
forecast error decompositions. Also includes utility functions
for plotting forecasts and impulse responses, and generating
draws from Wishart and singular multivariate normal densities.
Current version includes some limited functionality to build
models with Markov switching.
Current version includes functionality to build models with
Markov switching.
LazyLoad: yes
License: GPL (>= 2)
URL: http://www.utdallas.edu/~pbrandt/
Packaged: 2009-07-20 21:48:49 UTC; patrick
Packaged: 2010-08-07 00:04:02 UTC; patrick
Repository: CRAN
Date/Publication: 2009-07-21 12:26:57
Date/Publication: 2010-08-07 07:35:45
15 changes: 6 additions & 9 deletions INDEX
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ BHLK.filter Baum-Hamilton-Lindgren-Kim state-space filter
IsraelPalestineConflict
Weekly Goldstein Scaled Israeli-Palestinian
Conflict Data, 1979-2003
SS.draw State-space filter and sampler for a
Markov-switching VAR model
SZ.prior.evaluation Sims-Zha Bayesian VAR Prior Specification
Search
cf.forecasts Compare VAR forecasts to each other or real
Expand All @@ -17,18 +19,14 @@ forc.ecdf Empirical CDF computations for posterior
forecast Generate forecasts for fitted VAR objects
gibbs.A0 Gibbs sampler for posterior of Bayesian
structural vector autoregression models
gibbs.msbsvar Gibbs-Metropolis-Hastings sampler for posterior
of a Markov-switching Bayesian structural
vector autoregression model
gibbs.msbvar Gibbs sampler for a Markov-switching Bayesian
reduced form vector autoregression model
granger.test Bivariate Granger causality testing
hc.forecast Forecast density estimation of hard condition
forecasts for VAR models via MCMC
irf Impulse Response Function (IRF) Computation for
a VAR
irf.msbsvar Monte Carlo Integration / Simulation of Impulse
Response Functions for an MSBSVAR model.
ldwishart Log density for a Wishart variate
list.print Prints a list object for the VAR and BVAR
models in MSBVAR
mae Mean absolute error of VAR forecasts
Expand All @@ -40,17 +38,14 @@ mean.SS Summary measures and plots for MS-B(S)VAR
state-spaces
mountains Mountain plots for summarizing forecast
densities
msbsvar Markov-Switching Sims-Zha Bayesian VAR Model
estimation
msbvar Markov-switching Bayesian reduced form vector
autoregression model setup and posterior mode
estimation
normalize.svar Likelihood normalization of SVAR models
null.space Find the null space of a matrix
plot.forc.ecdf Plots VAR forecasts and their empirical error
bands
plot.forecast Plots competing sets of VAR forecasts or a
single set of VAR forecasts
plot.forecast Plot function for forecasts
plot.gibbs.A0 Plot a parameter density summary for B-SVAR
A(0) objects
plot.irf Plots impulse responses
Expand All @@ -71,6 +66,8 @@ rmultnorm Multivariate Normal Random Number Generator
rwishart Random deviates from a Wishart distribution
summary Summary functions for VAR / BVAR / B-SVAR model
objects
summary.forecast Summary functions for forecasts obtained
through VAR / BVAR / B-SVAR model objects
szbsvar Structural Sims-Zha Bayesian VAR model
estimation
szbvar Reduced form Sims-Zha Bayesian VAR model
Expand Down
3 changes: 1 addition & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MSBVAR Package (MSBVAR)
Copyright (C) 2005-2008 Patrick T. Brandt
Copyright (C) 2005-2010 Patrick T. Brandt

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -27,4 +27,3 @@ Email: pbrandt@utdallas.edu
WWW: http://www.utdallas.edu/~pbrandt
http://yule.utdallas.edu

With any problems or questions.
97 changes: 59 additions & 38 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import(KernSmooth)
import(coda)

export(dfev,
dfev.VAR,
dfev.BVAR,
dfev.BSVAR,
# dfev.VAR,
# dfev.BVAR,
# dfev.BSVAR,
print.dfev,
summary.dfev,
granger.test,
Expand All @@ -21,50 +21,63 @@ export(dfev,
normalize.svar,
A02mcmc,
irf,
irf.VAR,
irf.BVAR,
irf.BSVAR,
irf.msbsvar,
# irf.VAR,
# irf.BVAR,
# irf.BSVAR,
# irf.msbsvar,
mc.irf,
mc.irf.VAR,
mc.irf.BVAR,
mc.irf.BSVAR,
# mc.irf.VAR,
# mc.irf.BVAR,
# mc.irf.BSVAR,
null.space,
mae,
restmtx,
rmse,
rmultnorm,
ldwishart,
rwishart,
cf.forecasts,
forc.ecdf,
forecast,
forecast.VAR,
forecast.BVAR,
forecast.BSVAR,
# forecast.VAR,
# forecast.BVAR,
# forecast.BSVAR,
hc.forecast,
hc.forecast.VAR,
# hc.forecast.VAR,
uc.forecast,
uc.forecast.VAR,
# uc.forecast.VAR,
posterior.fit,
posterior.fit.BVAR,
posterior.fit.BSVAR,
# posterior.fit.BVAR,
# posterior.fit.BSVAR,
# posterior.fit.MSBVAR,
mountains,
plot.gibbs.A0,
plot.irf.VAR,
plot.irf.BVAR,
plot.irf.BSVAR,
plot.mc.irf.VAR,
plot.mc.irf.BVAR,
plot.mc.irf.BSVAR,
plot.forecast.VAR,
plot.irf,
# plot.irf.VAR,
# plot.irf.BVAR,
# plot.irf.BSVAR,
plot.mc.irf,
# plot.mc.irf.VAR,
# plot.mc.irf.BVAR,
# plot.mc.irf.BSVAR,
plot.forecast,
# plot.forecast.VAR,
# plot.forecast.BVAR,
# plot.forecast.BSVAR,
plot.forc.ecdf,
print.posterior.fit.BVAR,
print.posterior.fit.BSVAR,
summary.VAR,
summary.BVAR,
summary.BSVAR,
print.posterior.fit,
# print.posterior.fit.BVAR,
# print.posterior.fit.BSVAR,
summary,
# summary.VAR,
# summary.BVAR,
# summary.BSVAR,
summary.forecast,
# summary.forecast.VAR,
# summary.forecast.BVAR,
# summary.forecast.BSVAR,
list.print,
# SS.draw,
SS.draw,
sum.SS,
mean.SS,
plot.SS,
Expand All @@ -76,8 +89,8 @@ export(dfev,
ddirichlet,
rdirichlet,
# hregime.SS,
msbsvar,
gibbs.msbsvar,
# msbsvar,
# gibbs.msbsvar, # Leave out until we update.
msbvar,
gibbs.msbvar
# hregime.reg
Expand All @@ -87,21 +100,29 @@ export(dfev,

S3Method(print, posterior.fit.BVAR)
S3Method(print, posterior.fit.BSVAR)
S3Method(print, posterior.fit.MSBVAR)
S3Method(print, dfev)

S3Method(summary, dfev)

S3Method(summary, forecast.VAR)
S3Method(summary, forecast.BVAR)
S3Method(summary, forecast.BSVAR)

S3Method(plot, forecast)

S3Method(summary, VAR)
S3Method(summary, BVAR)
S3Method(summary, BSVAR)

S3Method(plot, irf.VAR)
S3Method(plot, irf.BVAR)
S3Method(plot, irf.BSVAR)
S3Method(plot, irf)
#S3Method(plot, irf.BVAR)
#S3Method(plot, irf.BSVAR)

S3Method(plot, mc.irf.VAR)
S3Method(plot, mc.irf.BVAR)
S3Method(plot, mc.irf.BSVAR)
S3Method(plot, mc.irf)
#S3Method(plot, mc.irf.VAR)
#S3Method(plot, mc.irf.BVAR)
#S3Method(plot, mc.irf.BSVAR)

S3Method(plot, forecast.VAR)

Expand Down
83 changes: 28 additions & 55 deletions R/SS.R
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
## SS.R: R-side functions for the SS C++ compressed state space class
## Date: 2007-11-15 -- Initial version
## 2008-10-14 -- Revised to use the bit package for storage.
## SS.R: R-side functions for the SS class objects that describe the
## state-space for an MSBVAR model
##
## Date: 20071115 : Initial version
## 20081014 : Revised to use the bit package for storage.
## 20100617 : Updated to move sampler to gibbs.msbvar. This
## file only now handles the posterior
## post-processing of the SS classed objects.
## Description:
##
## Functions to draw samples of the state space for a model with filter
## probabilities 'fp'. State spaces computed and stored in compressed
## State spaces computed and stored in compressed
## form to reduce the computation and memory overhead involved in
## computing and storing state space draws. R side storage of these
## state-spaces is done using the bit package.
## state spaces is done using the bit package. Functions here are
## used to summarize and plot the posterior state spaces.


# Draws a Markov state-space for a Markov-switching model. This
# function does it all: filters, smoothes and samples the 0-1 elements
# of the state-space.
SS.draw <- function(xi, b, Q, m, h, n0, init.model, fp)
{
SS <- hregime.SS(h, m, fp, b, xi, n0, init.model, method="update.Q")
TT <- nrow(fp)
SSe <- matrix(0, TT*m, h); Xik <- matrix(0, m*m, h)

xi.tmp <- matrix(xi^2, m, h);
for(i in 1:h){
Xik[,i] <- as.vector(diag(1/xi.tmp[,i]))
SSe[,i] <- as.vector(SS$e[,,i])
}

SQ <- steady.Q(Q)
# Call C side SS draw function
out <- .Call("SS.draw.cpp", SSe, Xik, Q, SQ, as.integer(TT),
as.integer(h), as.integer(m))
return(out);
}
######################################################################
# Summary functions for MS state-spaces from the BHLK filter or other
# estimators for the state-space
######################################################################

# Summary functions for MS state-spaces.
######################################################################
# Sums of the number of draws in each state as a function of the number
# of periods TT
######################################################################

sum.SS <- function(x, ...)
{
h <- x$h
N <- length(x$ss.sample)
ss <- x$ss.sample
TT <- attributes(x$ss.sample[[1]])$n
TTh <- TT*(h-1)
TTh <- virtual(x$ss.sample[[1]])$Length
TT <- TTh/(h-1)

# Now find the sums
sums <- apply(matrix(unlist(lapply(x$ss.sample, as.integer)), nrow=TTh, ncol=N), 1, sum)
Expand All @@ -50,47 +40,30 @@ sum.SS <- function(x, ...)
return(sums)
}

sum.SS1 <- function(x, c, ...)
{
h <- x$h
N <- length(x$ss.sample)
ss <- x$ss.sample
TT <- attributes(x$ss.sample[[1]])$n
TTh <- TT*(h-1)
sums <- matrix(0, TT, h)

# Now find the sums
tmp <- matrix(unlist(lapply(x$ss.sample, as.integer)), nrow=TTh,
ncol=N)


for(i in 1:N)
{
tmp2 <- matrix(0, TT, h)
tmp3 <- -1*(tmp[,i]-1)
tmp2[,seq(c$cluster[i], by=ifelse(c$cluster[i]>1, -1, 1),
length.out=2)] <- cbind(tmp[,i], tmp3)
sums <- sums + tmp2
}
return(sums)
}

######################################################################
# Mean regime probability for each state
######################################################################
mean.SS <- function(x, ...){
sums <- sum.SS(x)
N <- length(x$ss.sample)
return(sums/N)
}

######################################################################
# Plot the mean posterior regime probabilities
######################################################################

plot.SS <- function(x, ylab="State Probabilities", ...)
{
tmp <- mean.SS(x)
plot(ts(tmp), plot.type="single", col=1:ncol(tmp),
ylim=c(0,1), ylab=ylab, ...)
abline(h=0.5, lty=2, ...)

}

# Legacy R code version of some state-space generation functions --
# for legacy and exposition purposes.

# Function to generate the vector of binary indicators of the state
# space. This is a state-space generation function for one
Expand Down

0 comments on commit 8e7ecf4

Please sign in to comment.