%% Runs intraday RN estimation files - ROBUSTNESS: Smoothing spline
%
% This script processes intraday option prices for noise variance estimation
% using the alternative smoothing spline method (robustness check)
%
% Execution flags (set to 1 to run each stage):
%   runprices      - Stage 1: Intraday option prices -> RN distributions (altsmooth)
%   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/rnProbsBinaryAltSmooth.mat - Binary beliefs from mat_08run_all_matlab_altsmooth
%   Output/rnProbsBinaryNoiseAltSmooth.mat - All-bin binary beliefs (for Stage 4)
%
% Output:
%   Output/rnProbsIntradayAltSmooth.mat - Intraday RN distributions
%   Output/binaryNoise_altsmooth.mat - Noise variance estimates
%   Output/fullBinaryNoise_altsmooth.csv - Raw noise estimates for Stata
%   ../Stata/Input/fullBinaryNoise_altsmooth.csv - Same, in Stata input folder
%   Output/binaryStataNoise_AltSmooth.csv - All-bin beliefs panel for noise adjustment in Stata
%   ../Stata/Input/binaryStataNoise_AltSmooth.csv - Same, in Stata input folder

% Preliminaries
clear;
close all;

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

if runprices==1

    % Run intraday processing (altsmooth method)
    rn_probs_wrap_intraday_altsmooth;

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

    % Save
    save('Output/rnProbsIntradayAltSmooth.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/rnProbsIntradayAltSmooth.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 (altsmooth)
    binary_intraday_altsmooth;

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

    % Run noise export
    full_noise_export_altsmooth;

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

end

%% Export all-bin beliefs panel for Stata

if runexportnoise==1

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

    % Load noise data
    if ~exist('noiseEsts','var')
        load Output/binaryNoise_altsmooth.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_AltSmooth.csv',exportMat);
    csvwrite('../Stata/Input/binaryStataNoise_AltSmooth.csv',exportMat);
    disp('Exported binaryStataNoise_AltSmooth.csv');

end

disp('Intraday altsmooth processing completed.');
