kerreg-MPSKICB/KernelRegression.py

37 lines
1005 B
Python
Raw Normal View History

2021-05-30 23:26:52 +02:00
import numpy as np
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(bw_manual, input_x, iks, igrek):
w = weights(bw_manual, input_x, iks)
y_single = np.sum(np.dot(igrek,w))
return y_single