
% Fig 3
clear all;
load('FH_estimates_pi_12_1_1.mat');

% For honesty, we should ensure our inner products are within
% the correct bounds by cosine rule for all estimates
% (Note that the code for NR, CVE, Secant already accounts for this)

est_mat_orig(est_mat_orig > 1) = 1;
est_mat_orig(est_mat_orig < -1) = -1;

CV_values_mat(CV_values_mat > 1) = 1;
CV_values_mat(CV_values_mat < -1) = -1;
		
est_mat_empirical_CV(est_mat_empirical_CV > 1) = 1;
est_mat_empirical_CV(est_mat_empirical_CV < -1) = -1;

kvec = 1:100;
semilogy(kvec,mean((est_mat_orig - true_IP).^2), 'k', 'DisplayName', 'Baseline Estimate'); hold all
		
plot(kvec,mean((est_mat_NR - true_IP).^2), 'b-o', 'DisplayName', 'MLE-NR');
plot(kvec,mean((est_mat_sec - true_IP).^2), 'm-+', 'DisplayName', 'MLE-Secant');
plot(kvec,mean((est_mat_CV - true_IP).^2), 'r-*', 'DisplayName', 'CV-EM');

% CV after using 1 update step is equivalent to using initial
% estimate of sufficient statistic ; corresponding to the
% first step after initial estimate
plot(kvec,mean((squeeze(CV_values_mat(:,2,:)) - true_IP).^2), 'r-.', 'DisplayName', 'CV-Initial');
plot(kvec,mean((est_mat_empirical_CV - true_IP).^2), 'b-x', 'DisplayName', 'CV-Empirical');

xlim([1,100])

title(['Plot of $\|\vec{x}_1\|^2 = \|\vec{x}_2\|^2$ with $\theta = \frac{\pi}{12}$'], 'interpreter','latex','FontWeight', 'bold','FontSize', 70, 'FontName', 'Arial Black');

ylabel('    Mean Squared Error', 'FontWeight', 'bold','FontSize', 100, 'FontName', 'Arial Black');
xlabel('      Number of observations k = 1:100', 'FontWeight', 'bold','FontSize', 100, 'FontName', 'Arial Black');

% Legend at top right

lgd = legend('-DynamicLegend', 'location', 'northeast');
lgd.FontSize = 50;
lgd.LineWidth = 2; 
grid on;
b = gca;
b.FontSize = 50;



% Fig 4
clear all;
load('FH_estimates_pi_12_1_1.mat');
% Set estimates of inner product to be within bounds via cosine rule
est_mat_orig(est_mat_orig > 1) = 1;
est_mat_orig(est_mat_orig < -1) = -1;

CV_values_mat(CV_values_mat > 1) = 1;
CV_values_mat(CV_values_mat < -1) = -1;
		
est_mat_empirical_CV(est_mat_empirical_CV > 1) = 1;
est_mat_empirical_CV(est_mat_empirical_CV < -1) = -1;

k = 20;
% Set estimates of inner product to be within bounds via cosine rule
% of initial estimate, and CV-init, and CV-emp 


boxplot([est_mat_orig(:,k) , est_mat_NR(:,k), est_mat_sec(:,k), est_mat_CV(:,k), squeeze(CV_values_mat(:,2,k)),est_mat_empirical_CV(:,k)],'Labels',{'FH','MLE-NR', 'MLE-Secant', 'CV-EM', 'CV-Init', 'CV-Emp'})
grid on;
b = gca; 
b.XAxis.FontSize = 50; 
b.YAxis.FontSize = 50; 

title('      Estimates at k = 20', 'FontSize', 70, 'FontWeight', 'bold','FontName', 'Arial Black')
ylabel('        Inner Product Estimates', 'FontWeight', 'bold', 'FontSize', 50,'FontName', 'Arial Black');
line(xlim, [true_IP true_IP], 'Color', 'blue', 'LineWidth', 2,'LineStyle', ':');


% Fig 5

clear all;
load('FH_estimates_pi_12_1_1.mat');

figure;
hold on;
grid on;
for j = 1:5
	index = (j-1)*5 + 1;
	boxplot(NR_iter_mat(:,j*20), 'Positions', index, 'Colors', 'k');
end

for j = 1:5
	index = (j-1)*5 + 2;
	boxplot(sec_iter_mat(:,j*20), 'Positions', index,'Colors', 'g');
end

for j = 1:5
	index = (j-1)*5 + 3;
	boxplot(CV_iter_mat(:,j*20), 'Positions', index, 'Colors', 'b');
end

set(gca, 'XTick', [2 7 12 17 22] ,'XTickLabel', {'k=20', 'k=40', 'k=60', 'k=80', 'k=100'});

xlabel('       Number of observations k', 'FontWeight', 'bold', 'FontSize', 110,'FontName', 'Arial Black');
ylabel('      Update steps', 'FontWeight', 'bold', 'FontSize', 100,'FontName', 'Arial Black');
b = gca; 
b.XAxis.FontSize = 50; 
b.YAxis.FontSize = 50; 
title('             Update steps till convergence', 'FontSize', 60, 'FontWeight', 'bold','FontName', 'Arial Black');

h1 = findobj(gca,'Tag','Box','Color','k');
h2 = findobj(gca,'Tag','Box','Color','g');
h3 = findobj(gca,'Tag','Box','Color','b');

set(findobj(gca, 'Type', 'line', 'Tag', 'Median'), 'LineWidth', 2); 
set(findobj(gca, 'Type', 'line', 'Tag', 'Box'), 'LineWidth', 2);  
set(findobj(gca, 'Type', 'line', 'Tag', 'Upper Whisker'), 'LineWidth', 2);
set(findobj(gca, 'Type', 'line', 'Tag', 'Lower Whisker'), 'LineWidth', 2);

lgd = legend([h1(1), h2(1), h3(1)], {'MLE-NR', 'MLE-Secant', 'CV-EM'}, 'Location', 'northeast');

lgd.FontSize = 50;
lgd.LineWidth = 2; 
