Skip to content

Exchange

Exchange of goods shared by the economics packages.

They are Cobb-Douglas utilities, marginal rates of substitution, and trades of goods between agents.

ExchangeInteraction(label, *, target, goods=GOODS, pairing='pairs', quantity=0.1, phase='trade')

Bases: Interaction

Bilateral barter between paired traders.

Its trades repeat while both traders gain. Every step the exchange pairs the traders, and every pair barters small quantities of every pair of goods at the geometric mean of their marginal rates of substitution while both gain. A trader holds every good in the state of its name and its Cobb-Douglas weight in the state <good>_weight. Every trade moves goods between the two traders, so the total of every good stays the same, which the default run checks as invariants. The exchange reports the mean price of the second good in units of the first, the standard deviation dispersion of its logarithm over the trades of the step, and the number of trades volume.

Parameters:

Name Type Description Default
label str

The label of the exchange.

required
target Role

The path of the group of traders.

required
goods tuple[str, ...]

The names of the traded goods.

GOODS
pairing str

How the exchange pairs the traders, 'neighbor-pairs' for disjoint pairs of neighbours or 'pairs' for disjoint random pairs.

'pairs'
quantity float

The quantity of the bought good in every trade.

0.1
phase str

The phase the trades run in.

'trade'

Examples:

>>> from skcomplex.systems.economics.markets import ExchangeInteraction
>>> ExchangeInteraction('exchange', target='traders').reports
('price', 'dispersion', 'volume')
Source code in src/skcomplex/systems/economics/common/exchange/_exchange_interaction.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def __init__(
    self,
    label: str,
    *,
    target: Role,
    goods: tuple[str, ...] = GOODS,
    pairing: str = 'pairs',
    quantity: float = 0.1,
    phase: str = 'trade',
) -> None:
    super().__init__(label)
    self.target = target
    self.goods = goods
    self.pairing = pairing
    self.quantity = quantity
    self.phase = phase

report_trades(system)

Report the mean price of the second good in the first, and the trades.

It also reports the dispersion of the price, and the trades are those of the step.

Parameters:

Name Type Description Default
system SystemViewProtocol

The system view of the exchange.

required
Source code in src/skcomplex/systems/economics/common/exchange/_exchange_interaction.py
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
def report_trades(self, system: SystemViewProtocol) -> None:
    """Report the mean price of the second good in the first, and the trades.

    It also reports the dispersion of the price, and the trades are those of the step.

    Args:
        system:
            The system view of the exchange.
    """
    trades = system.storage.pop('trades', [])
    trades = trades if isinstance(trades, list) else []
    logs = [math.log(price) for bought, paid, price in trades if (bought, paid) == (0, 1)]
    system.report('price', math.exp(float(np.mean(logs))) if logs else math.nan)
    system.report('dispersion', float(np.std(logs)) if len(logs) > 1 else math.nan)
    system.report('volume', len(trades))

trade(system)

Pair the traders and let every pair barter, in random order.

Parameters:

Name Type Description Default
system SystemViewProtocol

The system view of the exchange.

required
Source code in src/skcomplex/systems/economics/common/exchange/_exchange_interaction.py
100
101
102
103
104
105
106
107
108
109
110
111
112
def trade(self, system: SystemViewProtocol) -> None:
    """Pair the traders and let every pair barter, in random order.

    Args:
        system:
            The system view of the exchange.
    """
    traders = system.get_agents('target')
    found = list(pairs(traders, traders, system.rng, PAIRINGS[self.pairing]))
    for index in system.rng.permutation(len(found)):
        first, second = found[int(index)]
        if first.alive and second.alive:
            self.transfer(first, second, system)

transfer(source, target, system)

Barter between two traders while both gain.

It keeps the trades of the step for its report.

Parameters:

Name Type Description Default
source AgentViewProtocol

The first trader.

required
target AgentViewProtocol

The second trader.

required
system SystemViewProtocol

The system view of the exchange.

required

Returns:

Name Type Description
traded bool

Whether the traders made a trade.

