【问题标题】:optimize code so that it run quickly优化代码,使其快速运行
【发布时间】:2017-01-21 10:51:15
【问题描述】:

假设我们有以下代码,使用 BIC 标准选择最佳 ARIMA 模型

function [AR_order,MA_order]= complete_arima(Y)
%% if not  specific input from user, use default time series
if nargin<1  
    Y=xlsread('ARIMA','ARIMA','d6:d468');
end
% Y is  the observed time series
%% graphical  representation of time series
subplot(3,1,1);
plot(Y);
title('original time series');
%%autocorrelation/partial autocorrelation of  given signal
subplot(3,1,2);
autocorr(Y);
subplot(3,1,3);
parcorr(Y);
hold off
%% entering necessary parameters for Arima Testing
d=input('enter necessary order of differencing       :  ');
M=input('maximum order of lag for ARMA simulation    :  ');
%% simulate ARIMA model on the base of given input
LOGL = zeros(M,M); %Initialize
PQ = zeros(M,M);
for p = 1:M
    for q = 1:M
        mod = arima(p,d,q);% for each pair generate  new ARIMA model
        [~,~,logL] = estimate(mod,Y,'print',false);
        LOGL(p,q) = logL;
        PQ(p,q) = p+q;
     end
end
%% calculate BIC 
LOGL = reshape(LOGL,M*M,1);
PQ = reshape(PQ,M*M,1);
[~,bic] = aicbic(LOGL,PQ+1,100);
bic=reshape(bic,M,M);
%% determine order of ARIMA  by finding minimum element from matrix  bic and  corresponding indeces
[AR_order,MA_order]=find(bic==min(min(bic)));
end

它的执行时间是

>> tic
>> [AR_order,MA_order]=complete_arima();
enter necessary order of differencing       :  1
maximum order of lag for ARMA simulation    :  4
>> toc
Elapsed time is 36.499133 seconds.

如何加快给定代码的速度?我应该使用 parfor 运行一个循环吗?

首先我创建了并行池

parpool('local',4)
Starting parallel pool (parpool) using the 'local' profile ... connected to 4 workers.

ans = 

 Pool with properties: 

            Connected: true
           NumWorkers: 4
              Cluster: local
        AttachedFiles: {}
          IdleTimeout: 30 minute(s) (30 minutes remaining)
          SpmdEnabled: true
>> [AR_order,MA_order]=complete_arima();
enter necessary order of differencing       :  1
maximum order of lag for ARMA simulation    :  4
>> toc
Elapsed time is 24.983676 seconds.

对于并行执行,我使用了以下代码

function [AR_order,MA_order]= complete_arima(Y)
%% if not  specific input from user, use default time series
if nargin<1  
    Y=xlsread('ARIMA','ARIMA','d6:d468');
end
% Y is  the observed time series
%% graphical  representation of time series
subplot(3,1,1);
plot(Y);
title('original time series');
%%autocorrelation/partial autocorrelation of  given signal
subplot(3,1,2);
autocorr(Y);
subplot(3,1,3);
parcorr(Y);
hold off
%% entering necessary parameters for Arima Testing
d=input('enter necessary order of differencing       :  ');
M=input('maximum order of lag for ARMA simulation    :  ');
%% simulate ARIMA model on the base of given input
LOGL = zeros(M,M); %Initialize
PQ = zeros(M,M);
parfor p = 1:M
    for q = 1:M
        mod = arima(p,d,q);% for each pair generate  new ARIMA model
        [~,~,logL] = estimate(mod,Y,'print',false);
        LOGL(p,q) = logL;
        PQ(p,q) = p+q;
     end
end
%% calculate BIC 
LOGL = reshape(LOGL,M*M,1);
PQ = reshape(PQ,M*M,1);
[~,bic] = aicbic(LOGL,PQ+1,100);
bic=reshape(bic,M,M);
%% determine order of ARIMA  by finding minimum element from matrix  bic and  corresponding indeces
[AR_order,MA_order]=find(bic==min(min(bic)));
end

我怎样才能加快速度?代码中是否有任何行可以矢量化?提前谢谢

【问题讨论】:

    标签: algorithm matlab performance parallel-processing


    【解决方案1】:

    在这里我将发布另一个持续 120 秒的解决方案

    function [AR_order,MA_order]= complete_arima(Y)
    %%  created parallel loop
    parpool('local',4);
    tic;
    %% if not  specific input from user, use default time series
    
    if nargin<1  
        Y=xlsread('ARIMA','ARIMA','d6:d468');
    end
    % Y is  the observed time series
    %% graphical  representation of time series
    subplot(3,1,1);
    plot(Y);
    title('original time series');
    %%autocorrelation/partial autocorrelation of  given signal
    subplot(3,1,2);
    autocorr(Y);
    subplot(3,1,3);
    parcorr(Y);
    hold off
    %% entering necessary parameters for Arima Testing
    d=input('enter necessary order of differencing       :  ');
    M=input('maximum order of lag for ARMA simulation    :  ');
    %% simulate ARIMA model on the base of given input
    LOGL = zeros(M,M); %Initialize
    PQ = zeros(M,M);
    parfor p = 1:M
        for q = 1:M
            mod = arima(p,d,q);% for each pair generate  new ARIMA model
            [~,~,logL] = estimate(mod,Y,'print',false);
            LOGL(p,q) = logL;
            PQ(p,q) = p+q;
         end
    end
    %% calculate BIC 
    LOGL = reshape(LOGL,M*M,1);
    PQ = reshape(PQ,M*M,1);
    [~,bic] = aicbic(LOGL,PQ+1,100);
    bic=reshape(bic,M,M);
    %% determine order of ARIMA  by finding minimum element from matrix  bic and  corresponding indeces
    [AR_order,MA_order]=find(bic==min(min(bic)));
    toc
    delete(gcp('nocreate'));
    end
    
    >> [AR_order,MA_order]=complete_arima();
    Starting parallel pool (parpool) using the 'local' profile ... connected to 4 workers.
    enter necessary order of differencing       :  1
    maximum order of lag for ARMA simulation    :  10
    Elapsed time is 120.370494 seconds.
    Parallel pool using the 'local' profile is shutting down.
    >> 
    

    我该如何解决这个问题?

    【讨论】:

      猜你喜欢
      • 2020-01-13
      • 1970-01-01
      • 2020-05-29
      • 2021-08-28
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      相关资源
      最近更新 更多