Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
micahjsmith committed Jan 25, 2016
0 parents commit 36b7021
Show file tree
Hide file tree
Showing 96 changed files with 12,051 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
*~
trash/

# Ignore files used in estimation
g1.mat
g2.mat
g3.mat
H.dat

# Ignore large output files
save/*
!mode_in

# Ignore directories for user-generate output
figures/*tex
figures/*pdf
graphs/*
48 changes: 48 additions & 0 deletions Main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
%% Main.m
%% This script produces all output associated with the FRBNY DSGE model.
%
% Copyright Federal Reserve Bank of New York. You may reproduce, use, modify,
% make derivative works of, and distribute and this code in whole or in part
% so long as you keep this notice in the documentation associated with any
% distributed works. Neither the name of the Federal Reserve Bank of New
% York (FRBNY) nor the names of any of the authors may be used to endorse or
% promote works derived from this code without prior written permission.
% Portions of the code attributed to third parties are subject to applicable
% third party licenses and rights. By your use of this code you accept this
% license and any applicable third party license.
%
% THIS CODE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT ANY WARRANTIES OR
% CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
% LIMITATION ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
% MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, EXCEPT TO THE EXTENT
% THAT THESE DISCLAIMERS ARE HELD TO BE LEGALLY INVALID. FRBNY IS NOT, UNDER
% ANY CIRCUMSTANCES, LIABLE TO YOU FOR DAMAGES OF ANY KIND ARISING OUT OF OR
% IN CONNECTION WITH USE OF OR INABILITY TO USE THE CODE, INCLUDING, BUT NOT
% LIMITED TO DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, PUNITIVE, SPECIAL OR
% EXEMPLARY DAMAGES, WHETHER BASED ON BREACH OF CONTRACT, BREACH OF WARRANTY,
% TORT OR OTHER LEGAL OR EQUITABLE THEORY, EVEN IF FRBNY HAS BEEN ADVISED OF
% THE POSSIBILITY OF SUCH DAMAGES OR LOSS AND REGARDLESS OF WHETHER SUCH
% DAMAGES OR LOSS IS FORESEEABLE.

%% Initialization
clear
close all
spec_990 % sets important variables and flags
set_paths % adds necessary paths

%% Estimation
% In this stage we draw from the distribution for the parameters. The modal
% parameters as well as the draws of parameters, are outputted in the /save
% folder.
gibb


%% Forecasting
% Here we produce forecasts for our observable variables, one associated
% with each draw of the parameters, and saves them in the /save folder.
forecast_parallel_est_ant

%% Plotting
forplot % produces series to be plotted
plotPresentation % produces plots of series outputted from forplot, which
% are saved in the /graphs folder
176 changes: 176 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# FRBNY DSGE Model (Version 990.2)

MATLAB code to estimate and forecast the model discussed in the Liberty Street Economics blog post "The FRBNY DSGE Model Forecast".

# Running the Code

## Running with Default Settings

All you need to do is run the file `Main.m`. This script will run the entire
set of code, calling

- `set_paths.m`: Set default directories for input and output; add code
subfolders to MATLAB path.
- `spec_990.m`: Set model specifications and important flags for estimation
and forecasting.
- `gibb_est_ant.m`: Find posterior mode and sample from posterior distribution.
- `forecast_parallel_est_ant.m`: Forecast observables; can run in parallel.
- `forplot.m`: Load forecasts into data structures to prepare for plotting.
- `plotPresentation.m`: Plot forecasts.

## Running with Modified Settings

If you would like to change defaults for estimation and forecasts, see
`spec_990.m`. There, you can modify

- **Estimation Parameters**
- `reoptimize`: Whether to re-optimize and find the mode or use saved mode.
- `CH`: Whether to re-compute the hession or use saved.
- `nsim`: The number of posterior draws per block.
- `nblocks`: The number of blocks.
- `nburn`: Size of the burn-in.
- `jstep`: From the blocks, the forecasting code will only use every
jstep-th element.
- **Forecast Parameters**
- `zerobound`: Whether to incorporate anticipated policy shocks.
- `peachflag`: Whether to condition time T+1 forecasts of observables on
user-provided forecasts of observables (treating the information supplied
by the user as data).
- `distr`: Flag to specify whether to parallelize the forecast procedure.
- `nMaxWorkers`: Number of workers to use in parallel forecast procedure.

# Directory Structure

In the main folder, there exist the following directories to house code
components:

- `data/`: Input data
- `dsgesolv/`: Solving the model; includes `gensys.m` code.
- `estimation/`: Mode-finding and posterior sampling.
- `figures/`: For output including parameter moment-tables and TeX tables.
- `forecast/`: Forecasting programs.
- `graphs/`: For graphs of forecasts.
- `initializaization/`: Loading data, defining model structure, setting up
important model flags.
- `kalman/`: Kalman filtering and smoothing.
- `plotting`: Loading forecast distributions and generating/saving plots.
- `save/`: Input mode and output data generated by the code (output mode,
posterior draws, forecasts).
- `toolbox/`: Supporting programs.


# Program Details

This section describes important programs in greater detail. If the user
is interested only in running the default model and reproducing the forecast
results, this section can be ignored.

This section focuses on what the code does and why, while the code itself
(including comments) provides detailed information regarding *how* these basic
procedures are implemented.

## Estimation

**Main Program**: `estimation/gibb_est_ant.m`

**Purpose**: Finds modal parameter estimates and samples from posterior
distribution.

**Main Steps**

- *Initialization*: Read in transform raw data from `data/`. Load files from
`initialization/` related to model specification, parameter priors, parameter
restrictions.
- *Find Mode*: The main program will call the `csminwel.m` optimization routine
to find modal parameter estimates. Can optionally start estimation from a
starting parameter vector by specifying `data/mode_in.`
- *Sample from Posterior*: Posterior sampling begins from the computed mode,
first computing the Hessian matrix to scale the proposal distribution in the
Metropolis Hastings algorithm. Settings for the number of sampling blocks and
the size of those blocks can be specified in `spec_990.m`.

*Remark*: In addition to saving each draw of the parameter vector, the
estimation program also saves the resulting posterior value and transition
equation matrices implied by each draw of the parameter vector. This is to save
time in the forecasting step since that code can avoid recomputing those
matrices. In addition, to save space, all files in `save/` are binary files.

## Forecasting

**Main Program**: `forecast/forecast_parallel_est_ant.m`

**Purpose**: Compute forecast distribution for the observables, sampling from
the full posterior distribution of parameters and sampling exogenous shocks.

**Main Steps**

- *Load Draws*: Load in posterior distribution blocks that are output from the
estimation stage.
- *Filter and Smooth*: Pass matrices defining the state transition equation
into `forecastFcn_est_ant.m`, which will filter and smooth the states over
the history.
- *Forecast*: Compute forecasts using `getForecast.m`, which takes matrices
corresponding to a posterior draw and uses them to iterate on the time T
state vector to obtain forecasts, adding in draws of exogenous shocks as
well.

*Remark*: The code can be run in parallel or sequentially. Running in parallel
requires the MATLAB parallel toolbox.

## Plotting ##

**Main Programs**: `plotting/forplot.m`, `plotting/plotPresentation.m`

**Purpose**: Generate plots of observables.

**Main Steps**

- *Load Plots*: The program `forplot.m` will load output from the forecast
program into the workspace, computing entries in the data structures `Means`
and `Bands`.
- *Plot*: The program `plotPresentation.m` will plot the forecasts and shock
decompositions, saving the graphs in the `graphs/` folder.

*Remark*: Each time `forplot.m` is called, it will attempt to recompute the
forecast means and bands, which takes some time. However, these are saved in
`save/` after the very first call to `forplot.m`. To use these saved Means and
Bands structures and avoid recomputing, set `useSavedMB=1` before running
`forplot.m`.

# Final Notes on MATLAB Versions and Toolboxes

In certain functions implemented in this program are Toolbox functions provided
by Mathworks. If you are receiving errors in running these programs due to
undefined functions, it is likely because you do not yet have access to these
Toolboxes. For example, to run the forecasts in parallel (the default setting),
you will need the parallel toolbox. Also, `dlyap.m`, a function to solve
discrete-time Lyapunov equations, comes from the Control System Toolbox.

These programs are meant to be run in Matlab09a. While we have not attempted to
run these programs using a more recent version of Matlab, it is possible that
some of the Matlab-defined functions are not identical and thus may yield
nonidentical results.

# DISCLAIMER

Copyright Federal Reserve Bank of New York. You may reproduce, use, modify,
make derivative works of, and distribute and this code in whole or in part so
long as you keep this notice in the documentation associated with any
distributed works. Neither the name of the Federal Reserve Bank of New York
(FRBNY) nor the names of any of the authors may be used to endorse or promote
works derived from this code without prior written permission. Portions of the
code attributed to third parties are subject to applicable third party licenses
and rights. By your use of this code you accept this license and any
applicable third party license.

THIS CODE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT ANY WARRANTIES OR CONDITIONS
OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS
FOR A PARTICULAR PURPOSE, EXCEPT TO THE EXTENT THAT THESE DISCLAIMERS ARE HELD
TO BE LEGALLY INVALID. FRBNY IS NOT, UNDER ANY CIRCUMSTANCES, LIABLE TO YOU
FOR DAMAGES OF ANY KIND ARISING OUT OF OR IN CONNECTION WITH USE OF OR
INABILITY TO USE THE CODE, INCLUDING, BUT NOT LIMITED TO DIRECT, INDIRECT,
INCIDENTAL, CONSEQUENTIAL, PUNITIVE, SPECIAL OR EXEMPLARY DAMAGES, WHETHER
BASED ON BREACH OF CONTRACT, BREACH OF WARRANTY, TORT OR OTHER LEGAL OR
EQUITABLE THEORY, EVEN IF FRBNY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES OR LOSS AND REGARDLESS OF WHETHER SUCH DAMAGES OR LOSS IS FORESEEABLE.
Binary file added README.pdf
Binary file not shown.
107 changes: 107 additions & 0 deletions data/ExpFFR_OIS.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

%% Loads in expected FFR derived from OIS quotes


function [ExpFFR,peachdata_FFR] = ExpFFR_OIS(nant,antlags,psize,zerobound,peachflag)

% 2008-Q4 expectations (from Jan-2009 BCFF survey, conducted mid-/end- of Dec-2008)
ExpFFR(1,:) = [0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.1 2.2 2.4];

% % 2009-Q1 expectations (from Apr-2009 BCFF survey, conducted mid-/end- of Mar-2009)
ExpFFR(2,:) = [0.2 0.3 0.5 0.6 0.8 1.1 1.3 1.5 1.7 2.0 2.2 2.4 2.5];

% 2009-Q2 expectations (from Jul-2009 BCFF survey, conducted mid-/end- of Jun-2009)
ExpFFR(3,:) = [0.2 0.4 0.7 1.1 1.4 1.8 2.2 2.6 2.9 3.2 3.5 3.7 4.0];

% 2009-Q3 expectations (from Oct-2009 BCFF survey, conducted mid-/end- of Sep-2009)
ExpFFR(4,:) = [0.2 0.4 0.7 1.0 1.4 1.8 2.1 2.4 2.7 2.9 3.1 3.3 3.6];

% 2009-Q4 expectations (from Jan-2010 BCFF survey, conducted mid-/end- of Dec-2009)
ExpFFR(5,:) = [0.2 0.4 0.8 1.2 1.6 2.0 2.4 2.7 3.0 3.3 3.5 3.7 4.0];

% 2010-Q1 expectations (from Apr-2010 BCFF survey, conducted mid-/end- of Mar-2010)
ExpFFR(6,:) = [0.2 0.4 0.6 0.9 1.2 1.6 1.9 2.3 2.6 2.9 3.1 3.4 3.6];

% 2010-Q2 expectations (from Jul-2010 BCFF survey, conducted mid-/end- of Jun-2010)
ExpFFR(7,:) = [0.2 0.3 0.4 0.5 0.6 0.8 1.0 1.2 1.4 1.7 1.9 2.2 2.2];

% 2010-Q3 expectations (from Oct-2010 BCFF survey, conducted mid-/end- of Sep-2010)
ExpFFR(8,:) = [0.2 0.2 0.2 0.3 0.4 0.5 0.6 0.8 0.9 1.1 1.3 1.5 1.6];

% 2010-Q4 expectations (from Jan-2011 BCFF survey, conducted mid-/end- of Dec-2010)
ExpFFR(9,:) = [0.2 0.2 0.3 0.4 0.6 0.8 1.0 1.3 1.6 1.8 2.1 2.4 2.6];

% 2011-Q1 expectations (from Apr-2011 BCFF survey, conducted mid-/end- of Mar-2011)
ExpFFR(10,:) = [0.1 0.2 0.4 0.7 0.9 1.2 1.5 1.8 2.1 2.4 2.7 2.9 3.2];

% 2011-Q2 expectations (from Jul-2011 BCFF survey, conducted mid-/end- of Jun-2011)
ExpFFR(11,:) = [0.1 0.2 0.3 0.4 0.5 0.7 0.9 1.2 1.4 1.7 2.0 2.2 2.4];

% 2011-Q3 expectations (from Oct-2011 BCFF survey, conducted mid-/end- of Sep-2011)
ExpFFR(12,:) = [0.1 0.1 0.1 0.1 0.2 0.2 0.3 0.4 0.5 0.6 0.8 0.9 1.0];

% 2011-Q4 expectations (from 12/30/2011 OIS data)
ExpFFR(13,:) = [0.1 0.1 0.1 0.1 0.2 0.2 0.3 0.4 0.5 0.6 0.7 0.9 1.0];

% 2012-Q1 expectations (from 3/30/2012 OIS data)
ExpFFR(14,:) = [0.1 0.1 0.2 0.2 0.3 0.3 0.4 0.5 0.6 0.7 0.9 1.0 1.2];
%
% % 2012-Q2 expectations (from 6/30/2012 OIS data)
ExpFFR(15,:) = [0.2 0.2 0.2 0.2 0.2 0.3 0.3 0.4 0.4 0.5 0.5 0.6 0.8];

% % 2012-Q3 expectations (from 9/30/2012 OIS data)
ExpFFR(16,:) = [0.1 0.1 0.1 0.1 0.1 0.1 0.2 0.2 0.3 0.4 0.5 0.6 0.7];
%
% % 2012-Q4 expectations (from 12/31/2012 OIS data)
ExpFFR(17,:) = [0.1 0.1 0.1 0.1 0.1 0.2 0.2 0.3 0.3 0.4 0.5 0.6 0.8];

% 2013-Q1 expectations (from 3/29/2013 OIS data)
ExpFFR(18,:) = [0.1 0.1 0.1 0.1 0.2 0.2 0.2 0.3 0.3 0.4 0.5 0.6 0.7];

% 2013-Q2 expectations (from 6/29/2013 OIS data)
ExpFFR(19,:) = [0.1 0.2 0.2 0.3 0.4 0.5 0.7 0.8 1.0 1.2 1.3 1.5 1.7];

% 2013-Q3 expectations (from 10/3/2013 OIS data)
ExpFFR(20,:) = [0.1 0.1 0.1 0.2 0.2 0.3 0.4 0.6 0.7 0.9 1.1 1.3 1.5];

% 2013-Q4 expectations (from 12/31/2013 OIS data)
ExpFFR(21,:) = [0.1 0.1 0.1 0.2 0.3 0.4 0.6 0.8 1.0 1.2 1.4 1.7 1.9];

% 2014-Q1 expectations (from 3/31/2014 OIS data)
ExpFFR(22,:) = [0.1 0.1 0.1 0.2 0.4 0.5 0.7 0.9 1.1 1.4 1.7 1.9 2.2];

% 2014-Q2 expectations (from 6/30/2014 OIS data)
ExpFFR(23,:) = [0.1 0.1 0.2 0.3 0.4 0.6 0.8 1.0 1.2 1.4 1.7 1.9 2.2];

% 2014-Q3 expectations (from 9/30/2014 OIS data)
ExpFFR(24,:) = [0.1 0.1 0.2 0.4 0.6 0.9 1.1 1.4 1.6 1.9 2.1 2.3 2.5];

% 2014-Q4 expectations (from 12/31/2014 OIS Data)
ExpFFR(25,:) = [0.1 0.2 0.4 0.5 0.7 0.9 1.2 1.4 1.6 1.8 1.9 2.1 2.3];

if nant <= size(ExpFFR,2)
ExpFFR = ExpFFR(:,1:nant);
else
ExpFFR = [ExpFFR,NaN(antlags+1,nant-size(ExpFFR,2))];
end

if peachflag
xpeachdata_FFR(1,:) = [0.16 0.28 0.44 0.61 0.79 0.98 1.17 1.34 1.51 1.67 NaN];

% This should start with the OIS value for the first quarter AFTER the
% one you are forecasting. So if you have data until Q2 and you are
% forecating Q3, the first element of the array should be for Q4. The
% conditional peachdata file will supply the Q3 value.

if nant <= size(xpeachdata_FFR,2)
for t = 1:psize
peachdata_FFR(t,:) = [xpeachdata_FFR(t,1:nant-t),NaN(1,t)];
end
else
peachdata_FFR = [xpeachdata_FFR,NaN(psize,nant-size(xpeachdata_FFR,2))];
end


else
peachdata_FFR = [];
end
Binary file added data/conddata.mat
Binary file not shown.
Binary file added data/rawData_990.mat
Binary file not shown.
Loading

0 comments on commit 36b7021

Please sign in to comment.