Skip to content

Commit

Permalink
Bubbles Detection Measures
Browse files Browse the repository at this point in the history
  • Loading branch information
TommasoBelluzzo committed Jan 5, 2023
1 parent ef9bc1a commit c9d27f1
Show file tree
Hide file tree
Showing 18 changed files with 500 additions and 351 deletions.
61 changes: 27 additions & 34 deletions ScriptsDataset/analyze_dataset.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,9 @@ function plot_boxes(ds,target,id)
set(ax,'XTick',1:n,'XTickLabels',ds.FirmNames,'XTickLabelRotation',45);
set(ax,'YLim',[y_low y_high]);

figure_title(target);
figure_title(f,target);

pause(0.01);
frame = get(f,'JavaFrame'); %#ok<JAVFM>
set(frame,'Maximized',true);
maximize_figure(f);

end

Expand Down Expand Up @@ -163,18 +161,14 @@ function plot_crises(ds,id)
end

if (strcmp(ds.CrisesType,'E'))
figure_title('Crises (Events)');
figure_title(f,'Crises (Events)');
else
figure_title('Crises (Ranges)');
figure_title(f,'Crises (Ranges)');
end

pause(0.01);
frame = get(f,'JavaFrame'); %#ok<JAVFM>
set(frame,'Maximized',true);
maximize_figure(f);

if (~isempty(tooltips))
drawnow();

dcm = datacursormode(f);
set(dcm,'Enable','on','SnapToDataVertex','off','UpdateFcn',@(targ,evtd)create_tooltip(targ,evtd,tooltips));
createDatatip(dcm,tooltips_target,[1 1]);
Expand Down Expand Up @@ -243,7 +237,7 @@ function plot_index(ds,id)
t2_position = get(t2,'Position');
set(t2,'Position',[0.4783 t2_position(2) t2_position(3)]);

t = figure_title(['Index (' ds.IndexName ')']);
t = figure_title(f,['Index (' ds.IndexName ')']);
t_position = get(t,'Position');
set(t,'Position',[t_position(1) -0.0157 t_position(3)]);

Expand Down Expand Up @@ -276,9 +270,7 @@ function plot_index(ds,id)

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

pause(0.01);
frame = get(f,'JavaFrame'); %#ok<JAVFM>
set(frame,'Maximized',true);
maximize_figure(f);

end

Expand Down Expand Up @@ -319,11 +311,9 @@ function plot_risk_free_rate(ds,id)
date_ticks([sub_1 sub_2],'x','yyyy','KeepLimits');
end

figure_title('Risk-Free Rate');
figure_title(f,'Risk-Free Rate');

pause(0.01);
frame = get(f,'JavaFrame'); %#ok<JAVFM>
set(frame,'Maximized',true);
maximize_figure(f);

end

Expand Down Expand Up @@ -402,25 +392,27 @@ function plot_function(subs,data)

delete(findall(gcf,'type','annotation'));

plot(subs(1),x,y,'Color',[0.000 0.447 0.741]);
set(subs(1),'YLim',[(y_min - 0.01) (y_max + 0.01)]);
sub_1 = subs(1);
plot(sub_1,x,y,'Color',[0.000 0.447 0.741]);
set(sub_1,'YLim',[(y_min - 0.01) (y_max + 0.01)]);

if (~isempty(xd))
hold(subs(1),'on');
plot(subs(1),[xd xd],get(subs(1),'YLim'),'Color',[1.000 0.400 0.400]);
hold(subs(1),'off');
hold(sub_1,'on');
plot(sub_1,[xd xd],get(sub_1,'YLim'),'Color',[1.000 0.400 0.400]);
hold(sub_1,'off');
end

hist = histogram(subs(2),y,50,'FaceColor',[0.749 0.862 0.933],'Normalization','pdf');
sub_2 = subs(2);
hist = histogram(sub_2,y,50,'FaceColor',[0.749 0.862 0.933],'Normalization','pdf');
edges = get(hist,'BinEdges');
edges_max = max(edges);
edges_min = min(edges);
[dv,dp] = ksdensity(y);

hold(subs(2),'on');
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))]);
hold(sub_2,'on');
plot(sub_2,dp,dv,'-b','LineWidth',1.5);
hold(sub_2,'off');
set(sub_2,'XLim',[(edges_min - (edges_min * 0.1)) (edges_max - (edges_max * 0.1))]);

txt_obs = sprintf('Observations: %d',y_obs);
txt_avg = sprintf('Mean: %.4f',y_avg);
Expand Down Expand Up @@ -449,7 +441,7 @@ function plot_function(subs,data)

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);
annotation('TextBox',(get(sub_2,'Position') + [0.01 -0.025 0 0]),'String',txt,'EdgeColor','none','FitBoxToText','on','FontSize',8);

end

Expand Down Expand Up @@ -513,12 +505,13 @@ function plot_function(subs,data)
xd = x(d) - 1;
end

plot(subs(1),x,y,'Color',[0.000 0.447 0.741]);
sub_1 = subs(1);
plot(sub_1,x,y,'Color',[0.000 0.447 0.741]);

