【问题标题】:Can I calculate the confidence bound of a Prophet model that would contain a certain value?我可以计算包含某个值的 Prophet 模型的置信区间吗?
【发布时间】:2021-07-17 01:03:17
【问题描述】:

我可以使用预测数据帧中的 y-hat 方差、边界和点估计值来计算包含给定值的置信水平吗?

我见过I can change my interval level prior to fitting,但从编程上来说,感觉就像是大量昂贵的试验和错误。 有没有办法仅使用来自模型参数和预测数据框的信息来估计置信区间?

类似:

for level in [.05, .1, .15, ... , .95]:
  if value_in_question in (yhat - Z_{level}*yhat_variance/N, yhat + Z_{level}*yhat_variance/N):
        print 'im in the bound level {level}'
# This is sudo code not meant to run in console 

编辑:工作先知示例

# csv from fbprohets working examples https://github.com/facebook/prophet/blob/master/examples/example_wp_log_peyton_manning.csv

import pandas as pd
from fbprophet import Prophet
import os
df = pd.read_csv('example_wp_log_peyton_manning.csv')
m = Prophet()
m.fit(df)
future = m.make_future_dataframe(periods=30)
forecast = m.predict(future)

# the smallest confidence level s.t. the confidence interval of the 30th prediction contains 9

## My current approach
def __probability_calculation(estimate, forecast, j = 30):
    sd_residuals = (forecast.yhat_lower[j] - forecast.yhat[j])/(-1.28)
    for alpha in np.arange(.5, .95, .01):
        z_val = st.norm.ppf(alpha)
        if (forecast.yhat[j]-z_val*sd_residuals < estimate < forecast.yhat[j]+z_val*sd_residuals):
           return alpha

prob = __probability_calculation(9, forecast)

【问题讨论】:

    标签: time-series confidence-interval facebook-prophet


    【解决方案1】:

    fbprophet 使用 numpy.percentile 方法来估计百分位数,您可以在源代码中看到: https://github.com/facebook/prophet/blob/0616bfb5daa6888e9665bba1f95d9d67e91fed66/python/prophet/forecaster.py#L1448

    这里已经回答了如何反向计算值的百分位数: Map each list value to its corresponding percentile

    根据您的代码示例组合所有内容:

    import pandas as pd
    import numpy as np
    import scipy.stats as st
    from fbprophet import Prophet
    
    url = 'https://raw.githubusercontent.com/facebook/prophet/master/examples/example_wp_log_peyton_manning.csv'
    df = pd.read_csv(url)
       
    # put the amount of uncertainty samples in a variable so we can use it later.
    uncertainty_samples = 1000 # 1000 is the default
    m = Prophet(uncertainty_samples=uncertainty_samples)
    m.fit(df)
    future = m.make_future_dataframe(periods=30)
    
    # You need to replicate some of the preparation steps which are part of the predict() call internals
    tmpdf = m.setup_dataframe(future)
    tmpdf['trend'] = m.predict_trend(tmpdf)
    sim_values = m.sample_posterior_predictive(tmpdf)
    

    sim_values 对象包含每个数据点的 1000 个模拟,置信区间所基于的模拟。

    现在您可以使用任何目标值调用 scipy.stats.percentileofscore 方法

    target_value = 8
    st.percentileofscore(sim_values['yhat'], target_value, 'weak') / uncertainty_samples
    # returns 44.26
    

    为了证明这可以前后运行,您可以获取np.percentile 方法的输出并将其放入scipy.stats.percentileofscore method。 这适用于 4 位小数的精度:

    ACCURACY = 4
    for test_percentile in np.arange(0, 100, 0.5):
        target_value = np.percentile(sim_values['yhat'], test_percentile)
        if not np.round(st.percentileofscore(sim_values['yhat'], target_value, 'weak') / uncertainty_samples, ACCURACY) == np.round(test_percentile, ACCURACY):
            print(test_percentile)
            raise ValueError('This doesnt work')
    

    【讨论】:

    • 感谢您对此进行调查!我添加了一个工作示例,并查看了您添加的链接。
    • 出于好奇,您认为我提出的解决方案是否有分量?还是有缺陷?
    • 很大程度上取决于你用它做什么。我们在谈论什么样的数据?你想达到什么目标?您将如何处理计算出的百分位数?
    • 数据是“事件”之间的一系列到达间隔时间,但出于所有强烈和目的,它可以被视为 IID 预测器。我正在尝试产生一个“信任”指标。有人可能会根据这个模型的结果采取行动,我想要一个可以暗示用户需要多么认真地接受这个预测的指标。例如,如果模型预测(很有可能)下周这些值将低于某个关键阈值,那么他们需要立即采取行动。否则,他们可以在闲暇时采取进一步措施进行调查。另外,我想指出,这将...
    • 感谢您分享进一步的解释。由于有一个固定的阈值,因此检查达到该值的概率确实是有意义的。不过,选择概率阈值可能仍然有点武断。或者:您可以计算下限,例如90% 并在该值低于阈值时触发警报。结果将是相同的:如果存在低价值的现实风险,您将收到警报。所以实际上更多的是偏好提供什么样的输入以及如何解释输出。
    猜你喜欢
    • 2016-10-30
    • 1970-01-01
    • 2019-01-18
    • 2018-12-06
    • 2023-03-11
    • 2013-05-09
    • 2020-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多