Example #1
0
 def _build_model(
     self, observed_time_series, trend='local', seasonal_periods=12, ar_order=1
 ):
     if trend == 'semilocal':
         trend = sts.SemiLocalLinearTrend(
             observed_time_series=observed_time_series, name='trend'
         )
     else:
         trend = sts.LocalLinearTrend(
             observed_time_series=observed_time_series, name='trend'
         )
     mods_list = [trend]
     if str(seasonal_periods).isdigit():
         seasonal = tfp.sts.SmoothSeasonal(
             period=seasonal_periods,
             observed_time_series=observed_time_series,
             name='seasonal',
             frequency_multipliers=[1, 2, 3],
         )
         mods_list.append(seasonal)
     if str(ar_order).isdigit():
         autoregressive = sts.Autoregressive(
             order=int(ar_order),
             observed_time_series=observed_time_series,
             name='autoregressive',
         )
         mods_list.append(autoregressive)
     model = sts.Sum(mods_list, observed_time_series=observed_time_series)
     return model
Example #2
0
def build_model(observed_time_series):
    trend = sts.LocalLinearTrend(observed_time_series=observed_time_series)
    seasonal = tfp.sts.Seasonal(num_seasons=12,
                                observed_time_series=observed_time_series)
    model = sts.Sum([trend, seasonal],
                    observed_time_series=observed_time_series)
    return model
Example #3
0
def cal_loss(training_data):
    
    #设置全局默认图形
    tf.reset_default_graph()
    #遵循加法模型,设置趋势
    trend = sts.LocalLinearTrend(observed_time_series=observed_time_series)
    
    #设置季节性
    seasonal = tfp.sts.Seasonal(
          num_seasons=12, observed_time_series=observed_time_series)
    #模型拟合,之所以用sum,而不是我们在建模中常见的fit定义,是因为,
    #模型时间序列为加法模型,有如上文提到的趋势,季节性,周期性等成分相加
    #默认的先验分布为正态(normal)
    ts_model = sts.Sum([trend, seasonal], observed_time_series=observed_time_series)

    #构建变分损失函数和后验
    with tf.variable_scope('sts_elbo', reuse=tf.AUTO_REUSE):
        elbo_loss, variational_posteriors = tfp.sts.build_factored_variational_loss(
          ts_model,observed_time_series=training_data)
    
    return ts_model,elbo_loss,variational_posteriors
Example #4
0
def build_model(observed_time_series):
  trend = sts.LocalLinearTrend(observed_time_series=observed_time_series)
  seasonal = tfp.sts.Seasonal(
      num_seasons=int(len(dataset)/janela), observed_time_series=observed_time_series)
  model = sts.Sum([trend, seasonal], observed_time_series=observed_time_series)
  return model