File: slice_sampler.m

package info (click to toggle)
dynare 7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,248 kB
  • sloc: cpp: 82,011; ansic: 28,583; objc: 12,573; yacc: 5,105; pascal: 2,374; lex: 1,502; python: 1,118; sh: 1,116; makefile: 605; lisp: 162; xml: 18
file content (451 lines) | stat: -rw-r--r-- 17,477 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
141
142
143
144
145
146
147
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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
252
253
254
255
256
257
258
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
314
315
316
317
318
319
320
321
322
323
324
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
function [theta, fxsim, neval, sampler_options] = slice_sampler(objective_function,theta,thetaprior,sampler_options,varargin)
% [theta, fxsim, neval, sampler_options] = slice_sampler(objective_function,theta,thetaprior,sampler_options,varargin)
% ----------------------------------------------------------
% UNIVARIATE SLICE SAMPLER - stepping out (Neal, 2003)
% W: optimal value in the range (3,10)*std(x)
%    - see C.Planas and A.Rossi (2014)
% objective_function(theta,varargin): -log of any unnormalized pdf
% with varargin (optional) a vector of auxiliary parameters
% to be passed to f( ).
% ----------------------------------------------------------
%
% INPUTS
%   objective_function:       objective function (expressed as minus the log of a density)
%   theta:                    last value of theta
%   thetaprior:               bounds of the theta space
%   sampler_options:          posterior sampler options
%   varargin:                 optional input arguments to objective function
%
% OUTPUTS
%   theta:       new theta sample
%   fxsim:       value of the objective function for the new sample
%   neval:       number of function evaluations
%   sampler_options:          posterior sampler options
%
% SPECIAL REQUIREMENTS
%   none

% Copyright © 2015-2026 Dynare Team
%
% This file is part of Dynare.
%
% 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 <https://www.gnu.org/licenses/>.

endo_init_state = false;
draw_endo_init_state_from_smoother=false;
draw_endo_init_state_with_rotated_slice = false;
if isfield(sampler_options,'draw_init_state_with_rotated_slice') && sampler_options.draw_init_state_with_rotated_slice
    endo_init_state = true;
    draw_endo_init_state_with_rotated_slice = true;
end
if isfield(sampler_options,'draw_init_state_from_smoother') && sampler_options.draw_init_state_from_smoother
    endo_init_state = true;
    draw_endo_init_state_from_smoother=true;
end

if endo_init_state
    [index_init_state, IS, index_deep_parameters] = get_init_state_estim_params(varargin{4}, varargin{6}, varargin{8});
    thetaprior(index_init_state,1) = theta(index_init_state);
    thetaprior(index_init_state,2) = theta(index_init_state);
end

if sampler_options.rotated %&& ~isempty(sampler_options.V1),
    sampler_options.endo_init_state.status = endo_init_state;
    if endo_init_state
        sampler_options.endo_init_state.IB = index_init_state;
        sampler_options.endo_init_state.IP = index_deep_parameters;
    end
    [theta, fxsim, neval] = rotated_slice_sampler(objective_function,theta,thetaprior,sampler_options,varargin{:});
    if endo_init_state
        % draw initial states
        if draw_endo_init_state_from_smoother
            [theta, fxsim, ~, ~, neval_init] = draw_init_state_from_smoother([false 5],sampler_options,theta,fxsim,thetaprior,varargin{:});
            if ~isempty(index_init_state)
                neval(index_init_state(1)) = neval(index_init_state(1)) + neval_init;
            else
                neval(1) = neval(1) + neval_init;
            end
        elseif draw_endo_init_state_with_rotated_slice
            [V, D]=get_init_state_prior(theta,varargin{3:end});
            % take eigenvectors of state priors and set zero wieghts for other
            % params
            nslice = size(V,2);
            V=V(IS,:);
            V1 = zeros(length(theta),size(V,2));
            V1(index_init_state,:) = V;
            sampler_options.V1=V1;
            stderr = sqrt(diag(D));
            sampler_options.WR=stderr*3;
            for k=1:nslice
                bounds.lb(k) = norminv(1e-10, 0, stderr(k));
                bounds.ub(k) = norminv(1-1e-10, 0, stderr(k));
            end
            sampler_options.rthetaprior=[bounds.lb(:) bounds.ub(:)];
             % here params are fixed, so no need to account for change in
             % state prior!
            sampler_options.endo_init_state.status = false;
            %         sampler_options.WR=sampler_options.initial_step_size*(bounds.ub-bounds.lb);
            [theta, fxsim, neval1] = rotated_slice_sampler(objective_function,theta,thetaprior,sampler_options,varargin{:});
            [~, icheck]=set_init_state(theta,varargin{3:end});
            neval(index_init_state(1:nslice)) = neval1(1:nslice);
        end
    end
    if isempty(sampler_options.mode) % jumping
        return
    else
        nevalR=sum(neval);
    end
