Help: Speed up for loop with two functions

1 visualización (últimos 30 días)
Kai Koslowsky
Kai Koslowsky el 12 de Oct. de 2021
Comentada: Kai Koslowsky el 3 de Nov. de 2021
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 comentarios
Kai Koslowsky
Kai Koslowsky el 15 de Oct. de 2021
Editada: Kai Koslowsky el 15 de Oct. de 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 el 3 de Nov. de 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!

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by