<jupyter_start><jupyter_text>Baseline Forecasts<jupyter_code>pred_df = pd.concat([ts_train, ts_test])
metric_record = []

ts_train = TimeSeries.from_series(ts_train)
ts_test = TimeSeries.from_series(ts_test)
def eval_model(model, ts_train, ts_test, name=None):
    if name is None:
        name = type(model).__name__
    model.fit(ts_train)
    y_pred = model.predict(len(ts_test))
    return y_pred, {
        "Algorithm": name,
        "MAE": mae(actual_series = ts_test, pred_series = y_pred),
        "MSE": mse(actual_series = ts_test, pred_series = y_pred),
        "MASE": mase(actual_series = ts_test, pred_series = y_pred, insample=ts_train),
        "Forecast Bias": forecast_bias(actual_series = ts_test, pred_series = y_pred)
    }

def eval_model_backtest(model, ts_train, ts_test, name=None):
    if name is None:
        name = type(model).__name__
    ts = ts_train.append(ts_test)
    start = ts_test.time_index.min()
    # model.fit(ts_train)
    y_pred = model.historical_forecasts(ts, start=start, verbose=True)
    return y_pred, {
        "Algorithm": name,
        "MAE": mae(actual_series = ts_test, pred_series = y_pred),
        "MSE": mse(actual_series = ts_test, pred_series = y_pred),
        "MASE": mase(actual_series = ts_test, pred_series = y_pred, insample=ts_train),
        "Forecast Bias": forecast_bias(actual_series = ts_test, pred_series = y_pred)
    }

def format_y_pred(y_pred, name):
    y_pred = y_pred.data_array().to_series()
    y_pred.index = y_pred.index.get_level_values(0)
    y_pred.name = name
    return y_pred
from itertools import cycle
def plot_forecast(pred_df, forecast_columns, forecast_display_names=None):
    if forecast_display_names is None:
        forecast_display_names = forecast_columns
    else:
        assert len(forecast_columns)==len(forecast_display_names)
    mask = ~pred_df[forecast_columns[0]].isnull()
    colors = ["rgba("+",".join([str(c) for c in plotting_utils.hex_to_rgb(c)])+",<alpha>)" for c in px.colors.qualitative.Plotly]
    act_color = colors[0]
    colors = cycle(colors[1:])
    fig = go.Figure()
    fig.add_trace(go.Scatter(x=pred_df[mask].index, y=pred_df[mask].energy_consumption,
                        mode='lines', line = dict(color=act_color.replace("<alpha>", "0.9")),
                        name='Actual Consumption'))
    for col, display_col in zip(forecast_columns,forecast_display_names):
        fig.add_trace(go.Scatter(x=pred_df[mask].index, y=pred_df.loc[mask, col],
                            mode='lines', line = dict(dash='dot', color=next(colors).replace("<alpha>", "1")),
                            name=display_col))
    return fig<jupyter_output><empty_output><jupyter_text>Running Baseline Forecast for all consumers<jupyter_code>lcl_ids = sorted(train_df.LCLid.unique())<jupyter_output><empty_output><jupyter_text>Naive Forecast<jupyter_code>from src.utils.ts_utils import darts_metrics_adapter
name = "Naive"
naive_preds = []
naive_metrics = []
for lcl_id in tqdm(lcl_ids):
    # naive_model = NaiveSeasonal(K=1)
    tr = train_df.loc[train_df.LCLid==lcl_id, ["timestamp","energy_consumption"]].set_index("timestamp")
    ts = test_df.loc[test_df.LCLid==lcl_id, ["timestamp","energy_consumption"]].set_index("timestamp")
    tr_ts = pd.concat([tr, ts])
    tr_ts['naive_predictions'] = tr_ts['energy_consumption'].shift(1)
    y_pred = tr_ts.loc[ts.index].drop(columns="energy_consumption")
    metrics = {
        "Algorithm": name,
        "MAE": darts_metrics_adapter(mae,actual_series = ts, pred_series = y_pred),
        "MSE": darts_metrics_adapter(mse,actual_series = ts, pred_series = y_pred),
        "MASE": darts_metrics_adapter(mase,actual_series = ts, pred_series = y_pred, insample=tr.energy_consumption),
        "Forecast Bias": darts_metrics_adapter(forecast_bias,actual_series = ts, pred_series = y_pred)
    }
    # break
    # y_pred = format_y_pred(y_pred, "naive_predictions").to_frame()
    y_pred['LCLid'] = lcl_id
    metrics["LCLid"] = lcl_id
    y_pred['energy_consumption'] = ts.energy_consumption.values
    naive_preds.append(y_pred)
    naive_metrics.append(metrics)
    # break
