Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix burnin for linalg #35

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions thermox/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def solve(
num_samples: int = 10000,
dt: float = 1.0,
burnin: int = 0,
key: Array = None,
key: Array | None = None,
associative_scan: bool = True,
) -> Array:
"""
Expand All @@ -37,11 +37,12 @@ def solve(
"""
if key is None:
key = random.PRNGKey(0)
ts = jnp.arange(burnin, burnin + num_samples) * dt
ts = jnp.arange(burnin, burnin + num_samples + 1) * dt
ts = jnp.concatenate([jnp.array([0]), ts])
x0 = jnp.zeros_like(b)
samples = sample_identity_diffusion(
key, ts, x0, A, jnp.linalg.solve(A, b), associative_scan
)
)[1:]
return jnp.mean(samples, axis=0)


Expand All @@ -50,7 +51,7 @@ def inv(
num_samples: int = 10000,
dt: float = 1.0,
burnin: int = 0,
key: Array = None,
key: Array | None = None,
associative_scan: bool = True,
) -> Array:
"""
Expand All @@ -72,10 +73,11 @@ def inv(
"""
if key is None:
key = random.PRNGKey(0)
ts = jnp.arange(burnin, burnin + num_samples) * dt
ts = jnp.arange(burnin, burnin + num_samples + 1) * dt
ts = jnp.concatenate([jnp.array([0]), ts])
b = jnp.zeros(A.shape[0])
x0 = jnp.zeros_like(b)
samples = sample(key, ts, x0, A, b, 2 * jnp.eye(A.shape[0]), associative_scan)
samples = sample(key, ts, x0, A, b, 2 * jnp.eye(A.shape[0]), associative_scan)[1:]
return jnp.cov(samples.T)


Expand All @@ -84,7 +86,7 @@ def expnegm(
num_samples: int = 10000,
dt: float = 1.0,
burnin: int = 0,
key: Array = None,
key: Array | None = None,
alpha: float = 0.0,
associative_scan: bool = True,
) -> Array:
Expand Down Expand Up @@ -113,10 +115,11 @@ def expnegm(
A_shifted = (A + alpha * jnp.eye(A.shape[0])) / dt
B = A_shifted + A_shifted.T

ts = jnp.arange(burnin, burnin + num_samples) * dt
ts = jnp.arange(burnin, burnin + num_samples + 1) * dt
ts = jnp.concatenate([jnp.array([0]), ts])
b = jnp.zeros(A.shape[0])
x0 = jnp.zeros_like(b)
samples = sample(key, ts, x0, A_shifted, b, B, associative_scan)
samples = sample(key, ts, x0, A_shifted, b, B, associative_scan)[1:]
return autocovariance(samples) * jnp.exp(alpha)


Expand All @@ -125,7 +128,7 @@ def expm(
num_samples: int = 10000,
dt: float = 1.0,
burnin: int = 0,
key: Array = None,
key: Array | None = None,
alpha: float = 1.0,
associative_scan: bool = True,
) -> Array:
Expand Down
2 changes: 1 addition & 1 deletion thermox/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def sample(

by using exact diagonalization.

Preprocessing (diagonalisation) costs O(d^3) and sampling costs O(T * d^2),
Preprocessing (diagonalization) costs O(d^3) and sampling costs O(T * d^2),
where T=len(ts).

If associative_scan=True then jax.lax.associative_scan is used which will run in
Expand Down