SPB Git

Repository Public

gen_fixtures.py 13.7 KB 263 lines python

1#!/usr/bin/env python32# =============================================================================3#                  SP BOUCHER | SOFTWARE & RESEARCH4#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# PROJECT METADATA8# -----------------------------------------------------------------------------9# Project          : ORIX Sheet10# File             : Packages/SheetCharts/Tests/SheetChartsTests/Generators/gen_fixtures.py11# Description      : Reference values for the SheetCharts numeric tests,12#                    computed with numpy / scipy / matplotlib.13# Module           : SheetChartsTests14# 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 the SheetCharts numeric tests, computed with numpy / scipy / matplotlib.9192Usage: /opt/homebrew/bin/python3 gen_fixtures.py <Fixtures directory>93Writes reference.json. The script is deterministic (fixed seeds). Re-run it only to extend the fixtures;94the Swift tests compare the engine against the stored numbers.95"""96import json, sys, os, datetime97import numpy as np98import scipy99from scipy import stats100import matplotlib101matplotlib.use("Agg")102from matplotlib import ticker, cbook103import matplotlib.pyplot as plt104105out_dir = sys.argv[1]106rng = np.random.default_rng(20261121)107ref = {"versions": {"numpy": np.__version__, "scipy": scipy.__version__, "matplotlib": matplotlib.__version__}}108109datasets = {110    "normal500": rng.normal(50, 12, 500),111    "lognormal200": rng.lognormal(1.0, 0.8, 200),112    "small7": np.array([2.0, 4.0, 4.0, 5.0, 7.0, 9.0, 21.0]),113    "integers60": rng.integers(0, 12, 60).astype(float),114    "uniform33": rng.uniform(-3, 8, 33),115    "pair": np.array([1.5, 4.0]),116    "tiny": rng.normal(0, 1e-9, 40),117    "huge": rng.normal(0, 1e9, 64),118}119ref["datasets"] = {k: [float(x) for x in v] for k, v in datasets.items()}120121# --- ticks: matplotlib MaxNLocator with steps 1-2-5-10 ------------------------------------------------------122tick_cases = [(0, 1, 5), (0, 100, 6), (-11.8, 18.9, 7), (3.2, 97.5, 5), (0.0012, 0.0087, 6), (-1e-12, 1e-12, 4),123              (1e-12, 1e12, 6), (123456, 987654, 8), (-0.5, 0.5, 10), (20, 80, 6), (0, 7, 7), (0.1, 0.9, 4),124              (-273.15, 1064.18, 5), (5.5, 5.9, 3), (0, 250000, 5), (17, 23, 2), (-8.1, 6.5, 12), (0, 0.3, 3)]125ticks = []126for lo, hi, n in tick_cases:127    loc = ticker.MaxNLocator(nbins=n, steps=[1, 2, 5, 10])128    ticks.append({"min": lo, "max": hi, "intervals": n, "ticks": [float(t) for t in loc.tick_values(lo, hi)]})129ref["ticks"] = ticks130131# --- log ticks: integer powers inside the range -------------------------------------------------------------132logs = []133for lo, hi, base in [(0.03, 4800, 10), (1, 1e6, 10), (1e-12, 1e12, 10), (3, 900, 2), (0.5, 64, 2), (120, 80000, 10)]:134    e0 = int(np.ceil(np.log(lo) / np.log(base) - 1e-9)); e1 = int(np.floor(np.log(hi) / np.log(base) + 1e-9))135    logs.append({"min": lo, "max": hi, "base": base, "powers": [float(base) ** e for e in range(e0, e1 + 1)]})136ref["logTicks"] = logs137138# --- calendar ticks: serial numbers (1900 system; valid after 1900-03-01) ------------------------------------139epoch = datetime.date(1899, 12, 30)140def serial(d): return (d - epoch).days141dates = []142for (a, b, unit, step) in [((2024, 1, 15), (2025, 3, 2), "month", 1), ((2019, 11, 3), (2031, 2, 1), "year", 1),143                           ((2023, 12, 20), (2024, 3, 10), "week", 1), ((2000, 2, 20), (2000, 3, 4), "day", 1),144                           ((1995, 6, 1), (2026, 6, 1), "year", 5), ((2024, 2, 1), (2026, 1, 31), "month", 3)]:145    da, db = datetime.date(*a), datetime.date(*b)146    vals = []147    d = da148    while d <= db:149        if unit == "day": ok = True150        elif unit == "week": ok = d.weekday() == 0151        elif unit == "month": ok = d.day == 1 and (d.month - 1) % step == 0152        else: ok = d.day == 1 and d.month == 1 and d.year % step == 0153        if ok: vals.append(serial(d))154        d += datetime.timedelta(days=1)155    dates.append({"min": serial(da), "max": serial(db), "unit": unit, "step": step, "ticks": vals,156                  "labels": [datetime.date.fromordinal(epoch.toordinal() + v).isoformat() for v in vals]})157ref["dateTicks"] = dates158ref["dateSerials"] = [{"iso": d.isoformat(), "serial": serial(d)} for d in159                      [datetime.date(1900, 3, 1), datetime.date(1999, 12, 31), datetime.date(2000, 2, 29), datetime.date(2024, 2, 29), datetime.date(2030, 1, 1)]]160161# --- histogram bins -----------------------------------------------------------------------------------------162bins = []163for name, x in datasets.items():164    for rule, np_rule in [("auto", "auto"), ("freedmanDiaconis", "fd"), ("sturges", "sturges"), ("scott", "scott"), ("fixedCount", 13)]:165        edges = np.histogram_bin_edges(x, bins=np_rule)166        counts, _ = np.histogram(x, bins=edges)167        dens, _ = np.histogram(x, bins=edges, density=True)168        bins.append({"data": name, "rule": rule, "edges": [float(e) for e in edges], "counts": [int(c) for c in counts], "density": [float(d) for d in dens]})169const = np.full(9, 3.25)170edges = np.histogram_bin_edges(const, bins="auto")171bins.append({"data": "const", "rule": "auto", "edges": [float(e) for e in edges], "counts": [int(c) for c in np.histogram(const, bins=edges)[0]], "density": []})172ref["datasets"]["const"] = [float(v) for v in const]173ref["bins"] = bins174175# --- quantiles ----------------------------------------------------------------------------------------------176methods = {"inclusive": "linear", "exclusive": "weibull", "averaged": "averaged_inverted_cdf", "medianUnbiased": "median_unbiased"}177probs = [0, 0.05, 0.1, 0.25, 1 / 3, 0.5, 0.6, 0.75, 0.9, 0.99, 1]178quant = []179qsets = dict(datasets)180for n in [1, 2, 3, 4, 5, 8, 11]:181    qsets["n%d" % n] = np.round(rng.normal(10, 4, n), 3)182for name, x in qsets.items():183    ref["datasets"][name] = [float(v) for v in x]184    for m, npm in methods.items():185        quant.append({"data": name, "method": m, "p": probs, "q": [float(np.quantile(x, p, method=npm)) for p in probs]})186ref["quantiles"] = quant187188# --- box plot statistics (matplotlib, Tukey whiskers, linear quartiles) ---------------------------------------189box = []190for name in ["normal500", "lognormal200", "small7", "integers60", "uniform33", "pair"]:191    s = cbook.boxplot_stats(datasets[name], whis=1.5)[0]192    box.append({"data": name, "q1": float(s["q1"]), "median": float(s["med"]), "q3": float(s["q3"]), "whiskerLow": float(s["whislo"]),193                "whiskerHigh": float(s["whishi"]), "outliers": sorted(float(f) for f in s["fliers"]), "mean": float(s["mean"])})194ref["box"] = box195196# --- Gaussian KDE -------------------------------------------------------------------------------------------197kde = []198for name in ["normal500", "lognormal200", "small7", "uniform33", "tiny", "huge"]:199    x = datasets[name]200    n = len(x); s = np.std(x, ddof=1)201    iqr = np.percentile(x, 75) - np.percentile(x, 25)202    h_scott = s * n ** (-0.2)203    h_silverman = 0.9 * min(s, iqr / 1.34) * n ** (-0.2)204    grid = np.linspace(x.min() - 3 * h_scott, x.max() + 3 * h_scott, 41)205    for rule, h in [("scott", h_scott), ("silverman", h_silverman), ("fixed", 0.5 * h_scott)]:206        # scipy's default bandwidth IS Scott's rule; other bandwidths are passed as a factor of the sample std207        k = stats.gaussian_kde(x) if rule == "scott" else stats.gaussian_kde(x, bw_method=h / s)208        kde.append({"data": name, "rule": rule, "bandwidth": float(h), "grid": [float(g) for g in grid], "density": [float(v) for v in k(grid)]})209ref["kde"] = kde210211# --- trendlines ---------------------------------------------------------------------------------------------212def r2(y, yhat): return float(1 - np.sum((y - yhat) ** 2) / np.sum((y - np.mean(y)) ** 2))213trend = []214x = np.sort(rng.uniform(0.5, 40, 60)); noise = rng.normal(0, 1, 60)215series = {216    "linear": (x, 3.2 * x - 7 + 4 * noise),217    "curved": (x, 0.04 * x ** 3 - 1.5 * x ** 2 + 9 * x + 20 + 12 * noise),218    "growth": (x, 2.5 * np.exp(0.09 * x) * np.exp(0.1 * noise)),219    "power": (x, 4.1 * x ** 0.63 * np.exp(0.07 * noise)),220    "logish": (x, 6.0 * np.log(x) + 2 + 0.5 * noise),221    "years": (np.arange(1990, 2026, dtype=float), 100 + 2.3 * (np.arange(36)) + rng.normal(0, 3, 36)),222    "bigx": (1e6 + np.arange(30, dtype=float) * 250, rng.normal(5, 1, 30) + 0.002 * np.arange(30) * 250),223}224for name, (xs, ys) in series.items():225    entry = {"name": name, "x": [float(v) for v in xs], "y": [float(v) for v in ys], "fits": []}226    for deg in [1, 2, 3, 5]:227        c = np.polyfit(xs, ys, deg)          # descending powers228        entry["fits"].append({"kind": "linear" if deg == 1 else "polynomial", "order": deg, "coefficients": [float(v) for v in c[::-1]],229                              "r2": r2(ys, np.polyval(c, xs)), "probe": [float(np.polyval(c, v)) for v in (xs[0], xs[len(xs) // 2], xs[-1])]})230    if np.all(ys > 0):231        b, a = np.polyfit(xs, np.log(ys), 1)232        entry["fits"].append({"kind": "exponential", "order": 0, "coefficients": [float(np.exp(a)), float(b)], "r2": r2(np.log(ys), a + b * xs), "probe": []})233        b, a = np.polyfit(np.log(xs), np.log(ys), 1)234        entry["fits"].append({"kind": "power", "order": 0, "coefficients": [float(np.exp(a)), float(b)], "r2": r2(np.log(ys), a + b * np.log(xs)), "probe": []})235    a, b = np.polyfit(np.log(xs), ys, 1)236    entry["fits"].append({"kind": "logarithmic", "order": 0, "coefficients": [float(a), float(b)], "r2": r2(ys, a * np.log(xs) + b), "probe": []})237    entry["movingAverage5"] = [float(v) for v in np.convolve(ys, np.ones(5) / 5, mode="valid")]238    trend.append(entry)239ref["trend"] = trend240241# --- stacking -----------------------------------------------------------------------------------------------242m = np.round(rng.normal(2, 6, (4, 7)), 2)243pos = np.where(m > 0, m, 0).cumsum(axis=0); neg = np.where(m < 0, m, 0).cumsum(axis=0)244top = np.where(m >= 0, pos, neg)245pct = m / np.abs(m).sum(axis=0) * 100246ppos = np.where(pct > 0, pct, 0).cumsum(axis=0); pneg = np.where(pct < 0, pct, 0).cumsum(axis=0)247ref["stack"] = {"values": m.tolist(), "top": top.tolist(), "base": (top - m).tolist(),248                "percentTop": np.where(pct >= 0, ppos, pneg).tolist(), "percentBase": (np.where(pct >= 0, ppos, pneg) - pct).tolist()}249250# --- contours: total iso-line length from matplotlib (contourpy) --------------------------------------------251g = np.linspace(-3, 3, 41)252X, Y = np.meshgrid(g, g)253Z = 3 * (1 - X) ** 2 * np.exp(-X ** 2 - (Y + 1) ** 2) - 10 * (X / 5 - X ** 3 - Y ** 5) * np.exp(-X ** 2 - Y ** 2) - 1 / 3 * np.exp(-(X + 1) ** 2 - Y ** 2)254levels = [-4.0, -1.5, 0.5, 2.0, 5.0]255cs = plt.contour(Z, levels=levels)     # coordinates are grid indices, like the engine's256lengths = []257for segs in cs.allsegs:258    lengths.append(float(sum(np.sum(np.hypot(np.diff(s[:, 0]), np.diff(s[:, 1]))) for s in segs)))259ref["contour"] = {"z": Z.tolist(), "levels": levels, "lengths": lengths, "segments": [len(s) for s in cs.allsegs]}260261json.dump(ref, open(os.path.join(out_dir, "reference.json"), "w"))262print("wrote reference.json with", ", ".join(k for k in ref))263
Files main
Menu

Theme