Source code in src/skcomplex/systems/economics/common/exchange/_exchange_interaction.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def transfer(self, source: AgentViewProtocol, target: AgentViewProtocol, system: SystemViewProtocol) -> bool:
    """Barter between two traders while both gain.

    It keeps the trades of the step for its report.

    Args:
        source:
            The first trader.

        target:
            The second trader.

        system:
            The system view of the exchange.

    Returns:
        traded:
            Whether the traders made a trade.
    """
    weights = [f'{good}_weight' for good in self.goods]
    trades = trade_goods(source, target, self.goods, weights, quantity=self.quantity)
    logs = system.storage.setdefault('trades', [])
    if isinstance(logs, list):
        logs.extend(trades)
    return bool(trades)

cobb_douglas(agent, goods, weights)

Return the logarithm of the Cobb-Douglas utility of an agent.

It is sum_i a_i log x_i.

Parameters:

Name Type Description Default
agent object

The agent, with a state for the holding of every good and for the weight of every good.

required
goods Sequence[str]

The names of the states that hold the goods.

required
weights Sequence[str]

The names of the states that hold the weights of the goods, in the order of goods.

required

Returns:

Name Type Description
utility float

The logarithm of the utility, minus infinity when a weighted good runs out.

Examples:

>>> from types import SimpleNamespace
>>> from skcomplex.systems.economics.markets import cobb_douglas
>>> agent = SimpleNamespace(x=4.0, y=1.0, a=0.5, b=0.5)
>>> round(cobb_douglas(agent, ('x', 'y'), ('a', 'b')), 6)
0.693147
Source code in src/skcomplex/systems/economics/common/exchange/_exchange.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def cobb_douglas(agent: object, goods: Sequence[str], weights: Sequence[str]) -> float:
    """Return the logarithm of the Cobb-Douglas utility of an agent.

    It is `sum_i a_i log x_i`.

    Args:
        agent:
            The agent, with a state for the holding of every good and for the weight of every good.

        goods:
            The names of the states that hold the goods.

        weights:
            The names of the states that hold the weights of the goods, in the order of `goods`.

    Returns:
        utility:
            The logarithm of the utility, minus infinity when a weighted good runs out.

    Examples:
        >>> from types import SimpleNamespace
        >>> from skcomplex.systems.economics.markets import cobb_douglas
        >>> agent = SimpleNamespace(x=4.0, y=1.0, a=0.5, b=0.5)
        >>> round(cobb_douglas(agent, ('x', 'y'), ('a', 'b')), 6)
        0.693147
    """
    total = 0.0
    for good, weight in zip(goods, weights, strict=True):
        share, holding = float(getattr(agent, weight)), float(getattr(agent, good))
        if share == 0:
            continue
        if holding <= 0:
            return -math.inf
        total += share * math.log(holding)
    return total

marginal_rate(agent, good, numeraire, weights)

Return the marginal rate of substitution of a good for a numeraire.

It is the units of numeraire one unit is worth.

Parameters:

Name Type Description Default
agent object

The agent.

required
good str

The name of the state that holds the good.

required
numeraire str

The name of the state that holds the numeraire.

required
weights tuple[str, str]

The names of the states that hold the weights of the good and of the numeraire.

required

Returns:

Name Type Description
rate float

The ratio of the marginal utilities, (a_good / x_good) / (a_numeraire / x_numeraire).

Examples:

>>> from types import SimpleNamespace
>>> from skcomplex.systems.economics.markets import marginal_rate
>>> agent = SimpleNamespace(x=2.0, y=6.0, a=0.5, b=0.5)
>>> marginal_rate(agent, 'x', 'y', ('a', 'b'))
3.0
Source code in src/skcomplex/systems/economics/common/exchange/_exchange.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def marginal_rate(agent: object, good: str, numeraire: str, weights: tuple[str, str]) -> float:
    """Return the marginal rate of substitution of a good for a numeraire.

    It is the units of numeraire one unit is worth.

    Args:
        agent:
            The agent.

        good:
            The name of the state that holds the good.

        numeraire:
            The name of the state that holds the numeraire.

        weights:
            The names of the states that hold the weights of the good and of the numeraire.

    Returns:
        rate:
            The ratio of the marginal utilities, `(a_good / x_good) / (a_numeraire / x_numeraire)`.

    Examples:
        >>> from types import SimpleNamespace
        >>> from skcomplex.systems.economics.markets import marginal_rate
        >>> agent = SimpleNamespace(x=2.0, y=6.0, a=0.5, b=0.5)
        >>> marginal_rate(agent, 'x', 'y', ('a', 'b'))
        3.0
    """
    good_weight, numeraire_weight = (float(getattr(agent, name)) for name in weights)
    return (good_weight / float(getattr(agent, good))) / (numeraire_weight / float(getattr(agent, numeraire)))