naive_pred_df = pd.concat(naive_preds)
naive_pred_df.head()
naive_metric_df = pd.DataFrame(naive_metrics)
naive_metric_df.head()
from src.utils import ts_utils
overall_metrics_naive = {
    "MAE": ts_utils.mae(naive_pred_df["energy_consumption"], naive_pred_df["naive_predictions"]),
    "MSE": ts_utils.mse(naive_pred_df.energy_consumption, naive_pred_df.naive_predictions),
    "meanMASE": naive_metric_df.MASE.mean(),
    "Forecast Bias": ts_utils.forecast_bias_aggregate(naive_pred_df.energy_consumption, naive_pred_df.naive_predictions)
}
overall_metrics_naive<jupyter_output><empty_output><jupyter_text>Seasonal Naive Forecast<jupyter_code>from src.utils.ts_utils import darts_metrics_adapter
name = "Naive"
naive_preds = []
naive_metrics = []
for lcl_id in tqdm(lcl_ids):
    # naive_model = NaiveSeasonal(K=1)
    tr = train_df.loc[train_df.LCLid==lcl_id, ["timestamp","energy_consumption"]].set_index("timestamp")
    ts = test_df.loc[test_df.LCLid==lcl_id, ["timestamp","energy_consumption"]].set_index("timestamp")
    tr_ts = pd.concat([tr, ts])
    tr_ts['naive_predictions'] = tr_ts['energy_consumption'].shift(1)
    y_pred = tr_ts.loc[ts.index].drop(columns="energy_consumption")
    metrics = {
        "Algorithm": name,
        "MAE": darts_metrics_adapter(mae,actual_series = ts, pred_series = y_pred),
        "MSE": darts_metrics_adapter(mse,actual_series = ts, pred_series = y_pred),
        "MASE": darts_metrics_adapter(mase,actual_series = ts, pred_series = y_pred, insample=tr.energy_consumption),
        "Forecast Bias": darts_metrics_adapter(forecast_bias,actual_series = ts, pred_series = y_pred)
    }
    # break
    # y_pred = format_y_pred(y_pred, "naive_predictions").to_frame()
    y_pred['LCLid'] = lcl_id
    metrics["LCLid"] = lcl_id
    y_pred['energy_consumption'] = ts.energy_consumption.values
    naive_preds.append(y_pred)
    naive_metrics.append(metrics)
    # break
naive_pred_df = pd.concat(naive_preds)
naive_pred_df.head()
naive_metric_df = pd.DataFrame(naive_metrics)
naive_metric_df.head()
from src.utils import ts_utils
overall_metrics_naive = {
    "MAE": ts_utils.mae(naive_pred_df["energy_consumption"], naive_pred_df["naive_predictions"]),
    "MSE": ts_utils.mse(naive_pred_df.energy_consumption, naive_pred_df.naive_predictions),
    "meanMASE": naive_metric_df.MASE.mean(),
    "Forecast Bias": ts_utils.forecast_bias_aggregate(naive_pred_df.energy_consumption, naive_pred_df.naive_predictions)
}
overall_metrics_naive<jupyter_output><empty_output><jupyter_text>FFT<jupyter_code>name = "FFT"
fft_preds = []
fft_metrics = []
for lcl_id in tqdm(lcl_ids):
    fft_model = FFT(nr_freqs_to_keep=35, trend="poly", trend_poly_degree=2)
    tr = TimeSeries.from_series(train_df.loc[train_df.LCLid==lcl_id, ["timestamp","energy_consumption"]].set_index("timestamp"))
    ts = TimeSeries.from_series(test_df.loc[test_df.LCLid==lcl_id, ["timestamp","energy_consumption"]].set_index("timestamp"))
    y_pred, metrics = eval_model(fft_model, tr, ts, name=name)
    y_pred = format_y_pred(y_pred, "fft_predictions").to_frame()
    y_pred['LCLid'] = lcl_id
    metrics["LCLid"] = lcl_id
    y_pred['energy_consumption'] = ts.data_array().to_series().values
    fft_preds.append(y_pred)
    fft_metrics.append(metrics)
