Help: Speed up for loop with two functions

3 views (last 30 days)
Hello everyone, i am currently running this code. But unfortunately it takes for ever to finsish. A 12x10 matrix took nearly 30 minutes and i need one for 1415596x10. Is there any way i can speed up the process? Is there something that i am missing?
My goal is to optimize my estimation parameters of fun with a particle swarm algorithm. For each day in my sample (6009 days), i have multiple data. I want to minimize the error between observed data (implied_volatility) and my estimation data (via fun) with my 10 parameters.
A = [log_moneyness maturity]; xdata = A; ydata = implied_volatility;
fun = @(x,xdata) (x(1)+x(2).*xdata(:,2)) + (x(3)+x(4).*xdata(:,2)).*((x(5)+x(6).*xdata(:,2)).*...
(xdata(:,1) -(x(7)+x(8).*xdata(:,2))) + sqrt((xdata(:,1) - (x(7)+x(8).*xdata(:,2)).^2 ...
+ (x(9)+x(10).*xdata(:,2)).^2))); %original function
parameters_forall = table2array(T_param (:,2:11)); %1415596x10 matrix
%implied_volatility is a 1415596x1 matrix with original values from market
%now i want to minimize the error of my estimate by:
tDays = size(parameters_forall(:,1));
%%
for i = 1:tDays
x = parameters_forall(i,:);
residue = @(x) (sum(fun(x,xdata)-implied_volatility(i)).^2);
y = particleswarm(residue, 10);
swarm_parameters(i,:) = y;
end
Grateful for any help, all the best to you!
  11 Comments
Kai Koslowsky
Kai Koslowsky on 15 Oct 2021
Edited: Kai Koslowsky on 15 Oct 2021
So do i understand you correctly, that the resulting parameters do not change with or without divisor and sqrt? I thought, that when particleswarm is trying to minimize the residue, it will minimize the result via the best parameters and not only the value of the function, so that it fits to ydata. How can i make sure, that my given formula is regarded in each point (sqrt and divisor)?
Edit
For the divisor i agree. I am rather lost on how to make sure, that my parameters are minimized by the function i sent (see Comment above).
Kai Koslowsky
Kai Koslowsky on 3 Nov 2021
Dear Mr. Robinson,
would you like to post something as a answer below, so i can accept your answer? This is the least that i can do. I am truely grateful for your help. I have looked over all your comments many times, and it helped me a lot.
All the best to you!

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!