Skip to content

Commit

Permalink
formalize the brute-force testing
Browse files Browse the repository at this point in the history
  • Loading branch information
afmagee42 committed Dec 4, 2024
1 parent fcb1cfc commit a0bedbc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ngm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,11 @@ def _ensure_positive_array(x: np.ndarray) -> np.ndarray:
return -x
else:
raise RuntimeError(f"Cannot make vector all positive: {x}")

def run_ngm(r: np.ndarray, x0: np.ndarray, steps: int) -> np.ndarray:
"""Repeatedly applies the NGM r, starting with x0"""
x = x0
for _ in range(steps):
x = np.matmul(r, x)

return x
7 changes: 7 additions & 0 deletions tests/test_ngm.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,10 @@ def test_kr():
r0 = ngm.dominant_eigen(r).value

assert np.isclose(r0, 2.0013, atol=5e-5)

def test_eigenvectors():
r = np.array([[3.1, 0.15, 1.7],[0.78, 1.5, 0.1],[0.32, 0.98, 1.1]])

brute_force = ngm.run_ngm(r, np.array([1, 0, 0]), 200)
brute_force = brute_force / brute_force.sum()
assert np.isclose(ngm.dominant_eigen(r).vector, brute_force).all()

0 comments on commit a0bedbc

Please sign in to comment.