Skip to content

Deffuant bounded confidence

The Deffuant bounded confidence system.

  • Class: DeffuantSystem
  • Import: from skcomplex.systems.social.opinion_dynamics import DeffuantSystem
  • Field: social/opinion_dynamics
  • Topics: opinion, agents, bounded confidence, clusters
  • References:
    • Deffuant et al. 2000
    • Castellano, Fortunato, and Loreto 2009
    • Gros 2015
    • Lenti et al. 2023

Use this system

The block imports the class and simulates the system with its default parameters and its tuned run settings.

from skcomplex.systems.social.opinion_dynamics import DeffuantSystem

trajectory = DeffuantSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
n_agents 200
confidence 0.2
convergence 0.5
steepness None
random_state 0
solver 'auto'
backend 'auto'

Tags

The system with its default parameters has these capability tags, which solvers and analyses check.

Tag Values
Time discrete
Space none
Laws update
Randomness stochastic
Domains real
Structure variable membership

Class

The source of the class shows how it builds its components from its model parameters.

class DeffuantSystem(BaseSystem):
    """The Deffuant bounded confidence system.

    Its opinions settle into about `1 / (2 * confidence)` clusters. The system has the group `people`.

    Args:
        n_agents:
            The number of agents.

        confidence:
            The largest opinion difference at which two agents compromise.

        convergence:
            The fraction of the difference by which each agent of a compromising pair moves.

        steepness:
            The steepness of the probability that a pair compromises, after Lenti et al. (2023), or `None` for the
            deterministic threshold of the confidence.

        random_state:
            The seed or generator of the uniform initial opinions.

        label:
            The label of the system, which names it when it is nested in another one.

        solver:
            The solver of the problem, or `'auto'` for the one the system chooses.

        backend:
            The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.

    Examples:
        >>> from skcomplex.systems.social.opinion_dynamics import DeffuantSystem
        >>> DeffuantSystem(20, random_state=0).prepare().system_.elements[0].size
        20
    """

    field = 'social/opinion_dynamics'
    topics = ('opinion', 'agents', 'bounded confidence', 'clusters')
    references = ('Deffuant et al. 2000', 'Castellano, Fortunato, and Loreto 2009', 'Gros 2015', 'Lenti et al. 2023')
    until = 50

    def __init__(
        self,
        n_agents: int = 200,
        *,
        confidence: float = 0.2,
        convergence: float = 0.5,
        steepness: float | None = None,
        random_state: int | np.random.Generator | None = 0,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.n_agents = n_agents
        self.confidence = confidence
        self.convergence = convergence
        self.steepness = steepness
        self.random_state = random_state

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set `system_` to the components built from the model parameters."""
        super()._prepare(context)
        opinions = np.random.default_rng(self.random_state).random(self.n_agents)
        people = DiscussantElement(
            'people',
            opinion=opinions,
            confidence=self.confidence,
            convergence=self.convergence,
            size=self.n_agents,
        )
        encounters = EncountersInteraction('meetings', target='people', steepness=self.steepness)
        self.system_ = System(self.label, elements=[people], interactions=[encounters])

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

Interactions:

  • EncountersInteraction, from Interaction
class DiscussantElement(Element):
    """Agent of the Deffuant system, with a continuous opinion and a confidence bound.

    Args:
        label:
            The label of the agent or group.

        opinion:
            The initial opinion, between 0 and 1.

        confidence:
            The largest opinion difference at which two agents still compromise.

        convergence:
            The fraction of the opinion difference by which each agent of a compromising pair moves.

        size:
            The number of members, or `None` for one.

    Examples:
        >>> from skcomplex.systems.social.opinion_dynamics import DiscussantElement
        >>> group = DiscussantElement('people', opinion=0.5, size=10)
        >>> group.label, type(group).__name__, group.size
        ('people', 'DiscussantElement', 10)
    """

    visual = VisualSpec('histograms', {'color_by': 'opinion', 'color_range': (0.0, 1.0), 'histogram_first': True})

    def __init__(
        self,
        label: str,
        *,
        opinion: Annotated[object, RealSet.closed_open(0, oo)] = 0.5,
        confidence: Annotated[object, RealSet.closed_open(0, oo)] = 0.2,
        convergence: Annotated[object, RealSet.closed_open(0, oo)] = 0.5,
        size: int | None = None,
    ) -> None:
        super().__init__(label, size=size)
        self.opinion = opinion
        self.confidence = confidence
        self.convergence = convergence

class EncountersInteraction(RandomPairsInteraction):
    """Random binary encounters of the Deffuant system.

    There are as many per step as there are agents. In every encounter two random agents move towards each other by the
    convergence fraction of their difference when a Bernoulli outcome, drawn by the name `outcome`, is 1. Without a
    steepness the outcome is 1 when their opinions differ by less than the confidence bound. With a steepness `rho` it
    is 1 with the probability `1 / (1 + exp(-rho * (confidence - |x_u - x_v|)))` of Lenti et al. (2023), which
    approaches the threshold as `rho` grows. Runs record and replay the outcomes.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the group of discussants.

        phase:
            The phase of the step the encounters run in, or `None` for the default phase.

        steepness:
            The steepness `rho` of the probability of a compromise, or `None` for the deterministic threshold.

    Examples:
        >>> from skcomplex.systems.social.opinion_dynamics import EncountersInteraction
        >>> EncountersInteraction('meetings', target='people', steepness=20.0).steepness
        20.0
    """

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        phase: str | None = None,
        steepness: float | None = None,
    ) -> None:
        super().__init__(label, target=target, phase=phase)
        self.steepness = steepness

    def act_on(self, members: list[AgentViewProtocol], system: SystemView) -> None:
        """Move the opinions of a pair towards each other when their drawn outcome is 1.

        Args:
            members:
                The two discussants of the encounter.

            system:
                The system view of the rule.
        """
        first, second = cast(list[DiscussantView], members)
        difference = second.opinion - first.opinion
        gap = first.confidence - abs(difference)
        probability = float(gap > 0) if self.steepness is None else float(expit(self.steepness * gap))
        if system.draw('outcome', 'bernoulli', p=probability):
            first.opinion += first.convergence * difference
            second.opinion -= first.convergence * difference

Equations

The equations are those that symbolic returns for the system with its default parameters.