Reformulating the problem to suit regression

A more general form for the one-dimensional Ising model is $$ \begin{align} H = - \sum_j^L \sum_k^L s_j s_k J_{jk}. \tag{22} \end{align} $$

Here we allow for interactions beyond the nearest neighbors and a state dependent coupling constant. This latter expression can be formulated as a matrix-product $$ \begin{align} \boldsymbol{H} = \boldsymbol{X} J, \tag{23} \end{align} $$

where \( X_{jk} = s_j s_k \) and \( J \) is a matrix which consists of the elements \( -J_{jk} \). This form of writing the energy fits perfectly with the form utilized in linear regression, that is $$ \begin{align} \boldsymbol{y} = \boldsymbol{X}\boldsymbol{\beta} + \boldsymbol{\epsilon}, \tag{24} \end{align} $$

We split the data in training and test data as discussed in the previous example

X = np.zeros((n, L ** 2))
for i in range(n):
    X[i] = np.outer(spins[i], spins[i]).ravel()
y = energies
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)