- Define your custom data
- Prepare your data
- Set up your bootstrapping
how do i use the bootstrap method with a custom fit function?
9 views (last 30 days)
Show older comments
i am trying to globally fit 17 double Gaussian derivative functions and then use the bootstrap method to analyze the error in the parameter fits. looking at the examples for [bootstat,bootsam] = bootstrp(...), i can't figure out how to implement my function for the @corr that works for the example given online.
the function i'm using is somewhat complicated: y0+A*((f/g)-B)*(exp(-((B-f/g)^2)/(2*(Bhyp^2+(S^2)*(f/g)^2))))/(Bhyp^2+(S^2)*(f/g)^2)+A*((f/g)-B)*(exp(-((B-f/g)^2)/(2*(Bhyp1^2+(S1^2)*(f/g)^2))))/(Bhyp1^2+(S1^2)*(f/g)^2)
but i should be able to label it as something and use it in the bootstrap function correct?
0 Comments
Answers (1)
Aditya
on 25 Mar 2025
Hi Chad,
Yes, you can use your custom function within the bootstrp function by defining it as a function handle. The bootstrp function allows you to resample your data and apply a specified function to each bootstrap sample. Here's a step-by-step guide on how you can implement this for your double Gaussian derivative function:
Here is a sample code for the same:
% Example data (replace with your actual data)
data = randn(100, 17); % 100 samples of 17 functions
% Define your double Gaussian derivative function
doubleGaussDeriv = @(params, f, g) params(1) + ...
params(2) * ((f ./ g) - params(3)) .* ...
(exp(-(((params(3) - (f ./ g)).^2) ./ ...
(2 * (params(4)^2 + (params(5)^2) * (f ./ g).^2)))) ./ ...
(params(4)^2 + (params(5)^2) * (f ./ g).^2)) + ...
params(6) * ((f ./ g) - params(7)) .* ...
(exp(-(((params(7) - (f ./ g)).^2) ./ ...
(2 * (params(8)^2 + (params(9)^2) * (f ./ g).^2)))) ./ ...
(params(8)^2 + (params(9)^2) * (f ./ g).^2));
% Define a function to fit the model to data
fitModel = @(data) ...
% Here you would include the fitting routine, such as using lsqcurvefit
% or another fitting function to optimize the parameters for your model
% For example:
% params = [initial guesses for parameters];
% options = optimset('Display','off');
% fittedParams = lsqcurvefit(@(p, f) doubleGaussDeriv(p, f, g), params, f, data, [], [], options);
% Return the fitted parameters
% fittedParams;
% Number of bootstrap samples
nBootstraps = 1000;
% Apply bootstrap
[bootstat, bootsam] = bootstrp(nBootstraps, fitModel, data);
% Analyze the bootstrap results
% For example, compute the mean and standard deviation of the bootstrap estimates
meanParams = mean(bootstat);
stdParams = std(bootstat);
fprintf('Bootstrap mean of parameters:\n');
disp(meanParams);
fprintf('Bootstrap standard deviation of parameters:\n');
disp(stdParams);
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!