jmquintana79
10/21/2016 - 10:15 AM

Smoothing 1D data

Smoothing 1D data

## MOVING AVERAGE SMOOTHING
def smooth_moving_average(df,svar,windows_size):
    import numpy as np
    
    # build windows to be moving
    box = np.ones(windows_size)/windows_size
    # calculate and store y same df
    df["%s_smooth%s"%(svar,windows_size)] = np.convolve(df[svar], box, mode='same')
    # filter the firsts lines
    return df[~df.index.isin(list(df.index[:windows_size]))]

WS= smooth_movingavg(WS,"ws1",3)

## KALMAN FILTER SMOOTHING
def smooth_kalman(df,svar,lag_size):
    from filterpy.kalman import FixedLagSmoother
    import numpy as np
    
    # build object
    fls = FixedLagSmoother(dim_x=2, dim_z=1)
    # settings
    fls.x = np.array([[0.],
                      [.5]])
    fls.F = np.array([[1.,1.],
                      [0.,1.]])
    fls.H = np.array([[1.,0.]])
    fls.P *= 200
    fls.R *= 5.
    fls.Q *= 0.001
    
    # smoothing
    xhatsmooth, xhat = fls.smooth_batch(df[svar].values, N=lag_size)
    df["%s_kalman_batch%s"%(svar,lag_size)] = xhatsmooth[:,0]
    df["%s_kalman_standar%s"%(svar,lag_size)] = xhat[:,0]
    # return
    return df