Zero-intelligence double auction
Gode and Sunder's double auction of zero-intelligence traders.
- Class:
DoubleAuctionSystem - Import:
from skcomplex.systems.economics.markets import DoubleAuctionSystem - Field:
economics/markets - Topics:
market,agents,double auction,zero intelligence - References:
- Gode and Sunder 1993
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 DoubleAuctionSystem
trajectory = DoubleAuctionSystem().prepare().simulate()
Parameters
The model parameters take these defaults, and set_params changes them.
| Parameter | Default |
|---|---|
n_buyers |
50 |
n_sellers |
50 |
protocol |
'order_book' |
rounds |
20 |
constrained |
True |
ceiling |
200.0 |
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 DoubleAuctionSystem(BaseSystem):
"""Gode and Sunder's double auction of zero-intelligence traders.
Each step is a trading period. Every buyer values one unit and every seller can produce one unit at a cost, both
drawn uniformly below the price ceiling. During a trading period of several rounds, every trader that has not traded
yet quotes a random price and the market matches the quotes, continuously in a limit order book or at one uniform
price in a call auction. With the budget constraint (ZI-C) no trader accepts a loss, and the market alone reaches
nearly the whole competitive surplus. Without it (ZI-U), traders make losing trades and the efficiency drops.
The traders are the zero-intelligence traders of Gode and Sunder (1993). The double auction of Gooding (2019), whose
traders adapt their bids and asks, is a different model. The system has the groups `buyers` and `sellers`, the
market `auction`, and the realised `ledger.surplus` and `ledger.efficiency` of every trading period.
Args:
n_buyers:
The number of buyers.
n_sellers:
The number of sellers.
protocol:
The protocol of the market, `'order_book'` or `'call_auction'`.
rounds:
The number of quoting rounds of a trading period.
constrained:
Whether traders never quote at a loss.
ceiling:
The highest price of the market.
random_state:
The seed or generator of the values and the costs.
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.
Raises:
ValueError:
The protocol is unknown.
Examples:
>>> from skcomplex.systems.economics.markets import DoubleAuctionSystem
>>> [group.size for group in DoubleAuctionSystem(10, 8).prepare().system_.elements]
[10, 8]
"""
field = 'economics/markets'
topics = ('market', 'agents', 'double auction', 'zero intelligence')
references = ('Gode and Sunder 1993',)
until = 20
def __init__(
self,
n_buyers: int = 50,
n_sellers: int = 50,
*,
protocol: str = 'order_book',
rounds: int = 20,
constrained: bool = True,
ceiling: float = 200.0,
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_buyers = n_buyers
self.n_sellers = n_sellers
self.protocol = protocol
self.rounds = rounds
self.constrained = constrained
self.ceiling = ceiling
self.random_state = random_state
@staticmethod
def _record_profit(trade: MatchSpec, system: SystemView) -> None:
"""Add the surplus of a trade to the profits of the buyer and the seller."""
del system
buyer, seller = trade.requester, trade.provider
if not isinstance(buyer, ZeroIntelligenceBuyerView) or not isinstance(seller, ZeroIntelligenceSellerView):
return
buyer.profit += trade.quantity * (buyer.value - trade.value)
seller.profit += trade.quantity * (trade.value - seller.value)
def _prepare(self, context: Sequence[Observable] | None = None) -> None:
"""Set the system of the components built from the model parameters."""
super()._prepare(context)
if self.protocol not in PROTOCOLS:
error_msg = f'Unknown protocol {self.protocol!r}. Use one of {PROTOCOLS}.'
raise ValueError(error_msg)
rng = np.random.default_rng(self.random_state)
values = rng.uniform(0.25 * self.ceiling, self.ceiling, self.n_buyers)
costs = rng.uniform(0.0, 0.75 * self.ceiling, self.n_sellers)
limit = float(self.constrained)
buyers = ZeroIntelligenceBuyerElement(
'buyers',
value=values,
ceiling=self.ceiling,
constrained=limit,
size=self.n_buyers,
)
sellers = ZeroIntelligenceSellerElement(
'sellers',
value=costs,
ceiling=self.ceiling,
constrained=limit,
size=self.n_sellers,
)
market = MatchingInteraction(
'auction',
requesters='buyers',
providers='sellers',
rule=ContinuousMatchingSpec() if self.protocol == 'order_book' else BatchMatchingSpec(),
reserve=('bid', 'ask'),
quantity=('demand', 'supply'),
phase='trade',
on_match=self._record_profit,
)
self.system_ = System(
self.label,
elements=[buyers, sellers],
interactions=[
QuotingInteraction('quotes', buyers='buyers', sellers='sellers'),
market,
AuctionLedgerInteraction('ledger', phase='close'),
],
solver=MesaSolver(phases=[PhaseSpec('trade', repeat=self.rounds), PhaseSpec('close')]),
)
Components
The system uses these element and interaction classes, each with its nearest class of the core.
Elements:
Interactions:
QuotingInteraction, fromInteractionMatchingInteraction, fromInteraction, through the shared recipeMatchingInteractionAuctionLedgerInteraction, fromInteraction
class ZeroIntelligenceBuyerElement(Element):
"""Buyer of one unit per trading period who bids a random price.
Under the budget constraint, the bid is below its value. In every round of a trading period, a buyer that has not
bought yet bids a price drawn uniformly from 0 to its value, or to the price ceiling without the constraint, the
zero-intelligence traders of Gode and Sunder (1993).
Args:
label:
The label of the agent or group.
value:
The value of the unit to the buyer, its redemption value.
bid:
The current bid.
demand:
The number of units still wanted in the trading period.
profit:
The accumulated surplus of the buyer's trades.
ceiling:
The highest price of the market.
constrained:
One when the buyer never bids above its value, zero otherwise.
size:
The number of members, or `None` for one.
Examples:
>>> from skcomplex.systems.economics.markets import ZeroIntelligenceBuyerElement
>>> ZeroIntelligenceBuyerElement('buyers', value=100.0, size=5).size
5
"""
def __init__(
self,
label: str,
*,
value: Annotated[object, RealSet.closed_open(0, oo)] = 100.0,
bid: Annotated[object, RealSet.closed_open(0, oo)] = 0.0,
demand: Annotated[object, RealSet.closed_open(0, oo)] = 1.0,
profit: object = 0.0,
ceiling: Annotated[object, RealSet.open(0, oo)] = 200.0,
constrained: Annotated[object, RealSet.closed_open(0, oo)] = 1.0,
size: int | None = None,
) -> None:
super().__init__(label, size=size)
self.value = value
self.bid = bid
self.demand = demand
self.profit = profit
self.ceiling = ceiling
self.constrained = constrained
class ZeroIntelligenceSellerElement(Element):
"""Seller of one unit per trading period who asks a random price.
Under the budget constraint, the ask is above its cost. In every round of a trading period, a seller that has not
sold yet asks a price drawn uniformly from its cost, or from 0 without the constraint, to the price ceiling.
Args:
label:
The label of the agent or group.
value:
The cost of the unit to the seller.
ask:
The current ask.
supply:
The number of units still offered in the trading period.
profit:
The accumulated surplus of the seller's trades.
ceiling:
The highest price of the market.
constrained:
One when the seller never asks below its cost, zero otherwise.
size:
The number of members, or `None` for one.
Examples:
>>> from skcomplex.systems.economics.markets import ZeroIntelligenceSellerElement
>>> ZeroIntelligenceSellerElement('sellers', value=50.0, size=5).size
5
"""
def __init__(
self,
label: str,
*,
value: Annotated[object, RealSet.closed_open(0, oo)] = 100.0,
ask: Annotated[object, RealSet.closed_open(0, oo)] = 200.0,
supply: Annotated[object, RealSet.closed_open(0, oo)] = 1.0,
profit: object = 0.0,
ceiling: Annotated[object, RealSet.open(0, oo)] = 200.0,
constrained: Annotated[object, RealSet.closed_open(0, oo)] = 1.0,
size: int | None = None,
) -> None:
super().__init__(label, size=size)
self.value = value
self.ask = ask
self.supply = supply
self.profit = profit
self.ceiling = ceiling
self.constrained = constrained
class QuotingInteraction(Interaction):
"""Quotes of the zero-intelligence traders in every round.
Every buyer bids a price drawn uniformly from 0 to its value and every seller asks a price drawn uniformly from its
cost to the price ceiling. Without the budget constraint, the bid goes up to the ceiling and the ask starts from 0.
Args:
label:
The label of the quotes.
buyers:
The path of the group of buyers.
sellers:
The path of the group of sellers.
phase:
The phase the traders quote in.
Examples:
>>> from skcomplex.systems.economics.markets import QuotingInteraction
>>> QuotingInteraction('quotes', buyers='buyers', sellers='sellers').phase
'trade'
"""
law_classes = (UpdateLaw,)
def __init__(self, label: str, *, buyers: Role, sellers: Role, phase: str | None = 'trade') -> None:
super().__init__(label)
self.buyers = buyers
self.sellers = sellers
self.phase = phase
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the quotes of every round 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.quote, phase=self.phase)]
def quote(self, system: SystemView) -> None:
"""Draw the bid of every buyer and then the ask of every seller.
Args:
system:
The system view of the quotes.
"""
for buyer in cast(list[ZeroIntelligenceBuyerView], system.get_agents('buyers')):
buyer.bid = float(system.rng.uniform(0.0, buyer.value if buyer.constrained else buyer.ceiling))
for seller in cast(list[ZeroIntelligenceSellerView], system.get_agents('sellers')):
bottom = seller.value if seller.constrained else 0.0
seller.ask = float(system.rng.uniform(bottom, max(bottom, seller.ceiling)))
class MatchingInteraction[SystemT: SystemViewProtocol](Interaction):
"""An interaction that matches requesters with providers and settles every match.
Requesters state in their states the highest value they accept, `reserve`, the quantity they want, `quantity`, and
the most value they can give, `cap`. Providers state the lowest value they accept and the quantity they offer. A
name given as a pair names the state of the requesters first and of the providers second. The `rule` is a spec of
the algorithm that pairs the offers, such as `RandomSearchSpec`, `QueueSpec`, `BatchMatchingSpec`, or
`StableMatchingSpec`.
Every match lowers the quantities of both sides and moves every state of `transfers`: a negative sign moves `value *
quantity` from the requester to the provider, and a positive sign moves `quantity` from the provider to the
requester, so the totals stay the same. `on_match(match, system)` adds any other effect. The matching runs once per
round of its phase and reports `<label>.value`, the clearing value weighted by quantity, `<label>.volume`, and
`<label>.unmatched`.
Unlike a labelled `RateLaw` flow, which moves amounts between given pairs, it first forms the pairs by a rule,
through `pairs` of the core, and then transfers. Choose it when who trades with whom and at what value are part of
the model.
It appears in many fields. Buyers and sellers trade goods, workers and firms agree on wages, borrowers and banks
agree on loans, and patients and hospitals are matched.
Args:
label:
The label of the matching.
requesters:
The path of the group of requesters.
providers:
The path of the group of providers.
rule:
The spec of the algorithm that matches the members, such as `ContinuousMatchingSpec()`, or a function
`rule(requesters, providers, rng, capacity)` of the agents with a positive quantity that returns their
pairs, which match at the midpoint of both reserves. A `PartnerSpec` picks among the providers that still
offer a quantity at a reserve the requester accepts, in random order of the requesters, and settles at
the midpoint, or at the value of a `PartnerMatchingSpec`.
reserve:
The state with the reserve of both sides, or a pair of the requesters' and the providers' states. An agent
without the default state `'reserve'` has no reserve as a requester and the reserve 0 as a provider.
quantity:
The state with the quantity of both sides, or a pair of the requesters' and the providers' states. An
agent without the default state `'quantity'` offers the quantity 1.
cap:
The state of a requester with the most value it can give. A requester without the default state
`'cap'` has no reserve on what it gives.
transfers:
The states every match moves, each with the sign `-1` for a move from the requester to the provider or
`1` for a move from the provider to the requester.
phase:
The phase the matching runs in, or `None` for the default phase.
on_match:
A function of every match and the system view, run after the transfers, or `None`.
Raises:
ValidationError:
A transfer has a sign other than `-1` or `1`, or the rule is neither a matching spec nor a function, or
it is invalid, raised by `prepare`, or an agent lacks a state other than a default one that names its
reserve, quantity, or cap, raised by `run`.
Examples:
>>> from skcomplex.systems.general.matching import MatchingInteraction
>>> from skcomplex.systems.general.matching import ContinuousMatchingSpec
>>> matching = MatchingInteraction(
... 'exchange',
... requesters='traders',
... providers='traders',
... rule=ContinuousMatchingSpec(),
... reserve=('bid', 'ask'),
... )
>>> matching.reports
('value', 'volume', 'unmatched')
"""
reports: ClassVar[tuple[str, ...]] = ('value', 'volume', 'unmatched')
law_classes: ClassVar[tuple[type[BaseLaw], ...]] = (UpdateLaw,)
def _get_draws(self) -> bool:
"""Return whether the rule draws.
Every rule draws but a queue served in a fixed order.
"""
return super()._get_draws() or not (isinstance(self.rule, QueueSpec) and self.rule.order != 'random')
def __init__(
self,
label: str,
*,
requesters: Role,
providers: Role,
rule: MatchingRule | Pairing = RandomSearchSpec(),
reserve: str | tuple[str, str] = 'reserve',
quantity: str | tuple[str, str] = 'quantity',
cap: str = 'cap',
transfers: Sequence[tuple[str, int]] = (),
phase: str | None = None,
on_match: Callable[[MatchSpec, SystemT], None] | None = None,
) -> None:
super().__init__(label)
self.requesters = requesters
self.providers = providers
self.rule = rule
self.reserve = reserve
self.quantity = quantity
self.cap = cap
self.transfers = transfers
self.phase = phase
self.on_match = on_match
@staticmethod
def _get_sides(name: str | tuple[str, str]) -> tuple[str, str]:
"""Return the state of the requesters and of the providers of a name.
The name is given once or as a pair.
"""
return (name, name) if isinstance(name, str) else name
def _read(self, agent: AgentViewProtocol, name: str, default: float) -> float:
"""Return a state of an agent.
It returns a default when the agent lacks a state of a default name.
"""
if hasattr(agent, name):
return float(getattr(agent, name))
if name in DEFAULT_STATES:
return default
error_msg = (
f'The agents of {agent.group!r} have no state {name!r}, which the matching {self.label!r} reads. Declare '
f'the state on their element, or name one of their states.'
)
raise ValidationError(error_msg)
@staticmethod
def _get_affordable(offer: SimpleNamespace, value: float) -> float:
"""Return the quantity a participant can still match at a value.
The value is within its cap.
"""
if value <= 0:
return float(offer.quantity)
return max(0.0, min(offer.quantity, offer.cap / value))
@staticmethod
def _record_match(
trades: list[MatchSpec],
requester: SimpleNamespace,
provider: SimpleNamespace,
value: float,
) -> None:
"""Record a match of the largest quantity both sides can match at a value.
It also lowers both offers.
"""
quantity = min(MatchingInteraction._get_affordable(requester, value), provider.quantity)
if quantity <= 0:
return
requester.quantity -= quantity
requester.cap -= quantity * value
provider.quantity -= quantity
trades.append(MatchSpec(requester.agent, provider.agent, quantity, value))
@staticmethod
def _get_value(requester: SimpleNamespace, provider: SimpleNamespace, clearing: str) -> float:
"""Return the value of a match under a clearing rule."""
if clearing == 'requester':
return requester.reserve
if clearing == 'provider':
return provider.reserve
return (requester.reserve + provider.reserve) / 2
@staticmethod
def _get_order(offers: list[SimpleNamespace], order: str, rng: np.random.Generator) -> list[SimpleNamespace]:
"""Return offers in random order, by decreasing reserve, or in order of creation."""
if order == 'reserve':
return sorted(offers, key=lambda item: (-item.reserve, item.agent.id))
if order == 'sequential':
return sorted(offers, key=lambda item: item.agent.id)
return [offers[index] for index in rng.permutation(len(offers))]
def _get_offers(self, system: SystemT) -> tuple[list[SimpleNamespace], list[SimpleNamespace]]:
"""Return the offers of the requesters and providers with a positive quantity.
Matches lower the quantity.
"""
reserves, quantities = self._get_sides(self.reserve), self._get_sides(self.quantity)
requesters = [
SimpleNamespace(
agent=agent,
reserve=self._read(agent, reserves[0], math.inf),
quantity=self._read(agent, quantities[0], 1.0),
cap=self._read(agent, self.cap, math.inf),
)
for agent in system.get_agents(self._get_role('requesters'))
]
providers = [
SimpleNamespace(
agent=agent,
reserve=self._read(agent, reserves[1], 0.0),
quantity=self._read(agent, quantities[1], 1.0),
cap=math.inf,
)
for agent in system.get_agents(self._get_role('providers'))
]
return [offer for offer in requesters if offer.quantity > 0], [
offer for offer in providers if offer.quantity > 0
]
def _match_random_search(
self,
rule: RandomSearchSpec,
requesters: list[SimpleNamespace],
providers: list[SimpleNamespace],
rng: np.random.Generator,
) -> list[MatchSpec]:
"""Return the matches of requesters in random order that meet random providers.
They match on crossing reserves.
"""
trades: list[MatchSpec] = []
for index in rng.permutation(len(requesters)):
requester = requesters[index]
for _ in range(rule.tries):
available = [provider for provider in providers if provider.quantity > 0]
if requester.quantity <= 0 or not available:
break
provider = available[int(rng.integers(len(available)))]
if provider.reserve <= requester.reserve:
self._record_match(trades, requester, provider, self._get_value(requester, provider, rule.value))
return trades
@staticmethod
def _choose_partner(
rule: PartnerSwitchSpec,
best: SimpleNamespace,
current: SimpleNamespace | None,
rng: np.random.Generator,
) -> SimpleNamespace:
"""Return the provider a requester matches with.
It is its current partner or the best of its sample.
"""
if current is None or current.quantity <= 0:
return best
if best.reserve >= current.reserve:
return current
if best.reserve <= 0:
return best
switching = 1 - math.exp(rule.intensity * (best.reserve - current.reserve) / best.reserve)
return best if rng.random() < switching else current
def _match_partner_switch(
self,
rule: PartnerSwitchSpec,
requesters: list[SimpleNamespace],
providers: list[SimpleNamespace],
rng: np.random.Generator,
) -> list[MatchSpec]:
"""Return the matches of every requester with its kept or new partner.
The matches are at the partner's reserve.
"""
trades: list[MatchSpec] = []
by_id = {provider.agent.id: provider for provider in providers}
for index in rng.permutation(len(requesters)):
requester = requesters[index]
available = [provider for provider in providers if provider.quantity > 0]
if not available:
break
picks = rng.choice(len(available), size=min(rule.sample, len(available)), replace=False)
best = min((available[int(pick)] for pick in picks), key=lambda provider: provider.reserve)
current = by_id.get(int(getattr(requester.agent, rule.partner, -1)))
chosen = self._choose_partner(rule, best, current, rng)
if hasattr(requester.agent, rule.partner):
setattr(requester.agent, rule.partner, chosen.agent.id)
if chosen.reserve <= requester.reserve:
self._record_match(trades, requester, chosen, chosen.reserve)
return trades
def _match_queue(
self,
rule: QueueSpec,
requesters: list[SimpleNamespace],
providers: list[SimpleNamespace],
rng: np.random.Generator,
) -> list[MatchSpec]:
"""Return the matches of the queue of requesters served by the lowest providers.
The providers serve at their reserves.
"""
queue = self._get_order(requesters, rule.order, rng)
stock = sorted(providers, key=lambda provider: provider.reserve)
trades: list[MatchSpec] = []
for requester in queue:
for provider in stock:
if requester.quantity <= 0 or provider.reserve > requester.reserve:
break
if provider.quantity > 0:
self._record_match(trades, requester, provider, provider.reserve)
return trades
@staticmethod
def _adjust(
rule: IterativeMatchingSpec,
requesters: list[SimpleNamespace],
providers: list[SimpleNamespace],
start: float,
) -> float:
"""Return the value the tâtonnement reaches from a start."""
value = start
for _ in range(rule.iterations):
requested = sum(
MatchingInteraction._get_affordable(requester, value)
for requester in requesters
if requester.reserve >= value
)
provided = sum(provider.quantity for provider in providers if provider.reserve <= value)
total = requested + provided
if not total or abs(requested - provided) / total < rule.tolerance:
break
value *= 1 + rule.adjustment * (requested - provided) / total
return value
def _match_at(
self,
requesters: list[SimpleNamespace],
providers: list[SimpleNamespace],
value: float,
rng: np.random.Generator,
) -> list[MatchSpec]:
"""Return the matches at one value between the willing requesters and providers.
They match in random order.
"""
trades: list[MatchSpec] = []
willing_requesters = self._get_order([item for item in requesters if item.reserve >= value], 'random', rng)
willing_providers = self._get_order([item for item in providers if item.reserve <= value], 'random', rng)
pairs = iter(willing_providers)
provider = next(pairs, None)
for requester in willing_requesters:
while provider is not None and MatchingInteraction._get_affordable(requester, value) > 0:
self._record_match(trades, requester, provider, value)
if provider.quantity <= 0:
provider = next(pairs, None)
return trades
def _match_iterative(
self,
rule: IterativeMatchingSpec,
requesters: list[SimpleNamespace],
providers: list[SimpleNamespace],
rng: np.random.Generator,
storage: MutableMapping[str, object],
) -> list[MatchSpec]:
"""Return the matches at the value of the tâtonnement.
The tâtonnement starts from the value of the last clearing.
"""
reserves = [offer.reserve for offer in requesters + providers if offer.reserve > 0]
if not reserves:
return []
last = storage.get('value')
start = last if isinstance(last, float) and math.isfinite(last) and last > 0 else float(np.median(reserves))
value = self._adjust(rule, requesters, providers, start)
storage['value'] = value
return self._match_at(requesters, providers, value, rng)
@staticmethod
def _arrange(
rule: ContinuousMatchingSpec,
requesters: list[SimpleNamespace],
providers: list[SimpleNamespace],
rng: np.random.Generator,
) -> list[tuple[SimpleNamespace, bool]]:
"""Return the offers in their order of arrival, each with whether it is a request."""
offers = [(offer, True) for offer in requesters] + [(offer, False) for offer in providers]
if rule.arrival == 'sequential':
return sorted(offers, key=lambda item: item[0].agent.id)
return [offers[index] for index in rng.permutation(len(offers))]
def _match_continuous(
self,
rule: ContinuousMatchingSpec,
requesters: list[SimpleNamespace],
providers: list[SimpleNamespace],
rng: np.random.Generator,
) -> list[MatchSpec]:
"""Return the matches of every arriving offer with the best resting offers.
The resting offers are those of the other side.
"""
requests: list[tuple[float, int, SimpleNamespace]] = []
provisions: list[tuple[float, int, SimpleNamespace]] = []
trades: list[MatchSpec] = []
for time, (offer, is_request) in enumerate(self._arrange(rule, requesters, providers, rng)):
if is_request:
while (
provisions
and provisions[0][0] <= offer.reserve
and MatchingInteraction._get_affordable(offer, provisions[0][0]) > 0
):
resting = provisions[0][2]
self._record_match(trades, offer, resting, resting.reserve)
if resting.quantity <= 0:
heapq.heappop(provisions)
if MatchingInteraction._get_affordable(offer, offer.reserve) > 0:
heapq.heappush(requests, (-offer.reserve, time, offer))
continue
while requests and -requests[0][0] >= offer.reserve and offer.quantity > 0:
resting = requests[0][2]
self._record_match(trades, resting, offer, resting.reserve)
if MatchingInteraction._get_affordable(resting, resting.reserve) <= 0:
heapq.heappop(requests)
if offer.quantity > 0:
heapq.heappush(provisions, (offer.reserve, time, offer))
return trades
@staticmethod
def _get_requested(offer: SimpleNamespace) -> float:
"""Return the quantity a requester can match at its own reserve."""
return (
offer.quantity
if not math.isfinite(offer.cap)
else MatchingInteraction._get_affordable(offer, offer.reserve)
)
def _get_pair(
self,
requesters: list[SimpleNamespace],
providers: list[SimpleNamespace],
) -> tuple[list[tuple[SimpleNamespace, SimpleNamespace]], float]:
"""Return the pairs of crossing offers in order of their reserves.
It also returns the value of the last pair.
"""
ranked_requesters = sorted(requesters, key=lambda offer: (-offer.reserve, offer.agent.id))
ranked_providers = sorted(providers, key=lambda offer: (offer.reserve, offer.agent.id))
left = {id(offer): self._get_requested(offer) for offer in ranked_requesters}
offered = {id(offer): offer.quantity for offer in ranked_providers}
pairs: list[tuple[SimpleNamespace, SimpleNamespace]] = []
marginal = math.nan
requester_iter, provider_iter = iter(ranked_requesters), iter(ranked_providers)
requester, provider = next(requester_iter, None), next(provider_iter, None)
while requester is not None and provider is not None and requester.reserve >= provider.reserve:
pairs.append((requester, provider))
marginal = (requester.reserve + provider.reserve) / 2
quantity = min(left[id(requester)], offered[id(provider)])
left[id(requester)] -= quantity
offered[id(provider)] -= quantity
if left[id(requester)] <= 0:
requester = next(requester_iter, None)
if offered[id(provider)] <= 0:
provider = next(provider_iter, None)
return pairs, marginal
def _match_batch(self, requesters: list[SimpleNamespace], providers: list[SimpleNamespace]) -> list[MatchSpec]:
"""Return the matches of the crossing offers at the midpoint of the last pair.
The last pair is the last crossing pair.
"""
pairs, marginal = self._get_pair(requesters, providers)
trades: list[MatchSpec] = []
for requester, provider in pairs:
self._record_match(trades, requester, provider, marginal)
return trades
@staticmethod
def _score_provider(rule: StableMatchingSpec, requester: SimpleNamespace, provider: SimpleNamespace) -> float:
"""Return the score of a provider for a requester, higher for a preferred provider."""
if rule.requester_preference is None:
return -provider.reserve
return rule.requester_preference(requester.agent, provider.agent)
@staticmethod
def _score_requester(rule: StableMatchingSpec, provider: SimpleNamespace, requester: SimpleNamespace) -> float:
"""Return the score of a requester for a provider.
The score is higher for a preferred requester.
"""
if rule.provider_preference is None:
return requester.reserve
return rule.provider_preference(provider.agent, requester.agent)
def _rank_providers(
self,
rule: StableMatchingSpec,
requesters: list[SimpleNamespace],
providers: list[SimpleNamespace],
) -> dict[int, list[int]]:
"""Return the ids of the acceptable providers of every requester.
The preferred come first.
"""
capacities = {provider.agent.id for provider in providers if int(provider.quantity) > 0}
ranks = {}
for requester in requesters:
acceptable = [
provider
for provider in providers
if provider.agent.id in capacities and provider.reserve <= requester.reserve
]
ranked = sorted(
(-self._score_provider(rule, requester, provider), provider.agent.id) for provider in acceptable
)
if ranked:
ranks[requester.agent.id] = [identity for _, identity in ranked]
return ranks
def _rank_requesters(
self,
rule: StableMatchingSpec,
ranks: dict[int, list[int]],
providers: list[SimpleNamespace],
by_id: dict[int, SimpleNamespace],
) -> dict[int, list[int]]:
"""Return the ids of the requesters that rank every provider, the preferred first."""
provider_ranks = {}
for provider in providers:
suitors = sorted(
(-self._score_requester(rule, provider, by_id[requester_id]), requester_id)
for requester_id, ranked in ranks.items()
if provider.agent.id in ranked
)
if suitors:
provider_ranks[provider.agent.id] = [identity for _, identity in suitors]
return provider_ranks
@staticmethod
def _defer_acceptance(
requester_ranks: dict[int, list[int]],
provider_ranks: dict[int, list[int]],
capacities: dict[int, int],
) -> dict[int, list[int]]:
"""Return the requesters of every provider in the stable matching.
The matching is the requester-optimal one, and the preferred requesters come first.
Every free requester proposes to its next provider, which holds its preferred proposals up to its capacity and
rejects the rest. The result does not depend on the order of the proposals.
"""
held: dict[int, list[int]] = {provider: [] for provider in provider_ranks}
proposals = {requester: iter(ranked) for requester, ranked in requester_ranks.items()}
free = list(requester_ranks)
while free:
requester = free.pop()
provider = next(proposals[requester], None)
if provider is None:
continue
held[provider].append(requester)
if len(held[provider]) > capacities[provider]:
rejected = max(held[provider], key=provider_ranks[provider].index)
held[provider].remove(rejected)
free.append(rejected)
return {provider: sorted(held[provider], key=ranked.index) for provider, ranked in provider_ranks.items()}
def _match_stable(
self,
rule: StableMatchingSpec,
requesters: list[SimpleNamespace],
providers: list[SimpleNamespace],
) -> list[MatchSpec]:
"""Return the matches of the requester-optimal stable matching, one unit per pair."""
by_id = {offer.agent.id: offer for offer in itertools.chain(requesters, providers)}
requester_ranks = self._rank_providers(rule, requesters, providers)
if not requester_ranks:
return []
provider_ranks = self._rank_requesters(rule, requester_ranks, providers, by_id)
capacities = {provider_id: int(by_id[provider_id].quantity) for provider_id in provider_ranks}
trades: list[MatchSpec] = []
for provider_id, requester_ids in self._defer_acceptance(requester_ranks, provider_ranks, capacities).items():
provider = by_id[provider_id]
for requester_id in requester_ids:
requester = by_id[requester_id]
value = self._get_value(requester, provider, rule.value)
quantity = min(1.0, MatchingInteraction._get_affordable(requester, value), provider.quantity)
if quantity > 0:
requester.quantity -= quantity
requester.cap -= quantity * value
provider.quantity -= quantity
trades.append(MatchSpec(requester.agent, provider.agent, quantity, value))
return trades
def _match_partners(
self,
rule: PartnerSpec,
requesters: list[SimpleNamespace],
providers: list[SimpleNamespace],
rng: np.random.Generator,
) -> list[MatchSpec]:
"""Return the matches of every requester with the feasible partners its rule picks.
The requesters go in random order.
"""
wanting = {id(offer.agent): offer for offer in requesters}
offering = {id(offer.agent): offer for offer in providers}
def is_feasible(requester: AgentViewProtocol, provider: AgentViewProtocol) -> bool:
"""Return whether a provider still offers a quantity at an acceptable reserve.
The requester accepts the reserve.
"""
offer = offering[id(provider)]
return offer.quantity > 0 and offer.reserve <= wanting[id(requester)].reserve
trades: list[MatchSpec] = []
left, right = [offer.agent for offer in requesters], [offer.agent for offer in providers]
for requester, provider in pairs(left, right, rng, rule, feasible=is_feasible):
first, second = wanting[id(requester)], offering[id(provider)]
value = rule.value if isinstance(rule, PartnerMatchingSpec) else 'midpoint'
self._record_match(trades, first, second, self._get_value(first, second, value))
return trades
def _match_function(
self,
rule: Pairing,
requesters: list[SimpleNamespace],
providers: list[SimpleNamespace],
rng: np.random.Generator,
) -> list[MatchSpec]:
"""Return the matches of the pairs of a function.
Each is at the midpoint of the reserves.
"""
wanting = {id(offer.agent): offer for offer in requesters}
offering = {id(offer.agent): offer for offer in providers}
trades: list[MatchSpec] = []
left = [offer.agent for offer in requesters]
right = left if self.requesters == self.providers else [offer.agent for offer in providers]
for requester, provider in pairs(left, right, rng, rule):
first, second = wanting[id(requester)], offering[id(provider)]
self._record_match(trades, first, second, self._get_value(first, second, 'midpoint'))
return trades
def _get_matches(
self,
requesters: list[SimpleNamespace],
providers: list[SimpleNamespace],
system: SystemT,
) -> list[MatchSpec]:
"""Return the matches of the offers by the algorithm the rule names.
There are none when a side has no offers.
"""
if not requesters or not providers:
return []
rule, rng = self.rule, system.rng
match rule:
case RandomSearchSpec():
matches = self._match_random_search(rule, requesters, providers, rng)
case PartnerSwitchSpec():
matches = self._match_partner_switch(rule, requesters, providers, rng)
case QueueSpec():
matches = self._match_queue(rule, requesters, providers, rng)
case IterativeMatchingSpec():
matches = self._match_iterative(rule, requesters, providers, rng, system.storage)
case ContinuousMatchingSpec():
matches = self._match_continuous(rule, requesters, providers, rng)
case BatchMatchingSpec():
matches = self._match_batch(requesters, providers)
case StableMatchingSpec():
matches = self._match_stable(rule, requesters, providers)
case PartnerSpec():
matches = self._match_partners(rule, requesters, providers, rng)
case _:
matches = self._match_function(rule, requesters, providers, rng)
return matches
def _settle(self, match: MatchSpec, system: SystemT) -> None:
"""Move the transfers of a match, lower both quantities, and run `on_match`.
It lowers the quantities of both sides.
"""
for state, sign in self.transfers:
if sign < 0:
transfer(match.requester, match.provider, match.quantity * match.value, state)
else:
transfer(match.provider, match.requester, match.quantity, state)
for agent, name in zip((match.requester, match.provider), self._get_sides(self.quantity), strict=True):
if hasattr(agent, name):
setattr(agent, name, getattr(agent, name) - match.quantity)
if self.on_match is not None:
self.on_match(match, system)
def _report_matches(self, matches: list[MatchSpec], unmatched: float, system: SystemT) -> None:
"""Report the value, the matched volume, and the unmatched quantity of a clearing.
The value is weighted by quantity.
Args:
matches:
The matches of the clearing.
unmatched:
The quantity of both sides left after the clearing.
system:
The system view of the matching.
"""
volume = sum(match.quantity for match in matches)
value = sum(match.quantity * match.value for match in matches) / volume if volume else math.nan
system.report('value', value)
system.report('volume', volume)
system.report('unmatched', unmatched)
def _check_space(self, space: BaseSpace | None) -> None:
"""Raise an error for candidates in neighbouring cells or nodes.
The error is for a continuous space.
"""
self._check_partners(self.rule, space)
def _check_arguments(self) -> None:
"""Raise an error for a bad declaration, an unsigned transfer, or a bad rule.
Raises:
ValidationError:
A transfer has a sign other than `-1` or `1`, the rule is neither a matching spec nor a function, or
it is invalid.
"""
super()._check_arguments()
for state, sign in self.transfers:
if sign not in SIGNS:
error_msg = f'The transfer of {state!r} has the sign {sign!r}. Use one of {SIGNS}.'
raise ValidationError(error_msg)
self._check_variant('rule', self.rule, (), RULES, functions=True)
if isinstance(self.rule, RULES):
self.rule._check()
def _prepare(self, context: SimpleNamespace) -> None:
"""Set the laws of a binding.
Args:
context:
The namespace of the elements a binding of the roles names.
"""
super()._prepare(context)
self.laws_: Sequence[BaseLaw] = self._get_matching_laws(context)
def _get_matching_laws(self, namespace: SimpleNamespace) -> Sequence[BaseLaw]:
"""Return the rule that matches the members in the matching's phase.
Args:
namespace:
The namespace of the symbols the laws can use.
Returns:
laws:
The laws of the interaction.
"""
return [UpdateLaw(namespace, self.match, phase=self.phase)]
def match(self, system: SystemT) -> None:
"""Match the members, settle the matches, and report the results.
It reports the value, the volume, and the unmatched quantity.
Args:
system:
The system view of the matching.
"""
requesters, providers = self._get_offers(system)
matches = self._get_matches(requesters, providers, system)
for match in matches:
self._settle(match, system)
unmatched = sum(offer.quantity for offer in requesters + providers)
self._report_matches(matches, unmatched, system)
class AuctionLedgerInteraction(PolicyRuleInteraction[SystemView]):
"""Ledger that closes every trading period.
It measures its surplus and gives every trader a new unit. The realised surplus is the value of the units bought
minus the cost of the units sold, and the allocative efficiency is its ratio to the maximum surplus of the supply
and demand schedules.
Args:
label:
The label of the ledger.
phase:
The phase the ledger acts in.
Examples:
>>> from skcomplex.systems.economics.markets import AuctionLedgerInteraction
>>> AuctionLedgerInteraction('ledger', phase='close').reports
('surplus', 'efficiency')
"""
reports: ClassVar[tuple[str, ...]] = ('surplus', 'efficiency')
def act(self, system: SystemView) -> None:
"""Report the surplus and the efficiency of the trading period.
It then opens the next one.
Args:
system:
The system view scoped to the ledger.
"""
buyers = cast(list[ZeroIntelligenceBuyerView], system.get_agents('buyers'))
sellers = cast(list[ZeroIntelligenceSellerView], system.get_agents('sellers'))
values = np.array([buyer.value for buyer in buyers])
costs = np.array([seller.value for seller in sellers])
bought = np.array([buyer.demand <= 0 for buyer in buyers])
sold = np.array([seller.supply <= 0 for seller in sellers])
surplus = float(values[bought].sum() - costs[sold].sum())
system.report('surplus', surplus)
system.report('efficiency', surplus / maximum_surplus(values, costs))
for buyer in buyers:
buyer.demand = 1.0
for seller in sellers:
seller.supply = 1.0
Equations
The equations are those that symbolic returns for the system with its default parameters.