Skip to main content

A wrapper for the Vegas integrator

Project description

PyPI - Version Test

vegas_params

Wrapper package for Vegas integrator.

This package allows user to define composite expressions, which can be used to:

  1. Calculate integrals using vegas algorithm
  2. Make random samples of given expressions (for MC simulation apart from vegas)

1. Installation

You can install vegas_params from PyPI:

pip install vegas_params

or directly via github link:

pip install git+https://github.com/RTESolution/vegas_params.git

2. Quickstart

Let's caluclate integral $$ \int\limits_{-1000}^{1000}dx \int\limits_{-1000}^{1000}dy ; e^{-(x^2+y^2)} $$

Here is how you can do it:

import vegas_params as vp
@vp.integral
@vp.expression(x=vp.Uniform([-1000,1000]),
            y=vp.Uniform([-1000,1000]))
def gaussian(x, y):
    return np.exp(-(x**2+y**2))

Now gaussian is a callable, where you can pass the parameters of the vegas integrator:

gaussian(nitn=20, neval=100000)
#3.13805(16) - close to expected Pi

3. Defining expressions

3.1 Basic parameters

There are basic types of expressions:

#fixed value:
c = vp.Fixed(299792458) #speed of light in vacuum in m/s
#uniform value, that will be integrated over
cos_theta = vp.Uniform([-1,1]) #cos_theta is in the given range

3.2 Concatenating parameters: collections.Concat

It's possible to concatenate the parameters using vp.collections.Concat:

#say we want to define 3 coordinates with different limits: x in [-1,1], y in [0,10] and z fixed to 0
xyz = vp.collections.Concat(vp.Uniform([-1,1]), vp.Uniform([0,10]), vp.Fixed(0))
xyz.sample(1)
# array([[-0.67329745,  5.77401635,  0.        ]])

Same concatenation can be done with | operator. Also Fixed can be omitted:

#equivalent to above
xyz = vp.Uniform([-1,1])|vp.Uniform([0,10])|0
xyz.sample(1)
#array([[-0.20342379,  0.7356486 ,  0.        ]])

3.3 Producing structured array with collections.StructArray

It's possible to make a numpy.structured_array from given parameters:

xyz = vp.collections.StructArray(x=vp.Uniform([-1,1]), y=vp.Uniform([0,10]), z=0)
data = xyz.sample(1)
#array(([-0.04495105], [1.97159299], [0.]),
# dtype=[('x', '<f8', (1,)), ('y', '<f8', (1,)), ('z', '<f8', (1,))])
data['x']
#array([-0.04495105])

3.4 General compund expressions with expression decorator

Other expressions can be defined from these components.

More complex expressions can be defined using vp.Expression class or vp.expression decorator:

@vp.expression
def product(x,y):
    return x*y
    

It's possible to also add the Jacobian factor to the expression. To do this, add self argument to function and set serlf.factor to needed value. This value will be multiplied by the expression value during the integration.

4. Sampling

The vegas_params expressions and parameters can be used not only for integration, but as random value generators.

4.1. Sampling without factor

You can ask an expression to generate a sample of given size:

data = expr.sample(size=1000)

If your expression, or it's components (the expression parameters, provided in constructor) define the factor, then a simple sample method will not take it into account.

4.2 Sampling with factor

In case you want to get a sample which is distributed according to the expression's factor, you need to use sample_with_factor method, which will iteratively generate samples and filter them, by randomly discarding the samples according to their factor values:

data = expr.sample_with_factor(size=1000)

Note that this method is iterative and much slower that sample method.

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

vegas_params-0.2.1.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

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

vegas_params-0.2.1-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file vegas_params-0.2.1.tar.gz.

File metadata

  • Download URL: vegas_params-0.2.1.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for vegas_params-0.2.1.tar.gz
Algorithm Hash digest
SHA256 88f97d04454d3a97a046cb05b01c902df65da194ab26202918372c31bd2d38b8
MD5 4e7190b8568b5d5b69d0babc0ab9be86
BLAKE2b-256 2c9b9192f328ec51413bbfa36450f5d5d1f3c8b6acd7677415f975363ed53441

See more details on using hashes here.

Provenance

The following attestation bundles were made for vegas_params-0.2.1.tar.gz:

Publisher: python-publish.yml on RTESolution/vegas_params

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

File details

Details for the file vegas_params-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: vegas_params-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for vegas_params-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e44311495759f83d3efa74a037734ec8b26fa74e46f173b0099282f19d3093be
MD5 2d97fc47f91a9e5a108d6f8c598f0947
BLAKE2b-256 3b5b3c9ee71795e897e8b253a249ad8fb8e3db075bac06cc9c87473e027ecb13

See more details on using hashes here.

Provenance

The following attestation bundles were made for vegas_params-0.2.1-py3-none-any.whl:

Publisher: python-publish.yml on RTESolution/vegas_params

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