Skip to content

Criminality and security

The kinetic system of criminality in a society.

  • Class: CriminalitySystem
  • Import: from skcomplex.systems.social.crowds import CriminalitySystem
  • Field: social/crowds
  • Topics: criminality, security, kinetic, distribution
  • References:
    • Bellomo et al. 2017
    • Bellomo, Colasuonno, Knopoff, and Soler 2015

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.crowds import CriminalitySystem

trajectory = CriminalitySystem().prepare().simulate()

Parameters

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

Parameter Default
classes 5
forces 3
criminals 0.2
security 0.05
attraction 0.3
redemption 0.1
deterrence 2.0
recruitment 0.5
retirement 0.1
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 unknown
Domains real
Structure fixed

Class

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

class CriminalitySystem(BaseSystem):
    """The kinetic system of criminality in a society.

    It includes the security forces that act against it. Citizens are a distribution over criminality classes and
    security agents a distribution over efficacy classes, held by site groups on lattices of the classes. Encounters
    between citizens spread crime when it attracts more than honesty redeems, encounters with security agents move
    criminal citizens toward honesty, a micro-scale action, and the security forces grow in proportion to the mean
    criminality, a macro-scale action that reads a moment of the citizens. The security forces grow while crime is high,
    crime recedes, and the forces retire, so crime and security oscillate toward a balance where some crime persists.

    The system has the distributions `citizens` and `security` and the interactions `contagion`, `deterrence`, and
    `recruitment`. Its standard observables are the mean criminality, the criminal fraction, and the density of the
    security forces.

    Args:
        classes:
            The number of criminality classes.

        forces:
            The number of efficacy classes of the security forces.

        criminals:
            The initial fraction of citizens in the most criminal class, the others being honest.

        security:
            The initial density of the security forces, spread evenly over their efficacy classes.

        attraction:
            The probability that a citizen who meets a more criminal one moves one class up.

        redemption:
            The probability that a citizen who meets a more honest one moves one class down.

        deterrence:
            The rate at which the most effective agents deter a criminal citizen.

        recruitment:
            The recruitment rate of security agents per unit of mean criminality.

        retirement:
            The retirement rate of security agents.

        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.crowds import CriminalitySystem
        >>> [item.label for item in CriminalitySystem().prepare().system_.interactions]
        ['contagion', 'deterrence', 'recruitment']
    """

    field = 'social/crowds'
    topics = ('criminality', 'security', 'kinetic', 'distribution')
    references = CRIMINALITY_REFERENCES
    until = 60
    dt = 0.5

    @staticmethod
    def _get_sizes_of(system: System) -> tuple[int, int]:
        """Return the number of criminality classes and of efficacy classes of a system."""
        sizes = {item.label: int(item.size or np.size(getattr(item, 'x', ()))) for item in system.elements}
        return sizes['citizens'], sizes['security']

    def __init__(
        self,
        classes: int = 5,
        forces: int = 3,
        *,
        criminals: float = 0.2,
        security: float = 0.05,
        attraction: float = 0.3,
        redemption: float = 0.1,
        deterrence: float = 2.0,
        recruitment: float = 0.5,
        retirement: float = 0.1,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.classes = classes
        self.forces = forces
        self.criminals = criminals
        self.security = security
        self.attraction = attraction
        self.redemption = redemption
        self.deterrence = deterrence
        self.recruitment = recruitment
        self.retirement = retirement

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set `system_` to the components built from the model parameters."""
        super()._prepare(context)
        citizens = np.zeros(self.classes)
        citizens[0], citizens[-1] = 1.0 - self.criminals, self.criminals
        self.system_ = System(
            self.label,
            elements=[
                ActivityDensityElement('citizens', x=citizens, space=LatticeSpace(self.classes, boundary='fixed')),
                SecurityAgentsElement(
                    'security',
                    x=np.full(self.forces, self.security / self.forces),
                    share=np.full(self.forces, 1.0 / self.forces),
                    space=LatticeSpace(self.forces, boundary='fixed'),
                ),
            ],
            interactions=[
                CriminalContagionInteraction(
                    'contagion',
                    candidate='citizens',
                    field='citizens',
                    classes=self.classes,
                    attraction=self.attraction,
                    redemption=self.redemption,
                ),
                DeterrenceInteraction(
                    'deterrence',
                    candidate='citizens',
                    field='security',
                    classes=self.classes,
                    forces=self.forces,
                    deterrence=self.deterrence,
                ),
                RecruitmentInteraction(
                    'recruitment',
                    target='security',
                    citizens='citizens',
                    classes=self.classes,
                    recruitment=self.recruitment,
                    retirement=self.retirement,
                ),
            ],
            space=LatticeSpace(self.classes, boundary='fixed'),
        )
        if not self.observables_:
            self.observables_ = tuple(criminality_observables(*CriminalitySystem._get_sizes_of(self.system_)))

Components

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

Elements:

Interactions:

  • CriminalContagionInteraction, from Interaction, through the shared recipe EncounterInteraction
  • DeterrenceInteraction, from Interaction, through the shared recipe EncounterInteraction
  • RecruitmentInteraction, from Interaction
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 SecurityAgentsElement(ActivityDensityElement):
    """Density of security agents at one efficacy class.

    It holds the share of new recruits that join the class.

    Args:
        label:
            The label of the distribution.

        x:
            The initial density.

        share:
            The share of new recruits that join the efficacy class.

        size:
            The number of classes, or `None` for one or one per site of the space.

        space:
            The lattice whose sites are the efficacy classes, or `None` for none.

    Examples:
        >>> from skcomplex.systems.social.crowds import SecurityAgentsElement
        >>> SecurityAgentsElement('security', share=[0.5, 0.3, 0.2], size=3).size
        3
    """

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

class CriminalContagionInteraction(EncounterInteraction):
    """Conservative encounters between citizens.

    In them crime attracts and honesty redeems.

    Args:
        label:
            The label of the interaction.

        candidate:
            The path of the distribution of citizens whose members move.

        field:
            The path of the distribution of citizens they meet, usually the same distribution.

        classes:
            The number of criminality classes.

        eta:
            The encounter rate.

        attraction:
            The probability that a citizen who meets a more criminal one moves one class up.

        redemption:
            The probability that a citizen who meets a more honest one moves one class down.

    Examples:
        >>> from skcomplex.systems.social.crowds import CriminalContagionInteraction
        >>> CriminalContagionInteraction('contagion', candidate='citizens', field='citizens').attraction
        0.3
    """

    def __init__(
        self,
        label: str,
        *,
        candidate: Role,
        field: Role,
        classes: Annotated[int, RealSet.open(0, oo)] = 5,
        eta: Annotated[float, RealSet.closed_open(0, oo)] = 1.0,
        attraction: Annotated[float, RealSet.closed_open(0, oo)] = 0.3,
        redemption: 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.classes = classes
        self.eta = eta
        self.attraction = attraction
        self.redemption = redemption

    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 attraction of crime and the redemption of honesty."""
        return contagion_table(int(self.classes), s.attraction, s.redemption)

class DeterrenceInteraction(EncounterInteraction):
    """Encounters of citizens with security forces.

    They move criminal citizens toward honesty.

    Args:
        label:
            The label of the interaction.

        candidate:
            The path of the distribution of citizens.

        field:
            The path of the distribution of the security forces over efficacy.

        classes:
            The number of criminality classes.

        forces:
            The number of efficacy classes of the security forces.

        deterrence:
            The rate at which the most effective agents deter a criminal citizen.

    Examples:
        >>> from skcomplex.systems.social.crowds import DeterrenceInteraction
        >>> DeterrenceInteraction('deterrence', candidate='citizens', field='security').deterrence
        2.0
    """

    def __init__(
        self,
        label: str,
        *,
        candidate: Role,
        field: Role,
        classes: Annotated[int, RealSet.open(0, oo)] = 5,
        forces: Annotated[int, RealSet.open(0, oo)] = 3,
        deterrence: Annotated[float, RealSet.closed_open(0, oo)] = 2.0,
    ) -> None:
        super().__init__(label, candidate=candidate, field=field, rate=self._get_rate, kernel=self._get_kernel)
        self.classes = classes
        self.forces = forces
        self.deterrence = deterrence

    def _get_rate(self, s: SimpleNamespace) -> object:
        """Return the deterrence rate of every pair of classes.

        A pair holds a criminality class and an efficacy class.
        """
        return deterrence_table(int(self.classes), int(self.forces))[0] * s.deterrence

    def _get_kernel(self, s: SimpleNamespace) -> object:
        """Return the table that moves a deterred citizen one class down."""
        return deterrence_table(int(self.classes), int(self.forces))[1]

class RecruitmentInteraction(Interaction):
    """Recruitment of security agents in proportion to the mean criminality.

    It is a macro-scale external action, a rate of the density `x` of the security forces that reads the citizens. The
    security forces grow at the rate `recruitment m share_k` in efficacy class `k`, where `m` is the mean criminality of
    the citizens, a moment of their distribution, and retire at the rate `retirement`.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the distribution of the security forces over efficacy.

        citizens:
            The path of the distribution of citizens.

        classes:
            The number of criminality classes.

        recruitment:
            The recruitment rate per unit of mean criminality.

        retirement:
            The retirement rate.

    Examples:
        >>> from skcomplex.systems.social.crowds import RecruitmentInteraction
        >>> RecruitmentInteraction('recruitment', target='security', citizens='citizens').retirement
        0.1
    """

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        citizens: Role,
        classes: Annotated[int, RealSet.open(0, oo)] = 5,
        recruitment: Annotated[float, RealSet.closed_open(0, oo)] = 0.5,
        retirement: Annotated[float, RealSet.closed_open(0, oo)] = 0.1,
    ) -> None:
        super().__init__(label)
        self.target = target
        self.citizens = citizens
        self.classes = classes
        self.recruitment = recruitment
        self.retirement = retirement

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the recruitment less the retirement of the security forces as the rate of their density.

        Args:
            context:
                The namespace of the elements a binding of the roles names.
        """
        super()._prepare(context)
        self.laws_: list[BaseLaw] = [RateLaw(context.target.x, self._get_rate(context))]

    def _get_rate(self, s: SimpleNamespace) -> Expression:
        """Return the recruitment of security agents, less the retirement.

        The recruitment is in proportion to the mean criminality. The criminality is that of the citizens.
        """
        security, citizens = s.target, s.citizens
        criminality = moment(citizens.x, 1, criminality_grid(int(self.classes)), normalized=True)
        return s.recruitment * criminality * security.share - s.retirement * security.x