Skip to content
eltrompeteroPublic

About

Convenient Interface to Inverse Ising (ConIII)

Topics

Resources

Stars

23 stars

Watchers

2 watching

Forks

Repository files navigation

ConIII

tests PyPI docs license

ConIII: Convenient Interface to Inverse Ising

ConIII is a Python package for solving maximum entropy problems, with a focus on the pairwise maximum entropy model, also known as the inverse Ising problem. Given samples of binary variables, it finds the fields hi and couplings Jij of the Ising model whose means and pairwise correlations match those of the data. Several standard solvers share one interface, so you can switch between them or compare them on the same data.

If you use ConIII in your research, please cite the paper.

Installation

$ pip install coniii

ConIII requires Python 3.10 or newer and runs on Linux, macOS and Windows. It is not on conda-forge; in a conda environment, install it with pip.

Quick start

Data go in a matrix with one row per observation and one column per variable, with values in {-1, 1}. Convert 0/1 data with X = 2*X - 1.

This example samples from an Ising model with known parameters, then solves for them with pseudolikelihood:

import numpy as np
from coniii import Pseudo, vec2mat
from coniii.samplers import sample_ising

# Draw 10,000 samples of 10 spins from an Ising model with known parameters.
n = 10
rng = np.random.RandomState(0)
true_params = rng.normal(scale=0.2, size=n + n*(n-1)//2)  # fields h_i, then couplings J_ij (i < j)
X = sample_ising(true_params, 10_000, seed=0, parallel=False)  # shape (10000, 10), values in {-1, 1}

# Find the fields and couplings that reproduce the data's means and pairwise correlations.
solver = Pseudo(X)
params = solver.solve()
h, J = vec2mat(params, separate_fields=True)  # h has shape (10,), J has shape (10, 10)

print(np.abs(params - true_params).max())  # largest error in any parameter

Every solver follows the same pattern: create it with the data, then call solve(). Some solvers take extra arguments, such as the accuracy threshold for ClusterExpansion. The solved parameters are also stored in solver.multipliers.

Solvers

Solver Method Notes
Enumerate Exact enumeration of the partition function Exact, but cost grows as 2N, so only for small systems. Equations for N ≤ 9 are included; see below for larger N.
SparseEnumerate Exact enumeration with a subset of the parameters Parameters you leave out are fixed at zero.
Pseudo Pseudolikelihood (Aurell & Ekeberg, PRL 2012) Fast and scales to large systems. Pairwise Ising model only.
MPF Minimum probability flow (Sohl-Dickstein, Battaglino & DeWeese, PRL 2011) Fast and approximate. Extends to other constraints through calc_observables.
ClusterExpansion Adaptive cluster expansion (Barton & Cocco, J. Stat. Mech. 2013) A threshold sets the accuracy; lower is more accurate and slower. Pairwise Ising model only.
MCH Monte Carlo histogram (Broderick et al., arXiv 2007) Works for general maximum entropy problems and can match the correlations as closely as you want. Slowest, since it samples by Monte Carlo, and needs its learning rule tuned.
RegularizedMeanField Regularized mean field (Daniels, Krakauer & Flack, Nat. Commun. 2017) Mean-field solution with regularized couplings; the regularization strength is chosen by Monte Carlo sampling. Pairwise Ising model only.

The usage guide notebook runs each solver on the same example and compares the results. It also shows a model with triplet interactions. The notebook is installed with the package; to find it, run

$ python -c "import coniii, os; print(os.path.join(os.path.dirname(coniii.__file__), 'usage_guide.ipynb'))"

Copy it to a directory outside the package before opening it with Jupyter.

The API reference is at coniii.readthedocs.io, and the paper describes the methods and the package design.

Exact solutions for N > 9

Enumerate needs a file of equations for each system size. Files for N ≤ 9 are included. For a larger system, generate the file by running the enumerate module (from any directory):

$ python -m coniii.enumerate [N] 1

where [N] is the system size. The trailing 1 selects the {-1, 1} basis (the package default); omit it for the {0, 1} basis. See the __main__ block at the end of enumerate.py for more options.

Package layout

Area Status What's there
coniii.solvers stable Inverse-Ising solvers: Enumerate, SparseEnumerate, MPF, MCH, Pseudo, ClusterExpansion, RegularizedMeanField.
coniii.samplers stable Monte Carlo samplers: Metropolis, WolffIsing, ParallelTempering, Potts3, and the sample_ising helper.
coniii.models stable Maxent model classes: Ising, Triplet, Potts3.
coniii.utils stable Indexing, correlation, parameter-conversion, and helper-function utilities.
coniii.enumerate, coniii.enumerate_potts, coniii.ising_eqn stable Exact enumeration and the generated equation files it produces.
coniii.experimental work in progress Unvalidated samplers (SWIsing, HamiltonianMC, Heisenberg3DSampler) and an entropy stub. Not imported by import coniii; import explicitly.
coniii.legacy deprecated mean_field_ising (still used internally by ClusterExpansion/RegularizedMeanField) and pseudo_inverse_ising. Direct imports warn.

The top-level names available via from coniii import * are listed in coniii.__all__.

Optional C++ samplers

The Metropolis and Potts3 samplers run compiled numba code, which is included in the pip package. They also have an optional C++ extension built with Boost.Python, which is used instead when it is built. It is about as fast as the numba samplers and will be removed in a later release (#41), so most users don't need it. The wheel on PyPI does not include it. To build it, install from source in an environment that has the Boost headers and the Boost.Python and Boost.NumPy libraries for your Python version:

$ git clone https://github.com/eltrompetero/coniii.git
$ cd coniii
$ pip install .

The build looks for Boost in the active conda environment, /usr/include and /usr/local/include. If it doesn't find Boost, it installs without the extension and the numba samplers are used. To check whether the extension was built, run

$ python -c "from coniii import samplers; print(samplers.IMPORTED_SAMPLERS_EXT)"

If your Boost is installed somewhere else, add its include directory to include_dirs in the _detect_boost() function in setup.py.

Known issue (#46): on Linux, building against the system's Boost packages (e.g. Ubuntu's libboost-python-dev) links the extension to a Boost.NumPy library compiled for NumPy 1.x, and import coniii then crashes with a segmentation fault under NumPy 2. Install numpy<2 in that environment, or install the wheel from PyPI, which has no extension. Removing the extension will also remove this issue.

Running the tests

$ pip install "coniii[test]"
$ pytest --pyargs coniii

CI runs the tests on Linux, macOS and Windows for Python 3.10 through 3.13.

Getting help and contributing

Please report bugs and request features on the issue tracker. Include your operating system, your Python and ConIII versions (python -c "import coniii; print(coniii.__version__)"), and the full error message or a short example that reproduces the problem. The output of the tests above is often useful too.

Pull requests are welcome. Please open them against the dev branch. See DEVREADME for how the repository is organized.

Release notes

Changes in each version are listed in RELEASE_NOTES. Version 4.0.0 changed the interface, so read its notes before upgrading from 3.x.

Citing ConIII

If you use ConIII in your research, please cite:

Lee, E.D. and Daniels, B.C., 2019. Convenient Interface to Inverse Ising (ConIII): A Python 3 Package for Solving Ising-Type Maximum Entropy Models. Journal of Open Research Software, 7(1), p.3. DOI: 10.5334/jors.217.

@article{lee2019coniii,
  title   = {Convenient Interface to Inverse Ising (ConIII): A Python 3 Package for
             Solving Ising-Type Maximum Entropy Models},
  author  = {Lee, Edward D. and Daniels, Bryan C.},
  journal = {Journal of Open Research Software},
  volume  = {7},
  number  = {1},
  pages   = {3},
  year    = {2019},
  doi     = {10.5334/jors.217}
}

License

ConIII is released under the MIT License. See LICENSE.txt.

About

Convenient Interface to Inverse Ising (ConIII)

Topics

Resources

Stars

23 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages