%% Runs all RN estimation files - ROBUSTNESS: Delta-based bins
%
% This script processes option prices into risk-neutral beliefs using
% delta-based return state bins (robustness check)
%
% Uses the same underlying rnProbs.mat as the main specification;
% bin boundaries are defined by delta surface rather than fixed percentages
%
% Execution flags (set to 1 to run each stage):
%   runbinary        - Stage 1: RN distributions -> Binary belief aggregation (resolving states)
%   runexport        - Stage 2: Binary beliefs -> CSV export for Stata
%   runbinaryallbins - Stage 3: RN distributions -> All-bin binary beliefs (for noise adjustment)
%
% Input data required:
%   Output/rnProbs.mat - RN probability distributions (from mat_01run_all_matlab)
%   Input/set_prices.mat, Input/zero_coupons.mat - Settlement and rate data
%   Input/delta_surface.mat - Delta surface data (date1, days1, delta, impl_strike)
%
% Output:
%   Output/rnProbsBinary_delta.mat - Binary belief aggregates (resolving states, delta bins)
%   Output/rnProbsBinaryNoise_delta.mat - All-bin binary beliefs (for noise adjustment)
%   Output/binaryStata_delta.csv - Export for Stata analysis
%   ../Stata/Input/binaryStata_delta.csv - Same, in Stata input folder

% Preliminaries
clear;
close all;

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

% Execution flags
runbinary = 1;        % Set to 1 to aggregate into binary beliefs (resolving states, delta)
runexport = 1;        % Set to 1 to export CSV for Stata
runbinaryallbins = 1; % Set to 1 to create all-bin binary beliefs (for noise adjustment)

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

%% Convert to binary (delta-based bins, resolving states)

if runbinary==1

    % Load data (reuses main spec rnProbs.mat, loads up option delta data)
    try 
        load Input/delta_surface.mat date1 days1 delta impl_strike; % load existing mat file if it exists
    catch 
        ds = readtable('delta_surface.csv'); % otherwise load csv file and save as mat file
        date1 = datetime(ds.date, 'InputFormat', 'MM/dd/yyyy');
        days1 = ds.days;
        delta = ds.delta;
        impl_strike = ds.impl_strike;
        save('Input/delta_surface.mat', 'date1', 'days1', 'delta', 'impl_strike');
    end
    if ~exist('rnProb','var')
        load('Output/rnProbs.mat')
    end
    if ~exist('set_price','var') || ~exist('zc_rate','var')
        load Input/set_prices.mat;
        load Input/zero_coupons.mat;
    end
    delta_dates = date1;
    delta_dtm = days1;
    delta_strike = impl_strike;
    zc_dates = date4;
    zc_dtm = dtm;

    % Run
    binary_probs_delta;

    % Save
    clear rnProb;
    save('Output/rnProbsBinary_delta.mat','-v7.3');

end

%% Calculate preliminary statistics and export to csv for estimation in Stata

if runexport==1

    % Load data
    if ~exist('binaryProbs','var')
        load Output/rnProbsBinary_delta.mat;
    end

    % Run
    binary_to_stata;

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

end

%% Convert RN distributions to all-bin binary beliefs (for noise adjustment in Stata)

if runbinaryallbins==1

    % Load data (reuses main spec rnProbs.mat)
    load('Output/rnProbs.mat')
    load Input/set_prices.mat;
    load Input/zero_coupons.mat;
    load Input/delta_surface.mat date1 days1 delta impl_strike;
    zc_dates = date4;
    zc_dtm = dtm;
    delta_dates = date1;
    delta_dtm = days1;
    delta_strike = impl_strike;
    zc_dates = date4;
    zc_dtm = dtm;

    % Run all-bin binary processing (delta bins)
    binary_probs_noise_delta;

    % Save
    save('Output/rnProbsBinaryNoise_delta.mat','-v7.3');

end
