0.6.2-dev0
FORCES
FORtran lib for Comp. Env. Sys.
Loading...
Searching...
No Matches
mo_corr.f90
Go to the documentation of this file.
1!> \file mo_corr.f90
2!> \brief \copybrief mo_corr
3!> \details \copydetails mo_corr
4
5!> \brief Provides autocorrelation function calculations.
6!> \author Sebastian Mueller
7!> \date Dec 2019
8!> \copyright Copyright 2005-\today, the CHS Developers, Sabine Attinger: All rights reserved.
9!! FORCES is released under the LGPLv3+ license \license_note
10MODULE mo_corr
11
12 USE mo_kind, ONLY : i4, sp, dp
13
14 Implicit NONE
15
16 PUBLIC :: autocorr ! Autocorrelation coefficient at lag k = autocoeffk(k)/autocoeffk(0)
17
18 ! ------------------------------------------------------------------
19
20 !> \brief Autocorrelation function with lag k.
21
22 !> \details Element at lag k of autocorrelation function
23 !! \f[ ACF(x,k) = \frac{s_{x,k}}{s_{x,0}} \f]
24 !! where \f$ s_{x,k} \f$ is the autocorrelation coefficient
25 !! \f[ s_{x,k} = \langle (x_i-\bar{x})(x_{i+k}-\bar{x})\rangle \f]
26 !!
27 !! If an optional mask is given, the calculations are only over those locations that correspond
28 !! to true values in the mask.\n
29 !! \f$ x \f$ can be single or double precision. The result will have the same numerical precision.
30 !!
31 !! \b Example
32 !!
33 !! Autocorrelation of 0 time steps
34 !! \code{.f90}
35 !! ak = autocorr(x, 0, mask=mask)
36 !! ---> ak = 1
37 !! \endcode
38
39 !> \param[in] "real(sp/dp) :: x(:)" Time series.
40 !> \param[in] "integer(i4) :: k[(:)]" Lag for autocorrelation.
41 !> \param[in] "optional, logical :: mask(:)" 1D-array of logical values with `size(vec)`.
42 !! If present, only those locations in vec corresponding
43 !! to the true values in mask are used.
44 !> \retval "real(sp/dp) :: ak[(:)]" Coefficient of autocorrelation function at lag k.
45
46 !> \author Matthias Cuntz
47 !> \date Nov 2011
48
49 !> \author Stephan Thober
50 !> \date Nov 2012
51 !! - added 1d version
52
53 !> \author Sebastion Mueller
54 !> \date Dec 2019
55 !! - rewritten
56
57 INTERFACE autocorr
58 MODULE PROCEDURE autocorr_sp, autocorr_dp, autocorr_1d_sp, autocorr_1d_dp
59 END INTERFACE autocorr
60
61 ! ------------------------------------------------------------------
62
63CONTAINS
64
65 ! ------------------------------------------------------------------
66
67 FUNCTION autocorr_dp(x, k, mask) result(acf)
68
69 USE mo_moment, ONLY: moment
70
71 IMPLICIT NONE
72
73 REAL(dp), DIMENSION(:), INTENT(IN) :: x
74 INTEGER(i4), INTENT(IN) :: k
75 LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
76 REAL(dp) :: acf
77
78 INTEGER(i4) :: kk ! absolute value of lag k
79 INTEGER(i4) :: nn ! number of true values in mask
80 REAL(dp) :: n ! real of nn
81 REAL(dp) :: mean, var ! moments
82 REAL(dp), DIMENSION(size(x)) :: x_shift
83 LOGICAL, DIMENSION(size(x)) :: maske
84
85 maske(:) = .true.
86 if (present(mask)) then
87 if (size(mask) /= size(x)) stop 'Error autocorr_dp: size(mask) /= size(x)'
88 maske = mask
89 end if
90
91 ! calculate mean and population variance of x
92 call moment(x, mean=mean, variance=var, mask=maske, sample=.false.)
93 ! shift x to 0 mean
94 x_shift = x - mean
95 ! use absolute value of k
96 kk = abs(k)
97 nn = size(x)
98 n = real(count(maske(1 : nn - kk).and.maske(1 + kk : nn)), dp)
99 ! calculate the auto-correlation function
100 acf = sum(x_shift(1 : nn - kk) * x_shift(1 + kk : nn), mask = (maske(1 : nn - kk) .and. maske(1 + kk : nn))) / n / var
101
102 END FUNCTION autocorr_dp
103
104 FUNCTION autocorr_sp(x, k, mask) result(acf)
105
106 USE mo_moment, ONLY: moment
107
108 IMPLICIT NONE
109
110 REAL(sp), DIMENSION(:), INTENT(IN) :: x
111 INTEGER(i4), INTENT(IN) :: k
112 LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
113 REAL(sp) :: acf
114
115 INTEGER(i4) :: kk ! absolute value of lag k
116 INTEGER(i4) :: nn ! number of true values in mask
117 REAL(sp) :: n ! real of nn
118 REAL(sp) :: mean, var ! moments
119 REAL(sp), DIMENSION(size(x)) :: x_shift
120 LOGICAL, DIMENSION(size(x)) :: maske
121
122 maske(:) = .true.
123 if (present(mask)) then
124 if (size(mask) /= size(x)) stop 'Error autocorr_sp: size(mask) /= size(x)'
125 maske = mask
126 end if
127
128 ! calculate mean and population variance of x
129 call moment(x, mean=mean, variance=var, mask=maske, sample=.false.)
130 ! shift x to 0 mean
131 x_shift = x - mean
132 ! use absolute value of k
133 kk = abs(k)
134 nn = size(x)
135 n = real(count(maske(1 : nn - kk).and.maske(1 + kk : nn)), sp)
136 ! calculate the auto-correlation function
137 acf = sum(x_shift(1 : nn - kk) * x_shift(1 + kk : nn), mask = (maske(1 : nn - kk) .and. maske(1 + kk : nn))) / n / var
138
139 END FUNCTION autocorr_sp
140
141 FUNCTION autocorr_1d_dp(x, k, mask) result(acf)
142
143 IMPLICIT NONE
144
145 REAL(dp), DIMENSION(:), INTENT(IN) :: x
146 INTEGER(i4), DIMENSION(:), INTENT(IN) :: k
147 LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
148 INTEGER(i4) :: i
149 REAL(dp), DIMENSION(size(k)) :: acf
150
151 if (present(mask)) then
152 do i = 1, size(k)
153 acf(i) = autocorr(x, k(i), mask)
154 end do
155 else
156 do i = 1, size(k)
157 acf(i) = autocorr(x, k(i))
158 end do
159 end if
160
161
162 END FUNCTION autocorr_1d_dp
163
164 FUNCTION autocorr_1d_sp(x, k, mask) result(acf)
165
166 IMPLICIT NONE
167
168 REAL(sp), DIMENSION(:), INTENT(IN) :: x
169 INTEGER(i4), DIMENSION(:), INTENT(IN) :: k
170 LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
171 INTEGER(i4) :: i
172 REAL(sp), DIMENSION(size(k)) :: acf
173
174 if (present(mask)) then
175 do i = 1, size(k)
176 acf(i) = autocorr(x, k(i), mask)
177 end do
178 else
179 do i = 1, size(k)
180 acf(i) = autocorr(x, k(i))
181 end do
182 end if
183
184 END FUNCTION autocorr_1d_sp
185
186END MODULE mo_corr
Autocorrelation function with lag k.
Definition mo_corr.f90:57
Mean of a vector.
First four moments, stddev and mean absolute devation.
Standard deviation of a vector.
Provides autocorrelation function calculations.
Definition mo_corr.f90:10
Define number representations.
Definition mo_kind.F90:17
integer, parameter sp
Single Precision Real Kind.
Definition mo_kind.F90:44
integer, parameter i4
4 Byte Integer Kind
Definition mo_kind.F90:40
integer, parameter dp
Double Precision Real Kind.
Definition mo_kind.F90:46
Statistical moments.
Definition mo_moment.f90:25