Logo: Adversarial Robustness Of Trading Signals

Adversarial Robustness Of Trading Signals

Communautéit
HimanshuJ16
adversarial-robustness-of-trading-signals

Benotzt et, ier Dir e ML-Handelssignal fräigitt, fir ze moossen, wéi dacks Feature-Verännerungen am Kader vun Epsilon-Grenzen d’Ausgab vum Signal ëmkippen. En gradientefräien Black-Box-Test; bei engem differenzéierbaren Netz liwwere richtegt FGSM oder PGD eng méi enk Grenz fir de schlëmmste Fall.

Add to TypingMind

Iwwersiicht

EditeurHimanshuJ16
RepositoryAlgo-Trading-Skills
Skill-Nummadversarial-robustness-of-trading-signals
Stären
50
Forks
7
Matgeliwwert Dateien
5
LizenzApache-2.0
Linken
  • Instruktiounen am Markdown-Format

    Eng SKILL.md-Datei, déi de Modell bei Bedarf luet, soudatt nëmmen Token ufalen, wann eng Ufro tatsächlech dozou passt.

  • Funktionéiert mat all LLM

    KI-Skills si reng Markdown, keen ubidderspezifesche Code. Dofir funktionéiert dëse Skill mat GPT, Claude, Gemini, Grok oder engem lokale Modell.

  • 5 matgeliwwert Dateien

    Skripten, Virlagen a Referenzen, déi de Modell beim Schaffe liese kann. D'Dateie kënnen nëmme gelies ginn a ginn ni ausgeféiert.

  • Open Source

    HimanshuJ16 huet de Skill op GitHub verëffentlecht. Liest de Quelltext, ier Dir de Skill installéiert.

Installatioun

Installéiert de KI-Skill Adversarial Robustness Of Trading Signals an TypingMind, fir e mat all LLM ze benotzen, oder setzt en an en anere KI-Agent an, deen SKILL.md liest.

1

An TypingMind installéieren

TypingMind installéiert e Skill direkt aus sengem GitHub-Dossier — dobäi gëtt SKILL.md gelies, d'Ressourcedateie gi gebëndelt an d'Resultat gëtt lokal gespäichert.

  1. Maacht d'App op a gitt op Plugins → Skills.
  2. Wielt „Vu GitHub installéieren“.
  3. Pecht d'URL vum Skill-Dossier, déi hei drënner steet, an a bestätegt d'Installatioun.
    https://github.com/HimanshuJ16/Algo-Trading-Skills/tree/main/skills/adversarial-robustness-of-trading-signals
  4. Aktivéiert de Skill an all Chat, an deem Dir e benotze wëllt.
Plugins → Skills → Skill derbäisetzen → Iwwer eng GitHub-URL. Pecht dann d'URL vum Dossier an a klickt op Weider.
2

An engem anere KI-Agent installéieren

All KI-Agent, deen d'Format Agent Skills liest, kann dëse Skill benotzen — kopéiert den Dossier an d'Skill-Verzeechnes vun deem KI-Agent.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/HimanshuJ16/Algo-Trading-Skills.git /tmp/Algo-Trading-Skills
mkdir -p .claude/skills
cp -r /tmp/Algo-Trading-Skills/skills/adversarial-robustness-of-trading-signals .claude/skills/adversarial-robustness-of-trading-signals
Start Claude Code nom Kopéieren nei, fir datt den neie Skill erkannt gëtt.

An TypingMind benotzen

Aktivéiert Adversarial Robustness Of Trading Signals an engem Chat vun TypingMind, an de Modell iwwerhëlt de Rescht. Den Numm an d'Beschreiwung stinn am System-Prompt. Soubal eng Ufro dozou passt, luet de Modell déi komplett Instruktioune selwer — Dir musst de Skill ni manuell opruffen, an et fale keng Token un, bis en tatsächlech benotzt gëtt.

De Modell luet Adversarial Robustness Of Trading Signals automatesch, soubal eng Ufro dozou passt.

Funktionéiert mat all KI-Modell

KI-Skills si reng Markdown-Instruktiounen amplaz vun ubidderspezifeschem Code. Dofir ass Adversarial Robustness Of Trading Signals net un de Modell gebonnen, fir deen de Skill geschriwwe gouf. Installéiert en eemol an TypingMind a benotzt e mat GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama oder engem lokale Modell, deen Dir selwer bedreift — alles mat Ären eegenen API-Schlësselen.

  • Gëtt nëmme gelueden, wann et néideg ass

    De System-Prompt enthält nëmmen den Numm an d'Beschreiwung. D'Instruktioune gi bei der éischter passender Ufro gelueden, soudatt en onbenotzte Skill näischt kascht.

  • De Modell am lafende Chat wiesselen

    Well de Skill aus Instruktiounen amplaz vu Code besteet, funktionéiert en och no engem Modellwiessel — den nächste Modell liest déi selwecht SKILL.md-Datei.

