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}")
wolf29 commented: Are you hard-coding the data points like your test data? Are the production values of the data points unchanging? +2

If in doubt, output the variables as the program runs. Here's a modified version of your program with the data set you wanted to run:

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 = []
    print(f"p: {p}")
    print(f"alpha: {alpha}")
    if p < alpha:
        for i in range(1, n):
            s1 = np.sign(data[i] - data[i-1])
            s2 = np.sign(s)
            print(f"np.sign(data[i] - data[i-1]): {s1}")
            print(f"np.sign(s): {s2}")
            if np.sign(data[i] - data[i-1]) != np.sign(s):
                change_points.append(i-1)
    return s, p, change_points

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]
s, p, change_points = mann_kendall_test(data)
print(f"Test statistic: {s}")
print(f"P-value: {p}")
print(f"Change points: {change_points}")

This outputs:

p: 0.4953662988030887
alpha: 0.05

There you have the problem. Values are only added to change_points if p < alpha and that condition evaluates false in your data set.

commented: Thanks +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.