示例#1
0
def test_estimate_trend():
    x = np.arange(0, 100)
    y = np.arange(0, 100)
    noise = np.random.normal(size=100)
    signal = y + noise

    estimation = HPfilter()
    cycle, trend = estimation.estimate_trend(x, signal)

    assert np.sum(y-trend)/y.shape[0] < 0.5
示例#2
0
# %%
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')

def obtain_config(config_file_name: str) -> configparser.ConfigParser:
    """
    Read the config file

    :param config_file_name: name of the file
    :return: parameters in the config file
    """
    config = configparser.ConfigParser(allow_no_value=True)
    config_file_path = SYNTHETIC_DIR + '/' + config_file_name
    config.read(config_file_path)
    return config


if __name__ == '__main__':
    generate_data()

    methods_detection = [ITA(), MannKendall(), Regression(), Theil()]
    methods_estimation = [
        EmpiricalModeDecomposition(),
        HPfilter(),
        Splines(),
        Theil(),
        Regression(),
        Lowess()
    ]

    trend_estimation_comparison(methods_estimation, '*')
    #generate_with_name('func', 5)
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)
                  f'snr: {noise_title}')

        for method in methods_list:
            estimation = method.estimate_trend(x, y)
            plt.plot(x, estimation, label=method.name)

            distance = np.linalg.norm((estimation - trend))
            file_results.append(distance)

        plt.plot(x, trend, label='True trend', linewidth=3.0, color='k', linestyle=':')
        plt.legend()
        plt.savefig(f'{PLOTS_DIR}/{name}_{timestamp}.png')
        plt.close()

        results[name] = file_results

    table = pd.DataFrame(results, columns)
    table.to_csv(f'{RESULTS_DIR}/trend_estimation_{file_prefix}_{timestamp}.csv')

    return table


if __name__ == '__main__':
    # methods_detection = [MannKendall(), ITA(), Theil(), DWS(), EmpiricalModeDecomposition(),
    #                      HPfilter(), Splines()]
    #
    # trend_detection_comparison(methods_detection, 'func')

    methods_estimation = [Theil(), DWS(), EmpiricalModeDecomposition(), HPfilter(), Splines()]
    trend_estimation_comparison(methods_estimation, 'func')