autoco Subroutine

public subroutine autoco(x, n, iwrite, xautoc)

NAME

autoco(3f) - [M_datapac:STATISTICS] compute the sample autocorrelation
             coefficient

SYNOPSIS

 SUBROUTINE AUTOCO(X,N,Iwrite,Xautoc)

  Real(kind=wp), Intent (InOut) ::  X(:)
  Integer,       Intent (In)    ::  Iwrite
  Real(kind=wp), Intent (In)    ::  Xautoc

DESCRIPTION

AUTOCO(3f) computes the sample autocorrelation coefficient of the
data in the input vector X. The sample autocorrelation coefficient
equals the correlation between X(I) and X(I+1) over the entire sample.
The autocorrelation coefficient coefficient will be a REAL
value between -1.0 and 1.0 (inclusively).

INPUT ARGUMENTS

X        The REAL vector of (unsorted) observations.
N        The integer number of observations in the vector x.
IWRITE   An integer flag code which (if set to 0) will suppress
         the printing of the sample autocorrelation coefficient
         as it is computed; or (if set to some integer value not
         equal to 0), like, say, 1) will cause the printing of the
         sample autocorrelation coefficient at the time it is computed.

OUTPUT ARGUMENTS

XAUTOC   The REAL value of the computed sample autocorrelation coefficient.
         This REAL value will be between -1.0 and 1.0 (inclusively).

EXAMPLES

Sample program:

program demo_autoco
use M_datapac, only : autoco, label
implicit none
real ::  x(100)
call label('autoco')
!call   call autoco(x,size(x),1,xautoc)
end program demo_autoco

Results:

REFERENCES

Jenkins and Watts, Spectral Analysis and its Applications, 1968, pages 5, 182.

AUTHOR

The original DATAPAC library was written by James J. Filliben of the Statistical
Engineering Division, National Institute of Standards and Technology.

MAINTAINER

John Urban, 2022.05.31

LICENSE

CC0-1.0

Arguments

Type IntentOptional Attributes Name
real(kind=wp) :: x(:)
integer :: n
integer :: iwrite
real(kind=wp) :: xautoc

Source Code

subroutine autoco(x,n,iwrite,xautoc)
real(kind=wp) :: an, hold , sum1 , sum2 , sum3 , x(:) , xautoc , xbar , xbar1 , xbar2
integer i , ip1 , iwrite , n , nm1

!     CHECK THE INPUT ARGUMENTS FOR ERRORS
      an = n
      if ( n<1 ) then
         write (G_IO,99001)
         99001    format (' ', '***** FATAL ERROR--THE SECOND INPUT ARGUMENT TO THE AUTOCO SUBROUTINE IS NON-POSITIVE *****')
         write (G_IO,99002) n
         99002    format (' ','***** THE VALUE OF THE ARGUMENT IS ',i0,' *****')
         return
      else
         if ( n==1 ) then
            write (G_IO,99003)
            99003 format (' ***** NON-FATAL DIAGNOSTIC--THE SECOND INPUT ARGUMENT TO THE AUTOCO SUBROUTINE HAS THE VALUE 1 *****')
            xautoc = 0.0_wp
         else
            hold = x(1)
            do i = 2 , n
               if ( x(i)/=hold ) goto 50
            enddo
            write (G_IO,99004) hold
99004       format (' ',&
             & '***** NON-FATAL DIAGNOSTIC--THE FIRST  INPUT ARGUMENT (A VECTOR) TO THE AUTOCO SUBROUTINE HAS ALL ELEMENTS = ', &
             & e15.8, &
             & ' *****')
            xautoc = 0.0_wp
         endif
         goto 100
!
!-----START POINT-----------------------------------------------------
!
 50      continue
         xbar = 0.0_wp
         do i = 1 , n
            xbar = xbar + x(i)
         enddo
         xbar1 = xbar - x(n)
         xbar1 = xbar1/(an-1.0_wp)
         xbar2 = xbar - x(1)
         xbar2 = xbar2/(an-1.0_wp)
         sum1 = 0.0_wp
         sum2 = 0.0_wp
         sum3 = 0.0_wp
         nm1 = n - 1
         do i = 1 , nm1
            ip1 = i + 1
            sum1 = sum1 + (x(i)-xbar1)*(x(ip1)-xbar2)
            sum2 = sum2 + (x(i)-xbar1)**2
            sum3 = sum3 + (x(ip1)-xbar2)**2
         enddo
         xautoc = sum1/(sqrt(sum2*sum3))
      endif
!
 100  continue
      if ( iwrite==0 ) return
      write (G_IO,99005)
      99005 format (' ')
      write (G_IO,99006) n , xautoc
      99006 format (' ', 'THE LINEAR AUTOCORRELATION COEFFICIENT OF THE SET OF ',i0,' OBSERVATIONS IS ',f14.6)
end subroutine autoco