%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Script to Calculate CH Inequality and For Systematics
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

format long g;

% define data processing constants
SET_SIZE = 16;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%load data file
%data = load('chsh_0_20141124.dat');
%data = load('chsh_180_20141125');
data_1 = load('chsh_h_optimise_20141129.dat');
data_2 = load('chsh_h_optimise_20141201.dat');
data_3 = load('chsh_h_optimise_20141202.dat');
data = [data_1; data_2; data_3];

% evaluate the number of complete set
NO_OF_SET = floor(size(data, 1)/SET_SIZE);

% truncating incomplete set
data(NO_OF_SET*SET_SIZE + 1 :end, :) = [];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% reshaping data 

data_singles_alice = reshape(data(:, 2), 16, NO_OF_SET)';
data_singles_bob = reshape(data(:, 3), 16, NO_OF_SET)';

data_2_fold = reshape(data(:, 4), 16, NO_OF_SET)';

data_2_fold_unnorm = data_2_fold;

data_2_fold_norm = data_2_fold_unnorm./sqrt(data_singles_alice.*data_singles_bob) ;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% plotting histograms

NO_OF_BINS = 50;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% unnormalized

% a0b0++
histfit(data_2_fold_unnorm(:, 1), NO_OF_BINS, 'poisson'),...
    title('a0b0++, Unnormalized');
% a0b0-+
histfit(data_2_fold_unnorm(:, 2), NO_OF_BINS, 'poisson'),...
    title('a0b0-+, Unnormalized');
% a0b0+-
histfit(data_2_fold_unnorm(:, 3), NO_OF_BINS, 'poisson'),...
    title('a0b0+-, Unnormalized');
% a0b0--
histfit(data_2_fold_unnorm(:, 4), NO_OF_BINS, 'poisson'),...
    title('a0b0--, Unnormalized');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% a0b1++
histfit(data_2_fold_unnorm(:, 5), NO_OF_BINS, 'poisson'),...
        title('a0b1++, Unnormalized');
% a0b1-+
histfit(data_2_fold_unnorm(:, 6), NO_OF_BINS, 'poisson'),...
        title('a0b1-+, Unnormalized');
% a0b1+-
histfit(data_2_fold_unnorm(:, 7), NO_OF_BINS, 'poisson'),...
        title('a0b1+-, Unnormalized');
% a0b1--
histfit(data_2_fold_unnorm(:, 8), NO_OF_BINS, 'poisson'),...
        title('a0b1--, Unnormalized');
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
% a1b0++
histfit(data_2_fold_unnorm(:, 9), NO_OF_BINS, 'poisson'),...
            title('a1b0++, Unnormalized');
% a1b0-+
histfit(data_2_fold_unnorm(:, 10), NO_OF_BINS, 'poisson'),...
            title('a1b0-+, Unnormalized');
% a1b0+-
histfit(data_2_fold_unnorm(:, 11), NO_OF_BINS, 'poisson'),...
            title('a1b0+-, Unnormalized');
% a1b0--
histfit(data_2_fold_unnorm(:, 12), NO_OF_BINS, 'poisson'),...
            title('a1b0--, Unnormalized');
        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
% a1b1++
histfit(data_2_fold_unnorm(:, 13), NO_OF_BINS, 'poisson'),...
                title('a1b1++, Unnormalized');
% a1b1-+
histfit(data_2_fold_unnorm(:, 14), NO_OF_BINS, 'poisson'),...
                title('a1b1-+, Unnormalized');
% a0b1+-
histfit(data_2_fold_unnorm(:, 15), NO_OF_BINS, 'poisson'),...
                title('a1b1+-, Unnormalized');
% a1b1--
histfit(data_2_fold_unnorm(:, 16), NO_OF_BINS, 'poisson'),...
                title('a1b1--, Unnormalized');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
% normalized

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
% a0b0++
histfit(data_2_fold_norm(:, 1), NO_OF_BINS, 'normal'),...
    title('a0b0++, Normalized');
% a0b0-+
histfit(data_2_fold_norm(:, 2), NO_OF_BINS, 'normal'),...
    title('a0b0-+, Normalized');
% a0b0+-
histfit(data_2_fold_norm(:, 3), NO_OF_BINS, 'normal'),...
    title('a0b0+-, Normalized');
% a0b0--
histfit(data_2_fold_norm(:, 4), NO_OF_BINS, 'normal'),...
    title('a0b0--, Normalized');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% a0b1++
