Skip to content

Kinetic opinion formation

The kinetic system of opinion formation over a discrete grid of opinions.

  • Class: KineticOpinionSystem
  • Import: from skcomplex.systems.social.opinion_dynamics import KineticOpinionSystem
  • Field: social/opinion_dynamics
  • Topics: opinion, kinetic, distribution, polarization
  • References:
    • Bellomo et al. 2017
    • Toscani 2006
    • Deffuant et al. 2000

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 KineticOpinionSystem

trajectory = KineticOpinionSystem().prepare().simulate()

Parameters

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

Parameter Default
levels 7
confidence 2
persuasion 0.3
repulsion 0.1
eta 1.0
solver 'auto'
backend 'auto'

Tags

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

Tag Values
Time continuous
Space lattice
Laws rate
Randomness deterministic
Domains real
Structure fixed

Class

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

class KineticOpinionSystem(BaseSystem):
    """The kinetic system of opinion formation over a discrete grid of opinions.

    The population is a distribution over opinion levels from -1 to 1, a site group on a lattice of the levels, which
    starts almost uniform with a slight lean toward positive opinions. Encounters between levels move an opinion one
    level toward a close opinion and one level away from a distant one, and keep the total density. With a wide
    confidence the opinions reach a consensus around the mean, and with a narrow confidence and repulsion they
    polarise into the two extremes.

    The system has the distribution `opinions` and the encounters `encounters`. Its standard observables are the mean
    opinion and the variance of the opinions.

    Args:
        levels:
            The number of opinion levels.

        confidence:
            The largest distance in levels at which encounters persuade.

        persuasion:
            The probability of moving toward a close opinion.

        repulsion:
            The probability of moving away from a distant opinion.

        eta:
            The encounter rate.

        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 KineticOpinionSystem
        >>> KineticOpinionSystem(levels=5).prepare().system_.elements[0].x.size
        5
    """

    field = 'social/opinion_dynamics'
    topics = ('opinion', 'kinetic', 'distribution', 'polarization')
    references = OPINION_REFERENCES
    until = 60
    dt = 0.5

    @staticmethod
    def _get_levels_of(system: System) -> int:
        """Return the number of opinion levels of a system, the size of its distribution."""
        return int(np.size(next(item.x for item in system.elements if item.label == 'opinions')))

    def __init__(
        self,
        levels: int = 7,
        *,
        confidence: int = 2,
        persuasion: float = 0.3,
        repulsion: float = 0.1,
        eta: float = 1.0,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.levels = levels
        self.confidence = confidence
        self.persuasion = persuasion
        self.repulsion = repulsion
        self.eta = eta

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set `system_` to the components built from the model parameters."""
        super()._prepare(context)
        initial = 1.0 + 0.1 * opinion_grid(self.levels)
        self.system_ = System(
            self.label,
            elements=[
                ActivityDensityElement(
                    'opinions',
                    x=initial / initial.sum(),
                    space=LatticeSpace(self.levels, boundary='fixed'),
                ),
            ],
            interactions=[
                OpinionEncountersInteraction(
                    'encounters',
                    candidate='opinions',
                    field='opinions',
                    levels=self.levels,
                    confidence=self.confidence,
                    eta=self.eta,
                    persuasion=self.persuasion,
                    repulsion=self.repulsion,
                ),
            ],
            space=LatticeSpace(self.levels, boundary='fixed'),
        )
        if not self.observables_:
            self.observables_ = tuple(opinion_observables(KineticOpinionSystem._get_levels_of(self.system_)))

Components

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

Elements:

Interactions:

class ActivityDensityElement(NonnegativeElement):
    """Value `x` of the density of active particles at one activity class.

    In the kinetic theory of active particles of Bellomo et al., a population is this element on a lattice of activity
    classes, with one value per class, and conservative, proliferative, and destructive encounters change it. The
    element has no laws of its own.

    Args:
        label:
            The label of the element, unique among the components of its system.

        x:
            The initial density, a nonnegative number.

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

        space:
            The space with one member per site, such as a lattice of classes, or `None` for none.

    Examples:
        >>> from skcomplex.core.components.spaces import LatticeSpace
        >>> from skcomplex.systems.social.common.kinetics import ActivityDensityElement
        >>> classes = LatticeSpace(5, boundary='fixed')
        >>> ActivityDensityElement('opinions', x=[0.2] * 5, space=classes).x
        [0.2, 0.2, 0.2, 0.2, 0.2]
    """

    def __init__(
        self,
        label: str,
        *,
        x: Annotated[object, RealSet.closed_open(0, oo)] = 0.0,
        size: int | None = None,
        space: BaseSpace | None = None,
    ) -> None:
        super().__init__(label, x=x, size=size, space=space)

class OpinionEncountersInteraction(EncounterInteraction):
    """Conservative encounters between opinion levels.

    They persuade close opinions and repel distant ones.

    Args:
        label:
            The label of the interaction.

        candidate:
            The path of the distribution over opinion levels whose members move.

        field:
            The path of the distribution over opinion levels they meet, usually the same distribution.

        levels:
            The number of opinion levels.

        confidence:
            The largest distance in levels at which encounters persuade.

        eta:
            The encounter rate.

        persuasion:
            The probability of moving toward a close opinion.

        repulsion:
            The probability of moving away from a distant opinion.

    Examples:
        >>> from skcomplex.systems.social.opinion_dynamics import OpinionEncountersInteraction
        >>> OpinionEncountersInteraction('encounters', candidate='opinions', field='opinions').confidence
        2
    """

    def __init__(
        self,
        label: str,
        *,
        candidate: Role,
        field: Role,
        levels: Annotated[int, RealSet.open(0, oo)] = 7,
        confidence: Annotated[int, RealSet.closed_open(0, oo)] = 2,
        eta: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
        persuasion: Annotated[float, RealSet.closed_open(0, oo)] = 0.3,
        repulsion: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
    ) -> None:
        super().__init__(label, candidate=candidate, field=field, rate=self._get_rate, kernel=self._get_kernel)
        self.levels = levels
        self.confidence = confidence
        self.eta = eta
        self.persuasion = persuasion
        self.repulsion = repulsion

    def _get_rate(self, s: SimpleNamespace) -> object:
        """Return the encounter rate."""
        return s.eta

    def _get_kernel(self, s: SimpleNamespace) -> object:
        """Return the table of the persuasion and repulsion of the opinion levels."""
        return persuasion_table(int(self.levels), int(self.confidence), s.persuasion, s.repulsion)