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 Sep 18, 2020
1 parent 1ce1fbb commit 1abafe6
Show file tree
Hide file tree
Showing 47 changed files with 2,718 additions and 1,641 deletions.
32 changes: 17 additions & 15 deletions ScriptsData/distress_data.m
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
% [INPUT]
% ts = A float t-by-n matrix containing the time series.
% offsets = A vector of length n containing the numeric date of the distress begin of each firm; offsets are equal to NaN in absence of distress.
% data = A float t-by-n matrix containing the time series to be distressed.
% offsets = A vector of length n containing the distress begin offsets of each firm; offsets must be equal to NaN in absence of distress.
%
% [OUTPUT]
% ts = A float t-by-n matrix containing the original time series in which distressed observations are replaced with NaNs.

function ts = distress_data(varargin)
function data = distress_data(varargin)

persistent ip;

if (isempty(ip))
ip = inputParser();
ip.addRequired('ts',@(x)validateattributes(x,{'double'},{'real' '2d'}));
ip.addRequired('data',@(x)validateattributes(x,{'double'},{'real' '2d'}));
ip.addRequired('offsets',@(x)validateattributes(x,{'double'},{'real' 'vector' 'nonempty'}));
end

ip.parse(varargin{:});

ipr = ip.Results;
[ts,offsets] = validate_input(ipr.ts,ipr.offsets);
[data,offsets] = validate_input(ipr.data,ipr.offsets);

nargoutchk(1,1);

ts = distress_data_internal(ts,offsets);
data = distress_data_internal(data,offsets);

end

function ts = distress_data_internal(ts,offsets)
function data = distress_data_internal(data,offsets)

if (isempty(ts))
if (isempty(data))
return;
end

Expand All @@ -39,18 +39,20 @@
continue;
end

ts(offset:end,i) = NaN;
data(offset:end,i) = NaN;
end

end

function [ts,offsets] = validate_input(ts,offsets)
function [data,offsets] = validate_input(data,offsets)

n = size(ts,2);
k = numel(offsets);

if ((n ~= 0) && (n ~= numel(offsets)))
error(['The number of columns in the time series (' num2str(n) ') must be equal to the number of offsets (' num2str(k) ').']);
if (~isempty(data))
n = size(data,2);
k = numel(offsets);

if (n ~= k)
error(['The number of columns in the time series (' num2str(n) ') must be equal to the number of offsets (' num2str(k) ').']);
end
end

end
2 changes: 1 addition & 1 deletion ScriptsData/extract_firms_data.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@
end
end

end
end
6 changes: 3 additions & 3 deletions ScriptsData/extract_rolling_windows.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
% [INPUT]
% data = A float t-by-n matrix containing the time series.
% data = A float t-by-n matrix containing the time series to be converted into rolling windows.
% bw = An integer [2,252] representing the dimension of each rolling window.
% truncate = A boolean that indicates whether to exclude all the rolling windows with a dimension less than the bandwidth (optional, default=true).
% truncate = A boolean that indicates whether to exclude all the rolling windows with a dimension less than the bandwidth (optional, default=false).
%
% [OUTPUT]
% windows = A column cell array of bw-by-n float matrices representing the rolling windows.
Expand Down Expand Up @@ -65,4 +65,4 @@
end
end

end
end
68 changes: 68 additions & 0 deletions ScriptsData/forward_roll_data.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
% [INPUT]
% data = A float t-by-n matrix containing the daily time series to be rolled forward.
% dates = A vector of length t containing the numeric reference dates of observations.
% fr = An integer [0,6] representing the number of months of forward-rolling to be applied to the time series.
%
% [OUTPUT]
% ts = A float t-by-n matrix containing the original time series in which distressed observations are replaced with NaNs.

function data = forward_roll_data(varargin)

persistent ip;

if (isempty(ip))
ip = inputParser();
ip.addRequired('data',@(x)validateattributes(x,{'double'},{'real' '2d'}));
ip.addRequired('dates',@(x)validateattributes(x,{'double'},{'real' 'vector'}));
ip.addRequired('fr',@(x)validateattributes(x,{'double'},{'real' 'finite' 'integer' '>=' 0 '<=' 6 'scalar'}));
end

ip.parse(varargin{:});

ipr = ip.Results;
[data,dates] = validate_input(ipr.data,ipr.dates);
fr = ipr.fr;

nargoutchk(1,1);

data = forward_roll_data_internal(data,dates,fr);

end

function data = forward_roll_data_internal(data,dates,fr)

if (isempty(data) || (fr == 0))
return;
end

[~,a] = unique(cellstr(datestr(dates,'mm/yyyy')),'stable');
data_monthly = data(a,:);

indices_seq = [a(1:fr:numel(a)) - 1; numel(dates)];
data_seq = data_monthly(1:fr:numel(a),:);

data_fr = NaN(size(data));

for i = 2:numel(indices_seq)
indices = (indices_seq(i-1) + 1):indices_seq(i);
data_fr(indices,:) = repmat(data_seq(i-1,:),numel(indices),1);
end

data = data_fr;

end

function [data,dates] = validate_input(data,dates)

if (~isempty(data))
t = size(data,1);
td = numel(dates);

if (t ~= td)
error(['The number of rows in the time series (' num2str(t) ') must be equal to the number of dates (' num2str(td) ').']);
end
end

dates = dates(:);

end
2 changes: 1 addition & 1 deletion ScriptsMeasures/run_comparison.m
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ function plot_scores_gc(ds,id) %#ok<DEFNU>
set(sub_2,'Box','on','XGrid','on','YGrid','on');
title('Data Browser');

figure_title(['Granger-causality (A=' num2str(ds.GCA) ', LM=' num2str(ds.LagMax) ', LS=' ds.LagSel ')']);
figure_title(['Granger-causality (A=' num2str(ds.GCA * 100) '%, LM=' num2str(ds.LagMax) ', LS=' ds.LagSel ')']);

pause(0.01);
frame = get(f,'JavaFrame');
Expand Down
Loading

0 comments on commit 1abafe6

Please sign in to comment.