Answer the question
In order to leave comments, you need to log in
How to use nuclear regression for forecasting?
Hello.
Two independent subtasks. There is data. It is necessary:
1) "Smooth" the row;
2) Predict
Everything using kernel regression.
I perform smoothing using the Nadaraya-Watson formula, the Gaussian kernel. Everything is working.
I predict with the same formula - trouble. All values are equal (in red):
What am I doing wrong?
UPD:
% Код для сглаживания
function [ yy_smooth ] = kernel_smoother( xx, yy, xx_smooth, h )
yy_smooth(1:numel(xx_smooth)) = nan;
kernel = @(x0,x) exp(-(x0-x).^2 / 0.5 / h^2);
n = numel(yy);
% Evaluating yy_smooth
function yhat = y(x0)
numerator = 0;
denominator = 0;
for i = 1:n
ker_x0xi = kernel(x0,xx(i))*yy(i);
numerator = numerator + (ker_x0xi * yy(i));
denominator = denominator + ker_x0xi;
end
yhat = numerator/denominator;
end
% Filling yy_smooth vector
for j = 1:numel(xx_smooth)
yy_smooth(j) = y(xx_smooth(j));
end
end
% --------------------------
% Код для прогнозирования
% 'p' -- порядок регрессионной зависимости, т.е. сколько предыдущих значений используется для прогноза
function [ yy_forecast ] = kernel_forecaster( xx, yy, xx_forecast, h, p )
yy_forecast = [];
for xi = xx_forecast
xx = xx((end-p):end);
yy = yy((end-p):end);
yy_forecast = [...
yy_forecast, ...
kernel_smoother(xx,yy,xi,h)];
xx = [xx, xi];
yy = [yy, yy_forecast(end)];
end
end
% --------------------------
%Само использование
y = [10.64 10.61 10.64 10.73 10.84 10.92 11.04 11.19 11.38 11.54 11.69 11.88];
x = 1:numel(y);
xx_smoothed = 1:0.2:numel(y);
xx_forecast = x(end)+1:1:x(end)+3;
yy_smoothed = kernel_smoother(x, y, xx_smoothed, 0.7);
yy_forecast = kernel_forecaster(x,y,xx_forecast, 0.7, 4);
%Дальше идет простая визуализация
Answer the question
In order to leave comments, you need to log in
Your forecast is clearly not moving, it is frozen at the last value.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question