Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
TommasoBelluzzo authored Nov 23, 2020
1 parent 9d69833 commit 0208807
Show file tree
Hide file tree
Showing 14 changed files with 323 additions and 62 deletions.
124 changes: 95 additions & 29 deletions ScriptsDataset/analyze_dataset.m
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,27 @@ function plot_crises(ds,id)
cd = ds.CrisisDummies;
k = size(cd,2);

co = get(gca,'ColorOrder');
co = get(gca(),'ColorOrder');
cor = ceil(k / size(co,1));
co = repmat(co,cor,1);

p = gobjects(k,1);

hold on;
for i = k:-1:1
cddn = ds.DatesNum(logical(cd(:,i)));
cddn_max = max(cddn);
cddn_min = min(cddn);

p = patch('XData',[cddn_min cddn_max cddn_max cddn_min],'YData',[0 0 1 1],'EdgeAlpha',0.25,'FaceAlpha',0.40,'FaceColor',co(i,:));
set(p,'Tag',num2str(i));
p(i) = patch('XData',[cddn_min cddn_max cddn_max cddn_min],'YData',[0 0 1 1],'EdgeAlpha',0.25,'FaceAlpha',0.40,'FaceColor',co(i,:));
set(p(i),'Tag',num2str(i));
end
hold off;

ax = gca();

if (k <= 5)
legend(ax,ds.CrisisNames,'Location','southoutside','Orientation','horizontal');
legend(ax,p,ds.CrisisNames,'Location','southoutside','Orientation','horizontal');

tooltips = [];
tooltips_target = [];
Expand All @@ -150,6 +152,7 @@ function plot_crises(ds,id)
end
end

set(ax,'Box','on');
set(ax,'XLim',[ds.DatesNum(1) ds.DatesNum(end)],'XTickLabelRotation',45);
set(ax,'YLim',[0 1],'YTick',[]);

Expand Down Expand Up @@ -189,24 +192,29 @@ function plot_crises(ds,id)

function plot_index(ds,id)

index = ds.Index;
y = ds.Index;

index_obs = numel(index);
index_max = max(index);
index_min = min(index);
yv = y(~isnan(y));
y_obs = numel(yv);
y_max = max(yv);
y_min = min(yv);
y_avg = mean(yv);
y_med = median(yv);
y_std = std(yv);
y_ske = skewness(yv,0);
y_kur = kurtosis(yv,0);

index_avg = mean(index);
index_med = median(index);
index_std = std(index);
index_ske = skewness(index,0);
index_kur = kurtosis(index,0);
r = yv - y_avg;
[s_h,s_pval] = shapiro_test(r,0.05);
[lbq_h,lbq_pval] = lbqtest(r.^2,'Alpha',0.05);
[a_h,a_pval] = archtest(r,'Alpha',0.05);

f = figure('Name','Dataset > Index','Units','normalized','Position',[100 100 0.85 0.85],'Tag',id);

sub_1 = subplot(2,1,1);
plot(sub_1,ds.DatesNum,ds.Index);
set(sub_1,'XLim',[ds.DatesNum(1) ds.DatesNum(end)],'XTickLabelRotation',45);
set(sub_1,'YLim',[(index_min - 0.01) (index_max + 0.01)]);
set(sub_1,'YLim',[(y_min - 0.01) (y_max + 0.01)]);
set(sub_1,'XGrid','on','YGrid','on');
t1 = title(sub_1,'Log Returns');
set(t1,'Units','normalized');
Expand All @@ -224,9 +232,10 @@ function plot_index(ds,id)
edges = get(hist,'BinEdges');
edges_max = max(edges);
edges_min = min(edges);
[values,points] = ksdensity(ds.Index);
[hv,hp] = ksdensity(ds.Index);

hold on;
plot(sub_2,points,values,'-b','LineWidth',1.5);
plot(sub_2,hp,hv,'-b','LineWidth',1.5);
hold off;
set(sub_2,'XLim',[(edges_min - (edges_min * 0.1)) (edges_max - (edges_max * 0.1))]);
t2 = title(sub_2,'P&L Distribution');
Expand All @@ -237,8 +246,34 @@ function plot_index(ds,id)
t = figure_title(['Index (' ds.IndexName ')']);
t_position = get(t,'Position');
set(t,'Position',[t_position(1) -0.0157 t_position(3)]);

txt_obs = sprintf('Observations: %d',y_obs);
txt_avg = sprintf('Mean: %.4f',y_avg);
txt_med = sprintf('Median: %.4f',y_med);
txt_std = sprintf('Standard Deviation: %.4f',y_std);
txt_ske = sprintf('Skewness: %.4f',y_ske);
txt_kur = sprintf('Kurtosis: %.4f',y_kur);

