Commit 75c27703 authored by Anna Warno's avatar Anna Warno
Browse files

nbeats confidence intervals

parent 72ea6e67
......@@ -96,18 +96,18 @@ def main():
"2", StartForecastingListener(start_conn.conn, START_TOPIC)
)
msg1 = Msg()
msg1.body = '[{"metric": "cpu_usage", "level": 3, "publish_rate": 60000}]'
msg2 = Msg()
msg2.body = (
"{"
+ f'"metrics": ["cpu_usage"],"timestamp": {int(time.time())}, "epoch_start": {int(time.time()) + 30}, "number_of_forward_predictions": 8,"prediction_horizon": 60'
+ "}"
)
print(msg1)
StartListener(start_conn.conn, START_APP_TOPIC).on_message(msg1)
StartForecastingListener(start_conn.conn, START_APP_TOPIC).on_message(msg2)
# msg1 = Msg()
# msg1.body = '[{"metric": "cpu_usage", "level": 3, "publish_rate": 60000}]'
# msg2 = Msg()
# msg2.body = (
# "{"
# + f'"metrics": ["cpu_usage"],"timestamp": {int(time.time())}, "epoch_start": {int(time.time()) + 30}, "number_of_forward_predictions": 8,"prediction_horizon": 60'
# + "}"
# )
# print(msg1)
# StartListener(start_conn.conn, START_APP_TOPIC).on_message(msg1)
# StartForecastingListener(start_conn.conn, START_APP_TOPIC).on_message(msg2)
while True:
pass
......
from pytorch_forecasting.metrics import RMSE
import yaml
import pandas as pd
import numpy as np
......@@ -7,6 +8,7 @@ import torch
from filelock import FileLock
from src.preprocess_dataset import Dataset
from pytorch_forecasting import NBeats
import scipy.stats as st
import logging
"""Script for nbeats fusion transformer prediction"""
......@@ -74,17 +76,48 @@ def predict(
model.load_state_dict(torch.load(model_path))
prediction_input = prediction_input.to_dataloader(train=False)
prediction = model.predict(prediction_input, mode="raw")["prediction"]
it = iter(prediction_input)
first = next(it)
model.train() # turning on dropout (in order to obtaining different prediction and calculate confidence interval)
predictions_with_dropout = [
model.forward(first[0])["prediction"][-1].item() for _ in range(20)
]
predictions_with_dropout = []
model.train()
model.loss = RMSE()
with torch.no_grad():
for _ in range(20):
for x, _ in prediction_input:
# make prediction
out = model(x) # raw output is dictionary
out = model.transform_output(out).item()
predictions_with_dropout.append(out)
# print(model.to_prediction(model.forward(first[0])), "TRANSFORMED")
conf_intervals = st.t.interval(
alpha=0.95,
df=len(predictions_with_dropout) - 1,
loc=np.mean(predictions_with_dropout),
scale=st.sem(predictions_with_dropout),
)
predicted_values = list(conf_intervals) + [prediction[-1].item()]
predicted_values.sort() # ensure that predictions and confidence intervals are in correct order
print(predicted_values)
msg = {
target_column: {
"metricValue": prediction[-1].item(),
"metricValue": predicted_values[1], # middle predicted value
"level": 1, # TODO
"timestamp": int(time.time()),
"probability": 0.95,
"confidence_interval": [
prediction[-1].item(),
prediction[-1].item(),
predicted_values[0],
predicted_values[-1],
], # quantiles difference
"predictionTime": timestamp + prediction_hor * m // 1000,
"refersTo": "TODO",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment