%% Runs intraday RN estimation files - ROBUSTNESS: 2pp bins
%
% This script processes intraday option prices for noise variance estimation
% using 2 percentage point return state bins (robustness check)
%
% Execution flags (set to 1 to run each stage):
%   runprices      - Stage 1: Intraday option prices -> RN distributions
%   runbinary      - Stage 2: Process intraday data for noise variance
%   runexport      - Stage 3: Export raw noise estimates to CSV for Stata
%   runexportnoise - Stage 4: All-bin beliefs panel -> CSV for Stata
%
% Input data required:
%   Output/rnProbsIntraday.mat - Intraday RN distributions (from mat_02)
%   Input/rand_intraday_dates.mat - Pre-drawn random dates that determined which days intraday data were obtained for; used to index into intraday observations
%   Output/rnProbsBinary_2ppt.mat - Binary beliefs from mat_03run_all_matlab_2ppt
%   Output/rnProbsBinaryNoise_2ppt.mat - All-bin binary beliefs (for Stage 4)
%
% Output:
%   Output/rnProbsIntraday.mat - Intraday RN distributions
%   Output/binaryNoise_2ppt.mat - Noise variance estimates
%   Output/fullBinaryNoise_2ppt.csv - Raw noise estimates for Stata
%   ../Stata/Input/fullBinaryNoise_2ppt.csv - Same, in Stata input folder
%   Output/binaryStataNoise_2ppt.csv - All-bin beliefs panel for noise adjustment in Stata
%   ../Stata/Input/binaryStataNoise_2ppt.csv - Same, in Stata input folder

% Preliminaries
clear;
close all;

% Set paths
script_folder = fileparts(which('mat_04run_all_matlab_intraday_2ppt.m'));
cd(script_folder);
addpath(genpath(script_folder));

% Data parameters (set by mat_00master.m)
load('Input/params.mat', 'last_ex');

% Execution flags
runprices = 0;      % Set to 1 to process intraday option prices (should always be 0, since this is run in main mat_02run_all_matlab_intraday.m)
runbinary = 1;      % Set to 1 to calculate noise variances
runexport = 1;      % Set to 1 to export raw noise CSV for Stata
runexportnoise = 1; % Set to 1 to export all-bin beliefs panel for Stata

%% Intraday prices to probabilities

if runprices==1

    % Run intraday processing (shared with main spec)
    rn_probs_wrap_intraday;

    % Re-orient matrices
    exDates = min(exDateMat,[],1)';
    priceDates = min(priceDateMat,[],2);
    setVals = min(setVals,[],1)';

    % Save
    save('Output/rnProbsIntraday.mat','xCdf','xProb','rnProb','priceDateMat','exDateMat','priceDates','exDates','setVals','priceVals','-v7.3');

end

%% Convert to binary and calculate noise (2pp bins)

if runbinary==1

    % Load data
    if ~exist('rnProb','var')
        load('Output/rnProbsIntraday.mat')
    end
    if ~exist('set_price','var') || ~exist('zc_rate','var')
        load Input/set_prices.mat;
        load Input/zero_coupons.mat;
    end
    zc_dates = date4;
    zc_dtm = dtm;

    % Run intraday binary processing and noise estimation (2pp bins)
    binary_intraday_2ppt;

    % Save noise estimates
    save('Output/binaryNoise_2ppt.mat','exDateIndsUse','exDatesOwn','day','exday','bin','noiseEsts','noiseEstMeans','plotmean','jvec','-v7.3');

end

%% Export raw noise estimates to CSV for Stata

if runexport==1

    % Load noise data
    if ~exist('noiseEsts','var')
        load Output/binaryNoise_2ppt.mat;
    end
    load Input/rand_intraday_dates.mat;

    % Run noise export
    full_noise_export_2ppt;

    % Export
    writematrix(exportMat,'Output/fullBinaryNoise_2ppt.csv');
    writematrix(exportMat,'../Stata/Input/fullBinaryNoise_2ppt.csv');
    disp('Exported fullBinaryNoise_2ppt.csv');

end

%% Export all-bin beliefs panel for Stata

if runexportnoise==1

    % Load all-bin binary beliefs
    if ~exist('binaryProbs','var')
        load Output/rnProbsBinaryNoise_2ppt.mat;
    end

    % Load noise data
    if ~exist('noiseEsts','var')
        load Output/binaryNoise_2ppt.mat;
    end
    load Input/rand_intraday_dates.mat;

    % Export all-bin panel for noise adjustment in Stata
    binary_to_stata_noise_2ppt;

    % Export
    csvwrite('Output/binaryStataNoise_2ppt.csv',exportMat);
    csvwrite('../Stata/Input/binaryStataNoise_2ppt.csv',exportMat);
    disp('Exported binaryStataNoise_2ppt.csv');

end

disp('Intraday 2ppt processing completed.');
