Fix sym shadowing warnings + coherent softmax gating
- Rename Init() param sym -> symName in IAgent base and overrides (MAAgent, MomentumAgent, RegimeADX) to stop hiding the global sym. - Softmax gating: derive temperature from dispersion of the competence weights ws[] (the same quantity the softmax weights), not from the variance of signals zs[]; skip disabled agents in the normalization. - Use |rho| (with weightMin floor) as competence and sign-flip anti-correlated agents instead of zeroing them, so rho<0 information is used (inverted) rather than discarded. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -37,8 +37,8 @@ public:
|
||||
|
||||
virtual ~IAgent() {}
|
||||
|
||||
virtual void Init(string sym, ENUM_TIMEFRAMES tf) {
|
||||
symbol = sym;
|
||||
virtual void Init(string symName, ENUM_TIMEFRAMES tf) {
|
||||
symbol = symName;
|
||||
timeframe = tf;
|
||||
}
|
||||
|
||||
|
||||
@@ -56,8 +56,8 @@ public:
|
||||
maHandle(INVALID_HANDLE), atrHandle(INVALID_HANDLE), lastMAPeriod(0), barCount(0),
|
||||
priceToMaCorr(0.1, 5), slopeCorr(0.1, 5), lastZ1(0), lastZ2(0) { signalStats.SetR(5.0); }
|
||||
|
||||
void Init(string sym, ENUM_TIMEFRAMES tf) override {
|
||||
IAgent::Init(sym, tf);
|
||||
void Init(string symName, ENUM_TIMEFRAMES tf) override {
|
||||
IAgent::Init(symName, tf);
|
||||
maHandle = INVALID_HANDLE;
|
||||
atrHandle = INVALID_HANDLE;
|
||||
lastMAPeriod = 0;
|
||||
|
||||
@@ -39,8 +39,8 @@ public:
|
||||
momHandle(INVALID_HANDLE), lastMomPeriod(0),
|
||||
lastZ1(0), lastZ2(0), momCorr(0.1, 5), accelCorr(0.1, 5) { signalStats.SetR(2.0); }
|
||||
|
||||
void Init(string sym, ENUM_TIMEFRAMES tf) override {
|
||||
IAgent::Init(sym, tf);
|
||||
void Init(string symName, ENUM_TIMEFRAMES tf) override {
|
||||
IAgent::Init(symName, tf);
|
||||
momHandle = INVALID_HANDLE;
|
||||
lastMomPeriod = 0;
|
||||
}
|
||||
|
||||
@@ -39,8 +39,8 @@ public:
|
||||
: IAgent(n, w), userPeriod(period), adxPeriod(0), adxHandle(INVALID_HANDLE),
|
||||
lastADXPeriod(0), adxMinP(7), adxMaxP(30), prevZ(0) { signalStats.SetR(50.0); }
|
||||
|
||||
void Init(string sym, ENUM_TIMEFRAMES tf) override {
|
||||
IAgent::Init(sym, tf);
|
||||
void Init(string symName, ENUM_TIMEFRAMES tf) override {
|
||||
IAgent::Init(symName, tf);
|
||||
adxHandle = INVALID_HANDLE;
|
||||
lastADXPeriod = 0;
|
||||
}
|
||||
|
||||
@@ -375,14 +375,18 @@ public:
|
||||
double Analyze(const MarketData &data) {
|
||||
if(agentCount == 0) { combinedZ = 0; return 0; }
|
||||
|
||||
double zs[MAX_AGENTS], ws[MAX_AGENTS];
|
||||
double zs[MAX_AGENTS], ws[MAX_AGENTS], corrSign[MAX_AGENTS];
|
||||
|
||||
// Fase 1: Analisi individuale
|
||||
for(int i=0; i<agentCount; i++) {
|
||||
if(!agents[i].enabled) { zs[i] = 0; ws[i] = 0; continue; }
|
||||
if(!agents[i].enabled) { zs[i] = 0; ws[i] = 0; corrSign[i] = 1.0; continue; }
|
||||
zs[i] = agents[i].Analyze(data);
|
||||
double rho = GetCorrelation(i);
|
||||
ws[i] = (rho > 0) ? MathMax(weightMin, rho) : 0;
|
||||
// Competenza = |correlazione| segnale/ritorni (con floor weightMin, così
|
||||
// ogni agente contribuisce una baseline). Il segno di rho non viene scartato:
|
||||
// un agente anti-correlato è informativo → il suo segnale viene invertito.
|
||||
ws[i] = MathMax(weightMin, MathAbs(rho));
|
||||
corrSign[i] = (rho < 0) ? -1.0 : 1.0;
|
||||
}
|
||||
|
||||
// Fase 2: Interazione tra agenti (es. RegimeDetector aggiorna shared context)
|
||||
@@ -419,32 +423,41 @@ public:
|
||||
combinedZ = m_neuralNet.GetCombinedZ(nnInput);
|
||||
combinedZ = MathTanh(combinedZ);
|
||||
} else {
|
||||
// Classic softmax gating (existing logic)
|
||||
double zMean = 0, zVar = 0;
|
||||
int zCount = 0;
|
||||
// Softmax gating coerente: il softmax pesa le competenze ws[] e la
|
||||
// temperatura deriva dalla dispersione delle STESSE competenze (non
|
||||
// dalla varianza dei segnali zs[]), così i due termini sono consistenti.
|
||||
// Competenze simili → temp bassa; competenze molto diverse → temp alta
|
||||
// (mixing più uniforme, evita di sovra-fidarsi di un singolo agente).
|
||||
double wMean = 0;
|
||||
int wCount = 0;
|
||||
for(int i=0; i<agentCount; i++) {
|
||||
if(!agents[i].enabled) continue;
|
||||
zMean += zs[i];
|
||||
zCount++;
|
||||
wMean += ws[i];
|
||||
wCount++;
|
||||
}
|
||||
if(zCount > 0) zMean /= zCount;
|
||||
if(wCount > 0) wMean /= wCount;
|
||||
double wVar = 0;
|
||||
for(int i=0; i<agentCount; i++) {
|
||||
if(!agents[i].enabled) continue;
|
||||
zVar += (zs[i] - zMean) * (zs[i] - zMean);
|
||||
wVar += (ws[i] - wMean) * (ws[i] - wMean);
|
||||
}
|
||||
if(zCount > 0) zVar /= zCount;
|
||||
double temp = (zCount > 0) ? (MathSqrt(zVar) + DATA_EPS(zVar)) / MathSqrt(zCount) : 1.0 / MathSqrt(MathMax(1, agentCount));
|
||||
if(wCount > 0) wVar /= wCount;
|
||||
double temp = (wCount > 0) ? (MathSqrt(wVar) + DATA_EPS(wVar)) / MathSqrt(wCount) : 1.0 / MathSqrt(MathMax(1, agentCount));
|
||||
double safeTemp = MathMax(temp, DATA_EPS(temp) * 10.0); // evita overflow di MathExp
|
||||
|
||||
double sumExp = 0;
|
||||
for(int i=0; i<agentCount; i++)
|
||||
for(int i=0; i<agentCount; i++) {
|
||||
if(!agents[i].enabled) continue;
|
||||
sumExp += MathExp(ws[i] / safeTemp);
|
||||
}
|
||||
|
||||
combinedZ = 0;
|
||||
double epsExp = DATA_EPS((double)agentCount);
|
||||
double epsExp = DATA_EPS((double)wCount);
|
||||
for(int i=0; i<agentCount; i++) {
|
||||
double softmaxW = (sumExp > epsExp) ? MathExp(ws[i] / safeTemp) / sumExp : 1.0 / agentCount;
|
||||
combinedZ += softmaxW * zs[i];
|
||||
if(!agents[i].enabled) continue;
|
||||
double softmaxW = (sumExp > epsExp) ? MathExp(ws[i] / safeTemp) / sumExp : 1.0 / MathMax(1, wCount);
|
||||
// Segnale sign-corretto: agenti anti-correlati contribuiscono invertiti.
|
||||
combinedZ += softmaxW * corrSign[i] * zs[i];
|
||||
}
|
||||
combinedZ = MathTanh(combinedZ);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user