#!/bin/sh
## This script executes the testing of spurious matches for the baseline Furfine algorithm
	# I) Generates simulated data for illustration 
	# II) Applies matching algorithm variant
	# III) Conducts resampling exercise
	# IV) Applies matching algorithm to resampled data
	# V) Generates summary results corresponding to key Figures/Tables in paper

echo "Code to replicate Improving Overnight Loan Identification in Payment Systems";        
echo "Note, input files for replication not openly available due to proprietary/classified data";
echo "Requires payments data to be pre-cleaned (i.e. civic / U.S. holidays must be removed)"
echo "Authored by Mark Rempel"


# 1) Apply Furfine Algorithm to Payments Data

bsub -pack original_furfine_jobs
bsub -pack FurfineMatToCSV

# 2) Re-pair payments data for testing
bootstrap_sampling

# 3) Re-pair separately payments data to be used for training of Rempel in test procedure
bootstrap_training

# 4) Apply algorithm to resampled payments
furfine_test_jobs

# 5) Compute the False Discovery rates

Rcall_serial summarize_results_submissions.R

summarize_furfine_test_results

Rcall_serial AggregateResults.R
	# this will output a pdf and three tables corresponding to Figure 1, Table 1 - 3 in the paper

# 6) Unique vs Faux-loan time value bias 

 # load payments data, and Furfine data and restrict to last four months of payments/loan data (ignore the misleading naming of LastQuarter)
 
matlab -nosplash -nodesktop -r "date_base=20110901;load('Inputs/LVTS_2005-2011ordered.mat');lvts(lvts(:,1)>=date_base,:);cd('/home/mfa/remm/Replication/PaperReplicationScripts');dlmwrite('Inputs/LastQuarterPayments.csv',lvts,'precision',14,'delimiter',',');cd('/scratch/remm/LVTS/ImprovingOvernightLoanIdentification');load('FurfineOutput/BaselineOriginalFurfineOutput.mat');Loans=Loans(Loans(:,1)>=date_base,:);cd('/home/mfa/remm/Replication/PaperReplicationScripts');save('FurfineOutput/LastQuarterFurfine.mat','Loans');dlmwrite('FurfineOutput/LastQuarterFurfine.csv', Loans,'precision', 14, 'delimiter', ',');"

Matcall_cmd ../KNN/RempelTrainingModelScript 'Inputs/LastQuarterPayments.csv' ../kNNvariantParameters.txt 'FurfineOutput/LastQuarterFauxLoans' 'FurfineOutput/LastQuarterFurfine.mat'

Rcall_serial ../FurfineAndKNNanalysis.R "FurfineOutput/LastQuarterFurfine.csv FurfineOutput/LastQuarterFauxLoans.csv ValueTimeDistributionLastQuarter 300 300 300 300 13 13 750 4.5 1 1 30 1"


# 8) Output of FX summary Figure

Rcall_serial ../FXexposure.R "FXconfigurationAndSpuriousMatches_2005-2012 Inputs/USDCAN_FX_MinCloseQuotes_2001-2013.RData 0 192564 15 months 2005-01-01 2012-01-01"



