sukikrishna/attrib-emergent-behavior

Python

0

29 commits

updated Aug 14, 2026

See the code

README

Emergence Detection via Entropy & Mutual Information

Detect and analyze emergence behavior in multi-agent RL systems through information-theoretic metrics and their dynamics.

Overview

This module detects emergence in multi-agent systems by analyzing:

  1. Entropy (S) - Disorder/diversity of system states
  2. Mutual Information (MI) - Coordination between agents
  3. Speed (dS/dt, dMI/dt) - Rate of change
  4. Acceleration (d²S/dt², d²MI/dt²) - Rate of rate of change

Emergence Signature

Phase 1 - Exploration: High entropy (exploring), low MI (independent) Phase 2 - Emergence: Large |dS/dt|, large |dMI/dt|, rapid coordination forming Phase 3 - Exploitation: Low entropy (converged), high MI (coordinated)


Modules (Core 3)

1. entropy_calculator.py

Calculate Shannon entropy of system states.

Methods:

  • histogram: Fast, requires discretization
  • kde: Smooth, better for continuous
  • plugin: Principled estimator

Key Functions:

calc = EntropyCalculator(method='histogram', bins=10)

# Single entropy value
s = calc.calculate_entropy(states)

# Trajectory of entropies
s_t = calc.calculate_trajectory_entropy(trajectory)

# Joint/conditional entropy
h_xy = calc.calculate_joint_entropy(x_states, y_states)

2. mutual_information.py

Calculate mutual information between agents.

Key Functions:

calc = MutualInformationCalculator(bins=10)

# Pairwise MI
mi = calc.calculate_mutual_information(agent_a_states, agent_b_states)

# Collective MI (all agents)
mi_collective = calc.calculate_collective_mi([agent1, agent2, agent3])

# Pairwise matrix
mi_matrix = calc.calculate_pairwise_mi([agent1, agent2, agent3])

# System properties
redundancy = calc.calculate_redundancy(agent_states)
synergy = calc.calculate_synergy(agent_states)

3. dynamics_analyzer.py

Analyze speed and acceleration of metrics.

Key Functions:

analyzer = DynamicsAnalyzer(smoothing_window=5)

# Derivatives
dS_dt = analyzer.calculate_speed(entropy_ts)
d2S_dt2 = analyzer.calculate_acceleration(entropy_ts)

# Emergence detection
signature = analyzer.calculate_emergence_signature(entropy_ts, mi_ts)
em_score = analyzer.calculate_emergence_score(entropy_ts, mi_ts)
windows = analyzer.identify_emergence_windows(em_score, threshold=0.5)

# Phase transitions
transitions = analyzer.detect_phase_transitions(speed, acceleration)

Installation

pip install -r requirements.txt

Dependencies:

  • numpy >= 1.20.0
  • scipy >= 1.7.0
  • matplotlib >= 3.4.0
  • scikit-learn >= 0.24.0

Quick Start

Test Core Modules

python test_core_modules.py

Output:

  • Validates all three core modules
  • Creates visualization: emergence_analysis.png
  • Prints comprehensive test results

Basic Usage

import numpy as np
from entropy_calculator import EntropyCalculator
from mutual_information import MutualInformationCalculator
from dynamics_analyzer import DynamicsAnalyzer

# Create synthetic data
entropy_ts = np.random.randn(200).cumsum()
mi_ts = -entropy_ts + np.random.randn(200) * 0.2

# Calculate emergence
analyzer = DynamicsAnalyzer()
em_score = analyzer.calculate_emergence_score(entropy_ts, mi_ts)

# Detect emergence windows
windows = analyzer.identify_emergence_windows(em_score, threshold=0.5)
print(f"Emergence windows: {windows}")

Example: Real Multi-Agent System

import numpy as np
from entropy_calculator import EntropyCalculator
from mutual_information import MutualInformationCalculator
from dynamics_analyzer import DynamicsAnalyzer

# Your RL environment
env = YourMultiAgentEnv()
agents = [agent1, agent2, agent3]

# Collect state history (from training)
state_history = []  # shape: (n_timesteps, n_agents, state_dim)

for t in range(10000):
    actions = [agent.act() for agent in agents]
    obs, rewards, done, info = env.step(actions)
    state_history.append(obs)

state_history = np.array(state_history)

# Calculate entropy and MI over time
entropy_calc = EntropyCalculator()
mi_calc = MutualInformationCalculator()

entropy_ts = entropy_calc.calculate_state_entropy_over_time(state_history, window_size=50)
mi_ts = mi_calc.calculate_collective_mi_over_time([state_history[:, i] for i in range(n_agents)], window_size=50)

# Analyze emergence
analyzer = DynamicsAnalyzer()
em_score = analyzer.calculate_emergence_score(entropy_ts, mi_ts)
windows = analyzer.identify_emergence_windows(em_score, threshold=0.5)

print(f"Emergence detected in windows: {windows}")
print(f"Peak emergence score: {em_score.max():.3f}")

Output Interpretation

