Skip to main content

Euclidean Projections onto Positive and Capped Simplices

DOI Openseize pypi release Python Version from PEP 621 TOML Simplexers is released under the BSD 3-Clause license. Simplexers' test status Pull Request Welcomed!

About | Installation | Dependencies | Attribution | Contributions | Issues | License | Acknowledgements

About

The s-capped simplex is defined as:

\Delta_{s}^{=} := \{\mathbf{x} \in \mathbb{R}^{n} \mid \mathbf{x}^T\mathbf{1} = s,
 \mathbf{0} \leq \mathbf{x} \leq \mathbf{1} \}

Geometrically, the simplex is a slice at $\mathbf{x}^T\mathbf{1} = s$ of a hypercube $\mathbf{0} \leq \mathbf{x} \leq \mathbf{1}$ shown by the blue region in the image.

Projecting a vector onto a simplex is an important subproblem that appears in imaging, statistics, and machine learning applications 1 2 3. The projection of vector $\mathbf{y}$ onto the simplex amounts to finding a vector $\mathbf{x}*$ that lives in the blue (feasible) region that is closest to y. This vector is the shadow of $\mathbf{y}$. Formally, this projection is written as:

\mathbf{x}^* = proj_{\Delta_{s}^{=}}\left(\mathbf{y}\right) = \underset{x}{\mathrm{argmin}}\{
\frac{1}{2}\|\mathbf{x} - \mathbf{y}\|^2 \mid \mathbf{x} \in
\Delta_{s}^{=}\}

- This software computes the projection of vectors onto s-capped and positive simplices (simplices where $\mathbf{x}^*$'s components can be > 1) using sorting methods and fast root finding methods of the Lagrangian's critical points. 2 4. -

Example

import numpy as np
from simplexers import capped, positive

rng = np.random.default_rng()
x = rng.uniform(0, 3, size=(4, 100))

# construct capped and positive projections
capped_projection = capped.capped_simplexer(x, s=1, axis=-1)
positive_projection = positive.positive_simplexer(x, s=1, axis=-1)

# validate the sum of each of the 4 vectors is 1
print(np.allclose(np.sum(capped_projection, axis=-1), 1))
print(np.allclose(np.sum(positive_projection, axis=-1), 1))

Installation

Simplexers is available on pypi for easy intallation into virtual environments.

Python Virtual Environment

  1. Create your virtual environment, Here we name it my_venv.
$ python3 -m venv my_venv
  1. Activate your 'my_venv' environment
$ source my_venv/bin/activate
  1. Install openseize into your virtual environment
(my_venv)$ pip install simplexers

Conda Virtual Environment

  1. Download the simplexers environment configuration yaml

  2. Create a conda simplexers environment.

$ conda env create --file environment.yml
  1. Activate the simplexers environment.
$ conda activate simplexers

From Source

To get the development version:

  1. Create a virtual environment with latest pip version.
$ python3 -m venv env
$ source env/bin/activate
$ pip install --upgrade pip
  1. Get the source code
$ git clone https://github.com/mscaudill/simplexers.git
  1. CD into the directory containing the pyproject.toml and create an editable install with pip using the development dependencies
$ pip install -e .[dev]

Dependencies

Simplexers requires Python ≥ 3.10 and has the following dependencies:

package pypi conda
numpy https://pypi.org/project/numpy/ ✓
scipy https://pypi.org/project/scipy/ ✓
ipython https://pypi.org/project/ipython/ ✓
notebook https://pypi.org/project/jupyter/ ✓

Attribution

Please see the Cite this repository under the About section or the citation file.

Contributions

Contributions are what makes open-source fun and we would love for you to contribute. Please check out our contribution guide to get started.

Issues

Simplexers provides custom issue templates for filing bugs, requesting feature enhancements, suggesting documentation changes, or just asking questions. You can file an issue here.

License

Simplexers is licensed under the terms of the 3-Clause BSD License.

Acknowledgements

This work is generously supported through the Ting Tsung and Wei Fong Chao Foundation

Metadata

Release files for simplexers 1.0.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 simplexers 1.0.0
File Size Uploaded
simplexers-1.0.0.tar.gz 14.9 kB Details

Built distribution (wheel)

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

Total release size: 26.3 kB

Release files / simplexers-1.0.0.tar.gz

Download URL simplexers-1.0.0.tar.gz
Size 14.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e5cdacab3181735f0aad004577b525d32a310831dd67f2bf20f5e40b34e18962
BLAKE2b-256 checksum
How to use checksums
9b2d4b9a9bc5d1a6ff149569f958ddb16b91465e3885010f1c91e3012af7aa97
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release files / simplexers-1.0.0-py3-none-any.whl

Download URL simplexers-1.0.0-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cf62d1c599eab48fcb7156ee6ef566ddf17d8dbbdb3a01fb89725815648f8e63
BLAKE2b-256 checksum
How to use checksums
08fb40cca98ec077e254e9211c4fb2cd7c364cc656752c62a885964cb544cd9d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

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