end

thetaprior0=thetaprior;
if isfield(sampler_options,'fast_likelihood_evaluation_for_rejection') && sampler_options.fast_likelihood_evaluation_for_rejection
    fast_likelihood_evaluation_for_rejection = true;
    rejection_penalty=sampler_options.fast_likelihood_evaluation_for_rejection_penalty;
else
    fast_likelihood_evaluation_for_rejection = false;
end

use_prior_draws = false;
if isfield(sampler_options,'use_prior_draws') && sampler_options.use_prior_draws.status
    use_prior_draws = sampler_options.use_prior_draws.status;
    try_prior_draws = sampler_options.use_prior_draws.mh_blck(sampler_options.curr_block);
end

theta=theta(:);
npar = length(theta);
W1 = sampler_options.W1;
neval = zeros(npar,1);

fname = [ int2str(sampler_options.curr_block)];

Prior = dprior(varargin{6},varargin{3}.prior_trunc);

if use_prior_draws && try_prior_draws
    fxsim = sampler_options.last_posterior;
    Z1 = fxsim + log(rand(1,1));
    ilogpo2=-inf;
    nattempts=0;
    while ilogpo2<Z1 && nattempts<10
        nattempts=nattempts+1;
        validate=false;
        while not(validate)
            candidate = Prior.draw();
            if all(candidate >= thetaprior0(:,1)) && all(candidate <= thetaprior0(:,2))
                if fast_likelihood_evaluation_for_rejection
                    itest = -rejection_objective_function(objective_function,theta,Z1-rejection_penalty,varargin{:});
                else
                    itest = -feval(objective_function,candidate,varargin{:});
                end
                if isfinite(itest)
                    validate=true;
                end
            end
        end
        if itest>ilogpo2
            ilogpo2= itest;
            best_candidate = candidate;
        end
    end
    if ilogpo2>=Z1
        theta= best_candidate;
        fxsim = ilogpo2;
    else
        % prior draw is worse than current draw, so I stop drawing from
        % prior in this chain
        sampler_options.use_prior_draws.mh_blck(sampler_options.curr_block)=false;
    end
end


