ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_sampleCCF::setACF Interface Reference

Return the auto-correlation function (ACF) \((f\star f)(\tau)\) of the discrete signal \(f\) lagging itself for a range of lags that spans the sequence length.
More...

Detailed Description

Return the auto-correlation function (ACF) \((f\star f)(\tau)\) of the discrete signal \(f\) lagging itself for a range of lags that spans the sequence length.

See the documentation of the parent module pm_sampleCCF for algorithmic details and sample correlation matrix definition.
Note that acf is always real-valued at lag 0 by definition, even when the input f is of type complex.
For all non-zero lags, the imaginary component of the auto-correlation function is an odd function.

Warning
This low-level generic interface neither right-pads nor normalizes the input sequence in any form.
The input sequence is used as is for computing the ACF.
In general, the following steps should be taken before and after computing the ACF using this routine,
  1. If the original (unpadded) sequence is not already mean-shifted (with zero mean), it is highly recommended to mean-shift the sequence first before computing the ACF.
    This can be readily done by the routines of pm_sampleMean and pm_sampleShift.
  2. While the input mean-shifted sequence can be passed to this generic interface as is, it is highly recommended to right-pad the mean-shifted sequence by zeros to the minimum length of size(f) * 2 - 1.
    This means that the length of the input sequence should always be an odd number, although not necessarily, if you know what you are doing.
  3. The output acf must from the generic interface must be multiplied by 1 / f(1) to properly normalize the output acf to the range [-1, +1].
    While easy, this can also be readily done via the generic interfaces of pm_sampleScale.
Note
When the input sequence is right-padded such that size(acf) = size(f) * 2 - 1, then,
  1. the resulting slice acf(1 : size(f)) by this generic interface contains the auto-correlation corresponding to lags [(i, i = 0, size(f))].
  2. the resulting slice acf(size(f) + 1 : size(acf)) contains the auto-correlation corresponding to lags [(i, i = -size(f) + 1, -1)].
Parameters
[in]factor: The input contiguous vector of shape (:) of type integer of default kind IK, containing the factorization of the length of the input sequence f whose FFT is to be computed.
This input argument along with the input argument coef is the direct output of getFactorFFT.
[in]coef: The input contiguous vector of shape (1:size(data)) of the same type and kind as the input argument f, containing the trigonometric look up table required for FFT of the specified sequence.
This input argument along with factor is the direct output of getFactorFFT.
[in,out]f: The input contiguous vector of arbitrary size (of minimum 2) of,
  1. type complex of kind any supported by the processor (e.g., CK, CK32, CK64, or CK128),
  2. type real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
containing the first sequence in the auto-correlation computation.
On output, the contents of f are destroyed.
If the output inf = .true., then f contains the resulting unnormalized ACF of the input sequence.
[out]work: The output contiguous vector of the same type, kind, and size as the input f, used as a workspace.
If the condition inf = .false. holds on output, then work contains the resulting unnormalized ACF of the input sequence f.
[out]inf: The output scalar of type logical of default kind LK.
  1. If .true., the resulting ACF is stored in the output argument f upon return from the procedure.
  2. If .false., the resulting ACF is stored in the output argument work upon return from the procedure.


Possible calling interfaces

use pm_sampleCCF, only: setACF
call setACF(factor(:), coef(1:nseq), f(1:nseq), work(1:nseq), inf)
Return the auto-correlation function (ACF) of the discrete signal lagging itself for a range of lag...
This module contains classes and procedures for computing properties related to the cross correlation...
Warning
The condition 2 < size(f) must hold for the corresponding input arguments.
The condition size(f) == size(coef) must hold for the corresponding input arguments.
The condition size(f) == size(work) must hold for the corresponding input arguments.
These conditions are verified only if the library is built with the preprocessor macro CHECK_ENABLED=1.
The pure procedure(s) documented herein become impure when the ParaMonte library is compiled with preprocessor macro CHECK_ENABLED=1.
By default, these procedures are pure in release build and impure in debug and testing builds.
See also
getACF
setACF
getCor
setCor
getRho
setRho
getCov
setCov
setECDF
getMean
setMean
getShifted
setShifted
getVar
setVar


