Exponential Smoothing in Redshift

て烟熏妆下的殇ゞ 提交于 2021-02-11 14:53:28

问题


I'm trying to write a window function to calculate exponential smoothing in Redshift. I am referencing this post (here).

SELECT p.*,
       (sum(power((1/0.8666666), seqnum) * price) over (order by seqnum rows unbounded preceding) +
        first_value(price) over (order by seqnum rows unbounded preceding)
       ) / power((1/.13333333), seqnum+1 )
FROM (SELECT
               date, 
               row_number() over (order by date) - 1 as seqnum,
               price
      FROM table.prices
      ) p; 

The issue is that when the value of the smoothing constant is anything but .5 (which is a 3-day period given by K = Smoothing Constant = 2 / (1 + n)), the equation doesn't compute the values correctly. However, when the values are .5 (so, replace .866666 and .13333 each with .5), it works perfectly as checked in Excel: X = (K * (C - P)) + P. (see this link).

Some notes: Redshift can't do recursive CTEs, so it has to be a Window Function. Normally, I would use R or Python to do this, but for this task it needs to be computed in SQL. Any and all ideas are welcome. Thank you!

来源:https://stackoverflow.com/questions/60982644/exponential-smoothing-in-redshift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!