← Back to case study

PROJECT 050 · PYTHON SOURCE

One-hour-ahead appliance forecast

Study-specific code. Shared modules, dependency versions, and reproduction instructions are included in all project files.

Download Python file ↓
from pathlib import Path
import sys
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
import numpy as np
import pandas as pd
from scipy.stats import spearmanr
from sklearn.decomposition import PCA
from sklearn.ensemble import IsolationForest
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, precision_score, recall_score
from portfolio.data import load
from portfolio.constants import *
from portfolio.methods import result, sql, rates, associations, distribution, regression, classification, cluster
from portfolio.engine import execute

META = {'id': 50, 'dataset': 'energy', 'title': 'One-hour-ahead appliance forecast', 'question': 'Can lagged energy and known calendar features estimate appliance use an hour ahead?', 'method': 'Chronological regression with clock-time lags at 1, 2 and 24 hours.', 'action': 'Measure reporting latency and test on a later season before using the forecast operationally.', 'limitations': 'Measurements come from one home over a limited period. Energy is Wh per recorded 10-minute interval. This is not a representative household sample; tariffs and occupancy labels are unavailable. '}

def analyze():
    df = load('energy').copy()
    d=df.sort_values('date').set_index('date').reindex(pd.date_range(df.date.min(),df.date.max(),freq='10min'))
    for lag in [6,12,144]: d[f'lag_{lag}']=d.appliances.shift(lag)
    d['prior_hour_mean']=d.appliances.shift(6).rolling(6).mean()
    d=d.iloc[144:].dropna(subset=['appliances','row_id']).reset_index(drop=True)
    features=['hour','weekday','lag_6','lag_12','lag_144','prior_hour_mean']
    t,p,e=regression(d,features,'appliances',split='ordered')
    out=result(t,'model','mae','Wh MAE','Forecast origin is one hour before each target interval, assuming the latest lagged interval has finalized. Later test predictions can use already observed earlier actuals.',extra={'predictions':p},evaluation=e)
    return out

if __name__ == "__main__":
    execute(Path(__file__).parent, META, analyze)