Help: Speed up for loop with two functions

3 次查看(过去 30 天)
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 个评论
Kai Koslowsky
Kai Koslowsky 2021-10-15
编辑:Kai Koslowsky 2021-10-15
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 2021-11-3
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!

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by