-
Notifications
You must be signed in to change notification settings - Fork 125
/
tests.py
72 lines (58 loc) · 2.54 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
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
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
import random
import numpy as np
from scipy import stats
from sugarscape_g1mt.model import SugarscapeG1mt, flatten
from sugarscape_g1mt.trader_agents import Trader
random.seed(1)
def check_slope(y, increasing):
x = range(len(y))
slope, intercept, _, p_value, _ = stats.linregress(x, y)
result = (slope > 0) if increasing else (slope < 0)
# p_value for significance.
assert result and p_value < 0.05, (slope, p_value)
def test_decreasing_price_variance():
# The variance of the average trade price should decrease over time (figure IV-3)
# See Growing Artificial Societies p. 109.
model = SugarscapeG1mt()
model.datacollector._new_model_reporter(
"price_variance",
lambda m: np.var(
flatten([a.prices for a in m.schedule.agents_by_type[Trader].values()])
),
)
model.run_model(step_count=50)
df_model = model.datacollector.get_model_vars_dataframe()
check_slope(df_model.price_variance, increasing=False)
def test_carrying_capacity():
def calculate_carrying_capacities(enable_trade):
carrying_capacities = []
visions = range(1, 10)
for vision_max in visions:
model = SugarscapeG1mt(vision_max=vision_max, enable_trade=enable_trade)
model.run_model(step_count=50)
carrying_capacities.append(len(model.schedule.agents_by_type[Trader]))
return carrying_capacities
# Carrying capacity should increase over mean vision (figure IV-6).
# See Growing Artificial Societies p. 112.
carrying_capacities_with_trade = calculate_carrying_capacities(True)
check_slope(
carrying_capacities_with_trade,
increasing=True,
)
# Carrying capacity should be higher when trade is enabled (figure IV-6).
carrying_capacities_no_trade = calculate_carrying_capacities(False)
check_slope(
carrying_capacities_no_trade,
increasing=True,
)
t_statistic, p_value = stats.ttest_rel(
carrying_capacities_with_trade, carrying_capacities_no_trade
)
# t_statistic > 0 means carrying_capacities_with_trade has larger values
# than carrying_capacities_no_trade.
# p_value for significance.
assert t_statistic > 0 and p_value < 0.05
# TODO:
# 1. Reproduce figure IV-12 that the log of average price should decrease over average agent age
# 2. Reproduce figure IV-13 that the gini coefficient on trade should decrease over mean vision, and should be higher with trade
# 3. a stricter test would be to ensure the amount of variance of the trade price matches figure IV-3