def test_detect_no_trend(self): x = np.zeros((100, )) y = np.zeros((100, )) noise = np.random.normal(size=100) signal = y + noise mk = MannKendall() trend, trend_type, p, z = mk.detect_trend(x, signal) assert not trend assert trend_type == 'no trend'
def test_detect_decreasing_trend(self): x = np.linspace(100, 0, num=100) y = np.linspace(100, 0, num=100) noise = np.random.normal(size=100) signal = y + noise mk = MannKendall() trend, trend_type, p, z = mk.detect_trend(x, signal) assert trend assert trend_type == 'decreasing'
def test_detect_increasing_trend(self): x = np.arange(0, 100) y = np.arange(0, 100) noise = np.random.normal(size=100) signal = y + noise mk = MannKendall() trend, trend_type, p, z = mk.detect_trend(x, signal) assert trend assert trend_type == 'increasing'
# %% import matplotlib.pyplot as plt from src.compare_functions import trend_estimation_comparison from src.data_handler import data_handler from src.methods.dws import DWS from src.methods.emd import EmpiricalModeDecomposition from src.methods.lowess import Lowess from src.methods.hp_filter import HPfilter from src.methods.ita import ITA from src.methods.mk import MannKendall from src.methods.regression import Regression from src.methods.splines import Splines from src.methods.theil import Theil from src.methods.regression import Regression if __name__ == '__main__': methods_detection = [ITA(), MannKendall(), Regression(), Theil()] methods_estimation = [DWS(), HPfilter(), Splines(), Theil(), Lowess(), EmpiricalModeDecomposition(), Regression()] x, y, trend, seasonality, noise = data_handler.generate_synthetic_data('data', 'monthly.ini') plt.plot(x, y) plt.plot(x, trend) plt.show() trend_estimation_comparison(methods_estimation, 'monthly')
from src.utils import generate_timestamp def show_estimation(methods: List[Method], time_series_x: np.ndarray, time_series_y: np.ndarray) -> None: for method in methods: trend_estimation = method.estimate_trend(time_series_x, time_series_y) plt.figure(figsize=(12, 8)) plt.plot(time_series_x, time_series_y) plt.plot(time_series_x, trend_estimation, label='Estimated trend') plt.title(f'Trend estimation of {method.name}') plt.xlabel('Days from January 1962 to July 2012') plt.ylabel('Difference between LOD and 86400 seconds (ms)') plt.savefig(f'{PLOTS_DIR}/case_study_{generate_timestamp()}.png') plt.show() plt.close() if __name__ == '__main__': methods_detection = [MannKendall(), ITA(plot=True)] methods_estimation = [Regression(), Lowess(), Splines(), HPfilter(), DWS()] data = loadmat(f'{DATA_DIR}/case_study.mat') y = data['LOD'].squeeze() x = np.arange(0, y.shape[0]) #print(method_detection.detect_trend(x, y)) show_estimation(methods_estimation, x, y)