class_docstr: Formal representation of a semi-local linear trend model.
  
  Like the `LocalLinearTrend` model, a semi-local linear trend posits a
  latent `level` and `slope`, with the level component updated according to
  the current slope plus a random walk:
  
  ```
  level[t] = level[t-1] + slope[t-1] + Normal(0., level_scale)
  ```
  
  The slope component in a `SemiLocalLinearTrend` model evolves according to
  a first-order autoregressive (AR1) process with potentially nonzero mean:
  
  ```
  slope[t] = (slope_mean +
              autoregressive_coef * (slope[t-1] - slope_mean) +
              Normal(0., slope_scale))
  ```
  
  Unlike the random walk used in `LocalLinearTrend`, a stationary
  AR1 process (coefficient in `(-1, 1)`) maintains bounded variance over time,
  so a `SemiLocalLinearTrend` model will often produce more reasonable
  uncertainties when forecasting over long timescales.
class_docstr_tok: ["Formal","representation","of","a","semi-local","linear","trend","model",".","Like","the","`","LocalLinearTrend","`","model",",","a","semi-local","linear","trend","posits","a","latent","`","level","`","and","`","slope","`",",","with","the","level","component","updated","according","to","the","current","slope","plus","a","random","walk",":","``","`","level","[","t","]","=","level","[","t-1","]","+","slope","[","t-1","]","+","Normal","(","0.",",","level_scale",")","``","`","The","slope","component","in","a","`","SemiLocalLinearTrend","`","model","evolves","according","to","a","first-order","autoregressive","(","AR1",")","process","with","potentially","nonzero","mean",":","``","`","slope","[","t","]","=","(","slope_mean","+","autoregressive_coef","*","(","slope","[","t-1","]","-","slope_mean",")","+","Normal","(","0.",",","slope_scale",")",")","``","`","Unlike","the","random","walk","used","in","`","LocalLinearTrend","`",",","a","stationary","AR1","process","(","coefficient","in","`","(","-1",",","1",")","`",")","maintains","bounded","variance","over","time",",","so","a","`","SemiLocalLinearTrend","`","model","will","often","produce","more","reasonable","uncertainties","when","forecasting","over","long","timescales","."]
docstr: Specify a semi-local linear trend model.
  
  Args:
    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`.
    slope_mean_prior: optional `tfd.Distribution` instance specifying a prior
      on the `slope_mean` parameter. If `None`, a heuristic default prior is
      constructed based on the provided `observed_time_series`.
      Default value: `None`.
    slope_scale_prior: optional `tfd.Distribution` instance specifying a prior
      on the `slope_scale` parameter. If `None`, a heuristic default prior is
      constructed based on the provided `observed_time_series`.
      Default value: `None`.
    autoregressive_coef_prior: optional `tfd.Distribution` instance specifying
      a prior on the `autoregressive_coef` parameter. If `None`, the default
      prior is a standard `Normal(0., 1.)`. Note that the prior may be
      implicitly truncated by `constrain_ar_coef_stationary` and/or
      `constrain_ar_coef_positive`.
      Default value: `None`.
    initial_level_prior: optional `tfd.Distribution` instance specifying a
      prior on the initial level. If `None`, a heuristic default prior is
      constructed based on the provided `observed_time_series`.
      Default value: `None`.
    initial_slope_prior: optional `tfd.Distribution` instance specifying a
      prior on the initial slope. If `None`, a heuristic default prior is
      constructed based on the provided `observed_time_series`.
      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 `NaN`s
      are interpreted as missing observations; missingness may be also be
      explicitly specified by passing a `tfp.sts.MaskedTimeSeries` instance.
      Any priors not explicitly set will be given default values according to
      the scale of the observed time series (or batch of time series).
      Default value: `None`.
    constrain_ar_coef_stationary: if `True`, perform inference using a
      parameterization that restricts `autoregressive_coef` to the interval
      `(-1, 1)`, or `(0, 1)` if `force_positive_ar_coef` is also `True`,
      corresponding to stationary processes. This will implicitly truncate
      the support of `autoregressive_coef_prior`.
      Default value: `True`.
    constrain_ar_coef_positive: if `True`, perform inference using a
      parameterization that restricts `autoregressive_coef` to be positive,
      or in `(0, 1)` if `constrain_ar_coef_stationary` is also `True`. This
      will implicitly truncate the support of `autoregressive_coef_prior`.
      Default value: `False`.
    name: the name of this model component.
      Default value: 'SemiLocalLinearTrend'.
docstr_tok: ["Specify","a","semi-local","linear","trend","model",".","Args",":","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","`",".","slope_mean_prior",":","optional","`","tfd.Distribution","`","instance","specifying","a","prior","on","the","`","slope_mean","`","parameter",".","If","`","None","`",",","a","heuristic","default","prior","is","constructed","based","on","the","provided","`","observed_time_series","`",".","Default","value",":","`","None","`",".","slope_scale_prior",":","optional","`","tfd.Distribution","`","instance","specifying","a","prior","on","the","`","slope_scale","`","parameter",".","If","`","None","`",",","a","heuristic","default","prior","is","constructed","based","on","the","provided","`","observed_time_series","`",".","Default","value",":","`","None","`",".","autoregressive_coef_prior",":","optional","`","tfd.Distribution","`","instance","specifying","a","prior","on","the","`","autoregressive_coef","`","parameter",".","If","`","None","`",",","the","default","prior","is","a","standard","`","Normal","(","0.",",","1.",")","`",".","Note","that","the","prior","may","be","implicitly","truncated","by","`","constrain_ar_coef_stationary","`","and\/or","`","constrain_ar_coef_positive","`",".","Default","value",":","`","None","`",".","initial_level_prior",":","optional","`","tfd.Distribution","`","instance","specifying","a","prior","on","the","initial","level",".","If","`","None","`",",","a","heuristic","default","prior","is","constructed","based","on","the","provided","`","observed_time_series","`",".","Default","value",":","`","None","`",".","initial_slope_prior",":","optional","`","tfd.Distribution","`","instance","specifying","a","prior","on","the","initial","slope",".","If","`","None","`",",","a","heuristic","default","prior","is","constructed","based","on","the","provided","`","observed_time_series","`",".","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","`","NaN","`","s","are","interpreted","as","missing","observations",";","missingness","may","be","also","be","explicitly","specified","by","passing","a","`","tfp.sts.MaskedTimeSeries","`","instance",".","Any","priors","not","explicitly","set","will","be","given","default","values","according","to","the","scale","of","the","observed","time","series","(","or","batch","of","time","series",")",".","Default","value",":","`","None","`",".","constrain_ar_coef_stationary",":","if","`","True","`",",","perform","inference","using","a","parameterization","that","restricts","`","autoregressive_coef","`","to","the","interval","`","(","-1",",","1",")","`",",","or","`","(","0",",","1",")","`","if","`","force_positive_ar_coef","`","is","also","`","True","`",",","corresponding","to","stationary","processes",".","This","will","implicitly","truncate","the","support","of","`","autoregressive_coef_prior","`",".","Default","value",":","`","True","`",".","constrain_ar_coef_positive",":","if","`","True","`",",","perform","inference","using","a","parameterization","that","restricts","`","autoregressive_coef","`","to","be","positive",",","or","in","`","(","0",",","1",")","`","if","`","constrain_ar_coef_stationary","`","is","also","`","True","`",".","This","will","implicitly","truncate","the","support","of","`","autoregressive_coef_prior","`",".","Default","value",":","`","False","`",".","name",":","the","name","of","this","model","component",".","Default","value",":","'SemiLocalLinearTrend","'","."]
code: def __init__(
      self,
      level_scale_prior=None,
      slope_mean_prior=None,
      slope_scale_prior=None,
      autoregressive_coef_prior=None,
      initial_level_prior=None,
      initial_slope_prior=None,
      observed_time_series=None,
      constrain_ar_coef_stationary=True,
      constrain_ar_coef_positive=False,
      name=None,
  ):
      """Specify a semi-local linear trend model.
  
      Args:
        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`.
        slope_mean_prior: optional `tfd.Distribution` instance specifying a prior
          on the `slope_mean` parameter. If `None`, a heuristic default prior is
          constructed based on the provided `observed_time_series`.
          Default value: `None`.
        slope_scale_prior: optional `tfd.Distribution` instance specifying a prior
          on the `slope_scale` parameter. If `None`, a heuristic default prior is
          constructed based on the provided `observed_time_series`.
          Default value: `None`.
        autoregressive_coef_prior: optional `tfd.Distribution` instance specifying
          a prior on the `autoregressive_coef` parameter. If `None`, the default
          prior is a standard `Normal(0., 1.)`. Note that the prior may be
          implicitly truncated by `constrain_ar_coef_stationary` and/or
          `constrain_ar_coef_positive`.
          Default value: `None`.
        initial_level_prior: optional `tfd.Distribution` instance specifying a
          prior on the initial level. If `None`, a heuristic default prior is
          constructed based on the provided `observed_time_series`.
          Default value: `None`.
        initial_slope_prior: optional `tfd.Distribution` instance specifying a
          prior on the initial slope. If `None`, a heuristic default prior is
          constructed based on the provided `observed_time_series`.
          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 `NaN`s
          are interpreted as missing observations; missingness may be also be
          explicitly specified by passing a `tfp.sts.MaskedTimeSeries` instance.
          Any priors not explicitly set will be given default values according to
          the scale of the observed time series (or batch of time series).
          Default value: `None`.
        constrain_ar_coef_stationary: if `True`, perform inference using a
          parameterization that restricts `autoregressive_coef` to the interval
          `(-1, 1)`, or `(0, 1)` if `force_positive_ar_coef` is also `True`,
          corresponding to stationary processes. This will implicitly truncate
          the support of `autoregressive_coef_prior`.
          Default value: `True`.
        constrain_ar_coef_positive: if `True`, perform inference using a
          parameterization that restricts `autoregressive_coef` to be positive,
          or in `(0, 1)` if `constrain_ar_coef_stationary` is also `True`. This
          will implicitly truncate the support of `autoregressive_coef_prior`.
          Default value: `False`.
        name: the name of this model component.
          Default value: 'SemiLocalLinearTrend'.
      """
      init_parameters = dict(locals())
      with tf.name_scope(name or "SemiLocalLinearTrend") as name:
          if observed_time_series is not None:
              (
                  _,
                  observed_stddev,
                  observed_initial,
              ) = sts_util.empirical_statistics(observed_time_series)
          else:
              observed_stddev, observed_initial = 1.0, 0.0
  
          # Heuristic default priors. Overriding these may dramatically
          # change inference performance and results.
          if level_scale_prior is None:
              level_scale_prior = lognormal.LogNormal(
                  loc=tf.math.log(0.01 * observed_stddev), scale=2.0
              )
          if slope_mean_prior is None:
              slope_mean_prior = normal.Normal(
                  loc=0.0, scale=observed_stddev
              )
          if slope_scale_prior is None:
              slope_scale_prior = lognormal.LogNormal(
                  loc=tf.math.log(0.01 * observed_stddev), scale=2.0
              )
          if autoregressive_coef_prior is None:
              autoregressive_coef_prior = normal.Normal(
                  loc=0.0, scale=tf.ones_like(observed_initial)
              )
          if initial_level_prior is None:
              initial_level_prior = normal.Normal(
                  loc=observed_initial,
                  scale=tf.abs(observed_initial) + observed_stddev,
              )
          if initial_slope_prior is None:
              initial_slope_prior = normal.Normal(
                  loc=0.0, scale=observed_stddev
              )
  
          dtype = dtype_util.common_dtype(
              [
                  level_scale_prior,
                  slope_scale_prior,
                  autoregressive_coef_prior,
                  initial_level_prior,
                  initial_slope_prior,
              ]
          )
  
          self._initial_state_prior = mvn_diag.MultivariateNormalDiag(
              loc=tf.stack(
                  [
                      initial_level_prior.mean(),
                      initial_slope_prior.mean(),
                  ],
                  axis=-1,
              ),
              scale_diag=tf.stack(
                  [
                      initial_level_prior.stddev(),
                      initial_slope_prior.stddev(),
                  ],
                  axis=-1,
              ),
          )
  
          # Constrain the support of the autoregressive coefficient.
          if (
              constrain_ar_coef_stationary
              and constrain_ar_coef_positive
          ):
              autoregressive_coef_bijector = (
                  sigmoid.Sigmoid()
              )  # support in (0, 1)
          elif constrain_ar_coef_positive:
              autoregressive_coef_bijector = (
                  softplus.Softplus()
              )  # support in (0, infty)
          elif constrain_ar_coef_stationary:
              autoregressive_coef_bijector = (
                  tanh.Tanh()
              )  # support in (-1, 1)
          else:
              autoregressive_coef_bijector = (
                  identity.Identity()
              )  # unconstrained
  
          stddev_preconditioner = scale.Scale(scale=observed_stddev)
          scaled_softplus = chain.Chain(
              [
                  stddev_preconditioner,
                  softplus.Softplus(low=dtype_util.eps(dtype)),
              ]
          )
          super(SemiLocalLinearTrend, self).__init__(
              parameters=[
                  Parameter(
                      "level_scale", level_scale_prior, scaled_softplus
                  ),
                  Parameter(
                      "slope_mean",
                      slope_mean_prior,
                      stddev_preconditioner,
                  ),
                  Parameter(
                      "slope_scale", slope_scale_prior, scaled_softplus
                  ),
                  Parameter(
                      "autoregressive_coef",
                      autoregressive_coef_prior,
                      autoregressive_coef_bijector,
                  ),
              ],
              latent_size=2,
              init_parameters=init_parameters,
              name=name,
          )
  
