【学习笔记-时间序列预测】prophet-使用.2变化点检测、趋势灵活性调整

1.变化点自动检测

指定增长速率可变的点来检测变化点,先对速率变化幅度进行L1正则化。默认情况下,Prophet 指定了 25 个变化点,均匀地放置在时间序列的前 80% 中。 图中的垂直线表示变化点的位置。

可以使用参数 n_changepoints 设置潜在变化点的数量,通过调整正则化可以更好地调整。 变化点的位置可以通过以下方式可视化

>>> import pystan
>>> import fbprophet
>>> import pandas as pd
>>> from fbprophet import Prophet
>>> df=pd.read_csv('D:\Machine learning\prophetmain\example_wp_log_peyton_manning.csv')
>>> m = Prophet()
>>> m.fit(df)
>>> future = m.make_future_dataframe(periods=1826)
>>> future['cap'] = 8.5
>>> forecast = m.predict(future)
>>> fig = m.plot(forecast)
>>> a = add_changepoints_to_plot(fig.gca(), m, forecast)
>>> fig.show()

【学习笔记-时间序列预测】prophet-使用.2变化点检测、趋势灵活性调整_第1张图片

默认情况下,仅在时间序列的前 80% 推断变化点,以便有足够的测试集用于预测未来趋势并避免过拟合。 此默认值适用于许多情况,但有部分情况需要使用 changepoint_range 参数进行更改。

m = Prophet(changepoint_range=0.9)

2.调整趋势灵活性(trend flexibility)

如果趋势变化过拟合(灵活性太大)或欠拟合(灵活性不够),可以使用输入参数 changepoint_prior_scale 调整稀疏先验的强度。

默认情况下,此参数设置为 0.05。

欠拟合时,增加该参数使趋势更加灵活:

>>> m = Prophet(changepoint_prior_scale=0.5)
>>> forecast = m.fit(df).predict(future)
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
Initial log joint probability = -130.381
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
      99       5132.68     0.0184527       199.828           1           1      118
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
     199       5147.36    0.00950524       88.0854           1           1      234
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
     299       5160.14     0.0409634       338.996           1           1      348
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
     399       5170.29     0.0032085       123.759           1           1      457
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
     499       5180.91     0.0159526       142.705      0.2658      0.8149      572
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
     599       5188.55       0.42206       238.495       1.457      0.1457      688
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
     699       5190.65    0.00254006       50.7216      0.3708      0.3708      811
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
     799       5192.52   0.000800588       63.0824      0.2513           1      923
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
     899       5193.26    0.00204647       36.7975      0.4506           1     1034
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
     994        5193.7  5.48253e-005       21.1011  1.232e-006       0.001     1185  LS failed, Hessian reset
     999       5193.75   0.000417762       35.5305      0.2677      0.2677     1192
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    1099       5194.12   0.000417867       8.15818      0.4978      0.4978     1313
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    1199       5194.38    0.00264916       42.4243      0.9648     0.09648     1433
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    1272       5195.26   0.000114039       51.6952   1.56e-006       0.001     1561  LS failed, Hessian reset
    1299       5195.41    0.00323062       32.1101           1           1     1594
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    1399       5195.66   0.000597172       27.5909      0.5719      0.5719     1715
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    1468       5195.79   0.000167234        23.025   1.15e-005       0.001     1840  LS failed, Hessian reset
    1499        5195.9   0.000499053       13.1386           1           1     1874
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    1571       5195.96  6.84819e-005       28.4726  2.269e-006       0.001     2011  LS failed, Hessian reset
    1599       5195.97   0.000134186       6.89436       2.952      0.2952     2053
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    1651       5196.18   0.000347281       26.5291  8.446e-006       0.001     2159  LS failed, Hessian reset
    1699       5196.34    0.00131928       22.5198           1           1     2217
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    1799       5196.42   0.000475465       5.55345           1           1     2335
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    1899       5196.52  5.05212e-005        15.869      0.2645     0.02645     2458
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    1999       5196.56    0.00201825       11.7838           1           1     2578
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    2099       5196.75     0.0171565       20.3801           1           1     2693
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    2199       5196.94   0.000438989       40.5423           1           1     2813
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    2299       5196.98   0.000233098       8.74493           1           1     2936
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    2322       5196.98  5.07452e-005       17.0471  6.092e-006       0.001     3031  LS failed, Hessian reset
    2399          5197    0.00161251       5.31807           1           1     3122
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
    2441       5197.01  4.59723e-005       5.32003      0.4935      0.4935     3184
Optimization terminated normally:
  Convergence detected: relative gradient magnitude is below tolerance
>>> fig = m.plot(forecast)
>>> fig.show()

【学习笔记-时间序列预测】prophet-使用.2变化点检测、趋势灵活性调整_第2张图片

 过拟合时,减小该参数

>>> m = Prophet(changepoint_prior_scale=0.001)
>>> forecast = m.fit(df).predict(future)
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
Initial log joint probability = -130.381
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
      99       4602.09  3.56568e-006       4040.87       0.712       0.712      130
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
     163       4602.61  6.51973e-009       4651.41      0.1156      0.1156      204
Optimization terminated normally:
  Convergence detected: absolute parameter change was below tolerance
>>> fig = m.plot(forecast)
>>> fig.show()

【学习笔记-时间序列预测】prophet-使用.2变化点检测、趋势灵活性调整_第3张图片

 3.手动确定变化点位置

使用 changepoints 参数手动指定潜在变化点的位置,只允许在这些点更改斜率,并使用与以前相同的稀疏正则化。自动创建一个点网格,然后用一些已知可能发生变化的特定日期来扩充该网格。 变更点可以完全限制为一小组日期

>>> m = Prophet(changepoints=['2014-01-01'])
>>> forecast = m.fit(df).predict(future)
INFO:fbprophet:Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
Initial log joint probability = -130.381
    Iter      log prob        ||dx||      ||grad||       alpha      alpha0  # evals  Notes
      38       4882.63  1.85072e-005        1.0142      0.3788           1       47
Optimization terminated normally:
  Convergence detected: relative gradient magnitude is below tolerance
>>> fig = m.plot(forecast)

【学习笔记-时间序列预测】prophet-使用.2变化点检测、趋势灵活性调整_第4张图片

 

你可能感兴趣的:(时间序列分析,机器学习,python,时序模型)