SVAR_IRF.inp (gretl-2020a.tar.xz) | : | SVAR_IRF.inp (gretl-2020b.tar.xz) | ||
---|---|---|---|---|

skipping to change at line 54 | skipping to change at line 54 | |||

# sel = selifr(transp(seq(1,n*n)), vec(tmp)) | # sel = selifr(transp(seq(1,n*n)), vec(tmp)) | |||

ret[, SVARobj.cumsel] = cum(ret[, SVARobj.cumsel]) # .cumsel was sel | ret[, SVARobj.cumsel] = cum(ret[, SVARobj.cumsel]) # .cumsel was sel | |||

endif | endif | |||

matrix SVARobj.IRFs = ret | matrix SVARobj.IRFs = ret | |||

end function | end function | |||

############## | ############## | |||

function matrix FEVD(bundle *SVARobj, int drawix[0::0]) | function matrix FEVD(bundle *SVARobj, int drawix[0::0]) | |||

# (drawix only needed/meant for the set id case (type 10)) | # (drawix only meant for the set id case (type 10)) | |||

n = SVARobj.n | n = SVARobj.n | |||

h = SVARobj.horizon + 1 | h = SVARobj.horizon + 1 | |||

if SVARobj.type == 10 | if SVARobj.type == 10 | |||

# In the set id case in each accepted draw the impulse | # In the set id case in each accepted draw the impulse | |||

# responses are already stored as "irfs"; however, the format | # responses are already stored as "irfs"; however, the format | |||

# there is an array of matrices. | # there is an array of matrices. | |||

errchkSRhisto(&SVARobj, drawix) | errchkSRhisto(&SVARobj, drawix) | |||

bundle pickdraw = SVARobj.acc_draws[drawix] | ||||

# allow drawix to override the setting in the bundle | ||||

whichdraw = drawix ? drawix : SVARobj.bestdraw | ||||

bundle pickdraw = SVARobj.acc_draws[whichdraw] | ||||

if cols(pickdraw.irfs[1]) != n | if cols(pickdraw.irfs[1]) != n | |||

funcerr "partial id not supported for FEVD" | funcerr "partial id not supported for FEVD" | |||

elif h != nelem(pickdraw.irfs) | elif h != nelem(pickdraw.irfs) | |||

funcerr "horizon mismatch" | funcerr "horizon mismatch" | |||

endif | endif | |||

if !inbundle(pickdraw, "IRFs") # maybe have already been added there | if !inbundle(pickdraw, "IRFs") # maybe have already been added there | |||

matrix IRFs = zeros(h, n*n) | putIrf_to_accdraw(&SVARobj, whichdraw) | |||

loop ix = 1..h -q | ||||

IRFs[ix, ] = vec(pickdraw.irfs[ix])' | # matrix IRFs = zeros(h, n*n) | |||

endloop | # loop ix = 1..h -q | |||

# copy to origin | # IRFs[ix, ] = vec(pickdraw.irfs[ix])' | |||

matrix SVARobj.acc_draws[drawix].IRFs = IRFs | # endloop | |||

else | ## copy to origin | |||

matrix IRFs = pickdraw.IRFs | # matrix SVARobj.acc_draws[whichdraw].IRFs = IRFs | |||

# else | ||||

# matrix IRFs = pickdraw.IRFs | ||||

endif | endif | |||

matrix IRFs = SVARobj.acc_draws[whichdraw].IRFs | ||||

else | else # standard non-SR model | |||

if drawix > 0 | ||||

print "Warning: 'drawix' arg meaningless for standard SVAR, ignoring | ||||

" | ||||

endif | ||||

matrix IRFs = SVARobj.IRFs | matrix IRFs = SVARobj.IRFs | |||

endif | endif | |||

matrix ret = zeros(h, n*n) | matrix ret = zeros(h, n*n) | |||

ctmp = cum(IRFs .* IRFs) | ctmp = cum(IRFs .* IRFs) | |||

loop i = 1..h --quiet | loop i = 1..h --quiet | |||

tmp = mshape(ctmp[i,],n,n)' | tmp = mshape(ctmp[i,],n,n)' | |||

ret[i,] = vec(tmp ./ sumc(tmp))' | ret[i,] = vec(tmp ./ sumc(tmp))' | |||

endloop | endloop | |||

return ret | return ret | |||

end function | end function | |||

### The function GetShock() isn't called anywhere, but it's public and | ### The functions GetShock() and SVAR_getshock aren't called anywhere, | |||