if (~isempty(xd))
hold(subs(1),'on');
plot(subs(1),[xd xd],get(subs(1),'YLim'),'Color',[1.000 0.400 0.400]);
hold(subs(1),'off');
hold(sub_1,'on');
plot(sub_1,[xd xd],get(sub_1,'YLim'),'Color',[1.000 0.400 0.400]);
hold(sub_1,'off');
end

end
Expand Down
205 changes: 200 additions & 5 deletions ScriptsMeasures/run_bubbles_detection.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,11 @@
break;
end


offset = min([(ds.Defaults(i) - 1) (ds.Insolvencies(i) - 1) t]);
offset = min(ds.Defaults(i) - 1,t);
p_i = p(1:offset,i);

[bsadfs,cvs,detection,breakdown] = psy_bubbles_detection(p_i,ds.CVM,ds.CVQ,ds.LagMax,ds.LagSel,ds.MBD);
ds.BSADF(1:offset,i) = bsadfs;
ds.BSADFS(1:offset,i) = bsadfs;
ds.CVS(1:offset,i) = cvs;
ds.BUB(1:offset,i) = detection(:,1);
ds.BMPH(1:offset,i) = detection(:,2);
Expand Down Expand Up @@ -187,7 +186,7 @@
ds.LabelsSheetsSimple = [ds.LabelsMeasuresSimple {'Indicators'}];
ds.LabelsSheets = [ds.LabelsMeasures {'Indicators'}];

ds.BSADF = NaN(t,n);
ds.BSADFS = NaN(t,n);
ds.CVS = NaN(t,n);
ds.BUB = NaN(t,n);
ds.BMPH = NaN(t,n);
Expand All @@ -202,7 +201,7 @@

function ds = finalize(ds)

bc = sum(ds.Bubbles .* ds.Capitalizations,2,'omitnan');
bc = sum(ds.BUB .* ds.Capitalizations,2,'omitnan');
bcp = bc ./ sum(ds.Capitalizations,2,'omitnan');

ds.Indicators = [bc bcp];
Expand Down Expand Up @@ -262,6 +261,202 @@ function write_results(ds,temp,out)

function analyze_result(ds)

safe_plot(@(id)plot_sequence(ds,id));
safe_plot(@(id)plot_indicators(ds,id));

end

function plot_indicators(ds,id)

bc = ds.Indicators(:,1);
bcp = ds.Indicators(:,2);

y_limits_bc = plot_limits(bc,0.1,0);
y_limits_bcp = [0 100];

f = figure('Name','Bubbles Detection Measures > Indicators','Units','normalized','Position',[100 100 0.85 0.85],'Tag',id);

sub_1 = subplot(2,1,1);
plot(sub_1,ds.DatesNum,smooth_data(bc),'Color',[0.000 0.447 0.741]);
set(sub_1,'YLim',y_limits_bc);
t1 = title(sub_1,ds.LabelsIndicators{1});
set(t1,'Units','normalized');
t1_position = get(t1,'Position');
set(t1,'Position',[0.4783 t1_position(2) t1_position(3)]);

sub_2 = subplot(2,1,2);
area(sub_2,ds.DatesNum,smooth_data(bcp),'EdgeColor',[0.000 0.447 0.741],'FaceAlpha',0.5,'FaceColor',[0.749 0.862 0.933]);
set(sub_2,'YLim',y_limits_bcp);
t2 = title(sub_2,ds.LabelsIndicators{2});
set(t2,'Units','normalized');
t2_position = get(t2,'Position');
set(t2,'Position',[0.4783 t2_position(2) t2_position(3)]);

set([sub_1 sub_2],'XLim',[ds.DatesNum(1) ds.DatesNum(end)],'XTickLabelRotation',45);
set([sub_1 sub_2],'XGrid','on','YGrid','on');

if (ds.MonthlyTicks)
date_ticks([sub_1 sub_2],'x','mm/yyyy','KeepLimits','KeepTicks');
else
date_ticks([sub_1 sub_2],'x','yyyy','KeepLimits');
end

figure_title(f,'Indicators');

maximize_figure(f);

end

function plot_sequence(ds,id)

n = ds.N;
t = ds.T;
dn = ds.DatesNum;
mt = ds.MonthlyTicks;

bmph = ds.BMPH;
brph = ds.BRPH;

data_bsadfs = ds.BSADFS;
data_cvs = ds.CVS;
data_ps = smooth_data(ds.Prices);
data_ps_bm = data_ps .* bmph;
data_ps_br = data_ps .* brph;
data_ps_ot = NaN(t,n);

data_bds = cell(1,n);

for i = 1:n
offset = min(ds.Defaults(i) - 1,t);

bsadfs_i = data_bsadfs(1:offset,i);
idx = find(bsadfs_i ~= 0,1,'first');
data_bsadfs(1:idx,i) = bsadfs_i(idx + 1);
data_bsadfs(1:offset,i) = smooth_data(data_bsadfs(1:offset,i));

cvs_i = data_cvs(1:offset,i);
idx = find(isnan(cvs_i),1,'last');
data_cvs(1:idx,i) = cvs_i(idx + 1);
data_cvs(1:offset,i) = smooth_data(data_cvs(1:offset,i));

