예제 #1
0
    def __init__(
        self,
        periodicities,
        moving_average_order,
        autoregressive_order,
        use_level_noise=True,
        configuration=state_space_model.StateSpaceModelConfiguration()):
        """Initialize the Basic Structural Time Series model.

    Args:
      periodicities: Number of time steps for cyclic behavior. May be a list, in
          which case one periodic component is created for each element.
      moving_average_order: The number of moving average coefficients to use,
          which also defines the number of steps after which transient
          deviations revert to the mean defined by periodic and level/trend
          components.
      autoregressive_order: The number of steps back for autoregression.
      use_level_noise: Whether to model the time series as having level
          noise. See level_noise in the model description above.
      configuration: A StateSpaceModelConfiguration object.
    """
        component_model_configuration = configuration._replace(
            use_observation_noise=False)
        univariate_component_model_configuration = (
            component_model_configuration._replace(num_features=1))

        adder_part = _replicate_level_trend_models(
            multivariate_configuration=component_model_configuration,
            univariate_configuration=univariate_component_model_configuration)
        with variable_scope.variable_scope("varma"):
            varma_part = varma.VARMA(
                autoregressive_order=autoregressive_order,
                moving_average_order=moving_average_order,
                configuration=component_model_configuration)

        cycle_parts = []
        periodicity_list = nest.flatten(periodicities)
        for cycle_number, cycle_periodicity in enumerate(periodicity_list):
            # For each specified periodicity, construct models for each feature with
            # correlated noise.
            with variable_scope.variable_scope("cycle{}".format(cycle_number)):
                cycle_features = []
                for feature in range(configuration.num_features):
                    with variable_scope.variable_scope(
                            "feature{}".format(feature)):
                        cycle_features.append(
                            periodic.CycleStateSpaceModel(
                                periodicity=cycle_periodicity,
                                configuration=
                                univariate_component_model_configuration))
                cycle_parts.append(
                    state_space_model.StateSpaceCorrelatedFeaturesEnsemble(
                        ensemble_members=cycle_features,
                        configuration=component_model_configuration))

        super(StructuralEnsemble, self).__init__(
            ensemble_members=[adder_part, varma_part] + cycle_parts,
            configuration=configuration)
예제 #2
0
 def test_make_ensemble_no_errors(self):
     with variable_scope.variable_scope("model_one"):
         model_one = varma.VARMA(10, 5)
     with variable_scope.variable_scope("model_two"):
         model_two = varma.VARMA(0, 3)
     configuration = state_space_model.StateSpaceModelConfiguration()
     ensemble = state_space_model.StateSpaceIndependentEnsemble(
         ensemble_members=[model_one, model_two],
         configuration=configuration)
     ensemble.initialize_graph()
     outputs = ensemble.define_loss(features={
         TrainEvalFeatures.TIMES:
         constant_op.constant([[1, 2]]),
         TrainEvalFeatures.VALUES:
         constant_op.constant([[[1.], [2.]]])
     },
                                    mode=estimator_lib.ModeKeys.TRAIN)
     initializer = variables.global_variables_initializer()
     with self.test_session() as sess:
         sess.run([initializer])
         outputs.loss.eval()
예제 #3
0
 def test_ar_smaller(self):
     model = varma.VARMA(autoregressive_order=0, moving_average_order=3)
     model.initialize_graph()
     outputs = model.define_loss(features={
         TrainEvalFeatures.TIMES:
         constant_op.constant([[1, 2]]),
         TrainEvalFeatures.VALUES:
         constant_op.constant([[[1.], [2.]]])
     },
                                 mode=estimator_lib.ModeKeys.TRAIN)
     initializer = variables.global_variables_initializer()
     with self.test_session() as sess:
         sess.run([initializer])
         outputs.loss.eval()
