← Back to case study

PROJECT 052 · PYTHON SOURCE

High-load interval warning benchmark

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': 52, 'dataset': 'energy', 'title': 'High-load interval warning benchmark', 'question': 'Can past appliance use rank unusually high-load intervals one hour ahead?', 'method': 'Threshold defined from the training prefix only, with chronological classification.', 'action': 'Choose warning costs and evaluate on a fresh period before defining an operational alert.', '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').copy()
    assert d.date.diff().dropna().eq(pd.Timedelta(minutes=10)).all()
    for lag in [6,12,144]: d[f'lag_{lag}']=d.appliances.shift(lag)
    d=d.iloc[144:].copy();cut=int(len(d)*.8);threshold=d.appliances.iloc[:cut].quantile(.9)
    d['high_load']=d.appliances.gt(threshold).astype(int)
    t,p,e=classification(d,['hour','weekday','lag_6','lag_12','lag_144'],'high_load',split='ordered');e['training_threshold_wh']=float(threshold)
    out=result(t,'model','average_precision','Average precision',f'High load is strictly greater than the training 90th percentile ({threshold:g} Wh). It is a study threshold, not a safety limit.',extra={'predictions':p},evaluation=e)
    return out

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