API Reference

Inference API

Inference API

UQPyL.inference

The inference module runs MCMC-style parameter inference on scalar-objective Problem instances.

Import

python
from UQPyL.inference import MH, AMH, MH_Gibbs, DEMC, DREAM_ZS
from UQPyL.inference import InfReader, InfResult

Public Objects

ObjectRole
MHRandom-walk Metropolis-Hastings sampler.
AMHAdaptive Metropolis-Hastings sampler.
MH_GibbsMetropolis-Hastings within Gibbs sampler.
DEMCDifferential evolution MCMC sampler.
DREAM_ZSDREAM(ZS) sampler.
InfResultStandard result object returned by inference runs.
InfReaderReader for sqlite results saved with saveFlag=True.

Inference Workflow

All inference methods use:

python
result = method.run(problem, gamma=0.1, seed=None)

problem must provide a scalar objective. Inference currently rejects problems where problem.nOutput != 1.

Shared constructor controls:

ParameterMeaning
nChainsNumber of parallel chains.
warmUpNumber of warm-up iterations before formal sampling.
maxIters / maxIterTimesNumber of formal sampling draws, including the initial draw.
verboseFlagPrint compact runtime summaries.
verboseFreqIteration interval for terminal and log summaries.
logFlagWrite a text log when enabled.
saveFlagPersist sqlite snapshots and final result when enabled.
saveFreqSnapshot save frequency.
logProbFuncOptional custom log-probability function.
maxInitAttemptsMaximum LHS batches used to find feasible initial chains.

Default log-probability convention:

text
log_prob = -oriented_objective

For minimization problems, this means lower objective values have higher default log probability. Provide logProbFunc(y, decs=None, cons=None) to override this convention.

Example:

python
from UQPyL.inference import MH
from UQPyL.problem import Sphere


problem = Sphere(nInput=2)
method = MH(
    nChains=3,
    warmUp=5,
    maxIters=30,
    verboseFlag=False,
    logFlag=False,
    saveFlag=False,
)

result = method.run(problem, gamma=0.2, seed=123)
print(result.decs.shape)
print(result.acceptanceRate)

InfResult

InfResult is returned by run().

FieldTypeMeaning
runIdstr or NoneSaved run id when saveFlag=True.
methodstrInference method name.
problemNamestrProblem name.
nInputintNumber of input variables.
nOutputintNumber of outputs.
nConintNumber of constraints.
settingsdictMethod settings.
runtimefloatRuntime in seconds.
createdAtstrCreation timestamp.
decsnp.ndarrayDecision draws with shape (n_chains, draws, n_input).
objsnp.ndarrayObjective draws with shape (n_chains, draws, n_output).
consnp.ndarray or NoneConstraint draws with shape (n_chains, draws, n_con).
logProbnp.ndarrayLog-probability values with shape (n_chains, draws).
acceptednp.ndarrayBoolean acceptance mask with shape (n_chains, draws).
feasibleMasknp.ndarrayBoolean feasibility mask with shape (n_chains, draws).
acceptanceRatenp.ndarrayAcceptance rate per chain.
bestDecsnp.ndarray or NoneBest decision found.
bestObjsnp.ndarray or NoneBest objective in original optimization direction.
bestConsnp.ndarray or NoneConstraint values for bestDecs.
bestFeasibleboolWhether the best sample is feasible.
FEsintNumber of function evaluations.
itersintFinal iteration count.
historyInfHistoryRuntime history.
diagnosticsdictMethod diagnostics.
extradictExtra method-specific payload.

Methods:

APIReturnsMeaning
summary()dictCompact runtime summary.
toDict()dictFull result dictionary.

InfHistory

InfHistory stores runtime progress.

FieldMeaning
snapshotsRuntime snapshot summaries.
iterToFEsIteration to function-evaluation mapping.
meanLogProbHistoryMean log-probability history.
acceptanceRateHistoryMean acceptance-rate history.
feasibleRateHistoryFeasible-rate history.
bestObjHistoryBest objective history.

MH

Random-walk Metropolis-Hastings sampler.