예제 #4
0
 def test_ma_smaller(self):
     model = varma.VARMA(
         autoregressive_order=6,
         moving_average_order=3,
         configuration=state_space_model.StateSpaceModelConfiguration(
             num_features=7))
     model.initialize_graph()
     outputs = model.define_loss(features={
         TrainEvalFeatures.TIMES:
         constant_op.constant([[1, 2]]),
         TrainEvalFeatures.VALUES:
         constant_op.constant([[[1.] * 7, [2.] * 7]])
     },
                                 mode=estimator_lib.ModeKeys.TRAIN)
     initializer = variables.global_variables_initializer()
     with self.test_session() as sess:
         sess.run([initializer])
         outputs.loss.eval()
예제 #5
0
  def __init__(self,
               cycle_num_latent_values,
               moving_average_order,
               autoregressive_order,
               periodicities,
               use_level_noise=True,
               configuration=state_space_model.StateSpaceModelConfiguration()):
    """Initialize the multi-resolution structural ensemble.

    Args:
      cycle_num_latent_values: Controls the model size and the number of latent
          values cycled between (but not the periods over which they cycle).
          Reducing this parameter can save significant amounts of memory, but
          the tradeoff is with resolution: cycling between a smaller number of
          latent values means that only smoother functions can be modeled. For
          multivariate series, may either be a scalar integer (in which case it
          is applied to all periodic components) or a list with length matching
          `periodicities`.
      moving_average_order: The number of moving average coefficients to use,
          which also defines the number of steps after which transient
          deviations revert to the mean defined by periodic and level/trend
          components. Adds to model size.
      autoregressive_order: The number of steps back for
          autoregression. Learning autoregressive coefficients typically
          requires more steps and a smaller step size than other components.
      periodicities: Same meaning as for StructuralEnsemble: number of steps for
          cyclic behavior. Floating point and Tensor values are supported. May
          be a list of values, in which case one component is created for each
          periodicity. If `periodicities` is a list while
          `cycle_num_latent_values` is a scalar, its value is broadcast to each
          periodic component. Otherwise they should be lists of the same length,
          in which case they are paired.
      use_level_noise: See StructuralEnsemble.
      configuration: A StateSpaceModelConfiguration object.
    Raises:
      ValueError: If `cycle_num_latent_values` is neither a scalar nor agrees in
          size with `periodicities`.
    """
    component_model_configuration = configuration._replace(
        use_observation_noise=False)
    univariate_component_model_configuration = (
        component_model_configuration._replace(
            num_features=1))

    adder_part = _replicate_level_trend_models(
        multivariate_configuration=component_model_configuration,
        univariate_configuration=univariate_component_model_configuration)
    with variable_scope.variable_scope("varma"):
      varma_part = varma.VARMA(
          autoregressive_order=autoregressive_order,
          moving_average_order=moving_average_order,
          configuration=component_model_configuration)

    cycle_parts = []
    if periodicities is None:
      periodicities = []
    periodicity_list = nest.flatten(periodicities)
    latent_values_list = nest.flatten(cycle_num_latent_values)
    if len(periodicity_list) != len(latent_values_list):
      if len(latent_values_list) != 1:
        raise ValueError(
            ("`cycle_num_latent_values` must either be a list with the same "
             "size as `periodicity` or a scalar. Received length {} "
             "`cycle_num_latent_values`, while `periodicities` has length {}.")
            .format(len(latent_values_list), len(periodicity_list)))
      latent_values_list *= len(periodicity_list)
    for cycle_number, (cycle_periodicity, num_latent_values) in enumerate(
        zip(periodicity_list, latent_values_list)):
      with variable_scope.variable_scope("cycle{}".format(cycle_number)):
        cycle_features = []
        for feature in range(configuration.num_features):
          with variable_scope.variable_scope("feature{}".format(feature)):
            cycle_features.append(
                periodic.ResolutionCycleModel(
                    num_latent_values=num_latent_values,
                    periodicity=cycle_periodicity,
                    configuration=univariate_component_model_configuration))
        cycle_parts.append(
            state_space_model.StateSpaceCorrelatedFeaturesEnsemble(
                ensemble_members=cycle_features,
                configuration=component_model_configuration))

    super(MultiResolutionStructuralEnsemble, self).__init__(
        ensemble_members=[adder_part, varma_part] + cycle_parts,
        configuration=configuration)