Econophysics
Econophysics: wealth exchange between agents.
CoalitionBarInteraction(label, *, rule=None, phase=None, patrons, strategies=(), capacity=60.0, smoothing=0.1, coalitions=True)
Bases: FeedbackInteraction[SystemView]
El Farol bar of Rand and Wilensky whose patrons share strategies within coalitions that split and merge.
A strategy s in [-1, 1]^(m + 1) predicts the attendance n s_1 + sum_j s_(j + 1) h_j from the last m
attendances h, and its error is the total absolute error of its predictions over the last m nights. Every night
every coalition uses the strategy of the lowest error among the strategies of its members, and every member goes
when the prediction is at most the capacity. Every member's return moves by smoothing towards 1 after a good
night and towards 0 after a bad one. With coalitions, after every night (Collins 2019, Section 3.2), every
coalition in random order that attended an overcrowded night within the memory, when the smallest excess x of
those nights is below its size N, splits off a random subgroup of at most N - x members that holds the owner of
its best strategy. Then, at most once per coalition and night, two coalitions that attended an uncrowded night
merge unless their joint size would have overcrowded an uncrowded night either attended within the memory. A merged
coalition keeps the longer history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the bar. |
required |
rule
|
None
|
Unused, since the bar overrides |
None
|
phase
|
str | None
|
The phase the bar acts in, or |
None
|
patrons
|
Annotated[str, RoleSpec()]
|
The path of the group of patrons. |
required |
strategies
|
tuple[tuple[tuple[float, ...], ...], ...]
|
The strategies of every patron, |
()
|
capacity
|
Annotated[float, ParameterSpec(domain=open(0, oo))]
|
The attendance above which the bar is overcrowded. |
60.0
|
smoothing
|
Annotated[float, ParameterSpec(domain=closed(0, 1))]
|
The weight |
0.1
|
coalitions
|
bool
|
Whether patrons form coalitions. |
True
|
Examples:
>>> from skcomplex.systems.economics.econophysics import CoalitionBarInteraction
>>> CoalitionBarInteraction('bar', patrons='patrons').reports[:2]
('attendance', 'coalitions')
Source code in src/skcomplex/systems/economics/econophysics/_coalitions.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
act(system)
Decide the night, score the patrons, and let the coalitions split and merge.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
SystemView
|
The system view of the bar. |
required |
Source code in src/skcomplex/systems/economics/econophysics/_coalitions.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
CoalitionPatronElement(label, visual=VisualSpec('agents', {'color_by': 'coalition'}), *, coalition=0, attends=0.0, score=0.0)
Bases: Element
Patron of the El Farol bar who belongs to a coalition and keeps a smoothed return of its nights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
visual
|
VisualSpec | None
|
How the element is drawn, or |
VisualSpec('agents', {'color_by': 'coalition'})
|
coalition
|
Annotated[float, StateSpec(domain=NN)]
|
The label of the coalition of the patron. |
0
|
attends
|
Annotated[float, StateSpec(domain=closed(0, 1))]
|
1 when the patron went to the bar on the last night, 0 otherwise. |
0.0
|
score
|
Annotated[float, StateSpec(domain=closed(0, 1))]
|
The exponentially smoothed share of good nights, a night at the bar that was not overcrowded or a night away from an overcrowded bar. |
0.0
|
Examples:
>>> from skcomplex.core.components.elements import GroupElement
>>> from skcomplex.systems.economics.econophysics import CoalitionPatronElement
>>> GroupElement('patrons', element=CoalitionPatronElement, size=3).size
3
Source code in src/skcomplex/systems/economics/econophysics/_coalitions.py
58 59 60 61 62 63 64 65 66 67 68 69 70 | |
CrowdAvoidanceInteraction(label, *, target, options, state='restaurant', intensity=1.0, activity=1.0, phase=None)
Bases: ChoiceInteraction
Stochastic crowd avoidance of diners, who leave crowded restaurants.
A diner returns to last evening's restaurant with probability 1 / n, with n the crowd it found there, and
otherwise moves to one of the other restaurants at random, which raises the utilization to about 0.8. It is a
Metropolis choice of intensity 1 where every other restaurant has utility 0 and staying has the utility
-log(1 - 1 / n), so a proposed move is accepted with probability 1 - 1 / n.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
str
|
The path of the group of diners. |
required |
options
|
Sequence[float]
|
The restaurants, from 0 to their number minus one. |
required |
state
|
str
|
The state of the diners that holds their restaurant. |
'restaurant'
|
intensity
|
float
|
The intensity of choice, 1 for the stay probability |
1.0
|
activity
|
float
|
The probability that a diner revises its choice in an evening. |
1.0
|
phase
|
str | None
|
The phase the choice runs in, or |
None
|
Examples:
>>> from skcomplex.systems.economics.econophysics import CrowdAvoidanceInteraction
>>> CrowdAvoidanceInteraction('choice', target='diners', options=(0.0, 1.0)).rule
'metropolis'
Source code in src/skcomplex/systems/economics/econophysics/_kolkata.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
DebtExchangeInteraction(label, *, source, target, debt=0.0, amount=1.0, phase=None)
Bases: TransferInteraction
Random pairwise trades of Dragulescu and Yakovenko of a fixed amount, allowing debt down to a limit.
In every trade an agent pays a fixed amount to another agent drawn at random, unless its wealth would fall below
minus the debt limit. Every step, every agent pays once, in random order. The stationary wealth distribution is
the exponential P(w) = exp(-(w + d) / T) / T shifted to the debt limit d, with temperature T = <w> + d.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
source
|
str
|
The path of the group of traders. |
required |
target
|
str
|
The path of the group of traders, the source again. |
required |
debt
|
Annotated[float, ParameterSpec(domain=closed_open(0, oo))]
|
The debt limit |
0.0
|
amount
|
Annotated[float, ParameterSpec(domain=open(0, oo))]
|
The amount paid in every trade. |
1.0
|
phase
|
str | None
|
The phase the trades run in, or |
None
|
Examples:
>>> from skcomplex.systems.economics.econophysics import DebtExchangeInteraction
>>> DebtExchangeInteraction('trades', source='traders', target='traders', debt=5.0).debt
5.0
Source code in src/skcomplex/systems/economics/econophysics/_kinetic.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
DinerElement(label, visual=None, *, restaurant=0, crowd=0, served=0.0)
Bases: Element
Diner of the Kolkata Paise Restaurant problem, who goes to one restaurant every evening.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
visual
|
VisualSpec | None
|
How the element is drawn, or |
None
|
restaurant
|
Annotated[float, StateSpec(domain=NN)]
|
The restaurant chosen last, from 0 to the number of restaurants minus one. |
0
|
crowd
|
Annotated[float, StateSpec(domain=NN)]
|
The number of diners at that restaurant, 0 before the first evening. |
0
|
served
|
Annotated[float, StateSpec(domain=FiniteEnumeratedSet([0, 1]))]
|
Whether the diner got a meal, 1 or 0. |
0.0
|
Examples:
>>> from skcomplex.core.components.elements import GroupElement
>>> from skcomplex.systems.economics.econophysics import DinerElement
>>> GroupElement('diners', element=DinerElement, size=10).size
10
Source code in src/skcomplex/systems/economics/econophysics/_kolkata.py
62 63 64 65 66 67 68 69 70 71 72 73 74 | |
MinorityGameInteraction(label, *, players, memory=3, phase=None)
Bases: Interaction
Rounds of the minority game of Challet and Zhang, where the agents on the minority side win.
Every round each agent plays the action, +1 or -1, that its chosen strategy assigns to the last m winning
sides, the strategy of higher virtual score that a ChoiceInteraction of rule 'best' chooses before. The side
with fewer agents wins, and every agent on it gains one point and every other loses one. The game reports the
attendance A = sum_i a_i and the code of the history the strategies read, so a cumulative
ReinforcementInteraction gives every strategy one virtual point when it would have played the winning side, by
the payoff payoff. The volatility sigma^2 / N of the attendance has a minimum below the value of random play
near alpha = 2^m / N ~ 0.34.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
players
|
Annotated[str, RoleSpec()]
|
The path of the group of agents. |
required |
memory
|
int
|
The memory |
3
|
phase
|
str | None
|
The phase the rounds run in, or |
None
|
Examples:
>>> from skcomplex.systems.economics.econophysics import MinorityGameInteraction
>>> MinorityGameInteraction('game', players='players', memory=4).memory
4
Source code in src/skcomplex/systems/economics/econophysics/_minority.py
186 187 188 189 190 191 192 193 194 195 196 197 | |
payoff(agent, system, option)
Return the virtual payoff of a strategy of an agent in the last round, +1 if it played the winning side.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
MinorityPlayerView
|
The agent. |
required |
system
|
SystemView
|
The system view of the rule that reads the payoff. |
required |
option
|
int
|
The strategy, 0 for the first and 1 for the second. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
payoff |
float
|
|
Source code in src/skcomplex/systems/economics/econophysics/_minority.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
play(system)
Play one round, pay the agents, and update and report the history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
SystemView
|
The system. |
required |
Source code in src/skcomplex/systems/economics/econophysics/_minority.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | |
MinorityPlayerElement(label, visual=None, *, first=0, second=1, first_score=0.0, second_score=0.0, strategy=0.0, action=0.0, wealth=0.0)
Bases: Element
Agent of the minority game, who holds two strategies and plays the one it chose.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
visual
|
VisualSpec | None
|
How the element is drawn, or |
None
|
first
|
Annotated[float, StateSpec(domain=NN)]
|
The code of the first strategy. |
0
|
second
|
Annotated[float, StateSpec(domain=NN)]
|
The code of the second strategy. |
1
|
first_score
|
Annotated[float, StateSpec()]
|
The virtual score of the first strategy, the number of times it would have been in the minority. |
0.0
|
second_score
|
Annotated[float, StateSpec()]
|
The virtual score of the second strategy. |
0.0
|
strategy
|
Annotated[float, StateSpec(domain=FiniteEnumeratedSet([0, 1]))]
|
The strategy played, 0 for the first and 1 for the second. |
0.0
|
action
|
Annotated[float, StateSpec(domain=ZZ)]
|
The last action, |
0.0
|
wealth
|
Annotated[float, StateSpec()]
|
The net number of times the agent was in the minority. |
0.0
|
Examples:
>>> from skcomplex.core.components.elements import GroupElement
>>> from skcomplex.systems.economics.econophysics import MinorityPlayerElement
>>> GroupElement('players', element=MinorityPlayerElement, size=11, values={'first': 3, 'second': 5}).size
11
Source code in src/skcomplex/systems/economics/econophysics/_minority.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
PatronElement(label, visual=None, *, predictors=1, forecast=0.0, attends=0.0, capacity=60.0)
Bases: Element
Patron of the El Farol bar, who forecasts the attendance with its most accurate predictor.
Every patron holds a few predictors of the pool, the set bits of predictors, and forecasts with the one of
smallest error in the pool. A choice then sends the patron to the bar when the forecast is below the capacity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
visual
|
VisualSpec | None
|
How the element is drawn, or |
None
|
predictors
|
Annotated[float, StateSpec(domain=NN)]
|
The bit mask of the predictors of the pool that the patron holds. |
1
|
forecast
|
Annotated[float, StateSpec(domain=closed_open(0, oo))]
|
The last forecast of the patron's most accurate predictor. |
0.0
|
attends
|
Annotated[float, StateSpec(domain=FiniteEnumeratedSet([0, 1]))]
|
Whether the patron went to the bar this week, 1 or 0. |
0.0
|
capacity
|
Annotated[float, ParameterSpec(domain=closed_open(0, oo))]
|
The attendance above which the bar is too crowded to enjoy. |
60.0
|
Examples:
>>> from skcomplex.core.components.elements import GroupElement
>>> from skcomplex.systems.economics.econophysics import PatronElement
>>> GroupElement('patrons', element=PatronElement, size=100, values={'predictors': 7}).size
100
Source code in src/skcomplex/systems/economics/econophysics/_el_farol.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
predict(agent, system)
Forecast the attendance with the most accurate predictor held.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
PatronView
|
The patron. |
required |
system
|
SystemView
|
The system. |
required |
Source code in src/skcomplex/systems/economics/econophysics/_el_farol.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
PredictorPoolElement(label, visual=None, *, forecasts=0.0, errors=0.0)
Bases: Element
Pool of Arthur's predictors, with the forecast and the accuracy of every predictor that the patrons share.
The accuracy of a predictor depends only on the history of attendance, the observable attendance of the run,
so one member holds it for every patron. Every week the pool forecasts the attendance with every predictor, from
ten random weeks it draws at the first week followed by the observed attendances, and a
ReinforcementInteraction moves the error of every predictor towards its last absolute error, a discounted mean
of the errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
visual
|
VisualSpec | None
|
How the element is drawn, or |
None
|
forecasts
|
Annotated[ArrayLike, StateSpec(vector=True)]
|
The forecast of next week's attendance by every predictor of the pool. |
0.0
|
errors
|
Annotated[ArrayLike, StateSpec(vector=True)]
|
The discounted mean absolute error of every predictor of the pool. |
0.0
|
Examples:
>>> from skcomplex.core.components.elements import GroupElement
>>> from skcomplex.systems.economics.econophysics import PredictorPoolElement
>>> GroupElement('pool', element=PredictorPoolElement, size=1).size
1
Source code in src/skcomplex/systems/economics/econophysics/_el_farol.py
136 137 138 139 140 141 142 143 144 145 146 | |
error(agent, system, index)
staticmethod
Return the absolute error of a predictor last week, or its error so far before the first attendance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
PredictorPoolView
|
The pool. |
required |
system
|
SystemView
|
The system view of the rule that reads the error. |
required |
index
|
int
|
The index of the predictor in the pool. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
error |
float
|
The absolute error of the forecast of the predictor. |
Source code in src/skcomplex/systems/economics/econophysics/_el_farol.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
forecast(agent, system)
Forecast next week's attendance with every predictor of the pool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
PredictorPoolView
|
The pool. |
required |
system
|
SystemView
|
The system. |
required |
Source code in src/skcomplex/systems/economics/econophysics/_el_farol.py
174 175 176 177 178 179 180 181 182 183 184 | |
RestaurantsInteraction(label, *, target, n_restaurants=100, by='restaurant', phase=None)
Bases: GroupingInteraction
Restaurants that each serve one meal every evening, to one of the diners who come chosen at random.
The diners are grouped by their restaurant. Every diner learns the crowd at its restaurant, and the first of the group in a random order is served. The interaction reports the fraction of restaurants that serve a meal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
target
|
str
|
The path of the group of diners. |
required |
n_restaurants
|
int
|
The number of restaurants. |
100
|
by
|
str
|
The state of the diners that holds their restaurant. |
'restaurant'
|
phase
|
str | None
|
The phase the restaurants serve in, or |
None
|
Examples:
>>> from skcomplex.systems.economics.econophysics import RestaurantsInteraction
>>> RestaurantsInteraction('restaurants', target='diners', n_restaurants=5).groups
'state'
Source code in src/skcomplex/systems/economics/econophysics/_kolkata.py
181 182 183 184 185 186 187 188 189 190 191 | |
act_on(members, system)
Tell every diner of a restaurant its crowd and serve the first diner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
members
|
list[AgentViewProtocol]
|
The diners at one restaurant, in random order. |
required |
system
|
SystemViewProtocol
|
The system view of the restaurants. |
required |
Source code in src/skcomplex/systems/economics/econophysics/_kolkata.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
report(system)
Report the fraction of restaurants with at least one diner.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
SystemViewProtocol
|
The system view of the restaurants. |
required |
Source code in src/skcomplex/systems/economics/econophysics/_kolkata.py
208 209 210 211 212 213 214 215 216 | |
SavingExchangeInteraction(label, *, source, target, saving=0.0, phase=None)
Bases: TransferInteraction
Random pairwise trades of Chakraborti and Chakrabarti, where every agent saves a fraction of its wealth.
A trade between agents i and j keeps total wealth and gives w_i' = lambda w_i + eps (1 - lambda) (w_i + w_j)
and w_j' = lambda w_j + (1 - eps) (1 - lambda) (w_i + w_j) with eps uniform on [0, 1], so i transfers
(1 - lambda) (w_i - eps (w_i + w_j)) to j, an amount of either sign. Every step, every agent trades once with
another agent drawn at random, in random order. Without saving the stationary wealth distribution is exponential,
and with saving propensity lambda it is close to a Gamma distribution of shape n = 1 + 3 lambda / (1 - lambda).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
source
|
str
|
The path of the group of traders. |
required |
target
|
str
|
The path of the group of traders, the source again. |
required |
saving
|
Annotated[float, ParameterSpec(domain=closed_open(0, oo))]
|
The saving propensity |
0.0
|
phase
|
str | None
|
The phase the trades run in, or |
None
|
Examples:
>>> from skcomplex.systems.economics.econophysics import SavingExchangeInteraction
>>> SavingExchangeInteraction('trades', source='traders', target='traders').saving
0.0
Source code in src/skcomplex/systems/economics/econophysics/_kinetic.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | |
SavingTraderElement(label, visual=None, *, wealth=1.0)
Bases: Element
Agent of the kinetic wealth exchange systems, whose wealth changes only in pairwise trades.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
visual
|
VisualSpec | None
|
How the element is drawn, or |
None
|
wealth
|
Annotated[float, StateSpec()]
|
The initial wealth, negative down to the debt limit. |
1.0
|
Examples:
>>> from skcomplex.core.components.elements import GroupElement
>>> from skcomplex.systems.economics.econophysics import SavingTraderElement
>>> group = GroupElement('traders', element=SavingTraderElement, size=10, values={'wealth': 2.0})
>>> group.label, group.element.__name__, group.size
('traders', 'SavingTraderElement', 10)
Source code in src/skcomplex/systems/economics/econophysics/_kinetic.py
47 48 49 50 51 52 53 54 55 | |
TraderElement(label, visual=None, *, wealth=1.0)
Bases: Element
Agent of the Boltzmann wealth system, whose wealth in whole units changes only by gifts of one unit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the agent or group. |
required |
visual
|
VisualSpec | None
|
How the element is drawn, or |
None
|
wealth
|
Annotated[float, StateSpec(domain=NN)]
|
The initial wealth. |
1.0
|
Examples:
>>> from skcomplex.systems.economics.econophysics import TraderElement
>>> group = GroupElement('traders', element=TraderElement, size=100)
>>> group.label, group.element.__name__, group.size
('traders', 'TraderElement', 100)
Source code in src/skcomplex/systems/economics/econophysics/_exchange.py
37 38 39 40 41 42 43 44 45 | |
WealthExchangeInteraction(label, *, candidate, field, classes=9, eta=1.0, cooperation=0.2, competition=0.3)
Bases: EncounterInteraction
Conservative encounters between wealth classes, with cooperation and competition.
A candidate of class h meets a field particle of class k at the rate eta and moves by the transition table
of exchange_table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
candidate
|
str
|
The path of the distribution over wealth classes whose particles move. |
required |
field
|
str
|
The path of the distribution over wealth classes they meet, the candidate again. |
required |
classes
|
Annotated[int, ParameterSpec(domain=open(0, oo))]
|
The number of wealth classes. |
9
|
eta
|
Annotated[float, ParameterSpec(domain=closed_open(0, oo))]
|
The encounter rate. |
1.0
|
cooperation
|
Annotated[float, ParameterSpec(domain=closed_open(0, oo))]
|
The probability of the move that narrows the gap between the classes of an encounter. |
0.2
|
competition
|
Annotated[float, ParameterSpec(domain=closed_open(0, oo))]
|
The probability of the move that widens it. |
0.3
|
Examples:
>>> from skcomplex.systems.economics.econophysics import WealthExchangeInteraction
>>> WealthExchangeInteraction('exchange', candidate='wealth', field='wealth').competition
0.3
Source code in src/skcomplex/systems/economics/econophysics/_welfare.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
WelfarePolicyInteraction(label, *, candidate, field, classes=9, tax=0.1)
Bases: EncounterInteraction
Welfare policy that taxes the richest classes and helps the poorest, a micro-scale external action.
The policy is an encounter whose field role binds a group with a fixed density, so it acts on the transitions of
the population at the rate tax times that density, by the pattern and the table of policy_table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The label of the interaction. |
required |
candidate
|
str
|
The path of the distribution over wealth classes. |
required |
field
|
str
|
The path of the group of one member whose fixed density is the strength of the policy. |
required |
classes
|
Annotated[int, ParameterSpec(domain=open(0, oo))]
|
The number of wealth classes. |
9
|
tax
|
Annotated[float, ParameterSpec(domain=closed_open(0, oo))]
|
The rate at which the policy moves the richest classes down and the poorest up. |
0.1
|
Examples:
>>> from skcomplex.systems.economics.econophysics import WelfarePolicyInteraction
>>> WelfarePolicyInteraction('welfare', candidate='wealth', field='state').tax
0.1
Source code in src/skcomplex/systems/economics/econophysics/_welfare.py
183 184 185 186 187 188 189 190 191 192 193 194 | |
boltzmann_wealth(n_agents=100)
Return a Boltzmann wealth exchange system where every agent starts with one unit.
Every step, every agent with any wealth gives one unit to another agent drawn at random, in random order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_agents
|
int
|
The number of agents. |
100
|
Returns:
| Name | Type | Description |
|---|---|---|
system |
System
|
The system, with the group |
Examples:
>>> from skcomplex.systems.economics.econophysics import boltzmann_wealth
>>> boltzmann_wealth(10).elements[0].prepare().size_
10
Source code in src/skcomplex/systems/economics/econophysics/_exchange.py
48 49 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 81 82 | |
el_farol(n_agents=100, *, capacity=60.0, n_predictors=6, discount=0.9, random_state=0)
Return Arthur's El Farol bar problem, whose attendance self-organizes around the capacity.
Every patron holds a random set of predictors of next week's attendance, drawn from a pool of 46, and acts on
its currently most accurate one, going when it forecasts room in the bar. The attendance is random-looking, yet
its mean settles near the capacity. The patrons read the past attendances from the observable attendance of
the run, which the catalog lists, so a run passes it in observables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_agents
|
int
|
The number of patrons. |
100
|
capacity
|
float
|
The attendance above which the bar is too crowded to enjoy. |
60.0
|
n_predictors
|
int
|
The number of predictors every patron holds. |
6
|
discount
|
float
|
The weight of past errors in the accuracy of a predictor. |
0.9
|
random_state
|
int | Generator | None
|
The seed or generator of the predictors that every patron holds. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
system |
System
|
The system, with the group |
Examples:
>>> from skcomplex.systems.economics.econophysics import el_farol
>>> el_farol(10).elements[0].prepare().size_
10
Source code in src/skcomplex/systems/economics/econophysics/_el_farol.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
el_farol_coalitions(n_agents=100, *, capacity=60.0, memory=5, n_strategies=10, smoothing=0.1, coalitions=True, random_state=0)
Return Collins' El Farol bar with strategic group formation, whose coalitions share their best strategy.
Patrons of Rand and Wilensky's El Farol model predict the attendance with linear strategies of the last memory
attendances and go when they predict room. In Collins' version (Carmichael et al. 2019, chapter 9) patrons form
coalitions that use the best strategy of all their members, so a larger coalition has better predictions but adds
more to the crowd. Coalitions split off the members with the best strategy after overcrowded nights, the core
condition that no subgroup gains by leaving, and coalitions that attended together merge when it would not have
crowded an earlier good night. Without coalitions the mean attendance stays near the capacity and patrons enjoy
about half their nights, while coalitions make the attendance swing and lower the returns of all, because the
patrons are boundedly rational. The return of a patron is exponentially smoothed with the weight smoothing,
which the source leaves open.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_agents
|
int
|
The number of patrons. |
100
|
capacity
|
float
|
The attendance above which the bar is overcrowded. |
60.0
|
memory
|
int
|
The number of past attendances a strategy reads. |
5
|
n_strategies
|
int
|
The number of strategies of every patron. |
10
|
smoothing
|
float
|
The weight of the last night in the return. |
0.1
|
coalitions
|
bool
|
Whether patrons form coalitions. |
True
|
random_state
|
int | Generator | None
|
The seed or generator of the strategies. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
system |
System
|
The system, with the group |
Examples:
>>> from skcomplex.systems.economics.econophysics import el_farol_coalitions
>>> el_farol_coalitions(10).elements[0].prepare().size_
10
Source code in src/skcomplex/systems/economics/econophysics/_coalitions.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | |
exchange_table(classes, cooperation, competition)
Return the transition table of encounters between wealth classes, A[h, k, i].
A candidate of class h that meets a richer field particle moves one class up with probability cooperation
and one class down with probability competition. One that meets a poorer field particle moves one class up with
probability competition and one class down with probability cooperation. So competition makes the rich richer
and the poor poorer, and cooperation does the opposite. A move past the poorest or the richest class is not made.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classes
|
int
|
The number of wealth classes. |
required |
cooperation
|
object
|
The probability of the move that narrows the gap between the two classes, a number or a parameter. |
required |
competition
|
object
|
The probability of the move that widens it, a number or a parameter. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
table |
NDArray[object_]
|
The transition probabilities, which sum to one over the outcomes. |
Examples:
>>> from skcomplex.systems.economics.econophysics import exchange_table
>>> exchange_table(3, 0.2, 0.3)[0, 2].astype(float).tolist()
[0.8, 0.2, 0.0]
Source code in src/skcomplex/systems/economics/econophysics/_welfare.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
forecasts(history)
Return the forecast of next week's attendance of every predictor of the pool, between 0 and 100.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
history
|
ArrayLike
|
The past attendances, the last one last, at least ten of them. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
forecasts |
Array
|
One forecast per predictor of the pool. |
Examples:
>>> from skcomplex.systems.economics.econophysics import forecasts
>>> forecasts([60.0] * 10)[:3].tolist()
[60.0, 60.0, 60.0]
Source code in src/skcomplex/systems/economics/econophysics/_el_farol.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
gini_coefficient(s, wealth)
Return the Gini coefficient of a distribution over wealth classes, read by every member of its group.
The coefficient is sum_ij f_i f_j |w_i - w_j| / (2 F M), with the density F and the total wealth M.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
SimpleNamespace
|
The namespace of one member of the distribution. |
required |
wealth
|
NDArray[float64]
|
The wealth of every class. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
gini |
Expression
|
The coefficient as an expression of reductions. |
Source code in src/skcomplex/systems/economics/econophysics/_welfare.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
kinetic_wealth_exchange(n_agents=500, *, saving=0.0, debt=None, wealth=1.0, amount=0.1)
Return a kinetic wealth exchange system with a saving propensity or a debt limit.
Without a debt limit, agents trade random shares of their pooled wealth after saving a fraction saving of it,
as in Chakraborti and Chakrabarti. With a debt limit, they pay a fixed amount in every trade and may go into debt
down to the limit, as in Dragulescu and Yakovenko.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_agents
|
int
|
The number of agents. |
500
|
saving
|
float
|
The saving propensity |
0.0
|
debt
|
float | None
|
The debt limit of the fixed-amount trades, or |
None
|
wealth
|
float
|
The initial wealth of every agent, the mean wealth. |
1.0
|
amount
|
float
|
The amount paid in every fixed-amount trade. |
0.1
|
Returns:
| Name | Type | Description |
|---|---|---|
system |
System
|
The system, with the group |
Raises:
| Type | Description |
|---|---|
ValueError
|
Both a saving propensity and a debt limit are given, or the saving propensity is not below 1. |
Examples:
>>> from skcomplex.systems.economics.econophysics import kinetic_wealth_exchange
>>> type(kinetic_wealth_exchange(10, debt=2.0).interactions[0]).__name__
'DebtExchangeInteraction'
Source code in src/skcomplex/systems/economics/econophysics/_kinetic.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
kinetic_wealth_welfare(classes=9, *, cooperation=0.2, competition=0.3, eta=1.0, tax=0.0)
Return the kinetic theory of wealth classes, with a welfare policy when the tax is positive.
The population is a distribution over wealth classes, a site group on a lattice of the classes. Encounters between classes move particles one class up or down, with cooperation narrowing the gaps and competition widening them, so the total density stays constant. When competition wins, wealth polarises toward the poorest and the richest classes, and the Gini coefficient grows. A welfare policy is an encounter with a group of fixed density, which moves the richest third of the classes down and the poorest third up and lowers the stationary Gini coefficient.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classes
|
int
|
The number of wealth classes. |
9
|
cooperation
|
float
|
The probability of the move that narrows the gap between the classes of an encounter. |
0.2
|
competition
|
float
|
The probability of the move that widens it. |
0.3
|
eta
|
float
|
The encounter rate. |
1.0
|
tax
|
float
|
The rate of the welfare policy, 0 for none. |
0.0
|
Returns:
| Name | Type | Description |
|---|---|---|
system |
System
|
The system, with the distribution |
Examples:
>>> from skcomplex.systems.economics.econophysics import kinetic_wealth_welfare
>>> [item.label for item in kinetic_wealth_welfare(tax=0.2).interactions]
['exchange', 'welfare']
Source code in src/skcomplex/systems/economics/econophysics/_welfare.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | |
kolkata_restaurant(n_agents=100, *, n_restaurants=None, strategy='random', random_state=0)
Return the Kolkata Paise Restaurant problem, where diners spread over restaurants that serve one meal each.
With random choice the expected utilization of N restaurants by N diners is 1 - 1/e, about 0.632, and with
the stochastic crowd avoiding strategy it settles near 0.8.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_agents
|
int
|
The number of diners. |
100
|
n_restaurants
|
int | None
|
The number of restaurants, or |
None
|
strategy
|
str
|
|
'random'
|
random_state
|
int | Generator | None
|
The seed or generator of the first choices. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
system |
System
|
The system, with the group |
Raises:
| Type | Description |
|---|---|
ValueError
|
The strategy is unknown. |
Examples:
>>> from skcomplex.systems.economics.econophysics import kolkata_restaurant
>>> type(kolkata_restaurant(10, strategy='crowd_avoiding').interactions[0]).__name__
'CrowdAvoidanceInteraction'
Source code in src/skcomplex/systems/economics/econophysics/_kolkata.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
mean_wealth(s, wealth)
Return the mean wealth of a distribution over wealth classes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
SimpleNamespace
|
The namespace of one member of the distribution. |
required |
wealth
|
NDArray[float64]
|
The wealth of every class. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
mean |
Expression
|
The normalized first moment. |
Source code in src/skcomplex/systems/economics/econophysics/_welfare.py
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
minority_game(n_agents=101, *, memory=3, evolutionary=False, period=50, fraction=0.1, random_state=0)
Return the minority game of an odd number of agents with two random strategies each.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_agents
|
int
|
The number of agents, odd so that there is always a minority. |
101
|
memory
|
int
|
The memory |
3
|
evolutionary
|
bool
|
Whether every |
False
|
period
|
int
|
The number of rounds between two evolutions of the evolutionary version. |
50
|
fraction
|
float
|
The fraction of poorest agents that take new strategies at an evolution. |
0.1
|
random_state
|
int | Generator | None
|
The seed or generator of the strategies. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
system |
System
|
The system, with the group |
Raises:
| Type | Description |
|---|---|
ValueError
|
The number of agents is even. |
Examples:
>>> from skcomplex.systems.economics.econophysics import minority_game
>>> minority_game(11, memory=2).interactions[1].memory
2
Source code in src/skcomplex/systems/economics/econophysics/_minority.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | |
policy_table(classes)
Return the rate pattern and the transition table of a welfare policy that taxes the rich and helps the poor.
The policy moves a candidate of the richest third of the classes one class down, and a candidate of the poorest third one class up, and leaves the middle classes alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classes
|
int
|
The number of wealth classes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
pattern |
NDArray[float64]
|
The rate pattern |
table |
NDArray[float64]
|
The transition table |
Examples:
>>> from skcomplex.systems.economics.econophysics import policy_table
>>> policy_table(3)[0].ravel().tolist()
[1.0, 0.0, 1.0]
Source code in src/skcomplex/systems/economics/econophysics/_welfare.py
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 | |
strategy_table(code, memory)
Return the table of a minority game strategy, the bit it plays after each of the 2^m histories.
A table has 2^m bits, too many to store as one number beyond small memories, so a strategy is stored as the
integer code that seeds a generator of its table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
float
|
The integer code of the strategy. |
required |
memory
|
int
|
The memory |
required |
Returns:
| Name | Type | Description |
|---|---|---|
table |
IntArray
|
The bit of the strategy for every history, from 0 to |
Examples:
>>> from skcomplex.systems.economics.econophysics import strategy_table
>>> strategy_table(7, 2).shape
(4,)
Source code in src/skcomplex/systems/economics/econophysics/_minority.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
wealth_classes(classes)
Return the wealth of every class, evenly spaced from 0 for the poorest to 1 for the richest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classes
|
int
|
The number of wealth classes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
wealth |
NDArray[float64]
|
The wealth of every class. |
Examples:
>>> from skcomplex.systems.economics.econophysics import wealth_classes
>>> wealth_classes(3).tolist()
[0.0, 0.5, 1.0]
Source code in src/skcomplex/systems/economics/econophysics/_welfare.py
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
wealth_density(s, wealth)
Return the density of a distribution over wealth classes, its order-0 moment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
SimpleNamespace
|
The namespace of one member of the distribution. |
required |
wealth
|
NDArray[float64]
|
The wealth of every class. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
density |
Expression
|
The order-0 moment. |
Source code in src/skcomplex/systems/economics/econophysics/_welfare.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
wealth_observables(classes)
Return the density, the mean wealth, and the Gini coefficient of a distribution wealth over wealth classes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classes
|
int
|
The number of wealth classes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
observables |
list[ObservableSpec]
|
The observables, each reading the moments of the distribution. |
Examples:
>>> from skcomplex.systems.economics.econophysics import wealth_observables
>>> [item.label for item in wealth_observables(9)]
['density', 'mean_wealth', 'gini']
Source code in src/skcomplex/systems/economics/econophysics/_welfare.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | |