Rolling average in pandas using a Gaussian window. Apply manual function in dataframe rolling

Viewed 31

I want to estimate the rolling average of a timeseries B using a Gaussian window.The equation to do this would correspond to enter image description here

I am aware that pandas has a an option for a gaussian window.

For example see Gaussian kernel density smoothing for pandas.DataFrame.resample?

However, I am not sure if it is equivalent to the version of Gaussian averaging that I am interested in using. I have made an effort to write this function as shown bellow. But I am not sure it works as it should. Any suggestions/comments?

def norm_factor_Gauss_window(s, dt):
    
    numer         = np.arange(-3*s, 3*s+dt, dt)
    multiplic_fac = np.exp(-(numer)**2/(2*s**2))
    norm_factor   = np.sum(multiplic_fac)
    window        = len(multiplic_fac)
    
    return window,  multiplic_fac, norm_factor


dt     = 0.1
s      = 10

aa = np.sin(np.linspace(0,2*np.pi,100))+10.2*np.random.rand(100)
df = pd.DataFrame({'x':aa})


window, multiplic_fac, norm_factor= norm_factor_Gauss_window(s, dt)

res2 =(1/norm_factor)*df.rolling(window, center=True).apply(lambda x: (x * multiplic_fac).sum(), raw=True, engine='numba', engine_kwargs= {'nopython': True,  'parallel': True} , args=None, kwargs=None)

So my questions are, In

hrly = pd.Series(hourly[0][344:468]) 
smooth = hrly.rolling(window=5, win_type='gaussian', center=True).mean(std=0.5)
  1. Is the win_type='gaussian' going to give me the desired result?
  2. What is the role of std=0.5?
  3. If I wanted to do this using .apply() and use a manual function, how should the function be like?
0 Answers
Related