2017-05-08 20:12:08 +04:00
|
|
|
"""
|
|
|
|
Kernel Density Estimation
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class KernelSmoothing(object):
|
|
|
|
"""Kernel Density Estimation"""
|
2017-07-02 02:42:45 +04:00
|
|
|
def __init__(self,h, method="epanechnikov"):
|
2017-05-08 20:12:08 +04:00
|
|
|
self.h = h
|
|
|
|
self.method = method
|
|
|
|
|
|
|
|
def kernel(self, u):
|
|
|
|
if self.method == "epanechnikov":
|
|
|
|
return (3/4) * (1 - u**2)
|
2017-07-02 02:42:45 +04:00
|
|
|
elif self.method == "gaussian":
|
2017-05-08 20:12:08 +04:00
|
|
|
return 0.5
|
|
|
|
elif self.method == "uniform":
|
|
|
|
return 0.5
|
|
|
|
|
2017-07-02 02:42:45 +04:00
|
|
|
def probability(self, x, data):
|
|
|
|
l = len(data)
|
|
|
|
p = sum([self.kernel((x - k)/self.h) for k in data]) / l*self.h
|
2017-05-08 20:12:08 +04:00
|
|
|
|
|
|
|
return p
|