CB_regimes_types = ["ric", "key", "avg", "optim"];

%calc and save CB_regimes
for k=1:length(CB_regimes_types)
    dynare_command = sprintf('dynare Hansen2020_NotLinearized_Ramsey.mod -DCB_objective="%s"', CB_regimes_types(k));
    eval(dynare_command);
    fname = "Ramsey_" + CB_regimes_types(k);
    save(fname);
    clearvars -except CB_regimes_types;
end

%Load CB_regimes
CB_regimes = struct();
for k=1:length(CB_regimes_types)
    fname = "Ramsey_" + CB_regimes_types(k);
    CB_regimes.(CB_regimes_types(k)) = load(fname);
    del_command = "delete " + fname + ".mat";
    eval(del_command);
end
clear del_command fname;

%calculate Welfare for regime types
W = struct();
for k=1:length(CB_regimes_types)
    %create beta vector
    betV = zeros(length(CB_regimes.(CB_regimes_types(k)).Y)-2,1);
    for m = 1:length(CB_regimes.(CB_regimes_types(k)).Y)-2
        betV(m) = CB_regimes.(CB_regimes_types(k)).bet^(m-1);
    end
    %calc welfare
    W.(CB_regimes_types(k)) = sum(betV .* CB_regimes.(CB_regimes_types(k)).W_optim_nodisc(2:end-1));
end