Exponential Smoothing

Author : Nilesh Chaturvedi

Date Created : 29th March, 2017

Central Idea : f(n+1) = f(n) + alpha* (a(n) - f(n))

where, a(n) : Raw value at nth point

  f(n)  : Smoothed value at nth point

  f(n+1): Smoothed value at (n+1)th point

  alpha : Smoothing rate

In [ ]:
# Import libraries

import csv
import numpy
import matplotlib.pylab as plt
from ipywidgets import *
from IPython.display import display
import ipywidgets as widgets

In [ ]:
# This funnction returns a numpy array of x and y coordinates.

def load_data(filename):

    reader = csv.reader(open(filename, "r"), delimiter = "\t")
    data_list= list(reader)
    x_comp = []
    y_comp = []

    for i in data_list[1:]:
        x_comp.append(i[4])
        y_comp.append(i[5])

    x_comp = numpy.array(x_comp, dtype = numpy.float)
    y_comp = numpy.array(y_comp, dtype = numpy.float)

    return x_comp, y_comp

In [ ]:
# Smooth target data
# Function returns a list.

def filter(vector, smoothing_factor):
    f_next = vector[0]
    
    smoothed_data = []
    
    for point in vector:
        f_present = f_next
        smoothed_data.append(f_next)
        f_next = f_present + smoothing_factor*(point-f_present)
        
    return smoothed_data

In [ ]:
def plot_data(smoothing_value):
    filedata = load_data("testdata.csv")

    x = filter(filedata[0], smoothing_value)
    y = filter(filedata[1], smoothing_value)

    plt.plot(x, y, 'r', linewidth=3, label="Smoothened Data")
    plt.scatter(a[0],a[1],c='g', label = "Raw Data")
    plt.grid()
    plt.legend()
    plt.show()

In [ ]:
i = interact(plot_data, smoothing_value=(0,1, 0.0001))