it=0;
islow=false(npar,1);
while it<npar
    it=it+1;
    neval(it) = 0;
    W = W1(it);
    xold  = theta(it);
    theta0=theta;
    XLB   = thetaprior(it,1);
    XUB   = thetaprior(it,2);
    if XLB==XUB
        continue
    end


    % -------------------------------------------------------
    % 1. DRAW Z = ln[f(X0)] - EXP(1) where EXP(1)=-ln(U(0,1))
    %    THIS DEFINES THE SLICE S={x: z < ln(f(x))}
    % -------------------------------------------------------
    fxold = -feval(objective_function,theta,varargin{:});
    if endo_init_state
        ys0 = get_steady_state(theta,varargin{3:end});
    end
    if ~isfinite(fxold)
        disp(['slice_sampler:: Iteration ' int2str(it) ' started with bad parameter set (fval is inf or nan)'])
        icount=0;
        while ~isfinite(fxold) && icount<1000
            icount=icount+1;
            theta = Prior.draw();
            if all(theta >= thetaprior(:,1)) && all(theta <= thetaprior(:,2))
                fxold = -feval(objective_function,theta,varargin{:});
            end
        end
        % restart from 1
        it = 1;
        neval(it) = 0;
        W = W1(it);
        xold  = theta(it);
        XLB   = thetaprior(it,1);
        XUB   = thetaprior(it,2);
    end
    neval(it) = neval(it) + 1;
    Z = fxold + log(rand(1,1));
    % -------------------------------------------------------------
    % 2. FIND I=(L,R) AROUND X0 THAT CONTAINS S AS MUCH AS POSSIBLE
    %    STEPPING-OUT PROCEDURE
    % -------------------------------------------------------------
    u = rand(1,1);
    L = max(XLB,xold-W*u);
    R = min(XUB,L+W);
    mytxt{it,1} = '';
    while(L > XLB)
        xsim = L;
        theta(it) = xsim;
        if endo_init_state
            theta0(it) = xsim;
            [theta, icheck]=set_init_state(theta0,ys0,varargin{3:end});
        end
        if fast_likelihood_evaluation_for_rejection
            fxl = -rejection_objective_function(objective_function,theta,Z-rejection_penalty,varargin{:});
         else
            fxl = -feval(objective_function,theta,varargin{:});
        end
        neval(it) = neval(it) + 1;
        if (fxl <= Z)
            break
        end
        L = max(XLB,L-W);
        if neval(it)>30
            L=XLB;
            xsim = L;
            theta(it) = xsim;
            if endo_init_state
                theta0(it) = xsim;
                [theta, icheck]=set_init_state(theta0,varargin{3:end});
            end
            fxl = -feval(objective_function,theta,varargin{:});
            icount = 0;
            while (isinf(fxl) || isnan(fxl)) && icount<300
                icount = icount+1;
                L=L+sqrt(eps);
                xsim = L;
                theta(it) = xsim;
                if endo_init_state
                    theta0(it) = xsim;
                    [theta, icheck]=set_init_state(theta0,varargin{3:end});
                end
                fxl = -feval(objective_function,theta,varargin{:});
            end
            mytxt{it,1} = sprintf('Getting L for [%s] is taking too long.', varargin{6}.name{it});
            if sampler_options.save_iter_info_file
                save([varargin{4}.dname filesep 'metropolis/slice_iter_info_' fname],'mytxt','neval','it','theta','fxl')
            end
        end
    end
    neval1 = neval(it);
    mytxt{it,2} = '';
    while(R < XUB)
        xsim = R;
        theta(it) = xsim;
        if endo_init_state
            theta0(it) = xsim;
            [theta, icheck]=set_init_state(theta0,ys0,varargin{3:end});
        end
        if fast_likelihood_evaluation_for_rejection
            fxr = -rejection_objective_function(objective_function,theta,Z-rejection_penalty,varargin{:});
        else
            fxr = -feval(objective_function,theta,varargin{:});
        end
        neval(it) = neval(it) + 1;
        if (fxr <= Z)
            break
        end
        R = min(XUB,R+W);
        if neval(it)>(neval1+30)
            R=XUB;
            xsim = R;
            theta(it) = xsim;
            if endo_init_state
                theta0(it) = xsim;
                [theta, icheck]=set_init_state(theta0,ys0,varargin{3:end});
            end
            fxr = -feval(objective_function,theta,varargin{:});
            icount = 0;
            while (isinf(fxr) || isnan(fxr)) && icount<300
                icount = icount+1;
                R=R-sqrt(eps);
                xsim = R;
                theta(it) = xsim;
                if endo_init_state
                    theta0(it) = xsim;
                    [theta, icheck]=set_init_state(theta0,ys0,varargin{3:end});
                end
                fxr = -feval(objective_function,theta,varargin{:});
            end
            mytxt{it,2} = sprintf('Getting R for [%s] is taking too long.', varargin{6}.name{it});
            if sampler_options.save_iter_info_file
                save([varargin{4}.dname filesep 'metropolis/slice_iter_info_' fname],'mytxt','neval','it','theta','fxr')
            end
        end
    end
    % ------------------------------------------------------
    % 3. SAMPLING FROM THE SET A = (I INTERSECT S) = (LA,RA)
    % ------------------------------------------------------
    fxsim = Z-1;
    mytxt{it,3} = '';
    while (fxsim < Z)
        u = rand(1,1);
        xsim = L + u*(R - L);
        theta(it) = xsim;
        if endo_init_state
            theta0(it) = xsim;
            [theta, icheck]=set_init_state(theta0,ys0,varargin{3:end});
        end
        if fast_likelihood_evaluation_for_rejection
            fxsim = -rejection_objective_function(objective_function,theta,Z-rejection_penalty,varargin{:});
        else
            fxsim = -feval(objective_function,theta,varargin{:});
        end
        neval(it) = neval(it) + 1;
        if (xsim > xold)
            R = xsim;
        else
            L = xsim;
        end
        if (R-L)<1.e-6 %neval(it)>(30+neval2)
            fprintf('The sampling for parameter [%s] is taking too long as the sampling set is too tight. Check the prior.\n', varargin{6}.name{it})
            mytxt{it,3} = sprintf('Sampling [%s] is taking too long.', varargin{6}.name{it});
            if sampler_options.save_iter_info_file
                save([varargin{4}.dname filesep 'metropolis/slice_iter_info_' fname],'mytxt','neval','it')
            end
            islow(it)=true;
            theta(it) = xold;
            fxsim = fxold;
            break
        end
    end
    if sampler_options.save_iter_info_file
        save([varargin{4}.dname filesep 'metropolis/slice_iter_info_' fname],'mytxt','neval','it','theta','fxsim')
    end
    if isinf(fxsim) || isnan(fxsim)
        theta(it) = xold;
        fxsim = fxold;
        disp('SLICE: posterior density is infinite. Reset values at initial ones.')
    end
    if endo_init_state && icheck %(icheck || islow)
        [theta, fxsim, ~, ~, neval_init] = draw_init_state_from_smoother([false 1],sampler_options,theta,fxsim,thetaprior,varargin{:});
        if ~isempty(index_init_state)
            neval(index_init_state(1)) = neval(index_init_state(1)) + neval_init;
        else
            neval(1) = neval(1) + neval_init;
        end
    end
    if isinf(fxsim) || isnan(fxsim)
        theta(it) = xold;
        fxsim = fxold;
        disp('SLICE: posterior density is infinite. Reset values at initial ones.')
    end
    if sampler_options.save_iter_info_file
        save([varargin{4}.dname filesep 'metropolis/slice_iter_info_' fname],'mytxt','neval','it','theta','fxsim')
    end