Skill-Instruktiounen

Dëst ass den Inhalt vu SKILL.md, deen de Modell luet. Liest e virun der Installatioun — e Skill besteet aus Instruktiounen, déi Äre Modell befollegt.

When to Use

Invoke this skill before promoting any ML trading-signal model to production — Random Forests, Gradient Boosted Trees, deep nets, or any model exposing a predict(X) callable. Financial microstructure is adversarial in the technical sense: a flash crash, a spoofing layer, or genuine quote noise can perturb order- book features by less than one bid-ask spread and flip a model's signal from BUY to SELL. A model that flips under such noise is unsafe to trade.

The skill produces a SignalAdversarialTester that injects epsilon-bounded perturbations into the validation feature matrix and reports:

  1. The vulnerability score — % of samples whose signal flips, with a one-sided Wilson upper confidence bound on that rate.
  2. The flipped-sample indices — so quant research can attribute which inputs sit on the decision boundary.
  3. A deployment verdict — robust / vulnerable against a tolerance threshold, in two strengths: is_robust (point estimate) and is_robust_at_ci (the confidence bound also clears the tolerance). Gate on the latter.

When NOT to Use

  • Differentiable models requiring a true worst-case bound. This engine is a black-box, gradient-free tester. For a differentiable net, real FGSM/PGD gives a tighter (and actually worst-case) attack: adv_x = x + ε·sign(∇ₓ L). Use a dedicated adversarial-robustness library (CleverHans, Foolbox, ART) instead, then feed that attack's flip rate into this governance gate if you want a uniform scoring contract.
  • Provable-robustness requirements. This is empirical robustness — it can only show a model is vulnerable, never prove it is robust. For a certified L2 radius use Cohen-style randomized smoothing (see references/standards.md §3).
  • Non-ML signals (pure rule-based / threshold strategies with no learned component). Adversarial perturbation of a fixed threshold is just sensitivity analysis; use backtest-parameter-sensitivity-analysis instead.
  • Production-time / online monitoring. The perturbation sweep is O(n_trials) expensive and assumes a static validation set. For live drift monitoring use concept-drift-vs-staleness-differentiation / model-staleness-detection.
  • Insufficient validation data. With < ~1000 out-of-sample samples the flip rate confidence interval is wide; a "pass" near the tolerance is statistically meaningless. Bootstrap or use a larger holdout.

Prerequisites

  • Python 3.10+, numpy.
  • A trained model exposing predict(X) returning one of:
    • integer/boolean 1D labels (used directly as the signal),
    • a 1D float score (split at decision_threshold, default 0.5), or
    • a 2D probability matrix (signal = argmax over columns).
  • A representative out-of-sample validation set X_clean (2D non-empty, all-finite array — NaN/inf are rejected, see Pitfalls). Never test on training data — the model has seen it and will appear artificially robust.
  • A calibrated epsilon — the perturbation budget. Default 0.01 (1% of per-feature scale); for equities, set this to one average bid-ask spread as a fraction of price (see references/standards.md §1).
  • (Recommended) Training-set feature scales — pass feature_scales so the test is independent of the validation sample drawn (the ptp fallback couples ε to the sample).