trade_goods(first, second, goods, weights, *, quantity=0.1, limit=1000)

Barter small quantities of goods between two agents while both gain.

It returns the trades. For every pair of goods in turn, the agent that values the first good more in units of the second buys quantity of it from the other at the geometric mean of their marginal rates of substitution, as in the Sugarscape trade of Epstein and Axtell (1996). A trade happens only when it raises the Cobb-Douglas utility of both agents, and every trade is two transfers, so the total of every good is unchanged. The agents trade until a round over all pairs of goods makes no trade, or until limit trades.

Parameters:

Name Type Description Default
first object

The first agent, with a state for the holding and for the weight of every good.

required
second object

The second agent.

required
goods Sequence[str]

The names of the states that hold the goods.

required
weights Sequence[str]

The names of the states that hold the weights of the goods, in the order of goods.

required
quantity float

The quantity of the bought good in every trade.

0.1
limit int

The largest number of trades.

1000

Returns:

Name Type Description
trades list[tuple[int, int, float]]

The index of the bought good, the index of the good paid with, and the price of every trade.

Examples:

>>> from types import SimpleNamespace
>>> from skcomplex.systems.economics.markets import trade_goods
>>> first = SimpleNamespace(x=9.0, y=1.0, a=0.5, b=0.5)
>>> second = SimpleNamespace(x=1.0, y=9.0, a=0.5, b=0.5)
>>> trades = trade_goods(first, second, ('x', 'y'), ('a', 'b'), quantity=0.5)
>>> first.x + second.x, len(trades) > 0
(10.0, True)
Source code in src/skcomplex/systems/economics/common/exchange/_exchange.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
def trade_goods(
    first: object,
    second: object,
    goods: Sequence[str],
    weights: Sequence[str],
    *,
    quantity: float = 0.1,
    limit: int = 1000,
) -> list[tuple[int, int, float]]:
    """Barter small quantities of goods between two agents while both gain.

    It returns the trades. For every pair of goods in turn, the agent that values the first good more in units of the
    second buys `quantity` of it from the other at the geometric mean of their marginal rates of substitution, as in the
    Sugarscape trade of Epstein and Axtell (1996). A trade happens only when it raises the Cobb-Douglas utility of both
    agents, and every trade is two transfers, so the total of every good is unchanged. The agents trade until a round
    over all pairs of goods makes no trade, or until `limit` trades.

    Args:
        first:
            The first agent, with a state for the holding and for the weight of every good.

        second:
            The second agent.

        goods:
            The names of the states that hold the goods.

        weights:
            The names of the states that hold the weights of the goods, in the order of `goods`.

        quantity:
            The quantity of the bought good in every trade.

        limit:
            The largest number of trades.

    Returns:
        trades:
            The index of the bought good, the index of the good paid with, and the price of every trade.

    Examples:
        >>> from types import SimpleNamespace
        >>> from skcomplex.systems.economics.markets import trade_goods
        >>> first = SimpleNamespace(x=9.0, y=1.0, a=0.5, b=0.5)
        >>> second = SimpleNamespace(x=1.0, y=9.0, a=0.5, b=0.5)
        >>> trades = trade_goods(first, second, ('x', 'y'), ('a', 'b'), quantity=0.5)
        >>> first.x + second.x, len(trades) > 0
        (10.0, True)
    """
    trades: list[tuple[int, int, float]] = []
    pairs = list(itertools.combinations(range(len(goods)), 2))
    while len(trades) < limit:
        traded = False
        for pair in pairs:
            price = _trade_once(first, second, pair, goods=goods, weights=weights, quantity=quantity)
            if price is not None:
                trades.append((pair[0], pair[1], price))
                traded = True
        if not traded:
            break
    return trades