Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project : ORIX Sheet9# File : Packages/SheetAnalytics/Tests/StatisticsTests/Generators/gen_timeseries.py10# Description : Reference values for Statistics/TimeSeries: differencing,11# ACF/PACF, exponential smoothing, ARIMA (exact MLE),12# GARCH(1,1) and the augmented Dickey-Fuller test.13# Module : StatisticsTests14# Repository : https://git.spboucher.ai/orix-sheet15# Created : 2026-11-2116# Last Modified : 2026-11-2117#18# =============================================================================19# AUTHOR & PROFESSIONAL AFFILIATION20# =============================================================================21#22# Author : Simon-Pierre Boucher23# Position : Professor of Finance24# Institution : Université du Québec en Outaouais (UQO)25# Department : Département des sciences administratives26# Organization : SP Boucher27#28# Website : https://spboucher.ai29# Git : https://git.spboucher.ai30#31# =============================================================================32# OFFICIAL CONTACT DIRECTORY33# =============================================================================34#35# General : info@spboucher.ai36# Professional : contact@spboucher.ai37# Direct : simon@spboucher.ai38# Research : research@spboucher.ai39# Development : dev@spboucher.ai40# Support : support@spboucher.ai41# Security/Admin : admin@spboucher.ai42# Notifications : notifications@spboucher.ai43#44# Preferred Contact: support@spboucher.ai45#46# =============================================================================47# COPYRIGHT & INTELLECTUAL PROPERTY48# =============================================================================49#50# Copyright (c) 202651# Simon-Pierre Boucher52#53# All rights reserved where applicable.54#55# This file is governed by the applicable project license,56# copyright notices, institutional agreements and relevant law.57#58# Unless expressly authorized by the applicable license or59# rights holder, copying, redistribution, sublicensing,60# publication and commercial exploitation are not permitted.61#62# Existing third-party rights and notices remain unaffected.63#64# =============================================================================65# CONFIDENTIALITY & SECURITY66# =============================================================================67#68# Classification : Public69# Distribution : Public repository; use is governed by the project license70# Security Contact : admin@spboucher.ai71#72# Access to this source does not independently grant permission73# to redistribute or disclose confidential materials.74#75# Do not embed credentials, private keys, access tokens,76# certificates or confidential personal information.77#78# =============================================================================79# LICENSE & MAINTENANCE80# =============================================================================81#82# License : None specified (no license file in this repository)83# Maintainer : Simon-Pierre Boucher84# Technical Contact: dev@spboucher.ai85#86# =============================================================================87# END OF SP BOUCHER SOURCE HEADER88# =============================================================================8990"""Reference values for Statistics/TimeSeries: differencing, ACF/PACF, exponential smoothing,91ARIMA (exact MLE), GARCH(1,1) and the augmented Dickey-Fuller test.92References: numpy, statsmodels 0.14 (tsa.stattools, holtwinters, exponential_smoothing.ets, arima), arch 8."""93import types94import warnings9596import numpy as np97from arch import arch_model98from statsmodels.tsa.arima.model import ARIMA99from statsmodels.tsa.arima_process import arma2ma100from statsmodels.tsa.exponential_smoothing.ets import ETSResults101from statsmodels.tsa.holtwinters import ExponentialSmoothing102from statsmodels.tsa.stattools import acf, adfuller, pacf103104from gen_common import dump105106warnings.simplefilter("ignore")107rng = np.random.default_rng(20261121)108out = {}109110# --- series ---------------------------------------------------------------------------------111n = 240112e = rng.normal(size=n + 50)113ar = np.zeros(n + 50)114for t in range(2, n + 50):115 ar[t] = 0.6 * ar[t - 1] - 0.25 * ar[t - 2] + e[t] + 0.4 * e[t - 1]116arma = 5.0 + ar[50:]117walk = 20 + np.cumsum(0.15 + rng.normal(size=200) + 0.5 * np.r_[0, rng.normal(size=199)])118tt = np.arange(96)119seasonal = 100 + 1.2 * tt + 12 * np.sin(2 * np.pi * tt / 12) + 5 * np.cos(2 * np.pi * tt / 6) + rng.normal(scale=3, size=96)120seasonal_mul = (80 + 0.9 * tt) * (1 + 0.25 * np.sin(2 * np.pi * tt / 4)) * np.exp(rng.normal(scale=0.03, size=96))121trendy = 50 + 0.8 * np.arange(60) + np.cumsum(rng.normal(scale=0.6, size=60)) + rng.normal(scale=1.5, size=60)122flat = 30 + rng.normal(scale=2, size=50) + np.r_[np.zeros(25), 4 * np.ones(25)]123out["series"] = {"arma": arma, "walk": walk, "seasonal": seasonal, "seasonal_mul": seasonal_mul, "trendy": trendy, "flat": flat}124125# --- differencing ---------------------------------------------------------------------------126out["diff"] = {127 "d1": np.diff(seasonal), "d2": np.diff(seasonal, n=2),128 "s12": seasonal[12:] - seasonal[:-12], "s12_d1": np.diff(seasonal[12:] - seasonal[:-12]),129}130131# --- ACF / PACF -----------------------------------------------------------------------------132a, ci = acf(arma, nlags=20, alpha=0.05, adjusted=False, fft=False)133out["acf"] = {"values": a, "lower": ci[:, 0], "upper": ci[:, 1]}134p_ld, pci = pacf(arma, nlags=15, method="ldb", alpha=0.05)135out["pacf"] = {"ldb": p_ld, "ywm": pacf(arma, nlags=15, method="ywm"), "lower": pci[:, 0], "upper": pci[:, 1]}136out["acf_default_lags"] = len(acf(arma)) - 1137a2 = acf(seasonal, nlags=30, adjusted=False, fft=False)138out["acf_seasonal"] = a2139140# --- exponential smoothing: fixed parameters -------------------------------------------------141def hw(y, trend, damped, seas, m, params, known=None):142 kw = dict(trend=trend, damped_trend=damped, seasonal=seas, seasonal_periods=m if seas else None)143 if known is None:144 mod = ExponentialSmoothing(y, initialization_method="legacy-heuristic", **kw)145 else:146 mod = ExponentialSmoothing(y, initialization_method="known", initial_level=known["level"],147 initial_trend=known.get("trend") if trend else None,148 initial_seasonal=known.get("seasonal") if seas else None, **kw)149 fit_kw = dict(smoothing_level=params["alpha"], optimized=False)150 if trend:151 fit_kw["smoothing_trend"] = params["beta"]152 if seas:153 fit_kw["smoothing_seasonal"] = params["gamma"]154 if damped:155 fit_kw["damping_trend"] = params["phi"]156 r = mod.fit(**fit_kw)157 return {158 "trend": "damped" if damped else ("add" if trend else "none"), "seasonal": seas or "none", "period": m,159 "params": params, "known": known, "fitted": r.fittedvalues, "level": r.level,160 "trend_states": r.trend if trend else [], "season_states": r.season if seas else [],161 "sse": r.sse, "aic": r.aic, "bic": r.bic, "forecast": r.forecast(15),162 "l0": r.params["initial_level"], "b0": r.params["initial_trend"] if trend else None,163 "s0": r.params["initial_seasons"] if seas else [],164 }165166167ets = {}168ets["ses"] = {"y": "flat", **hw(flat, None, False, None, 1, {"alpha": 0.35})}169ets["ses_known"] = {"y": "flat", **hw(flat, None, False, None, 1, {"alpha": 0.8}, {"level": 28.5})}170ets["holt"] = {"y": "trendy", **hw(trendy, "add", False, None, 1, {"alpha": 0.6, "beta": 0.15})}171ets["holt_known"] = {"y": "trendy", **hw(trendy, "add", False, None, 1, {"alpha": 0.25, "beta": 0.4}, {"level": 49.0, "trend": 1.1})}172ets["damped"] = {"y": "trendy", **hw(trendy, "add", True, None, 1, {"alpha": 0.5, "beta": 0.2, "phi": 0.9})}173ets["hw_add"] = {"y": "seasonal", **hw(seasonal, "add", False, "add", 12, {"alpha": 0.3, "beta": 0.05, "gamma": 0.2})}174ets["hw_mul"] = {"y": "seasonal_mul", **hw(seasonal_mul, "add", False, "mul", 4, {"alpha": 0.4, "beta": 0.1, "gamma": 0.3})}175ets["hw_add_damped"] = {"y": "seasonal", **hw(seasonal, "add", True, "add", 12, {"alpha": 0.45, "beta": 0.1, "gamma": 0.15, "phi": 0.95})}176ets["hw_mul_damped"] = {"y": "seasonal_mul", **hw(seasonal_mul, "add", True, "mul", 4, {"alpha": 0.2, "beta": 0.3, "gamma": 0.5, "phi": 0.85})}177ets["seasonal_no_trend_add"] = {"y": "seasonal", **hw(seasonal, None, False, "add", 12, {"alpha": 0.5, "gamma": 0.25})}178ets["seasonal_no_trend_mul"] = {"y": "seasonal_mul", **hw(seasonal_mul, None, False, "mul", 4, {"alpha": 0.6, "gamma": 0.1})}179ets["hw_add_known"] = {"y": "seasonal", **hw(seasonal, "add", False, "add", 12, {"alpha": 0.2, "beta": 0.1, "gamma": 0.4},180 {"level": 101.0, "trend": 1.0, "seasonal": list(np.round(12 * np.sin(2 * np.pi * np.arange(12) / 12), 3))})}181out["ets_fixed"] = ets182183# --- exponential smoothing: optimised SSE (statsmodels with the same fixed initial states) ----184opt = {}185for key, (y, trend, damped, seas, m) in {186 "ses": (flat, None, False, None, 1), "holt": (trendy, "add", False, None, 1), "damped": (trendy, "add", True, None, 1),187 "hw_add": (seasonal, "add", False, "add", 12), "hw_mul": (seasonal_mul, "add", False, "mul", 4),188 "seasonal_only": (seasonal, None, False, "add", 12),189}.items():190 mod = ExponentialSmoothing(y, trend=trend, damped_trend=damped, seasonal=seas, seasonal_periods=m if seas else None,191 initialization_method="legacy-heuristic")192 r = mod.fit()193 opt[key] = {"sse": r.sse, "alpha": r.params["smoothing_level"], "beta": r.params["smoothing_trend"],194 "gamma": r.params["smoothing_seasonal"], "phi": r.params["damping_trend"]}195out["ets_opt"] = opt196197# --- class-1 prediction variance multipliers (statsmodels ETSResults closed forms) ------------198def relvar(short, alpha, beta=None, gamma=None, phi=None, m=1, hmax=30):199 ns = types.SimpleNamespace(smoothing_level=alpha, smoothing_trend=beta, smoothing_seasonal=gamma, damping_trend=phi,200 has_trend=beta is not None, has_seasonal=gamma is not None, damped_trend=phi is not None,201 seasonal_periods=m, model=types.SimpleNamespace(short_name=short))202 return [float(ETSResults._relative_forecast_variance(ns, h)) for h in range(1, hmax + 1)]203204205# beta/gamma here are the state-space parameters: beta_ss = alpha * beta_recursion, gamma_ss = gamma_recursion206out["ets_relvar"] = {207 "ANN": {"alpha": 0.35, "v": relvar("ANN", 0.35)},208 "AAN": {"alpha": 0.6, "beta": 0.15, "v": relvar("AAN", 0.6, beta=0.6 * 0.15)},209 "AAdN": {"alpha": 0.5, "beta": 0.2, "phi": 0.9, "v": relvar("AAdN", 0.5, beta=0.5 * 0.2, phi=0.9)},210 "ANA": {"alpha": 0.5, "gamma": 0.25, "m": 12, "v": relvar("ANA", 0.5, gamma=0.25, m=12)},211 "AAA": {"alpha": 0.3, "beta": 0.05, "gamma": 0.2, "m": 12, "v": relvar("AAA", 0.3, beta=0.3 * 0.05, gamma=0.2, m=12)},212 "AAdA": {"alpha": 0.45, "beta": 0.1, "gamma": 0.15, "phi": 0.95, "m": 12,213 "v": relvar("AAdA", 0.45, beta=0.45 * 0.1, gamma=0.15, phi=0.95, m=12)},214}215216# --- ARIMA: exact likelihood at fixed parameters ----------------------------------------------217def arima_fixed(y, order, const, params, name):218 p, d, q = order219 w = np.diff(y, n=d) if d else np.asarray(y)220 mod = ARIMA(w, order=(p, 0, q), trend="c" if const else "n")221 res = mod.smooth(params)222 fc = res.get_forecast(12)223 o = {"series": name, "order": [p, d, q], "mean": params[0] if const else None,224 "ar": params[(1 if const else 0):(1 if const else 0) + p], "ma": params[(1 if const else 0) + p:-1],225 "sigma2": params[-1], "loglike": mod.loglike(params), "resid": res.resid,226 "aic": res.aic, "bic": res.bic, "w_forecast": fc.predicted_mean, "w_se": fc.se_mean}227 if d:228 # full-model forecasts of y (statsmodels' approximate diffuse initialisation; loose reference)229 full = ARIMA(y, order=(p, d, q), trend=("t" if const else "n") if d == 1 else "n")230 if d == 1 or not const:231 fr = full.smooth(params).get_forecast(12)232 o["y_forecast"] = fr.predicted_mean233 o["y_se"] = fr.se_mean234 # large-sample standard errors from the psi-weights of the integrated model235 arpoly = np.r_[1, -np.asarray(o["ar"], dtype=float)]236 for _ in range(d):237 arpoly = np.convolve(arpoly, [1, -1])238 psi = arma2ma(arpoly, np.r_[1, np.asarray(o["ma"], dtype=float)], lags=12)239 o["psi_se"] = np.sqrt(params[-1] * np.cumsum(psi ** 2))240 return o241242243fixed = [244 arima_fixed(arma, (1, 0, 0), True, [5.1, 0.55, 1.2], "arma"),245 arima_fixed(arma, (2, 0, 1), True, [4.9, 0.5, -0.2, 0.35, 1.05], "arma"),246 arima_fixed(arma, (0, 0, 2), False, [0.3, 0.2, 30.0], "arma"),247 arima_fixed(arma, (3, 0, 2), True, [5.0, 0.3, 0.1, -0.2, 0.5, 0.25, 0.9], "arma"),248 arima_fixed(walk, (1, 1, 1), False, [0.3, 0.4, 1.3], "walk"),249 arima_fixed(walk, (2, 1, 0), True, [0.2, 0.25, -0.1, 1.5], "walk"),250 arima_fixed(walk, (0, 2, 1), False, [-0.7, 1.6], "walk"),251 arima_fixed(walk, (0, 1, 0), False, [1.8], "walk"),252]253out["arima_fixed"] = fixed254255# --- ARIMA: estimates --------------------------------------------------------------------------256def arima_est(y, order, const, name):257 p, d, q = order258 w = np.diff(y, n=d) if d else np.asarray(y)259 mod = ARIMA(w, order=(p, 0, q), trend="c" if const else "n")260 res = mod.fit(cov_type="approx")261 return {"series": name, "order": [p, d, q], "const": const, "params": res.params, "bse": res.bse,262 "loglike": res.llf, "aic": res.aic, "bic": res.bic}263264265out["arima_est"] = [266 arima_est(arma, (1, 0, 0), True, "arma"),267 arima_est(arma, (2, 0, 1), True, "arma"),268 arima_est(arma, (0, 0, 1), True, "arma"),269 arima_est(walk, (0, 1, 1), False, "walk"),270 arima_est(walk, (1, 1, 0), True, "walk"),271 arima_est(arma, (1, 0, 1), True, "arma"),272 arima_est(walk, (0, 1, 2), True, "walk"),273]274275# --- GARCH(1,1) ---------------------------------------------------------------------------------276m = 1500277z = rng.normal(size=m + 200)278r = np.zeros(m + 200)279s2 = 1.0280prev = 0.0281for t in range(m + 200):282 s2 = 0.08 + 0.1 * prev ** 2 + 0.85 * s2283 prev = np.sqrt(s2) * z[t]284 r[t] = 0.05 + prev285ret = r[200:]286g = {"returns": ret}287for mean, key in [("Constant", "constant"), ("Zero", "zero")]:288 am = arch_model(ret, mean=mean, vol="GARCH", p=1, q=1, rescale=False)289 cases = []290 for prm in ([0.04, 0.1, 0.08, 0.88], [0.0, 0.3, 0.2, 0.6], [0.1, 0.05, 0.02, 0.97]):291 pv = prm if mean == "Constant" else prm[1:]292 fx = am.fix(pv)293 cases.append({"params": prm, "loglike": fx.loglikelihood, "variance": fx.conditional_volatility ** 2,294 "forecast": fx.forecast(horizon=10).variance.values[-1]})295 fit = am.fit(disp="off", tol=1e-12, options={"maxiter": 2000, "ftol": 1e-14})296 g[key] = {"fixed": cases, "fit_params": fit.params.values, "fit_loglike": fit.loglikelihood, "aic": fit.aic, "bic": fit.bic,297 "fit_forecast": fit.forecast(horizon=10).variance.values[-1]}298out["garch"] = g299300# --- augmented Dickey-Fuller ---------------------------------------------------------------------301trend_stat = 3 + 0.05 * np.arange(180) + 0.5 * rng.normal(size=180) + np.r_[0, 0.4 * rng.normal(size=179)]302series = {"walk": walk, "arma": arma, "trend_stationary": trend_stat, "seasonal": seasonal}303adf = []304for name, x in series.items():305 for reg in ["n", "c", "ct"]:306 for sel in [("fixed", 0), ("fixed", 1), ("fixed", 4), ("aic", None), ("bic", None), ("aic", 6)]:307 if sel[0] == "fixed":308 res = adfuller(x, maxlag=sel[1], regression=reg, autolag=None)309 stat, pv, lag, nobs, crit = res310 ic = None311 else:312 stat, pv, lag, nobs, crit, ic = adfuller(x, maxlag=sel[1], regression=reg, autolag=sel[0].upper())313 adf.append({"series": name, "regression": reg, "select": sel[0], "maxlag": sel[1], "stat": stat, "p": pv,314 "lag": lag, "nobs": nobs, "crit": [crit["1%"], crit["5%"], crit["10%"]], "ic": ic})315out["adf_series"] = {"trend_stationary": trend_stat}316out["adf"] = adf317318dump("timeseries", out)319