← Back to case study

PROJECT 014 · PYTHON SOURCE

Demand persistence over time

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': 14, 'dataset': 'bikes', 'title': 'Demand persistence over time', 'question': 'At which elapsed-time lags are observed hourly rental counts most strongly correlated?', 'method': 'Pairwise-complete autocorrelation on a complete hourly grid.', 'action': 'Use lag structure to propose forecast features and validate them chronologically.', 'limitations': 'Historical system rentals measure realized use, not unmet demand or station inventory. Missing hours are unknown. Weather associations do not establish causal effects. '}

def analyze():
    df = load('bikes').copy()
    s=df.set_index('timestamp').cnt.reindex(pd.date_range(df.timestamp.min(),df.timestamp.max(),freq='h'))
    rows=[]
    for lag in [1,2,3,6,12,24,48,72,168]:
        paired=pd.concat([s,s.shift(lag)],axis=1).dropna()
        rows.append({'lag_hours':lag,'paired_hours':len(paired),'correlation':paired.iloc[:,0].corr(paired.iloc[:,1])})
    t=pd.DataFrame(rows)
    out=result(t,'lag_hours','correlation','Pearson correlation','Seasonality and trend contribute to correlation. This descriptive use of all dates is separate from forecast feature selection.')
    return out

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