histfit(data_2_fold_norm(:, 5), NO_OF_BINS, 'normal'),...
        title('a0b1++, Normalized');
% a0b1-+
histfit(data_2_fold_norm(:, 6), NO_OF_BINS, 'normal'),...
        title('a0b1-+, Normalized');
% a0b1+-
histfit(data_2_fold_norm(:, 7), NO_OF_BINS, 'normal'),...
        title('a0b1+-, Normalized');
% a0b1--
histfit(data_2_fold_norm(:, 8), NO_OF_BINS, 'normal'),...
        title('a0b1--, Normalized');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% a1b0++
histfit(data_2_fold_norm(:, 9), NO_OF_BINS, 'normal'),...
        title('a1b0++, Normalized');
% a1b0-+
histfit(data_2_fold_norm(:, 10), NO_OF_BINS, 'normal'),...
        title('a1b0-+, Normalized');
% a1b0+-
histfit(data_2_fold_norm(:, 11), NO_OF_BINS, 'normal'),...
        title('a1b0+-, Normalized');
% a1b0--
histfit(data_2_fold_norm(:, 12), NO_OF_BINS, 'normal'),...
        title('a1b0--, Normalized');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% a1b1++
histfit(data_2_fold_norm(:, 13), NO_OF_BINS, 'normal'),...
        title('a1b1++, Normalized');
% a1b1-+
histfit(data_2_fold_norm(:, 14), NO_OF_BINS, 'normal'),...
        title('a1b1-+, Normalized');
% a1b1+-
histfit(data_2_fold_norm(:, 15), NO_OF_BINS, 'normal'),...
        title('a1b1+-, Normalized');
% a1b1--
histfit(data_2_fold_norm(:, 16), NO_OF_BINS, 'normal'),...
        title('a1b1--, Normalized');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

plot(data_2_fold_norm(:, 1), title('a0b0++'), grid;
plot(data_2_fold_norm(:, 2), title('a0b0-+'), grid;
plot(data_2_fold_norm(:, 3), title('a0b0+-'), grid;
plot(data_2_fold_norm(:, 4), title('a0b0--'), grid;
plot(data_2_fold_norm(:, 5), title('a0b1++'), grid;
plot(data_2_fold_norm(:, 6), title('a0b1-+'), grid;
plot(data_2_fold_norm(:, 7), title('a0b1+-'), grid;
plot(data_2_fold_norm(:, 8), title('a0b1--'), grid;
plot(data_2_fold_norm(:, 9), title('a1b0++'), grid;
plot(data_2_fold_norm(:, 10), title('a1b0-+'), grid;
plot(data_2_fold_norm(:, 11), title('a1b0+-'), grid;
plot(data_2_fold_norm(:, 12), title('a1b0--'), grid;
plot(data_2_fold_norm(:, 13), title('a1b1++'), grid;
plot(data_2_fold_norm(:, 14), title('a1b1-+'), grid;
plot(data_2_fold_norm(:, 15), title('a1b1+-'), grid;
plot(data_2_fold_norm(:, 16), title('a1b1--'), grid;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% correlation functions

ea0b0 = (data_2_fold_unnorm(:, 1) - data_2_fold_unnorm(:, 2) - data_2_fold_unnorm(:, 3) + data_2_fold_unnorm(:, 4))./sum(data_2_fold_unnorm(:, [1:4]), 2);
ea0b1 = (data_2_fold_unnorm(:, 5) - data_2_fold_unnorm(:, 6) - data_2_fold_unnorm(:, 7) + data_2_fold_unnorm(:, 8))./sum(data_2_fold_unnorm(:, [5:8]), 2);
ea1b0 = (data_2_fold_unnorm(:, 9) - data_2_fold_unnorm(:, 10) - data_2_fold_unnorm(:, 11) + data_2_fold_unnorm(:, 12))./sum(data_2_fold_unnorm(:, [8:12]), 2);
ea1b1 = (data_2_fold_unnorm(:, 13) - data_2_fold_unnorm(:, 14) - data_2_fold_unnorm(:, 15) + data_2_fold_unnorm(:, 16))./sum(data_2_fold_unnorm(:, [12:16]), 2);

histfit(ea0b0, NO_OF_BINS, 'normal'),...
    title('ea0b0');
histfit(ea0b1, NO_OF_BINS, 'normal'),...
    title('ea0b1');
histfit(ea1b0, NO_OF_BINS, 'normal'),...
    title('ea1b0');
histfit(ea1b1, NO_OF_BINS, 'normal'),...
    title('ea1b1');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

