# Nonparametric Regression and Gaussian Random Paths

Posted 2015.12.23 01:11

Nonparametric Regression (kernel regression)

: alphabar = (Kxz'*Kxz + 1E-4*eye(length(xu)))\Kxz'*Y;

[Change 1E-4 to different numbers.]

ccc

global click_pos flag key_pressed

click_pos = [0 0]; key_pressed = ''; flag = 'plot';

xtest = linspace(0, 10, 100)';

ytest = zeros(100, 1);

xu    = linspace(0, 10, 5)'; % Inducing points

kfun  = @(X1, X2)(kernel_se(X1, X2, struct('g2', 1, 'A', 1/(2)^2, 'w2', 0E-6, 'mzflag', 0)));

fig = figure(1);

set(fig, 'WindowButtonDownFcn', @buttonDownCallback,'KeyPressFcn', @keyDownListener ...

, 'Position', [200 200 800 500], 'NumberTitle', 'off', 'Name', 'Kernel Regression');

ppos = zeros(1E3, 2); nrppos = 0;

while ~isequal(flag, 'terminate') && ishandle(fig)

% Click handler

switch flag

case 'normal' % left click

nrppos = nrppos + 1;

ppos(nrppos, :) = click_pos;

flag = 'update';

case 'alt'

if nrppos > 0, nrppos = nrppos -1; end;

flag = 'update';

end

% Find Nonlinear Function

if isequal(flag, 'update')

X        = ppos(1:nrppos, 1);

Y        = ppos(1:nrppos, 2);

Kxz      = kfun(X, xu);

alphabar = (Kxz'*Kxz + 1E-4*eye(length(xu)))\Kxz'*Y;

Ktest    = kfun(xtest, xu);

ytest    = Ktest*alphabar;

flag = 'plot';

end

% Plot

if isequal(flag, 'plot')

clf; hold on;

hp = plot(ppos(1:nrppos, 1), ppos(1:nrppos, 2), 'bo', 'MarkerSize', 15, 'LineWidth', 2);

plot(xtest, ytest, 'k', 'LineWidth', 3);

xlabel('x (input)', 'FontSize', 13); ylabel('f(x) (output)', 'FontSize', 13);

axis equal; axis([0 10 -3 3]); grid on;

title(sprintf('Kernel Regression with %d Positions', nrppos));

drawnow;

flag = 'waiting';

end

if isequal(key_pressed, 'q')

flag = 'terminate';

else

key_pressed = '';

end

pause(1E-10);

end

fprintf(2, 'Terminated. \n');

Gaussian Random Paths (sampling a diverse set of paths given waypoints)

ccc

global click_pos flag key_pressed

click_pos = [0 0]; key_pressed = ''; flag = 'plot';

fig = figure(1);

set(fig, 'WindowButtonDownFcn', @buttonDownCallback,'KeyPressFcn', @keyDownListener ...

, 'Position', [200 200 800 500], 'NumberTitle', 'off', 'Name', 'Gaussian Random Paths');

ppos = zeros(1E3, 2); nrppos = 0; emsec = 0; nrPath = 0;

while ~isequal(flag, 'terminate') && ishandle(fig)

% Click handler

switch flag

case 'normal' % left click

nrppos = nrppos + 1;

ppos(nrppos, :) = click_pos;

flag = 'update';

case 'alt'

if nrppos > 0, nrppos = nrppos -1; end;

flag = 'update';

end

% Find connecting GRP

if isequal(flag, 'update')

xy = ppos(1:nrppos, :);

if nrppos >= 2

ts = get_ts(xy); xs = xy(:, 1); ys = xy(:, 2);

testInputs = linspace(0, ts(end), 50)';

kfun   = @(X1, X2)(kernel_se(X1, X2, struct('g2', 5^2, 'A', 1/(5)^2, 'w2', 0, 'mzflag', 0)));

nrPath = 1000;

iclk = clock;

[xPaths, xMeanPath] = grp(ts, xs, testInputs, kfun, nrPath);

[yPaths, yMeanPath] = grp(ts, ys, testInputs, kfun, nrPath);

emsec = etime(clock, iclk)*1000;

end

flag = 'plot';

end

% Plot

if isequal(flag, 'plot')

clf; hold on;

if nrppos >= 2

colors = copper(nrPath);

for i = 1:nrPath

plot(xPaths(i, :), yPaths(i, :), '-', 'Color', colors(i, :));

end

plot(xMeanPath, yMeanPath, 'b--', 'LineWidth', 3);

end

if nrppos > 0, hp = plot(xy(:, 1), xy(:, 2), 'bo', 'MarkerSize', 11, 'LineWidth', 2); end

xlabel('X', 'FontSize', 13); ylabel('Y', 'FontSize', 13);

axis equal; axis([0 10 0 5]); grid on; title(sprintf('Sampled %d paths in %.1fms', nrPath, emsec));

drawnow;

flag = 'waiting';

end

if isequal(key_pressed, 'q')

flag = 'terminate';

else

key_pressed = '';

end

pause(1E-10);

end

fprintf(2, 'Terminated. \n');

Matlab code

더보기

#### 'Enginius > Matlab' 카테고리의 다른 글

 Highway Simulator  (0) 2016.02.23 2016.01.15 2015.12.23 2015.07.05 2015.06.23 2015.04.09
« PREV : 1 : ··· : 5 : 6 : 7 : 8 : 9 : 10 : 11 : 12 : 13 : ··· : 68 : NEXT »