careem
careem

Reputation: 31

How to run Standard Normal Homogeneity Test for a time series data

I have a timeseries data (sample data) for a variable wind for nearly 40 stations and 36 years (details in sample screenshot).

I need to run the Standard Normal Homogeneity Test and Pettitt's Test for this data as per recommendations. Are they available in python?

I couldn't find any code for the mentioned tests in python documentations and packages.

I need some help here to know if there is any package holding these tests.

There are codes in R as follows:

snht(data, period, robust = F, time = NULL, scaled = TRUE, rmSeasonalPeriod = Inf, ...)

However, no results so far... only errors.

Upvotes: 3

Views: 1272

Answers (1)

boro141
boro141

Reputation: 65

Regarding the Pettitt test, I found this python implementation.

I believe there is a small typo: the t+1 on line 19 should actually just be t.

I also have developed a faster, vectorised implementation::

import numpy as np

def pettitt_test(X):
    """
    Pettitt test calculated following Pettitt (1979): https://www.jstor.org/stable/2346729?seq=4#metadata_info_tab_contents
    """

    T = len(X)
    U = []
    for t in range(T): # t is used to split X into two subseries
        X_stack = np.zeros((t, len(X[t:]) + 1), dtype=int)
        X_stack[:,0] = X[:t] # first column is each element of the first subseries
        X_stack[:,1:] = X[t:] # all rows after the first element are the second subseries
        U.append(np.sign(X_stack[:,0] - X_stack[:,1:].transpose()).sum()) # sign test between each element of the first subseries and all elements of the second subseries, summed.

    tau = np.argmax(np.abs(U)) # location of change (first data point of second sub-series)
    K = np.max(np.abs(U))
    p = 2 * np.exp(-6 * K**2 / (T**3 + T**2))
        
    return (tau, p)

Upvotes: 2

Related Questions