end

if any(islow)
    fxsim = -feval(objective_function,theta,varargin{:});
    Z1 = fxsim + log(rand(1,1));
    ilogpo2=-inf;
    nattempts=0;
    while ilogpo2<Z1 && nattempts<10
        nattempts=nattempts+1;


        validate=false;
        while not(validate)
            candidate = theta;
            my_candidate = Prior.draw();
            candidate(islow) = my_candidate(islow);

            if all(candidate(islow) >= thetaprior(islow,1)) && all(candidate(islow) <= thetaprior(islow,2))
                if endo_init_state
                    init = true;
                    [candidate] = draw_init_state_from_smoother(init,sampler_options,candidate,nan,thetaprior,varargin{:});
                end
                itest = -feval(objective_function,candidate,varargin{:});
                if isfinite(itest)
                    validate=true;
                end

            end
        end
        if itest>ilogpo2
            ilogpo2= itest;
            best_candidate = candidate;
        end
    end
    theta= best_candidate;
    fxsim = ilogpo2;
end
if endo_init_state
    % draw initial states
    if draw_endo_init_state_from_smoother
        [theta, fxsim, ~, ~, neval_init] = draw_init_state_from_smoother([false 5],sampler_options,theta,fxsim,thetaprior,varargin{:});
        if ~isempty(index_init_state)
            neval(index_init_state(1)) = neval(index_init_state(1)) + neval_init;
        else
            neval(1) = neval(1) + neval_init;
        end
    elseif draw_endo_init_state_with_rotated_slice
        [V, D]=get_init_state_prior(theta,varargin{3:end});
        % take eigenvectors of state priors and set zero wieghts for other
        % params
        nslice = size(V,2);
        V=V(IS,:);
        V1 = zeros(length(theta),size(V,2));
        V1(index_init_state,:) = V;
        sampler_options.V1=V1;
        stderr = sqrt(diag(D));
        sampler_options.WR=stderr*3;
        for k=1:nslice
            bounds.lb(k) = norminv(1e-10, 0, stderr(k));
            bounds.ub(k) = norminv(1-1e-10, 0, stderr(k));
        end
        sampler_options.rthetaprior=[bounds.lb(:) bounds.ub(:)];
        %         sampler_options.WR=sampler_options.initial_step_size*(bounds.ub-bounds.lb);
        [theta, fxsim, neval1] = rotated_slice_sampler(objective_function,theta,thetaprior,sampler_options,varargin{:});
        [~, icheck]=set_init_state(theta,ys0,varargin{3:end});
        neval(index_init_state(1:nslice)) = neval1(1:nslice);
    end
    save([varargin{4}.dname filesep 'metropolis/slice_iter_info_' fname],'mytxt','neval','it','theta','fxsim')
end

if sampler_options.rotated && ~isempty(sampler_options.mode) % jumping
    neval=sum(neval)+nevalR;
end


function ys = get_steady_state(xparam1, options_,M_,estim_params_,~,~,~, endo_steady_state, exo_steady_state, exo_det_steady_state)
% wrapper function to get steady state

M_ = set_all_parameters(xparam1,estim_params_,M_);
ys = evaluate_steady_state(endo_steady_state,[exo_steady_state; exo_det_steady_state],M_,options_,true);