Home > matlab > ms-sbvar > ms_write_markov_file.m

ms_write_markov_file

PURPOSE ^

function ms_write_markov_file(fname, options)

SYNOPSIS ^

function ms_write_markov_file(fname, options)

DESCRIPTION ^

 function ms_write_markov_file(fname, options)

 INPUTS
    fname:       (string)    name of markov file
    options:     (struct)    options

 OUTPUTS

 SPECIAL REQUIREMENTS
    none

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function ms_write_markov_file(fname, options)
0002 % function ms_write_markov_file(fname, options)
0003 %
0004 % INPUTS
0005 %    fname:       (string)    name of markov file
0006 %    options:     (struct)    options
0007 %
0008 % OUTPUTS
0009 %
0010 % SPECIAL REQUIREMENTS
0011 %    none
0012 
0013 % Copyright (C) 2011 Dynare Team
0014 %
0015 % This file is part of Dynare.
0016 %
0017 % Dynare is free software: you can redistribute it and/or modify
0018 % it under the terms of the GNU General Public License as published by
0019 % the Free Software Foundation, either version 3 of the License, or
0020 % (at your option) any later version.
0021 %
0022 % Dynare is distributed in the hope that it will be useful,
0023 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0024 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0025 % GNU General Public License for more details.
0026 %
0027 % You should have received a copy of the GNU General Public License
0028 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0029 
0030     n_chains = length(options.ms.ms_chain);
0031     nvars = size(options.varobs,1);
0032     
0033     fh = fopen(fname,'w');
0034     %/******************************************************************************/
0035     %/********************* Markov State Variable Information **********************/
0036     %/******************************************************************************/
0037     
0038     fprintf(fh,'//== Markov State Variables with Simple Restrictions ==//\n\n');
0039     
0040     
0041     %//This number is NOT used but read in.
0042     fprintf(fh,'//== Number Observations ==//\n');
0043     fprintf(fh,'0\n\n');
0044 
0045     fprintf(fh,'//== Number independent state variables ==//\n');
0046     fprintf(fh,'%d\n\n',n_chains);
0047 
0048     for i_chain = 1:n_chains
0049 
0050         %//=====================================================//
0051         %//== state_variable[i] (1 <= i <= n_state_variables) ==//
0052         %//=====================================================//
0053         fprintf(fh,'//== Number of states for state_variable[%d] ==//\n', ...
0054                 i_chain);
0055         n_states = length(options.ms.ms_chain(i_chain).regime);
0056         fprintf(fh,'%d\n\n',n_states);
0057 
0058         %//== 03/15/06: DW TVBVAR code reads the data below and overwrite the prior data read somewhere else if any.
0059         %//== Each column contains the parameters for a Dirichlet prior on the corresponding
0060         %//== column of the transition matrix.  Each element must be positive.  For each column,
0061         %//== the relative size of the prior elements determine the relative size of the elements
0062         %//== of the transition matrix and overall larger sizes implies a tighter prior.
0063         fprintf(fh,['//== Transition matrix prior for state_variable[%d] ==//\n'], ...
0064           i_chain);
0065         Alpha = ones(n_states,n_states);
0066         for i_state = 1:n_states
0067             p = 1-1/options.ms.ms_chain(i_chain).regime(i_state).duration;
0068             Alpha(i_state,i_state) = p*(n_states-1)/(1-p);
0069             fprintf(fh,'%22.16f',Alpha(i_state,:));
0070             fprintf(fh,'\n');
0071         end
0072  
0073         fprintf(fh,['\n//== Dirichlet dimensions for state_variable[%d] ' ...
0074                     '==//\n'],i_chain);
0075         %        fprintf(fh,'%d ',repmat(n_states,1,n_states));
0076         fprintf(fh,'%d ',repmat(2,1,n_states));
0077         fprintf(fh,'\n\n');
0078 
0079         %//== The jth restriction matrix is n_states-by-free[j].  Each row of the restriction
0080         %//== matrix has exactly one non-zero entry and the sum of each column must be one.
0081         fprintf(fh,['//== Column restrictions for state_variable[%d] ' ...
0082                     '==//\n'],i_chain);
0083         for i_state = 1:n_states
0084             if i_state == 1
0085                 M = eye(n_states,2);
0086             elseif i_state == n_states
0087                 M = [zeros(n_states-2,2); eye(2)];
0088             else
0089                 M = zeros(n_states,2);
0090                 M(i_state+[-1 1],1) = ones(2,1)/2;
0091                 M(i_state,2) = 1;
0092                 disp(M)
0093             end
0094             for j_state = 1:n_states
0095                 fprintf(fh,'%f ',M(j_state,:));
0096                 fprintf(fh,'\n');
0097             end
0098             fprintf(fh,'\n');
0099         end
0100     end
0101 
0102     %/******************************************************************************/
0103     %/******************************* VAR Parameters *******************************/
0104     %/******************************************************************************/
0105     %//NOT read
0106     fprintf(fh,'//== Number Variables ==//\n');
0107     fprintf(fh,'%d\n\n',nvars);
0108 
0109     %//NOT read
0110     fprintf(fh,'//== Number Lags ==//\n');
0111     fprintf(fh,'%d\n\n',options.ms.nlags);
0112 
0113     %//NOT read
0114     fprintf(fh,'//== Exogenous Variables ==//\n');
0115     fprintf(fh,'1\n\n');
0116 
0117 
0118     %//== nvar x n_state_variables matrix.  In the jth row, a non-zero value implies that
0119     %this state variable controls the jth column of A0 and Aplus
0120     fprintf(fh,['//== Controlling states variables for coefficients ==//\' ...
0121                 'n']);
0122     
0123     for i_var = 1:nvars
0124         for i_chain = 1:n_chains
0125             if ~isfield(options.ms.ms_chain(i_chain),'svar_coefficients') ...
0126                     || isempty(options.ms.ms_chain(i_chain).svar_coefficients)
0127                 i_equations = 0;
0128             else
0129                 i_equations = ...
0130                     options.ms.ms_chain(i_chain).svar_coefficients.equations; 
0131             end
0132             if strcmp(i_equations,'ALL') || any(i_equations == i_var)
0133                 fprintf(fh,'%d ',1);
0134             else
0135                 fprintf(fh,'%d ',0);
0136             end
0137         end
0138         fprintf(fh,'\n');
0139     end
0140 
0141     %//== nvar x n_state_variables matrix.  In the jth row, a non-zero value implies that
0142     %this state variable controls the jth diagonal element of Xi
0143     fprintf(fh,'\n//== Controlling states variables for variance ==//\n');
0144     for i_var = 1:nvars
0145         for i_chain = 1:n_chains
0146             if ~isfield(options.ms.ms_chain(i_chain),'svar_variances') ...
0147                     || isempty(options.ms.ms_chain(i_chain).svar_variances)
0148                     i_equations = 0;
0149             else
0150                 i_equations = ...
0151                     options.ms.ms_chain(i_chain).svar_variances.equations; 
0152             end
0153             if strcmp(i_equations,'ALL') || any(i_equations == i_var)
0154                 fprintf(fh,'%d ',1);
0155             else
0156                 fprintf(fh,'%d ',0);
0157             end
0158         end
0159         fprintf(fh,'\n');
0160     end
0161 
0162     fclose(fh);

Generated on Tue 22-May-2012 02:40:23 by m2html © 2005