大工至善|大学至真分享 http://blog.sciencenet.cn/u/lcj2212916

博文

[转载]【源码】自相关函数Autocorrelation Function (ACF)的计算

已有 3683 次阅读 2019-2-9 11:21 |系统分类:科研笔记| Function |文章来源:转载



本代码计算给定序列的自相关函数ACF。

Computes ACF for a given series. 


根据滞后量p返回自相关输出向量。

Returns a vector of autocorrelations through lag p. 


还可以生成自相关的条形图,其中拒绝域用于测试(在白噪声假设下)每个自相关等于0。

Also produces bar graph of autocorrelations, with rejection region bands for testing (under white noise assumption) each autocorrelation = 0.


源代码:

function ta = acf(y,p)

% ACF - Compute Autocorrelations Through p Lags

% >> myacf = acf(y,p)

%

% Inputs:

% y - series to compute acf for, nx1 column vector

% p - total number of lags, 1x1 integer

%

% Output:

% myacf - px1 vector containing autocorrelations

%        (First lag computed is lag 1. Lag 0 not computed)

%

%

% A bar graph of the autocorrelations is also produced, with

% rejection region bands for testing individual autocorrelations = 0.

%

% Note that lag 0 autocorelation is not computed,

% and is not shown on this graph.

%

% Example:

% >> acf(randn(100,1), 10)

%



% --------------------------

% USER INPUT CHECKS

% --------------------------


[n1, n2] = size(y) ;

if n2 ~=1

   error('Input series y must be an nx1 column vector')

end


[a1, a2] = size(p) ;

if ~((a1==1 & a2==1) & (p<n1))

   error('Input number of lags p must be a 1x1 scalar, and must be less than length of series y')

end




% -------------

% BEGIN CODE

% -------------


ta = zeros(p,1) ;

global N

N = max(size(y)) ;

global ybar

ybar = mean(y);


% Collect ACFs at each lag i

for i = 1:p

  ta(i) = acf_k(y,i) ;

end


% Plot ACF

% Plot rejection region lines for test of individual autocorrelations

% H_0: rho(tau) = 0 at alpha=.05

bar(ta)

line([0 p+.5], (1.96)*(1/sqrt(N))*ones(1,2))

line([0 p+.5], (-1.96)*(1/sqrt(N))*ones(1,2))


% Some figure properties

line_hi = (1.96)*(1/sqrt(N))+.05;

line_lo = -(1.96)*(1/sqrt(N))-.05;

bar_hi = max(ta)+.05 ;

bar_lo = -max(ta)-.05 ;


if (abs(line_hi) > abs(bar_hi)) % if rejection lines might not appear on graph

   axis([0 p+.60 line_lo line_hi])

else

   axis([0 p+.60 bar_lo bar_hi])

end

title({' ','Sample Autocorrelations',' '})

xlabel('Lag Length')

set(gca,'YTick',[-1:.20:1])

% set number of lag labels shown

if (p<28 & p>4)

   set(gca,'XTick',floor(linspace(1,p,4)))

elseif (p>=28)

   set(gca,'XTick',floor(linspace(1,p,8)))

end

set(gca,'TickLength',[0 0])





% ---------------

% SUB FUNCTION

% ---------------

function ta2 = acf_k(y,k)

% ACF_K - Autocorrelation at Lag k

% acf(y,k)

%

% Inputs:

% y - series to compute acf for

% k - which lag to compute acf

%

global ybar

global N

cross_sum = zeros(N-k,1) ;


% Numerator, unscaled covariance

for i = (k+1):N

   cross_sum(i) = (y(i)-ybar)*(y(i-k)-ybar) ;

end


% Denominator, unscaled variance

yvar = (y-ybar)'*(y-ybar) ;


ta2 = sum(cross_sum) / yvar ;



源码下载地址:

http://page2.dfpan.com/fs/8lcje221e291c679df8/  


更多精彩文章请关注微信号:qrcode_for_gh_60b944f6c215_258.jpg




https://blog.sciencenet.cn/blog-69686-1161333.html

上一篇:[转载]【机器学习开放项目】降水数据分析
下一篇:[转载]【新书推荐】【2018.11】股票会说话(股学里分享)
收藏 IP: 114.102.186.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-26 17:27

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部