### meant to be used by the user. | #### but they're public and meant to be used by the user. | |||

## GetShock may be deprecated in the future in favor of SVAR_getshock. | ||||

function series SVAR_getshock(bundle *mod, string sname[null], | ||||

int drawix[0::0]) | ||||

# This is a wrapper to provide a nicer interface, using the | ||||

# name of the shock instead of the number. | ||||

# Default (as in GetShock) is to use the first shock. | ||||

s_ix = !exists(sname) ? 1 : strpos_allin(mod.snames, sname) | ||||

return GetShock(&mod, s_ix, drawix) | ||||

end function | ||||

function series GetShock(bundle *SVARobj, int i[1::1], int drawix[0::0]) | function series GetShock(bundle *SVARobj, int i[1::1], int drawix[0::0]) | |||

/* | /* | |||

Produces the series corresponding to the historical shock | Produces the series corresponding to the historical shock | |||

realizations associated with the point estimates of the model | realizations associated with the point estimates of the model | |||

(and IRFs). | (and IRFs). | |||

For set identification (sign restrictions) there is no point | For set identification (sign restrictions) there is no point | |||

estimate; however, we support that | estimate; however, we support that | |||

the user picks one of the accepted draws and then the shock series | the user picks one of the accepted draws and then the shock series | |||

is based on that particular model draw. | is based on that particular model draw. | |||

# (drawix only needed/meant for the set id case (type 10)) | # (drawix only meant for the set id case (type 10)) | |||

*/ | */ | |||

series ret = NA | series ret = NA | |||

type = SVARobj.type | type = SVARobj.type | |||

matrix B10 = {} # to be filled in type 10 | matrix B10 = {} # to be filled in type 10 | |||

## some error checks ## | ## some error checks ## | |||

if type > 4 && type != 10 | if type > 4 && type != 10 | |||

printf "Given type %d\n", type | printf "Given type %d\n", type | |||

funcerr "Unknown model type" | funcerr "Unknown model type" | |||

elif type == 10 | elif i > SVARobj.n | |||

errchkSRhisto(&SVARobj, drawix) | ||||

elif drawix > 0 | ||||

print "Warning: 'drawix' meaningless for standard SVAR, ignoring" | ||||

elif i > SVARobj.n | ||||

printf "Chosen shock index: %d\n", i | printf "Chosen shock index: %d\n", i | |||

funcerr "Shock index out of range" | funcerr "Shock index out of range" | |||

elif type != 10 && drawix > 0 | ||||

print "Warning: 'drawix' arg meaningless for standard SVAR" | ||||

elif type == 10 | ||||

errchkSRhisto(&SVARobj, drawix) | ||||

endif | endif | |||

## get the C matrix (and then the inv) ## | ## get the C matrix (and then the inv) ## | |||

if (type == 1) || (type == 2) || (type == 4) | if (type == 1) || (type == 2) || (type == 4) | |||

matrix C = SVARobj.C | matrix C = SVARobj.C | |||

elif type == 3 | elif type == 3 | |||

if inbundle(SVARobj, "C") # maybe not yet computed | if inbundle(SVARobj, "C") # maybe not yet computed | |||

matrix C = SVARobj.C | matrix C = SVARobj.C | |||

else | else | |||

matrix C = SVARobj.S1 \ SVARobj.S2 | matrix C = SVARobj.S1 \ SVARobj.S2 | |||

endif | endif | |||

elif type == 10 # set id | elif type == 10 # set id | |||

bundle pickdraw = SVARobj.acc_draws[drawix] | # allow drawix to override the setting in the bundle | |||

whichdraw = drawix ? drawix : SVARobj.bestdraw | ||||

bundle pickdraw = SVARobj.acc_draws[whichdraw] | ||||

matrix C = pickdraw.irfs[1] # impact effect is C | matrix C = pickdraw.irfs[1] # impact effect is C | |||

B10 = pickdraw.B | B10 = pickdraw.B | |||

if cols(C) < SVARobj.n | ||||

funcerr "partial id not supported for shock retrieval" | ||||

endif | ||||

endif | endif | |||

matrix iC = inv(C') | matrix iC = inv(C') | |||

matrix resids = muVARparE_mayberedr(SVARobj, B10)[3] | matrix resids = muVARparE_mayberedr(SVARobj, B10)[3] | |||

## construct the wanted series ## | ## construct the wanted series ## | |||

extra = $nobs - rows(resids) | extra = $nobs - rows(resids) | |||

matrix tmp = {} | matrix tmp = {} | |||

