source: trunk/STATISTICS/a2_correlate.pro @ 2

Last change on this file since 2 was 2, checked in by opalod, 22 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1; $Id$
2; Copyright (c) 1995-1997, Research Systems, Inc.  All rights reserved.
3;       Unauthorized reproduction prohibited.
4;+
5; NAME:
6;       a2_correlate
7;
8; PURPOSE:
9;       This function computes the autocorrelation Px(L) or autocovariance
10;       Rx(L) of a sample population X as a function of the lag (L).
11;
12; CATEGORY:
13;       Statistics.
14;
15; CALLING SEQUENCE:
16;       Result = A_correlate(X, Lag)
17;
18; INPUTS:
19;       X:    An n-element vector of type integer, float or double.
20;
21;     LAG:    A scalar or n-element vector, in the interval [-(n-2), (n-2)],
22;             of type integer that specifies the absolute distance(s) between
23;             indexed elements of X.
24;
25; KEYWORD PARAMETERS:
26;       COVARIANCE:    If set to a non-zero value, the sample autocovariance
27;                      is computed.
28;
29;       DOUBLE:        If set to a non-zero value, computations are done in
30;                      double precision arithmetic.
31;
32; EXAMPLE
33;       Define an n-element sample population.
34;         x = [3.73, 3.67, 3.77, 3.83, 4.67, 5.87, 6.70, 6.97, 6.40, 5.57]
35;
36;       Compute the autocorrelation of X for LAG = -3, 0, 1, 3, 4, 8
37;         lag = [-3, 0, 1, 3, 4, 8]
38;         result = a_correlate(x, lag)
39;
40;       The result should be:
41;         [0.0146185, 1.00000, 0.810879, 0.0146185, -0.325279, -0.151684]
42;
43; PROCEDURE:
44;       See computational formula published in IDL manual.
45;
46; REFERENCE:
47;       INTRODUCTION TO STATISTICAL TIME SERIES
48;       Wayne A. Fuller
49;       ISBN 0-471-28715-6
50;
51; MODIFICATION HISTORY:
52;       Written by:  GGS, RSI, October 1994
53;       Modified:    GGS, RSI, August 1995
54;                    Corrected a condition which excluded the last term of the
55;                    time-series.
56;       Modified:    GGS, RSI, April 1996
57;                    Simplified AUTO_COV function. Added DOUBLE keyword.
58;                    Modified keyword checking and use of double precision.
59;       Modified:    W. Biagiotti,  Advanced Testing Technologies Inc., Hauppauge, NY, July 1997
60;                    Moved all constant calculations out of main loop for greatly
61;                    reduced processing time.
62;
63; DISCLAIMER:  This routine has been modified from its original form as it was
64;              supplied by Research Systems, Inc (RSI).  As such, RSI is not responsible
65;              for any errors existing in this code.
66;-
67
68FUNCTION Auto_Cov, X, M, nX, Double = Double
69
70COMMON data, Xmean
71
72    ;Sample autocovariance function.
73  RETURN, TOTAL((X[0:nX - M] - Xmean) * (X[M:nX] - Xmean), Double = Double)
74
75END
76
77FUNCTION a2_correlate, X, Lag, Covariance = Covariance, Double = Double
78
79COMMON data, Xmean
80
81  ;Compute the sample-autocorrelation or autocovariance of (Xt, Xt+l)
82  ;as a function of the lag (l).
83
84  ON_ERROR, 2
85
86  TypeX = SIZE(X)
87  nX = TypeX[TypeX[0]+2]
88
89  ;Check length.
90  if nX lt 2 then $
91    MESSAGE, "X array must contain 2 or more elements."
92
93  ;If the DOUBLE keyword is not set then the internal precision and
94  ;result are identical to the type of input.
95  if N_ELEMENTS(Double) eq 0 then $
96    Double = (TypeX[TypeX[0]+1] eq 5)
97
98  nLag = N_ELEMENTS(Lag)
99
100  if nLag eq 1 then Lag = [Lag] ;Create a 1-element vector.
101
102  if Double eq 0 then Auto = FLTARR(nLag) else Auto = DBLARR(nLag)
103
104  ; Calculate constants OUTSIDE of main loop
105  Xmean = TOTAL(X, Double = Double) / nX
106  nX = nX - 1         ; Translate into last index (avoid redundancy)
107  last_idx = nLag - 1 ; Last loop indice
108  Lag = ABS(Lag)      ; Calculate with vector ops
109
110  if KEYWORD_SET(Covariance) eq 0 then begin ;Compute Autocorrelation.
111
112    for k = 0, last_idx do $
113      Auto[k] = Auto_Cov(X, Lag[k], nX, Double = Double)
114
115    temp_Corr = Auto_Cov(X, 0L, nX, Double = Double)
116    Auto = Auto / temp_Corr
117
118  endif else begin ;Compute Autocovariance.
119    for k = 0, last_idx do $
120      Auto[k] = Auto_Cov(X, Lag[k], nX, Double = Double)
121
122    Auto = Auto / nX
123  endelse
124
125  if Double eq 0 then RETURN, FLOAT(Auto) else $
126  RETURN, Auto
127
128END
Note: See TracBrowser for help on using the repository browser.