Error in Algorithm Implementation

07/21/2023 14:04 TonyFinch09#1
I'm putting together a Master's Algorithm for a time series analysis assignment. However, I appear to have encountered an unusual bug that is preventing my code from running as planned. The relevant section of the code is as follows:
Code:
def masters_algorithm(data):
    if not data or len(data) == 0:
        return None

    # Extracting timestamps and values from data
    timestamps = [item[0] for item in data]
    values = [item[1] for item in data]

    # Applying a moving average to the values
    window_size = 3
    moving_averages = []
    for i in range(len(values) - window_size + 1):
        window_sum = sum(values[i:i + window_size])
        moving_averages.append(window_sum / window_size)

    # Finding the timestamp corresponding to the highest moving average
    max_average = max(moving_averages)
    max_average_index = moving_averages.index(max_average)
    result_timestamp = timestamps[max_average_index]

    return result_timestamp

# Example usage
data_points = [(1609459200, 10), (1609545600, 5), (1609632000, 2), (1609718400, 7), (1609804800, 15)]
result = masters_algorithm(data_points)
print(result)
The method seeks the timestamp that corresponds to the highest moving average of the data points, as seen in this [Only registered and activated users can see links. Click Here To Register...]. However, when the code is executed with the sample data_points, it does not yield the expected timestamp. I believe there is a problem with how I calculate the moving averages or obtain the correct date.

Could you kindly assist me in determining the source of the problem and making recommendations for how to resolve it? Your help would be highly appreciated!

Thank you very much!
07/23/2023 23:06 cypher#2
Quote:
Originally Posted by TonyFinch09 View Post
I'm putting together a Master's Algorithm for a time series analysis assignment. However, I appear to have encountered an unusual bug that is preventing my code from running as planned. The relevant section of the code is as follows:
Code:
def masters_algorithm(data):
    if not data or len(data) == 0:
        return None

    # Extracting timestamps and values from data
    timestamps = [item[0] for item in data]
    values = [item[1] for item in data]

    # Applying a moving average to the values
    window_size = 3
    moving_averages = []
    for i in range(len(values) - window_size + 1):
        window_sum = sum(values[i:i + window_size])
        moving_averages.append(window_sum / window_size)

    # Finding the timestamp corresponding to the highest moving average
    max_average = max(moving_averages)
    max_average_index = moving_averages.index(max_average)
    result_timestamp = timestamps[max_average_index]

    return result_timestamp

# Example usage
data_points = [(1609459200, 10), (1609545600, 5), (1609632000, 2), (1609718400, 7), (1609804800, 15)]
result = masters_algorithm(data_points)
print(result)
The method seeks the timestamp that corresponds to the highest moving average of the data points, as seen in this [Only registered and activated users can see links. Click Here To Register...]. However, when the code is executed with the sample data_points, it does not yield the expected timestamp. I believe there is a problem with how I calculate the moving averages or obtain the correct date.

Could you kindly assist me in determining the source of the problem and making recommendations for how to resolve it? Your help would be highly appreciated!

Thank you very much!
The master's theorem has nothing to do with your assignment. It is used to determine the time complexity of recursive functions. You should rename your method.

Your method returns the correct value. What timestamp did you expect it to return? (2+7+15) / 3 is the highest moving average. The timestamp for the first value (2) is 1609632000. That's what your method returns.