imports: import tensorflow.compat.v2
  from tensorflow_probability.python.bijectors import chain
  from tensorflow_probability.python.bijectors import identity
  from tensorflow_probability.python.bijectors import scale
  from tensorflow_probability.python.bijectors import sigmoid
  from tensorflow_probability.python.bijectors import softplus
  from tensorflow_probability.python.bijectors import tanh
  from tensorflow_probability.python.distributions import linear_gaussian_ssm
  from tensorflow_probability.python.distributions import lognormal
  from tensorflow_probability.python.distributions import mvn_diag
  from tensorflow_probability.python.distributions import normal
  from tensorflow_probability.python.internal import distribution_util
  from tensorflow_probability.python.internal import dtype_util
  from tensorflow_probability.python.sts.internal import util
  from tensorflow_probability.python.sts.structural_time_series import Parameter
  from tensorflow_probability.python.sts.structural_time_series import StructuralTimeSeries
label_desc: Use image node_id 1 to create a new SemiLocalLinearTrend object from inherited base classes: StructuralTimeSeries with example: obj = SemiLocalLinearTrend(level_scale_prior, slope_mean_prior, slope_scale_prior, autoregressive_coef_prior, initial_level_prior, initial_slope_prior, observed_time_series, constrain_ar_coef_stationary, constrain_ar_coef_positive, name)