Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Dóra Kocsis
dynare
Commits
ded34f99
Commit
ded34f99
authored
Sep 01, 2010
by
Stéphane Adjemian (Scylla)
Browse files
Merge branch 'master' of kirikou.dynare.org:/srv/d_kirikou/git/dynare
parents
6870010e
18c94791
Changes
11
Expand all
Hide whitespace changes
Inline
Side-by-side
matlab/swz/c-code/Makefile
View file @
ded34f99
...
...
@@ -191,6 +191,7 @@ MATRIX_DIR = $(WORK_DIR)/utilities/DWCcode/matrix
ERROR_DIR
=
$(WORK_DIR)
/utilities/DWCcode/error
ARRAY_DIR
=
$(WORK_DIR)
/utilities/DWCcode/arrays
ASCII_DIR
=
$(WORK_DIR)
/utilities/DWCcode/ascii
HIST_DIR
=
$(WORK_DIR)
/utilities/DWCcode/histogram
STAT_DIR
=
$(WORK_DIR)
/utilities/DWCcode/stat
SPHERICAL_DIR
=
$(WORK_DIR)
/utilities/DWCcode/spherical
SORT_DIR
=
$(WORK_DIR)
/utilities/DWCcode/sort
...
...
@@ -199,9 +200,9 @@ VAR_DIR = $(WORK_DIR)/sbvar/var
#################################################################################
# DW FILES
INCLUDE_DIR
:=
-I
$(MATRIX_DIR)
-I
$(ERROR_DIR)
-I
$(ARRAY_DIR)
-I
$(ASCII_DIR)
-I
$(STAT_DIR)
-I
$(SPHERICAL_DIR)
-I
$(SORT_DIR)
-I
$(SWITCH_DIR)
-I
$(VAR_DIR)
$(INCLUDE_DIR)
VPATH
:=
$(VPATH)
$(MATRIX_DIR)
$(ERROR_DIR)
$(ARRAY_DIR)
$(ASCII_DIR)
$(STAT_DIR)
$(SPHERICAL_DIR)
$(SORT_DIR)
$(SWITCH_DIR)
$(VAR_DIR)
OBJS
:=
$(OBJS)
bmatrix.o swzmatrix.o dw_error.o dw_rand.o dw_matrix_rand.o dw_array.o dw_matrix_array.o dw_matrix_sort.o dw_ascii.o dw_parse_cmd.o
INCLUDE_DIR
:=
-I
$(MATRIX_DIR)
-I
$(ERROR_DIR)
-I
$(ARRAY_DIR)
-I
$(ASCII_DIR)
-I
$(HIST_DIR)
-I
$(STAT_DIR)
-I
$(SPHERICAL_DIR)
-I
$(SORT_DIR)
-I
$(SWITCH_DIR)
-I
$(VAR_DIR)
$(INCLUDE_DIR)
VPATH
:=
$(VPATH)
$(MATRIX_DIR)
$(ERROR_DIR)
$(ARRAY_DIR)
$(ASCII_DIR)
$(HIST_DIR)
$(STAT_DIR)
$(SPHERICAL_DIR)
$(SORT_DIR)
$(SWITCH_DIR)
$(VAR_DIR)
OBJS
:=
$(OBJS)
bmatrix.o swzmatrix.o dw_error.o dw_rand.o dw_matrix_rand.o dw_array.o dw_matrix_array.o dw_matrix_sort.o dw_ascii.o dw_parse_cmd.o
dw_histogram.o
# MEX
INCLUDE_DIR
:=
-I
$(WORK_DIR)
/mex
$(INCLUDE_DIR)
...
...
@@ -221,13 +222,13 @@ OBJS_INIT := $(OBJS) create_init_file.o switch.o switchio.o VARbase.o VARio.o VA
OBJS_MHM_1
:=
$(OBJS)
mhm_VAR_main_1.o mhm_VAR.o VARbase.o VARio.o command_line_VAR.o switch.o switchio.o
OBJS_MHM_2
:=
$(OBJS)
mhm_VAR_main_2.o spherical.o VARbase.o VARio.o switch.o switchio.o mhm_VAR.o
OBJS_PROBA
:=
$(OBJS)
probabilities.o switch.o switchio.o VARbase.o VARio.o command_line_VAR.o
OBJS_FORECAST
:=
$(OBJS)
forecast.o switch.o switchio.o VARbase.o VARio.o command_line_VAR.o
#################################################################################
# OUTPUT
all
:
$(OUT_DIR)/sbvar_draws $(OUT_DIR)/sbvar_estimation $(OUT_DIR)/sbvar_init_file $(OUT_DIR)/sbvar_mhm_1 $(OUT_DIR)/sbvar_mhm_2 $(OUT_DIR)/sbvar_probabilities
all
:
$(OUT_DIR)/sbvar_draws $(OUT_DIR)/sbvar_estimation $(OUT_DIR)/sbvar_init_file $(OUT_DIR)/sbvar_mhm_1 $(OUT_DIR)/sbvar_mhm_2 $(OUT_DIR)/sbvar_probabilities
$(OUT_DIR)/sbvar_forecast
$(OUT_DIR)/sbvar_draws
:
$(OBJS_DRAWS)
...
...
@@ -248,6 +249,8 @@ $(OUT_DIR)/sbvar_mhm_2: $(OBJS_MHM_2)
$(OUT_DIR)/sbvar_probabilities
:
$(OBJS_PROBA)
$(CC)
$(CFLAGS)
$^
$(LIBS_DIR)
$(LIBS)
-o
$(OUT_DIR)
/sbvar_probabilities
$(OUT_DIR)/sbvar_forecast
:
$(OBJS_FORECAST)
$(CC)
$(CFLAGS)
$^
$(LIBS_DIR)
$(LIBS)
-o
$(OUT_DIR)
/sbvar_forecast
%.o
:
%.c
...
...
matlab/swz/c-code/sbvar/switching/switchio.c
View file @
ded34f99
...
...
@@ -561,6 +561,9 @@ int ReadBaseTransitionMatrices_SV(FILE *f_in, TMarkovStateVariable* sv, char *he
/* // Convert base transition matrix to full transition matrix. ansi-c*/
ConvertBaseTransitionMatrix
(
sv
->
Q
,
Q
,
sv
->
nlags_encoded
);
/* Free Q */
FreeMatrix
(
Q
);
/* // Update ansi-c*/
if
(
!
Update_B_from_Q_SV
(
sv
))
{
...
...
@@ -570,6 +573,7 @@ int ReadBaseTransitionMatrices_SV(FILE *f_in, TMarkovStateVariable* sv, char *he
return
1
;
}
FreeMatrix
(
Q
);
return
0
;
}
}
...
...
@@ -712,6 +716,74 @@ void WriteBaseTransitionMatricesFlat_Headers_SV(FILE *f_out, TMarkovStateVariabl
}
}
/*
Returns 1 upon success and 0 upon failure.
*/
int
ReadBaseTransitionMatricesFlat_SV
(
FILE
*
f_in
,
TMarkovStateVariable
*
sv
)
{
int
i
,
j
;
TMatrix
Q
;
PRECISION
sum
;
if
(
sv
->
n_state_variables
>
1
)
{
for
(
i
=
0
;
i
<
sv
->
n_state_variables
;
i
++
)
if
(
!
ReadBaseTransitionMatricesFlat_SV
(
f_in
,
sv
->
state_variable
[
i
]))
return
0
;
}
else
{
/* // Read transition matrix ansi-c*/
Q
=
CreateMatrix
(
sv
->
nbasestates
,
sv
->
nbasestates
);
for
(
j
=
0
;
j
<
ColM
(
Q
);
j
++
)
for
(
i
=
0
;
i
<
RowM
(
Q
);
i
++
)
if
(
fscanf
(
f_in
,
" %lf "
,
&
ElementM
(
Q
,
i
,
j
))
!=
1
)
{
FreeMatrix
(
Q
);
return
0
;
}
/* // Scale the columns of Q - loose requirement on sumation to one ansi-c*/
for
(
j
=
sv
->
nbasestates
-
1
;
j
>=
0
;
j
--
)
{
for
(
sum
=
0
.
0
,
i
=
sv
->
nbasestates
-
1
;
i
>=
0
;
i
--
)
if
(
ElementM
(
Q
,
i
,
j
)
<
0
.
0
)
{
FreeMatrix
(
Q
);
dw_UserError
(
"Transition matrix can not have negative elements."
);
return
0
;
}
else
sum
+=
ElementM
(
Q
,
i
,
j
);
if
(
fabs
(
sum
-
1
.
0
)
>
1.0e-4
)
{
FreeMatrix
(
Q
);
dw_UserError
(
"Transition matrix columns must sum to one."
);
return
0
;
}
for
(
sum
=
1
.
0
/
sum
,
i
=
sv
->
nbasestates
-
1
;
i
>=
0
;
i
--
)
ElementM
(
Q
,
i
,
j
)
*=
sum
;
}
/* // Convert base transition matrix to full transition matrix. ansi-c*/
ConvertBaseTransitionMatrix
(
sv
->
Q
,
Q
,
sv
->
nlags_encoded
);
/* // Free Q ansi-c*/
FreeMatrix
(
Q
);
/* // Update ansi-c*/
if
(
!
Update_B_from_Q_SV
(
sv
))
{
dw_UserError
(
"Transition matrices do not satisfy restrictions"
);
return
0
;
}
return
1
;
}
return
1
;
}
/*
Returns 1 upon success and 0 upon failure.
...
...
@@ -894,6 +966,11 @@ int WriteBaseTransitionMatrices(FILE *f, char *filename, char *header, TStateMod
return
rtrn
;
}
int
ReadBaseTransitionMatricesFlat
(
FILE
*
f
,
TStateModel
*
model
)
{
return
f
?
ReadBaseTransitionMatricesFlat_SV
(
f
,
model
->
sv
)
:
0
;
}
int
WriteBaseTransitionMatricesFlat
(
FILE
*
f
,
TStateModel
*
model
,
char
*
fmt
)
{
return
f
?
WriteBaseTransitionMatricesFlat_SV
(
f
,
model
->
sv
,
fmt
)
:
0
;
...
...
matlab/swz/c-code/sbvar/switching/switchio.h
View file @
ded34f99
...
...
@@ -12,6 +12,7 @@ int WriteTransitionMatrices_SV(FILE *f_out, TMarkovStateVariable* sv, char *head
int
ReadBaseTransitionMatrices_SV
(
FILE
*
f_out
,
TMarkovStateVariable
*
sv
,
char
*
header
,
char
*
idstring
);
int
WriteBaseTransitionMatrices_SV
(
FILE
*
f_out
,
TMarkovStateVariable
*
sv
,
char
*
header
,
char
*
idstring
);
int
ReadBaseTransitionMatricesFlat_SV
(
FILE
*
f_out
,
TMarkovStateVariable
*
sv
);
int
WriteBaseTransitionMatricesFlat_SV
(
FILE
*
f_out
,
TMarkovStateVariable
*
sv
,
char
*
fmt
);
void
WriteBaseTransitionMatricesFlat_Headers_SV
(
FILE
*
f_out
,
TMarkovStateVariable
*
sv
,
char
*
idstring
);
...
...
@@ -28,6 +29,7 @@ int WriteStates(FILE *f, char *filename, char *header, TStateModel *model);
int
ReadBaseTransitionMatrices
(
FILE
*
f
,
char
*
filename
,
char
*
header
,
TStateModel
*
model
);
int
WriteBaseTransitionMatrices
(
FILE
*
f
,
char
*
filename
,
char
*
header
,
TStateModel
*
model
);
int
ReadBaseTransitionMatricesFlat
(
FILE
*
f
,
TStateModel
*
model
);
int
WriteBaseTransitionMatricesFlat
(
FILE
*
f
,
TStateModel
*
model
,
char
*
fmt
);
/*
...
...
matlab/swz/c-code/sbvar/var/VARbase.c
View file @
ded34f99
...
...
@@ -2389,6 +2389,203 @@ void Update_lambda_psi_from_bplus(T_VAR_Parameters *p)
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
/********************************* Forecasts **********************************/
/******************************************************************************/
/*
Assumes:
forecast : horizon x nvars matrix or null pointer
horizon : positive integer - forecast horizon
initial : initial value of predetermined variables.
shocks : array of length horizon of shocks or null pointer. If null
pointer, then the shocks are all zero. Each vector is of length
nvar.
S : array of length horizon. S[t] is the state at time T+1+t.
model : pointer to valid TStateModel structure.
Results:
Computes forecast
Returns:
The matrix forecast upon success and null upon failure. If forecast is
null, then it created.
*/
TMatrix
forecast_base
(
TMatrix
forecast
,
int
horizon
,
TVector
initial
,
TVector
*
shocks
,
int
*
S
,
TStateModel
*
model
)
{
T_VAR_Parameters
*
p
=
(
T_VAR_Parameters
*
)(
model
->
theta
);
TMatrix
*
A0
,
*
Aplus
;
TVector
x
,
y
;
int
i
,
t
;
/* // allocate forecast if necessary ansi-c*/
if
(
!
forecast
&&
!
(
forecast
=
CreateMatrix
(
horizon
,
p
->
nvars
)))
return
(
TMatrix
)
NULL
;
/* // allocate memory ansi-c*/
y
=
CreateVector
(
p
->
nvars
);
x
=
EquateVector
((
TVector
)
NULL
,
initial
);
A0
=
MakeA0_All
((
TMatrix
*
)
NULL
,
p
);
Aplus
=
MakeAplus_All
((
TMatrix
*
)
NULL
,
p
);
/* // forecast ansi-c*/
for
(
t
=
0
;
t
<
horizon
;
t
++
)
{
ProductVM
(
y
,
x
,
Aplus
[
S
[
t
]]);
if
(
shocks
)
{
for
(
i
=
p
->
nvars
-
1
;
i
>=
0
;
i
--
)
ElementV
(
y
,
i
)
+=
ElementV
(
shocks
[
t
],
i
)
/
sqrt
(
p
->
Zeta
[
i
][
p
->
var_states
[
i
][
S
[
t
]]]);
}
ProductInverseVM
(
y
,
y
,
A0
[
S
[
t
]]);
for
(
i
=
p
->
nvars
-
1
;
i
>=
0
;
i
--
)
ElementM
(
forecast
,
t
,
i
)
=
ElementV
(
y
,
i
);
memmove
(
pElementV
(
x
)
+
p
->
nvars
,
pElementV
(
x
),(
p
->
nlags
-
1
)
*
p
->
nvars
*
sizeof
(
PRECISION
));
memcpy
(
pElementV
(
x
),
pElementV
(
y
),
p
->
nvars
*
sizeof
(
PRECISION
));
}
/* // free memory ansi-c*/
dw_FreeArray
(
Aplus
);
dw_FreeArray
(
A0
);
FreeVector
(
x
);
FreeVector
(
y
);
return
forecast
;
}
/*
For 1 <= k < h, y[k][i] is null if the ith coordinate of y(t0+1+k) is
unrestricted and is its value otherwise. In general, t0 is the last index for
which we have full information. It must be the case that t0 <= nobs.
*/
/* TVector* dw_state_space_mean_conditional_forecast(TVector *F, PRECISION ***y, int h, int t0, TStateModel *model) */
/* { */
/* T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); */
/* TVector Pxi_i, *Pxi, *Pxi1, *SPxi, *Ez_i, **IEz, **Ez1, **Ez, **SEz, **ISEz, SPzeta, SPs, u, *z; */
/* TMatrix Q, *Ezz_i, **IEzz, **Ezz1, **Ezz; */
/* int i, k, s; */
/* if ((t0 > statespace->t0) && !Filter(t0,model)) return (TVector*)NULL; */
/* if ((t0 > model->t0) && !ForwardRecursion(t0,model)) return (TVector*)NULL; */
/* if (((t0 < model->sv->t0) || (model->sv->t1 < t0)) && !sv_ComputeTransitionMatrix(t0,model->sv,model)) return (TVector*)NULL; */
/* Pxi=dw_CreateArray_vector(h); */
/* Pxi1=dw_CreateArray_vector(h); */
/* SPxi=dw_CreateArray_vector(h); */
/* IEz=dw_CreateRectangularArray_vector(h,statespace->zeta_modulus); */
/* IEzz=dw_CreateRectangularArray_matrix(h,statespace->zeta_modulus); */
/* Ez1=dw_CreateRectangularArray_vector(h,statespace->nstates); */
/* Ezz1=dw_CreateRectangularArray_matrix(h,statespace->nstates); */
/* Ez=dw_CreateRectangularArray_vector(h,statespace->nstates); */
/* Ezz=dw_CreateRectangularArray_matrix(h,statespace->nstates); */
/* SEz=dw_CreateRectangularArray_vector(h,statespace->nstates); */
/* ISEz=dw_CreateRectangularArray_vector(h,statespace->zeta_modulus); */
/* for (k=h-1; k >= 0; k--) */
/* { */
/* Pxi[k]=CreateVector(statespace->nstates); */
/* Pxi1[k]=CreateVector(statespace->nstates); */
/* SPxi[k]=CreateVector(statespace->nstates); */
/* for (i=statespace->zeta_modulus-1; i >= 0; i--) */
/* { */
/* IEz[k][i]=CreateVector(statespace->nz); */
/* IEzz[k][i]=CreateMatrix(statespace->nz,statespace->nz); */
/* ISEz[k][i]=CreateVector(statespace->nz); */
/* } */
/* for (i=statespace->nstates-1; i >= 0; i--) */
/* { */
/* Ez1[k][i]=CreateVector(statespace->nz); */
/* Ezz1[k][i]=CreateMatrix(statespace->nz,statespace->nz); */
/* Ez[k][i]=CreateVector(statespace->nz); */
/* Ezz[k][i]=CreateMatrix(statespace->nz,statespace->nz); */
/* SEz[k][i]=CreateVector(statespace->nz); */
/* } */
/* } */
/* ConditionalFilter(0,h-1,y,statespace->Ez[t0],statespace->Ezz[t0],model->V[t0],model->sv->Q, */
/* Pxi,Pxi1,IEz,IEzz,Ez1,Ezz1,Ez,Ezz,statespace); */
/* SmoothProbabilities_MSStateSpace(0,h-1,SPxi,Pxi,Pxi1,model->sv->Q); */
/* SmoothMean_MSStateSpace(0,h-1,SEz,ISEz,Ez1,Ezz1,IEz,IEzz,SPxi,statespace); */
/* SPzeta=CreateVector(statespace->zeta_modulus); */
/* SPs=CreateVector(statespace->nbasestates); */
/* u=CreateVector(statespace->ny); */
/* z=dw_CreateArray_vector(statespace->nbasestates); */
/* for (s=statespace->nbasestates-1; s >= 0; s--) z[s]=CreateVector(statespace->nz); */
/* if (!F) */
/* { */
/* F=dw_CreateArray_vector(h); */
/* for (k=h-1; k >= 0; k--) */
/* F[k]=CreateVector(statespace->ny); */
/* } */
/* for (k=h-1; k >= 0; k--) */
/* { */
/* InitializeVector(F[k],0.0); */
/* if (statespace->zeta_modulus > statespace->nbasestates) */
/* { */
/* IntegrateStatesSingle(SPzeta,SPxi[k],statespace->zeta_modulus,statespace->nbasestates,2); */
/* IntegrateStatesSingleV(z,SPzeta,ISEz[k],statespace->nbasestates,statespace->zeta_modulus/statespace->nbasestates,2); */
/* IntegrateStatesSingle(SPs,SPzeta,statespace->nbasestates,statespace->zeta_modulus/statespace->nbasestates,2); */
/* for (s=statespace->nbasestates-1; s >= 0; s--) */
/* { */
/* ProductMV(u,statespace->H[s],z[s]); */
/* AddVV(u,statespace->a[s],u); */
/* LinearCombinationV(F[k],1.0,F[k],ElementV(SPs,s),u); */
/* } */
/* } */
/* else */
/* { */
/* IntegrateStatesSingle(SPs,SPxi[k],statespace->nbasestates,statespace->zeta_modulus,2); */
/* for (s=statespace->nbasestates-1; s >= 0; s--) */
/* { */
/* ProductMV(u,statespace->H[s],ISEz[k][s]); */
/* AddVV(u,statespace->a[s],u); */
/* LinearCombinationV(F[k],1.0,F[k],ElementV(SPs,s),u); */
/* } */
/* } */
/* } */
/* // Clean up */
/* dw_FreeArray(z); */
/* FreeVector(u); */
/* FreeVector(SPs); */
/* FreeVector(SPzeta); */
/* dw_FreeArray(ISEz); */
/* dw_FreeArray(SEz); */
/* dw_FreeArray(Ezz); */
/* dw_FreeArray(Ez); */
/* dw_FreeArray(Ezz1); */
/* dw_FreeArray(Ez1); */
/* dw_FreeArray(IEzz); */
/* dw_FreeArray(IEz); */
/* dw_FreeArray(SPxi); */
/* dw_FreeArray(Pxi1); */
/* dw_FreeArray(Pxi); */
/* return F; */
/* } */
/*
*/
/* TVector* dw_state_space_mean_unconditional_forecast(TVector *F, int h, int t0, TStateModel *model) */
/* { */
/* T_MSStateSpace *statespace=(T_MSStateSpace*)(model->theta); */
/* PRECISION ***y=(PRECISION***)dw_CreateMultidimensionalArrayList_scalar(3,h,statespace->ny,1); */
/* int i, j; */
/* for (i=h-1; i >= 0; i--) */
/* for (j=statespace->ny-1; j >= 0; j--) */
/* { dw_FreeArray(y[i][j]); y[i][j]=(PRECISION*)NULL; } */
/* F=dw_state_space_mean_conditional_forecast(F,y,h,t0,model); */
/* dw_FreeArray(y); */
/* return F; */
/* } */
/******************************************************************************/
/** Impulse Response Routines **/
/******************************************************************************/
...
...
@@ -3381,6 +3578,36 @@ TMatrix MakeA0(TMatrix A0, int s, T_VAR_Parameters *p)
return
A0
;
}
/*
Assumes:
A0 : Matrix array of length n_states or null pointer. A0[s] is either
p->nvars x p->nvars matrix or null pointer
*/
TMatrix
*
MakeA0_All
(
TMatrix
*
A0
,
T_VAR_Parameters
*
p
)
{
int
s
;
TMatrix
*
A0_in
=
A0
;
if
(
!
A0
)
{
if
(
!
(
A0
=
dw_CreateArray_matrix
(
p
->
nstates
)))
return
(
TMatrix
*
)
NULL
;
}
else
if
((
dw_DimA
(
A0
)
!=
p
->
nstates
))
{
dw_Error
(
SIZE_ERR
);
return
(
TMatrix
*
)
NULL
;
}
for
(
s
=
p
->
nstates
-
1
;
s
>=
0
;
s
--
)
if
(
!
(
A0
[
s
]
=
MakeA0
(
A0
[
s
],
s
,
p
)))
{
if
(
A0_in
!=
A0
)
dw_FreeArray
(
A0
);
return
(
TMatrix
*
)
NULL
;
}
return
A0
;
}
/*
Assumes:
Aplus : p->npre x p->nvars matrix or null pointer
...
...
@@ -3405,6 +3632,36 @@ TMatrix MakeAplus(TMatrix Aplus, int k, T_VAR_Parameters *p)
return
Aplus
;
}
/*
Assumes:
Aplus : Matrix array of length n_states or null pointer. Aplus[s] is either
p->npre x p->nvars matrix or null pointer
*/
TMatrix
*
MakeAplus_All
(
TMatrix
*
Aplus
,
T_VAR_Parameters
*
p
)
{
int
s
;
TMatrix
*
Aplus_in
=
Aplus
;
if
(
!
Aplus
)
{
if
(
!
(
Aplus
=
dw_CreateArray_matrix
(
p
->
nstates
)))
return
(
TMatrix
*
)
NULL
;
}
else
if
((
dw_DimA
(
Aplus
)
!=
p
->
nstates
))
{
dw_Error
(
SIZE_ERR
);
return
(
TMatrix
*
)
NULL
;
}
for
(
s
=
p
->
nstates
-
1
;
s
>=
0
;
s
--
)
if
(
!
(
Aplus
[
s
]
=
MakeAplus
(
Aplus
[
s
],
s
,
p
)))
{
if
(
Aplus_in
!=
Aplus
)
dw_FreeArray
(
Aplus
);
return
(
TMatrix
*
)
NULL
;
}
return
Aplus
;
}
TMatrix
MakeZeta
(
TMatrix
Zeta
,
int
k
,
T_VAR_Parameters
*
p
)
{
int
j
;
...
...
@@ -3425,6 +3682,35 @@ TMatrix MakeZeta(TMatrix Zeta, int k, T_VAR_Parameters *p)
return
Zeta
;
}
/*
Assumes:
Zeta : Matrix array of length n_states or null pointer. Zeta[s] is either
p->vars x p->nvars matrix or null pointer
*/
TMatrix
*
MakeZeta_All
(
TMatrix
*
Zeta
,
T_VAR_Parameters
*
p
)
{
int
s
;
TMatrix
*
Zeta_in
=
Zeta
;
if
(
!
Zeta
)
{
if
(
!
(
Zeta
=
dw_CreateArray_matrix
(
p
->
nstates
)))
return
(
TMatrix
*
)
NULL
;
}
else
if
((
dw_DimA
(
Zeta
)
!=
p
->
nstates
))
{
dw_Error
(
SIZE_ERR
);
return
(
TMatrix
*
)
NULL
;
}
for
(
s
=
p
->
nstates
-
1
;
s
>=
0
;
s
--
)
if
(
!
(
Zeta
[
s
]
=
MakeZeta
(
Zeta
[
s
],
s
,
p
)))
{
if
(
Zeta_in
!=
Zeta
)
dw_FreeArray
(
Zeta
);
return
(
TMatrix
*
)
NULL
;
}
return
Zeta
;
}
/*
Assumes
X: n x n matrix or null pointer in column major format
...
...
matlab/swz/c-code/sbvar/var/VARbase.h
View file @
ded34f99
...
...
@@ -198,14 +198,23 @@ void DrawAplus(TStateModel *model);
void
Draw_psi
(
TStateModel
*
model
);
void
Draw_lambda
(
TStateModel
*
model
);
/* Forecasts */
TMatrix
forecast_base
(
TMatrix
forecast
,
int
horizon
,
TVector
initial
,
TVector
*
shocks
,
int
*
S
,
TStateModel
*
model
);
/* //TVector* mean_conditional_forecast(TVector *F, PRECISION ***y, int h, int t0, TStateModel *model); ansi-c*/
/* //TVector* mean_unconditional_forecast(TVector *F, int h, int t0, TStateModel *model); ansi-c*/
/* Utilities */
void
ComputeDotProducts_All
(
T_VAR_Parameters
*
p
);
void
ComputeLogAbsDetA0_All
(
T_VAR_Parameters
*
p
);
void
ComputeLogAbsDetA0
(
int
j
,
int
k
,
T_VAR_Parameters
*
p
);
TMatrix
MakeA0
(
TMatrix
A0
,
int
k
,
T_VAR_Parameters
*
p
);
TMatrix
*
MakeA0_All
(
TMatrix
*
A0
,
T_VAR_Parameters
*
p
);
TMatrix
MakeAplus
(
TMatrix
Aplus
,
int
k
,
T_VAR_Parameters
*
p
);
TMatrix
*
MakeAplus_All
(
TMatrix
*
Aplus
,
T_VAR_Parameters
*
p
);
TMatrix
MakeZeta
(
TMatrix
Zeta
,
int
k
,
T_VAR_Parameters
*
p
);
TMatrix
*
MakeZeta_All
(
TMatrix
*
Zeta
,
T_VAR_Parameters
*
p
);
TMatrix
ConstructMatrixFromColumns
(
TMatrix
X
,
TVector
**
,
int
k
);
void
UpdateStateDependentFields
(
T_VAR_Parameters
*
p
,
int
*
S
);
...
...
matlab/swz/c-code/sbvar/var/VARio.c
View file @
ded34f99
...
...
@@ -563,6 +563,74 @@ int Write_VAR_ParametersFlat_Headers(FILE *f_out, TStateModel *model)
return
1
;
}
int
Read_VAR_ParametersFlat
(
FILE
*
f_in
,
TStateModel
*
model
)
{
TMatrix
*
A0
,
*
Aplus
;
TVector
*
Zeta
;
int
i
,
j
,
s
,
rtrn
=
0
;
T_VAR_Parameters
*
p
=
(
T_VAR_Parameters
*
)(
model
->
theta
);
/* // Allocate memory ansi-c*/
A0
=
dw_CreateArray_matrix
(
p
->
nstates
);
Aplus
=
dw_CreateArray_matrix
(
p
->
nstates
);
Zeta
=
dw_CreateArray_vector
(
p
->
nstates
);
/* // Read File ansi-c*/
for
(
s
=
0
;
s
<
p
->
nstates
;
s
++
)
{
A0
[
s
]
=
CreateMatrix
(
p
->
nvars
,
p
->
nvars
);
for
(
j
=
0
;
j
<
p
->
nvars
;
j
++
)
for
(
i
=
0
;
i
<
p
->
nvars
;
i
++
)
if
(
fscanf
(
f_in
,
" %lf "
,
&
ElementM
(
A0
[
s
],
i
,
j
))
!=
1
)
goto
ERROR
;
Aplus
[
s
]
=
CreateMatrix
(
p
->
npre
,
p
->
nvars
);
for
(
j
=
0
;
j
<
p
->
nvars
;
j
++
)
for
(
i
=
0
;
i
<
p
->
npre
;
i
++
)
if
(
fscanf
(
f_in
,
" %lf "
,
&
ElementM
(
Aplus
[
s
],
i
,
j
))
!=
1
)
goto
ERROR
;
Zeta
[
s
]
=
CreateVector
(
p
->
nvars
);
for
(
j
=
0
;
j
<
p
->
nvars
;
j
++
)
if
(
fscanf
(
f_in
,
" %lf "
,
&
ElementV
(
Zeta
[
s
],
j
))
!=
1
)
goto
ERROR
;
else
if
(
ElementV
(
Zeta
[
s
],
j
)
<
0
.
0
)
goto
ERROR
;
}
/* // Set A0, Aplus, and Zeta ansi-c*/
for
(
j
=
0
;
j
<
p
->
nvars
;
j
++
)
for
(
s
=
0
;
s
<
p
->
nstates
;
s
++
)
{
for
(
i
=
0
;
i
<
p
->
nvars
;
i
++
)
ElementV
(
p
->
A0
[
j
][
p
->
coef_states
[
j
][
s
]],
i
)
=
ElementM
(
A0
[
s
],
i
,
j
);
for
(
i
=
0
;
i
<
p
->
npre
;
i
++
)
ElementV
(
p
->
Aplus
[
j
][
p
->
coef_states
[
j
][
s
]],
i
)
=
ElementM
(
Aplus
[
s
],
i
,
j
);
p
->
Zeta
[
j
][
p
->
var_states
[
j
][
s
]]
=
ElementV
(
Zeta
[
s
],
j
);
}
/* // Update b0, bplus, lambda, psi ansi-c*/
Update_b0_bplus_from_A0_Aplus
(
p
);
if
((
p
->
Specification
&
SPEC_SIMS_ZHA
)
==
SPEC_SIMS_ZHA
)
Update_lambda_psi_from_bplus
(
p
);
/* // Flags and notification that the VAR parameters have changed ansi-c*/
p
->
valid_parameters
=
1
;
ThetaChanged
(
model
);
rtrn
=
1
;
ERROR:
/* // Free memory ansi-c*/
dw_FreeArray
(
A0
);
dw_FreeArray
(
Aplus
);
dw_FreeArray
(
Zeta
);
return
rtrn
;
}
/*
For each state the VAR parameters are printed as follows
A0 (by columns)
...
...
matlab/swz/c-code/sbvar/var/VARio.h
View file @
ded34f99
...
...
@@ -10,6 +10,7 @@ TStateModel* Read_VAR_Specification(FILE *f, char *filename);
int
Write_VAR_Parameters
(
FILE
*
f
,
char
*
filename
,
char
*
id
,
TStateModel
*
model
);
int
Read_VAR_Parameters
(
FILE
*
f
,
char
*
filename
,
char
*
id
,
TStateModel
*
model
);
int
Read_VAR_ParametersFlat
(
FILE
*
f_in
,
TStateModel
*
model
);
int
Write_VAR_ParametersFlat
(
FILE
*
f
,
TStateModel
*
model
,
char
*
fmt
);
int
Write_VAR_ParametersFlat_Headers
(
FILE
*
f_out
,
TStateModel
*
model
);
int
Write_VAR_ParametersFlat_A0_Diagonal_One
(
FILE
*
f
,
TStateModel
*
model
,
char
*
fmt
);
...
...
matlab/swz/c-code/sbvar/var/forecast.c
0 → 100644
View file @
ded34f99
#include
"switch.h"
#include
"switchio.h"
#include
"VARio.h"
#include
"dw_parse_cmd.h"
#include
"dw_ascii.h"
#include
"dw_histogram.h"
#include
<stdlib.h>
#include
<string.h>
#include
"modify_for_mex.h"