redform_map.m 17.5 KB
Newer Older
1
function redform_map(dirname,options_gsa_)
2
3
4
5
6
7
8
9
10
11
12
13
%function redform_map(dirname)
% inputs (from opt_gsa structure
% anamendo    = options_gsa_.namendo;
% anamlagendo = options_gsa_.namlagendo;
% anamexo     = options_gsa_.namexo;
% iload       = options_gsa_.load_redform;
% pprior      = options_gsa_.pprior;
% ilog        = options_gsa_.logtrans_redform;
% threshold   = options_gsa_.threshold_redform;
% ksstat      = options_gsa_.ksstat_redform;
% alpha2      = options_gsa_.alpha2_redform;
%
14
% Written by Marco Ratto
15
16
% Joint Research Centre, The European Commission,
% (http://eemc.jrc.ec.europa.eu/),
17
% marco.ratto@jrc.it
18
19
20
%
% Reference:
% M. Ratto, Global Sensitivity Analysis for Macroeconomic models, MIMEO, 2006.
21
22
23
24

% Copyright (C) 2012 Dynare Team
%
% This file is part of Dynare.
25
%
26
27
28
29
30
31
32
33
34
35
36
37
38
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

39
40
41

global M_ oo_ estim_params_ options_ bayestopt_

42
% options_gsa_ = options_.opt_gsa;
43
44
45
46
47
48
49
50

anamendo = options_gsa_.namendo;
anamlagendo = options_gsa_.namlagendo;
anamexo = options_gsa_.namexo;
iload = options_gsa_.load_redform;
pprior = options_gsa_.pprior;
ilog = options_gsa_.logtrans_redform;
threshold = options_gsa_.threshold_redform;
51
% ksstat = options_gsa_.ksstat_redform;
52
alpha2 = options_gsa_.alpha2_redform;
53
54
55
alpha2=0;
pvalue_ks = options_gsa_.ksstat_redform;
pvalue_corr = options_gsa_.alpha2_redform;
56
57
58

pnames = M_.param_names(estim_params_.param_vals(:,1),:);
if nargin==0,
59
    dirname='';
60
61
62
end

if pprior
63
64
    load([dirname,'/',M_.fname,'_prior'],'lpmat', 'lpmat0', 'istable','T');
    adir=[dirname '/redform_stab'];
65
else
66
67
    load([dirname,'/',M_.fname,'_mc'],'lpmat', 'lpmat0', 'istable','T');
    adir=[dirname '/redform_mc'];
68
69
end
if ~exist('T')
70
71
72
73
74
75
    stab_map_(dirname,options_gsa_);
    if pprior
        load([dirname,'/',M_.fname,'_prior'],'T');
    else
        load([dirname,'/',M_.fname,'_mc'],'T');
    end
76
77
78
79
80
    if ~exist('T'),
        disp('The model is too large!')
        disp('Reduced form mapping stopped!')
        return
    end
81
82
end
if isempty(dir(adir))
83
    mkdir(adir)
84
85
86
87
88
89
end
adir0=pwd;
%cd(adir)

nspred=size(T,2)-M_.exo_nbr;
x0=lpmat(istable,:);
90
xx0=lpmat0(istable,:);
91
92
93
[kn, np]=size(x0);
offset = length(bayestopt_.pshape)-np;
if options_gsa_.prior_range,
94
95
    pshape=5*(ones(np,1));
    pd =  [NaN(np,1) NaN(np,1) bayestopt_.lb(offset+1:end) bayestopt_.ub(offset+1:end)];
96
else
97
98
    pshape = bayestopt_.pshape(offset+1:end);
    pd =  [bayestopt_.p6(offset+1:end) bayestopt_.p7(offset+1:end) bayestopt_.p3(offset+1:end) bayestopt_.p4(offset+1:end)];
99
100
101
end

nsok = length(find(M_.lead_lag_incidence(M_.maximum_lag,:)));
102
clear lpmat lpmat0
103
104
js=0;
for j=1:size(anamendo,1)
105
106
107
108
109
110
111
112
113
    namendo=deblank(anamendo(j,:));
    iendo=strmatch(namendo,M_.endo_names(oo_.dr.order_var,:),'exact');
    ifig=0;
    iplo=0;
    for jx=1:size(anamexo,1)
        namexo=deblank(anamexo(jx,:));
        iexo=strmatch(namexo,M_.exo_names,'exact');
        disp(' ')
        disp(['[', namendo,' vs. ',namexo,']'])
114

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
        
        if ~isempty(iexo),
            %y0=squeeze(T(iendo,iexo+nspred,istable));
            y0=squeeze(T(iendo,iexo+nspred,:));
            if (max(y0)-min(y0))>1.e-10,
                if mod(iplo,9)==0 && isempty(threshold) && ~options_.nograph,
                    ifig=ifig+1;
                    hfig = dyn_figure(options_,'name',[namendo,' vs. shocks ',int2str(ifig)]);
                    iplo=0;
                end
                iplo=iplo+1;
                js=js+1;
                xdir0 = [adir,'/',namendo,'_vs_', namexo];
                if ilog==0,
                    if isempty(threshold)
                        if isempty(dir(xdir0))
                            mkdir(xdir0)
                        end
                        si(:,js) = redform_private(x0, y0, pshape, pd, iload, pnames, namendo, namexo, xdir0, options_gsa_);
                    else
                        iy=find( (y0>threshold(1)) & (y0<threshold(2)));
                        iyc=find( (y0<=threshold(1)) | (y0>=threshold(2)));
                        xdir = [xdir0,'_cut'];
                        if isempty(dir(xdir))
                            mkdir(xdir)
                        end
141
142
143
144
145
146
147
                        if ~options_.nograph,
                            hf=dyn_figure(options_); hist(y0,30), title([namendo,' vs. ', namexo])
                            dyn_saveas(hf,[xdir,'/', namendo,'_vs_', namexo],options_);
                            if ~options_.nodisplay
                                close(hf);
                            end
                        end
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
                        %             if ~isempty(iy),
                        %               si(:,js) = redform_private(x0(iy,:), y0(iy), pshape, pd, iload, pnames, namendo, namexo, xdir, options_gsa_);
                        %             else
                        si(:,js) = NaN(np,1);
                        %             end
                        if ~isempty(iy) && ~isempty(iyc)
                            delete([xdir, '/*cut*.*'])
                            [proba, dproba] = stab_map_1(x0, iy, iyc, 'cut',0);
                            %             indsmirnov = find(dproba>ksstat);
                            indsmirnov = find(proba<pvalue_ks);
                            for jp=1:length(indsmirnov),
                                disp([M_.param_names(estim_params_.param_vals(indsmirnov(jp),1),:),'   d-stat = ', num2str(dproba(indsmirnov(jp)),'%1.3f'),'   p-value = ', num2str(proba(indsmirnov(jp)),'%1.3f')])
                            end
                            disp(' ');
                            stab_map_1(x0, iy, iyc, 'cut',1,indsmirnov,xdir);
                            stab_map_2(x0(iy,:),alpha2,pvalue_corr,'cut',xdir)
                            stab_map_2(x0(iyc,:),alpha2,pvalue_corr,'trim',xdir)
                            lpmat=x0(iy,:);
                            lpmat0=xx0(iy,:);
                            istable=[1:length(iy)];
                            save([xdir,filesep,'threshold.mat'],'lpmat','lpmat0','istable')
                            clear lpmat lpmat0 istable
                        end
                    end
                else
                    [yy, xdir] = log_trans_(y0,xdir0);
                    if isempty(dir(xdir))
                        mkdir(xdir)
                    end
                    silog(:,js) = redform_private(x0, yy, pshape, pd, iload, pnames, namendo, namexo, xdir, options_gsa_);
                end
                
                if isempty(threshold) && ~options_.nograph,
                    figure(hfig)
                    subplot(3,3,iplo),
                    if ilog,
                        [saso, iso] = sort(-silog(:,js));
                        bar([silog(iso(1:min(np,10)),js)])
                        logflag='log';
                    else
                        [saso, iso] = sort(-si(:,js));
                        bar(si(iso(1:min(np,10)),js))
                        logflag='';
                    end
                    %set(gca,'xticklabel',pnames(iso(1:min(np,10)),:),'fontsize',8)
                    set(gca,'xticklabel',' ','fontsize',10)
                    set(gca,'xlim',[0.5 10.5])
                    for ip=1:min(np,10),
                        text(ip,-0.02,deblank(pnames(iso(ip),:)),'rotation',90,'HorizontalAlignment','right','interpreter','none')
                    end
                    title([logflag,' ',namendo,' vs. ',namexo],'interpreter','none')
                    if iplo==9,
                        dyn_saveas(hfig,[dirname,'/',M_.fname,'_redform_', namendo,'_vs_shocks_',logflag,num2str(ifig)],options_);
                        if ~options_.nodisplay
                            close(hfig);
                        end
                    end
                end
                
207
208
            end
        end
209
210
211
212
    end
    if iplo<9 && iplo>0 && ifig && ~options_.nograph,
        dyn_saveas(hfig,[dirname,'/',M_.fname,'_redform_', namendo,'_vs_shocks_',logflag,num2str(ifig)],options_);
        if ~options_.nodisplay
213
            close(hfig);
214
        end
215
    end
216
217
218
219
    ifig=0;
    iplo=0;
    for je=1:size(anamlagendo,1)
        namlagendo=deblank(anamlagendo(je,:));
220
        ilagendo=strmatch(namlagendo,M_.endo_names(oo_.dr.order_var(M_.nstatic+1:M_.nstatic+nsok),:),'exact');
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
        disp(' ')
        disp(['[', namendo,' vs. lagged ',namlagendo,']'])
        
        if ~isempty(ilagendo),
            %y0=squeeze(T(iendo,ilagendo,istable));
            y0=squeeze(T(iendo,ilagendo,:));
            if (max(y0)-min(y0))>1.e-10,
                if mod(iplo,9)==0 && isempty(threshold) && ~options_.nograph,
                    ifig=ifig+1;
                    hfig = dyn_figure(options_,'name',[namendo,' vs. lags ',int2str(ifig)]);
                    iplo=0;
                end
                iplo=iplo+1;
                js=js+1;
                xdir0 = [adir,'/',namendo,'_vs_', namlagendo];
                if ilog==0,
                    if isempty(threshold)
                        if isempty(dir(xdir0))
                            mkdir(xdir0)
                        end
                        si(:,js) = redform_private(x0, y0, pshape, pd, iload, pnames, namendo, namlagendo, xdir0, options_gsa_);
                    else
                        iy=find( (y0>threshold(1)) & (y0<threshold(2)));
                        iyc=find( (y0<=threshold(1)) | (y0>=threshold(2)));
                        xdir = [xdir0,'_cut'];
                        if isempty(dir(xdir))
                            mkdir(xdir)
                        end
                        %           if ~isempty(iy)
                        %           si(:,js) = redform_private(x0(iy,:), y0(iy), pshape, pd, iload, pnames, namendo, namlagendo, xdir, options_gsa_);
                        %           end
252
253
254
255
256
257
258
                        if ~options_.nograph,
                            hf=dyn_figure(options_); hist(y0,30), title([namendo,' vs. ', namlagendo])
                            dyn_saveas(hf,[xdir,'/', namendo,'_vs_', namlagendo],options_);
                            if ~options_.nodisplay
                                close(hf);
                            end
                        end
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
                        if ~isempty(iy) && ~isempty(iyc),
                            delete([xdir, '/*cut*.*'])
                            [proba, dproba] = stab_map_1(x0, iy, iyc, 'cut',0);
                            %           indsmirnov = find(dproba>ksstat);
                            indsmirnov = find(proba<pvalue_ks);
                            for jp=1:length(indsmirnov),
                                disp([M_.param_names(estim_params_.param_vals(indsmirnov(jp),1),:),'   d-stat = ', num2str(dproba(indsmirnov(jp)),'%1.3f'),'   p-value = ', num2str(proba(indsmirnov(jp)),'%1.3f')])
                            end
                            disp(' ');
                            stab_map_1(x0, iy, iyc, 'cut',1,indsmirnov,xdir);
                            stab_map_2(x0(iy,:),alpha2,pvalue_corr,'cut',xdir)
                            stab_map_2(x0(iyc,:),alpha2,pvalue_corr,'trim',xdir)
                            lpmat=x0(iy,:);
                            lpmat0=xx0(iy,:);
                            istable=[1:length(iy)];
                            save([xdir,filesep,'threshold.mat'],'lpmat','lpmat0','istable')
                            clear lpmat lpmat0 istable
                            
                        end
                    end
                else
                    [yy, xdir] = log_trans_(y0,xdir0);
                    if isempty(dir(xdir))
                        mkdir(xdir)
                    end
                    silog(:,js) = redform_private(x0, yy, pshape, pd, iload, pnames, namendo, namlagendo, xdir, options_gsa_);
                end
                
                if isempty(threshold) && ~options_.nograph
                    figure(hfig),
                    subplot(3,3,iplo),
                    if ilog,
                        [saso, iso] = sort(-silog(:,js));
                        bar([silog(iso(1:min(np,10)),js)])
                        logflag='log';
                    else
                        [saso, iso] = sort(-si(:,js));
                        bar(si(iso(1:min(np,10)),js))
                        logflag='';
                    end
                    %set(gca,'xticklabel',pnames(iso(1:min(np,10)),:),'fontsize',8)
                    set(gca,'xticklabel',' ','fontsize',10)
                    set(gca,'xlim',[0.5 10.5])
                    for ip=1:min(np,10),
                        text(ip,-0.02,deblank(pnames(iso(ip),:)),'rotation',90,'HorizontalAlignment','right','interpreter','none')
                    end
                    title([logflag,' ',namendo,' vs. ',namlagendo,'(-1)'],'interpreter','none')
                    if iplo==9,
                        dyn_saveas(hfig,[dirname,'/',M_.fname,'_redform_', namendo,'_vs_lags_',logflag,num2str(ifig)],options_);
                        if ~options_.nodisplay
                            close(hfig);
                        end
                    end
                end
                
314
            end
315
316
        end
    end
317
318
319
320
321
    if iplo<9 && iplo>0 && ifig && ~options_.nograph,
        dyn_saveas(hfig,[dirname,'/',M_.fname,'_redform_', namendo,'_vs_lags_',logflag,num2str(ifig)],options_);
        if ~options_.nodisplay
            close(hfig);
        end
322
    end
323
324
end

325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
if isempty(threshold) && ~options_.nograph,
    if ilog==0,
        hfig=dyn_figure(options_); %bar(si)
        % boxplot(si','whis',10,'symbol','r.')
        myboxplot(si',[],'.',[],10)
        xlabel(' ')
        set(gca,'xticklabel',' ','fontsize',10,'xtick',[1:np])
        set(gca,'xlim',[0.5 np+0.5])
        set(gca,'ylim',[0 1])
        set(gca,'position',[0.13 0.2 0.775 0.7])
        for ip=1:np,
            text(ip,-0.02,deblank(pnames(ip,:)),'rotation',90,'HorizontalAlignment','right','interpreter','none')
        end
        title('Reduced form GSA')
        dyn_saveas(hfig,[dirname,'/',M_.fname,'_redform_gsa'],options_);
        
    else
        hfig=dyn_figure(options_); %bar(silog)
        % boxplot(silog','whis',10,'symbol','r.')
        myboxplot(silog',[],'.',[],10)
        set(gca,'xticklabel',' ','fontsize',10,'xtick',[1:np])
        xlabel(' ')
        set(gca,'xlim',[0.5 np+0.5])
        set(gca,'ylim',[0 1])
        set(gca,'position',[0.13 0.2 0.775 0.7])
        for ip=1:np,
            text(ip,-0.02,deblank(pnames(ip,:)),'rotation',90,'HorizontalAlignment','right','interpreter','none')
        end
        title('Reduced form GSA - Log-transformed elements')
        dyn_saveas(hfig,[dirname,'/',M_.fname,'_redform_gsa_log'],options_);
        
    end
357
358
359
end

function si  = redform_private(x0, y0, pshape, pd, iload, pnames, namy, namx, xdir, opt_gsa)
360
361
global bayestopt_ options_

362
% opt_gsa=options_.opt_gsa;
363
364
np=size(x0,2);
x00=x0;
365
if opt_gsa.prior_range,
366
    for j=1:np,
367
        x0(:,j)=(x0(:,j)-pd(j,3))./(pd(j,4)-pd(j,3));
368
    end
369
else
370
    x0=priorcdf(x0,pshape, pd(:,1), pd(:,2), pd(:,3), pd(:,4));
371
end
372
373
374

fname=[xdir,'/map'];
if iload==0,
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
    if isempty(dir(xdir))
        mkdir(xdir)
    end
    if ~options_.nograph,
        hfig=dyn_figure(options_); hist(y0,30), title([namy,' vs. ', namx])
        dyn_saveas(hfig,[xdir,'/', namy,'_vs_', namx],options_);
        if ~options_.nodisplay
            close(hfig);
        end
    end
    %   gsa_ = gsa_sdp_dyn(y0, x0, -2, [],[],[],1,fname, pnames);
    nrun=length(y0);
    nest=min(250,nrun);
    nfit=min(1000,nrun);
    %   dotheplots = (nfit<=nest);
    gsa_ = gsa_sdp(y0(1:nest), x0(1:nest,:), 2, [],[-1 -1 -1 -1 -1 0],[],0,[fname,'_est'], pnames);
    if nfit>nest,
        gsa_ = gsa_sdp(y0(1:nfit), x0(1:nfit,:), -2, gsa_.nvr*nest^3/nfit^3,[-1 -1 -1 -1 -1 0],[],0,fname, pnames);
    end
    save([fname,'.mat'],'gsa_')
    [sidum, iii]=sort(-gsa_.si);
    gsa_.x0=x00(1:nfit,:);
    if ~options_.nograph,
        hfig=gsa_sdp_plot(gsa_,fname,pnames,iii(1:min(12,np)));
        close(hfig);
    end
    gsa_.x0=x0(1:nfit,:);
    %   copyfile([fname,'_est.mat'],[fname,'.mat'])
    if ~options_.nograph,
        hfig=dyn_figure(options_);
        plot(y0(1:nfit),[gsa_.fit y0(1:nfit)],'.'),
        title([namy,' vs. ', namx,' fit'])
        dyn_saveas(hfig,[xdir,'/', namy,'_vs_', namx,'_fit'],options_);
        if ~options_.nodisplay
            close(hfig);
        end
        if nfit<nrun,
            npred=[nfit+1:nrun];
            yf = ss_anova_fcast(x0(npred,:), gsa_);
            hfig=dyn_figure(options_);
            plot(y0(npred),[yf y0(npred)],'.'),
            title([namy,' vs. ', namx,' pred'])
            dyn_saveas(hfig,[xdir,'/', namy,'_vs_', namx,'_pred'],options_);
            if ~options_.nodisplay
                close(hfig);
            end
        end
        
    end
424
else
425
426
427
428
429
430
431
432
433
434
435
436
    %   gsa_ = gsa_sdp_dyn(y0, x0, 0, [],[],[],0,fname, pnames);
    gsa_ = gsa_sdp(y0, x0, 0, [],[],[],0,fname, pnames);
    if ~options_.nograph,
        yf = ss_anova_fcast(x0, gsa_);
        hfig=dyn_figure(options_);
        plot(y0,[yf y0],'.'),
        title([namy,' vs. ', namx,' pred'])
        dyn_saveas(hfig,[xdir,'/', namy,'_vs_', namx,'_pred'],options_);
        if ~options_.nodisplay
            close(hfig);
        end
    end
437
438
439
440
end
% si = gsa_.multivariate.si;
si = gsa_.si;