How to apply multivariate bootstrapping in Matlab?

3 vues (au cours des 30 derniers jours)
Ebru Angun
Ebru Angun le 26 Nov 2024
Réponse apportée : Abhas le 26 Nov 2024
I have two output matrices W0 and W1 which have the same number of rows and columns.
The rows of these matrices need to be bootstrapped simultaneously, and the means and the standard deviations
are to be computed. Currently, I am obtaining a bootstrap sample for W_0 for each row of W_0, say, row = 1:
[stat, sample] = bootstrp(1,@(x)[mean(x) std(x) ], W_0(1,:));
Then, using the indices in sample, I am computing the mean and the standard deviation of the bootstrap sample of W_1.
Is it possible to do these two steps in one step in Matlab?

Réponse acceptée

Abhas
Abhas le 26 Nov 2024
To perform multivariate bootstrapping for two output matrices "W0"​ and "W1"​ simultaneously in MATLAB, you can use the "bootstrp" function: https://www.mathworks.com/help/stats/bootstrp.html with a custom function that operates on both matrices at once. The goal is to ensure that the same bootstrap resampling indices are used for corresponding rows of "W0"​ and "W1"​ and to compute the means and standard deviations for both matrices in one step.
Here's an example code to achieve the same:
% Example matrices W_0 and W_1
W_0 = rand(5, 10);
W_1 = rand(5, 10);
% Number of bootstrap samples
nBoot = 1000;
% Define a custom function for multivariate bootstrapping
bootstrapFunction = @(rows) computeStats(W_0(rows, :), W_1(rows, :));
% Use bootstrp to perform the bootstrap
bootstrapResults = bootstrp(nBoot, bootstrapFunction, 1:size(W_0, 1));
meanW0 = bootstrapResults(:, 1);
stdW0 = bootstrapResults(:, 2);
meanW1 = bootstrapResults(:, 3);
stdW1 = bootstrapResults(:, 4);
disp('Bootstrap means and std devs for W_0 and W_1:');
Bootstrap means and std devs for W_0 and W_1:
disp('W_0 - Mean:');
W_0 - Mean:
disp(mean(meanW0));
0.5107
disp('W_0 - Std:');
W_0 - Std:
disp(mean(stdW0));
0.2516
disp('W_1 - Mean:');
W_1 - Mean:
disp(mean(meanW1));
0.5567
disp('W_1 - Std:');
W_1 - Std:
disp(mean(stdW1));
0.2499
% Function to compute statistics
function stats = computeStats(W0, W1)
meanW0 = mean(W0, 2);
stdW0 = std(W0, 0, 2);
meanW1 = mean(W1, 2);
stdW1 = std(W1, 0, 2);
stats = [mean(meanW0) mean(stdW0) mean(meanW1) mean(stdW1)];
end
I hope this helps!

Plus de réponses (0)

Produits


Version

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by