Emergence Score

  • 0.0-0.2: No emergence (stable baseline)
  • 0.2-0.5: Weak emergence signals
  • 0.5-0.7: Clear emergence occurring
  • 0.7-1.0: Strong emergence with rapid coordination

Phases

  • Exploration: Agents exploring independently
  • Emergence: Phase transition, rapid behavior change
  • Exploitation: Agents highly coordinated, stable pattern

Speed & Acceleration

  • High |dS/dt|: Entropy changing rapidly
  • High |dMI/dt|: Agents rapidly coordinating
  • High d²S/dt²: Sudden shift in exploration/exploitation
  • High d²MI/dt²: Sudden shift in coordination level

Key Equations

Shannon Entropy

S = -Σ p_i * log₂(p_i)

Mutual Information

MI(X;Y) = H(X) + H(Y) - H(X,Y)
        = Σ p(x,y) * log₂(p(x,y) / (p(x)*p(y)))

Speed (1st Derivative)

dS/dt ≈ (S(t+1) - S(t-1)) / 2

Acceleration (2nd Derivative)

d²S/dt² ≈ (dS/dt(t+1) - dS/dt(t-1)) / 2

Emergence Score

E(t) = (|dS/dt| + |dMI/dt|) / (1 + d²S/dt² + d²MI/dt²)
Emergence detected when E(t) > 0.5

Architecture (Future Modules)

Phase 2 will add:

  • emergence_detector.py - Main detection engine
  • multi_agent_rl_wrapper.py - RL integration
  • visualizer.py - Advanced plotting
  • benchmark_scenarios.py - Test environments

Features

✓ Multiple entropy calculation methods (histogram, KDE, plugin) ✓ Pairwise and collective mutual information ✓ System redundancy and synergy measures ✓ Speed and acceleration analysis ✓ Phase transition detection ✓ Emergence scoring and windowing ✓ Stability and baseline comparison ✓ Transfer entropy for directed information flow


Next Steps

  1. Run tests: python test_core_modules.py
  2. Check visualization: emergence_analysis.png
  3. Explore other modules and examples
  4. Integrate with your RL environment

Author Notes

This emergence detection framework is designed for:

  • Multi-agent RL systems
  • Complex adaptive systems
  • Swarm robotics
  • Collective behavior analysis
  • Phase transition studies

The three core modules provide robust foundation for detecting and characterizing emergence through information-theoretic metrics.


License

MIT License - See LICENSE file


Contact & Issues

For questions or issues, refer to the main project documentation.

Contributors

sukikrishna/attrib-emergent-behavior

Python

0

29 commits

updated Aug 14, 2026

See the code

README

Emergence Detection via Entropy & Mutual Information

Detect and analyze emergence behavior in multi-agent RL systems through information-theoretic metrics and their dynamics.

Overview

This module detects emergence in multi-agent systems by analyzing:

  1. Entropy (S) - Disorder/diversity of system states
  2. Mutual Information (MI) - Coordination between agents
  3. Speed (dS/dt, dMI/dt) - Rate of change
  4. Acceleration (d²S/dt², d²MI/dt²) - Rate of rate of change

Emergence Signature

Phase 1 - Exploration: High entropy (exploring), low MI (independent) Phase 2 - Emergence: Large |dS/dt|, large |dMI/dt|, rapid coordination forming Phase 3 - Exploitation: Low entropy (converged), high MI (coordinated)


Modules (Core 3)

1. entropy_calculator.py

Calculate Shannon entropy of system states.

Methods:

  • histogram: Fast, requires discretization
  • kde: Smooth, better for continuous
  • plugin: Principled estimator

Key Functions:

calc = EntropyCalculator(method='histogram', bins=10)

# Single entropy value
s = calc.calculate_entropy(states)

# Trajectory of entropies
s_t = calc.calculate_trajectory_entropy(trajectory)

# Joint/conditional entropy
h_xy = calc.calculate_joint_entropy(x_states, y_states)

2. mutual_information.py

Calculate mutual information between agents.

Key Functions:

calc = MutualInformationCalculator(bins=10)

# Pairwise MI
mi = calc.calculate_mutual_information(agent_a_states, agent_b_states)

# Collective MI (all agents)
mi_collective = calc.calculate_collective_mi([agent1, agent2, agent3])

# Pairwise matrix
mi_matrix = calc.calculate_pairwise_mi([agent1, agent2, agent3])

# System properties
redundancy = calc.calculate_redundancy(agent_states)
synergy = calc.calculate_synergy(agent_states)

3. dynamics_analyzer.py

Analyze speed and acceleration of metrics.

Key Functions:

analyzer = DynamicsAnalyzer(smoothing_window=5)

# Derivatives
dS_dt = analyzer.calculate_speed(entropy_ts)
d2S_dt2 = analyzer.calculate_acceleration(entropy_ts)

# Emergence detection
signature = analyzer.calculate_emergence_signature(entropy_ts, mi_ts)
em_score = analyzer.calculate_emergence_score(entropy_ts, mi_ts)
windows = analyzer.identify_emergence_windows(em_score, threshold=0.5)

