Skip to main content

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.

Metadata

Release files for coniii 4.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for coniii 4.1.0
File Size Uploaded Provenance Actions
coniii-4.1.0.tar.gz 633.7 kB Without provenance Details for coniii-4.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for coniii 4.1.0
File Interpreter ABI Platform Actions
coniii-4.1.0-py3-none-any.whl Python 3 none any Details for coniii-4.1.0-py3-none-any.whl

Total release size: 1.3 MB

Release files / coniii-4.1.0.tar.gz

Download URL coniii-4.1.0.tar.gz
Size 633.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5af1105f432a5471afe465ec746d9cb0752314d1187a025a407013a188f6d976
BLAKE2b-256 checksum
How to use checksums
6dda4e127912de982d5e9c1c8c550b43df2ff250c7cdfdeb591534b8d8701ae8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.18

Release files / coniii-4.1.0-py3-none-any.whl

Download URL coniii-4.1.0-py3-none-any.whl
Size 672.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5eddb0d68e2cf36753e0bd765241867932ec0d256ff352a189f23025470f8f2e
BLAKE2b-256 checksum
How to use checksums
a8c6d1a9037d2706ad2a3bd96684cde42fe89e89e8769759b889c11466270c41
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.18

Security for coniii 4.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

4.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page