"""QE-VINTAGE-01/1.0: invented publication, archive-import and decision clocks. Demo: January industrial production change relative to December in an imaginary country. An invented statistical agency revises its initial estimate after receiving more responses. value is a percentage: 2 means +2%, -1 means -1%. received_at records when the program imports that publication into its data archive. UNAVAILABLE means no supplied version can be used yet; it is not a measured zero or NO_BUY. IT: caso inventato; produzione industriale gennaio/dicembre; value è percentuale. received_at è l’importazione nell’archivio. UNAVAILABLE blocca la decisione per dato assente. ES: caso inventado; producción industrial enero/diciembre; value es un porcentaje. received_at es la importación al archivo. UNAVAILABLE bloquea la decisión por falta del dato. No verified vendor latency, prediction, order execution or return calculation. Python 3.10+. """ import argparse from datetime import datetime import json import math from pathlib import Path def instant(value): if not isinstance(value, str) or not value.endswith('Z'): raise ValueError('UTC_TIMESTAMP_REQUIRED') return datetime.fromisoformat(value[:-1] + '+00:00') def demo(): return {'case': 'QE-VINTAGE-01/1.0', 'synthetic': True, 'versions': [ {'period': '2026-01', 'version': 1, 'value': 2, 'published_at': '2026-02-01T09:00:00Z', 'received_at': '2026-02-01T09:05:00Z'}, {'period': '2026-01', 'version': 2, 'value': -1, 'published_at': '2026-02-03T09:00:00Z', 'received_at': '2026-02-03T09:01:00Z'}], 'decisions': [ {'id': 'D1', 'period': '2026-01', 'at': '2026-02-01T09:02:00Z'}, {'id': 'D2', 'period': '2026-01', 'at': '2026-02-01T09:10:00Z'}, {'id': 'D3', 'period': '2026-01', 'at': '2026-02-03T09:00:00Z'}, {'id': 'D4', 'period': '2026-01', 'at': '2026-02-03T09:02:00Z'}]} def evaluate(data): versions = data['versions']; seen = set() for row in versions: key = (row['period'], row['version']) if key in seen: raise ValueError('DUPLICATE_VERSION') seen.add(key) if type(row['version']) is not int or row['version'] < 1: raise ValueError('VERSION_REQUIRED') if type(row['value']) not in (int, float) or not math.isfinite(row['value']): raise ValueError('FINITE_VALUE_REQUIRED') if instant(row['received_at']) < instant(row['published_at']): raise ValueError('RECEIPT_BEFORE_PUBLICATION') # Version numbers increase with publication time within an observation period. for period in {r['period'] for r in versions}: group = sorted((r for r in versions if r['period'] == period), key=lambda r:r['version']) if any(instant(b['published_at']) <= instant(a['published_at']) for a,b in zip(group,group[1:])): raise ValueError('VERSION_ORDER') rows = []; ids = set() for decision in data['decisions']: if decision['id'] in ids: raise ValueError('DUPLICATE_DECISION') ids.add(decision['id']); at = instant(decision['at']) group = [r for r in versions if r['period'] == decision['period']] eligible = [r for r in group if max(instant(r['published_at']),instant(r['received_at'])) <= at] selected = max(eligible, key=lambda r:r['version']) if eligible else None # Deliberately wrong comparator: today's value attached to first publication. latest = max(group,key=lambda r:r['version']) if group else None naive_green = bool(group) and min(instant(r['published_at']) for r in group) <= at rows.append({'decision':decision['id'], 'at':decision['at'], 'naive_time_check':naive_green, 'naive_value':latest['value'] if naive_green else None, 'selected_version':selected['version'] if selected else None, 'usable_value':selected['value'] if selected else None, 'action':'UNAVAILABLE' if selected is None else ('BUY' if selected['value'] > 0 else 'NO_BUY')}) return {'case':'QE-VINTAGE-01/1.0','synthetic':True,'rows':rows, 'scope':'Declared clocks and complete supplied vintages; no verified vendor latency, forecasts or market returns.'} def main(): parser=argparse.ArgumentParser(description=__doc__) parser.add_argument('--input',type=Path);parser.add_argument('--out',type=Path) args=parser.parse_args() data=json.loads(args.input.read_text('utf-8')) if args.input else demo() result=evaluate(data);output=json.dumps(result,ensure_ascii=False,allow_nan=False,indent=2)+'\n' if args.out: with args.out.open('x',encoding='utf-8') as f:f.write(output) else:print(output,end='') if __name__=='__main__':main()