1function [ts_boot, cfl, cfu] = bootts(ts, B, alpha, med)
2% Calculates non-parametric t-percentile bootstrap statistics.
3%
4% Calculates non-parametric t-percentile bootstrap statistics of a given time
5% series.
6%
7% Input parameters:
8%   ts    ... Time series (trials x epochs)
9%   B     ... Number of resamplings (default: 300)
10%   alpha ... Alpha significance of confidence intervals (default: [0.1 0.05 0.01])
11%   med   ... 0: mean, 1: median (default: 0)
12%
13% Output parameters:
14%   ts_boot ... Bootstrapped time series
15%   cfl     ... Lower limit of confidence interval
16%   cfu     ... Upper limit of confidence interval
17
18% Copyright by Bernhard Graimann, modified by Clemens Brunner
19% $Revision: 0.1 $ $Date: 10/25/2006 12:13:07 $
20% E-Mail: clemens.brunner@tugraz.at
21
22% This program is free software; you can redistribute it and/or modify it
23% under the terms of the GNU General Public License as published by the
24% Free Software Foundation; either version 2 of the License, or (at your
25% option) any later version.
26%
27% This program is distributed in the hope that it will be useful, but
28% WITHOUT ANY WARRANTY; without even the implied warranty of
29% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
30% Public License for more details.
31%
32% You should have received a copy of the GNU General Public License along
33% with this program; if not, write to the Free Software Foundation, Inc.,
34% 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
35
36
37if nargin < 1, help bootts; return; end;
38
39if nargin < 2, B = 300; end;
40if nargin < 3, alpha = [0.1 0.05 0.01]; end;
41if nargin < 4, med = 0; end;
42
43
44%==============================================================================
45% Initialization
46%==============================================================================
47
48[trials,tpts] = size(ts);    % trials and number of samples (time points)
49
50nbootlevels = length(alpha);
51
52cfl = zeros(nbootlevels,tpts);
53cfu = zeros(nbootlevels,tpts);
54
55ts_boot = zeros(1,tpts);     % bootstrapped time series
56midx = fix(B/2);             % index of median value
57
58ts_mean = mean(ts);          % mean over all trials
59ts_std = std(ts);            % standard deviation over all trials
60
61
62%==============================================================================
63% Calculate percentiles
64%==============================================================================
65
66for k=1:nbootlevels
67  q1(k)=floor(B*alpha(k)/2);
68  if q1(k)<1
69    q1(k)=1;
70    B=ceil(2/alpha(k));
71    fprintf('BOOTTS: At least %d bootstrap resamplings are necessary for alpha=%.3f\n',B,alpha(k));
72  end;
73  q2(k)=B-q1(k)+1;
74end;
75
76
77%==============================================================================
78% Bootstrap each single sample
79%==============================================================================
80
81for j=1:tpts
82
83  % do the resampling
84  idx = ceil(trials*rand(trials,B));
85  strials = ts(:,j);
86  resamples = strials(idx);               % draw B resamples  (trials x boostraps)
87  resamples_stdest = std(resamples);      % standard deviation of resamples (1 x bootstraps)
88
89  % average the resampled values
90  if med == 0, resamples = mean(resamples); else, resamples = median(resamples); end;
91
92  bootstat = sort((resamples-ts_mean(j))./resamples_stdest);
93  ts_boot(j) = bootstat(midx)*resamples_stdest(midx)+ts_mean(j);
94
95  % for each bootstrap level, i.e. percentile, according to alpha, calc. conf. limits
96  for kk = 1:nbootlevels
97    cfl(kk,j) = ts_mean(j)-bootstat(q2(kk))*ts_std(j);
98    cfu(kk,j) = ts_mean(j)-bootstat(q1(kk))*ts_std(j);
99  end;
100
101end;
102