t = 0:0.1:10;
%t = 0:0.1:20;
x = 2*sin(10*t)+0.5*t.^2+4;
x = 2*sin(10*t)+0.5*t+4;
%x = 2*rand(size(t))+0.5*t+4;
x = (x - min(x)) / (max(x) - min(x));
x = x';
data              = x(1:end-1);
dataLabels        = x(2:end);
trainDataLength   = round(length(data)*70/100);
TrainingSet       = data(1:trainDataLength);
TrainingSetLabels = dataLabels(1:trainDataLength);
TestSet           = data(trainDataLength+1:end);
TestSetLabels     = dataLabels(trainDataLength+1:end);

options = ' -s 3 -t 2 -c 100 -p 0.001 -h 0';
options = ' -s 4 -t 0 -n 0.5 -c 1';
model   = svmtrain(TrainingSetLabels, TrainingSet, options);

%[predicted_label, accuracy, decision_values] = svmpredict(TestSetLabels, TestSet, model);
[predicted_label, accuracy, decision_values] = svmpredict(TrainingSetLabels, TrainingSet, model);

figure('color','w');
plot(t(1:length(TrainingSetLabels)), TrainingSetLabels, '-b');

f2=figure('color','w');
plot(t(1:length(TrainingSetLabels)), TrainingSetLabels, '-b');
hold on;
plot(t(1:length(TrainingSetLabels)), predicted_label, '-r');
hold on;
f3=figure('color','w');
plot(TrainingSet,TrainingSetLabels,'b.')
hold on;plot(TrainingSet,predicted_label,'r.')

[predicted_label, accuracy, decision_values] = svmpredict(TestSetLabels, TestSet, model);
figure(f2)
plot(t(length(TrainingSetLabels)+1:end-1), TestSetLabels, '-k');
hold on;
plot(t(length(TrainingSetLabels)+1:end-1), predicted_label, '-m');
hold off;
xlim([min(t),max(t)])

figure(f3)
hold on;
plot(TestSet,TestSetLabels,'k.')
hold on;plot(TestSet,predicted_label,'m.')

% not use svm, just use linear regression
b = glmfit(data, dataLabels);
figure('color','w');plot(data,dataLabels,'-')

predicted = b(1) + b(2) * data;
figure('color','w');
plot([1:length(data)],data,'b')
figure('color','w');
plot(dataLabels,'k')
hold on; plot(predicted,'r')
hold on; plot(predicted(2:end),'g')

%%
%% 2-D

t = 0:0.1:10;
%t = 0:0.1:20;
x = 2*sin(10*t)+0.5*t.^2+4;
x = 2*sin(10*t)+0.5*t+4;

x = (x - min(x)) / (max(x) - min(x));
x = x';
data              = [x(1:end-2) x(2:end-1)];
dataLabels        = x(3:end);
trainDataLength   = round(length(data)*70/100);
TrainingSet       = data(1:trainDataLength,:);
TrainingSetLabels = dataLabels(1:trainDataLength);
TestSet           = data(trainDataLength+1:end,:);
TestSetLabels     = dataLabels(trainDataLength+1:end);

options = ' -s 3 -t 2 -c 100 -p 0.001 -h 0';
options = ' -s 4 -t 0 -n 0.5 -c 1';
model   = svmtrain(TrainingSetLabels, TrainingSet, options);

%[predicted_label, accuracy, decision_values] = svmpredict(TestSetLabels, TestSet, model);
[predicted_label, accuracy, decision_values] = svmpredict(TrainingSetLabels, TrainingSet, model);

figure('color','w');
plot(t(1:length(TrainingSetLabels)), TrainingSetLabels, '-b');

f2=figure('color','w');
plot(t(1:length(TrainingSetLabels)), TrainingSetLabels, '-b');
hold on;
plot(t(1:length(TrainingSetLabels)), predicted_label, '-r');
hold on;
f3=figure('color','w');
plot(TrainingSet,TrainingSetLabels,'b.')
hold on;plot(TrainingSet,predicted_label,'r.')

[predicted_label, accuracy, decision_values] = svmpredict(TestSetLabels, TestSet, model);
figure(f2)
plot(t(length(TrainingSetLabels)+1:end-2), TestSetLabels, '-k');
hold on;
plot(t(length(TrainingSetLabels)+1:end-2), predicted_label, '-m');
hold off;
xlim([min(t),max(t)])

figure(f3)
hold on;
plot(TestSet,TestSetLabels,'k.')
hold on;plot(TestSet,predicted_label,'m.')

return;



N = 1000;
M = 1;
t = randn(N,1);

clear r
m = 1:10:100;
for M = m
x = [t];
for ii=1:M-1
    x = [x  t+ii*randn(N,1)/2];
end

x = normalize(x);

% t1 = randn(N,1);
% t2 = randn(N,1);
% x = [t1 t2];
% y = 3*t1 - 5*t2;
y = 2*t + randn(N,1)/2 + 7;

% corrcoef([x y]);
% 
% b= glmfit(x,y);
for ii = 1
    for jj=1
        tic;model = svmtrain(y(1:N/2),x(1:N/2,:),['-s 4 -t 2 -n ' num2str(ii/2) ' -c ' num2str(1)]);toc
        tic;zz=svmpredict(y(N/2+1:end),x(N/2+1:end,:),model);toc
        tmp = corrcoef(zz, y(N/2+1:end));
        r(1+(M-1)/10) = tmp(2);
    end
end

end

w = model.SVs' * model.sv_coef
b = -model.rho

hold on;plot(m, r, 'ro-');xlabel('# of dimension'); ylabel('r')
figure('color','w');plot(m, r, 'ro-');

figure('color','w');plot(x(1:N/2,:), y(1:N/2), 'b.');
hold on;plot(x(N/2+1:end,:), zz, 'r.');
xlabel('x')
ylabel('y')
legend({'training','test'})

%figure('color','w'); plot(zz, y(N/2+1:end), '.'); axis equal;axis square;
%figure('color','w'); plot(zz - y(N/2+1:end), '.')