%% Runs all RN estimation files - ROBUSTNESS: Smoothing spline
%
% This script processes option prices into risk-neutral beliefs using
% an alternative smoothing spline method (robustness check)
%
% Unlike 2ppt/delta specs, this has its own runprices stage because
% the smoothing method differs from the main specification
%
% Execution flags (set to 1 to run each stage):
%   runprices        - Stage 1: Raw option prices -> RN probability distributions (altsmooth)
%   sumcounts        - Summary statistics on 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)
%
% 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/rnProbsAltSmooth.mat - RN probability distributions (altsmooth)
%   Output/rnProbsBinaryAltSmooth.mat - Binary belief aggregates (resolving states)
%   Output/rnProbsBinaryNoiseAltSmooth.mat - All-bin binary beliefs (for noise adjustment)
%   Output/binaryStata_AltSmooth.csv - Export for Stata analysis
%   ../Stata/Input/binaryStata_AltSmooth.csv - Same, in Stata input folder

% Preliminaries
clear;
close all;

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

% Execution flags
runprices = 1;        % Set to 1 to process raw option prices (altsmooth method)
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 (smoothing spline)

if runprices==1

    % Run
    rn_probs_wrap_altsmooth;

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

    % Save
    save('Output/rnProbsAltSmooth.mat','xCdf','xProb','rnProb','priceDateMat','exDateMat','priceDates','exDates','setVals','priceVals','indicators_t_T','indicators_index','optCountPreClean','optCountClean','prices_millions','prices_zeros','-v7.3');

end

%% Summary counts

if sumcounts==1

    load Output/rnProbsAltSmooth 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 (resolving states)

if runbinary==1

    % Load data
    if ~exist('rnProb','var')
        load('Output/rnProbsAltSmooth.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
    binary_probs_altsmooth;

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

    % Run
    binary_to_stata;

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

end

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

if runbinaryallbins==1

    % Load data
    load('Output/rnProbsAltSmooth.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_altsmooth;

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

end
