mcquad¶
-
xitorch.integrate.
mcquad
(ffcn: Union[Callable[[…], torch.Tensor], Callable[[…], Sequence[torch.Tensor]]], log_pfcn: Callable[[…], torch.Tensor], x0: torch.Tensor, fparams: Sequence[Any] = [], pparams: Sequence[Any] = [], bck_options: Mapping[str, Any] = {}, method: Optional[Union[str, Callable]] = None, **fwd_options) → Union[torch.Tensor, Sequence[torch.Tensor]][source]¶ Performing monte carlo quadrature to calculate the expectation value:
\[\mathbb{E}_p[f] = \frac{\int f(\mathbf{x},\theta_f) p(\mathbf{x},\theta_p) \ \mathrm{d}\mathbf{x} }{ \int p(\mathbf{x},\theta_p)\ \mathrm{d}\mathbf{x} }\]- Parameters
ffcn (Callable) – The function with to be integrated. Its outputs is a tensor or a list of tensors. To call the function:
ffcn(x, *fparams)
log_pfcn (Callable) – The natural logarithm of the probability function. The output should be a one-element tensor. To call the function:
log_pfcn(x, *pparams)
x0 (torch.Tensor) – Tensor with any size as the initial position. The call
ffcn(x0,*fparams)
must work.fparams (list) – Sequence of any other parameters for
ffcn
.pparams (list) – Sequence of any other parameters for
gfcn
.bck_options (dict) – Options for the backward mcquad operation. Unspecified fields will be taken from
fwd_options
.method (str or callable or None) – Monte Carlo quadrature method. If None, it will choose
"mh"
.**fwd_options (dict) – Method-specific options (see method section below).
- Returns
The expectation values of the function
ffcn
over the space ofx
. If the output offfcn
is a list, then this is also a list.- Return type
torch.Tensor or a list of torch.Tensor
-
method="mh"
mcquad(..., method="mh", *, nsamples=10000, nburnout=5000, step_size=1.0)
Perform Metropolis-Hasting steps to collect samples
- Keyword Arguments
nsamples (int) – The number of samples to be collected
nburnout (int) – The number of initial steps to be performed before collecting samples
step_size (float) – The size of the steps to be taken
-
method="mhcustom"
mcquad(..., method="mhcustom", *, nsamples=10000, nburnout=5000, custom_step=None)
Perform Metropolis sampling using custom_step
- Keyword Arguments
nsamples (int) – The number of samples to be collected
nburnout (int) – The number of initial steps to be performed before collecting samples
custom_step (callable or None) – Callable with call signature
custom_step(x, *pparams)
to produce the next samples (already decided whether to accept or not). This argument is required. IfNone
, it will raise an error