% Export all-bin beliefs panel to CSV for Stata (delta spec)
%
% Same as binary_to_stata_noise.m for the delta-based bin specification
%
% Assumes in workspace: binaryProbs, binaryProbStartOfContract,
%   binaryDates, exDates, last_ex, noiseEsts, rand_intraday_dates,
%   rand_indices, exDateIndsUse, twoBinTotalProbs

clear am_exp ans day baseIndex binaryProb binIndexes bothLessThan date4 datesToUse diffFinalEnd dtm exDate exDateIndex finalObservationIndex firstObservationIndex i initPriceDateIndex* opt_dtm opt_ytm pctIndex priceDateIndexes probData returnDiff select_zc_indices startDate startPrice temp_zc_date tmp zc_curve* zc_interp;

dayIndices = sort(rand_indices);

binaryProbsUse = binaryProbs;
binaryProbM = NaN(size(binaryProbsUse));
binaryProbR = NaN(size(binaryProbsUse));
binaryNoise = NaN(size(binaryProbsUse)); % forward-looking noise match — not used in estimation, see full_noise_export
binaryProbMRFull = NaN(size(binaryProbsUse,1),size(binaryProbsUse,2));
binaryProbMFull = NaN(size(binaryProbsUse,1),size(binaryProbsUse,2));
binaryProbRFull = NaN(size(binaryProbsUse,1),size(binaryProbsUse,2));
contractLength = NaN(size(binaryProbsUse,1),size(binaryProbsUse,2));
pi0 = binaryProbStartOfContract;

for t_exp=1:size(binaryProbsUse,1)
    
    bin_inds=1:size(binaryProbsUse,2);
    
    for bin=bin_inds
        
        t=1;
        first=1;
        while t<size(binaryProbsUse,3)
            
            t_next = t+find(~isnan(binaryProbsUse(t_exp,bin,t+1:end)),1,'first');
            if isempty(t_next)
                if first==1
                    contractLength(t_exp,bin) = t-1;
                end
                first=0;
                t_next=t+1;
            end
            % Noise from t_next's date (forward-looking; Stata replaces with same-date from fullBinaryNoise)
            binaryDatePrev = binaryDates(t_exp,t_next);
            indNoise1 = find(rand_intraday_dates==binaryDatePrev);
            indNoise2 = find(exDateIndsUse==t_exp);
            if ~isempty(indNoise1)&&~isempty(indNoise2)
                binaryDateThis = binaryDatePrev;
                exDateThis = exDates(t_exp);
                binThis = bin;
                binaryNoise(t_exp,bin,t) = noiseEsts(indNoise1,indNoise2,bin,1);
            end
            binaryProbM(t_exp,bin,t) = (binaryProbsUse(t_exp,bin,t)-binaryProbsUse(t_exp,bin,t_next))^2;
            binaryProbR(t_exp,bin,t) = binaryProbsUse(t_exp,bin,t_next)*(1-binaryProbsUse(t_exp,bin,t_next))-binaryProbsUse(t_exp,bin,t)*(1-binaryProbsUse(t_exp,bin,t));
            t = t_next;
            
        end
        
        binaryProbMRFull(t_exp,bin) = nansum(binaryProbM(t_exp,bin,:))-nansum(binaryProbR(t_exp,bin,:));
        binaryProbMFull(t_exp,bin) = nansum(binaryProbM(t_exp,bin,:));
        binaryProbRFull(t_exp,bin) = nansum(binaryProbR(t_exp,bin,:));
        
    end
    
end

binaryProbMR = binaryProbM-binaryProbR;

binaryDatesTemp = NaT(size(binaryDates,1),1,size(binaryDates,2));
binaryDatesTemp(:,1,:) = binaryDates;
binaryDates = repmat(binaryDatesTemp,1,size(binaryProbMR,2),1);
binaryMonth = month(binaryDates);
binaryDay = day(binaryDates);
binaryYear = year(binaryDates);
if length(exDates)>last_ex
    exDates = exDates(1:last_ex); 
end
exDates = repmat(exDates,1,size(binaryProbMR,2),size(binaryProbMR,3));
exDateMonth = month(exDates);
exDateDay = day(exDates);
exDateYear = year(exDates);

contractLength = repmat(contractLength,1,1,size(binaryProbMR,3));
pi0 = repmat(pi0,1,1,size(binaryProbMR,3));
bin = 1:size(binaryProbs,2);
bin = repmat(bin,size(binaryProbs,1),1,size(binaryProbs,3));

exportMat = [binaryYear(:) binaryMonth(:) binaryDay(:) exDateYear(:) exDateMonth(:) exDateDay(:) binaryProbM(:) binaryProbR(:) binaryNoise(:) binaryProbMR(:) binaryProbs(:) contractLength(:) pi0(:) bin(:) twoBinTotalProbs(:)];