Пример #1
0
    def predict(self, future_points: int = 1) -> List[Prediction]:

        train_X, train_Y, test_X = self.build_train_test(future_points)
        df = self.build_df(train_X, train_Y)

        model = Prophet(uncertainty_samples=2000)
        model.fit(df)

        future = pd.DataFrame({"ds": test_X})
        forecast = model.predictive_samples(future)

        posterior_pred = forecast["yhat"]
        pred_mean = np.mean(posterior_pred, axis=1)
        pred_var = np.var(posterior_pred, axis=1)

        return [Prediction(pred_mean[i],pred_var[i]) for i in np.arange(len(posterior_pred))]
Пример #2
0
plt.ylabel('Number of Passengers')
plt.axvline(x=131, c='k', linestyle='dashed', linewidth=0.5)
plt.plot(may_1960_index,
         forecast['yhat'][may_1960_index],
         'ro',
         fillstyle='none',
         color='c')
plt.annotate(s='',
             xy=(may_1960_index, forecast_new.iloc[may_1960_index]),
             xytext=(may_1960_index, forecast['yhat'][may_1960_index]),
             arrowprops=dict(arrowstyle='simple', color='g'))
plt.legend()
plt.show()

# Extract the samples from the posterior predictive distribution for may 1960
pred_samples = m.predictive_samples(future)
pred_samples_df = pd.DataFrame(pred_samples['yhat'])
prior_samples = pred_samples_df.iloc[may_1960_index, :]

print(
    "We introduce th optinion of an expert:  \n This May we had 420.000 Passengers and we will definitely not have fewer in May 1960 (['-inf', 420] has probability 1%).\n Furthermore, given the numbers of the last three years, I am sure that a growth rate compared to this May between 7.5% and 15% is extremely probable ([451, 483] has probability 80%).  \n However, an increase of 15% or more compared to this May, in my opinion, is unrealistic ([483, 'inf'] has probability 1%)."
)
input("Press Enter to continue...")

# Define the constraints for may 1960
rules = [['-inf', 420, 0.01], [451, 483, 0.80], [483, 'inf', 0.01]]

# Calculate the Maximum Entropy Distribution for may 1960
points, weights, prior, max_ent_dist = me.opt_max_ent(rules, prior_samples)

# Plot prior and the Maximum Entropy Distribution for may 1960
Пример #3
0
    #loop over each state. Parallelise?
    #initialise model
    m = Prophet(mcmc_samples=100, growth='logistic')

    df_prophet['y'] = cases[state].values[:-truncation]
    df_prophet['cap'] = pop[state]
    df_prophet['floor'] = 0

    m.fit(df_prophet)
    future = m.make_future_dataframe(periods=truncation + forecast_length,
                                     include_history=False)

    future['cap'] = pop[state]
    future['floor'] = 0
    df_forecast = m.predict(future)
    forecast = m.predictive_samples(future)
    fig1 = m.plot(df_forecast,
                  ax=ax[i // 2, i % 2],
                  plot_cap=False,
                  xlabel='Date of Symptom Onset',
                  ylabel='Local cases')

    fig2 = m.plot_components(df_forecast, plot_cap=False)

    fig2.savefig("./results/prophet/" + data_date.strftime("%m-%d") + "/" +
                 state + "components.png",
                 dpi=144)

    samples = m.predictive_samples(future)
    temp = pd.DataFrame(samples['yhat'])
    temp['state'] = state
def main(timey, fcst_type):
    location = "886-limited"
    timestring = time.strftime("%Y-%m-%d")
    sql = r"C:\Users\uxkp\sql_queries\inventory\avg_bal_daily.sql"
    df = daily_time_series(data_prep(read_sql_to_df(sql)))
    print(df.columns)
    df.rename(columns={"DATETIME": "ds", "INVENTORY": "y"}, inplace=True)
    print(df.dtypes)
    print(df.tail)

    df.reset_index(drop=True)

    # m = Prophet()

    df["y"] = np.log(df["y"].replace(0, np.nan))
    df.to_csv(r"c:\users\uxkp\desktop\filetest.csv", index=False)

    df["cap"] = 24.5
    df["floor"] = 0

    m = Prophet(
        mcmc_samples=100,
        growth="logistic",
        uncertainty_samples=100,
        interval_width=0.9,
        changepoint_range=0.90,
        changepoint_prior_scale=0.01,
        weekly_seasonality=False,
    )
    m.fit(df)

    future = m.make_future_dataframe(periods=60, freq="d")
    future["cap"] = 24.5
    future["floor"] = 0
    forecast = m.predict(future)
    psf = m.predictive_samples(future)
    # m.plot(psf).show()

    forecast.to_csv(
        f"c:\\users\\uxkp\\desktop\\{timey}_{fcst_type}_forecast_{location}_{timestring}.csv",
        index=False)

    dcv = cross_validation(m,
                           initial="30 days",
                           period="7 days",
                           horizon="60 days")

    performance_metrics_results = performance_metrics(dcv)
    performance_metrics_results.to_csv(
        f"c:\\users\\uxkp\\desktop\\{timey}_{fcst_type}_performance_metrics_{location}_{timestring}.csv",
        index=False)

    fig1 = plot_cross_validation_metric(dcv, metric="mape")
    plt.savefig(
        f"c:\\users\\uxkp\\desktop\\{timey}_{fcst_type}_cross validation_{location}_{timestring}.png",
        format="png")
    fig2 = m.plot(forecast, xlabel=f"Time Series Forecast")
    plt.savefig(
        f"c:\\users\\uxkp\\desktop\\{timey}_{fcst_type}_forecast_{location}_{timestring}.png",
        format="png")
    fig3 = m.plot_components(forecast)
    a = add_changepoints_to_plot(fig3.gca(), m, forecast)
    plt.savefig(
        f"c:\\users\\uxkp\\desktop\\{timey}_{fcst_type}_components_{location}_{timestring}.png",
        format="png")
    fig1.show()
    fig2.show()
    fig3.show()