vegas
Module¶
Introduction¶
The key Python objects supported by the vegas
module are:
vegas.Integrator
— an object describing a multidimensional integration operator. Such objects contain information about the integration volume, and also about optimal remappings of the integration variables based upon the last integral evaluated using the object.vegas.AdaptiveMap
— an object describing the remappings used byvegas
.vegas.RAvg
— an object describing the result of avegas
integration.vegas
returns the weighted average of the integral estimates from eachvegas
iteration as an object of classvegas.RAvg
. These are Gaussian random variables — that is, they have a mean and a standard deviation — but also contain information about the iterationsvegas
used in generating the result.vegas.RAvgArray
— an array version ofvegas.RAvg
used when the integrand is array-valued.vegas.RAvgDict
— a dictionary version ofvegas.RAvg
used when the integrand is dictionary-valued.vegas.PDFIntegrator
— a specialized integrator for evaluating Gaussian expectation values.
These are described in detail below.
Integrator Objects¶
The central component of the vegas
package is the integrator class:
-
class
vegas.
Integrator
¶ Adaptive multidimensional Monte Carlo integration.
vegas.Integrator
objects make Monte Carlo estimates of multidimensional functionsf(x)
wherex[d]
is a point in the integration volume:integ = vegas.Integrator(integration_region) result = integ(f, nitn=10, neval=10000)
The integator makes
nitn
estimates of the integral, each using at mostneval
samples of the integrand, as it adapts to the specific features of the integrand. Successive estimates (iterations) typically improve in accuracy until the integrator has fully adapted. The integrator returns the weighted average of allnitn
estimates, together with an estimate of the statistical (Monte Carlo) uncertainty in that estimate of the integral. The result is an object of typeRAvg
(which is derived fromgvar.GVar
).Integrands
f(x)
return numbers, arrays of numbers (any shape), or dictionaries whose values are numbers or arrays (any shape). Each number returned by an integrand corresponds to a different integrand. When arrays are returned,vegas
adapts to the first number in the flattened array. When dictionaries are returned,vegas
adapts to the first number in the value corresponding to the first key.vegas
can generate integration points in batches for integrands built from classes derived fromvegas.BatchIntegrand
, or integrand functions decorated byvegas.batchintegrand()
. Batch integrands are typically much faster, especially if they are coded in Cython or C/C++ or Fortran.vegas.Integrator
s have a large number of parameters but the only ones that most people will care about are: the numbernitn
of iterations of thevegas
algorithm; the maximum numberneval
of integrand evaluations per iteration; and the damping parameteralpha
, which is used to slow down the adaptive algorithms when they would otherwise be unstable (e.g., with very peaky integrands). Setting parameteranalyzer=vegas.reporter()
is sometimes useful, as well, since it causesvegas
to print (onsys.stdout
) intermediate results from each iteration, as they are produced. This helps when each iteration takes a long time to complete (e.g., longer than an hour) because it allows you to monitor progress as it is being made (or not).Parameters: - map (array,
vegas.AdaptiveMap
orvegas.Integrator
) –The integration region as specified by an array
map[d, i]
whered
is the direction andi=0,1
specify the lower and upper limits of integration in directiond
.map
could also be the integration map from anothervegas.Integrator
, or thatvegas.Integrator
itself. In this case the grid is copied from the existing integrator. - nitn (positive int) – The maximum number of iterations used to adapt to the integrand and estimate its value. The default value is 10; typical values range from 10 to 20.
- neval (positive int) – Approximate number of integrand evaluations
in each iteration of the
vegas
algorithm. Increasingneval
increases the precision: statistical errors should fall at least as fast assqrt(1./neval)
and often fall much faster. The default value is 1000; real problems often require 10–10,000 times more evaluations than this. - nstrat (int array) –
nstrat[d]
specifies the number of stratifications to use in directiond
. By default this parameter is set automatically, based on parameterneval
, withnstrat[d]
approximately the same for everyd
. Specifyingnstrat
explicitly makes it possible to concentrate stratifications in directions where they are most needed. Ifnstrat
is set butneval
is not,neval
is set equal to2*prod(nstrat)/(1-neval_frac)
. - alpha (float) – Damping parameter controlling the remapping
of the integration variables as
vegas
adapts to the integrand. Smaller values slow adaptation, which may be desirable for difficult integrands. Small or zeroalpha
s are also sometimes useful after the grid has adapted, to minimize fluctuations away from the optimal grid. The default value is 0.5. - beta (float) – Damping parameter controlling the redistribution
of integrand evaluations across hypercubes in the
stratified sampling of the integral (over transformed
variables). Smaller values limit the amount of
redistribution. The theoretically optimal value is 1;
setting
beta=0
prevents any redistribution of evaluations. The default value is 0.75. - neval_frac (float) – Approximate fraction of function evaluations
used for adaptive stratified sampling.
vegas
distributes(1-neval_frac)*neval
integrand evaluations uniformly over all hypercubes, with at least 2 evaluations per hypercube. The remainingneval_frac*neval
evaluations are concentrated in hypercubes where the errors are largest. Increasingneval_frac
makes more integrand evaluations available for adaptive stratified sampling, but reduces the number of hypercubes, which limits the algorithm’s ability to adapt. Ignored whenbeta=0
. Default isneval_frac=0.75
. - adapt (bool) – Setting
adapt=False
prevents further adaptation byvegas
. Typically this would be done after training thevegas.Integrator
on an integrand, in order to stabilize further estimates of the integral.vegas
uses unweighted averages to combine results from different iterations whenadapt=False
. The default setting isadapt=True
. - nproc (int or None) –
Number of processes/processors used to evalute the integrand. If
nproc>1
Python’smultiprocessing
module is used to spread integration points across multiple processors, thereby potentially reducing the time required to evaluate the integral. There is a significant overhead involved in using multiple processors so this option is useful only when the integrand is expensive to evaluate. When using themultiprocessing
module in its default mode for MacOS and Windows, it is important that the main module can be safely imported (i.e., without launching new processes). This can be accomplished with some version of theif __name__ == '__main__`:
construct in the main module: e.g.,if __name__ == '__main__': main()
This is not an issue on other Unix platforms. See the
multiprocessing
documentation for more information. Note that settingnproc
greater than 1 disables MPI support. Setnproc=None
to use all the processors on the machine (equivalent tonproc=os.cpu_count()
). Default value isnproc=1
. (Requires Python 3.3 or later.) - analyzer –
An object with methods
analyzer.begin(itn, integrator)
analyzer.end(itn_result, result)
where:
begin(itn, integrator)
is called at the start of eachvegas
iteration withitn
equal to the iteration number andintegrator
equal to the integrator itself; andend(itn_result, result)
is called at the end of each iteration withitn_result
equal to the result for that iteration andresult
equal to the cummulative result of all iterations so far. Settinganalyzer=vegas.reporter()
, for example, causes vegas to print out a running report of its results as they are produced. The default isanalyzer=None
. - uses_jac (bool) – Setting
uses_jac=True
causesvegas
to call the integrand with two arguments:fcn(x, jac=jac)
. The second argument is the Jacobianjac[d] = dx[d]/dy[d]
of thevegas
map. The integral overx[d]
of1/jac[d]
equals 1 (exactly). The default setting isuses_jac=False
. - nhcube_batch (positive int) – The number of hypercubes (in y space)
whose integration points are combined into a single
batch to be passed to the integrand, together,
when using
vegas
in batch mode. The default value is 10,000. Larger values may be lead to faster evaluations, but at the cost of more memory for internal work arrays. - maxinc_axis (positive int) – The maximum number of increments per axis allowed for the x-space grid. The default value is 1000; there is probably little need to use other values.
- max_nhcube (positive int) – Maximum number of y-space hypercubes
used for stratified sampling. Setting
max_nhcube=1
turns stratified sampling off, which is probably never a good idea. The default setting (1e9) was chosen to correspond to the point where internal work arrays become comparable in size to the typical amount of RAM available to a processor (in a laptop in 2014). Internal memory usage is large only whenbeta>0
andminimize_mem=False
; thereforemax_nhcube
is ignored ifbeta=0
orminimize_mem=True
. - max_neval_hcube (positive int) – Maximum number of integrand
evaluations per hypercube in the stratification. The default
value is 1e6. Larger values might allow for more adaptation
(when
beta>0
), but also can result in large internal work arrays. - max_mem (positive float) – Maximum number of floats allowed in
internal work arrays (approx.). A
MemoryError
is raised if the work arrays are too large, in which case one might want to reducemax_neval_hcube
ormax_nhcube
(or increasemax_mem
if there is enough RAM). Default value is 1e9. - rtol (float) – Relative error in the integral estimate
at which point the integrator can stop. The default
value is 0.0 which turns off this stopping condition.
This stopping condition can be quite unreliable
in early iterations, before
vegas
has converged. Use with caution, if at all. - atol (float) – Absolute error in the integral estimate
at which point the integrator can stop. The default
value is 0.0 which turns off this stopping condition.
This stopping condition can be quite unreliable
in early iterations, before
vegas
has converged. Use with caution, if at all. - ran_array_generator – Function that generates
numpy
arrays of random numbers distributed uniformly between 0 and 1.ran_array_generator(shape)
should create an array whose dimensions are specified by the integer-valued tupleshape
. The default generator isnumpy.random.random
. - sync_ran (bool) – If
True
(default), the default random number generator is synchronized across all processors when using MPI. IfFalse
,vegas
does no synchronization (but the random numbers should synchronized some other way). - adapt_to_errors (bool) –
adapt_to_errors=False
causesvegas
to remap the integration variables to emphasize regions where|f(x)|
is largest. This is the default mode.adapt_to_errors=True
causesvegas
to remap variables to emphasize regions where the Monte Carlo error is largest. This might be superior when the number of the number of stratifications (self.nstrat
) in the y grid is large (> 100). It is typically useful only in one or two dimensions. - uniform_nstrat (bool) – If
True
, requires that thenstrat[d]
be equal for alld
. IfFalse
(default), the algorithm maximizes the number of stratifications while requiring|nstrat[d1] - nstrat[d2]| <= 1
. This parameter is ignored ifnstrat
is specified explicitly. - mpi (bool) – Setting
mpi=False
disablesmpi
support invegas
even ifmpi
is available; settingmpi=True
(default) allows use ofmpi
provided modulempi4py
is installed. This flag is ignored whenmpi
is not being used (and so has no impact on performance). - minimize_mem – When
True
,vegas
minimizes internal workspace at the cost of extra evaluations of the integrand. This can increase execution time by 50–100% but might be desirable when the number of evaluations is very large (e.g.,neval=1e9
). Normallyvegas
uses internal work space that grows in proportion toneval
. If that work space exceeds the size of the RAM available to the processor,vegas
runs much more slowly. Settingminimize_mem=True
greatly reduces the internal storage used byvegas
; in particular memory becomes independent ofneval
. The default setting (minimize_mem=False
), however, is much superior unless memory becomes a problem. (The large memory is needed for adaptive stratified sampling, so memory is not an issue ifbeta=0
.)
- Methods:
-
__call__
(fcn, save=None, saveall=None, **kargs)¶ Integrate integrand
fcn
.A typical integrand has the form, for example:
def f(x): return x[0] ** 2 + x[1] ** 4
The argument
x[d]
is an integration point, where indexd=0...
represents direction within the integration volume.Integrands can be array-valued, representing multiple integrands: e.g.,
def f(x): return [x[0] ** 2, x[0] / x[1]]
The return arrays can have any shape. Dictionary-valued integrands are also supported: e.g.,
def f(x): return dict(a=x[0] ** 2, b=[x[0] / x[1], x[1] / x[0]])
Integrand functions that return arrays or dictionaries are useful for multiple integrands that are closely related, and can lead to substantial reductions in the errors for ratios or differences of the results.
It is usually much faster to use
vegas
in batch mode, where integration points are presented to the integrand in batches. A simple batch integrand might be, for example:@vegas.batchintegrand def f(x): return x[:, 0] ** 2 + x[:, 1] ** 4
where decorator
@vegas.batchintegrand
tellsvegas
that the integrand processes integration points in batches. The arrayx[i, d]
represents a collection of different integration points labeled byi=0...
. (The number of points is controlledvegas.Integrator
parameternhcube_batch
.) The batch index is always first.Batch integrands can also be constructed from classes derived from
vegas.BatchIntegrand
.Batch mode is particularly useful (and fast) when the class derived from
vegas.BatchIntegrand
is coded in Cython. Then loops over the integration points can be coded explicitly, avoiding the need to usenumpy
’s whole-array operators if they are not well suited to the integrand.Any
vegas
parameter can also be reset: e.g.,self(fcn, nitn=20, neval=1e6)
.Parameters: - fcn (callable) – Integrand function.
- save (str or file or None) –
Writes
results
into pickle file specified bysave
at the end of each iteration. For example, settingsave='results.pkl'
means that the results returned by the last vegas iteration can be reconstructed later using:import pickle with open('results.pkl', 'rb') as ifile: results = pickle.load(ifile)
Ignored if
save=None
(default). - saveall (str or file or None) –
Writes
(results, integrator)
into pickle file specified bysaveall
at the end of each iteration. For example, settingsaveall='allresults.pkl'
means that the results returned by the last vegas iteration, together with a clone of the (adapted) integrator, can be reconstructed later using:import pickle with open('allresults.pkl', 'rb') as ifile: results, integrator = pickle.load(ifile)
Ignored if
saveall=None
(default).
-
set
(ka={}, **kargs)¶ Reset default parameters in integrator.
Usage is analogous to the constructor for
vegas.Integrator
: for example,old_defaults = integ.set(neval=1e6, nitn=20)
resets the default values for
neval
andnitn
invegas.Integrator
integ
. A dictionary, hereold_defaults
, is returned. It can be used to restore the old defaults using, for example:integ.set(old_defaults)
-
settings
(ngrid=0)¶ Assemble summary of integrator settings into string.
Parameters: ngrid (int) – Number of grid nodes in each direction to include in summary. The default is 0. Returns: String containing the settings.
-
random
(yield_hcube=False, yield_y=False)¶ Iterator over integration points and weights.
This method creates an iterator that returns integration points from
vegas
, and their corresponding weights in an integral. Each pointx[d]
is accompanied by the weight assigned to that point byvegas
when estimating an integral. Optionally it will also return the index of the hypercube containing the integration point and/or the y-space coordinates:integ.random() yields x, wgt integ.random(yield_hcube=True) yields x, wgt, hcube integ.random(yield_y=True) yields x, y, wgt integ.random(yield_hcube=True, yield_y=True) yields x, y, wgt, hcube
The number of integration points returned by the iterator corresponds to a single iteration.
-
random_batch
(yield_hcube=False, yield_y=False)¶ Iterator over integration points and weights.
This method creates an iterator that returns integration points from
vegas
, and their corresponding weights in an integral. The points are provided in arraysx[i, d]
wherei=0...
labels the integration points in a batch andd=0...
labels direction. The corresponding weights assigned byvegas
to each point are provided in an arraywgt[i]
.Optionally the integrator will also return the indices of the hypercubes containing the integration points and/or the y-space coordinates of those points:
integ.random_batch() yields x, wgt integ.random_batch(yield_hcube=True) yields x, wgt, hcube integ.random_batch(yield_y=True) yields x, y, wgt integ.random_batch(yield_hcube=True, yield_y=True) yields x, y, wgt, hcube
The number of integration points returned by the iterator corresponds to a single iteration. The number in a batch is controlled by parameter
nhcube_batch
.
-
- map (array,
AdaptiveMap Objects¶
vegas
’s remapping of the integration variables is handled
by a vegas.AdaptiveMap
object, which maps the original
integration variables x into new variables y in a unit hypercube.
Each direction has its own map specified by a grid in x space:
where and
are the limits of integration.
The grid specifies the transformation function at the points
for
:
Linear interpolation is used between those points. The Jacobian for this transformation is:
vegas
adjusts the increments sizes to optimize its Monte Carlo
estimates of the integral. This involves training the grid. To
illustrate how this is done with vegas.AdaptiveMap
s consider a simple
two dimensional integral over a unit hypercube with integrand:
def f(x):
return x[0] * x[1] ** 2
We want to create a grid that optimizes uniform Monte Carlo estimates
of the integral in y space. We do this by sampling the integrand
at a large number ny
of random points y[j, d]
, where j=0...ny-1
and d=0,1
, uniformly distributed throughout the integration
volume in y space. These samples be used to train the grid
using the following code:
import vegas
import numpy as np
def f(x):
return x[0] * x[1] ** 2
m = vegas.AdaptiveMap([[0, 1], [0, 1]], ninc=5)
ny = 1000
y = np.random.uniform(0., 1., (ny, 2)) # 1000 random y's
x = np.empty(y.shape, float) # work space
jac = np.empty(y.shape[0], float)
f2 = np.empty(y.shape[0], float)
print('intial grid:')
print(m.settings())
for itn in range(5): # 5 iterations to adapt
m.map(y, x, jac) # compute x's and jac
for j in range(ny): # compute training data
f2[j] = (jac[j] * f(x[j])) ** 2
m.add_training_data(y, f2) # adapt
m.adapt(alpha=1.5)
print('iteration %d:' % itn)
print(m.settings())
In each of the 5 iterations, the vegas.AdaptiveMap
adjusts the
map, making increments smaller where f2
is larger and
larger where f2
is smaller.
The map converges after only 2 or 3 iterations, as
is clear from the output:
initial grid:
grid[ 0] = [ 0. 0.2 0.4 0.6 0.8 1. ]
grid[ 1] = [ 0. 0.2 0.4 0.6 0.8 1. ]
iteration 0:
grid[ 0] = [ 0. 0.412 0.62 0.76 0.883 1. ]
grid[ 1] = [ 0. 0.506 0.691 0.821 0.91 1. ]
iteration 1:
grid[ 0] = [ 0. 0.428 0.63 0.772 0.893 1. ]
grid[ 1] = [ 0. 0.531 0.713 0.832 0.921 1. ]
iteration 2:
grid[ 0] = [ 0. 0.433 0.63 0.772 0.894 1. ]
grid[ 1] = [ 0. 0.533 0.714 0.831 0.922 1. ]
iteration 3:
grid[ 0] = [ 0. 0.435 0.631 0.772 0.894 1. ]
grid[ 1] = [ 0. 0.533 0.715 0.831 0.923 1. ]
iteration 4:
grid[ 0] = [ 0. 0.436 0.631 0.772 0.895 1. ]
grid[ 1] = [ 0. 0.533 0.715 0.831 0.924 1. ]
The grid increments along direction 0 shrink at larger
values x[0]
, varying as 1/x[0]
. Along direction 1
the increments shrink more quickly varying like 1/x[1]**2
.
vegas
samples the integrand in order to estimate the integral.
It uses those same samples to train its vegas.AdaptiveMap
in this
fashion, for use in subsequent iterations of the algorithm.
-
class
vegas.
AdaptiveMap
¶ Adaptive map
y->x(y)
for multidimensionaly
andx
.An
AdaptiveMap
defines a multidimensional mapy -> x(y)
from the unit hypercube, with0 <= y[d] <= 1
, to an arbitrary hypercube inx
space. Each direction is mapped independently with a Jacobian that is tunable (i.e., “adaptive”).The map is specified by a grid in
x
-space that, by definition, maps into a uniformly spaced grid iny
-space. The nodes of the grid are specified bygrid[d, i]
where d is the direction (d=0,1...dim-1
) andi
labels the grid point (i=0,1...N
). The mapping for a specific pointy
intox
space is:y[d] -> x[d] = grid[d, i(y[d])] + inc[d, i(y[d])] * delta(y[d])
where
i(y)=floor(y*N
),delta(y)=y*N - i(y)
, andinc[d, i] = grid[d, i+1] - grid[d, i]
. The Jacobian for this map,dx[d]/dy[d] = inc[d, i(y[d])] * N,
is piece-wise constant and proportional to the
x
-space grid spacing. Each increment in thex
-space grid maps into an increment of size1/N
in the correspondingy
space. So regions inx
space whereinc[d, i]
is small are stretched out iny
space, while larger increments are compressed.The
x
grid for anAdaptiveMap
can be specified explicitly when the map is created: for example,m = AdaptiveMap([[0, 0.1, 1], [-1, 0, 1]])
creates a two-dimensional map where the
x[0]
interval(0,0.1)
and(0.1,1)
map into they[0]
intervals(0,0.5)
and(0.5,1)
respectively, whilex[1]
intervals(-1,0)
and(0,1)
map intoy[1]
intervals(0,0.5)
and(0.5,1)
.More typically, an uniform map with
ninc
increments is first created: for example,m = AdaptiveMap([[0, 1], [-1, 1]], ninc=1000)
creates a two-dimensional grid, with 1000 increments in each direction, that spans the volume
0<=x[0]<=1
,-1<=x[1]<=1
. This map is then trained with dataf[j]
corresponding tony
pointsy[j, d]
, withj=0...ny-1
, (usually) uniformly distributed in y space: for example,m.add_training_data(y, f) m.adapt(alpha=1.5)
m.adapt(alpha=1.5)
shrinks grid increments wheref[j]
is large, and expands them wheref[j]
is small. Usually one has to iterate over several sets ofy
s andf
s before the grid has fully adapted.The speed with which the grid adapts is determined by parameter
alpha
. Large (positive) values imply rapid adaptation, while small values (much less than one) imply slow adaptation. As in any iterative process that involves random numbers, it is usually a good idea to slow adaptation down in order to avoid instabilities caused by random fluctuations.Parameters: - grid (list of arrays) – Initial
x
grid, wheregrid[d][i]
is thei
-th node in directiond
. Different directions can have different numbers of nodes. - ninc (int or array or
None
) –ninc[d]
(orninc
, if it is a number) is the number of increments along directiond
in the newx
grid. The new grid is designed to give the same Jacobiandx(y)/dy
as the original grid. The default value,ninc=None
, leaves the grid unchanged.
- Attributes and methods:
-
dim
¶ Number of dimensions.
-
ninc
¶ ninc[d]
is the number increments in directiond
.
-
grid
¶ The nodes of the grid defining the maps are
self.grid[d, i]
whered=0...
specifies the direction andi=0...self.ninc
the node.
-
inc
¶ The increment widths of the grid:
self.inc[d, i] = self.grid[d, i + 1] - self.grid[d, i]
-
adapt
(alpha=0.0, ninc=None)¶ Adapt grid to accumulated training data.
self.adapt(...)
projects the training data onto each axis independently and maps it intox
space. It shrinksx
-grid increments in regions where the projected training data is large, and grows increments where the projected data is small. The grid along any direction is unchanged if the training data is constant along that direction.The number of increments along a direction can be changed by setting parameter
ninc
(array or number).The grid does not change if no training data has been accumulated, unless
ninc
is specified, in which case the number of increments is adjusted while preserving the relative density of increments at different values ofx
.Parameters: - alpha (double) – Determines the speed with which the grid
adapts to training data. Large (postive) values imply
rapid evolution; small values (much less than one) imply
slow evolution. Typical values are of order one. Choosing
alpha<0
causes adaptation to the unmodified training data (usually not a good idea). - ninc (int or array or None) – The number of increments in the new
grid is
ninc[d]
(orninc
, if it is a number) in directiond
. The number is unchanged from the old grid ifninc
is omitted (or equalsNone
, which is the default).
- alpha (double) – Determines the speed with which the grid
adapts to training data. Large (postive) values imply
rapid evolution; small values (much less than one) imply
slow evolution. Typical values are of order one. Choosing
-
add_training_data
(y, f, ny=-1)¶ Add training data
f
fory
-space pointsy
.Accumulates training data for later use by
self.adapt()
. Grid increments will be made smaller in regions wheref
is larger than average, and larger wheref
is smaller than average. The grid is unchanged (converged?) whenf
is constant across the grid.Parameters: - y (array) –
y
values corresponding to the training data.y
is a contiguous 2-d array, wherey[j, d]
is for points along directiond
. - f (array) – Training function values.
f[j]
corresponds to pointy[j, d]
iny
-space. - ny (int) – Number of
y
points:y[j, d]
ford=0...dim-1
andj=0...ny-1
.ny
is set toy.shape[0]
if it is omitted (or negative).
- y (array) –
-
adapt_to_samples
(x, f, nitn=5, alpha=1.0, nproc=1)¶ Adapt map to data
{x, f(x)}
.Replace grid with one that is optimized for integrating function
f(x)
. New grid is found iterativelyParameters: - x (array) –
x[:, d]
are the components of the sample points in directiond=0,1...self.dim-1
. - f (callable or array) – Function
f(x)
to be adapted to. Iff
is an array, it is assumes to contain valuesf[i]
corresponding to the function evaluated at pointsx[i]
. - nitn (int) – Number of iterations to use in adaptation. Default
is
nitn=5
. - alpha (float) – Damping parameter for adaptation. Default
is
alpha=1.0
. Smaller values slow the iterative adaptation, to improve stability of convergence. - nproc (int or None) –
Number of processes/processors to use. If
nproc>1
Python’smultiprocessing
module is used to spread the calculation across multiple processors. There is a significant overhead involved in using multiple processors so this option is useful mainly when very high dimenions or large numbers of samples are involved. When using themultiprocessing
module in its default mode for MacOS and Windows, it is important that the main module can be safely imported (i.e., without launching new processes). This can be accomplished with some version of theif __name__ == '__main__`:
construct in the main module: e.g.,if __name__ == '__main__': main()
This is not an issue on other Unix platforms. See the
multiprocessing
documentation for more information. Setnproc=None
to use all the processors on the machine (equivalent tonproc=os.cpu_count()
). Default value isnproc=1
. (Requires Python 3.3 or later.)
- x (array) –
-
__call__
(y)¶ Return
x
values corresponding toy
.y
can be a singledim
-dimensional point, or it can be an arrayy[i,j, ..., d]
of such points (d=0..dim-1
).If
y=None
(default),y
is set equal to a (uniform) random point in the volume.
-
jac
(y)¶ Return the map’s Jacobian at
y
.y
can be a singledim
-dimensional point, or it can be an arrayy[i,j,...,d]
of such points (d=0..dim-1
). Returns an arrayjac
wherejac[i,j,...]
is the (multidimensional) Jacobian (dx/dy
) corresponding toy[i,j,...]
.
-
jac1d
(y)¶ Return the map’s Jacobian at
y
for each direction.y
can be a singledim
-dimensional point, or it can be an arrayy[i,j,...,d]
of such points (d=0..dim-1
). Returns an arrayjac
wherejac[i,j,...,d]
is the (one-dimensional) Jacobian (dx[d]/dy[d]
) corresponding toy[i,j,...,d]
.
-
make_uniform
(ninc=None)¶ Replace the grid with a uniform grid.
The new grid has
ninc[d]
(orninc
, if it is a number) increments along each direction ifninc
is specified. Ifninc=None
(default), the new grid has the same number of increments in each direction as the old grid.
-
map
(y, x, jac, ny=-1)¶ Map y to x, where jac is the Jacobian (
dx/dy
).y[j, d]
is an array ofny
y
-values for directiond
.x[j, d]
is filled with the correspondingx
values, andjac[j]
is filled with the corresponding Jacobian values.x
andjac
must be preallocated: for example,x = numpy.empty(y.shape, float) jac = numpy.empty(y.shape[0], float)
Parameters: - y (array) –
y
values to be mapped.y
is a contiguous 2-d array, wherey[j, d]
contains values for points along directiond
. - x (array) – Container for
x[j, d]
values corresponding toy[j, d]
. Must be a contiguous 2-d array. - jac (array) – Container for Jacobian values
jac[j]
(= dx/dy
) corresponding toy[j, d]
. Must be a contiguous 1-d array. - ny (int) – Number of
y
points:y[j, d]
ford=0...dim-1
andj=0...ny-1
.ny
is set toy.shape[0]
if it is omitted (or negative).
- y (array) –
-
invmap
(x, y, jac, nx=-1)¶ Map x to y, where jac is the Jacobian (
dx/dy
).y[j, d]
is an array ofny
y
-values for directiond
.x[j, d]
is filled with the correspondingx
values, andjac[j]
is filled with the corresponding Jacobian values.x
andjac
must be preallocated: for example,x = numpy.empty(y.shape, float) jac = numpy.empty(y.shape[0], float)
Parameters: - x (array) –
x
values to be mapped toy
-space.x
is a contiguous 2-d array, wherex[j, d]
contains values for points along directiond
. - y (array) – Container for
y[j, d]
values corresponding tox[j, d]
. Must be a contiguous 2-d array - jac (array) – Container for Jacobian values
jac[j]
(= dx/dy
) corresponding toy[j, d]
. Must be a contiguous 1-d array - nx (int) – Number of
x
points:x[j, d]
ford=0...dim-1
andj=0...nx-1
.nx
is set tox.shape[0]
if it is omitted (or negative).
- x (array) –
-
show_grid
(ngrid=40, shrink=False)¶ Display plots showing the current grid.
Parameters: - ngrid (int) – The number of grid nodes in each direction to include in the plot. The default is 40.
- axes – List of pairs of directions to use in
different views of the grid. Using
None
in place of a direction plots the grid for only one direction. Omittingaxes
causes a default set of pairings to be used. - shrink – Display entire range of each axis
if
False
; otherwise shrink range to include just the nodes being displayed. The default isFalse
. - plotter –
matplotlib
plotter to use for plots; plots are not displayed if set. Ignored ifNone
, and plots are displayed usingmatplotlib.pyplot
.
-
settings
(ngrid=5)¶ Create string with information about grid nodes.
Creates a string containing the locations of the nodes in the map grid for each direction. Parameter
ngrid
specifies the maximum number of nodes to print (spread evenly over the grid).
-
extract_grid
()¶ Return a list of lists specifying the map’s grid.
-
clear
()¶ Clear information accumulated by
AdaptiveMap.add_training_data()
.
-
- grid (list of arrays) – Initial
PDFIntegrator Objects¶
Expectation values using probability density functions defined by
collections of Gaussian random variables (see gvar
)
can be evaluated using the following
specialized integrator:
-
class
vegas.
PDFIntegrator
(g, limit=1e15, scale=1., svdcut=1e-15)¶ vegas
integrator for PDF expectation values.Given a multi-dimensional Gaussian distribtuion
g
(a collection ofgvar.GVar
s),PDFIntegrator
creates avegas
integrator that evaluates expectation values of arbitrary functionsf(p)
wherep
is a point in the parameter space of the distribution. Typical usage is illustrated by:import vegas import gvar as gv import numpy as np g = gv.BufferDict() g['a'] = gv.gvar([10., 2.], [[1, 1.4], [1.4, 2]]) g['b'] = gv.BufferDict.uniform('fb', 2.9, 3.1) expval = vegas.PDFIntegrator(g) def f(p): a = p['a'] b = p['b'] return a[0] + np.fabs(a[1]) ** b result = expval(f, neval=1000, nitn=5) print('<f(p)> =', result)
Here dictionary
g
specifies a three-dimensional distribution where the first two variablesg['a'][0]
andg['a'][1]
are Gaussian with means 10 and 2, respectively, and covariance matrix [[1, 1.4], [1.4, 2.]]. The last variableg['b']
is uniformly distributed on interval [2.9, 3.1]. The result is:<f(p)> = 30.233(14)
.PDFIntegrator
evaluates integrals of bothf(p) * pdf(p)
andpdf(p)
, wherepdf(p)
is the probability density function (PDF) associated with distributiong
. The expectation value off(p)
is the ratio of these two integrals (sopdf(p)
need not be normalized). Integration variables are chosen to optimize the integral, and the integrator is pre-adapted topdf(p)
(so it is often unnecessary to discard early iterations). The result of aPDFIntegrator
integration has an extra attribute,result.pdfnorm
, which is thevegas
estimate of the integral over the PDF.The default (Gaussian) PDF associated with
g
can be replaced by an arbitrary PDF functionpdf(p)
, allowing PDFIntegrator to be used for non-Gaussian distributions. In such cases, Gaussian distributiong
does not affect the values of the integrals; it is used only to optimize the integration variables, and pre-adapt the integrator (and so affects the accuracy of the results). Ideallyg
is an approximation to the real distribution — for example, it could be the prior in a Bayesian analysis, or the result of a least-squares (or other peak-finding) analysis.Parameters: - g –
gvar.GVar
, array ofgvar.GVar
s, or dictionary whose values aregvar.GVar
s or arrays ofgvar.GVar
s that specifies the multi-dimensional Gaussian distribution used to construct the probability density function. The integration variables are optimized for this function. - pdf – If specified,
pdf(p)
is used as the probability density function rather than the Gaussian PDF associated withg
. The Gaussian PDF is used ifpdf=None
(default). Note that PDFs need not be normalized. - adapt_to_pdf (bool) –
vegas
adapts to the PDF whenadapt_to_pdf=True
(default).vegas
adapts topdf(p) * f(p)
when calculating the expectation value off(p)
ifadapt_to_pdf=False
. - limit (positive float) – Limits the integrations to a finite
region of size
limit
times the standard deviation on either side of the mean. This can be useful if the functions being integrated misbehave for large parameter values (e.g.,numpy.exp
overflows for a large range of arguments). Default islimit=100
. - scale (positive float) – The integration variables are
rescaled to emphasize parameter values of order
scale
times the standard deviation. The rescaling does not change the value of the integral but it can reduce uncertainties in thevegas
estimate. Default isscale=1.0
. - svdcut (non-negative float or None) – If not
None
, replace correlation matrix ofg
with a new matrix whose small eigenvalues are modified: eigenvalues smaller thansvdcut
times the maximum eigenvalueeig_max
are replaced bysvdcut*eig_max
. This can ameliorate problems caused by roundoff errors when inverting the covariance matrix. It increases the uncertainty associated with the modified eigenvalues and so is conservative. Settingsvdcut=None
orsvdcut=0
leaves the covariance matrix unchanged. Default issvdcut=1e-15
.
All other keyword parameters are passed on to the the underlying
vegas.Integrator
; theuses_jac
keyword is ignored.- Methods:
-
__call__
(f, f=None, pdf=None, adapt_to_pdf=None, save=None, saveall=None, **kargs)¶ Estimate expectation value of function
f(p)
.Uses module
vegas
to estimate the integral off(p)
multiplied by the probability density function associated withg
(i.e.,pdf(p)
). At the same time it integrates the PDF. The ratio of the two integrals is the expectation value.Parameters: - f (function) – Function
f(p)
to integrate. Integral is the expectation value of the function with respect to the distribution. The function can return a number, an array of numbers, or a dictionary whose values are numbers or arrays of numbers. Settingf=None
means that only the PDF is integrated. Integrals can be substantially faster iff(p)
(andpdf(p)
if set) are batch functions (seevegas
documentation). - pdf – If specified,
pdf(p)
is used as the probability density function rather than the Gaussian PDF associated withg
. The Gaussian PDF is used ifpdf=None
(default). Note that PDFs need not be normalized. - adapt_to_pdf (bool) –
vegas
adapts to the PDF whenadapt_to_pdf=True
(default).vegas
adapts topdf(p) * f(p)
ifadapt_to_pdf=False
. - save (str or file or None) –
Writes
results
into pickle file specified bysave
at the end of each iteration. For example, settingsave='results.pkl'
means that the results returned by the last vegas iteration can be reconstructed later using:import pickle with open('results.pkl', 'rb') as ifile: results = pickle.load(ifile)
Ignored if
save=None
(default). - saveall (str or file or None) –
Writes
(results, integrator)
into pickle file specified bysaveall
at the end of each iteration. For example, settingsaveall='allresults.pkl'
means that the results returned by the last vegas iteration, together with a clone of the (adapted) integrator, can be reconstructed later using:import pickle with open('allresults.pkl', 'rb') as ifile: results, integrator = pickle.load(ifile)
Ignored if
saveall=None
(default).
All other keyword arguments are passed on to a
vegas
integrator; see thevegas
documentation for further information.- f (function) – Function
-
- g –
Other Objects and Functions¶
-
class
vegas.
RAvg
¶ Running average of scalar-valued Monte Carlo estimates.
This class accumulates independent Monte Carlo estimates (e.g., of an integral) and combines them into a single average. It is derived from
gvar.GVar
(from thegvar
module if it is present) and represents a Gaussian random variable.Different estimates are weighted by their inverse variances if parameter
weight=True
; otherwise straight, unweighted averages are used.- Attributes and methods:
-
mean
¶ The mean value of the weighted average.
-
sdev
¶ The standard deviation of the weighted average.
-
chi2
¶ chi**2 of weighted average.
-
dof
¶ Number of degrees of freedom in weighted average.
-
Q
¶ Q or p-value of weighted average’s chi**2.
-
itn_results
¶ A list of the results from each iteration.
-
sum_neval
¶ Total number of integrand evaluations used in all iterations.
-
add
(g)¶ Add estimate
g
to the running average.
-
-
class
vegas.
RAvgArray
¶ Running average of array-valued Monte Carlo estimates.
This class accumulates independent arrays of Monte Carlo estimates (e.g., of an integral) and combines them into an array of averages. It is derived from
numpy.ndarray
. The array elements aregvar.GVar
s (from thegvar
module if present) and represent Gaussian random variables.Different estimates are weighted by their inverse covariance matrices if parameter
weight=True
; otherwise straight, unweighted averages are used.- Attributes and methods:
-
chi2
¶ chi**2 of weighted average.
-
dof
¶ Number of degrees of freedom in weighted average.
-
Q
¶ Q or p-value of weighted average’s chi**2.
-
itn_results
¶ A list of the results from each iteration.
-
sum_neval
¶ Total number of integrand evaluations used in all iterations.
-
add
(g)¶ Add estimate
g
to the running average.
-
summary
(extended=False, weighted=None)¶ Assemble summary of results, iteration-by-iteration, into a string.
Parameters: - extended (bool) – Include a table of final averages for every
component of the integrand if
True
. Default isFalse
. - weighted (bool) – Display weighted averages of results from different
iterations if
True
; otherwise show unweighted averages. Default behavior is determined byvegas
.
- extended (bool) – Include a table of final averages for every
component of the integrand if
-
-
class
vegas.
RAvgDict
¶ Running average of dictionary-valued Monte Carlo estimates.
This class accumulates independent dictionaries of Monte Carlo estimates (e.g., of an integral) and combines them into a dictionary of averages. It is derived from
gvar.BufferDict
. The dictionary values aregvar.GVar
s or arrays ofgvar.GVar
s.Different estimates are weighted by their inverse covariance matrices if parameter
weight=True
; otherwise straight, unweighted averages are used.- Attributes and methods:
-
chi2
¶ chi**2 of weighted average.
-
dof
¶ Number of degrees of freedom in weighted average.
-
Q
¶ Q or p-value of weighted average’s chi**2.
-
itn_results
¶ A list of the results from each iteration.
-
sum_neval
¶ Total number of integrand evaluations used in all iterations.
-
add
(g)¶
-
summary
(extended=False, weighted=None)¶ Assemble summary of results, iteration-by-iteration, into a string.
Parameters: - extended (bool) – Include a table of final averages for every
component of the integrand if
True
. Default isFalse
. - weighted (bool) – Display weighted averages of results from different
iterations if
True
; otherwise show unweighted averages. Default behavior is determined byvegas
.
- extended (bool) – Include a table of final averages for every
component of the integrand if
-
-
vegas.
batchintegrand
()¶ Decorator for batch integrand functions.
Applying
vegas.batchintegrand()
to a functionfcn
repackages the function in a format thatvegas
can understand. Appropriate functions take anumpy
array of integration pointsx[i, d]
as an argument, wherei=0...
labels the integration point andd=0...
labels direction, and return an arrayf[i]
of integrand values (or arrays of integrand values) for the corresponding points. The meaning offcn(x)
is unchanged by the decorator.An example is
import vegas import numpy as np @vegas.batchintegrand # or @vegas.lbatchintegrand def f(x): return np.exp(-x[:, 0] - x[:, 1])
for the two-dimensional integrand
.
vegas.lbatchintegrand()
is the same asvegas.batchintegrand()
.
-
class
vegas.
BatchIntegrand
¶ Wrapper for l-batch integrands.
Used by
vegas.batchintegrand()
.vegas.LBatchIntegrand
is the same asvegas.BatchIntegrand
.
-
vegas.
rbatchintegrand
()¶ Same as
vegas.batchintegrand()
but with batch indices on the right (not left).
-
class
vegas.
RBatchIntegrand
¶ Same as
vegas.BatchIntegrand
but with batch indices on the right (not left).
-
vegas.
lbatchintegrand
()¶ Decorator for batch integrand functions.
Applying
vegas.batchintegrand()
to a functionfcn
repackages the function in a format thatvegas
can understand. Appropriate functions take anumpy
array of integration pointsx[i, d]
as an argument, wherei=0...
labels the integration point andd=0...
labels direction, and return an arrayf[i]
of integrand values (or arrays of integrand values) for the corresponding points. The meaning offcn(x)
is unchanged by the decorator.An example is
import vegas import numpy as np @vegas.batchintegrand # or @vegas.lbatchintegrand def f(x): return np.exp(-x[:, 0] - x[:, 1])
for the two-dimensional integrand
.
vegas.lbatchintegrand()
is the same asvegas.batchintegrand()
.
-
vegas.
LBatchIntegrand
¶ alias of
vegas._vegas.BatchIntegrand
-
vegas.
ravg
(reslist, weighted=None, rescale=None)¶ Create running average from list of
vegas
results.This function is used to change how the weighted average of
vegas
results is calculated. For example, the following code discards the first five results (wherevegas
is still adapting) and does an unweighted average of the last five:import vegas def fcn(p): return p[0] * p[1] * p[2] * p[3] * 16. itg = vegas.Integrator(4 * [[0,1]]) r = itg(fcn) print(r.summary()) ur = vegas.ravg(r.itn_results[5:], weighted=False) print(ur.summary())
The unweighted average can be useful because it is unbiased. The output is:
itn integral wgt average chi2/dof Q ------------------------------------------------------- 1 1.013(19) 1.013(19) 0.00 1.00 2 0.997(14) 1.002(11) 0.45 0.50 3 1.021(12) 1.0112(80) 0.91 0.40 4 0.9785(97) 0.9980(62) 2.84 0.04 5 1.0067(85) 1.0010(50) 2.30 0.06 6 0.9996(75) 1.0006(42) 1.85 0.10 7 1.0020(61) 1.0010(34) 1.54 0.16 8 1.0051(52) 1.0022(29) 1.39 0.21 9 1.0046(47) 1.0029(24) 1.23 0.27 10 0.9976(47) 1.0018(22) 1.21 0.28 itn integral average chi2/dof Q ------------------------------------------------------- 1 0.9996(75) 0.9996(75) 0.00 1.00 2 1.0020(61) 1.0008(48) 0.06 0.81 3 1.0051(52) 1.0022(37) 0.19 0.83 4 1.0046(47) 1.0028(30) 0.18 0.91 5 0.9976(47) 1.0018(26) 0.31 0.87
Parameters: - reslist (list) – List whose elements are
gvar.GVar
s, arrays ofgvar.GVar
s, or dictionaries whose values aregvar.GVar
s or arrays ofgvar.GVar
s. Alternativelyreslist
can be the object returned by a call to avegas.Integrator
object (i.e, an instance of any ofvegas.RAvg
,vegas.RAvgArray
,vegas.RAvgArray
,vegas.PDFRAvg
,vegas.PDFRAvgArray
,vegas.PDFRAvgArray
). - weighted (bool) – Running average is weighted (by the inverse
covariance matrix) if
True
. Otherwise the average is unweighted, which makes most sense ifreslist
items were generated byvegas
with little or no adaptation (e.g., withadapt=False
). Ifweighted
is not specified (or isNone
), it is set equal togetattr(reslist, 'weighted', True)
. - rescale – Integration results are divided by
rescale
before taking the weighted average ifweighted=True
; otherwiserescale
is ignored. Settingrescale=True
is equivalent to settingrescale=reslist[-1]
. Ifrescale
is not specified (or isNone
), it is set equal togetattr(reslist, 'rescale', True)
.
- reslist (list) – List whose elements are