Workflow

  1. Assemble the validation set. Pull a frozen, out-of-sample X_clean aligned with the model's training feature schema. Confirm it is 2D and finite.

  2. Configure the perturbation budget. Construct an AdversarialRobustnessConfig:

    python
    from signal_adversarial_tester import (
        SignalAdversarialTester, AdversarialRobustnessConfig, NOISE_RANDOM_SIGN,
    )
    
    config = AdversarialRobustnessConfig(
        epsilon=0.01,               # 1% of per-feature scale
        flip_tolerance_pct=5.0,    # reject if > 5% of signals flip
        noise_type=NOISE_RANDOM_SIGN,   # full-magnitude random-sign stress
        n_trials=25,                # for montecarlo_worst only
        seed=42,                    # reproducible (reproducible-ml-pipelines)
        feature_scales=train_scales,   # from training set, not validation
        feature_bounds=train_bounds,   # [min, max] per feature for clipping
        clip_to_clean_domain=True,
        decision_threshold=0.5,
    )
  3. Run the evaluation:

    python
    tester = SignalAdversarialTester(config)
    report = tester.evaluate_model(model.predict, X_clean)
  4. Interpret the verdict:

    • report.is_robust_at_ci == True → the flip rate and its 95% Wilson upper bound clear the tolerance. This is the promotion signal; the model may proceed subject to the other gates in your pipeline.
    • report.is_robust == True but is_robust_at_ci == False → marginal. The point estimate clears the tolerance but the validation set is too small to resolve it. Do not promote: grow the holdout (see standards.md §4).
    • report.is_robust == False → reject. Route the model back to quant research for adversarial training (retrain on perturbed data to smooth the decision boundary), feature re-engineering, or a higher decision threshold. Use tester.perturb(X_train, rng) to generate the augmentation with the same noise model the gate failed on.
  5. Attribute the fragility. report.flipped_indices identifies exactly which validation samples sit on a flippable boundary — the actionable set for a targeted adversarial-training augmentation.

Decision Points

SituationAction
vulnerability_score_pct just under tolerance (e.g. 4.7% vs 5%)Check is_robust_at_ci. If the Wilson upper bound does not clear the tolerance the pass is statistically marginal — grow the validation set; do not promote.
Explicit feature_bounds are narrower than the validation rangeSamples outside the bounds are held to the ε ball, not clipped all the way back — otherwise a multi-ε clip would be scored as an ε-bounded flip. Prefer bounds that actually contain the holdout.
ValueError: X_clean contains N non-finite value(s)A NaN/inf cell would poison the per-feature scale and silently zero the flip rate. Drop or impute the rows upstream; do not disable the check.
random_sign passes but montecarlo_worst (n_trials=50) failsThe model is fragile to some perturbation direction that a single draw missed. Trust the worst-of-N bound; reject.
Flips concentrate in a feature subset (inspect flipped_indices → rows)The model is over-reliant on a few fragile features. Re-engineer or drop them rather than globally retrain.
Flip rate is ~50% on a boundary-clustered setExpected for a sharp threshold at the boundary — not a bug. Re-test on a realistic distribution, not a degenerate constant.
Model returns probabilities, not labelsHandled automatically (argmax over a 2D array). Verify the class ordering matches your BUY/SELL convention.
Need a true worst-case bound (not empirical)This engine cannot provide it. Graduate to FGSM/PGD (differentiable models) or certified randomized smoothing.
clip_to_clean_domain=True collapses all noiseThe validation feature domain is degenerate (constant column). Either widen the domain via feature_bounds or set clip_to_clean_domain=False for stress tests that deliberately leave the manifold.
Reproducibility required across CI runsAlways set seed. None makes the test non-deterministic and unfit for governance gating.

Common Pitfalls

  • Treating random_sign as worst-case. The legacy worst_case_sign name was a misnomer — the signs are random, not gradient-derived. Worst-case directional noise needs the loss gradient (FGSM). montecarlo_worst gives a lower bound on the worst case by taking the max over N random trials; it is tighter than a single draw but still not a true worst case. Read the verdict accordingly.
  • Testing on training data. The model has memorised its training set; flip rates will be artificially low and the gate is meaningless. Always use a frozen out-of-sample holdout.
  • Epsilon not calibrated to microstructure. A blanket epsilon=0.01 is wrong when features have wildly different scales (price in $ vs a normalized ratio in [0,1]). Either pass per-feature feature_scales (training-set) or calibrate ε to one bid-ask spread per instrument.
  • No domain clipping. Perturbing a price feature to a negative value, or a normalized ratio above 1, produces infeasible inputs that overstate vulnerability (the model never sees them in production). Use clip_to_clean_domain=True or explicit feature_bounds.
  • Gating on the point estimate alone. A 0% flip rate on 50 samples has a 95% Wilson upper bound of 5.13% — it cannot clear a 5% tolerance. is_robust answers "did the point estimate clear?"; only is_robust_at_ci answers "did the evidence clear?". Promote on the latter.
  • NaN in the validation set. Before v1.3.0 a single NaN cell made the per-feature ptp NaN, which made every perturbation NaN, which decoded to one class for clean and adversarial alike — a silent 0% flip rate and an automatic PASS. Non-finite inputs and non-finite model outputs are now hard errors; treat them as data-quality defects, not as things to work around.
  • Non-deterministic governance. A deployment gate that flips verdicts between CI runs is worse than no gate. Always set seed; pin it in the model card.
  • Single noise model. uniform is average-case; it can pass a model that random_sign or montecarlo_worst fails. Run at least random_sign for governance; prefer montecarlo_worst for the tighter bound.
  • Ignoring the probability path. A model returning 2D probabilities is decoded via argmax; a 1D float score via decision_threshold. Verify the decode matches your signal convention before trusting the flip count.
  • Using Gaussian noise only. Gaussian samples rarely reach the ε boundary, so they under-state vulnerability versus random_sign (which always hits the boundary). Do not use Gaussian as the sole stress.

