Exemple #1
0
def plot_model(df, model_type, n_components, title='', plot_CIs=True, repetitions=20, save_file_to='model.pdf',
               maxiter=5000, maxfun=5000, method='nm', period=24):
    rows, cols = hlp.get_factors(1)
    fig = plt.figure(figsize=(8 * cols, 8 * rows))
    gs = gridspec.GridSpec(rows, cols)

    results, df_result, _ = dproc.fit_to_model(df, n_components, model_type, period, maxiter, maxfun, method, 0)

    # plot
    ax = fig.add_subplot(gs[0])
    if plot_CIs:
        CIs = subplot_confidence_intervals(df, n_components, model_type, ax, repetitions=repetitions, maxiter=maxiter,
                                           maxfun=maxfun, period=period, method=method)
    subplot_model(df['X'], df['Y'], df_result['X_test'], df_result['Y_test'], ax, color='blue', title=title,
                  fit_label='fitted curve')

    ax_list = fig.axes
    for ax in ax_list:
        ax.legend(loc='upper left', fontsize='large')
    fig.tight_layout()
    plt.show()

    # save
    try:
        hlp.make_results_dir()
        fig.savefig(r'results\/' + save_file_to)
    except:
        print("Can not save plot.")

    if plot_CIs:
        return CIs
Exemple #2
0
def plot_raw_data(df, title, hour_intervals, save_file_to='raw.pdf'):
    rows, cols = hlp.get_factors(1)
    fig = plt.figure(figsize=(8 * cols, 8 * rows))
    gs = gridspec.GridSpec(rows, cols)

    var = df[['Y']].to_numpy().var()
    mean = df[['Y']].to_numpy().mean()
    print(title, " Var: ", var, " Mean: ", mean)

    ax = fig.add_subplot(gs[0])
    ax.scatter(df.date.head(500), df.Y.head(500), c='blue', s=1)

    date_form = md.DateFormatter("%d-%m %H:00")
    ax.xaxis.set_major_formatter(date_form)
    ax.xaxis.set_major_locator(mdates.HourLocator(interval=hour_intervals))
    plt.xticks(rotation=45)
    plt.xlabel('Day [d-m h:min]')
    plt.ylabel('Count')
    plt.title(title)

    fig.tight_layout()
    plt.show()

    # save
    try:
        hlp.make_results_dir()
        fig.savefig(r'results\/' + save_file_to)
    except:
        print("Can not save plot.")
Exemple #3
0
def plot_confidence_intervals(df, model_type, n_components, title='', repetitions=20, maxiter=5000, maxfun=5000,
                              period=24, method='nm', save_file_to='CIs.pdf'):
    rows, cols = hlp.get_factors(1)
    fig = plt.figure(figsize=(8 * cols, 8 * rows))
    gs = gridspec.GridSpec(rows, cols)

    ax = fig.add_subplot(gs[0])
    Y = df['Y']
    results, df_result, X_fit_test = dproc.fit_to_model(df, n_components, model_type, period, maxiter, maxfun, method,
                                                        0)

    # CI
    res2 = copy.deepcopy(results)
    params = res2.params
    CIs = dproc.calculate_confidence_intervals(df, n_components, model_type, repetitions, maxiter, maxfun, method,
                                               period)

    N2 = round(10 * (0.7 ** n_components) + 4)
    P = np.zeros((len(params), N2))

    i = 0
    for index, CI in CIs.iterrows():
        P[i, :] = np.linspace(CI[0], CI[1], N2)
        i = i + 1

    param_samples = hlp.lazy_cartesian_product(P)
    size = param_samples.max_size
    N = round(df.shape[0] - df.shape[0] / 3)

    for i in range(0, N):
        j = random.randint(0, size)
        p = param_samples.entry_at(j)
        res2.initialize(results.model, p)
        if model_type == 'zero_nb' or model_type == "zero_poisson":
            Y_test_CI = res2.predict(X_fit_test, exog_infl=X_fit_test)
        else:
            Y_test_CI = res2.predict(X_fit_test)
        if i == 0:
            ax.plot(df_result['X_test'], Y_test_CI, color='tomato', alpha=0.05, linewidth=0.1,
                    label='confidence intervals')
        else:
            ax.plot(df_result['X_test'], Y_test_CI, color='tomato', alpha=0.05, linewidth=0.1)

    subplot_model(df['X'], Y, df_result['X_test'], df_result['Y_test'], ax, title=title, plot_model=False)

    ax_list = fig.axes
    for ax in ax_list:
        ax.legend(loc='upper left', fontsize='large')
    fig.tight_layout()
    plt.show()

    # save
    try:
        hlp.make_results_dir()
        fig.savefig(r'results\/' + save_file_to)
    except:
        print("Can not save plot.")

    return CIs
Exemple #4
0
def fit_to_models(df, models_type=models_type, n_components=n_components, maxiter=5000, maxfun=5000, disp=0,
                  method='nm', plot_models=True, period=24, ax_lim=2500,save_file_to='models.pdf'):
    df_results = pd.DataFrame()

    if plot_models:
        rows, cols = hlp.get_factors(len(models_type))
        fig = plt.figure(figsize=(8 * cols, 8 * rows))
        gs = gridspec.GridSpec(rows, cols)

    i = 0
    for model_type in models_type:
        c = 0
        if plot_models:
            ax = fig.add_subplot(gs[i])
            if model_type == "zero_nb":
                ax.set_ylim(0,ax_lim)
        for n_component in n_components:

            _, df_result, _ = fit_to_model(df, n_component, model_type, period, maxiter, maxfun, method, disp)

            # plot
            if plot_models:
                title = hlp.get_model_name(model_type)
                if c == 0:
                    plot.subplot_model(df['X'], df['Y'], df_result['X_test'], df_result['Y_test'], ax, color=colors[c],
                                       title=title, fit_label='N=' + str(n_component))
                else:
                    plot.subplot_model(df['X'], df['Y'], df_result['X_test'], df_result['Y_test'], ax, color=colors[c],
                                       title=title, fit_label='N=' + str(n_component), plot_measurements=False)
                c = c + 1

            df_results = df_results.append(df_result, ignore_index=True)

        i = i + 1

    # show plots
    if plot_models:
        ax_list = fig.axes
        for ax in ax_list:
            ax.legend(loc='upper left', fontsize='medium')
        fig.tight_layout()
        plt.show()

        # save
        try:
            hlp.make_results_dir()
            fig.savefig(r'results\/' + save_file_to)
        except:
            print("Can not save plot.")

    return df_results