% Export canonical noise estimates to CSV for Stata (2ppt spec)
%
% Assumes the following variables are in the workspace:
%   From binaryNoise_2ppt.mat: bin, day, exday, noiseEsts, exDatesOwn
%   From rand_intraday_dates.mat: rand_intraday_dates
%
% Output: exportMat with columns:
%   [noiseYear noiseMonth noiseDay noiseExYear noiseExMonth noiseExDay binaryNoise noiseBin]

noiseExp = NaN(size(bin,2),1);
noiseYear = noiseExp;
noiseMonth = noiseExp;
noiseDay = noiseExp;
noiseExYear = noiseExp;
noiseExMonth = noiseExp;
noiseExDay = noiseExp;
bin_t = bin';
day_t = day';
exday_t = exday';
clear day;

for n = 1:length(bin_t)

    bin_n = bin_t(n);
    ind_day = day_t(n);
    ind_exday = exday_t(n);
    day_n = rand_intraday_dates(ind_day);
    exday_n = exDatesOwn(ind_exday);
    noiseYear(n) = year(day_n);
    noiseMonth(n) = month(day_n);
    noiseDay(n) = day(day_n);
    noiseExYear(n) = year(exday_n);
    noiseExMonth(n) = month(exday_n);
    noiseExDay(n) = day(exday_n);
    noiseExp(n) = noiseEsts(ind_day,ind_exday,bin_n,1);

end

noiseBin = bin_t;
binaryNoise = noiseExp;

exportMat = [noiseYear noiseMonth noiseDay noiseExYear noiseExMonth noiseExDay binaryNoise noiseBin];
