stsb3.coreAddBlockRepresents the result of adding two blocks toogether.
.model(...) is computed as x ~ p(x); y ~ p(y); x + y
Args:
left (Block)right (Block)_maybe_add_blocksdef _maybe_add_blocks(self, *args):
Adds parameters to prec and succ if they subclass Block.
Args:
args: iterable of (name, parameter, bound)_maybe_remove_blocksNone
_modelNone
_transformDefines a transform from a string argument.
Currently the following string arguments are supported:
The resulting transform will be added to the transform stack iff it is not already at the top of the stack.
Args:
arg (str): one of the above strings corresponding to functionReturns:
self (stsb.Block)
arctanhx -> arctanh(x), i.e. x -> 0.5 log ((1 + x) / (1 - x))
clear_cacheClears the block cache.
This method does not alter the cache mode.
cosx -> cos x
diffx -> x[1:] - x[:-1]
Note that this lowers the time dimension from T to T - 1.
expx -> exp(x)
floorx -> x - [[x]], where [[.]] is the fractional part operator
invlogitx -> 1 / (1 + exp(-x))
logx -> log x
Block paths must be positive for valid output.
logdiffx -> log x[1:] - log x[:-1]
Note that this lowers the time dimension from T to T - 1.
logitx -> log(x / (1 - x))
modeldef model(self, *args, **kwargs):
Draws a batch of samples from the block.
Args:
args: optional positional argumentskwargs: optional keyword argumentsReturns:
draws (torch.tensor) sampled values from the blockprecReturns the predecessor nodes of self in the (implicit) compute graph
Returns:
_prec (list): list of predecessor nodes
sinx -> sin x
softplusx -> log(1 + exp(x))
succReturns the successor nodes of self in the (implicit) compute graph
Returns:
_succ (list): list of successor nodes
tanhx -> tanh(x), i.e. x -> (exp(x) - exp(-x)) / (exp(x) + exp(-x))
BlockBase class that all STS blocks should subclass.
Defines a number of useful modeling constructs and methods, such as deterministic transformations.
_maybe_add_blocksdef _maybe_add_blocks(self, *args):
Adds parameters to prec and succ if they subclass Block.
Args:
args: iterable of (name, parameter, bound)_maybe_remove_blocksNone
_modelNone
_transformDefines a transform from a string argument.
Currently the following string arguments are supported:
The resulting transform will be added to the transform stack iff it is not already at the top of the stack.
Args:
arg (str): one of the above strings corresponding to functionReturns:
self (stsb.Block)
arctanhx -> arctanh(x), i.e. x -> 0.5 log ((1 + x) / (1 - x))
clear_cacheClears the block cache.
This method does not alter the cache mode.
cosx -> cos x
diffx -> x[1:] - x[:-1]
Note that this lowers the time dimension from T to T - 1.
expx -> exp(x)
floorx -> x - [[x]], where [[.]] is the fractional part operator
invlogitx -> 1 / (1 + exp(-x))
logx -> log x
Block paths must be positive for valid output.
logdiffx -> log x[1:] - log x[:-1]
Note that this lowers the time dimension from T to T - 1.
logitx -> log(x / (1 - x))
modeldef model(self, *args, **kwargs):
Draws a batch of samples from the block.
Args:
args: optional positional argumentskwargs: optional keyword argumentsReturns:
draws (torch.tensor) sampled values from the blockprecReturns the predecessor nodes of self in the (implicit) compute graph
Returns:
_prec (list): list of predecessor nodes
sinx -> sin x
softplusx -> log(1 + exp(x))
succReturns the successor nodes of self in the (implicit) compute graph
Returns:
_succ (list): list of successor nodes
tanhx -> tanh(x), i.e. x -> (exp(x) - exp(-x)) / (exp(x) + exp(-x))
LikelihoodCollectiondef __init__(
self,
dgp,
name2lik,
name=None,
t0=None,
t1=None,
):
A NoiseBlock subclass that facilitates running inference against multiple NoiseBlock objects simultaneously.
Args:
dgp (callable): a callable that, when called, calls all NoiseBlock objects in the correct order. This callable might be created with a call to core._collect.name2lik (dict): structure {str: Block}, name of the block mapping to the block object.See NoiseBlock for definitions of other parameters.
_fit_autoguideNone
_maybe_add_blocksdef _maybe_add_blocks(self, *args):
Adds parameters to prec and succ if they subclass Block.
Args:
args: iterable of (name, parameter, bound)_maybe_remove_blocksNone
_modelNone
_transformDefines a transform from a string argument.
Currently the following string arguments are supported:
The resulting transform will be added to the transform stack iff it is not already at the top of the stack.
Args:
arg (str): one of the above strings corresponding to functionReturns:
self (stsb.Block)
arctanhx -> arctanh(x), i.e. x -> 0.5 log ((1 + x) / (1 - x))
clear_cacheClears the block cache.
This method does not alter the cache mode.
cosx -> cos x
diffx -> x[1:] - x[:-1]
Note that this lowers the time dimension from T to T - 1.
expx -> exp(x)
fitdef fit(self, method="nf_block_ar", method_kwargs=dict(), verbosity=0.01):
Fits a guide (variational posterior) to the model.
Wraps multiple Pyro implementations of variational inference. To minimize noise in the estimation you should follow the Pyro guidelines about marginalizing out discrete latent rvs, etc.
Args:
method (str): one of “advi”, “low_rank”, or “nf_block_ar”.
"advi": fits a diagonal normal distribution in unconstrained latent space"low_rank": fits a low-rank multivariate normal in unconstrained latent space. Unlike the diagonal normal, this guide can capture some nonlocal dependence in latent rvs."nf_block_ar": fits a normalizing flow block autoregressive neural density estimator in unconstrained latent space. This method uses two stacked block autoregressive NNs. See the Pyro docs for more details about this.method_kwargs (dict): optional keyword arguments to pass to Pyro’s inference capabilities. If no keyword arguments are specified, sane defaults will be passed instead. Some arguments could include:
"niter": number of iterations to run optimization (default 1000)"lr": the learning rate (default 0.01)"loss": the loss function to use (default "Trace_ELBO")"optim": the optimizer to use (default "AdamW")verbosity (float): status messages are printed every int(1.0 / verbosity) iterationsfloorx -> x - [[x]], where [[.]] is the fractional part operator
invlogitx -> 1 / (1 + exp(-x))
logx -> log x
Block paths must be positive for valid output.
logdiffx -> log x[1:] - log x[:-1]
Note that this lowers the time dimension from T to T - 1.
logitx -> log(x / (1 - x))
modeldef model(self, *args, **kwargs):
Draws a batch of samples from the block.
Args:
args: optional positional argumentskwargs: optional keyword argumentsReturns:
draws (torch.tensor) sampled values from the blockposterior_predictivedef posterior_predictive(
self,
nsamples=1,
):
Draws from the posterior predictive distribution of the graph with self as the root
Args:
nsamples (int): number of samples to drawReturns:
samples (torch.tensor)
precReturns the predecessor nodes of self in the (implicit) compute graph
Returns:
_prec (list): list of predecessor nodes
prior_predictivedef prior_predictive(
self,
nsamples=1,
):
Draws from the prior predictive distribution of the graph with self as the root
Args:
nsamples (int): number of samples to drawReturns:
samples (torch.tensor)
sampledef sample(
self,
nsamples=100,
thin=0.1,
burnin=500,
):
Sample from the model’s posterior using the Pyro implementation of the No-U Turn Sampler
This could take a very long time for long time series. It is recommended to use .fit(...) instead.
Args:
nsamples (int): number of desired samples after burn in and thinningthin (float): every int(1.0 / thin) sample is keptburnin (int): samples[burnin:] are keptsinx -> sin x
softplusx -> log(1 + exp(x))
succReturns the successor nodes of self in the (implicit) compute graph
Returns:
_succ (list): list of successor nodes
tanhx -> tanh(x), i.e. x -> (exp(x) - exp(-x)) / (exp(x) + exp(-x))
MultiplyBlockRepresents the result of multiplying two blocks toogether.
.model(...) is computed as x ~ p(x); y ~ p(y); x * y
Args:
left (Block)right (Block)_maybe_add_blocksdef _maybe_add_blocks(self, *args):
Adds parameters to prec and succ if they subclass Block.
Args:
args: iterable of (name, parameter, bound)_maybe_remove_blocksNone
_modelNone
_transformDefines a transform from a string argument.
Currently the following string arguments are supported:
The resulting transform will be added to the transform stack iff it is not already at the top of the stack.
Args:
arg (str): one of the above strings corresponding to functionReturns:
self (stsb.Block)
arctanhx -> arctanh(x), i.e. x -> 0.5 log ((1 + x) / (1 - x))
clear_cacheClears the block cache.
This method does not alter the cache mode.
cosx -> cos x
diffx -> x[1:] - x[:-1]
Note that this lowers the time dimension from T to T - 1.
expx -> exp(x)
floorx -> x - [[x]], where [[.]] is the fractional part operator
invlogitx -> 1 / (1 + exp(-x))
logx -> log x
Block paths must be positive for valid output.
logdiffx -> log x[1:] - log x[:-1]
Note that this lowers the time dimension from T to T - 1.
logitx -> log(x / (1 - x))
modeldef model(self, *args, **kwargs):
Draws a batch of samples from the block.
Args:
args: optional positional argumentskwargs: optional keyword argumentsReturns:
draws (torch.tensor) sampled values from the blockprecReturns the predecessor nodes of self in the (implicit) compute graph
Returns:
_prec (list): list of predecessor nodes
sinx -> sin x
softplusx -> log(1 + exp(x))
succReturns the successor nodes of self in the (implicit) compute graph
Returns:
_succ (list): list of successor nodes
tanhx -> tanh(x), i.e. x -> (exp(x) - exp(-x)) / (exp(x) + exp(-x))
NoiseBlockBase class for all likelihood function-type blocks
Implements a number of inference wrappers to Pyro implementations.
Args:
dgp (Block): the latent data-generating process for which self serves as a likelihood functiondata (None || torch.tensor): the observed data. If data is None, then using the noise block is equivalent to drawing from the prior of a state space modelname (None || str): a unique name of the block. If name is None, a unique name will be automatically generatedFor other argument documentation, see Block
_fit_autoguideNone
_maybe_add_blocksdef _maybe_add_blocks(self, *args):
Adds parameters to prec and succ if they subclass Block.
Args:
args: iterable of (name, parameter, bound)_maybe_remove_blocksNone
_modelNone
_transformDefines a transform from a string argument.
Currently the following string arguments are supported:
The resulting transform will be added to the transform stack iff it is not already at the top of the stack.
Args:
arg (str): one of the above strings corresponding to functionReturns:
self (stsb.Block)
arctanhx -> arctanh(x), i.e. x -> 0.5 log ((1 + x) / (1 - x))
clear_cacheClears the block cache.
This method does not alter the cache mode.
cosx -> cos x
diffx -> x[1:] - x[:-1]
Note that this lowers the time dimension from T to T - 1.
expx -> exp(x)
fitdef fit(self, method="nf_block_ar", method_kwargs=dict(), verbosity=0.01):
Fits a guide (variational posterior) to the model.
Wraps multiple Pyro implementations of variational inference. To minimize noise in the estimation you should follow the Pyro guidelines about marginalizing out discrete latent rvs, etc.
Args:
method (str): one of “advi”, “low_rank”, or “nf_block_ar”.
"advi": fits a diagonal normal distribution in unconstrained latent space"low_rank": fits a low-rank multivariate normal in unconstrained latent space. Unlike the diagonal normal, this guide can capture some nonlocal dependence in latent rvs."nf_block_ar": fits a normalizing flow block autoregressive neural density estimator in unconstrained latent space. This method uses two stacked block autoregressive NNs. See the Pyro docs for more details about this.method_kwargs (dict): optional keyword arguments to pass to Pyro’s inference capabilities. If no keyword arguments are specified, sane defaults will be passed instead. Some arguments could include:
"niter": number of iterations to run optimization (default 1000)"lr": the learning rate (default 0.01)"loss": the loss function to use (default "Trace_ELBO")"optim": the optimizer to use (default "AdamW")verbosity (float): status messages are printed every int(1.0 / verbosity) iterationsfloorx -> x - [[x]], where [[.]] is the fractional part operator
invlogitx -> 1 / (1 + exp(-x))
logx -> log x
Block paths must be positive for valid output.
logdiffx -> log x[1:] - log x[:-1]
Note that this lowers the time dimension from T to T - 1.
logitx -> log(x / (1 - x))
modeldef model(self, *args, **kwargs):
Draws a batch of samples from the block.
Args:
args: optional positional argumentskwargs: optional keyword argumentsReturns:
draws (torch.tensor) sampled values from the blockposterior_predictivedef posterior_predictive(
self,
nsamples=1,
):
Draws from the posterior predictive distribution of the graph with self as the root
Args:
nsamples (int): number of samples to drawReturns:
samples (torch.tensor)
precReturns the predecessor nodes of self in the (implicit) compute graph
Returns:
_prec (list): list of predecessor nodes
prior_predictivedef prior_predictive(
self,
nsamples=1,
):
Draws from the prior predictive distribution of the graph with self as the root
Args:
nsamples (int): number of samples to drawReturns:
samples (torch.tensor)
sampledef sample(
self,
nsamples=100,
thin=0.1,
burnin=500,
):
Sample from the model’s posterior using the Pyro implementation of the No-U Turn Sampler
This could take a very long time for long time series. It is recommended to use .fit(...) instead.
Args:
nsamples (int): number of desired samples after burn in and thinningthin (float): every int(1.0 / thin) sample is keptburnin (int): samples[burnin:] are keptsinx -> sin x
softplusx -> log(1 + exp(x))
succReturns the successor nodes of self in the (implicit) compute graph
Returns:
_succ (list): list of successor nodes
tanhx -> tanh(x), i.e. x -> (exp(x) - exp(-x)) / (exp(x) + exp(-x))
_add_fns_to_reprNone
_apply_fnsNone
_closure_initdef closure_init(init_fn):
Generates a complete constructor given only the implementation-specific portion
_collectGenerates a single callable model from a collection of NoiseBlock instances.
The callable model dgp returns a dictionary {model1_name: model1_returnval,...}
_forecast_is_marginalized_varNone
_forecast_replaceattrsNone
_forecast_setattrsNone
_generic_initdef _generic_init(self, init_fn, name=None, t0=0, t1=2, size=1, **kwargs)
Generic portion of init function for dynamically-created blocks
Args:
init_fn (callable): defines the implementation-specific portion of the constructorFor other argument documentation, see Block
_is_blockNone
_is_observable_blockNone
_is_pyro_distNone
_leaf_arg_errorNone
_make_2dNone
_make_idNone
_noblock_leaf_arg_errorNone
_obj_name_to_definiteNone
_obj_name_to_definite_fobNone
_obj_name_to_definite_likelihoodNone
_obj_name_to_definite_seasonNone
collectdef collect(*liks, name="likelihood_collection")
Factory function that generates a unified collection of multiple NoiseBlock subclasses.
Args:
*liks: each element of liks must subclass NoiseBlockname (str): the name of the returned LikelihoodCollection.construct_initdef construct_init(fn_addr_param):
Constructs an implementation-specific constructor given a parameter specification dictionary.
Args:
fn_addr_params (dict): {function_address: parameters}. parametersis adict` with structure
{
"expand": bool,
"domain": domain defined in constants,
"default": Block || torch.tensor || pyro.distributions
}forecastdef forecast(dgp, samples, *args, Nt=1, nsamples=1, **kwargs):
Forecasts the root node of the DGP forward in time.
Args:
dgp (Block): the root node to forecast forwardsamples (dict): {semantic site name: value} The value tensors should have shape (m, n, T), where m is the number of samples, n is the batch size, and T is the length of the time series*args: any additional positional arguments to pass to dgp.modelNt (int): number of timesteps for which to generate forecast. Forecast is generated from t1 + 1 to t1 + 1 + Nt.nsamples (int): number of samples to draw from the forecast distributiondesign_tensors (Dict[str, torch.Tensor]):**kwargs: any additional keyword arguments to pass to dgp.modelname_to_definitedef name_to_definite(skeleton, *names,):
Makes the names associated with the block skeleton into torch.tensors according to the skeleton’s current interpretation.
Args:
skeleton (Block-like): an incompletely-constructed block. The block will be incompletely constructed because the definition of this method is part of the block’s definition.*names (List[str]): names to make definiteReturns:
tuple of torch.tensor corresponding to the passed names.redefinedef redefine(
block,
attribute,
obj,
):
Redefines an attribute of a block to the passed object
Args:
block (Block)attribute (str)obj (Block || torch.tensor || pyro.distributions)register_address_componentdef register_address_component(
name,
expand,
domain=None,
):
Registers a new functional address component and metadata.
Addresses in stsb3 look like x/y-z or y-z, where x is a context coomponent, y is the name of the rv, and z describes its function. register_address_component allows for runtime definition of z address components for use in new blocks or structure search algorithms.
Args:
name (str): the name. E.g., already-defined names include loc, scale, and amplitudeexpand (bool): whether objects whose address contains this component can be expanded using Blocksdomain (None || tuple): if expand, must be a tuple in constants.DOMAINS (i.e., one of (-inf, inf), (0, inf), or (0, 1))This function is not safe – if name already exists in constants, this will overwrite its definition and properties.