def __init__(self,
               order,
               coefficients_prior=None,
               level_scale_prior=None,
               initial_state_prior=None,
               coefficient_constraining_bijector=None,
               observed_time_series=None,
               name=None):
    """Specify an autoregressive model.

    Args:
      order: scalar Python positive `int` specifying the number of past
        timesteps to regress on.
      coefficients_prior: optional `tfd.Distribution` instance specifying a
        prior on the `coefficients` parameter. If `None`, a default standard
        normal (`tfd.MultivariateNormalDiag(scale_diag=tf.ones([order]))`) prior
        is used.
        Default value: `None`.
      level_scale_prior: optional `tfd.Distribution` instance specifying a prior
        on the `level_scale` parameter. If `None`, a heuristic default prior is
        constructed based on the provided `observed_time_series`.
        Default value: `None`.
      initial_state_prior: optional `tfd.Distribution` instance specifying a
        prior on the initial state, corresponding to the values of the process
        at a set of size `order` of imagined timesteps before the initial step.
        If `None`, a heuristic default prior is constructed based on the
        provided `observed_time_series`.
        Default value: `None`.
      coefficient_constraining_bijector: optional `tfb.Bijector` instance
        representing a constraining mapping for the autoregressive coefficients.
        For example, `tfb.Tanh()` constrains the coefficients to lie in
        `(-1, 1)`, while `tfb.Softplus()` constrains them to be positive, and
        `tfb.Identity()` implies no constraint. If `None`, the default behavior
        constrains the coefficients to lie in `(-1, 1)` using a `Tanh` bijector.
        Default value: `None`.
      observed_time_series: optional `float` `Tensor` of shape
        `batch_shape + [T, 1]` (omitting the trailing unit dimension is also
        supported when `T > 1`), specifying an observed time series.
        Any priors not explicitly set will be given default values according to
        the scale of the observed time series (or batch of time series). May
        optionally be an instance of `tfp.sts.MaskedTimeSeries`, which includes
        a mask `Tensor` to specify timesteps with missing observations.
        Default value: `None`.
      name: the name of this model component.
        Default value: 'Autoregressive'.
    """
    with tf.compat.v1.name_scope(
        name, 'Autoregressive', values=[observed_time_series]) as name:

      masked_time_series = None
      if observed_time_series is not None:
        masked_time_series = (
            sts_util.canonicalize_observed_time_series_with_mask(
                observed_time_series))

      dtype = dtype_util.common_dtype(
          [(masked_time_series.time_series
            if masked_time_series is not None else None),
           coefficients_prior,
           level_scale_prior,
           initial_state_prior], preferred_dtype=tf.float32)

      if observed_time_series is not None:
        _, observed_stddev, observed_initial = sts_util.empirical_statistics(
            masked_time_series)
      else:
        observed_stddev, observed_initial = (
            tf.convert_to_tensor(value=1., dtype=dtype),
            tf.convert_to_tensor(value=0., dtype=dtype))
      batch_ones = tf.ones(tf.concat([
          tf.shape(input=observed_initial),  # Batch shape
          [order]], axis=0), dtype=dtype)

      # Heuristic default priors. Overriding these may dramatically
      # change inference performance and results.
      if coefficients_prior is None:
        coefficients_prior = tfd.MultivariateNormalDiag(
            scale_diag=batch_ones)
      if level_scale_prior is None:
        level_scale_prior = tfd.LogNormal(
            loc=tf.math.log(0.05 *  observed_stddev), scale=3.)

      if (coefficients_prior.event_shape.is_fully_defined() and
          order != coefficients_prior.event_shape[0]):
        raise ValueError("Prior dimension {} doesn't match order {}.".format(
            coefficients_prior.event_shape[0], order))

      if initial_state_prior is None:
        initial_state_prior = tfd.MultivariateNormalDiag(
            loc=observed_initial[..., tf.newaxis] * batch_ones,
            scale_diag=(tf.abs(observed_initial) +
                        observed_stddev)[..., tf.newaxis] * batch_ones)

      self._order = order
      self._coefficients_prior = coefficients_prior
      self._level_scale_prior = level_scale_prior
      self._initial_state_prior = initial_state_prior

      if coefficient_constraining_bijector is None:
        coefficient_constraining_bijector = tfb.Tanh()
      super(Autoregressive, self).__init__(
          parameters=[
              Parameter('coefficients',
                        coefficients_prior,
                        coefficient_constraining_bijector),
              Parameter('level_scale', level_scale_prior, tfb.Softplus())
          ],
          latent_size=order,
          name=name)
