Fastest (parallelized, maybe) way to run exponential fit function in a big matrix of data

6 ビュー (過去 30 日間)
Dear experts,
I am trying to ensure the most optimized syntax to run a part of my code. In summary, I need to perform an exponential fit of a series with 6 points, but ~2 million times (independent samples). In other words, I have a reshaped matrix 6x2,000,000.
This is my code right now (each loop iteration takes 0.02 seconds what makes this process prohibitive):
Xax = [30;60;90;120;150]; % The constant X series.
f = @(b,Xax) b(1).*exp(b(2).*Xax); % Exponential model
% LoopMap == my 6x2,000,000 matrix
for k = 1:size(LoopMap,2) %parfor? Any GPU model (if this is and optimal example)
bet = fminsearch(@(b) norm(LoopMap(:,k) - f(b,Xax)),[0;0]);
CoefAtmp(1,k) = abs(1/bet(2));
end
My PC is a standard machine (8th gen Core i7, 32Gb ram) with a not exceptional graphic card.
Thank you all in advance.
  1 件のコメント
Matt J
Matt J 2021 年 9 月 29 日
Do you have the Parallel Computing Toolbox? Is parfor an option?

サインインしてコメントする。

採用された回答

Matt J
Matt J 2021 年 9 月 29 日
編集済み: Matt J 2021 年 9 月 29 日
Additional speed-up should also be possible by reducing your problem to a 1-variable estimation and removing subsref operations from your objective function.
Xax = [30;60;90;120;150]; % The constant X series.
B2=nan(1,size(LoopMap,2));
Initial=Xa.^[0,1]\log(LoopMap);
Initial=Initial(2,:);
for k = 1:size(LoopMap,2) %parfor? Any GPU model (if this is and optimal example)
y=LoopMap(:,k);
B2(k) = fminsearch( @(b2) objective(b2,Xax,y) , Initial(k));
end
CoefAtmp = abs(1./B2);
function cost=objective(b2,Xax,y)
ex=exp(b2*Xa);
b1=ex\y;
cost=norm(b1*ex-y);
end
  1 件のコメント
Brunno Machado de Campos
Brunno Machado de Campos 2021 年 9 月 29 日
Thanks again, this solution is 200 times faster and just 7 times slower than the linear fit on the log transformed data, very reasonable!

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2021 年 9 月 29 日
編集済み: Matt J 2021 年 9 月 29 日
If it's acceptable to you, a log-linear least squares fit can be done very fast and without loops
A=Xax.^[0,1];
Bet=A\log(LoopMap);
CoefAtmp=1./abs(Bet(2,:));
  3 件のコメント
Matt J
Matt J 2021 年 9 月 29 日
編集済み: Matt J 2021 年 9 月 29 日
Both methods are exponential fits.
Do you mean you must have a linear least squares objective? If so, intiailizing fminsearch with the log-linear solution will probably speed things up.
However, you should first check that the linear and the loglinear methods give significantly different results over a small but representative subset of your LoopMap(:,k). If not, you can use that to defend the loglinear method to the academic community.
Brunno Machado de Campos
Brunno Machado de Campos 2021 年 9 月 29 日
Right! Sure, thank you. Since a simple exponential fits well the model, both option will give very similar answeres.
So (just to documment):
For an example were sig = [527.32;398.95;284.22;197.562;155.01];
1)
Xax = [30;60;90;120;150]; % The constant X series.
f = @(b,Xax) b(1).*exp(b(2).*Xax);
bet = fminsearch(@(b) norm(sig - f(b,Xax)),[0;0]);
CoefAtmp1 = abs(1/bet(2));
0.02 seconds
2)
Xax = [30;60;90;120;150]; % The constant X series.
M = bsxfun(@power,Xax,0:1);
c2 = M\log(sig);
CoefAtmp2 = abs(1./c2(2));
0.000002 seconds
Nothing more to do here, thanks again!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeParallel Computing Fundamentals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by