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 build_model(Risk_Factors):
    # Seasonality effect if relevant: daily data assumed here, datetime index assumed to be comprised of business days
    day_of_week_effect = sts.Seasonal(
        num_seasons=5,
        observed_time_series=np.array(
            Risk_Factors[Risk_Factors.columns[1]]).astype(np.float32),
        name='day_of_week_effect')

    # effect of each explicative factor
    data = np.array(Risk_Factors[Risk_Factors.columns[2:]]).astype(np.float32)
    other_effects = sts.LinearRegression(design_matrix=tf.reshape(
        data - np.mean(data), (-1, data.shape[1])),
                                         name='other_effects')
    # auto-regressive effect
    autoregressive = sts.Autoregressive(
        order=1,
        observed_time_series=np.array(
            Risk_Factors[Risk_Factors.columns[1]]).astype(np.float32),
        name='autoregressive')

    model = sts.Sum([day_of_week_effect, other_effects, autoregressive],
                    observed_time_series=np.array(
                        Risk_Factors[Risk_Factors.columns[1]]).astype(
                            np.float32))
    return model
def build_model(observed_time_series):
    # trend = sts.LocalLinearTrend(observed_time_series=observed_time_series)
    seasonal_daily = tfp.sts.Seasonal(
        num_seasons=48,
        num_steps_per_season=1,
        observed_time_series=observed_time_series,
        name='day_of_week_effect')
    seasonal_weekly = tfp.sts.Seasonal(
        num_seasons=5,
        num_steps_per_season=48 * 5,
        observed_time_series=observed_time_series,
        name='week_of_year_effect')
    model = sts.Sum([seasonal_daily, seasonal_weekly],
                    observed_time_series=observed_time_series)

    return model
Example #5
0
def build_model(observed_time_series):
    day_of_week_effect = sts.Seasonal(
        num_seasons=7,
        observed_time_series=observed_time_series,
        name='day_of_week_effect')
    month_of_year_effect = sts.Seasonal(
        num_seasons=12,
        num_steps_per_season=num_days_per_month,
        observed_time_series=observed_time_series,
        name='month_of_year_effect')
    autoregressive = sts.Autoregressive(
        order=1,
        observed_time_series=observed_time_series,
        name='autoregressive')
    model = sts.Sum([day_of_week_effect, month_of_year_effect, autoregressive],
                    observed_time_series=observed_time_series)
    return model
Example #6
0
    def build_model(self, day=True, month=True, res=True, ext=False):
        if day:
            self.day_of_week = self.day_of_week_effect()
        if month:
            self.month_of_yr = self.month_of_yr_effect()
        if ext:
            self.external = self.external_effect()
        if res:
            self.residue = self.residue_effect()

        # get rid of None in the list
        list_of_effects = [
            self.day_of_week, self.month_of_yr, self.external, self.residue
        ]

        list_of_effects = list(filter(None.__ne__, list_of_effects))

        self.model = sts.Sum(components=list_of_effects,
                             observed_time_series=self.obs)
Example #7
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 #8
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