← Back to case study

PROJECT 048 · PYTHON SOURCE

Energy concentration in high-load intervals

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': 48, 'dataset': 'energy', 'title': 'Energy concentration in high-load intervals', 'question': 'What fraction of appliance energy occurs in the highest-load recorded intervals?', 'method': 'Rank intervals by appliance Wh and compute concentration at prespecified fractions.', 'action': 'Investigate the appliances responsible for high-load intervals before estimating flexible energy.', '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()
    s=df.appliances.sort_values(ascending=False)
    t=pd.DataFrame([{'top_interval_fraction':f,'intervals':int(np.ceil(len(s)*f)),'energy_share':s.head(int(np.ceil(len(s)*f))).sum()/s.sum()} for f in [.01,.05,.1,.2,.5,1.]])
    out=result(t,'top_interval_fraction','energy_share','Share of appliance energy','High observed load is not necessarily waste or shiftable demand.')
    return out

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