%% Master Script: Option Price Processing and Simulations
%
% This script orchestrates all Matlab processing of option prices into
% RN beliefs and noise variance estimates for both the main specification
% and robustness checks, plus model simulations. Run this Matlab code first
% to process the data before running the Stata code to analyze the
% processed data and output tables. The bulk of the data included with the 
% replication package is pseudo-data; see the readme for information on 
% obtaining the data used for the paper.
%
% Each empirical specification uses 2 scripts:
%   mat_XX[_spec]          - Daily: prices -> RN probs -> binary beliefs -> CSV
%   mat_XX[_spec]_intraday - Intraday: intraday prices -> RN probs -> noise variance estimates -> CSV
%
% Empirical specifications:
%   Main:    5 percentage point return bins (Tables I-III)
%   Alt.:    2 pp return bins (Table CI row 1), delta-based bins (row 2),
%            smoothing spline (row 3), smoothing spline + GEV tails (row 4)
%
% Index volatility (Table V):
%   Exports 2pp full histograms for S_alt index construction
%
% Simulations (Table IV):
%   GARCH:  CHJ GARCH option pricing model (mat_12garch_sim.m)
%   SVCJ:   Stochastic volatility with correlated jumps (mat_13svcj_sim.m)
%
% Info:
%   Navigate in Matlab to the top-level folder (i.e., the folder containing
%   this master script). Set desired specification flags to 1 in the
%   configuration block below. For each script, set the execution flags
%   (runprices, runbinary, runexport, etc.) within the script file itself.
%
% Output:
%   CSV files in ../Stata/Input/ for Stata analysis
%
% Data requirements:
%   Raw OptionMetrics and CBOE data files in Input/ folder
%   (The package contains pseudo-data versions of these files)

clear;
close all;

%% Configuration

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

% Which specifications to run (set to 1 to include)
run_main = 1;           % main 5pp specification
run_2ppt = 1;           % robustness: 2pp bins
run_delta = 1;          % robustness: delta-based bins
run_altsmooth = 1;      % robustness: smoothing spline
run_altsmooth2 = 1;     % robustness: smoothing spline + GEV tails

% Table V: Index volatility analysis (uses 2pp non-binary data)
run_indexvol = 1;       % Export 2pp full histograms for index volatility analysis

% Simulations (set to 1 to include)
run_garch = 0;          % CHJ GARCH model simulations (Table IV, part 1)
run_svcj = 0;           % SVCJ model simulations (Table IV, part 2)

% Data parameters: determine last expiration through 2018 from the data
max_yr = 2018;
opts = detectImportOptions('option_prices.csv');
opts.SelectedVariableNames = {'exdate'};
opts = setvaropts(opts, 'exdate', 'InputFormat', 'MM/dd/yyyy');
T = readtable('option_prices.csv', opts);
exdates_all = sort(unique(T.exdate));
last_ex = find(exdates_all <= datetime(max_yr, 12, 31), 1, 'last');
fprintf('Detected %d expirations through %d (last_ex = %d)\n', last_ex, max_yr, last_ex);
clear T opts exdates_all max_yr;

% Save setup params for later use
save('Input/params.mat');


%% MAIN SPECIFICATION (5pp bins)

if run_main
    disp('========================================');
    disp('Running MAIN SPECIFICATION (5pp bins)');
    disp('========================================');

    % Stage 1: Prices -> RN probs -> binary beliefs -> all-bin beliefs -> CSV
    mat_01run_all_matlab;

    % Stage 2: Intraday prices -> RN probs -> noise variance estimates -> CSV
    mat_02run_all_matlab_intraday;

    disp('Main specification complete.');
end

%% ROBUSTNESS 1: 2pp Bins
load('Input/params.mat');
if run_2ppt
    disp('========================================');
    disp('Running ROBUSTNESS: 2pp bins');
    disp('========================================');

    % Stage 1: Binary beliefs with 2pp bins (reuses main rnProbs.mat)
    mat_03run_all_matlab_2ppt;

    % Stage 2: Intraday noise estimation + noise export
    mat_04run_all_matlab_intraday_2ppt;

    disp('2pp specification complete.');
end

%% TABLE V: Index Volatility Analysis
% Exports full 2pp histograms (non-binary) for computing S_alt index
load('Input/params.mat');
if run_indexvol
    disp('========================================');
    disp('Running INDEX VOLATILITY export (Table V)');
    disp('========================================');

    % Export 2pp full histogram data to CSV for Stata
    % Requires: Output/rnProbsBinary_2ppt.mat (from run_2ppt)
    mat_05run_all_matlab_indexvol;

    disp('Index volatility export complete.');
end

%% ROBUSTNESS 2: Delta-based Bins
load('Input/params.mat');
if run_delta
    disp('========================================');
    disp('Running ROBUSTNESS: Delta-based bins');
    disp('========================================');

    % Stage 1: Binary beliefs with delta bins (reuses main rnProbs.mat)
    mat_06run_all_matlab_delta;

    % Stage 2: Intraday noise estimation + noise export
    mat_07run_all_matlab_intraday_delta;

    disp('Delta specification complete.');
end

%% ROBUSTNESS 3: Smoothing Spline
load('Input/params.mat');
if run_altsmooth
    disp('========================================');
    disp('Running ROBUSTNESS: Smoothing spline');
    disp('========================================');

    % Stage 1: Altsmooth prices -> RN probs -> binary beliefs
    mat_08run_all_matlab_altsmooth;

    % Stage 2: Intraday noise estimation + noise export
    mat_09run_all_matlab_intraday_altsmooth;

    disp('Altsmooth specification complete.');
end

%% ROBUSTNESS 4: Smoothing Spline + GEV Tails
load('Input/params.mat');
if run_altsmooth2
    disp('========================================');
    disp('Running ROBUSTNESS: Smoothing spline + GEV');
    disp('========================================');

    % Stage 1: Altsmooth2 prices -> RN probs -> binary beliefs
    mat_10run_all_matlab_altsmooth2;

    % Stage 2: Intraday noise estimation + noise export
    mat_11run_all_matlab_intraday_altsmooth2;

    disp('Altsmooth2 (GEV) specification complete.');
end

%% SIMULATIONS: CHJ GARCH Model (Table IV, part 1)
load('Input/params.mat');
if run_garch
    disp('========================================');
    disp('Running CHJ GARCH SIMULATIONS');
    disp('========================================');

    % Set resimulate/estimate_true flags inside mat_12garch_sim.m before running
    % Output: Output/garch_output.mat
    mat_12garch_sim;

    disp('CHJ GARCH simulations complete.');
end

%% SIMULATIONS: SVCJ Model (Table IV, part 2)
load('Input/params.mat');
if run_svcj
    disp('========================================');
    disp('Running SVCJ SIMULATIONS');
    disp('========================================');

    % Set resimulate/recombine flags inside mat_13svcj_sim.m before running
    % Requires GPU (or set run_cpu=1 for much slower CPU fallback)
    % Output: Output/svcj_sim_streams.mat
    mat_13svcj_sim;

    disp('SVCJ simulations complete.');
end

%% End

disp('========================================');
disp('PROCESSING COMPLETE');
disp('========================================');
disp('Output CSV files should be in ../Stata/Input/');
disp('Run Stata scripts starting with 01rnSetup.do');
