function [cumulants_mean, cumulants_ci, bootstrap_samples] = bootstrapCumulants(x, maxOrder, nBoot)
% bootstrapCumulants - compute bootstrap estimates of cumulants and confidence intervals
%
% Syntax:
%   [meanC, ciC, allC] = bootstrapCumulants(x, maxOrder, nBoot)
%
% Inputs:
%   x        - Data vector (may contain NaNs)
%   maxOrder - Max cumulant order (default: 6)
%   nBoot    - Number of bootstrap samples (default: 1000)
%
% Outputs:
%   cumulants_mean    - Mean of bootstrap cumulants
%   cumulants_ci      - 95% confidence intervals [2.5th; 97.5th] percentile
%   bootstrap_samples - All bootstrap cumulants (nBoot x maxOrder)

    if nargin < 2, maxOrder = 6; end
    if nargin < 3, nBoot = 1000; end
    
    x = x(:);
    x = x(~isnan(x)); % Remove NaNs
    
    if isempty(x)
        cumulants_mean = NaN(1, maxOrder);
        cumulants_ci = NaN(2, maxOrder);
        bootstrap_samples = NaN(nBoot, maxOrder);
        return;
    end
    
    N = numel(x);
    bootstrap_samples = zeros(nBoot, maxOrder);
    
    for b = 1:nBoot
        xb = x(randi(N, [N, 1])); % Resample with replacement
        bootstrap_samples(b, :) = computeCumulants(xb, maxOrder);
    end
    
    cumulants_mean = mean(bootstrap_samples, 1);
    cumulants_ci = prctile(bootstrap_samples, [2.5, 97.5]);

end
