Python code gets executed for sample data only

Hi everyone,

I’m new to Python. My manager wants me to run a Python code and generate output for 40 set of values. The code works fine for sample data. But when I replace it with actual data, it doesn’t give me any output.

Below is the code. Sample data is given in the code as data = […, …, …]. One set of actual data for which I would like to generate output is data = [10.7369, 10.9114, 10.2428, 10.4129, 10.6323, 10.3315, 11.213, 10.405, 10.1623, 10.4183, 10.463, 10.1772, 10.967, 10.9385, 10.4935, 10.3695, 10.708, 11.0088].

It would be a great help if anyone could help me with this. Thanks very much.

import numpy as np
from scipy.stats import norm

def mann_kendall_test(data, alpha=0.05):
    """
    Perform the Mann-Kendall test to detect trends and change points in a time series dataset.
    Returns a tuple containing the test statistic, p-value, and a list of change point indices.
    """
    n = len(data)
    s = 0
    for i in range(n-1):
        for j in range(i+1, n):
            s += np.sign(data[j] - data[i])
    var_s = n*(n-1)*(2*n+5)/18
    if s > 0:
        z = (s - 1) / np.sqrt(var_s)
    elif s < 0:
        z = (s + 1) / np.sqrt(var_s)
    else:
        z = 0
    p = 2 * (1 - norm.cdf(np.abs(z)))
    change_points = []
    if p < alpha:
        for i in range(1, n):
            if np.sign(data[i] - data[i-1]) != np.sign(s):
                change_points.append(i-1)
    return s, p, change_points
 
data = [10, 11, 13, 12, 15, 18, 16, 19, 20, 22, 24, 25, 27, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10]
s, p, change_points = mann_kendall_test(data)
print(f"Test statistic: {s}")
print(f"P-value: {p}")
print(f"Change points: {change_points}")

Looking at your function, the output may seem empty if the p-value is not less than the provided alpha (0.05 by default). The “Change points” will only be populated if this condition is met.

For debugging, you might want to print intermediate values inside your function or check the p-value and test statistic you’re getting for your actual data. It’s possible that your actual data doesn’t meet the criteria set by the Mann-Kendall test, hence no change points are printed.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.