Speed up (vectorized?) bootci to get estimates by groups

6 visualizaciones (últimos 30 días)
Ricardo Henriquez
Ricardo Henriquez el 11 de Mayo de 2023
Respondida: Ashutosh el 24 de Ag. de 2023
Hello,
I have a bootstrapped sample of investment strategy with n (weights), m(#bootstraps), and p(duration of investment). I want to calculate the sharpe ratio and the confidence interval for each weight and duration.
Since I have several investment strategies I would like to run this code several times.
How can I speed up or vectorize the following code that calculates Sharpe ratios and confidence intervals using bootstrapping? I have tried to split the matrix, and parallel computing but it still takes a long time to run for my dataset. Any suggestions would be appreciated.
% set matrix dimensions
n = 60;
m = 1000000;
p = 3;
% create random 3D matrix (Bootstrapped Sample)
A = rand(n, m, p);
l_ci = 0.05; % set the alpha level for the confidence interval
nboot = 100; % set the number of bootstrap samples
% Preallocate matrices to store Sharpe ratios and confidence intervals
sharpe_ratios1 = zeros(n, p);
cis1u = zeros(n, p);
cis1l = zeros(n, p);
% Sharpe ratio bootfun
sr = @(x) mean(x) ./ std(x);
tic
for i = 1:n % Loop over the weights
for j = 1:p % Loop over the horizons
% Get the boostrap returns for the current weight and horizon
yhat1 = A(i, :, j);%
% Calculate the Sharpe ratio using bootstrapping
[ci_sr1, sr1] = bootci(nboot, {sr,yhat1'}, 'alpha', l_ci, 'Options',statset('UseParallel',true));
% Store the Sharpe ratio and confidence interval
sharpe_ratios1(i,j) = mean(sr1);
cis1u(i,j,:) = ci_sr1(2);
cis1l(i,j,:) = ci_sr1(1);
end
end
toc

Respuestas (1)

Ashutosh
Ashutosh el 24 de Ag. de 2023
These steps can be followed to improve the performance of "bootci" as follows:
  • Calculate the inputs before passing it to the "bootci" function so that the computation time is saved and store these inputs in different cell array.
  • Try to use "parfor" operations of Parallel Computing Toolbox to parallelize the code and can also use "parfeval" to run computations in background.
  • If there is access to GPU, then try to use "spmd" in the parallel pool to execute code parallelly in different thread workers.
The following links would help in understanding usage of above operations:

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by