stsb3.core

AddBlock

Represents the result of adding two blocks toogether.

.model(...) is computed as x ~ p(x); y ~ p(y); x + y

Args:

_maybe_add_blocks

def _maybe_add_blocks(self, *args):

Adds parameters to prec and succ if they subclass Block.

Args:

_maybe_remove_blocks

None

_model

None

_transform

Defines 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:

Returns:

self (stsb.Block)

arctanh

x -> arctanh(x), i.e. x -> 0.5 log ((1 + x) / (1 - x))

clear_cache

Clears the block cache.

This method does not alter the cache mode.

cos

x -> cos x

diff

x -> x[1:] - x[:-1]

Note that this lowers the time dimension from T to T - 1.

exp

x -> exp(x)

floor

x -> x - [[x]], where [[.]] is the fractional part operator

invlogit

x -> 1 / (1 + exp(-x))

log

x -> log x

Block paths must be positive for valid output.

logdiff

x -> log x[1:] - log x[:-1]

Note that this lowers the time dimension from T to T - 1.

logit

x -> log(x / (1 - x))

model

def model(self, *args, **kwargs):

Draws a batch of samples from the block.

Args:

Returns:

prec

Returns the predecessor nodes of self in the (implicit) compute graph

Returns:

_prec (list): list of predecessor nodes

sin

x -> sin x

softplus

x -> log(1 + exp(x))

succ

Returns the successor nodes of self in the (implicit) compute graph

Returns:

_succ (list): list of successor nodes

tanh

x -> tanh(x), i.e. x -> (exp(x) - exp(-x)) / (exp(x) + exp(-x))

Block

Base class that all STS blocks should subclass.

Defines a number of useful modeling constructs and methods, such as deterministic transformations.

_maybe_add_blocks

def _maybe_add_blocks(self, *args):

Adds parameters to prec and succ if they subclass Block.

Args:

_maybe_remove_blocks

None

_model

None

_transform

Defines 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:

Returns:

self (stsb.Block)

arctanh

x -> arctanh(x), i.e. x -> 0.5 log ((1 + x) / (1 - x))

clear_cache

Clears the block cache.

This method does not alter the cache mode.

cos

x -> cos x

diff

x -> x[1:] - x[:-1]

Note that this lowers the time dimension from T to T - 1.

exp

x -> exp(x)

floor

x -> x - [[x]], where [[.]] is the fractional part operator

invlogit

x -> 1 / (1 + exp(-x))

log

x -> log x

Block paths must be positive for valid output.

logdiff

x -> log x[1:] - log x[:-1]

Note that this lowers the time dimension from T to T - 1.

logit

x -> log(x / (1 - x))

model

def model(self, *args, **kwargs):

Draws a batch of samples from the block.

Args:

Returns:

prec

Returns the predecessor nodes of self in the (implicit) compute graph

Returns:

_prec (list): list of predecessor nodes

sin

x -> sin x

softplus

x -> log(1 + exp(x))

succ

Returns the successor nodes of self in the (implicit) compute graph

Returns:

_succ (list): list of successor nodes

tanh

x -> tanh(x), i.e. x -> (exp(x) - exp(-x)) / (exp(x) + exp(-x))

LikelihoodCollection

def __init__(
    self,
    dgp,
    name2lik,
    name=None,
    t0=None,
    t1=None,
):

A NoiseBlock subclass that facilitates running inference against multiple NoiseBlock objects simultaneously.

Args:

See NoiseBlock for definitions of other parameters.

_fit_autoguide

None

_maybe_add_blocks

def _maybe_add_blocks(self, *args):

Adds parameters to prec and succ if they subclass Block.

Args:

_maybe_remove_blocks

None

_model

None

_transform

Defines 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:

Returns:

self (stsb.Block)

arctanh

x -> arctanh(x), i.e. x -> 0.5 log ((1 + x) / (1 - x))

clear_cache

Clears the block cache.

This method does not alter the cache mode.

cos

x -> cos x

diff

x -> x[1:] - x[:-1]

Note that this lowers the time dimension from T to T - 1.

exp

x -> exp(x)

fit

def 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:

floor

x -> x - [[x]], where [[.]] is the fractional part operator

invlogit

x -> 1 / (1 + exp(-x))

log

x -> log x

Block paths must be positive for valid output.

logdiff

x -> log x[1:] - log x[:-1]

Note that this lowers the time dimension from T to T - 1.

logit

x -> log(x / (1 - x))

model

def model(self, *args, **kwargs):

Draws a batch of samples from the block.

Args:

Returns:

posterior_predictive

def posterior_predictive(
    self,
    nsamples=1,
):

Draws from the posterior predictive distribution of the graph with self as the root

Args:

Returns:

samples (torch.tensor)

prec

Returns the predecessor nodes of self in the (implicit) compute graph

Returns:

_prec (list): list of predecessor nodes

prior_predictive

def prior_predictive(
    self,
    nsamples=1,
):

Draws from the prior predictive distribution of the graph with self as the root

Args:

Returns:

samples (torch.tensor)

sample

def 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:

sin

x -> sin x

softplus

x -> log(1 + exp(x))

succ

Returns the successor nodes of self in the (implicit) compute graph

Returns:

_succ (list): list of successor nodes

tanh

x -> tanh(x), i.e. x -> (exp(x) - exp(-x)) / (exp(x) + exp(-x))

MultiplyBlock

