Skip to main content

Monte-Carlo Nystrom method in NumPy / PyTorch

Project description

Monte-Carlo Nystrom

CI Status Documentation Status Test coverage percentage

uv Ruff pre-commit

PyPI Version Supported Python versions License


Documentation: https://montecarlo-nystrom.readthedocs.io

Source Code: https://github.com/ultrasphere-dev/montecarlo-nystrom


Monte-Carlo Nystrom method in NumPy / PyTorch

Installation

Install this via pip (or your favourite package manager):

pip install montecarlo-nystrom

Usage

Solve integral equations of the second kind of the following form.

$\forall d \in \mathbb{N}.$ $\forall \Omega \in \mathbb{R}^d [\Omega \text{ is bounded Lipschitz}].$ $\forall p \in L^\infty(\Omega, {\mathbb{R}}_{\geq 0}) [\int_\Omega p(y) dy = 1].$ $\forall f, z \in L^2(\Omega,\mathbb{C}).$ $\forall k \in L^2(\Omega,L^2(\Omega,\mathbb{C}))$ $[z(y) + \int_\Omega k(y, y') z(y') p(y') dy' = f(y)].$

Let $N \in \mathbb{N}$, $(y_i)_{i=1}^N$ be i.i.d. samples drawn from $p$.

Let $(z_{N,i})_{i=1}^N$ be the solution of the linear system

$$ z_{N,i} + \frac{1}{N} \sum_{j=1}^N k(y_i, y_j) z_{N,j} = f(y_i) \quad i \in {1, \ldots, N} $$

and

$$ z_N(y) := f(y) - \frac{1}{N} \sum_{i=1}^N k(y, y_i) z_{N,i} \quad y \in \Omega $$

Then $z_N$ would approximate $z$ as $N \to \infty$.

The below example solves the case where $d = 1$, $\Omega = [0, 1]$, $p$ (random_samples) is the uniform distribution on $[0, 1]$, $k(x, y) = |x - y|^{-0.4}$ (kernel), and $f(x) = 1$ (rhs), and evaluates the solution at $x = (0.5,)$.

>>> import numpy as np
>>> from montecarlo_nystrom import montecarlo_nystrom
>>> rng = np.random.default_rng(0)
>>> def random_samples(n):
...     return rng.uniform(0, 1, size=(n, 1))
>>> def kernel(x, y):
...     return np.linalg.vector_norm(x - y, axis=-1) ** -0.4
>>> def rhs(x):
...     x0 = x[..., 0]
...     return np.ones_like(x0)
>>> z_N = montecarlo_nystrom(
...     random_samples=random_samples,
...     kernel=kernel,
...     rhs=rhs,
...     n=100,
...     n_mean=10,
... )
>>> np.round(z_N(np.asarray((0.5,))), 6)  # Evaluate at x=0.5
np.float64(0.272957)

References

Contributors ✨

Thanks goes to these wonderful people (emoji key):

ultrasphere-dev
ultrasphere-dev

💻 🤔 📖

This project follows the all-contributors specification. Contributions of any kind welcome!

Credits

Copier

This package was created with Copier and the browniebroke/pypackage-template project template.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

montecarlo_nystrom-1.1.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

montecarlo_nystrom-1.1.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file montecarlo_nystrom-1.1.0.tar.gz.

File metadata

  • Download URL: montecarlo_nystrom-1.1.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for montecarlo_nystrom-1.1.0.tar.gz
Algorithm Hash digest
SHA256 847327384af73959b283256ca43d1839ab5d65fb18f91b01c7c2b1886fee598c
MD5 182409c4dd731f7a4d06f3c4e8c8b089
BLAKE2b-256 cd961bed7bef7e4d05f35b4c8952ed63fc18e2310eef975e9be105d97aab82bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for montecarlo_nystrom-1.1.0.tar.gz:

Publisher: ci.yml on ultrasphere-dev/montecarlo-nystrom

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file montecarlo_nystrom-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for montecarlo_nystrom-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c6dc6cef117b66b26e968ea032303d9fe8f7f1e3ae1890c388d7e54c936df37d
MD5 b515846f4cddd76b98a2742488012631
BLAKE2b-256 e059a075a8e669d06141297f575208ccb905700e4e57fcf210f0ee1b8f454c65

See more details on using hashes here.

Provenance

The following attestation bundles were made for montecarlo_nystrom-1.1.0-py3-none-any.whl:

Publisher: ci.yml on ultrasphere-dev/montecarlo-nystrom

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page