Example usage

1program example
2
3 use pm_kind, only: SK, IK, LK, RK
4 use pm_io, only: getErrTableWrite
5 use pm_io, only: display_type
6 use pm_io, only: getFormat
7 use pm_fftpack, only: allocatable
8 use pm_fftpack, only: getFactorFFT
9 use pm_sampleCCF, only: setACF
10 use pm_sampleCCF, only: setCCF
11 use pm_sampleCCF, only: stdscale
12 use pm_arrayRange, only: getRange
13 use pm_distUnif, only: getUnifRand
14 use pm_distNorm, only: getNormLogPDF
15 use pm_arrayPad, only: getPaddedr
16 use pm_arrayFill, only: getFilled
17 use pm_sampleShift, only: getShifted
18 use pm_arrayResize, only: setResized
19 use pm_arraySpace, only: getLinSpace
20 use pm_sampleNorm, only: getNormed
21 use pm_sampleMean, only: getMean
22 use pm_sampleVar, only: getVar
23
24 implicit none
25
26 logical(LK) :: inf
27 type(display_type) :: disp
28 character(:), allocatable :: format
29 integer(IK) :: nsam, itry, ntry = 1
30 disp = display_type(file = "main.out.F90")
31
32 call disp%skip()
33 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
34 call disp%show("!Compute the cross-correlation of two samples.")
35 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
36 call disp%skip()
37
38 block
39 use pm_kind, only: TKG => RKS ! All other real types are also supported.
40 integer(IK), allocatable :: factor(:), lag(:)
41 real(TKG), allocatable :: seq(:), f(:), g(:), acf(:), coef(:), range(:)
42 real(TKG), parameter :: ZERO = 0._TKG
43 call disp%skip()
44 call disp%show("nsam = 41")
45 nsam = 41
46 call disp%show("range = getLinSpace(-10., 10., nsam)")
47 range = getLinSpace(-10., 10., nsam)
48 call disp%show("seq = sin(range)")
49 seq = sin(range)
50 call disp%show("seq = getPaddedr(getShifted(seq, -getMean(seq)), nsam - 1, ZERO)")
51 seq = getPaddedr(getShifted(seq, -getMean(seq)), nsam - 1, ZERO)
52 call disp%show("seq")
53 call disp%show( seq )
54 call disp%show("call setResized(acf, size(seq, 1, IK))")
55 call setResized(acf, size(seq, 1, IK))
56 call disp%show("factor = getFactorFFT(seq, coef, allocatable)")
57 factor = getFactorFFT(seq, coef, allocatable)
58 call disp%show("f = seq")
59 f = seq
60 call disp%show("call setACF(factor, coef, f, acf, inf)")
61 call setACF(factor, coef, f, acf, inf)
62 call disp%show("acf = merge(f, acf, inf) / size(acf)")
63 acf = merge(f, acf, inf) / size(acf)
64 call disp%show("size(acf)")
65 call disp%show( size(acf) )
66 call disp%show("acf")
67 call disp%show( acf )
68 call disp%show("f = seq; g = seq")
69 f = seq; g = seq
70 call disp%show("call setCCF(factor, coef, f, g, acf, inf) ! for comparison with the above.")
71 call setCCF(factor, coef, f, g, acf, inf) ! for comparison with the above.
72 call disp%show("acf = merge(f, g, inf) / size(acf)")
73 acf = merge(f, g, inf) / size(acf)
74 call disp%show("size(acf)")
75 call disp%show( size(acf) )
76 call disp%show("acf")
77 call disp%show( acf )
78 call disp%show("lag = getRange(-nsam + 1_IK, nsam - 1_IK)")
79 lag = getRange(-nsam + 1_IK, nsam - 1_IK)
80 call disp%show("if (0 /= getErrTableWrite(SK_'setACF.crd.sin.RK.txt', reshape([range, seq], [nsam, 2_IK]), header = SK_'crd,f')) error stop 'acf outputting failed.'")
81 if (0 /= getErrTableWrite(SK_'setACF.crd.sin.RK.txt', reshape([range, seq], [nsam, 2_IK]), header = SK_'crd,f')) error stop 'acf outputting failed.'
82 call disp%show("if (0 /= getErrTableWrite(SK_'setACF.acf.sin.RK.txt', reshape([real(lag, TKG), acf], [size(lag), 2]), header = SK_'lag,acf')) error stop 'acf outputting failed.'")
83 if (0 /= getErrTableWrite(SK_'setACF.acf.sin.RK.txt', reshape([real(lag, TKG), acf], [size(lag), 2]), header = SK_'lag,acf')) error stop 'acf outputting failed.'
84 call disp%skip()
85 end block
86
87end program example
Generate and return an array of the specified rank and shape of arbitrary intrinsic type and kind wit...
Generate a resized copy of the input array by padding it to the right with the requested paddings and...
Generate minimally-spaced character, integer, real sequences or sequences at fixed intervals of size ...
Allocate or resize (shrink or expand) an input allocatable scalar string or array of rank 1....
Generate count evenly spaced points over the interval [x1, x2] if x1 < x2, or [x2,...
Generate the natural logarithm of probability density function (PDF) of the univariate Normal distrib...
Generate and return a scalar or a contiguous array of rank 1 of length s1 of randomly uniformly distr...
Generate and return the factorization vector factor of the specified input sequence length and the co...
Definition: pm_fftpack.F90:292
Generate and return the iostat code resulting from writing the input table of rank 1 or 2 to the spec...
Definition: pm_io.F90:5940
Generate and return a generic or type/kind-specific IO format with the requested specifications that ...
Definition: pm_io.F90:18485
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11726
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11508
Return the cross-correlation function (CCF) of the discrete signal lagging the signal the discrete ...
Generate and return the (weighted) mean of an input sample of nsam observations with ndim = 1 or 2 at...
Generate a sample of shape (nsam), or (ndim, nsam) or (nsam, ndim) that is normalized by the specifie...
Generate a sample of shape (nsam), or (ndim, nsam) or (nsam, ndim) that is shifted by the specified i...
Generate and return the variance of the input sample of type complex or real of shape (nsam) or (ndim...
This module contains procedures and generic interfaces for convenient allocation and filling of array...
This module contains procedures and generic interfaces for resizing an input array and padding them w...
Definition: pm_arrayPad.F90:30
This module contains procedures and generic interfaces for generating ranges of discrete character,...
This module contains procedures and generic interfaces for resizing allocatable arrays of various typ...
This module contains procedures and generic interfaces for generating arrays with linear or logarithm...
This module contains classes and procedures for computing various statistical quantities related to t...
This module contains classes and procedures for computing various statistical quantities related to t...
This module contains procedures and generic interfaces for computing the Discrete Fourier Transform o...
Definition: pm_fftpack.F90:205
This module contains classes and procedures for input/output (IO) or generic display operations on st...
Definition: pm_io.F90:252
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
Definition: pm_kind.F90:268
integer, parameter RK
The default real kind in the ParaMonte library: real64 in Fortran, c_double in C-Fortran Interoperati...
Definition: pm_kind.F90:543
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
Definition: pm_kind.F90:541
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
integer, parameter RKS
The single-precision real kind in Fortran mode. On most platforms, this is an 32-bit real kind.
Definition: pm_kind.F90:567
This module contains classes and procedures for computing the first moment (i.e., the statistical mea...
This module contains classes and procedures for normalizing univariate or multivariate samples by arb...
This module contains classes and procedures for shifting univariate or multivariate samples by arbitr...
This module contains classes and procedures for computing the properties related to the covariance ma...
Generate and return an object of type display_type.
Definition: pm_io.F90:10282

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example output
1
2!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3!Compute the cross-correlation of two samples.
4!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
6
7nsam = 41
8range = getLinSpace(-10., 10., nsam)
9seq = sin(range)
10seq = getPaddedr(getShifted(seq, -getMean(seq)), nsam - 1, ZERO)
11seq
12+0.544021070, +0.751511455E-1, -0.412118465, -0.798487127, -0.989358246, -0.937999964, -0.656986594, -0.215119958, +0.279415518, +0.705540299, +0.958924294, +0.977530122, +0.756802440, +0.350783259, -0.141119972, -0.598472178, -0.909297407, -0.997494996, -0.841470957, -0.479425520, +0.261678927E-7, +0.479425579, +0.841470957, +0.997494996, +0.909297407, +0.598472178, +0.141120031, -0.350783199, -0.756802440, -0.977530122, -0.958924294, -0.705540299, -0.279415458, +0.215120018, +0.656986594, +0.937999964, +0.989358246, +0.798487127, +0.412118524, -0.751510859E-1, -0.544021130, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000, +0.00000000
13call setResized(acf, size(seq, 1, IK))
14factor = getFactorFFT(seq, coef, allocatable)
15f = seq
16call setACF(factor, coef, f, acf, inf)
17acf = merge(f, acf, inf) / size(acf)
18size(acf)
19+81
20acf
21+19.4603882, +16.5995274, +9.90436745, +1.18769729, -7.34153843, -13.6373682, -16.3073692, -14.9171066, -10.0427866, -3.07247305, +4.18143559, +9.95184994, +12.9474525, +12.6391058, +9.33939934, +4.06805849, -1.74958324, -6.66454172, -9.56497478, -9.92598820, -7.89280128, -4.18799973, +0.120422743, +3.91993761, +6.34000349, +6.95056915, +5.82759619, +3.47926354, +0.664440393, -1.83813798, -3.44246578, -3.89219093, -3.28998709, -2.02030611, -0.597234249, +0.511142671, +1.02663577, +0.930730164, +0.442753345, -0.817697793E-1, -0.295960993, -0.295958340, -0.817682743E-1, +0.442753911, +0.930729270, +1.02663636, +0.511143804, -0.597234249, -2.02030659, -3.28998709, -3.89218998, -3.44246483, -1.83813930, +0.664440751, +3.47926354, +5.82759619, +6.95056915, +6.34000349, +3.91993928, +0.120418593, -4.18800020, -7.89280176, -9.92598629, -9.56497478, -6.66454268, -1.74958158, +4.06805849, +9.33940029, +12.6391058, +12.9474497, +9.95185184, +4.18143511, -3.07247376, -10.0427876, -14.9171066, -16.3073692, -13.6373672, -7.34153557, +1.18769836, +9.90436840, +16.5995255
22f = seq; g = seq
23call setCCF(factor, coef, f, g, acf, inf) ! for comparison with the above.
24acf = merge(f, g, inf) / size(acf)
25size(acf)
26+81
27acf
28+19.4603882, +16.5995274, +9.90436745, +1.18769729, -7.34153843, -13.6373682, -16.3073692, -14.9171066, -10.0427866, -3.07247305, +4.18143559, +9.95184994, +12.9474525, +12.6391058, +9.33939934, +4.06805849, -1.74958324, -6.66454172, -9.56497478, -9.92598820, -7.89280128, -4.18799973, +0.120422743, +3.91993761, +6.34000349, +6.95056915, +5.82759619, +3.47926354, +0.664440393, -1.83813798, -3.44246578, -3.89219093, -3.28998709, -2.02030611, -0.597234249, +0.511142671, +1.02663577, +0.930730164, +0.442753345, -0.817697793E-1, -0.295960993, -0.295958340, -0.817682743E-1, +0.442753911, +0.930729270, +1.02663636, +0.511143804, -0.597234249, -2.02030659, -3.28998709, -3.89218998, -3.44246483, -1.83813930, +0.664440751, +3.47926354, +5.82759619, +6.95056915, +6.34000349, +3.91993928, +0.120418593, -4.18800020, -7.89280176, -9.92598629, -9.56497478, -6.66454268, -1.74958158, +4.06805849, +9.33940029, +12.6391058, +12.9474497, +9.95185184, +4.18143511, -3.07247376, -10.0427876, -14.9171066, -16.3073692, -13.6373672, -7.34153557, +1.18769836, +9.90436840, +16.5995255
29lag = getRange(-nsam + 1_IK, nsam - 1_IK)
30if (0 /= getErrTableWrite(SK_'setACF.crd.sin.RK.txt', reshape([range, seq], [nsam, 2_IK]), header = SK_'crd,f')) error stop 'acf outputting failed.'
31if (0 /= getErrTableWrite(SK_'setACF.acf.sin.RK.txt', reshape([real(lag, TKG), acf], [size(lag), 2]), header = SK_'lag,acf')) error stop 'acf outputting failed.'
32
33

Postprocessing of the example output
1#!/usr/bin/env python
2
3import matplotlib.pyplot as plt
4import pandas as pd
5import numpy as np
6import glob
7import sys
8
9linewidth = 2
10fontsize = 17
11
12for kind in ["sin.RK"]:
13
14 file = glob.glob("*crd*"+kind+".txt")[0]
15 df = pd.read_csv(file, delimiter = ",")
16
17 #print(df.values)
18 fig = plt.figure(figsize = (8, 6))
19 ax = plt.subplot(1,1,1)
20 ax.plot ( df.values[:, 0]
21 , df.values[:,1:]
22 , zorder = 1000
23 )
24 plt.minorticks_on()
25 ax.set_xlabel("x", fontsize = 17)
26 ax.set_ylabel("f(x)", fontsize = 17)
27 ax.tick_params(axis = "x", which = "minor")
28 ax.tick_params(axis = "y", which = "minor")
29 plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
30 ax.legend([file.split(".")[-3] + "(x)"], fontsize = fontsize)
31 plt.tight_layout()
32 plt.savefig(file.replace(".txt",".png"))
33
34 file = glob.glob("*acf*"+kind+".txt")[0]
35 df = pd.read_csv(file, delimiter = ",")
36 fig = plt.figure(figsize = (8, 6))
37 ax = plt.subplot(1,1,1)
38 ax.plot ( df.values[:, 0]
39 , df.values[:, 1]
40 , zorder = 1000
41 )
42 plt.minorticks_on()
43 ax.set_xlabel("Lag", fontsize = 17)
44 ax.set_ylabel("acf(f)", fontsize = 17)
45 ax.tick_params(axis = "x", which = "minor")
46 ax.tick_params(axis = "y", which = "minor")
47 plt.grid(visible = True, which = "both", axis = "both", color = "0.85", linestyle = "-")
48 plt.tight_layout()
49 plt.savefig(file.replace(".txt",".png"))

Visualization of the example output
Test:
test_pm_sampleCCF
Internal naming convention:
The following illustrates the internal naming convention used for the procedures within this generic interface.
setACF_FP_D1_CK5()
||| || || |||
||| || || The type and kind parameters of the input sequence.
||| || The dimension of the input sequence `f`.
||| The method used: FP => fftpack.
ACF: Cross-Correlation Function.


Final Remarks


If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.

  1. If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
  2. If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.

This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.

Author:
Fatemeh Bagheri, Monday 02:15 AM, September 27, 2021, Dallas, TX
Amir Shahmoradi, Wednesday 4:13 AM, August 13, 2016, Institute for Computational Engineering and Sciences (ICES), The University of Texas at Austin

Definition at line 525 of file pm_sampleCCF.F90.


The documentation for this interface was generated from the following file: