clear all
close all
settings = createSettings();

% ------------------------------------------------------------------------
% Parameters
a = .9;
b = .1;
sigma = .25;
nStep = 1000;
 
% ------------------------------------------------------------------------
% Test runs
[x,y] = createData(nStep,a,b,sigma);
settings.param = [a,b,sigma];
settings.nStep = nStep;
settings.x = x;
settings.y = y;

NMC = 1;
settings.N = 2^13;
settings.silent = true;
settings.resetFigs = false;
essThresholds = .6;
nTH = length(essThresholds);
settings.store = true;

methodLabels = {'SimpleRearrange'};
progressLabels = {''};
nMethod = length(methodLabels);

fncLabels = {'$\phi(x) = x$','$\phi(x) = x^4$','\phi(x) = 1_{x>0}$'};
fncYRanges = {[0 .04],[0 .3],[0, .3]};
nFnc = length(fncLabels);

burnIn = 30;

results = zeros(nMethod,nFnc,settings.nLag,nTH,NMC);
wresults = zeros(nMethod,nFnc,settings.nLag,nTH,NMC);

for meth = 1:nMethod
    
    fprintf('%s\n',methodLabels{meth});
    settings.method = methodLabels{meth};
    
    fprintf('Settings:\n');
    disp(settings);
    
    for th = 1:nTH
        
        fprintf('\tESS threshold %6.2f ',essThresholds(th));
        settings.essThreshold = essThresholds(th);
        divisor = 0;
        
        fprintf('*****');
        for k = 1:NMC
            
            fprintf('\b\b\b\b\b%5i',k);
            
            [X,MU,MUW,ESS,NSTRATA,W] = alphaSMC(settings);
            
        end
        
        fprintf('Done\n');
    end
end

gd = linspace(-3,3);
ngd = length(gd);
H = zeros(ngd,settings.nStep);
for k = 1:settings.nStep
    H(:,k) = hist(X(:,k,end),gd);    
end

% imagesc(1:settings.nStep,gd,H)
set(gca,'ydir','normal');
hold on
% plot(settings.x,'linewidth',3,'color','k')
plot(settings.x,'linewidth',1,'color','k')

% plot(MU{1}(1,:),'linewidth',3,'color','k')
plot(MU{1}(1,:),'linewidth',1,'color','g')

plot(abs(settings.y),'rx');
grid on

figure(2)
plot((settings.x - MU{1}(1,:)))