Skip to content

Commit

Permalink
lint: fix NPY002 for Brock-Hommes model
Browse files Browse the repository at this point in the history
To reproduce the errors:
```
ruff check --select "NPY" examples/models/economics/brock_hommes.py
```

We also added tests to ensure reproducibility with fixed seed.
  • Loading branch information
marcofavorito authored and marcofavoritobi committed Sep 21, 2023
1 parent a67a1f6 commit e3dccc3
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 114 deletions.
14 changes: 7 additions & 7 deletions examples/models/economics/brock_hommes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def BH2( # noqa: N802
Returns:
simulated series
"""
np.random.seed(seed=seed)
rng = np.random.default_rng(seed=seed)

R = 1.10 # noqa: N806
beta = 3.6
Expand Down Expand Up @@ -78,7 +78,7 @@ def BH2( # noqa: N802

for t in range(2, N + 1):
x[t] = n[1] * g[1] * x[t - 1] / R
x[t] = x[t] + np.random.uniform(low=div_eps_min, high=div_eps_max, size=1)
x[t] = x[t] + rng.uniform(low=div_eps_min, high=div_eps_max, size=1)

n[0] = np.exp(bsa * (R * x[t - 1] * (R * x[t - 1] - x[t])) - beta * C)
n[1] = np.exp(bsa * (x[t] - R * x[t - 1]) * (g[1] * x[t - 2] - R * x[t - 1]))
Expand Down Expand Up @@ -115,10 +115,10 @@ def BH4( # noqa: N802
Returns:
simulated series
"""
np.random.seed(seed=seed)
rng = np.random.default_rng(seed=seed)

R = 1.01 # noqa: N806
beta = 120
R = 1.0 + theta[-2] # noqa: N806
beta = theta[-1]
sigma = 0.04

# BH noise:
Expand All @@ -133,7 +133,7 @@ def BH4( # noqa: N802
x[0] = x_lag2
x[1] = x_lag1

for i in range(min(int(len(theta) / 2), 4)):
for i in range((len(theta) // 2) - 1):
g[i] = theta[i * 2]
b[i] = theta[i * 2 + 1]

Expand All @@ -150,7 +150,7 @@ def BH4( # noqa: N802
# divEpsMax = 0 # 0.05
# dividend_noise = np.random.uniform(low=divEpsMin, high=divEpsMax, size=1)
# DP noise:
dividend_noise = np.random.normal(0, sigma, 1)
dividend_noise = rng.normal(0, sigma, 1)
x[t] = (np.sum(weighted_exp) + dividend_noise) / R

left_factor = x[t] - R * x[t - 1]
Expand Down
2 changes: 2 additions & 0 deletions tests/test_calibrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ def test_calibrator_calibrate(self, n_jobs: int) -> None:

# This is a temporary workaround to make tests to run also on Windows.
# See: https://github.com/bancaditalia/black-it/issues/49
print(params.tolist())
print(losses.tolist())
if sys.platform == "darwin":
assert np.allclose(params, self.darwin_expected_params)
assert np.allclose(losses, self.darwin_expected_losses)
Expand Down
198 changes: 99 additions & 99 deletions tests/test_examples/test_brock_hommes.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,105 +142,105 @@ def test_bh4_model() -> None:
seed = 42
expected_output = np.array(
[
[0.0892263],
[0.0563433],
[0.06414067],
[0.1050808],
[0.06465298],
[0.03484719],
[0.08615989],
[0.09137854],
[0.04505338],
[0.05185943],
[0.0178482],
[-0.00671861],
[0.00442111],
[-0.07247632],
[-0.12025483],
[-0.10683951],
[-0.11411574],
[-0.06706414],
[-0.08159742],
[-0.11298336],
[-0.02118343],
[-0.02181211],
[-0.01250533],
[-0.06493467],
[-0.06777318],
[-0.04278995],
[-0.07482654],
[-0.0378188],
[-0.04933496],
[-0.04609427],
[-0.05581557],
[0.03434773],
[0.02512761],
[-0.02459935],
[0.01448361],
[-0.0374949],
[-0.01884807],
[-0.09034455],
[-0.11689355],
[-0.07402553],
[-0.02132221],
[-0.00696484],
[-0.00913479],
[-0.01832021],
[-0.07147812],
[-0.07928009],
[-0.073528],
[-0.00914052],
[0.00854694],
[-0.06352846],
[-0.03278851],
[-0.03743684],
[-0.05293504],
[-0.01289774],
[0.03266696],
[0.06049519],
[0.00938186],
[-0.00674687],
[0.00810651],
[0.04456955],
[0.01274162],
[0.00086996],
[-0.04344781],
[-0.07846817],
[-0.02308353],
[0.03877772],
[0.0253172],
[0.05708022],
[0.05465977],
[0.01240909],
[0.02209176],
[0.07646935],
[0.05285044],
[0.09823849],
[-0.03447823],
[0.00596483],
[0.00840848],
[-0.00594631],
[-0.00078766],
[-0.07916013],
[-0.06531819],
[-0.03099877],
[0.03767566],
[0.00701667],
[-0.0277527],
[-0.03985857],
[0.00829645],
[0.01974978],
[-0.00702008],
[0.01491091],
[0.01465264],
[0.04854568],
[0.00663405],
[-0.00920254],
[-0.0222435],
[-0.07368844],
[-0.04054655],
[-0.0171874],
[-0.01127672],
[0.08162246],
[0.01518795],
[0.0389381],
[0.06481314],
[-0.03167937],
[-0.07550877],
[-0.04828303],
[-0.04554649],
[-0.03227944],
[-0.05596049],
[-0.00456788],
[0.02865612],
[0.02319651],
[0.06066374],
[0.06145787],
[0.00871906],
[0.01960806],
[-0.02412119],
[0.01714444],
[0.01077186],
[4.172e-05],
[-0.02715309],
[0.02899408],
[0.01516408],
[-0.00669485],
[-0.01903755],
[0.00759759],
[0.02028846],
[0.03070637],
[0.03862254],
[0.11183309],
[0.0631743],
[0.02264867],
[-0.01730684],
[0.01156946],
[0.05333543],
[0.03340287],
[-0.01044686],
[-0.04078947],
[-0.00319678],
[0.02796493],
[0.04157364],
[0.00282316],
[0.01038224],
[0.01199221],
[0.01703188],
[0.04645603],
[0.04174959],
[0.05582337],
[0.04178035],
[0.04021709],
[0.05293433],
[-0.02064256],
[-0.0284747],
[-0.03858314],
[-0.05233422],
[-0.04756549],
[0.02622807],
[-0.01459457],
[0.02737786],
[-0.0467665],
[-0.04727375],
[-0.02643611],
[0.00525546],
[0.0324541],
[0.05452664],
[0.02454655],
[-0.00184605],
[0.03216834],
[0.01547663],
[-0.04009435],
[-0.07386154],
[-0.08844246],
[-0.0421084],
[-0.02268923],
[0.01196387],
[-0.00791187],
[0.00037916],
[0.02520534],
[0.00577081],
[0.02171413],
[-0.01079571],
[-0.02253499],
[-0.0310218],
[-0.06910108],
[-0.02953476],
[-0.03832997],
[-0.02633743],
[0.00096724],
[0.01890278],
[0.03985309],
[0.02423046],
[-0.00022935],
[-0.0038057],
[-0.06954288],
[-0.10698327],
[-0.12752616],
[-0.12860932],
[-0.07361663],
[0.0],
],
)
Expand Down
9 changes: 1 addition & 8 deletions tests/test_samplers/test_xgboost.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,7 @@ def test_clip_losses() -> None:
assert np.allclose(
losses,
np.array(
[
7.46098998e02,
5.80544566e17,
3.40282347e38,
3.40282347e38,
3.40282347e38,
2.94273501e41,
],
[0.18388932, 0.58118863, 0.84728975, 0.87882275, 0.88818152, 1.2508034],
),
)

Expand Down

0 comments on commit e3dccc3

Please sign in to comment.