%% Runs intraday RN estimation files - For noise variance estimation
%
% This script processes intraday option prices for noise variance estimation
% Used to calculate microstructure noise from intraday price variation
%
% Pipeline: Intraday option prices -> RN distributions -> Noise estimates -> CSV
%
% 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:
%   Intraday option price CSVs (UnderlyingOptionsIntervalsQuotes_60sec_YYYY.csv)
%   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.mat - Binary beliefs from mat_01run_all_matlab
%   Output/rnProbsBinaryNoise.mat - All-bin binary beliefs (for Stage 4)
%
% Output:
%   Output/rnProbsIntraday.mat - Intraday RN distributions
%   Output/binaryNoise.mat - Noise variance estimates
%   Output/fullBinaryNoise.csv - Raw noise estimates for Stata
%   ../Stata/Input/fullBinaryNoise.csv - Same, in Stata input folder
%   Output/binaryStataNoise.csv - All-bin beliefs panel for noise adjustment in Stata
%   ../Stata/Input/binaryStataNoise.csv - Same, in Stata input folder

% Preliminaries
clear;
close all;

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

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

% Execution flags
runprices = 1;      % Set to 1 to process intraday option prices
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
    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','indicators_t_T','indicators_index','-v7.3');

end

%% Convert to binary and calculate noise

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
    binary_intraday;

    % Save noise estimates (consolidated save with full variable set)
    save('Output/binaryNoise.mat','exDateIndsUse','exDatesOwn','day','exday','bin','noiseEsts','noiseEstMeans','plotmean','jvec','-v7.3');

end

%% Export noise estimates to CSV for Stata

if runexport==1

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

    % Run noise export
    full_noise_export;

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

end

%% Export all-bin beliefs panel for Stata

if runexportnoise==1

    % Load all-bin binary beliefs (from mat_01run_all_matlab runbinaryallbins stage)
    if ~exist('binaryProbs','var')
        load Output/rnProbsBinaryNoise.mat;
    end

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

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

    % Export to Output and Stata Input folder
    csvwrite('Output/binaryStataNoise.csv',exportMat);
    csvwrite('../Stata/Input/binaryStataNoise.csv',exportMat);
    disp('Exported binaryStataNoise.csv');

end

disp('Intraday processing completed.');