if (s_h)
txt_s = sprintf('Shapiro Test: T (%.4f)',s_pval);
else
txt_s = sprintf('Shapiro Test: F (%.4f)',s_pval);
end

if (lbq_h)
txt_lbq = sprintf('LBQ Test: T (%.4f)',lbq_pval);
else
txt_lbq = sprintf('LBQ Test: F (%.4f)',lbq_pval);
end

if (a_h)
txt_a = sprintf('ARCH Test: T (%.4f)',a_pval);
else
txt_a = sprintf('ARCH Test: F (%.4f)',a_pval);
end

txt = {sprintf('Observations: %d',index_obs) sprintf('Mean: %.4f',index_avg) sprintf('Median: %.4f',index_med) sprintf('Standard Deviation: %.4f',index_std) sprintf('Skewness: %.4f',index_ske) sprintf('Kurtosis: %.4f',index_kur)};
txt = {txt_obs '' txt_avg txt_med txt_std txt_ske txt_kur '' txt_s txt_lbq txt_a};

annotation('TextBox',(get(sub_2,'Position') + [0.01 -0.025 0 0]),'String',txt,'EdgeColor','none','FitBoxToText','on','FontSize',8);

pause(0.01);
Expand Down Expand Up @@ -342,15 +377,20 @@ function plot_function(subs,data)
x = data{1};
y = data{2};

y_obs = numel(y);
y_max = max(y,[],'omitnan');
y_min = min(y,[],'omitnan');

y_avg = mean(y,'omitnan');
y_med = median(y,'omitnan');
y_std = std(y,'omitnan');
y_ske = skewness(y,0);
y_kur = kurtosis(y,0);
yv = y(~isnan(y));
y_obs = numel(yv);
y_max = max(yv);
y_min = min(yv);
y_avg = mean(yv);
y_med = median(yv);
y_std = std(yv);
y_ske = skewness(yv,0);
y_kur = kurtosis(yv,0);

r = yv - y_avg;
[s_h,s_pval] = shapiro_test(r,0.05);
[lbq_h,lbq_pval] = lbqtest(r.^2,'Alpha',0.05);
[a_h,a_pval] = archtest(r,'Alpha',0.05);

d = find(isnan(y),1,'first');

Expand All @@ -375,14 +415,40 @@ function plot_function(subs,data)
edges = get(hist,'BinEdges');
edges_max = max(edges);
edges_min = min(edges);
[values,points] = ksdensity(y);
[dv,dp] = ksdensity(y);

hold(subs(2),'on');
plot(subs(2),points,values,'-b','LineWidth',1.5);
plot(subs(2),dp,dv,'-b','LineWidth',1.5);
hold(subs(2),'off');
set(subs(2),'XLim',[(edges_min - (edges_min * 0.1)) (edges_max - (edges_max * 0.1))]);

txt = {sprintf('Observations: %d',y_obs) sprintf('Mean: %.4f',y_avg) sprintf('Median: %.4f',y_med) sprintf('Standard Deviation: %.4f',y_std) sprintf('Skewness: %.4f',y_ske) sprintf('Kurtosis: %.4f',y_kur)};
txt_obs = sprintf('Observations: %d',y_obs);
txt_avg = sprintf('Mean: %.4f',y_avg);
txt_med = sprintf('Median: %.4f',y_med);
txt_std = sprintf('Standard Deviation: %.4f',y_std);
txt_ske = sprintf('Skewness: %.4f',y_ske);
txt_kur = sprintf('Kurtosis: %.4f',y_kur);

if (s_h)
txt_s = sprintf('Shapiro Test: T (%.4f)',s_pval);
else
txt_s = sprintf('Shapiro Test: F (%.4f)',s_pval);
end

if (lbq_h)
txt_lbq = sprintf('LBQ Test: T (%.4f)',lbq_pval);
else
txt_lbq = sprintf('LBQ Test: F (%.4f)',lbq_pval);
end

if (a_h)
txt_a = sprintf('ARCH Test: T (%.4f)',a_pval);
else
txt_a = sprintf('ARCH Test: F (%.4f)',a_pval);
end

txt = {txt_obs '' txt_avg txt_med txt_std txt_ske txt_kur '' txt_s txt_lbq txt_a};

annotation('TextBox',(get(subs(2),'Position') + [0.01 -0.025 0 0]),'String',txt,'EdgeColor','none','FitBoxToText','on','FontSize',8);

