function mixedup_observations()
% What if one will mix-up the observations? A simple example
% Suppose we have observations (t,x) to make a regression model.
% We choose a model and tune parameters. 
% How can we proove that the model was cosen correcly?
% Let us mixe depended parts x and tune the model again. 
% If the parameters and approximation error are the same the model s wrong.
% It is a joke, but let us think about it.
%
% http://strijov.com

t = [0:0.01:1]';  % regressor
N = length(t);    % number of samples

% assume the linear regression model
a = 1;            % a weight, constant
x = a*t;          % dependent variable
% add sone noise to the model-generated data
econst = 0.5;     % noise constant
e = econst*(rand(N,1)-1/2);
% make a time series 'xe' with the additive niose 'e'
xe=x+e;

% assume we do not know that the model data are linear dependent
% use one of the hypothesis models:
% 1) linear 'f_lin' (it doesn't matter wherer we've got it or not
% 2) affine 'f_afin' 
% 3) or more complex, you can create it, use example codes below

% let us use f_afin
b0=[0.5 0.5];     % vector of the initial parameters 
[b,r,J] = nlinfit(t,xe,@f_afin,b0); % ture 

% recover the series, calculate the error
err = sumsqr(r)/N;

% plot the recovered series
reg_plot(t, x, xe, f_afin(b,t), err, b)

% make 1000 cyclic premutation of the regressor
% how the model will change? 
% how the model error depends on the permutation?
for i=1:1000;
	idxper = randperm(N);	    % generate a permutation
	corc_t = corrcoef([1:N],idxper); % calculate the correllarion
	corlist(i) = abs(corc_t(1,2)); % store the coefficient
	
    xe_perm = xe(idxper);       % permute x
	[b,r,J] = nlinfit(t,xe_perm,@f_afin,b0); % tune the model
    err(i) = sumsqr(r)/N;       % get error and store it
    %close all;
    %reg_plot(t,x,xe_perm,f_afin(b,t),err(i),[]);
    %pause;
end;

%[cor_t, idx] = sort(corlist);
%err = err(idx);

subplot(2,1,2);
plot(corlist,err,'r.');
xlabel('Correlation');
ylabel('Error');
title('Permutations');

function y=f_lin(b,x)
y = b(1)*x;
return

function y=f_afin(b,x)
y = b(1)*x+b(2);
return

function reg_plot(t,x,xe,x1,err,b)
	% plot the series 'xe' with the model series 'x' to recover
    figure;
    subplot(2,1,1);
    hold on;
	plot(t,xe,'b.');
	xlabel('t');
	ylabel('x_e=x(t,e)');
	plot(t,x,'g-');
    plot(t,x1,'r-');
    %title(sprintf('single-criterial regression RMSE=%1.3f, b=%1.3f',err,b));
    title('No permutation');% sprintf('single-criterial regression RMSE=%1.3f, b=%1.3f',err,b));
    legend('model series', 'source series','recovered series');
return 
