usingNeuroAnalyzerimportCairoMakie# Quarto sets a default figure size (672 × 480 px), which overrides the sizes of# NeuroAnalyzer figures: remove itdelete!(CairoMakie.Makie.CURRENT_DEFAULT_THEME, :resolution)usingStatisticsNeuroAnalyzer.progress_bar =false# visual ERP recording: 100 epochs time-locked to the stimulus s71eeg =import_recording("files/eeg/s1_faces.vhdr")NeuroAnalyzer.filter!(eeg; ch ="eeg", fprototype =:butterworth, ftype =:hp, cutoff =0.1, order =4)NeuroAnalyzer.filter!(eeg; ch ="eeg", fprototype =:butterworth, ftype =:lp, cutoff =30, order =8)reference_avg!(eeg)e71 =epoch(eeg; marker ="s71", offset =0.5, ep_len =2.5)# baseline correction of each epoch (-200-0 ms)bl = (vsearch(-0.2, e71.epoch_time), vsearch(0, e71.epoch_time))remove_dc!(e71; ch ="eeg", n = bl)
Bootstrapping is a resampling method used to estimate the sampling distribution of a statistic by repeatedly sampling with replacement from the observed data. It is widely used in statistical inference, machine learning, and signal processing (e.g., EEG analysis) to:
Estimate confidence intervals,
Assess uncertainty in estimators,
Test hypotheses,
Validate models,
Handle small or non-normal datasets.
Bootstrapping is a non-parametric method that uses resampling to approximate the sampling distribution of a statistic (e.g., mean, median, correlation). It is particularly useful when:
The theoretical distribution of the statistic is unknown,
The sample size is small,
The data is non-normal,
Parametric assumptions (e.g., normality) are violated.
Key Ideas
Instead of relying on theoretical distributions (e.g., normal distribution for means), bootstrapping uses the data itself to estimate uncertainty by:
Resampling the data with replacement,
Computing the statistic for each resample,
Repeating this process many times (e.g., 1000–10,000 iterations),
Using the distribution of bootstrap statistics to estimate confidence intervals, bias, or p-values.
Confidence Interval of an ERP
The bootstrap confidence interval of the mean of the epochs (the ERP):
draw n2 epochs at random with replacement and average them,
repeat n1 times (e.g. 3000), giving n1 bootstrap ERPs,
at each time point, the 2.5th and 97.5th percentiles of the bootstrap ERPs are the bounds of the 95% confidence interval.
n2 must be equal to the number of epochs (the default): drawing more epochs than were recorded would make the interval too narrow.
bootstrap_ci() takes a matrix of shape (samples, epochs); here channel O2 of all 100 epochs:
The result contains the bootstrap grand mean (gm) and the lower (ll) and upper (ul) bounds:
plot_ci(bs.gm, bs.ll, bs.ul, e71.epoch_time; xlabel ="Time [s]", ylabel ="Amplitude [μV]", title ="ERP of O2 with 95% bootstrap CI")
The confidence interval excludes 0 from about 180 to 550 ms (positive deflection) and during most of the slow negative wave after 600 ms: these responses to the stimulus are reliable. Note that the interval is calculated separately at each time point, so for a significance test over many time points a correction for multiple comparisons is needed.
Bootstrap Distribution of a Statistic
bootstrap_stat() calculates the bootstrap distribution of any statistic of the ERP: at each of the n1 iterations, n2 epochs are drawn with replacement and averaged, and the statistic f is calculated from the average. f is a function of the averaged signal (or a string with a Julia expression, in which the signal is obj).
The amplitude of the positive peak of the ERP between 150 and 350 ms:
w =vsearch(0.15, e71.epoch_time):vsearch(0.35, e71.epoch_time)peak_amp(x) =maximum(x[w])erp =vec(mean(s; dims =2))s_stat =peak_amp(erp)s_dist =bootstrap_stat(s; f = peak_amp, n1 =3000)
The same using a string expression:
s_dist =bootstrap_stat(s; f ="maximum(obj[w])", n1 =3000)
The 95% confidence interval of the peak amplitude is given by the percentiles of the bootstrap distribution:
The bootstrap distribution with the peak amplitude of the observed ERP:
plot_histogram(s_dist, s_stat; draw_median =false, draw_mean =true, xlabel ="Peak amplitude [μV]", title ="Bootstrap distribution of the peak amplitude")
The mean of the bootstrap distribution is slightly higher than the observed peak amplitude: the maximum of a noisy average is biased upwards. The difference between the mean of the bootstrap distribution and the observed value estimates this bias.