pred = pd.DataFrame(means, index=train.index, columns=train.columns) smse = ((pred - test)**2).mean(axis=0) / ( (train.mean(axis=0) - test)**2).mean(axis=0) # Report average SMSE. wbml.out.kv("SMSEs", smse.dropna()) wbml.out.kv("Average SMSE", smse.mean()) # Compute PPLP. x_test = np.array(test.index) y_test = np.array(test.reindex(train.columns, axis=1)) logprob = model.logpdf(torch.tensor(x_test), torch.tensor(normaliser.normalise(y_test))) logdet = normaliser.normalise_logdet(y_test) pplp = logprob + logdet wbml.out.kv("PPLP", pplp / B.length(y_test)) # Plot the result. plt.figure(figsize=(12, 2)) wbml.plot.tex() for i, name in enumerate(test.columns): p = list(train.columns).index(name) # Index of output. plt.subplot(1, 3, i + 1) plt.plot(x, means[:, p], style="pred") plt.fill_between(x, lowers[:, p], uppers[:, p], style="pred") plt.scatter(x, y[:, p], style="train") plt.scatter(test[name].index, test[name], style="test") plt.xlabel("Time (year)") plt.ylabel(name) wbml.plot.tweak(legend=False)
def objective(vs): return -construct_model(vs).logpdf(torch.tensor(x_train), torch.tensor(y_train_norm)) # Perform optimisation. minimise_l_bfgs_b(objective, vs, trace=True, iters=1000) # Print variables. vs.print() # Predict. model = construct_model(vs) model = model.condition(torch.tensor(x_train), torch.tensor(y_train_norm)) means, lowers, uppers = B.to_numpy(model.predict(x_test)) # Compute RMSE. wbml.out.kv("RMSE", B.mean((normaliser.unnormalise(means) - y_test)**2)**0.5) # Compute LML. lml = -objective(vs) + normaliser.normalise_logdet(y_train) wbml.out.kv("LML", lml / B.length(y_train)) # Compute PPLP. logprob = model.logpdf(torch.tensor(x_test), torch.tensor(normaliser.normalise(y_test))) logdet = normaliser.normalise_logdet(y_test) pplp = logprob + logdet wbml.out.kv("PPLP", pplp / B.length(y_test))