clear;

%% Data

rng(2022);

T = 2;
nSteps_vec = 2.^[5, 7, 9];
nTrajectories_vec = [20, 200, 2000];
x = 1;
y = 2;

%% Brownian bridge

for i = 1:length(nSteps_vec)
    nSteps = nSteps_vec(i);
    dt = T/nSteps;
    tVec = 0:dt:T;
    for j = 1:length(nTrajectories_vec)       
        nTrajectories = nTrajectories_vec(j); 
        W = BM(T, nSteps, nTrajectories);
        X = x + W - (x + W(:,end) - y)*(tVec/T);
        figure;
        plot(tVec, X, 'color', 0.75*ones(1, 3));
        xlabel('t', 'Fontsize', 20);
        title(['{\Delta}t = ', num2str(dt), ', ', num2str(nTrajectories), ' sample paths'], 'Fontsize', 20);
        hold on;
        plot(tVec, mean(X), 'r', 'Linewidth', 2);
        plot(tVec, x - (tVec/T)*(x - y), 'g', 'Linewidth', 2);
        set(gca, 'FontSize', 20);
        saveas(gcf, ['figures/exercise4X_', num2str(i), num2str(j), '.png']);
    end
end