Closes #013 - Refactor Crypto Bayes Engine and expand quantitative handbook

This commit is contained in:
Antigravity Agent
2026-06-13 13:25:47 +02:00
parent 0182bc22f0
commit f3c549e476
4 changed files with 183 additions and 55 deletions

View File

@@ -107,5 +107,18 @@ This document tracks all modifications, npm packages, active compilation states,
* **Active Bugs**: None. * **Active Bugs**: None.
* **Type Checker Status**: Verified clean compilation (`npx tsc --noEmit` returns exit code 0). * **Type Checker Status**: Verified clean compilation (`npx tsc --noEmit` returns exit code 0).
---
## [2026-06-13] - Crypto Bayes Mathematical Refactoring (#ISSUE-013)
### Added
* **Mathematical Specifications & Integration Proof**: Expanded [CryptoMathModal.tsx](file:///c:/Users/jannr/.gemini/antigravity/scratch/investment-sandbox/components/modules/crypto/CryptoMathModal.tsx) with a detailed Section D on the 4-feature client-side Random Forest, Section E on the conjugate Beta-Binomial update, and Section F with the formal calculus integration proof and expanded operational formula.
* **UI Status Badge**: Mounted a static glowing amber status badge `🟡 SYSTEM-AUTARK (OFFLINE-CORE)` in the header of [CryptoDemo.tsx](file:///c:/Users/jannr/.gemini/antigravity/scratch/investment-sandbox/components/modules/crypto/CryptoDemo.tsx) to confirm the module runs 100% locally with zero external API calls.
### Modified
* **KaTeX Escaping Repairs**: Fixed string escaping anomalies by double-escaping LaTeX backslashes across [CryptoMathModal.tsx](file:///c:/Users/jannr/.gemini/antigravity/scratch/investment-sandbox/components/modules/crypto/CryptoMathModal.tsx) to eliminate runtime KaTeX rendering corruption.
* **`QUANT_ROADMAP.md`**: Synchronized quantitative system specifications under Section 4.V.
### Active Bugs / Compile Status
* **Active Bugs**: None.
* **Type Checker Status**: Verified clean compilation (`npx tsc --noEmit` returns exit code 0).

View File

@@ -166,6 +166,37 @@ Where:
--- ---
### V. Crypto Bayesian Markov & Self-Correcting Engine
Integrates a two-stage predictive mapping pipeline for cryptocurrency assets (BTC, ETH, SOL) that combines on-chain derivatives data with online Bayesian updates.
#### 1. Machine Learning Random Forest Classifier
Ensemble of 10 decision trees mapping four features to forecast trend probabilities:
* **Funding Rates (FR)**: Future leverage balance indicators.
* **Open Interest (OI) Volatility**: Contract buildup velocities.
* **Long/Short (LS) Retail Skew**: Sentiment extreme markers.
* **Whale Inflows (W)**: Cold-wallet transfer proxy metrics.
$$P_{\text{ML}} = \frac{1}{M} \sum_{m=1}^{M} T_m(FR_t, OI_t, LS_t, W_t)$$
#### 2. Conjugate Beta-Binomial Update
Continuous ML probability outputs ($P_{\text{ML}}$) are mapped into a discrete Binomial likelihood by defining the Trust-Weight Hyperparameter ($w=12$) as the Effective Sample Size (ESS):
* **Prior distribution**: $\theta \sim \text{Beta}(\alpha_{\text{prior}}, \beta_{\text{prior}})$
* **Binomial Likelihood pseudo-observations**: $k = P_{\text{ML}} \times w$ (successes), $w - k = (1 - P_{\text{ML}}) \times w$ (failures)
* **Conjugate Posterior Update**:
$$\alpha_{\text{post}} = \alpha_{\text{prior}} + k$$
$$\beta_{\text{post}} = \beta_{\text{prior}} + (w - k)$$
#### 3. Posterior Mean Integration Proof
Integrating the continuous parameter $\theta$ out of the posterior distribution gives the mathematical expectation of the posterior:
$$\mathbb{E}[\theta \mid \text{Data}] = \int_{0}^{1} \theta \cdot P(\theta \mid \text{Data}) \, d\theta = \int_{0}^{1} \theta \cdot \frac{\theta^{\alpha_{\text{post}}-1}(1-\theta)^{\beta_{\text{post}}-1}}{\text{B}(\alpha_{\text{post}}, \beta_{\text{post}})} \, d\theta$$
$$\mathbb{E}[\theta \mid \text{Data}] = \frac{\text{B}(\alpha_{\text{post}} + 1, \beta_{\text{post}})}{\text{B}(\alpha_{\text{post}}, \beta_{\text{post}})} = \frac{\alpha_{\text{post}}}{\alpha_{\text{post}} + \beta_{\text{post}}}$$
#### 4. Expanded Workstation Formula
$$P_{\text{Posterior}} = \frac{\alpha_{\text{prior}} + (P_{\text{ML}} \times w)}{\alpha_{\text{prior}} + \beta_{\text{prior}} + w}$$
---
## 5. Multi-Regime Transition Classifier ## 5. Multi-Regime Transition Classifier
The core cognitive brain of the sandbox dynamically adjusts allocation weights across our portfolio modules based on estimated macroeconomic and market states. The core cognitive brain of the sandbox dynamically adjusts allocation weights across our portfolio modules based on estimated macroeconomic and market states.

View File

@@ -185,6 +185,11 @@ export default function CryptoDemo() {
</h2> </h2>
</div> </div>
<div className="flex flex-wrap items-center gap-3"> <div className="flex flex-wrap items-center gap-3">
<span className="inline-flex items-center gap-1.5 px-3 py-2.5 rounded-xl text-xs font-bold bg-amber-500/10 text-amber-400 border border-amber-500/20 shadow-[0_0_15px_rgba(245,158,11,0.15)] h-11">
<ShieldAlert className="w-4 h-4 text-amber-400" />
<span>SYSTEM-AUTARK (OFFLINE-CORE)</span>
</span>
<button <button
onClick={() => setIsMathModalOpen(true)} onClick={() => setIsMathModalOpen(true)}
className="flex items-center gap-1.5 px-4 py-2 rounded-xl bg-slate-950/80 hover:bg-slate-900 border border-slate-800 hover:border-slate-700 transition-all font-semibold text-xs tracking-wider text-cyan-400 justify-center h-11" className="flex items-center gap-1.5 px-4 py-2 rounded-xl bg-slate-950/80 hover:bg-slate-900 border border-slate-800 hover:border-slate-700 transition-all font-semibold text-xs tracking-wider text-cyan-400 justify-center h-11"

View File

@@ -1,5 +1,5 @@
import React from 'react'; import React from 'react';
import { BookOpen } from 'lucide-react'; import { BookOpen, X, Cpu, BarChart2, Compass, ShieldAlert, Activity } from 'lucide-react';
import 'katex/dist/katex.min.css'; import 'katex/dist/katex.min.css';
import { BlockMath, InlineMath } from 'react-katex'; import { BlockMath, InlineMath } from 'react-katex';
@@ -26,11 +26,11 @@ export default function CryptoMathModal({ isOpen, onClose }: CryptoMathModalProp
if (!isOpen) return null; if (!isOpen) return null;
return ( return (
<div className="fixed inset-0 z-50 flex items-center justify-center bg-slate-950/85 backdrop-blur-md p-4 sm:p-6 md:p-8"> <div className="fixed inset-0 z-50 flex items-center justify-center bg-slate-950/85 backdrop-blur-md p-4 sm:p-6 md:p-8 animate-fade-in">
<div className="bg-slate-900 border border-slate-800/80 rounded-3xl w-full max-w-4xl h-[80vh] flex flex-col overflow-hidden shadow-2xl relative text-slate-300"> <div className="bg-slate-900 border border-slate-800/80 rounded-3xl w-full max-w-4xl h-[80vh] flex flex-col overflow-hidden shadow-2xl relative text-slate-300">
{/* Modal Header */} {/* Modal Header */}
<div className="flex justify-between items-center px-6 py-4 bg-slate-950/40 border-b border-slate-800/60"> <div className="flex justify-between items-center px-6 py-4 bg-slate-950/45 border-b border-slate-800/60">
<div> <div>
<h2 className="text-base font-bold bg-gradient-to-r from-cyan-400 to-sky-400 bg-clip-text text-transparent flex items-center gap-2"> <h2 className="text-base font-bold bg-gradient-to-r from-cyan-400 to-sky-400 bg-clip-text text-transparent flex items-center gap-2">
<BookOpen className="w-5 h-5 text-cyan-400" /> Crypto Bayesian Markov - Math & Logic Specification <BookOpen className="w-5 h-5 text-cyan-400" /> Crypto Bayesian Markov - Math & Logic Specification
@@ -39,69 +39,148 @@ export default function CryptoMathModal({ isOpen, onClose }: CryptoMathModalProp
</div> </div>
<button <button
onClick={onClose} onClick={onClose}
className="text-slate-400 hover:text-slate-200 bg-slate-950/50 border border-slate-800 hover:border-slate-700 px-3 py-1.5 rounded-lg text-xs font-semibold font-mono transition-all cursor-pointer" className="text-slate-400 hover:text-slate-200 bg-slate-950/50 border border-slate-800 hover:border-slate-700 p-2 rounded-xl transition-all cursor-pointer flex items-center justify-center"
aria-label="Close modal"
> >
Schließen (ESC) <X className="w-4 h-4" />
</button> </button>
</div> </div>
{/* Modal Body */} {/* Modal Body */}
<div className="flex-1 overflow-y-auto p-6 sm:p-8 space-y-6 text-slate-300 scrollbar-thin"> <div className="flex-1 overflow-y-auto p-6 sm:p-8 space-y-8 text-slate-300 scrollbar-thin">
<div className="space-y-6">
<div className="border-b border-slate-800/80 pb-3"> <div className="border-b border-slate-800/80 pb-3">
<h3 className="text-base font-bold text-slate-200">4. Crypto Bayesian Markov Engine</h3> <h3 className="text-base font-bold text-slate-200">4. Crypto Bayesian Markov Engine</h3>
<p className="text-xs text-slate-400 mt-1">Models momentum regimes and updates transition probabilities using on-chain alpha inputs.</p> <p className="text-xs text-slate-400 mt-1">Models momentum regimes and updates transition probabilities using on-chain alpha inputs.</p>
</div> </div>
<div className="space-y-3"> {/* Section A: Markov Chain State Space */}
<h4 className="text-xs font-bold text-cyan-400 uppercase tracking-wider font-mono">A. Markov Chain State Space</h4> <div className="space-y-3">
<p className="text-xs leading-relaxed text-slate-400"> <h4 className="text-xs font-bold text-cyan-400 uppercase tracking-wider font-mono">A. Markov Chain State Space</h4>
The asset return state space is mapped into 3 momentum regimes: <p className="text-xs leading-relaxed text-slate-400">
</p> The asset return state space is mapped into 3 momentum regimes:
<div className="grid grid-cols-3 gap-3 text-xs text-slate-400 font-mono text-center"> </p>
<div className="bg-slate-950/40 p-3 rounded-lg border border-slate-800/50"> <div className="grid grid-cols-3 gap-3 text-xs text-slate-400 font-mono text-center">
<span className="block text-rose-400 font-bold">State 1 (S1)</span> <div className="bg-slate-950/40 p-3 rounded-lg border border-slate-800/50">
<span>Bearish Squeeze / Crackdown</span> <span className="block text-rose-400 font-bold">State 1 (S1)</span>
</div> <span>Bearish Squeeze / Crackdown</span>
<div className="bg-slate-950/40 p-3 rounded-lg border border-slate-800/50">
<span className="block text-slate-300 font-bold">State 2 (S2)</span>
<span>Consolidation / Mean Reversion</span>
</div>
<div className="bg-slate-950/40 p-3 rounded-lg border border-slate-800/50">
<span className="block text-emerald-400 font-bold">State 3 (S3)</span>
<span>Parabolic Bull Run</span>
</div>
</div> </div>
</div> <div className="bg-slate-955/40 p-3 rounded-lg border border-slate-800/50">
<span className="block text-slate-300 font-bold">State 2 (S2)</span>
<div className="space-y-3"> <span>Consolidation / Mean Reversion</span>
<h4 className="text-xs font-bold text-cyan-400 uppercase tracking-wider font-mono">B. Transition Matrix (P)</h4>
<p className="text-xs leading-relaxed text-slate-400">
Calculates transition probabilities over rolling 90-day return vectors:
</p>
<div className="bg-slate-950/40 p-4 rounded-xl border border-slate-800/60 my-2">
<BlockMath math="P = \begin{bmatrix} p_{11} & p_{12} & p_{13} \\ p_{21} & p_{22} & p_{23} \\ p_{31} & p_{32} & p_{33} \end{bmatrix}" />
<p className="text-[11px] text-slate-400 font-mono mt-2 text-center">
where <InlineMath math="p_{ij} = P(X_{t+1} = S_j \mid X_t = S_i)" /> represents the frequency probability of moving from State i to State j.
</p>
</div> </div>
</div> <div className="bg-slate-955/40 p-3 rounded-lg border border-slate-800/50">
<span className="block text-emerald-400 font-bold">State 3 (S3)</span>
<div className="space-y-3"> <span>Parabolic Bull Run</span>
<h4 className="text-xs font-bold text-cyan-400 uppercase tracking-wider font-mono">C. Bayesian Update Engine</h4>
<p className="text-xs leading-relaxed text-slate-400">
When external alpha inputs (e.g. Funding Rate anomalies, Whale inflows) occur, state probabilities are updated using Bayes' theorem:
</p>
<div className="bg-slate-950/40 p-4 rounded-xl border border-slate-800/60 my-2">
<BlockMath math="P(S_i \mid \text{Alpha}) = \frac{P(\text{Alpha} \mid S_i) \times P(S_i)}{\sum_{j=1}^3 P(\text{Alpha} \mid S_j) \times P(S_j)}" />
<p className="text-[11px] text-slate-400 mt-2 font-mono leading-relaxed">
Where:<br/>
- <InlineMath math="P(S_i)" /> is the prior state probability from the Markov transition matrix.<br/>
- <InlineMath math="P(\text{Alpha} \mid S_i)" /> is the conditional likelihood of observing this whale spike / funding squeeze in State i.
</p>
</div> </div>
</div> </div>
</div> </div>
{/* Section B: Transition Matrix (P) */}
<div className="space-y-3">
<h4 className="text-xs font-bold text-cyan-400 uppercase tracking-wider font-mono">B. Transition Matrix (P)</h4>
<p className="text-xs leading-relaxed text-slate-400">
Calculates transition probabilities over rolling 90-day return vectors:
</p>
<div className="bg-slate-950/40 p-4 rounded-xl border border-slate-800/60 my-2">
<BlockMath math="P = \\begin{bmatrix} p_{11} & p_{12} & p_{13} \\\\ p_{21} & p_{22} & p_{23} \\\\ p_{31} & p_{32} & p_{33} \\end{bmatrix}" />
<p className="text-[11px] text-slate-400 font-mono mt-2 text-center">
{"where "}<InlineMath math="p_{ij} = P(X_{t+1} = S_j \\mid X_t = S_i)" />{" represents the frequency probability of moving from State i to State j."}
</p>
</div>
</div>
{/* Section C: Bayesian Update Engine */}
<div className="space-y-3">
<h4 className="text-xs font-bold text-cyan-400 uppercase tracking-wider font-mono">C. Bayesian Update Engine</h4>
<p className="text-xs leading-relaxed text-slate-400">
When external alpha inputs (e.g. Funding Rate anomalies, Whale inflows) occur, state probabilities are updated using Bayes' theorem:
</p>
<div className="bg-slate-950/40 p-4 rounded-xl border border-slate-800/60 my-2">
<BlockMath math="P(S_i \\mid \\text{Alpha}) = \\frac{P(\\text{Alpha} \\mid S_i) \\times P(S_i)}{\\sum_{j=1}^3 P(\\text{Alpha} \\mid S_j) \\times P(S_j)}" />
<p className="text-[11px] text-slate-400 mt-2 font-mono leading-relaxed">
{"Where:"}
<br />
{"- "}<InlineMath math="P(S_i)" />{" is the prior state probability from the Markov transition matrix."}
<br />
{"- "}<InlineMath math="P(\\text{Alpha} \\mid S_i)" />{" is the conditional likelihood of observing this whale spike / funding squeeze in State i."}
</p>
</div>
</div>
{/* Section D: Two-Stage Hybrid Machine Learning Pipeline */}
<div className="space-y-3">
<h4 className="text-xs font-bold text-cyan-400 uppercase tracking-wider font-mono">D. Two-Stage Hybrid Machine Learning Pipeline</h4>
<p className="text-xs leading-relaxed text-slate-400">
To minimize false signals and adapt to changing momentum regimes, we run a two-stage hybrid ML pipeline. First, a client-side Random Forest Classifier maps non-linear combinations of four features to forecast trend probabilities:
</p>
<div className="bg-slate-950/40 p-5 rounded-2xl border border-slate-800/60 space-y-3">
<span className="text-xs font-semibold text-slate-200 block">Ensemble Feature Inputs:</span>
<ul className="text-xs text-slate-400 list-disc pl-5 space-y-1">
<li><strong className="text-cyan-400">Funding Rates (\\(FR_t\\)):</strong> Measures leverage imbalance in futures contracts.</li>
<li><strong className="text-cyan-400">Open Interest Volatility (\\(OI_t\\)):</strong> Captures the contract buildup speed.</li>
<li><strong className="text-cyan-400">Long/Short Retail Skew (\\(LS_t\\)):</strong> Identifies retail sentiment extremes.</li>
<li><strong className="text-cyan-400">Whale Accumulation Inflows (\\(W_t\\)):</strong> Proxy for cold-wallet transfer velocities.</li>
</ul>
<p className="text-xs text-slate-400 leading-relaxed pt-2">
The Random Forest ensemble evaluates 10 non-linear decision trees to output the short-term and medium-term trend probabilities:
</p>
<BlockMath math="P_{\\text{ML}} = \\frac{1}{M} \\sum_{m=1}^{M} T_m(FR_t, OI_t, LS_t, W_t)" />
</div>
</div>
{/* Section E: Conjugate Beta-Binomial Error-Correction Engine */}
<div className="space-y-3">
<h4 className="text-xs font-bold text-cyan-400 uppercase tracking-wider font-mono">E. Conjugate Beta-Binomial Error-Correction Engine</h4>
<p className="text-xs leading-relaxed text-slate-400">
Raw ML forecasts are susceptible to regime drift. We implement a self-correcting Beta-Binomial update. The continuous probability output from the Random Forest model is mapped into a discrete Binomial likelihood by defining the Trust-Weight Hyperparameter (<InlineMath math="w" />) as the Effective Sample Size (ESS):
</p>
<div className="bg-slate-955/40 p-5 rounded-2xl border border-slate-850 space-y-4">
<div>
<p className="text-xs text-slate-350 mb-2 font-semibold">1. Prior Distribution (Accuracy History):</p>
<BlockMath math="\\theta \\sim \\text{Beta}(\\alpha_{\\text{prior}}, \\beta_{\\text{prior}})" />
<p className="text-[10px] text-slate-500 font-mono">
where <InlineMath math="\\alpha_{\\text{prior}}" /> represents historical prediction successes and <InlineMath math="\\beta_{\\text{prior}}" /> represents false alarms.
</p>
</div>
<div>
<p className="text-xs text-slate-350 mb-2 font-semibold">2. Likelihood Formulation (Binomial pseudo-observations):</p>
<BlockMath math="P(x \\mid \\theta) = \\binom{w}{k} \\theta^k (1 - \\theta)^{w - k}" />
<p className="text-[10px] text-slate-500 font-mono">
where <InlineMath math="k = P_{\\text{ML}} \\times w" /> is the number of simulated successes and <InlineMath math="w - k = (1 - P_{\\text{ML}}) \\times w" /> is the number of simulated failures.
</p>
</div>
<div>
<p className="text-xs text-slate-350 mb-2 font-semibold">3. Conjugate Posterior Update:</p>
<BlockMath math="\\alpha_{\\text{post}} = \\alpha_{\\text{prior}} + k, \\quad \\beta_{\\text{post}} = \\beta_{\\text{prior}} + (w - k)" />
</div>
</div>
</div>
{/* Section F: Mathematical Proof of Posterior Mean Integration */}
<div className="space-y-3">
<h4 className="text-xs font-bold text-cyan-400 uppercase tracking-wider font-mono">F. Mathematical Proof of Posterior Mean Integration</h4>
<p className="text-xs leading-relaxed text-slate-400">
To resolve a single operational point-estimate from our posterior distribution, we integrate out the continuous parameter <InlineMath math="\\theta" /> to calculate the mathematical expectation of the posterior distribution:
</p>
<div className="bg-slate-955/40 p-5 rounded-2xl border border-slate-850 space-y-4">
<p className="text-xs text-slate-350 font-semibold">Posterior Expectation Integral Proof:</p>
<BlockMath math="\\mathbb{E}[\\theta \\mid \\text{Data}] = \\int_{0}^{1} \\theta \\cdot P(\\theta \\mid \\text{Data}) \\, d\\theta = \\int_{0}^{1} \\theta \\cdot \\frac{\\theta^{\\alpha_{\\text{post}}-1}(1-\\theta)^{\\beta_{\\text{post}}-1}}{\\text{B}(\\alpha_{\\text{post}}, \\beta_{\\text{post}})} \\, d\\theta" />
<p className="text-xs text-slate-400 leading-relaxed">
Using the definition of the Beta function <InlineMath math="\\text{B}(a, b) = \\frac{\\Gamma(a)\\Gamma(b)}{\\Gamma(a+b)}" /> and the recurrence relation <InlineMath math="\\Gamma(x+1) = x\\Gamma(x)" />:
</p>
<BlockMath math="\\mathbb{E}[\\theta \\mid \\text{Data}] = \\frac{\\text{B}(\\alpha_{\\text{post}} + 1, \\beta_{\\text{post}})}{\\text{B}(\\alpha_{\\text{post}}, \\beta_{\\text{post}})} = \\frac{\\Gamma(\\alpha_{\\text{post}} + 1)\\Gamma(\\beta_{\\text{post}})}{\\Gamma(\\alpha_{\\text{post}} + \\beta_{\\text{post}} + 1)} \\cdot \\frac{\\Gamma(\\alpha_{\\text{post}} + \\beta_{\\text{post}})}{\\Gamma(\\alpha_{\\text{post}})\\Gamma(\\beta_{\\text{post}})}" />
<BlockMath math="\\mathbb{E}[\\theta \\mid \\text{Data}] = \\frac{\\alpha_{\\text{post}}\\Gamma(\\alpha_{\\text{post}})\\Gamma(\\beta_{\\text{post}})}{(\\alpha_{\\text{post}} + \\beta_{\\text{post}})\\Gamma(\\alpha_{\\text{post}} + \\beta_{\\text{post}})} \\cdot \\frac{\\Gamma(\\alpha_{\\text{post}} + \\beta_{\\text{post}})}{\\Gamma(\\alpha_{\\text{post}})\\Gamma(\\beta_{\\text{post}})} = \\frac{\\alpha_{\\text{post}}}{\\alpha_{\\text{post}} + \\beta_{\\text{post}}}" />
<p className="text-xs text-slate-350 font-semibold pt-2">Expanded Workstation Implementation Formula:</p>
<BlockMath math="P_{\\text{Posterior}} = \\frac{\\alpha_{\\text{prior}} + (P_{\\text{ML}} \\times w)}{\\alpha_{\\text{prior}} + \\beta_{\\text{prior}} + w}" />
<p className="text-[10px] text-slate-500 font-mono leading-relaxed">
This formulation ensures that if the prior model is highly accurate (large <InlineMath math="\\alpha_{\\text{prior}}" />), the raw ML signal is smoothed towards historical baseline expectations. If historical errors are high, the prior variance restricts overreaction to noisy signals.
</p>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>