Verification

Run the unit tests:

bash
python -m unittest discover -s skills/adversarial-robustness-of-trading-signals/scripts -v

What they assert:

  • A robust model passes under uniform noise.
  • A fragile model fails under random_sign and under montecarlo_worst.
  • montecarlo_worst reports the max flip rate across n_trials (≥ any single trial).
  • Same seed → identical report and flipped_indices (determinism).
  • 2D probability matrices decode via argmax; 1D float scores via decision_threshold.
  • Domain clipping suppresses infeasible perturbations (both implicit and explicit bounds).
  • Explicit feature_scales collapse a huge ε; zero-variance features fall back to scale 1.0.
  • Config validation rejects negative ε/tolerance, invalid noise_type, n_trials < 1, non-positive feature_scales, mis-shaped feature_bounds, inverted bounds, bad batch_size, out-of-range ci_confidence_level; ε=0 logs a vacuous-gate warning.
  • Inputs that used to yield a silent PASS now raise: non-finite X_clean, zero-sample or zero-feature matrices, non-finite model output, higher-rank model output (which previously corrupted flipped_indices).
  • The domain clip never moves a sample further than ε·scale, so an out-of-domain sample cannot manufacture a flip.
  • wilson_upper_bound matches values derived independently by solving the score equation, and stays non-zero at zero flips.
  • A 0-flip run on 50 samples is is_robust but not is_robust_at_ci; the same result on 5000 samples clears both.
  • Categorical (string) class labels are compared directly instead of raising.
  • perturb() honours the ε budget and the feasible domain, and is seeded.
  • Legacy worst_case_sign alias routes to random_sign (deprecation logged).
  • report.as_dict() round-trips through JSON and carries seed, epsilon and flip_tolerance_pct for the model card.
  • batch_size chunking is byte-identical to whole-array evaluation.
  • Non-2D input raises ValueError.

Confirm with the operational checklist in assets/checklist.md before promoting.

Success Criteria

An adversarial-robustness gate is healthy in production when:

  1. Every model promotion produces a RobustnessReport persisted to the model card with seed, epsilon, noise_type, n_trials, and vulnerability_score_pct.
  2. Promotion is gated on is_robust_at_ci, not on vulnerability_score_pct alone. A 5% tolerance needs roughly ≥ 2000 samples before a realistic observed rate can clear its own confidence bound (standards.md §4).
  3. montecarlo_worst with n_trials ≥ 25 is the default governance noise model; uniform/random_sign single-draw results are recorded but not gating.
  4. epsilon is calibrated per instrument to one average bid-ask spread and the calibration is documented (not a blanket 0.01).
  5. Rejected models are routed to adversarial training with the flipped_indices attached as the augmentation target set.
  6. The gate is deterministic across CI runs (fixed seed); a re-run yields the same verdict.

Related Skills

  • feature-engineering-without-leakage — the validation set fed to this gate must be leak-free or the robustness verdict is invalid.
  • backtest-outlier-and-bad-tick-filtering — adversarial perturbations and bad-tick outliers are two faces of the same noise; filter the latter, stress the former.
  • reproducible-ml-training-pipelines — the seed discipline this gate depends on.
  • model-card-documentation-for-trading-models — persist the RobustnessReport (seed, ε, noise model, vulnerability score) into the model card.
  • model-versioning-and-rollback — a regression in vulnerability score between versions should block the promotion.
  • backtest-parameter-sensitivity-analysis — for non-ML / rule-based strategies, where adversarial perturbation reduces to parameter sensitivity.
  • concept-drift-vs-staleness-differentiation — live complement: this gate is pre-deployment; drift monitoring is post-deployment.
  • class-imbalance-handling-for-rare-signal-events — rare-signal models have few positive samples to flip; the flip-rate CI is correspondingly wide.

Matgeliwwert Dateien

De Modell liest dës Dateie bei Bedarf, wärend de Skill gelueden ass. Si ginn als liesbar Dateie bereetgestallt a ginn ni ausgeféiert.