data_ps_ot(1:offset,i) = data_ps(1:offset,i) .* ~(bmph(1:offset,i) | bmph(1:offset,i));

bd_i = ds.Breakdowns{i};

bm_count = sum(bd_i(:,5));
br_count = sum(bd_i(:,6));
ot_count = t - (bm_count + br_count);
data_bds{i} = [ot_count bm_count br_count];
end

data_ones = ones(1,n);
data = [repmat({dn},1,n); mat2cell(data_bsadfs,t,data_ones); mat2cell(data_cvs,t,data_ones); mat2cell(data_ps,t,data_ones); mat2cell(data_ps_bm,t,data_ones); mat2cell(data_ps_br,t,data_ones); mat2cell(data_ps_ot,t,data_ones); data_bds];

plots_title = [repmat({'Bubbles'},1,n); repmat({'Model'},1,n); repmat({'Breakdown - Percentage'},1,n); repmat({'Breakdown - Count'},1,n)];

x_limits = [dn(1) dn(end)];

core = struct();

core.N = n;
core.Data = data;
core.Function = @(subs,data)plot_function(subs,data);

core.OuterTitle = 'Bubbles Detection Measures > Time Series';
core.InnerTitle = 'Time Series';
core.SequenceTitles = ds.FirmNames;

core.PlotsAllocation = [7 4];
core.PlotsSpan = {[1:3 5:7 9:11 13:15] [21:23 25:27] [4 8 12] [20 24 28]};
core.PlotsTitle = plots_title;

core.XDates = {mt mt [] []};
core.XGrid = {true true false false};
core.XLabel = {[] [] [] []};
core.XLimits = {x_limits x_limits [] []};
core.XRotation = {45 45 [] []};
core.XTick = {[] [] [] []};
core.XTickLabels = {[] [] [] []};

core.YGrid = {true true false true};
core.YLabel = {[] [] [] []};
core.YLimits = {[] [] [] []};
core.YRotation = {[] [] [] []};
core.YTick = {[] [] [] []};
core.YTickLabels = {[] [] [] []};

sequential_plot(core,id);

function plot_function(subs,data)

x = data{1};
bsadfs = data{2};
cvs = data{3};
ps = data{4};
ps_bm = data{5};
ps_br = data{6};
ps_ot = data{7};
bd = data{8};

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

if (isempty(d))
xd = [];
else
xd = x(d) - 1;
end

sub_1 = subs(1);
a1 = area(sub_1,x,ps_ot,'EdgeColor','none','FaceAlpha',0.5,'FaceColor',[0.749 0.862 0.933]);
hold(sub_1,'on');
a2 = area(sub_1,x,ps_bm,'EdgeColor','none','FaceColor',[0.200 0.627, 0.173]);
a3 = area(sub_1,x,ps_br,'EdgeColor','none','FaceColor',[0.984 0.502 0.447]);
plot(sub_1,x,ps,'Color',[0.000 0.000 0.000])
hold(sub_1,'off');

if (~isempty(xd))
hold(sub_1,'on');
plot(sub_1,[xd xd],get(sub_1,'YLim'),'Color',[1.000 0.400 0.400]);
hold(sub_1,'off');
end

l = legend(sub_1,[a1 a2 a3],'None','Boom Phase','Burst Phase','Location','south','Orientation','horizontal');
set(l,'Units','normalized');
l_position = get(l,'Position');
set(l,'Position',[l_position(1) 0.3645 l_position(3) l_position(4)]);

sub_2 = subs(2);
p1 = plot(sub_2,x,bsadfs,'Color',[0.000 0.447 0.741]);
hold(sub_2,'on');
p2 = plot(sub_2,x,cvs,'Color',[0.000 0.000 0.000],'LineStyle','--');
hold(sub_2,'off');

if (~isempty(xd))
hold(sub_2,'on');
plot(sub_2,[xd xd],get(sub_2,'YLim'),'Color',[1.000 0.400 0.400]);
hold(sub_2,'off');
end

legend(sub_2,[p1 p2],'BSADF','Critical Values','Location','northwest');

sub_3 = subs(3);
pc1 = pie(sub_3,bd);
hpc1 = findobj(pc1,'Type','patch');
set(hpc1(1),'FaceAlpha',0.5,'FaceColor',[0.749 0.862 0.933]);
set(hpc1(2),'FaceColor',[0.200 0.627, 0.173]);
set(hpc1(3),'FaceColor',[0.984 0.502 0.447]);

sub_4 = subs(4);
b1 = bar(sub_4,1,bd(1));
set(b1,'FaceAlpha',0.5,'FaceColor',[0.749 0.862 0.933]);
hold(sub_4,'on');
b2 = bar(sub_4,2,bd(2));
set(b2,'FaceColor',[0.200 0.627, 0.173]);
b3 = bar(sub_4,3,bd(3));
set(b3,'FaceColor',[0.984 0.502 0.447]);
hold(sub_4,'off');

end

end

Expand Down
Loading

0 comments on commit c9d27f1

Please sign in to comment.