Spaces:
Runtime error

File size: 6,014 Bytes
d516e49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
 * Enhanced 6D Probability Matrix
 * Fuses 6D Harmonic probability with HNC/Lighthouse probability
 */

import { sixDimensionalEngine, HarmonicWaveform6D, WaveState } from './sixDimensionalHarmonicEngine';

export type TradingAction = 
  | 'STRONG_BUY' 
  | 'BUY' 
  | 'SLIGHT_BUY' 
  | 'HOLD' 
  | 'SLIGHT_SELL' 
  | 'SELL' 
  | 'STRONG_SELL';

export interface ProbabilityFusion {
  // Input probabilities
  probability6D: number;
  probabilityHNC: number;
  probabilityLighthouse: number;
  
  // Weights (dynamic based on wave state)
  weight6D: number;
  weightHNC: number;
  weightLighthouse: number;
  
  // Output
  fusedProbability: number;
  action: TradingAction;
  confidence: number;
  
  // Metadata
  waveState: WaveState;
  harmonicLock: boolean;
  resonanceBoost: number;
}

interface DynamicWeights {
  w6D: number;
  wHNC: number;
  wLighthouse: number;
}

function getWeightsForWaveState(waveState: WaveState): DynamicWeights {
  // Weights shift based on wave state
  switch (waveState) {
    case 'CRYSTALLINE':
      // In crystalline state, 6D is most reliable
      return { w6D: 0.50, wHNC: 0.30, wLighthouse: 0.20 };
    case 'RESONANT':
      // Balanced weights
      return { w6D: 0.40, wHNC: 0.35, wLighthouse: 0.25 };
    case 'TURBULENT':
      // Trust HNC more in turbulent conditions
      return { w6D: 0.30, wHNC: 0.40, wLighthouse: 0.30 };
    case 'CHAOTIC':
      // In chaos, rely more on Lighthouse consensus
      return { w6D: 0.20, wHNC: 0.35, wLighthouse: 0.45 };
    default:
      return { w6D: 0.33, wHNC: 0.34, wLighthouse: 0.33 };
  }
}

function mapProbabilityToAction(probability: number): TradingAction {
  if (probability >= 0.70) return 'STRONG_BUY';
  if (probability >= 0.60) return 'BUY';
  if (probability >= 0.55) return 'SLIGHT_BUY';
  if (probability >= 0.45) return 'HOLD';
  if (probability >= 0.40) return 'SLIGHT_SELL';
  if (probability >= 0.30) return 'SELL';
  return 'STRONG_SELL';
}

function calculateConfidence(
  fusedProb: number,
  waveState: WaveState,
  harmonicLock: boolean,
  inputVariance: number
): number {
  // Base confidence from distance from 0.5 (neutral)
  const baseConfidence = Math.abs(fusedProb - 0.5) * 2;
  
  // Wave state multiplier
  const stateMultiplier = 
    waveState === 'CRYSTALLINE' ? 1.0 :
    waveState === 'RESONANT' ? 0.85 :
    waveState === 'TURBULENT' ? 0.65 : 0.45;
  
  // Harmonic lock boost
  const lockBoost = harmonicLock ? 0.15 : 0;
  
  // Variance penalty (lower variance = higher confidence)
  const variancePenalty = Math.min(0.2, inputVariance * 0.5);
  
  return Math.min(1, Math.max(0, baseConfidence * stateMultiplier + lockBoost - variancePenalty));
}

export class Enhanced6DProbabilityMatrix {
  private lastFusion: ProbabilityFusion | null = null;
  
  fuse(
    symbol: string,
    hncProbability: number,
    lighthouseProbability: number,
    waveform?: HarmonicWaveform6D
  ): ProbabilityFusion {
    // Get 6D waveform if not provided
    const wf = waveform || sixDimensionalEngine.getWaveform(symbol);
    
    // Default 6D values if no waveform
    const prob6D = wf?.probabilityField ?? 0.5;
    const waveState = wf?.waveState ?? 'TURBULENT';
    const harmonicLock = wf?.harmonicLock ?? false;
    const resonanceScore = wf?.resonanceScore ?? 0.5;
    
    // Get dynamic weights based on wave state
    const weights = getWeightsForWaveState(waveState);
    
    // Calculate resonance boost (extra weight for 6D when in resonance)
    const resonanceBoost = resonanceScore > 0.7 ? (resonanceScore - 0.7) * 0.5 : 0;
    
    // Adjust weights with resonance boost
    const adjustedW6D = Math.min(0.6, weights.w6D + resonanceBoost);
    const remainingWeight = 1 - adjustedW6D;
    const ratio = weights.wHNC / (weights.wHNC + weights.wLighthouse);
    const adjustedWHNC = remainingWeight * ratio;
    const adjustedWLighthouse = remainingWeight * (1 - ratio);
    
    // Fuse probabilities
    const fusedProbability = 
      prob6D * adjustedW6D +
      hncProbability * adjustedWHNC +
      lighthouseProbability * adjustedWLighthouse;
    
    // Calculate input variance for confidence
    const mean = (prob6D + hncProbability + lighthouseProbability) / 3;
    const variance = (
      Math.pow(prob6D - mean, 2) +
      Math.pow(hncProbability - mean, 2) +
      Math.pow(lighthouseProbability - mean, 2)
    ) / 3;
    
    // Map to action
    const action = mapProbabilityToAction(fusedProbability);
    
    // Calculate confidence
    const confidence = calculateConfidence(fusedProbability, waveState, harmonicLock, variance);
    
    const fusion: ProbabilityFusion = {
      probability6D: prob6D,
      probabilityHNC: hncProbability,
      probabilityLighthouse: lighthouseProbability,
      weight6D: adjustedW6D,
      weightHNC: adjustedWHNC,
      weightLighthouse: adjustedWLighthouse,
      fusedProbability,
      action,
      confidence,
      waveState,
      harmonicLock,
      resonanceBoost
    };
    
    this.lastFusion = fusion;
    return fusion;
  }
  
  getLastFusion(): ProbabilityFusion | null {
    return this.lastFusion;
  }
  
  // Quick check if we should trade based on fusion
  shouldTrade(minConfidence = 0.6): boolean {
    if (!this.lastFusion) return false;
    
    const { action, confidence, waveState } = this.lastFusion;
    
    // Don't trade in chaotic state
    if (waveState === 'CHAOTIC') return false;
    
    // Need minimum confidence
    if (confidence < minConfidence) return false;
    
    // Only trade on actionable signals
    return action !== 'HOLD';
  }
  
  // Get direction: 1 = long, -1 = short, 0 = neutral
  getDirection(): number {
    if (!this.lastFusion) return 0;
    
    const { action } = this.lastFusion;
    
    if (['STRONG_BUY', 'BUY', 'SLIGHT_BUY'].includes(action)) return 1;
    if (['STRONG_SELL', 'SELL', 'SLIGHT_SELL'].includes(action)) return -1;
    return 0;
  }
}

// Singleton instance
export const probabilityMatrix = new Enhanced6DProbabilityMatrix();