简体   繁体   中英

Pandas: Using rolling_mean() with maximum information criteria as a Smoothing Function?

I would like to use pd.rolling_mean() as a smoothing function keeping the maximum information criteria. This means the endpoints are computed differently depending on the information available. An example of a window=3, center=True is below:

For Example: Window = 3, Center = True
ts_smooth[0] = 1/2 * ts[0] + 1/2 * ts[1]
ts_smooth[0<n<N-1] = 1/3 * ts[n-1] + 1/3 * ts[n] + 1/3 * ts[n+1]
ts_smooth[N] = 1/2 * ts[N-1] + 1/2 * ts[N]

What is the best way to achieve this in Pandas?

  1. Compute rolling_mean() for midpoints
  2. Write a function to replace the end conditions based on window size?

you could use the shift function, like so,

ts_shiftedPlus = ts.shift(1)
ts_shiftedMinus = ts.shift(-1)

ts_smooth = 1/3 * ts_shiftedMinus + 1/3 * ts + 1/3 * ts_shiftedPlus
ts_smooth.ix[0] = 1/2 * ts.ix[0] + 1/2 * ts.ix[1]
N = len(ts)
ts_smooth.ix[N] = 1/2 * ts.ix[N-1] + 1/2 * ts.ix[N]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM