iliasoroka1/Spin-Model-Fitting-on-GPU
Ising Spin Model Fitting on GPU using thrml and jax
Spin Model Fitting on GPU
GPU-accelerated implementation of Ising model sampling and maximum entropy fitting using JAX and THRML.
Overview
This package provides tools for maximum entropy parameter fitting to match target statistical properties (mean and correlations).
Background
The Ising Model
where:
-
$J_{ij}$ are pairwise couplings (interactions between random variables) -
$h_i$ are external fields acting on each spin (exogenous variables, biases) - The first sum runs over all pairs of spins
$(i,j)$ with$i < j$
Boltzmann Distribution
At inverse temperature
where
Observable Statistics
From the Boltzmann distribution, we can compute expectation values:
Magnetizations (single-spin averages):
Correlations (two-spin averages):
Gibbs Sampling
Since exact summation over all
where the effective field is:
Maximum Entropy Principle
Given target statistics
The gradients of the log-likelihood with respect to parameters are simple:
We use momentum-based gradient ascent:
where
Usage
import numpy as np
from model import MaxEntBoltzmann
# suppose we observed these statistics from an unknown system
N = 5
target_magnetizations = np.array([0.2, -0.3, 0.1, 0.4, -0.2])
target_correlations = np.array([
[1.0, 0.5, 0.1, 0.0, -0.1],
[0.5, 1.0, 0.6, 0.2, 0.0],
[0.1, 0.6, 1.0, 0.4, 0.1],
[0.0, 0.2, 0.4, 1.0, 0.3],
[-0.1, 0.0, 0.1, 0.3, 1.0]
])
fitter = MaxEntBoltzmannTHRML(
n=N,
target_s=target_magnetizations,
target_ss=target_correlations,
beta=1.0
)
J_fit, h_fit = fitter.fit(
n_iterations=50,
learning_rate=0.1,
momentum=0.9,
n_chains=1000,
n_steps=200,
n_burn_in=100,
adaptive_lr=True,
verbose=True
)