def __init__ ( self , order , coefficients_prior = None , level_scale_prior = None , initial_state_prior = None , coefficient_constraining_bijector = None , observed_time_series = None , name = None ) : with tf . compat . v1 . name_scope ( name , 'Autoregressive' , values = [ observed_time_series ] ) as name : masked_time_series = None if observed_time_series is not None : masked_time_series = ( sts_util . canonicalize_observed_time_series_with_mask ( observed_time_series ) ) dtype = dtype_util . common_dtype ( [ ( masked_time_series . time_series if masked_time_series is not None else None ) , coefficients_prior , level_scale_prior , initial_state_prior ] , preferred_dtype = tf . float32 ) if observed_time_series is not None : _ , observed_stddev , observed_initial = sts_util . empirical_statistics ( masked_time_series ) else : observed_stddev , observed_initial = ( tf . convert_to_tensor ( value = 1. , dtype = dtype ) , tf . convert_to_tensor ( value = 0. , dtype = dtype ) ) batch_ones = tf . ones ( tf . concat ( [ tf . shape ( input = observed_initial ) , [ order ] ] , axis = 0 ) , dtype = dtype ) if coefficients_prior is None : coefficients_prior = tfd . MultivariateNormalDiag ( scale_diag = batch_ones ) if level_scale_prior is None : level_scale_prior = tfd . LogNormal ( loc = tf . math . log ( 0.05 * observed_stddev ) , scale = 3. ) if ( coefficients_prior . event_shape . is_fully_defined ( ) and order != coefficients_prior . event_shape [ 0 ] ) : raise ValueError ( "Prior dimension {} doesn't match order {}." . format ( coefficients_prior . event_shape [ 0 ] , order ) ) if initial_state_prior is None : initial_state_prior = tfd . MultivariateNormalDiag ( loc = observed_initial [ ... , tf . newaxis ] * batch_ones , scale_diag = ( tf . abs ( observed_initial ) + observed_stddev ) [ ... , tf . newaxis ] * batch_ones ) self . _order = order self . _coefficients_prior = coefficients_prior self . _level_scale_prior = level_scale_prior self . _initial_state_prior = initial_state_prior if coefficient_constraining_bijector is None : coefficient_constraining_bijector = tfb . Tanh ( ) super ( Autoregressive , self ) . __init__ ( parameters = [ Parameter ( 'coefficients' , coefficients_prior , coefficient_constraining_bijector ) , Parameter ( 'level_scale' , level_scale_prior , tfb . Softplus ( ) ) ] , latent_size = order , name = name )
Specify an autoregressive model.

    Args:
      order: scalar Python positive `int` specifying the number of past
        timesteps to regress on.
      coefficients_prior: optional `tfd.Distribution` instance specifying a
        prior on the `coefficients` parameter. If `None`, a default standard
        normal (`tfd.MultivariateNormalDiag(scale_diag=tf.ones([order]))`) prior
        is used.
        Default value: `None`.
      level_scale_prior: optional `tfd.Distribution` instance specifying a prior
        on the `level_scale` parameter. If `None`, a heuristic default prior is
        constructed based on the provided `observed_time_series`.
        Default value: `None`.
      initial_state_prior: optional `tfd.Distribution` instance specifying a
        prior on the initial state, corresponding to the values of the process
        at a set of size `order` of imagined timesteps before the initial step.
        If `None`, a heuristic default prior is constructed based on the
        provided `observed_time_series`.
        Default value: `None`.
      coefficient_constraining_bijector: optional `tfb.Bijector` instance
        representing a constraining mapping for the autoregressive coefficients.
        For example, `tfb.Tanh()` constrains the coefficients to lie in
        `(-1, 1)`, while `tfb.Softplus()` constrains them to be positive, and
        `tfb.Identity()` implies no constraint. If `None`, the default behavior
        constrains the coefficients to lie in `(-1, 1)` using a `Tanh` bijector.
        Default value: `None`.
      observed_time_series: optional `float` `Tensor` of shape
        `batch_shape + [T, 1]` (omitting the trailing unit dimension is also
        supported when `T > 1`), specifying an observed time series.
        Any priors not explicitly set will be given default values according to
        the scale of the observed time series (or batch of time series). May
        optionally be an instance of `tfp.sts.MaskedTimeSeries`, which includes
        a mask `Tensor` to specify timesteps with missing observations.
        Default value: `None`.
      name: the name of this model component.
        Default value: 'Autoregressive'.