I am working with climate variables time series (such as temperature, wind speed and humidity) and would like to understand how the wind speed (e.g.) at a place is influenced by other entries, by distance and time lag.

Consider that I have an array A of dimensions (T, N, M), where T is the amount of time steps, N is the latitude span and M is the longitude span. Therefore, the array A represents the evolution of wind speed at the rectangle defined by N and M. Moreover, consider two variables: k, representing the distance between entries (pixels in an image); and t, representing the lag between time steps.

The idea I had was to calculate the autocorrelation of each entry with all others that satisfy any combination of k and t. With this in mind, consider this simplified example:

from scipy.stats import pearsonr

A = np.random.rand((5, 3, 4)) # A.shape = (5, 3, 4), or T = 5, N = 3, M = 4

t, k = 2, 1

# to account for t = 2, two copies of the array A are defined - notice that they have the same shape
present = A[:-t, :, :] # present.shape = (3, 3, 4)
lagged  = A[ t:, :, :] # lagged.shape  = (3, 3, 4)

# considering the time series in the top left corner, `present[:, 0, 0]`, there are two time series 
# to consider for k = 1: `lagged[:, 1, 0]` and `lagged[:, 0, 1]`

# `pearsonr` return `statistic, pvalue`, but only `statistic` matters
# for present[:, 0, 0], an average of the contributions of all entries that satisfy (t = 2, k = 1) 
# is taken
statistic = (
    pearsonr(present[:, 0, 0], lagged[:, 1, 0])[0] 
    + pearsonr(present[:, 0, 0], lagged[:, 0, 1])[0]
) / 2

# notice that for `present[:, 1, 1]` there are four to consider: `lagged[:, 1, 0]`, `lagged[:, 0, 1]`, 
# `lagged[:, 1, 2]` and `lagged[:, 2, 1]`; all have distance (Manhattan distance, or 
# scipy.spatial.distance.cityblock) `k = 1`

Having calculated every combination of k and t possible (which I have using a combination of itertools.product and scipy.spatial.distance.cityblock), plotting the result should give an idea of both time dependence (like an autocorrelation function, ACF) and spatial correlation (that, in this example, is highly related to terrain).

Below I bring two images as examples: on the left, the series being considered; and on the right what I would expect for the plot.

The first one is a simple series that goes from total black to total white, frame by frame. Whenever we have an even time lag (t = 0), any pixel is being compared with a pixel that is equal to it, no matter the distance. Therefore, it has the maximum value of correlation: 1, represented by black. If the time lag is odd (t = 1), then the correlation is zero.

correlation on time example

The second example is an identity matrix with shape (2, 2) on even time stamps and its mirrored version on odd ones. Therefore, when t + k is even, we have maximum correlation, otherwise it is zero.

correlation on time and distance example

If the images haven't loaded, this https://imgur.com/a/U9OVAFO should work.

I have already about correlate2d and convolve2d, but wasn't able to understand how to apply them for this purpose.

I am currently searching for other solutions, so if you are able to point me to good resources or have any other ideas on how to measure this spatio-temporal relation, please feel welcomed to share.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.