1function myoutput=prior_posterior_statistics_core(myinputs,fpar,B,whoiam, ThisMatlab)
2% PARALLEL CONTEXT
3% Core functionality for prior_posterior.m function, which can be parallelized.
4% See also the comment in posterior_sampler_core.m funtion.
5%
6% INPUTS
7%   See the comment in posterior_sampler_core.m funtion.
8%
9% OUTPUTS
10% o myoutput  [struc]
11%  Contained OutputFileName_smooth;
12%                          _update;
13%                          _inno;
14%                          _error;
15%                          _filter_step_ahead;
16%                          _param;
17%                          _forc_mean;
18%                          _forc_point;
19%                          _forc_point_ME;
20%                          _filter_covar;
21%                          _trend_coeff;
22%                          _smoothed_trend;
23%                          _smoothed_constant;
24%                          _state_uncert;
25%
26% ALGORITHM
27%   Portion of prior_posterior.m function.
28% This file is part of Dynare.
29%
30% SPECIAL REQUIREMENTS.
31%   None.
32
33% Copyright (C) 2005-2020 Dynare Team
34%
35% This file is part of Dynare.
36%
37% Dynare is free software: you can redistribute it and/or modify
38% it under the terms of the GNU General Public License as published by
39% the Free Software Foundation, either version 3 of the License, or
40% (at your option) any later version.
41%
42% Dynare is distributed in the hope that it will be useful,
43% but WITHOUT ANY WARRANTY; without even the implied warranty of
44% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45% GNU General Public License for more details.
46%
47% You should have received a copy of the GNU General Public License
48% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
49
50global options_ oo_ M_ bayestopt_ estim_params_
51
52if nargin<4
53    whoiam=0;
54end
55
56% Reshape 'myinputs' for local computation.
57% In order to avoid confusion in the name space, the instruction struct2local(myinputs) is replaced by:
58
59type=myinputs.type;
60run_smoother=myinputs.run_smoother;
61filter_covariance=myinputs.filter_covariance;
62smoothed_state_uncertainty=myinputs.smoothed_state_uncertainty;
63gend=myinputs.gend;
64Y=myinputs.Y;
65data_index=myinputs.data_index;
66missing_value=myinputs.missing_value;
67varobs=myinputs.varobs;
68mean_varobs=myinputs.mean_varobs;
69irun=myinputs.irun;
70endo_nbr=myinputs.endo_nbr;
71nvn=myinputs.nvn;
72naK=myinputs.naK;
73horizon=myinputs.horizon;
74iendo=myinputs.iendo;
75IdObs=myinputs.IdObs; %index of observables
76if horizon
77    i_last_obs=myinputs.i_last_obs;
78    MAX_nforc1=myinputs.MAX_nforc1;
79    MAX_nforc2=myinputs.MAX_nforc2;
80    if ~isequal(M_.H,0)
81        MAX_nforc_ME=myinputs.MAX_nforc_ME;
82    end
83end
84if naK
85    MAX_naK=myinputs.MAX_naK;
86end
87if filter_covariance
88    MAX_filter_covariance=myinputs.MAX_filter_covariance;
89end
90if smoothed_state_uncertainty
91    MAX_n_smoothed_state_uncertainty=myinputs.MAX_n_smoothed_state_uncertainty;
92end
93
94exo_nbr=myinputs.exo_nbr;
95maxlag=myinputs.maxlag;
96MAX_nsmoo=myinputs.MAX_nsmoo;
97MAX_ninno=myinputs.MAX_ninno;
98MAX_n_smoothed_constant=myinputs.MAX_n_smoothed_constant;
99MAX_n_smoothed_trend=myinputs.MAX_n_smoothed_trend;
100MAX_n_trend_coeff=myinputs.MAX_n_trend_coeff;
101MAX_nerro = myinputs.MAX_nerro;
102MAX_nruns=myinputs.MAX_nruns;
103MAX_momentsno = myinputs.MAX_momentsno;
104ifil=myinputs.ifil;
105
106if ~strcmpi(type,'prior')
107    x=myinputs.x;
108    if strcmpi(type,'posterior')
109        logpost=myinputs.logpost;
110    end
111end
112if whoiam
113    Parallel=myinputs.Parallel;
114end
115
116% DirectoryName = myinputs.DirectoryName;
117if strcmpi(type,'posterior')
118    DirectoryName = CheckPath('metropolis',M_.dname);
119elseif strcmpi(type,'gsa')
120    if options_.opt_gsa.pprior
121        DirectoryName = CheckPath(['gsa',filesep,'prior'],M_.dname);
122    else
123        DirectoryName = CheckPath(['gsa',filesep,'mc'],M_.dname);
124    end
125elseif strcmpi(type,'prior')
126    DirectoryName = CheckPath('prior',M_.dname);
127end
128
129RemoteFlag = 0;
130if whoiam
131    if Parallel(ThisMatlab).Local==0
132        RemoteFlag =1;
133    end
134    ifil=ifil(:,whoiam);
135    prct0={0,whoiam,Parallel(ThisMatlab)};
136else
137    prct0=0;
138end
139h = dyn_waitbar(prct0,['Taking ',type,' subdraws...']);
140
141if RemoteFlag==1
142    OutputFileName_smooth = {};
143    OutputFileName_update = {};
144    OutputFileName_inno = {};
145    OutputFileName_error = {};
146    OutputFileName_filter_step_ahead = {};
147    OutputFileName_param = {};
148    OutputFileName_forc_mean = {};
149    OutputFileName_forc_point = {};
150    OutputFileName_forc_point_ME = {};
151    OutputFileName_filter_covar ={};
152    OutputFileName_trend_coeff = {};
153    OutputFileName_smoothed_trend = {};
154    OutputFileName_smoothed_constant = {};
155    % OutputFileName_moments = {};
156end
157
158%initialize arrays
159if run_smoother
160    stock_smooth=NaN(endo_nbr,gend,MAX_nsmoo);
161    stock_update=NaN(endo_nbr,gend,MAX_nsmoo);
162    stock_innov=NaN(M_.exo_nbr,gend,MAX_ninno);
163    stock_smoothed_constant=NaN(endo_nbr,gend,MAX_n_smoothed_constant);
164    stock_smoothed_trend=NaN(endo_nbr,gend,MAX_n_smoothed_trend);
165    stock_trend_coeff = zeros(endo_nbr,MAX_n_trend_coeff);
166    if horizon
167        stock_forcst_mean= NaN(endo_nbr,horizon,MAX_nforc1);
168        stock_forcst_point = NaN(endo_nbr,horizon,MAX_nforc2);
169        if ~isequal(M_.H,0)
170            stock_forcst_point_ME = NaN(length(varobs),horizon,MAX_nforc_ME);
171        end
172    end
173end
174if nvn
175    stock_error = NaN(length(varobs),gend,MAX_nerro);
176end
177if naK
178    stock_filter_step_ahead =NaN(length(options_.filter_step_ahead),endo_nbr,gend+max(options_.filter_step_ahead),MAX_naK);
179end
180stock_param = NaN(MAX_nruns,size(myinputs.x,2));
181stock_logpo = NaN(MAX_nruns,1);
182stock_ys = NaN(MAX_nruns,endo_nbr);
183if filter_covariance
184    stock_filter_covariance = zeros(endo_nbr,endo_nbr,gend+1,MAX_filter_covariance);
185end
186if smoothed_state_uncertainty
187    stock_smoothed_uncert = zeros(endo_nbr,endo_nbr,gend,MAX_n_smoothed_state_uncertainty);
188end
189
190for b=fpar:B
191    if strcmpi(type,'prior')
192
193        [deep, logpo] = GetOneDraw(type,M_,estim_params_,oo_,options_,bayestopt_);
194
195    else
196        deep = x(b,:);
197        if strcmpi(type,'posterior')
198            logpo = logpost(b);
199        else
200            logpo = evaluate_posterior_kernel(deep',M_,estim_params_,oo_,options_,bayestopt_);
201        end
202    end
203    M_ = set_all_parameters(deep,estim_params_,M_);
204
205    if run_smoother
206        [dr,info,M_,options_,oo_] =compute_decision_rules(M_,options_,oo_);
207        [alphahat,etahat,epsilonhat,alphatilde,SteadyState,trend_coeff,aK,~,~,P,~,~,trend_addition,state_uncertainty,M_,oo_,options_,bayestopt_] = ...
208            DsgeSmoother(deep,gend,Y,data_index,missing_value,M_,oo_,options_,bayestopt_,estim_params_);
209
210        stock_trend_coeff(options_.varobs_id,irun(9))=trend_coeff;
211        stock_smoothed_trend(IdObs,:,irun(11))=trend_addition;
212        if options_.loglinear %reads values from smoother results, which are in dr-order and put them into declaration order
213            constant_part=repmat(log(SteadyState(dr.order_var)),1,gend);
214            stock_smooth(dr.order_var,:,irun(1)) = alphahat(1:endo_nbr,:)+ ...
215                constant_part;
216            stock_update(dr.order_var,:,irun(1)) = alphatilde(1:endo_nbr,:)+ ...
217                constant_part;
218        else
219            constant_part=repmat(SteadyState(dr.order_var),1,gend);
220            stock_smooth(dr.order_var,:,irun(1)) = alphahat(1:endo_nbr,:)+ ...
221                constant_part;
222            stock_update(dr.order_var,:,irun(1)) = alphatilde(1:endo_nbr,:)+ ...
223                constant_part;
224        end
225        stock_smoothed_constant(dr.order_var,:,irun(10))=constant_part;
226        %% Compute constant for observables
227        if options_.prefilter == 1 %as mean is taken after log transformation, no distinction is needed here
228            constant_part=repmat(mean_varobs',1,gend);
229        elseif options_.prefilter == 0 && options_.loglinear %logged steady state must be used
230            constant_part=repmat(log(SteadyState(IdObs)),1,gend);
231        elseif options_.prefilter == 0 && ~options_.loglinear %unlogged steady state must be used
232            constant_part=repmat(SteadyState(IdObs),1,gend);
233        end
234        %add trend to observables
235        if options_.prefilter
236            %do correction for prefiltering for observed variables
237            if options_.loglinear
238                mean_correction=-repmat(log(SteadyState(IdObs)),1,gend)+constant_part;
239            else
240                mean_correction=-repmat(SteadyState(IdObs),1,gend)+constant_part;
241            end
242            stock_smoothed_constant(IdObs,:,irun(10))=stock_smoothed_constant(IdObs,:,irun(10))+mean_correction;
243            %smoothed variables are E_T(y_t) so no trend shift is required
244            stock_smooth(IdObs,:,irun(1))=stock_smooth(IdObs,:,irun(1))+trend_addition+mean_correction;
245            %updated variables are E_t(y_t) so no trend shift is required
246            stock_update(IdObs,:,irun(1))=stock_update(IdObs,:,irun(1))+trend_addition+mean_correction;
247        else
248            stock_smooth(IdObs,:,irun(1))=stock_smooth(IdObs,:,irun(1))+trend_addition;
249            stock_update(IdObs,:,irun(1))=stock_update(IdObs,:,irun(1))+trend_addition;
250        end
251        stock_innov(:,:,irun(2))  = etahat;
252        if nvn
253            stock_error(:,:,irun(3))  = epsilonhat;
254        end
255        if naK
256            %filtered variable E_t(y_t+k) requires to shift trend by k periods
257            %write variables into declaration order
258            if options_.loglinear %reads values from smoother results, which are in dr-order and put them into declaration order
259                constant_part=repmat(log(SteadyState(dr.order_var))',[length(options_.filter_step_ahead),1,gend+max(options_.filter_step_ahead)]);
260            else
261                constant_part=repmat(SteadyState(dr.order_var)',[length(options_.filter_step_ahead),1,gend+max(options_.filter_step_ahead)]);
262            end
263            stock_filter_step_ahead(:,dr.order_var,:,irun(4)) = aK(options_.filter_step_ahead,1:endo_nbr,:) + constant_part;
264            %now add trend to observables
265            for ii=1:length(options_.filter_step_ahead)
266                if options_.prefilter
267                    zdim = size(stock_filter_step_ahead(ii,IdObs,:,irun(4)));
268                    squeezed = reshape(stock_filter_step_ahead(ii,IdObs,:,irun(4)), [zdim(2:end) 1]);
269                    stock_filter_step_ahead(ii,IdObs,:,irun(4)) = squeezed ...
270                        +repmat(mean_correction(:,1),1,gend+max(options_.filter_step_ahead)) ... %constant correction
271                        +[trend_addition repmat(trend_addition(:,end),1,max(options_.filter_step_ahead))+trend_coeff*[1:max(options_.filter_step_ahead)]]; %trend
272                else
273                    zdim = size(stock_filter_step_ahead(ii,IdObs,:,irun(4)));
274                    squeezed = reshape(stock_filter_step_ahead(ii,IdObs,:,irun(4)), [zdim(2:end) 1]);
275                    stock_filter_step_ahead(ii,IdObs,:,irun(4)) = squeezed ...
276                        +[trend_addition repmat(trend_addition(:,end),1,max(options_.filter_step_ahead))+trend_coeff*[1:max(options_.filter_step_ahead)]]; %trend
277                end
278            end
279        end
280        if horizon
281            yyyy = alphahat(iendo,i_last_obs);
282            yf = forcst2a(yyyy,dr,zeros(horizon,exo_nbr));
283            if options_.prefilter
284                % add mean
285                yf(:,IdObs) = yf(:,IdObs)+repmat(mean_varobs, ...
286                                                 horizon+maxlag,1);
287                % add trend, taking into account that last point of sample is still included in forecasts and only cut off later
288                yf(:,IdObs) = yf(:,IdObs)+((options_.first_obs-1)+gend+[1-maxlag:horizon]')*trend_coeff'-...
289                    repmat(mean(trend_coeff*[options_.first_obs:options_.first_obs+gend-1],2)',length(1-maxlag:horizon),1); %center trend
290            else
291                % add trend, taking into account that last point of sample is still included in forecasts and only cut off later
292                yf(:,IdObs) = yf(:,IdObs)+((options_.first_obs-1)+gend+[1-maxlag:horizon]')*trend_coeff';
293            end
294            if options_.loglinear
295                yf = yf+repmat(log(SteadyState'),horizon+maxlag,1);
296            else
297                yf = yf+repmat(SteadyState',horizon+maxlag,1);
298            end
299            yf1 = forcst2(yyyy,horizon,dr,1);
300            if options_.prefilter == 1
301                % add mean
302                yf1(:,IdObs,:) = yf1(:,IdObs,:)+ ...
303                    repmat(mean_varobs,[horizon+maxlag,1,1]);
304                % add trend, taking into account that last point of sample is still included in forecasts and only cut off later
305                yf1(:,IdObs) = yf1(:,IdObs)+((options_.first_obs-1)+gend+[1-maxlag:horizon]')*trend_coeff'-...
306                    repmat(mean(trend_coeff*[options_.first_obs:options_.first_obs+gend-1],2)',length(1-maxlag:horizon),1); %center trend
307            else
308                % add trend, taking into account that last point of sample is still included in forecasts and only cut off later
309                yf1(:,IdObs,:) = yf1(:,IdObs,:)+repmat(((options_.first_obs-1)+gend+[1-maxlag:horizon]')* ...
310                                                       trend_coeff',[1,1,1]);
311            end
312            if options_.loglinear
313                yf1 = yf1 + repmat(log(SteadyState'),[horizon+maxlag,1,1]);
314            else
315                yf1 = yf1 + repmat(SteadyState',[horizon+maxlag,1,1]);
316            end
317
318            stock_forcst_mean(:,:,irun(6)) = yf(maxlag+1:end,:)';
319            stock_forcst_point(:,:,irun(7)) = yf1(maxlag+1:end,:)';
320            if ~isequal(M_.H,0)
321                ME_shocks=zeros(length(varobs),horizon);
322                i_exo_var = setdiff([1:length(varobs)],find(diag(M_.H) == 0));
323                nxs = length(i_exo_var);
324                chol_H = chol(M_.H(i_exo_var,i_exo_var));
325                if ~isempty(M_.H)
326                    ME_shocks(i_exo_var,:) = chol_H*randn(nxs,horizon);
327                end
328                stock_forcst_point_ME(:,:,irun(12)) = yf1(maxlag+1:end,IdObs)'+ME_shocks;
329            end
330        end
331        if filter_covariance
332            stock_filter_covariance(dr.order_var,dr.order_var,:,irun(8)) = P;
333        end
334        if smoothed_state_uncertainty
335            stock_smoothed_uncert(dr.order_var,dr.order_var,:,irun(13)) = state_uncertainty;
336        end
337    else
338        [T,R,SteadyState,info,M_,options_,oo_] = dynare_resolve(M_,options_,oo_);
339    end
340    stock_param(irun(5),:) = deep;
341    stock_logpo(irun(5),1) = logpo;
342    stock_ys(irun(5),:) = SteadyState';
343
344
345    irun = irun +  ones(13,1);
346
347
348    if run_smoother && (irun(1) > MAX_nsmoo || b == B)
349        stock = stock_smooth(:,:,1:irun(1)-1);
350        ifil(1) = ifil(1) + 1;
351        save([DirectoryName '/' M_.fname '_smooth' int2str(ifil(1)) '.mat'],'stock');
352
353        stock = stock_update(:,:,1:irun(1)-1);
354        save([DirectoryName '/' M_.fname '_update' int2str(ifil(1)) '.mat'],'stock');
355        if RemoteFlag==1
356            OutputFileName_smooth = [OutputFileName_smooth; {[DirectoryName filesep], [M_.fname '_smooth' int2str(ifil(1)) '.mat']}];
357            OutputFileName_update = [OutputFileName_update; {[DirectoryName filesep], [M_.fname '_update' int2str(ifil(1)) '.mat']}];
358        end
359        irun(1) = 1;
360    end
361
362    if run_smoother && (irun(2) > MAX_ninno || b == B)
363        stock = stock_innov(:,:,1:irun(2)-1);
364        ifil(2) = ifil(2) + 1;
365        save([DirectoryName '/' M_.fname '_inno' int2str(ifil(2)) '.mat'],'stock');
366        if RemoteFlag==1
367            OutputFileName_inno = [OutputFileName_inno; {[DirectoryName filesep], [M_.fname '_inno' int2str(ifil(2)) '.mat']}];
368        end
369        irun(2) = 1;
370    end
371
372    if run_smoother && nvn && (irun(3) > MAX_nerro || b == B)
373        stock = stock_error(:,:,1:irun(3)-1);
374        ifil(3) = ifil(3) + 1;
375        save([DirectoryName '/' M_.fname '_error' int2str(ifil(3)) '.mat'],'stock');
376        if RemoteFlag==1
377            OutputFileName_error = [OutputFileName_error; {[DirectoryName filesep], [M_.fname '_error' int2str(ifil(3)) '.mat']}];
378        end
379        irun(3) = 1;
380    end
381
382    if run_smoother && naK && (irun(4) > MAX_naK || b == B)
383        stock = stock_filter_step_ahead(:,:,:,1:irun(4)-1);
384        ifil(4) = ifil(4) + 1;
385        save([DirectoryName '/' M_.fname '_filter_step_ahead' int2str(ifil(4)) '.mat'],'stock');
386        if RemoteFlag==1
387            OutputFileName_filter_step_ahead = [OutputFileName_filter_step_ahead; {[DirectoryName filesep], [M_.fname '_filter_step_ahead' int2str(ifil(4)) '.mat']}];
388        end
389        irun(4) = 1;
390    end
391
392    if irun(5) > MAX_nruns || b == B
393        stock = stock_param(1:irun(5)-1,:);
394        stock_logpo = stock_logpo(1:irun(5)-1);
395        stock_ys = stock_ys(1:irun(5)-1,:);
396        ifil(5) = ifil(5) + 1;
397        save([DirectoryName '/' M_.fname '_param' int2str(ifil(5)) '.mat'],'stock','stock_logpo','stock_ys');
398        if RemoteFlag==1
399            OutputFileName_param = [OutputFileName_param; {[DirectoryName filesep], [M_.fname '_param' int2str(ifil(5)) '.mat']}];
400        end
401        irun(5) = 1;
402    end
403
404    if run_smoother && horizon && (irun(6) > MAX_nforc1 || b == B)
405        stock = stock_forcst_mean(:,:,1:irun(6)-1);
406        ifil(6) = ifil(6) + 1;
407        save([DirectoryName '/' M_.fname '_forc_mean' int2str(ifil(6)) '.mat'],'stock');
408        if RemoteFlag==1
409            OutputFileName_forc_mean = [OutputFileName_forc_mean; {[DirectoryName filesep], [M_.fname '_forc_mean' int2str(ifil(6)) '.mat']}];
410        end
411        irun(6) = 1;
412    end
413
414    if run_smoother && horizon && (irun(7) > MAX_nforc2 ||  b == B)
415        stock = stock_forcst_point(:,:,1:irun(7)-1);
416        ifil(7) = ifil(7) + 1;
417        save([DirectoryName '/' M_.fname '_forc_point' int2str(ifil(7)) '.mat'],'stock');
418        if RemoteFlag==1
419            OutputFileName_forc_point = [OutputFileName_forc_point; {[DirectoryName filesep], [M_.fname '_forc_point' int2str(ifil(7)) '.mat']}];
420        end
421        irun(7) = 1;
422    end
423
424    if run_smoother && filter_covariance && (irun(8) > MAX_filter_covariance || b == B)
425        stock = stock_filter_covariance(:,:,:,1:irun(8)-1);
426        ifil(8) = ifil(8) + 1;
427        save([DirectoryName '/' M_.fname '_filter_covar' int2str(ifil(8)) '.mat'],'stock');
428        if RemoteFlag==1
429            OutputFileName_filter_covar = [OutputFileName_filter_covar; {[DirectoryName filesep], [M_.fname '_filter_covar' int2str(ifil(8)) '.mat']}];
430        end
431        irun(8) = 1;
432    end
433
434    irun_index=9;
435    if run_smoother && (irun(irun_index) > MAX_n_trend_coeff || b == B)
436        stock = stock_trend_coeff(:,1:irun(irun_index)-1);
437        ifil(irun_index) = ifil(irun_index) + 1;
438        save([DirectoryName '/' M_.fname '_trend_coeff' int2str(ifil(irun_index)) '.mat'],'stock');
439        if RemoteFlag==1
440            OutputFileName_trend_coeff = [OutputFileName_trend_coeff; {[DirectoryName filesep], [M_.fname '_trend_coeff' int2str(ifil(irun_index)) '.mat']}];
441        end
442        irun(irun_index) = 1;
443    end
444
445    irun_index=10;
446    if run_smoother && (irun(irun_index) > MAX_n_smoothed_constant || b == B)
447        stock = stock_smoothed_constant(:,:,1:irun(irun_index)-1);
448        ifil(irun_index) = ifil(irun_index) + 1;
449        save([DirectoryName '/' M_.fname '_smoothed_constant' int2str(ifil(irun_index)) '.mat'],'stock');
450        if RemoteFlag==1
451            OutputFileName_smoothed_constant = [OutputFileName_smoothed_constant; {[DirectoryName filesep], [M_.fname '_smoothed_constant' int2str(ifil(irun_index)) '.mat']}];
452        end
453        irun(irun_index) = 1;
454    end
455
456    irun_index=11;
457    if run_smoother && (irun(irun_index) > MAX_n_smoothed_trend || b == B)
458        stock = stock_smoothed_trend(:,:,1:irun(irun_index)-1);
459        ifil(irun_index) = ifil(irun_index) + 1;
460        save([DirectoryName '/' M_.fname '_smoothed_trend' int2str(ifil(irun_index)) '.mat'],'stock');
461        if RemoteFlag==1
462            OutputFileName_smoothed_trend = [OutputFileName_smoothed_trend; {[DirectoryName filesep], [M_.fname '_smoothed_trend' int2str(ifil(irun_index)) '.mat']}];
463        end
464        irun(irun_index) = 1;
465    end
466
467    irun_index=12;
468    if run_smoother && horizon && ~isequal(M_.H,0) && (irun(irun_index) > MAX_nforc_ME ||  b == B)
469        stock = stock_forcst_point_ME(:,:,1:irun(irun_index)-1);
470        ifil(irun_index) = ifil(irun_index) + 1;
471        save([DirectoryName '/' M_.fname '_forc_point_ME' int2str(ifil(irun_index)) '.mat'],'stock');
472        if RemoteFlag==1
473            OutputFileName_forc_point_ME = [OutputFileName_forc_point_ME; {[DirectoryName filesep], [M_.fname '_forc_point_ME' int2str(ifil(irun_index)) '.mat']}];
474        end
475        irun(irun_index) = 1;
476    end
477
478    irun_index=13;
479    if run_smoother && smoothed_state_uncertainty && (irun(irun_index) > MAX_n_smoothed_state_uncertainty || b == B)
480        stock = stock_smoothed_uncert(:,:,:,1:irun(irun_index)-1);
481        ifil(irun_index) = ifil(irun_index) + 1;
482        save([DirectoryName '/' M_.fname '_state_uncert' int2str(ifil(irun_index)) '.mat'],'stock');
483        if RemoteFlag==1
484            OutputFileName_state_uncert = [OutputFileName_state_uncert; {[DirectoryName filesep], [M_.fname '_state_uncert' int2str(ifil(irun_index)) '.mat']}];
485        end
486        irun(irun_index) = 1;
487    end
488
489    dyn_waitbar((b-fpar+1)/(B-fpar+1),h);
490end
491
492myoutput.ifil=ifil;
493if RemoteFlag==1
494    myoutput.OutputFileName = [OutputFileName_smooth;
495                        OutputFileName_update;
496                        OutputFileName_inno;
497                        OutputFileName_error;
498                        OutputFileName_filter_step_ahead;
499                        OutputFileName_param;
500                        OutputFileName_forc_mean;
501                        OutputFileName_forc_point;
502                        OutputFileName_forc_point_ME;
503                        OutputFileName_filter_covar;
504                        OutputFileName_trend_coeff;
505                        OutputFileName_smoothed_trend;
506                        OutputFileName_smoothed_constant;
507                        OutputFileName_state_uncert];
508end
509
510dyn_waitbar_close(h);
511