# Phase transitions
transitions = analyzer.detect_phase_transitions(speed, acceleration)

Installation

pip install -r requirements.txt

Dependencies:

  • numpy >= 1.20.0
  • scipy >= 1.7.0
  • matplotlib >= 3.4.0
  • scikit-learn >= 0.24.0

Quick Start

Test Core Modules

python test_core_modules.py

Output:

  • Validates all three core modules
  • Creates visualization: emergence_analysis.png
  • Prints comprehensive test results

Basic Usage

import numpy as np
from entropy_calculator import EntropyCalculator
from mutual_information import MutualInformationCalculator
from dynamics_analyzer import DynamicsAnalyzer

# Create synthetic data
entropy_ts = np.random.randn(200).cumsum()
mi_ts = -entropy_ts + np.random.randn(200) * 0.2

# Calculate emergence
analyzer = DynamicsAnalyzer()
em_score = analyzer.calculate_emergence_score(entropy_ts, mi_ts)

# Detect emergence windows
windows = analyzer.identify_emergence_windows(em_score, threshold=0.5)
print(f"Emergence windows: {windows}")

Example: Real Multi-Agent System

import numpy as np
from entropy_calculator import EntropyCalculator
from mutual_information import MutualInformationCalculator
from dynamics_analyzer import DynamicsAnalyzer

# Your RL environment
env = YourMultiAgentEnv()
agents = [agent1, agent2, agent3]

# Collect state history (from training)
state_history = []  # shape: (n_timesteps, n_agents, state_dim)

for t in range(10000):
    actions = [agent.act() for agent in agents]
    obs, rewards, done, info = env.step(actions)
    state_history.append(obs)

state_history = np.array(state_history)

# Calculate entropy and MI over time
entropy_calc = EntropyCalculator()
mi_calc = MutualInformationCalculator()

entropy_ts = entropy_calc.calculate_state_entropy_over_time(state_history, window_size=50)
mi_ts = mi_calc.calculate_collective_mi_over_time([state_history[:, i] for i in range(n_agents)], window_size=50)

# Analyze emergence
analyzer = DynamicsAnalyzer()
em_score = analyzer.calculate_emergence_score(entropy_ts, mi_ts)
windows = analyzer.identify_emergence_windows(em_score, threshold=0.5)

print(f"Emergence detected in windows: {windows}")
print(f"Peak emergence score: {em_score.max():.3f}")

Output Interpretation

Emergence Score

  • 0.0-0.2: No emergence (stable baseline)
  • 0.2-0.5: Weak emergence signals
  • 0.5-0.7: Clear emergence occurring
  • 0.7-1.0: Strong emergence with rapid coordination

Phases

  • Exploration: Agents exploring independently
  • Emergence: Phase transition, rapid behavior change
  • Exploitation: Agents highly coordinated, stable pattern

Speed & Acceleration

  • High |dS/dt|: Entropy changing rapidly
  • High |dMI/dt|: Agents rapidly coordinating
  • High d²S/dt²: Sudden shift in exploration/exploitation
  • High d²MI/dt²: Sudden shift in coordination level

Key Equations

Shannon Entropy

S = -Σ p_i * log₂(p_i)

Mutual Information

MI(X;Y) = H(X) + H(Y) - H(X,Y)
        = Σ p(x,y) * log₂(p(x,y) / (p(x)*p(y)))

Speed (1st Derivative)

dS/dt ≈ (S(t+1) - S(t-1)) / 2

Acceleration (2nd Derivative)

d²S/dt² ≈ (dS/dt(t+1) - dS/dt(t-1)) / 2

Emergence Score

E(t) = (|dS/dt| + |dMI/dt|) / (1 + d²S/dt² + d²MI/dt²)
Emergence detected when E(t) > 0.5

Architecture (Future Modules)

Phase 2 will add:

  • emergence_detector.py - Main detection engine
  • multi_agent_rl_wrapper.py - RL integration
  • visualizer.py - Advanced plotting
  • benchmark_scenarios.py - Test environments

Features

✓ Multiple entropy calculation methods (histogram, KDE, plugin) ✓ Pairwise and collective mutual information ✓ System redundancy and synergy measures ✓ Speed and acceleration analysis ✓ Phase transition detection ✓ Emergence scoring and windowing ✓ Stability and baseline comparison ✓ Transfer entropy for directed information flow


Next Steps

  1. Run tests: python test_core_modules.py
  2. Check visualization: emergence_analysis.png
  3. Explore other modules and examples
  4. Integrate with your RL environment

Author Notes

This emergence detection framework is designed for:

  • Multi-agent RL systems
  • Complex adaptive systems
  • Swarm robotics
  • Collective behavior analysis
  • Phase transition studies

The three core modules provide robust foundation for detecting and characterizing emergence through information-theoretic metrics.


License

MIT License - See LICENSE file


Contact & Issues

For questions or issues, refer to the main project documentation.

Contributors

Languages

Python

72.6%

TeX

19.7%

Shell

6.3%