-
Notifications
You must be signed in to change notification settings - Fork 0
/
stationary_bootstrap.m
68 lines (63 loc) · 1.86 KB
/
stationary_bootstrap.m
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
function [bsdata, indices]=stationary_bootstrap(data,B,w)
% Implements the stationary bootstrap for bootstrapping stationary, dependent series
%
% USAGE:
% [BSDATA, INDICES] = stationary_bootstrap(DATA,B,W)
%
% INPUTS:
% DATA - T by 1 vector of data to be bootstrapped
% B - Number of bootstraps
% W - Average block length. P, the probability of starting a new block is defined P=1/W
%
% OUTPUTS:
% BSDATA - T by B matrix of bootstrapped data
% INDICES - T by B matrix of locations of the original BSDATA=DATA(indexes);
%
% COMMENTS:
% To generate bootstrap sequences for other uses, such as bootstrapping vector processes,
% set DATA to (1:N)'.
%
% See also block_bootstrap
% Author: Kevin Sheppard
% Revision: 2 Date: 12/31/2001
%%%%%%%%%%%%%%%%%%%%%%%%%
% Input Checking
%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin~=3
error('3 inputs required')
end
% Get the length of the data
[t,k]=size(data);
if k>1
error('DATA must be a column vector')
end
if t<2
error('DATA must have at least 2 observations.')
end
if ~isscalar(w) || w<=0
error('W must be a positive scalar.')
end
if ~isscalar(B) || B<1 || floor(B)~=B
error('B must be a positive scalar integer')
end
%%%%%%%%%%%%%%%%%%%%%%%%%
% Input Checking
%%%%%%%%%%%%%%%%%%%%%%%%%
% Define the probability of a new block
p=1/w;
% Set up the bsdata and indices
indices=zeros(t,B);
% Initial positions
indices(1,:)=ceil(t*rand(1,B));
% Set up the random numbers
select=rand(t,B)<p;
indices(select)=ceil(rand(1,sum(sum(select)))*t);
for i=2:t
% Determine whether we stay (rand>p) or move to a new starting value
% (rand<p)
indices(i,~select(i,:))=indices(i-1,~select(i,:))+1;
end
indices(indices>t) = indices(indices>t)-t;
% The indices make finding the bsdata simple
bsdata=data(indices);