python
MH(
    nChains=1,
    warmUp=1000,
    propDist="gauss",
    maxIters=10000,
    verboseFlag=True,
    verboseFreq=10,
    logFlag=False,
    saveFlag=True,
    saveFreq=100,
    logProbFunc=None,
    maxInitAttempts=1000,
)
ParameterMeaning
propDistProposal distribution. One of "gauss" or "uniform".
gammaProposal scale passed to run(). Can be scalar, list, or array.

AMH

Adaptive Metropolis-Hastings sampler.

python
AMH(
    nChains=1,
    warmUp=1000,
    maxIterTimes=1000,
    propDist="gauss",
    verboseFlag=True,
    verboseFreq=10,
    logFlag=False,
    saveFlag=True,
    saveFreq=100,
    logProbFunc=None,
    maxInitAttempts=1000,
)
ParameterMeaning
maxIterTimesNumber of formal sampling draws.
propDistProposal distribution. One of "gauss" or "uniform".
gammaInitial proposal scale passed to run().

MH_Gibbs

Metropolis-Hastings within Gibbs sampler.

python
MH_Gibbs(
    nChains=1,
    warmUp=1000,
    maxIters=1000,
    propDist="gauss",
    verboseFlag=True,
    verboseFreq=10,
    logFlag=False,
    saveFlag=True,
    saveFreq=100,
    logProbFunc=None,
    maxInitAttempts=1000,
)
ParameterMeaning
propDistProposal distribution. One of "gauss" or "uniform".
gammaPer-variable proposal scale passed to run().

DEMC

Differential evolution MCMC sampler.

python
DEMC(
    nChains=1,
    warmUp=1000,
    maxIterTimes=1000,
    verboseFlag=True,
    verboseFreq=10,
    logFlag=False,
    saveFlag=True,
    saveFreq=100,
    logProbFunc=None,
    maxInitAttempts=1000,
)
ParameterMeaning
gammaOptional differential-evolution scale passed to run(). If omitted, the method uses its internal default.

DREAM_ZS

DREAM(ZS) sampler with snooker updates and adaptive crossover weights.

python
DREAM_ZS(
    nChains=10,
    warmUp=1000,
    ps=0.1,
    k=1,
    jitter=0.1,
    adpInterval=50,
    archSize=10,
    acTarget=0.25,
    nCR=5,
    maxIters=1000,
    verboseFlag=True,
    verboseFreq=10,
    logFlag=False,
    saveFlag=True,
    saveFreq=100,
    logProbFunc=None,
    maxInitAttempts=1000,
)
ParameterMeaning
psProbability of snooker update.
kNumber of differential evolution pairs.
jitterMultiplicative proposal jitter scale.
adpIntervalInterval for adaptive crossover updates.
archSizeArchive size multiplier relative to chain count.
acTargetTarget acceptance rate for gamma scaling.
nCRNumber of crossover rate candidates.
gammaOptional scale passed to run(). If omitted, the method uses 2.38 / sqrt(2 * nInput).

Chain

Chain is the fixed-length storage container used internally for one inference chain.

python
Chain(nInput, nOutput, nCons, length)
FieldMeaning
decsDecision draws.
objsObjective draws.
consConstraint draws, or None for unconstrained problems.
logProbLog-probability values.
acceptedAcceptance mask.
countNumber of stored draws.

Method:

MethodMeaning
add(decs, objs, cons=None, logProb=None, accepted=True)Append one draw to the chain.

InfReader

Use InfReader to read sqlite results saved with saveFlag=True.

python
from UQPyL.inference import InfReader


with InfReader("Result/mh_Sphere_20260509_1200_0000.sqlite3") as reader:
    result = reader.load_result()
    print(result.acceptanceRate)
MethodReturnsMeaning
InfReader.list_runs(result_dir)table-like dataList saved inference runs in a result directory.
get_run()dict or NoneReturn raw run metadata.
get_run_params()dictReturn stored method parameters.
get_run_summary()dictReturn compact run summary.
load_problem()problem objectLoad the saved problem payload.
list_snapshots()list[dict]List saved snapshots.
load_snapshot_members(snapshotId)list[dict]Load chain members for a snapshot.
load_last_snapshot_members()list[dict]Load chain members from the latest snapshot.
load_result()InfResultLoad the saved final result artifact.
close()NoneClose the sqlite connection.