fft_pred_df = pd.concat(fft_preds)
fft_pred_df.head()
fft_metric_df = pd.DataFrame(fft_metrics)
fft_metric_df.head()
actual_series = TimeSeries.from_values(fft_pred_df.energy_consumption.values)
pred_series = TimeSeries.from_values(fft_pred_df.fft_predictions.values)
overall_metrics_fft = {
    "MAE": mae(actual_series = actual_series, pred_series = pred_series),
    "MSE": mse(actual_series = actual_series, pred_series = pred_series),
    "meanMASE": fft_metric_df.MASE.mean(),
    "Forecast Bias": ope(actual_series = actual_series, pred_series = pred_series)
}
overall_metrics_fft<jupyter_output><empty_output><jupyter_text>Evaluation of Baseline Forecast<jupyter_code>p_df = pd.DataFrame([overall_metrics_fft, overall_metrics_theta], index=["FFT","Theta"])

p_df.style.format({"MAE": "{:.3f}", 
                          "MSE": "{:.3f}", 
                          "meanMASE": "{:.3f}", 
                          "Forecast Bias": "{:.2f}%"}).highlight_min(color='lightgreen')
baseline_pred_df = theta_pred_df.reset_index().merge(fft_pred_df.reset_index().drop(columns='energy_consumption'), on=['time','LCLid'], how='outer')
baseline_metrics_df = pd.concat([theta_metric_df, fft_metric_df])
baseline_metrics_df.head()
fig = px.histogram(baseline_metrics_df, 
                   x="MASE", 
                   color="Algorithm",
                   pattern_shape="Algorithm", 
                   marginal="box", 
                   nbins=500, 
                   barmode="overlay",
                   histnorm="probability density")
fig = format_plot(fig, xlabel="MASE", ylabel="Probability Density", title="Distribution of MASE in the dataset")
fig.update_layout(xaxis_range=[0,10])
fig.write_image("imgs/chapter_4/mase_dist.png")
fig.show()
fig = px.histogram(baseline_metrics_df, 
                   x="MAE", 
                   color="Algorithm",
                   pattern_shape="Algorithm", 
                   marginal="box", 
                   nbins=100, 
                   barmode="overlay",
                   histnorm="probability density")
fig = format_plot(fig, xlabel="MAE", ylabel="Probability Density", title="Distribution of MAE in the dataset")
fig.write_image("imgs/chapter_4/mae_dist.png")
fig.update_layout(xaxis_range=[0,1.1])
fig.show()
fig = px.histogram(baseline_metrics_df, 
                   x="MSE", 
                   color="Algorithm",
                   pattern_shape="Algorithm", 
                   marginal="box", 
                   nbins=500, 
                   barmode="overlay",
                   histnorm="probability density")
fig = format_plot(fig, xlabel="MSE", ylabel="Probability Density", title="Distribution of MSE in the dataset")
fig.update_layout(xaxis_range=[0,1])
fig.write_image("imgs/chapter_4/mse_dist.png")
fig.show()
fig = px.histogram(baseline_metrics_df, 
                   x="Forecast Bias", 
                   color="Algorithm",
                   pattern_shape="Algorithm", 
                   marginal="box", 
                   nbins=250,
                   barmode="overlay",
                   histnorm="probability density")
fig = format_plot(fig, xlabel="Forecast Bias", ylabel="Probability Density", title="Distribution of Forecast Bias in the dataset")
fig.update_layout(xaxis_range=[-200,200])
fig.write_image("imgs/chapter_4/bias_dist.png")
fig.show()<jupyter_output><empty_output><jupyter_text>Saving the Baseline Forecasts and Metrics<jupyter_code>os.makedirs("data/london_smart_meters/output", exist_ok=True)
output = Path("data/london_smart_meters/output")
baseline_pred_df.to_pickle(output/"baseline_prediction_df.pkl")
baseline_metrics_df.to_pickle(output/"baseline_metrics_df.pkl")
p_df.to_pickle(output/"baseline_aggregate_metrics.pkl")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12899758129535072, 'fraction_numerical': 0.00429991937651169, 'mean_word_length': 4.19981369352585, 'pattern_counts': {'":': 36, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '412640', 'license': 'MIT', 'n_tokens_mistral': 4034, 'n_tokens_neox': 3917, 'n_words': 547, 'path': 'notebooks/Chapter04/02-Baseline Forecasts using darts-Copy1.ipynb', 'repo_name': 'PacktPublishing/Modern-Time-Series-Forecasting-with-Python-', 'size': '11163'}