end
Expand Down
6 changes: 3 additions & 3 deletions ScriptsDataset/worksheets_batch.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,19 @@ function worksheets_batch_internal(file_path,sheets,names)
wb.Save();
wb.Close();
catch e
warning('MATLAB:SystemicRisk',['An error occurred while cleaning the file ''' escape_path(file_path) '''.' new_line() e.message]);
warning('MATLAB:SystemicRisk',['An error occurred while cleaning the file ''' clear_text(file_path) '''.' new_line() e.message]);
end

try
excel.Quit();
delete(excel);
catch e1
warning('MATLAB:SystemicRisk',['An error occurred while disposing the file ''' escape_path(file_path) ''' (step 1).' new_line() e1.message]);
warning('MATLAB:SystemicRisk',['An error occurred while disposing the file ''' clear_text(file_path) ''' (step 1).' new_line() e1.message]);

try
delete(excel);
catch e2
warning('MATLAB:SystemicRisk',['An error occurred while disposing the file ''' escape_path(file_path) ''' (step 2).' new_line() e2.message]);
warning('MATLAB:SystemicRisk',['An error occurred while disposing the file ''' clear_text(file_path) ''' (step 2).' new_line() e2.message]);
end
end

Expand Down
8 changes: 4 additions & 4 deletions ScriptsMeasures/run_comparison.m
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,12 @@ function write_results(ds,temp,out)
parfor k = 1:mc_len
md_k = mc{k,2};

[f,cv,h0,lag_r,lag_u] = granger_causality(md_k,gca,lag_max,lag_sel);
[h0,stat,cv,lag_r,lag_u] = granger_causality(md_k,gca,lag_max,lag_sel);

data_k = struct();
data_k.F = f;
data_k.CV = cv;
data_k.H0 = h0;
data_k.Stat = stat;
data_k.CV = cv;
data_k.LagR = lag_r;
data_k.LagU = lag_u;

Expand Down Expand Up @@ -833,7 +833,7 @@ function plot_scores_gc(ds,id) %#ok<DEFNU>
tooltip = '';
else
vs = [data_labels{i} ' vs ' data_labels{j}];
tooltip = sprintf('%s\nF: %.4f | CV: %.4f\nRestricted Lag: %d\nUnrestricted Lag: %d',vs,data_ij.F,data_ij.CV,data_ij.LagR,data_ij.LagU);
tooltip = sprintf('%s\nF: %.4f | CV: %.4f\nRestricted Lag: %d\nUnrestricted Lag: %d',vs,data_ij.Stat,data_ij.CV,data_ij.LagR,data_ij.LagU);
end

end
Expand Down
2 changes: 1 addition & 1 deletion ScriptsModels/catfin.m
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
t = numel(r);

if (t < 5)
error(['The value of ''r'' is invalid. Expected input to be an array of ' num2str(n) ' elements.']);
error('The value of ''r'' is invalid. Expected input to be a vector containing at least 5 elements.');
end

end
2 changes: 1 addition & 1 deletion ScriptsModels/cimdo.m
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@
pods = pods(:);

if (numel(pods) ~= n)
error(['The value of ''pods'' is invalid. Expected input to be an array of ' num2str(n) ' elements.']);
error(['The value of ''pods'' is invalid. Expected input to be a vector containing at least ' num2str(n) ' elements.']);
end

end
18 changes: 11 additions & 7 deletions ScriptsModels/granger_causality.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
% lag_r = An integer [1,lag_max] representing the selected lag order of the restricted model.
% lag_u = An integer [1,lag_max] representing the selected lag order of the unrestricted model.

function [f,cv,h0,lag_r,lag_u] = granger_causality(varargin)
function [h0,stat,cv,lag_r,lag_u] = granger_causality(varargin)

persistent ip;

Expand All @@ -35,12 +35,16 @@
lag_sel = ipr.lag_sel;

nargoutchk(3,5);

if (nargout == 4)
error('Both lag outputs must be either assigned or discarded.');
end

[f,cv,h0,lag_r,lag_u] = granger_causality_internal(data,a,lag_max,lag_sel);
[h0,stat,cv,lag_r,lag_u] = granger_causality_internal(data,a,lag_max,lag_sel);

end

function [f,cv,h0,lag_r,lag_u] = granger_causality_internal(data,a,lag_max,lag_sel)
function [h0,stat,cv,lag_r,lag_u] = granger_causality_internal(data,a,lag_max,lag_sel)

up = isempty(getCurrentTask());

Expand All @@ -53,7 +57,7 @@
y_uni = numel(y);

if ((x_uni < bt) || (y_uni < bt))
f = 1;
stat = 1;
cv = 1;
h0 = true;
lag_r = 1;
Expand Down Expand Up @@ -258,11 +262,11 @@
f_den = max(rss_u(lag_u,:) / df,0);

if ((f_den > 0.0) && (df > 0))
f = f_num / f_den;
stat = f_num / f_den;
cv = finv(1 - a,lag_u,df);
h0 = f <= cv;
h0 = stat <= cv;
else
f = 1;
stat = 1;
cv = 1;
h0 = true;
end
Expand Down
Loading

0 comments on commit 0208807

Please sign in to comment.