%% Runs intraday RN estimation files - ROBUSTNESS: Smoothing spline + GEV tails
%
% This script processes intraday option prices for noise variance estimation
% using the alternative smoothing spline + GEV tails method (robustness check)
%
% Execution flags (set to 1 to run each stage):
%   runprices      - Stage 1: Intraday option prices -> RN distributions (altsmooth2)
%   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/rnProbsBinaryAltSmooth2.mat - Binary beliefs from mat_10run_all_matlab_altsmooth2
%   Output/rnProbsBinaryNoiseAltSmooth2.mat - All-bin binary beliefs (for Stage 4)
%
% Output:
%   Output/rnProbsIntradayAltSmooth2.mat - Intraday RN distributions
%   Output/binaryNoise_altsmooth2.mat - Noise variance estimates
%   Output/fullBinaryNoise_altsmooth2.csv - Raw noise estimates for Stata
%   ../Stata/Input/fullBinaryNoise_altsmooth2.csv - Same, in Stata input folder
%   Output/binaryStataNoise_AltSmooth2.csv - All-bin beliefs panel for noise adjustment in Stata
%   ../Stata/Input/binaryStataNoise_AltSmooth2.csv - Same, in Stata input folder

% Preliminaries
clear;
close all;

% Set paths
script_folder = fileparts(which('mat_11run_all_matlab_intraday_altsmooth2.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 (altsmooth2)
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 (altsmooth2)

if runprices==1

    % Run intraday processing (altsmooth2 method)
    rn_probs_wrap_intraday_altsmooth2;

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

    % Save
    save('Output/rnProbsIntradayAltSmooth2.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/rnProbsIntradayAltSmooth2.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 (altsmooth2)
    binary_intraday_altsmooth2;

    % Save noise estimates
    save('Output/binaryNoise_altsmooth2.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_altsmooth2.mat;
    end
    load Input/rand_intraday_dates.mat;

    % Run noise export
    full_noise_export_altsmooth2;

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

end

%% Export all-bin beliefs panel for Stata

if runexportnoise==1

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

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

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

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

end

disp('Intraday altsmooth2 processing completed.');
