90 lines
2.6 KiB
Python
Executable File
90 lines
2.6 KiB
Python
Executable File
import numpy as np
|
|
from numpy.core.fromnumeric import squeeze
|
|
|
|
def gauss_const(h):
|
|
"""
|
|
Returns the normalization constant for a gaussian
|
|
"""
|
|
return 1/(h*np.sqrt(np.pi*2))
|
|
|
|
def gauss_exp(ker_x, xi, h):
|
|
"""
|
|
Returns the gaussian function exponent term
|
|
"""
|
|
num = - 0.5*np.square((xi- ker_x))
|
|
den = h*h
|
|
return num/den
|
|
|
|
def kernel_function(h, ker_x, xi):
|
|
"""
|
|
Returns the gaussian function value. Combines the gauss_const and
|
|
gauss_exp to get this result
|
|
"""
|
|
const = gauss_const(h)
|
|
gauss_val = const*np.exp(gauss_exp(ker_x,xi,h))
|
|
return gauss_val
|
|
|
|
def weights(bw_manual, input_x, all_input_values ):
|
|
w_row = []
|
|
for x_i in all_input_values:
|
|
ki = kernel_function(bw_manual, x_i, input_x)
|
|
ki_sum = np.sum(kernel_function(bw_manual, all_input_values, input_x))
|
|
w_row.append(ki/ki_sum)
|
|
return w_row
|
|
|
|
|
|
def single_y_pred_gauss(bw_manual, input_x, x_values, y_values):
|
|
w = weights(bw_manual, input_x, x_values)
|
|
y_single = np.sum(np.dot(y_values,w))
|
|
return y_single
|
|
|
|
def epanechnikov_one(h, ker_x, xi):
|
|
"""
|
|
Returns the epanechnikov function value for one element.
|
|
"""
|
|
value = 0.75*(1-np.square((xi-ker_x)/h))
|
|
if (value < 0):
|
|
value = 0
|
|
return value
|
|
|
|
def epanechnikov_list(h, ker_x, xi):
|
|
"""
|
|
Returns the epanechnikov function value for list of elements.
|
|
"""
|
|
value = 0.75*(1-np.square((xi-ker_x)/h))
|
|
value = [0 if i < 0 else i for i in value]
|
|
return value
|
|
|
|
|
|
def weights_epanechnikov(bw_manual, input_x, all_input_values ):
|
|
w_row = []
|
|
for x_i in all_input_values:
|
|
ki = epanechnikov_one(bw_manual, x_i, input_x)
|
|
ki_sum = np.sum(epanechnikov_list(bw_manual, all_input_values, input_x))
|
|
w_row.append(ki/ki_sum)
|
|
return w_row
|
|
|
|
def single_y_pred_epanechnikov(bw_manual, input_x, x_values, y_values):
|
|
w = weights_epanechnikov(bw_manual, input_x, x_values)
|
|
y_single = np.sum(np.dot(y_values,w))
|
|
return y_single
|
|
|
|
def ker_reg(x_values, y_values, bw = 1, ker_fun = 'gauss'):
|
|
"""
|
|
x_values, y_values, bw = 1, ker_fun = 'gauss'
|
|
ker_fun = 'gauss' or 'epanechnikov'
|
|
returns Y_pred
|
|
"""
|
|
if (ker_fun == 'gauss'):
|
|
Y_pred = []
|
|
for input_x in x_values:
|
|
Y_single = single_y_pred_epanechnikov(bw, input_x, x_values, y_values)
|
|
Y_pred.append(Y_single)
|
|
elif (ker_fun == 'epanechnikov'):
|
|
Y_pred = []
|
|
for input_x in x_values:
|
|
Y_single = single_y_pred_gauss(bw, input_x, x_values, y_values)
|
|
Y_pred.append(Y_single)
|
|
else:
|
|
return 0
|
|
return Y_pred |