Skip to content

Cournot duopoly with beliefs

Two Cournot duopolists that adjust their output to their beliefs.

  • Class: CournotDuopolySystem
  • Import: from skcomplex.systems.economics.markets import CournotDuopolySystem
  • Field: economics/markets
  • Topics: market, oligopoly, agents
  • References:
    • Hamill and Gilbert 2016
    • Cournot 1838

Use this system

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

from skcomplex.systems.economics.markets import CournotDuopolySystem

trajectory = CournotDuopolySystem().prepare().simulate()

Parameters

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

Parameter Default
outputs (100.0, 100.0)
costs (0.0, 0.0)
a 1500.0
b 10.0
error 0.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 unknown
Domains real
Structure variable membership

Class

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

class CournotDuopolySystem(BaseSystem):
    """Two Cournot duopolists that adjust their output to their beliefs.

    The beliefs are about each other. Without errors the outputs converge to the Nash equilibrium,
    `q_i = (a - 2 c_i + c_j) / (3 b)`, which is 50 units each for the defaults. Errors in the beliefs keep the total
    output fluctuating around it. The system has the group `firms`, the total output `total`, and the price `price`.

    Args:
        outputs:
            The initial outputs of the two firms.

        costs:
            The marginal costs of the two firms.

        a:
            The intercept of the inverse demand.

        b:
            The slope of the inverse demand.

        error:
            The largest relative error of a firm's belief about the rival's output.

        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.economics.markets import CournotDuopolySystem
        >>> CournotDuopolySystem(error=0.01).prepare().system_.interactions[0].error
        0.01
    """

    field = 'economics/markets'
    topics = ('market', 'oligopoly', 'agents')
    references = ('Hamill and Gilbert 2016', 'Cournot 1838')
    until = 30
    observables = (
        Observable('total', group='firms', reduce='sum', variable='output'),
        Observable('price', _get_price),
    )

    def __init__(
        self,
        outputs: Sequence[float] = (100.0, 100.0),
        costs: Sequence[float] = (0.0, 0.0),
        *,
        a: float = 1500.0,
        b: float = 10.0,
        error: float = 0.0,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.outputs = outputs
        self.costs = costs
        self.a = a
        self.b = b
        self.error = error

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of the components built from the model parameters."""
        super()._prepare(context)
        firms = DuopolistElement('firms', output=list(self.outputs), cost=list(self.costs), size=N_FIRMS)
        self.system_ = System(
            self.label,
            elements=[firms],
            interactions=[CournotAdjustmentInteraction('market', target='firms', a=self.a, b=self.b, error=self.error)],
        )

Components

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

Elements:

Interactions:

  • CournotAdjustmentInteraction, from Interaction
class DuopolistElement(Element):
    """Firm of a Cournot duopoly.

    It is described by its output and its constant marginal cost.

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

        output:
            The initial output.

        cost:
            The marginal cost.

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

    Examples:
        >>> from skcomplex.systems.economics.markets import DuopolistElement
        >>> DuopolistElement('firms', output=[100.0, 100.0], size=2).size
        2
    """

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

class CournotAdjustmentInteraction(Interaction):
    """Simultaneous best responses of two firms to their beliefs.

    The beliefs are about the rival's last output. Under the inverse demand `P = a - b Q`, each firm sets
    `q_i = (a - c_i) / (2 b) - belief_j / 2`, where the belief is the rival's last output scaled by a factor drawn
    uniformly from `[1 - error, 1 + error]`. It is a rule of the system that sets every output from the outputs of the
    step before, so both firms respond to them.

    Args:
        label:
            The label of the interaction.

        target:
            The path of the group of the two firms.

        a:
            The intercept of the inverse demand.

        b:
            The slope of the inverse demand.

        error:
            The largest relative error of a firm's belief about the rival's output.

    Examples:
        >>> from skcomplex.systems.economics.markets import CournotAdjustmentInteraction
        >>> CournotAdjustmentInteraction('market', target='firms', error=0.1).error
        0.1
    """

    def __init__(
        self,
        label: str,
        *,
        target: Role,
        a: Annotated[float, RealSet.open(0, oo)] = 1500.0,
        b: Annotated[float, RealSet.open(0, oo)] = 10.0,
        error: Annotated[float, RealSet.closed_open(0, oo)] = 0.0,
    ) -> None:
        super().__init__(label)
        self.target = target
        self.a = a
        self.b = b
        self.error = error

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the responses of the firms of every step as the rule of the system.

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

    def respond(self, system: SystemView) -> None:
        """Set the output of every firm to its response to the outputs of the step before.

        Args:
            system:
                The system view of the market.
        """
        firms = cast(list[DuopolistView], system.get_agents('target'))
        responses = [self._get_response(firm, system) for firm in firms]
        for firm, response in zip(firms, responses, strict=True):
            firm.output = response

    def _get_response(self, agent: DuopolistView, system: SystemView) -> float:
        """Return the best response of a firm to its belief.

        The belief is about the last output of its rival.
        """
        a, b, error = (system.get_parameter(name) for name in ('a', 'b', 'error'))
        rival = next(firm for firm in cast(list[DuopolistView], system.get_agents('target')) if firm.id != agent.id)
        belief = rival.output * (1 + error * float(system.rng.uniform(-1.0, 1.0)))
        return max((a - agent.cost) / (2 * b) - belief / 2, 0.0)

Equations

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