%% Runs all RN estimation files - MAIN SPECIFICATION (5pp bins)
%
% This script processes option prices into risk-neutral beliefs for the
% main specification using 5 percentage point return state bins
%
% Pipeline: Option prices -> RN distributions -> Binary beliefs -> Stata CSV
%            Also: RN distributions -> All-bin binary beliefs (for noise adjustment)
%
% Execution flags (set to 1 to run each stage):
%   runprices        - Stage 1: Raw option prices -> RN probability distributions
%   sumcounts        - Summary statistics for data counts
%   runbinary        - Stage 2: RN distributions -> Binary belief aggregation (resolving states)
%   runexport        - Stage 3: Binary beliefs -> CSV export for Stata
%   runbinaryallbins - Stage 4: RN distributions -> All-bin binary beliefs (for noise adjustment in Stata)
%
% Input data required (in Input/ folder):
%   option_prices.mat, index_prices.mat, div_yields.mat, zero_coupons.mat,
%   atmf.mat, set_prices.mat
%
% Output:
%   Output/rnProbs.mat - RN probability distributions
%   Output/rnProbsBinary.mat - Binary belief aggregates (resolving states)
%   Output/rnProbsBinaryNoise.mat - All-bin binary beliefs (for noise adjustment)
%   ../Stata/Input/binaryStata.csv - Export for Stata analysis

% Preliminaries
clear;
close all;

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

% Execution flags
runprices = 1;        % Set to 1 to process raw option prices
sumcounts = 1;        % Set to 1 to print summary counts
runbinary = 1;        % Set to 1 to aggregate into binary beliefs (resolving states)
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');

%% Prices to probabilities

if runprices==1
    
    % Run
    rn_probs_wrap;

    % Re-orient some matrices
    exDates = min(exDateMat,[],1)';
    priceDates = min(priceDateMat,[],2);
    setVals = min(setVals,[],1)';
    
    % Save
    save('Output/rnProbs.mat','xCdf','xProb','rnProb','priceDateMat','exDateMat','priceDates','exDates','setVals','priceVals','optCountPreClean','optCountClean','prices_millions','prices_zeros','indicators_t_T','indicators_index','-v7.3');
    
end

%% Summary counts

if sumcounts==1

    load Output/rnProbs optCountPreClean optCountClean prices_millions prices_zeros priceDates exDates;
    tradeDaysNum = length(~ismissing(priceDates))
    exDaysNum = length(~ismissing(exDates))
    rawObsMillions = prices_millions
    preCleanObsMillions = sum(sum(optCountPreClean))/1000000
    cleanObsMillions = sum(sum(optCountClean))/1000000
    
end

%% Convert to binary

if runbinary==1

    % Load data
    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
    zc_dates = date4;
    zc_dtm = dtm;
    % Note: rnProb is sparse 2D, last_ex cutoff handled in binary_probs via indicators_t_T

    % Run
    binary_probs;

    % Save
    clear rnProb;
    save('Output/rnProbsBinary.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.mat;
    end

    % Run
    binary_to_stata;

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

end

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

if runbinaryallbins==1
    
    % Load RN probability data
    load('Output/rnProbs.mat')
    load Input/set_prices.mat;
    load Input/zero_coupons.mat;
    zc_dates = date4;
    zc_dtm = dtm;

    % Run all-bin binary processing
    binary_probs_noise;

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

end