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, |
'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 | |
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 | |
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 | |
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 | |
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 |
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 | |
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, |
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 | |
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 |
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 | |