Represents the result of multiplying two blocks toogether.

.model(...) is computed as x ~ p(x); y ~ p(y); x * y

Args:

_maybe_add_blocks

def _maybe_add_blocks(self, *args):

Adds parameters to prec and succ if they subclass Block.

Args:

_maybe_remove_blocks

None

_model

None

_transform

Defines 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:

Returns:

self (stsb.Block)

arctanh

x -> arctanh(x), i.e. x -> 0.5 log ((1 + x) / (1 - x))

clear_cache

Clears the block cache.

This method does not alter the cache mode.

cos

x -> cos x

diff

x -> x[1:] - x[:-1]

Note that this lowers the time dimension from T to T - 1.

exp

x -> exp(x)

floor

x -> x - [[x]], where [[.]] is the fractional part operator

invlogit

x -> 1 / (1 + exp(-x))

log

x -> log x

Block paths must be positive for valid output.

logdiff

x -> log x[1:] - log x[:-1]

Note that this lowers the time dimension from T to T - 1.

logit

x -> log(x / (1 - x))

model

def model(self, *args, **kwargs):

Draws a batch of samples from the block.

Args:

Returns:

prec

Returns the predecessor nodes of self in the (implicit) compute graph

Returns:

_prec (list): list of predecessor nodes

sin

x -> sin x

softplus

x -> log(1 + exp(x))

succ

Returns the successor nodes of self in the (implicit) compute graph

Returns:

_succ (list): list of successor nodes

tanh

x -> tanh(x), i.e. x -> (exp(x) - exp(-x)) / (exp(x) + exp(-x))

NoiseBlock

Base class for all likelihood function-type blocks

Implements a number of inference wrappers to Pyro implementations.

Args:

For other argument documentation, see Block

_fit_autoguide

None

_maybe_add_blocks

def _maybe_add_blocks(self, *args):

Adds parameters to prec and succ if they subclass Block.

Args:

_maybe_remove_blocks

None

_model

None

_transform

Defines 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:

Returns:

self (stsb.Block)

arctanh

x -> arctanh(x), i.e. x -> 0.5 log ((1 + x) / (1 - x))

clear_cache

Clears the block cache.

This method does not alter the cache mode.

cos

x -> cos x

diff

x -> x[1:] - x[:-1]

Note that this lowers the time dimension from T to T - 1.

exp

x -> exp(x)

fit

def 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:

floor

x -> x - [[x]], where [[.]] is the fractional part operator

invlogit

x -> 1 / (1 + exp(-x))

log

x -> log x

Block paths must be positive for valid output.

logdiff

x -> log x[1:] - log x[:-1]

Note that this lowers the time dimension from T to T - 1.

logit

x -> log(x / (1 - x))

model

def model(self, *args, **kwargs):

Draws a batch of samples from the block.

Args:

Returns:

posterior_predictive

def posterior_predictive(
    self,
    nsamples=1,
):

Draws from the posterior predictive distribution of the graph with self as the root

Args:

Returns:

samples (torch.tensor)

prec

Returns the predecessor nodes of self in the (implicit) compute graph

Returns:

_prec (list): list of predecessor nodes

prior_predictive

def prior_predictive(
    self,
    nsamples=1,
):

Draws from the prior predictive distribution of the graph with self as the root

Args:

Returns:

samples (torch.tensor)

sample

def 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:

sin

x -> sin x

softplus

x -> log(1 + exp(x))

succ

Returns the successor nodes of self in the (implicit) compute graph

Returns:

_succ (list): list of successor nodes

tanh

x -> tanh(x), i.e. x -> (exp(x) - exp(-x)) / (exp(x) + exp(-x))

_add_fns_to_repr

None

_apply_fns

None

_closure_init

def closure_init(init_fn):

Generates a complete constructor given only the implementation-specific portion

_collect

Generates a single callable model from a collection of NoiseBlock instances.

The callable model dgp returns a dictionary {model1_name: model1_returnval,...}

_forecast_is_marginalized_var

None

_forecast_replaceattrs

None

_forecast_setattrs

None

_generic_init

def _generic_init(self, init_fn, name=None, t0=0, t1=2, size=1, **kwargs)

Generic portion of init function for dynamically-created blocks

Args:

For other argument documentation, see Block

_is_block

None

_is_observable_block

None

_is_pyro_dist

None

_leaf_arg_error

None

_make_2d

None

_make_id

None

_noblock_leaf_arg_error

None

_obj_name_to_definite

None

_obj_name_to_definite_fob

None

_obj_name_to_definite_likelihood

None

_obj_name_to_definite_season

None

collect

def collect(*liks, name="likelihood_collection")

Factory function that generates a unified collection of multiple NoiseBlock subclasses.

Args:

construct_init

def construct_init(fn_addr_param):

Constructs an implementation-specific constructor given a parameter specification dictionary.

Args:

forecast

def forecast(dgp, samples, *args, Nt=1, nsamples=1, **kwargs):

Forecasts the root node of the DGP forward in time.

Args:

name_to_definite

def name_to_definite(skeleton, *names,):

Makes the names associated with the block skeleton into torch.tensors according to the skeleton’s current interpretation.

Args:

Returns:

redefine

def redefine(
    block,
    attribute,
    obj,
):

Redefines an attribute of a block to the passed object

Args:

register_address_component

def 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:

This function is not safe – if name already exists in constants, this will overwrite its definition and properties.