anky2002 commited on
Commit
e0af913
Β·
verified Β·
1 Parent(s): 4def862

Upload bayesian_engine.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. bayesian_engine.py +45 -17
bayesian_engine.py CHANGED
@@ -297,31 +297,59 @@ def bayesian_synthesis(agent_results: List[AgentEvidence]) -> ForensicVerdict:
297
  conf_label = "High"
298
 
299
  # Compute confidence based on agreement strength and active agent count
300
- non_failed = [s for s, f in zip(scores, failure_probs) if f < 0.5]
301
- if non_failed:
302
- avg_score = float(np.mean(non_failed))
303
- n_agents = len(non_failed)
 
 
 
 
 
 
304
 
305
  # Count agents agreeing with the majority direction
306
- signs = [1 if s > 0.02 else (-1 if s < -0.02 else 0) for s in non_failed]
307
  n_pos = sum(1 for s in signs if s > 0)
308
  n_neg = sum(1 for s in signs if s < 0)
309
  n_agreeing = max(n_pos, n_neg)
310
- n_directional = n_pos + n_neg
311
 
312
- if n_directional == 0:
313
- confidence_numeric = 0.15
314
- elif n_pos > 0 and n_neg > 0:
315
- # Mixed β€” penalize
316
- agreement_ratio = max(n_pos, n_neg) / n_directional
317
- confidence_numeric = min(1.0, 0.1 + 0.3 * abs(avg_score) * agreement_ratio)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318
  else:
319
- # All agree β€” compound with agent count
320
  agent_bonus = min(1.0, np.sqrt(n_agreeing / 2.0))
321
- coverage = n_agents / 7.0
322
- confidence_numeric = min(1.0, 0.15 + 0.6 * abs(avg_score) * agent_bonus * coverage)
323
- if n_agreeing >= 4:
324
- confidence_numeric = min(1.0, confidence_numeric + 0.1)
 
 
 
 
 
325
  else:
326
  confidence_numeric = 0.1
327
 
 
297
  conf_label = "High"
298
 
299
  # Compute confidence based on agreement strength and active agent count
300
+ # Exclude near-zero agents from confidence: an agent saying "I don't know"
301
+ # should contribute zero, not drag confidence down
302
+ NEAR_ZERO = 0.02
303
+ non_failed = [(s, f) for s, f in zip(scores, failure_probs) if f < 0.5]
304
+ informative = [s for s, f in non_failed if abs(s) > NEAR_ZERO]
305
+ n_total_active = len(non_failed)
306
+
307
+ if informative:
308
+ avg_informative = float(np.mean(informative))
309
+ n_informative = len(informative)
310
 
311
  # Count agents agreeing with the majority direction
312
+ signs = [1 if s > 0 else -1 for s in informative]
313
  n_pos = sum(1 for s in signs if s > 0)
314
  n_neg = sum(1 for s in signs if s < 0)
315
  n_agreeing = max(n_pos, n_neg)
316
+ n_dissenting = min(n_pos, n_neg)
317
 
318
+ if n_pos > 0 and n_neg > 0:
319
+ # Mixed β€” but scale with how strong the majority is
320
+ # 3:1 ratio should give decent confidence; 1:1 should give low
321
+ majority_ratio = n_agreeing / (n_agreeing + n_dissenting)
322
+
323
+ # Majority scores only (ignore dissent magnitude for base)
324
+ majority_dir = 1 if n_pos > n_neg else -1
325
+ majority_scores = [s for s in informative if (s > 0) == (majority_dir > 0)]
326
+ majority_avg = abs(float(np.mean(majority_scores))) if majority_scores else 0
327
+
328
+ # Strong majority (>=75%) with decent magnitude β†’ reasonable confidence
329
+ # Weak majority (50-60%) β†’ low confidence
330
+ agent_bonus = min(1.0, np.sqrt(n_agreeing / 2.0))
331
+ coverage = n_total_active / 7.0
332
+
333
+ if majority_ratio >= 0.75:
334
+ # 3:1 or better β€” this is real agreement with a dissenter
335
+ confidence_numeric = min(1.0, 0.12 + 0.5 * majority_avg * agent_bonus * coverage)
336
+ if n_agreeing >= 3:
337
+ confidence_numeric = min(1.0, confidence_numeric + 0.06)
338
+ else:
339
+ # Near 50:50 β€” genuinely ambiguous
340
+ confidence_numeric = min(1.0, 0.1 + 0.2 * abs(avg_informative) * majority_ratio)
341
  else:
342
+ # All informative agents agree β€” compound with count
343
  agent_bonus = min(1.0, np.sqrt(n_agreeing / 2.0))
344
+ coverage = n_total_active / 7.0
345
+ confidence_numeric = min(1.0, 0.15 + 0.6 * abs(avg_informative) * agent_bonus * coverage)
346
+ if n_agreeing >= 3:
347
+ confidence_numeric = min(1.0, confidence_numeric + 0.08)
348
+ if n_agreeing >= 5:
349
+ confidence_numeric = min(1.0, confidence_numeric + 0.08)
350
+ elif n_total_active > 0:
351
+ # All agents near zero β€” no information
352
+ confidence_numeric = 0.12
353
  else:
354
  confidence_numeric = 0.1
355