Heefeg gestallte Froen

Wat mécht de KI-Skill Adversarial Robustness Of Trading Signals?

Benotzt et, ier Dir e ML-Handelssignal fräigitt, fir ze moossen, wéi dacks Feature-Verännerungen am Kader vun Epsilon-Grenzen d’Ausgab vum Signal ëmkippen. En gradientefräien Black-Box-Test; bei engem differenzéierbaren Netz liwwere richtegt FGSM oder PGD eng méi enk Grenz fir de schlëmmste Fall.

Firwat Adversarial Robustness Of Trading Signals op TypingMind benotzen?

Well Dir en eemol installéiert a mat all Modell benotze kënnt. Adversarial Robustness Of Trading Signals besteet aus einfachem Markdown amplaz vun ubidderspezifeschem Code. Dee selwechte Skill funktionéiert also mat GPT-5, Claude, Gemini, Grok oder engem lokale Modell – an Dir kënnt de Modell matzen am Chat wiesselen, ouni datt de Skill ophält ze funktionéieren. TypingMind benotzt Är eegen API-Schlësselen, sou datt Dir d'Ubidder direkt bezuelt, amplaz en Abonnement pro Benotzerplaz ze bezuelen. Är Skills a Chats bleiwen an Ärem eegene Späicher.

Wéi installéieren ech Adversarial Robustness Of Trading Signals an TypingMind?

Maacht an TypingMind Plugins → Skills → Vu GitHub installéieren op a setzt https://github.com/HimanshuJ16/Algo-Trading-Skills/tree/main/skills/adversarial-robustness-of-trading-signals an. TypingMind liest d'Datei SKILL.md, bëndelt déi zougehéiereg Dateien an installéiert dat Ganzt als Skill, deen Dir pro Chat aktivéiere kënnt.

Wéi eng KI-Modeller kënnen Adversarial Robustness Of Trading Signals benotzen?

All Modell, deen Dir an TypingMind verbënnt. KI-Skills sinn einfach Markdown-Instruktiounen an net ubidderspezifesche Code. Dofir kënne GPT, Claude, Gemini, Grok a lokal Modeller dëse Skill lueden, wann eng Ufro dozou passt.

Wéi vill KI-Modeller kann ech mat Adversarial Robustness Of Trading Signals benotzen?

Esou vill wéi Dir wëllt. Soulaang e Modell Skills ënnerstëtzt, kënnt Dir Adversarial Robustness Of Trading Signals domat benotzen – zum Beispill mat GPT, Claude, Gemini, Grok, DeepSeek, Mistral oder Llama – alles op TypingMind mat Ären eegenen API-Schlësselen.

Ass de KI-Skill Adversarial Robustness Of Trading Signals gratis?

Jo. HimanshuJ16 huet de Skill ënner der Lizenz Apache-2.0 op GitHub publizéiert. Dir bezuelt nëmmen Ären eegene KI-Ubidder fir d'Token, déi Dir benotzt.

Wat si KI-Skills?

E KI-Skill ass e weiderverwendbare Pak vun Instruktiounen, deen engem KI-Modell bäibréngt, wéi en eng bestëmmt Aufgab erfëllt. E follegt dem oppenen Agent Skills-Format: eng Datei SKILL.md mat engem Numm an enger Beschreiwung, plus all Scripten, Virlagen oder Referenzdateien, déi de Modell eventuell brauch. De Modell liest d'Instruktiounen eréischt, wann Är Ufro zum Skill passt. En installéierte Skill kascht also näischt, bis e benotzt gëtt.

Wéi ënnerscheede sech KI-Skills vu Plugins oder MCP-Serveren?

E Plugin oder MCP-Server gëtt engem Modell nei Tools, déi en opruffe kann – Code, deen iergendwou leeft an e Resultat zeréckgëtt. E KI-Skill gëtt dem Modell dogéint Wëssen an eng Virgoensweis: wéi en eng Aufgab ugeet, wéi eng Schrëtt e maache soll a wéi eng gutt Ausgab ausgesäit. Skills bestinn aus einfachem Markdown, brauche kee Server, keen API-Schlëssel a kee Lafzäitëmfeld a funktionéiere mat all Modell.

Alleguer weisen

Elo Ären eegene KI-Aarbechtsberäich ariichten

Abonnéiert Iech op eisen Newsletter, fir iwwer nei Funktiounen an zukünfteg Gewënnspiller informéiert ze ginn 👇