[FEAT-AGENT][Added CR-CA/Deep Research agent + docs and examples] - #1181
[FEAT-AGENT][Added CR-CA/Deep Research agent + docs and examples]#1181IlumCI wants to merge 6 commits into
Conversation
| from dataclasses import dataclass | ||
| from typing import Dict, List, Tuple, Optional, Any | ||
|
|
||
| import numpy as np |
Check failure
Code scanning / Pyre
Undefined import Error
| from typing import Dict, List, Tuple, Optional, Any | ||
|
|
||
| import numpy as np | ||
| import pandas as pd |
Check failure
Code scanning / Pyre
Undefined import Error
|
|
||
| import numpy as np | ||
| import pandas as pd | ||
| from loguru import logger |
Check failure
Code scanning / Pyre
Undefined import Error
| return any(abs(float(strengths.get(k, 0.0))) > tol for k in keys) | ||
|
|
||
| # ===== Helper: calibrate z to target service via short grid search ===== | ||
| def calibrate_z_to_service(self, target_service: float = 0.95, z_grid: Optional[np.ndarray] = None) -> float: |
Check failure
Code scanning / Pyre
Undefined or invalid type Error
| def validate_and_rollback( | ||
| self, | ||
| new_kpis: Dict[str, float], | ||
| thresholds: Dict[str, float] = None, |
Check failure
Code scanning / Pyre
Incompatible variable type Error
| base = agent.simulate() | ||
| base_kpi = agent.summarize(base) | ||
| for g in grid: | ||
| impact = agent._quantified_impact(g["z"], g["r"], g["e"], trials=50) |
Check failure
Code scanning / Pyre
Incompatible parameter type Error
| base = agent.simulate() | ||
| base_kpi = agent.summarize(base) | ||
| for g in grid: | ||
| impact = agent._quantified_impact(g["z"], g["r"], g["e"], trials=50) |
Check failure
Code scanning / Pyre
Incompatible parameter type Error
| base = agent.simulate() | ||
| base_kpi = agent.summarize(base) | ||
| for g in grid: | ||
| impact = agent._quantified_impact(g["z"], g["r"], g["e"], trials=50) |
Check failure
Code scanning / Pyre
Incompatible parameter type Error
| svc_delta = float(exp.get("service", 0.0)) | ||
| cost_delta = float(exp.get("cost_proxy", 0.0)) | ||
| marginal_cost_per_pp = float(cost_delta / max(1e-6, svc_delta * 100.0)) if svc_delta > 0 else float("inf") | ||
| results[g["name"]] = { |
Check failure
Code scanning / Pyre
Incompatible parameter type Error
|
|
||
| def nested_logit_stub() -> Dict[str, Any]: | ||
| try: | ||
| from statsmodels.discrete.discrete_model import MNLogit # noqa: F401 |
Check failure
Code scanning / Pyre
Undefined import Error
Description:
This PR introduces the CR-CA (Causal Reasoning with Counterfactual Analysis) Agent,
a revolutionary causal inference system that implements Pearl's Structural Causal
Model (SCM) framework. The agent transforms resource management by enabling
proactive issue resolution through deep causal analysis, going beyond correlation
to understand true cause-and-effect relationships.
Issue: #1169
Dependencies:
Tag maintainer: @kyegomez
Twitter handle: https://x.com/IlumTheProtogen
================================================================================
WHAT IS THE CR-CA AGENT?
The CR-CA Agent is a sophisticated causal reasoning system that performs:
Unlike traditional correlation-based approaches, the CR-CA Agent implements structural
causal models that enable reliable predictions about intervention effects and
counterfactual reasoning.
================================================================================
ARCHITECTURE OVERVIEW
graph TD A[CR-CA Agent Initialization] --> B[Causal Graph Construction] B --> C[NetworkX DiGraph] C --> D[Variable Standardization] D --> E[Edge Strength Estimation] E --> F[Weighted Least Squares] E --> G[Exponential Decay Weights] E --> H[Ridge Regularization] F --> I[Structural Causal Model] G --> I H --> I I --> J["Linear SCM: y = Σβᵢ·xᵢ + ε"] I --> K[Non-Linear Extensions] I --> L[Interaction Terms] J --> M[Prediction Pipeline] K --> M L --> M M --> N["Standardize Inputs: z = x-μ/σ"] M --> O[Topological Sort] M --> P[Do-Operator Application] N --> Q[Break Parent Dependencies] O --> Q P --> Q Q --> R["Linear Propagation: z_y = Σβᵢ·z_xi"] Q --> S["De-Standardize: x = z·σ + μ"] R --> T[Counterfactual Reasoning] S --> T T --> U[Abduction: Infer Noise ε] T --> V[Action: Apply do-operator] T --> W["Prediction: y_cf = Σβᵢ·x_cf + ε"] U --> X[Root Cause Analysis] V --> X W --> X X --> Y[Backward Tracing] X --> Z[Find All Ancestors] X --> AA[Compute Path Strengths] Y --> AB["Path Strength = ∏β_ij"] Z --> AB AA --> AB AB --> AC[Rank by Multi-Objective] AB --> AD[Exogenous Node Detection] AB --> AE[Intervention Opportunities] AC --> AF[Optimization Methods] AD --> AF AE --> AF AF --> AG[Gradient-Based Optimization] AF --> AJ[Bellman Optimal Intervention] AF --> AN[Evolutionary Multi-Objective] AF --> AT[Convex Optimization] AG --> AH["Finite Differences: ∂y/∂θ"] AH --> AI[L-BFGS-B / BFGS / SLSQP] AJ --> AK[Dynamic Programming] AK --> AL["Value Function: V* = max r + γV*"] AL --> AM[Backward Induction] AN --> AO[NSGA-II Inspired] AO --> AP[Pareto Dominance] AP --> AQ[Tournament Selection] AQ --> AR[Blend Crossover] AR --> AS[Gaussian Mutation] AT --> AU[CVXPY Integration] AI --> AV[Risk Quantification] AM --> AV AS --> AV AU --> AV AV --> AW[Bootstrap Confidence Intervals] AV --> AZ[Bayesian Inference] AV --> BD[CVaR Risk Metric] AW --> AX[Monte Carlo Sampling] AX --> AY["CI = Q_α/2, Q_1-α/2"] AZ --> BA["Prior: β ~ N μ₀, σ₀²"] BA --> BB["Posterior: β|data ~ N μₙ, σₙ²"] BB --> BC[Precision Formulation] BD --> BE[Conditional Value-at-Risk] AY --> BF[Temporal Causal Analysis] BC --> BF BE --> BF BF --> BG[Distributed Lag Models] BF --> BH[VAR Estimation] BF --> BI[Granger Causality] BG --> BJ[F-Statistic Computation] BH --> BJ BI --> BJ BJ --> BK[Impulse Response Functions] BK --> BL[IRF Recursive Computation] BL --> BM[Information Theory] BL --> BT[Explainability] BL --> BZ[Causal Discovery] BM --> BN[Shannon Entropy] BM --> BP[Mutual Information] BM --> BR[Conditional MI] BN --> BO["HX = -Σp log₂ p"] BP --> BQ["IX;Y = HX + HY - HX,Y"] BR --> BS["IX;Y|Z = HX,Z + HY,Z - HX,Y,Z - HZ"] BT --> BU[Shapley Value Attribution] BT --> BW[Integrated Gradients] BU --> BV["φᵢ = Σ S! n-S-1! / n! vS∪i - vS"] BW --> BX["IG = x-x⁰ · ∫₀¹ ∂f/∂x dt"] BX --> BY[Riemann Sum Approximation] BZ --> CA[PC Algorithm] CA --> CB[Conditional Independence Tests] CB --> CC[V-Structure Detection] CC --> CD[Meek's Orientation Rules] BO --> CE[Multi-Layer What-If Analysis] BQ --> CE BS --> CE BV --> CE BY --> CE CD --> CE CE --> CF[Nested Counterfactuals] CE --> CJ[Chain Reaction Detection] CE --> CN[Historical Pattern Matching] CF --> CG[Layer 1: Direct Effects] CF --> CH[Layer 2: Cascades] CF --> CI[Layer 3+: Deep Analysis] CJ --> CK[Feedback Loop Identification] CK --> CL[Cascade Probability] CL --> CM["Pcascade = min 0.95, Path Strength · 0.5 + 0.05"] CN --> CO[Cosine Similarity] CO --> CP[State Similarity] CP --> CQ[Adapt Intervention] CG --> CR[Meta-Learning] CH --> CR CI --> CR CM --> CR CQ --> CR CR --> CS[Learn from Past] CR --> CT[Extract Patterns] CR --> CU[Intervention Strategy] CS --> CV[Performance Optimizations] CT --> CV CU --> CV CV --> CW[LRU Caching] CV --> CX[Hash-Based Cache Keys] CV --> CY[Vectorized Batch Predictions] CV --> CZ[Efficient Topological Sort] CV --> DA[Sparse Graph Operations] CW --> DB[Cross-Validation] CX --> DB CY --> DB CZ --> DB DA --> DB DB --> DC[K-Fold CV] DB --> DD[MSE Computation] DB --> DE[Standard Error] DB --> DF[Sensitivity Analysis] DC --> DG["∂y/∂x_i via Finite Differences"] DD --> DG DE --> DG DF --> DG DG --> DH["Elasticity: E = S · x/y"] DG --> DI["Total Sensitivity: ||∇y||₂"] DG --> DJ[Adversarial Analysis] DH --> DM[Probabilistic Simulation] DI --> DM DJ --> DK[Worst-Case Scenarios] DK --> DL[Robust Interventions] DM --> DN[Monte Carlo Tree] DL --> DN DN --> DO[Uncertainty Propagation] DN --> DP[Edge Strength Perturbation] DO --> DQ[Expected Values] DP --> DQ DQ --> DR[90% Confidence Intervals] DR --> DS[Final Output] DS --> DT[Comprehensive Causal Analysis] DS --> DU[Intervention Recommendations] DS --> DV[Risk-Quantified Decisions] DS --> DW[Explainable Reasoning] DT --> FINAL[Complete CR-CA Analysis] DU --> FINAL DV --> FINAL DW --> FINAL================================================================================
HOW IT REVOLUTIONIZES RESOURCE MANAGEMENT
PROACTIVE PROBLEM SOLVING:
Traditional systems react to problems after they occur. The CR-CA Agent identifies
root causes before issues escalate by:
Example: In supply chain management, instead of reacting to backlog spikes, the agent
traces back through: backlog → inventory → receipts → supplier_capacity, identifying
that supplier capacity constraints are the ultimate root cause. This enables
proactive supplier diversification before crises occur.
OPTIMIZED RESOURCE ALLOCATION:
The agent uses causal understanding to allocate resources efficiently:
Example: Instead of setting safety stock to cover 95% of demand variability, the agent
optimizes z_alpha (safety factor) based on causal relationships between lead time,
demand, and inventory. It calibrates z_alpha to achieve target service levels while
minimizing cost, considering the full causal structure.
EVIDENCE-BASED DECISIONS:
The agent uses causal inference to evaluate intervention effectiveness:
Example: In financial markets, the agent identifies that volume Granger-causes price
(rather than just correlating), enabling more reliable trading strategies based on
causal understanding rather than patterns that may be spurious.
PREDICTIVE INSIGHTS:
The agent anticipates cascading effects of interventions:
Example: In government policy, a tax rate change affects disposable income, which affects
consumption, which affects GDP. The agent models these cascading effects with temporal
lags, predicting the full trajectory of policy impacts over time.
================================================================================
MATHEMATICAL FOUNDATION
STRUCTURAL CAUSAL MODELS (SCM):
The agent implements Pearl's framework where each variable is defined by a structural equation:
y = f(parents(y), ε_y)
For linear SCMs:
y = Σᵢ βᵢ·xᵢ + ε
where βᵢ are structural coefficients representing causal effects, and ε is an error term
representing unobserved confounders.
STANDARDIZATION:
All variables are standardized to z-scores for numerical stability and scale-invariance:
z = (x - μ)/σ
Prediction in z-space:
z_y = Σᵢ βᵢ·z_xi + z_ε
After prediction, values are de-standardized:
x = z·σ + μ
DO-OPERATOR:
The do-operator, do(X=x), represents an intervention that sets variable X to value x,
breaking its dependence on its parents. This is fundamentally different from conditioning:
P(Y | do(X=x)) ≠ P(Y | X=x)
The do-operator enables answering interventional questions: "What would happen if we set X to x?"
COUNTERFACTUAL REASONING:
Pearl's three-step counterfactual reasoning process:
Abduction: Infer latent noise terms from factual observations
ε = y_factual - Σᵢ βᵢ·x_factual,i
Action: Apply do-operator to set intervention values
do(X = x*)
Prediction: Predict counterfactual outcome using new values but old noise
y_cf = Σᵢ βᵢ·x_cf,i + ε
This answers: "What would have happened if X had been x* instead of x_factual?"
================================================================================
CORE IMPLEMENTATION DETAILS
EDGE STRENGTH ESTIMATION:
The agent estimates causal edge strengths using weighted least squares regression:
β = (X' W X + λI)⁻¹ X' W y
where:
Exponential decay weights:
w_i = α^(n-1-i) / Σⱼ α^(n-1-j)
This emphasizes recent observations, making the model adaptive to regime changes.
PREDICTION PROCESS:
ROOT CAUSE ANALYSIS:
Path strength computation:
Path Strength = ∏(i,j)∈Path β_ij
Root causes ranked using multi-objective criteria:
f(rc) = w₁·I_exo(rc) + w₂·S_path(rc) - w₃·D(rc)
where:
CASCADE ANALYSIS:
Cascade probability estimation:
P(cascade) = min(0.95, Path Strength · 0.5 + 0.05)
For each causal path from intervention variables to outcomes:
Path Strength = ∏(i,j)∈Path β_ij
OPTIMIZATION METHODS:
Gradient-Based Optimization:
Objective: maximize predicted outcome
max_θ y(θ)
Gradient computation using finite differences:
∂y/∂θ_i ≈ (y(θ + ε·e_i) - y(θ))/ε
Update rule (gradient descent):
θ_{k+1} = θ_k - α·∇_θ y(θ_k)
Bellman Optimal Intervention (Dynamic Programming):
Value function (Bellman equation):
V*(x_t) = max_u_t [r(x_t, u_t) + γ·V*(f(x_t, u_t))]
Optimal policy:
π*(x_t) = argmax_u_t [r(x_t, u_t) + γ·V*(f(x_t, u_t))]
where:
Multi-Objective Optimization (NSGA-II inspired):
Weighted sum scalarization:
F(x) = Σᵢ wᵢ·fᵢ(x)
Pareto dominance: solution x₁ dominates x₂ if:
∀i: fᵢ(x₁) ≥ fᵢ(x₂) ∧ ∃j: fⱼ(x₁) > fⱼ(x₂)
RISK QUANTIFICATION:
Bootstrap Confidence Intervals:
CI_{1-α} = [Q_{α/2}, Q_{1-α/2}]
where Q_p is the p-th quantile of bootstrap distribution.
Bayesian Inference:
Prior: β ~ N(μ₀, σ₀²)
Posterior: β | data ~ N(μ_n, σ_n²)
Posterior mean:
μ_n = (τ₀·μ₀ + τ_likelihood·n·β̂_OLS) / (τ₀ + τ_likelihood·n)
where τ = 1/σ² is precision.
EXPLAINABILITY:
Shapley Value Attribution:
φᵢ = Σ_{S ⊆ N{i}} [|S|!(n-|S|-1)!/n!] · [v(S∪{i}) - v(S)]
Properties:
Integrated Gradients:
IG_i = (x_i - x_i⁰) · ∫₀¹ [∂f/∂x_i](x⁰ + t·(x - x⁰)) dt
Approximated using Riemann sum:
IG_i ≈ (x_i - x_i⁰) · (1/m) Σⱼ₌₁ᵐ [∂f/∂x_i](x⁰ + (j/m)·(x - x⁰))
================================================================================
CODE SAMPLES: CORE IMPLEMENTATION
The agent initializes with extensive state tracking for standardization, caching,
Bayesian priors, and performance optimizations:
The core prediction method implements topological propagation with proper
standardization and do-operator semantics:
Full implementation of abduction-action-prediction for counterfactual reasoning:
Sophisticated edge strength learning with exponential decay and regularization:
Infinite nesting root cause analysis with multi-objective ranking:
Advanced optimization with numerical gradients and multiple solver methods:
Dynamic programming approach for optimal intervention sequences:
Fair attribution using Shapley values with proper mathematical formulation:
Comprehensive information-theoretic analysis with caching:
Complex nested analysis of cascading chain reactions:
These code samples demonstrate the comprehensive implementation covering:
================================================================================
KEY FEATURES IMPLEMENTED
CAUSAL GRAPH CONSTRUCTION
STRUCTURAL CAUSAL MODELING
COUNTERFACTUAL REASONING
DEEP ROOT CAUSE ANALYSIS
MULTI-LAYER WHAT-IF ANALYSIS
OPTIMAL INTERVENTION PLANNING
RISK-AWARE DECISION MAKING
TEMPORAL CAUSAL ANALYSIS
INFORMATION THEORY
EXPLAINABILITY
PERFORMANCE OPTIMIZATIONS
================================================================================
FILES CHANGED
Core Implementation:
Documentation:
Integration:
Example Implementations:
================================================================================
REAL-WORLD APPLICATIONS AND EXAMPLES
================================================================================
SUPPLY CHAIN MANAGEMENT:
The agent revolutionizes supply chain management by identifying root causes of
disruptions and optimizing inventory policies.
Example from crca_supply_shock_agent.py:
Key Benefits:
================================================================================
MATHEMATICAL CORRECTNESS VERIFICATION
================================================================================
All mathematical formulations are verified:
STANDARDIZATION:
EDGE STRENGTH ESTIMATION:
PREDICTION:
COUNTERFACTUAL REASONING:
PATH STRENGTH:
OPTIMIZATION:
INFORMATION THEORY:
TIME SERIES:
RISK QUANTIFICATION:
EXPLAINABILITY:
================================================================================
TESTING
The implementation includes comprehensive testing considerations:
MATHEMATICAL CORRECTNESS:
CAUSAL GRAPH OPERATIONS:
OPTIMIZATION METHODS:
EDGE CASES:
PERFORMANCE:
================================================================================
DOCUMENTATION
Comprehensive documentation provided:
MATHEMATICAL FOUNDATION:
USAGE EXAMPLES:
API REFERENCE:
BEST PRACTICES:
PERFORMANCE CONSIDERATIONS:
================================================================================
BREAKING CHANGES
None. This is a new feature addition.
================================================================================
BACKWARD COMPATIBILITY
Fully backward compatible. No changes to existing APIs.
================================================================================
PERFORMANCE IMPACT
The CR-CA Agent adds new functionality without impacting existing code performance.
Agent operations are optimized with:
Computational complexity:
================================================================================
CHECKLIST
make lint)make format)make test)================================================================================
MAINTAINER CONTACTS
Maintainer responsibilities:
If no one reviews your PR within a few days, feel free to email Kye at kye@swarms.world
================================================================================
SEE ALSO
Full documentation: docs/swarms/agents/cr_ca_agent.md
Example implementations:
📚 Documentation preview 📚: https://swarms--1181.org.readthedocs.build/en/1181/