if extra > 0 | if extra > 0 | |||

skipping to change at line 176 | skipping to change at line 202 | |||

snames = SVARobj.snames # strings array? | snames = SVARobj.snames # strings array? | |||

string vlab = snames[i] | string vlab = snames[i] | |||

setinfo ret --description="@vlab" | setinfo ret --description="@vlab" | |||

return ret | return ret | |||

end function | end function | |||

####################### | ####################### | |||

function list SVAR_HD(bundle *mod, string vname[null], | ||||

int drawix[0::0]) | ||||

# wrapper around SVAR_hd to use a string interface for the | ||||

# variable | ||||

v_ix = !exists(vname) ? 1 : strpos_allin(mod.Ynames, vname) | ||||

return SVAR_hd(&mod, v_ix, drawix) | ||||

end function | ||||

function list SVAR_hd(bundle *Mod, int nv[1::1], int drawix[0::0]) | function list SVAR_hd(bundle *Mod, int nv[1::1], int drawix[0::0]) | |||

# historical decomposition | # historical decomposition | |||

# (drawix only needed/meant for the set id case (type 10)) | # (drawix only meant for the set id case (type 10)) | |||

list ret = null | list ret = null | |||

loop foreach i n p t1 t2 type T k --quiet | loop foreach i n p t1 t2 type T k --quiet | |||

scalar $i = Mod.$i | scalar $i = Mod.$i | |||

endloop | endloop | |||

matrix B10 = {} # to be filled for type 10 | matrix B10 = {} # to be filled for type 10 | |||

if type == 10 | ||||

errchkSRhisto(&Mod, drawix) | ||||

endif | ||||

if nv > n | if nv > n | |||

printf "Hm. There are %d variables in the model. ", n | printf "Hm. There are %d variables in the model. ", n | |||

printf "Chosen shock index: %d\n", nv | printf "Chosen shock index: %d\n", nv | |||

funcerr "Shock index out of range" | funcerr "Shock index out of range" | |||

## (further range check for SR partial id below) ## | ||||

endif | endif | |||

# Prepare the set id case | # Prepare the set id case | |||

if type == 10 | if type == 10 | |||

bundle pickdraw = Mod.acc_draws[drawix] | errchkSRhisto(&Mod, drawix) | |||

# allow drawix to override the setting in the bundle | ||||

whichdraw = drawix ? drawix : Mod.bestdraw | ||||

bundle pickdraw = Mod.acc_draws[whichdraw] | ||||

matrix B10 = pickdraw.B | matrix B10 = pickdraw.B | |||

endif | endif | |||

# The following might be redrawn in type10/Bayesian | # The following might be redrawn in type10/Bayesian | |||

matrices muVARparE = muVARparE_mayberedr(Mod, B10) | matrices muVARparE = muVARparE_mayberedr(Mod, B10) | |||

# compute the exogenous part | # compute the exogenous part | |||

if type < 4 | if type < 4 | |||

matrix m = Mod.X * Mod.mu | matrix m = Mod.X * Mod.mu | |||

skipping to change at line 237 | skipping to change at line 271 | |||

elif type == 3 | elif type == 3 | |||

if inbundle(Mod, "C") | if inbundle(Mod, "C") | |||

matrix C = Mod.C | matrix C = Mod.C | |||

else | else | |||

matrix C = Mod.S1 \ Mod.S2 | matrix C = Mod.S1 \ Mod.S2 | |||

endif | endif | |||

elif type == 10 | elif type == 10 | |||

matrix C = pickdraw.irfs[1] # impact effect is C | matrix C = pickdraw.irfs[1] # impact effect is C | |||

if cols(C) < Mod.n | ||||

funcerr "partial id not supported for historical decomp" | ||||

endif | ||||

endif | endif | |||

matrix iC = inv(C) | matrix iC = inv(C) | |||

strings Ynames = Mod.Ynames | strings Ynames = Mod.Ynames | |||

strings snames = Mod.snames | strings snames = Mod.snames | |||

string yn = Ynames[nv] | string yn = Ynames[nv] | |||

smpl t1 t2 | smpl t1 t2 | |||

if cols(m)>0 | if cols(m)>0 | |||

Xdet = varsimul(muVARparE[2], m[p+1:,], Mod.Y[1:p,]) # was VARpar | Xdet = varsimul(muVARparE[2], m[p+1:,], Mod.Y[1:p,]) # was VARpar | |||

End of changes. 17 change blocks. | ||||

28 lines changed or deleted | | 66 lines changed or added |