%% Large US public-company bankruptcies, monthly, April 1980 to date
%
% A case is a Chapter 7 or Chapter 11 petition by a US public company with
% total assets of at least $100 million in 1980 dollars, the definition of
% the Florida-UCLA-LoPucki Bankruptcy Research Database (BRD).
%
%   1980-04 to 2022-12  monthly counts from the BRD (final release, January
%                       2023), written at the bottom of this file. The BRD
%                       itself is licensed and is not redistributed here.
%   2023-01 onwards     cases from SEC Form 8-K filings under Item 1.03,
%                       listed in bankruptcy_cases.csv (same folder) with a
%                       link to each filing.
%
% This script builds the monthly series from those two sources and draws it.
% Finding new cases on EDGAR is done by update_bankruptcies.py --harvest.
%
% Output: bankruptcies_monthly.csv with the same columns as the Python
% script: month, cases, cases_ch11, cases_ch7, cases_ma12, source,
% nber_recession.
%
% Cite: LoPucki, L. M. (2023), Florida-UCLA-LoPucki Bankruptcy Research
% Database, final release January 2023, for 1980-2022.
%
% Requires MATLAB R2019b or later.

clear; clc;

% Last complete month: 8-Ks are due four business days after a petition.
today = datetime('today');
lastMonth = dateshift(today, 'start', 'month') - calmonths(1);
if day(today) < 8, lastMonth = lastMonth - calmonths(1); end

month = (datetime(1980,4,1):calmonths(1):lastMonth)';
n = numel(month);
splice = datetime(2023,1,1);
early = month < splice;

% BRD segment
[cAll, cCh11, cCh7] = brd_counts();
brdMonths = (datetime(1980,1,1):calmonths(1):datetime(2022,12,1))';
cases = zeros(n,1); ch11 = zeros(n,1); ch7 = zeros(n,1);
[hit, loc] = ismember(month, brdMonths);
cases(hit) = cAll(loc(hit)); ch11(hit) = cCh11(loc(hit)); ch7(hit) = cCh7(loc(hit));

% SEC 8-K segment
C = readtable('bankruptcy_cases.csv', 'TextType', 'string');
pm = dateshift(datetime(C.petition_date, 'InputFormat', 'yyyy-MM-dd'), 'start', 'month');
for i = find(~early)'
    sel = pm == month(i);
    cases(i) = sum(sel);
    ch11(i) = sum(sel & C.chapter == 11);
    ch7(i)  = sum(sel & C.chapter == 7);
end

ma12 = movmean(cases, [11 0]);          % trailing 12-month mean (shorter at the start)
[tR, rec] = fred('USREC');
nber = nan(n,1);
[hit, loc] = ismember(month, tR);
nber(hit) = rec(loc(hit));
source = repmat("sec_8k", n, 1);  source(early) = "brd";

T = table(string(datestr(month, 'yyyy-mm')), cases, ch11, ch7, round(ma12, 4), source, nber, ...
    'VariableNames', {'month','cases','cases_ch11','cases_ch7','cases_ma12','source','nber_recession'});
writetable(T, 'bankruptcies_monthly.csv');
fprintf('Wrote bankruptcies_monthly.csv: %d months, %s to %s, %d cases\n', n, T.month(1), T.month(end), sum(cases));
fprintf('Last 12 months: %d cases; latest month %s: %d\n', sum(cases(end-11:end)), T.month(end), cases(end));

% Chart: monthly counts, 12-month mean, NBER recessions
figure('Color', 'w'); hold on;
yl = [0 max(cases) + 1];
inRec = nber == 1;
starts = find(diff([0; inRec]) == 1);  stops = find(diff([inRec; 0]) == -1);
for k = 1:numel(starts)
    x0 = month(starts(k));  x1 = month(stops(k)) + calmonths(1);
    patch([x0 x1 x1 x0], [yl(1) yl(1) yl(2) yl(2)], [0.88 0.88 0.88], 'EdgeColor', 'none', 'HandleVisibility', 'off');
end
bar(month, cases, 1, 'FaceColor', [0.75 0.82 0.93], 'EdgeColor', 'none', 'DisplayName', 'Cases in the month');
plot(month, ma12, 'Color', [0.16 0.47 0.84], 'LineWidth', 1.6, 'DisplayName', '12-month mean');
xline(splice, ':', 'BRD | SEC 8-K', 'HandleVisibility', 'off');
ylim(yl); grid on; box on; legend('Location', 'northwest');
ylabel('Large public-company bankruptcies per month');

%% ---------------------------------------------------------------- functions
function [t, x] = fred(id)
% Download one monthly FRED series. FRED's server stalls on MATLAB's default
% request, so ask for an uncompressed reply on a kept-alive connection.
    url = ['https://fred.stlouisfed.org/graph/fredgraph.csv?id=' id '&cosd=1979-01-01'];
    file = [tempname '.csv'];
    opts = weboptions('Timeout', 60, 'UserAgent', 'bankruptcy-update/1.0', ...
        'HeaderFields', {'Accept-Encoding', 'identity'; 'Connection', 'keep-alive'});
    websave(file, url, opts);
    o = detectImportOptions(file);
    o = setvartype(o, 2, 'double');
    D = readtable(file, o);
    delete(file);
    t = dateshift(datetime(D{:,1}), 'start', 'month');
    x = D{:,2};
end

function [cAll, cCh11, cCh7] = brd_counts()
% Florida-UCLA-LoPucki Bankruptcy Research Database, final release
% (12 January 2023): cases by month of filing, 1980-01 to 2022-12, one row
% per year, January to December. NaN is before the series starts.
    A = [
        1980  NaN  NaN  NaN  1  0  0  0  0  1  1  0  0
        1981  1  1  0  0  0  0  0  0  1  0  1  1
        1982  0  1  0  3  1  1  2  2  0  1  1  1
        1983  0  0  1  1  0  0  0  1  2  0  1  0
        1984  0  0  0  1  0  0  1  0  1  2  0  1
        1985  0  0  2  1  0  0  0  2  1  1  1  0
        1986  2  1  2  0  0  0  1  1  1  1  1  1
        1987  0  1  1  2  0  1  0  2  0  0  0  1
        1988  1  4  1  0  1  0  3  2  0  0  0  0
        1989  2  0  3  1  0  0  1  1  2  1  3  2
        1990  2  2  4  5  2  2  1  0  2  4  3  3
        1991  6  6  1  4  5  2  3  1  3  2  2  6
        1992  8  2  2  1  2  3  0  4  2  7  1  0
        1993  2  2  3  2  3  1  1  1  2  1  3  4
        1994  2  2  1  0  0  0  0  1  1  0  4  1
        1995  1  0  0  0  3  3  2  2  2  3  2  0
        1996  3  0  0  1  1  1  2  2  3  2  0  1
        1997  2  1  0  0  1  1  5  3  2  0  1  1
        1998  5  3  0  0  1  2  3  1  4  4  4  4
        1999  3  4  7  1  1  5  2  3  6  5  3  5
        2000  7  6  4  8  9  9  4  7  7  5  6  8
        2001  9  3  9 15  8  5  9  6  6  8  9 10
        2002  9 13 10  7  8  7  2  3  3  7  8  6
        2003  3  2  7  4  8  9  7  6  4  4  1  2
        2004  1  4  7  1  2  3  0  1  3  3  2  2
        2005  6  3  1  1  2  0  1  1  4  3  1  2
        2006  1  1  3  0  1  1  1  1  2  2  0  1
        2007  0  0  1  1  1  1  1  2  1  2  2  1
        2008  3  3  2  3  1  1  4  2  3  4  6  7
        2009 10  9 16  9  9  6  5  6  3  6 10  2
        2010  3  3  3  2  4  3  1  3  3  2  5  4
        2011  1  3  0  3  1  2  0  1  1  2  5  4
        2012  3  4  1  2  2  0  2  4  0  2  1  3
        2013  4  3  4  4  0  5  1  1  3  0  1  0
        2014  0  1  4  5  0  1  0  1  2  2  1  0
        2015  1  2  6  0  2  1  3  4  2  2  0  2
        2016  3  5  4  5  9  3  3  3  1  3  2  2
        2017  5  1  2  2  5  3  1  1  1  1  4  4
        2018  2  3  4  1  1  0  0  0  0  4  0  1
        2019  2  3  1  4  2  3  1  3  1  2  2  1
        2020  1  2  3  5 12  9  9  4  5  2  3  1
        2021  1  0  2  0  1  1  0  2  0  1  0  0
        2022  0  0  0  1  1  1  0  2  0  0  0  1
    ];
    B = [
        1980  NaN  NaN  NaN  1  0  0  0  0  1  1  0  0
        1981  1  1  0  0  0  0  0  0  1  0  1  1
        1982  0  1  0  3  1  1  2  2  0  1  1  1
        1983  0  0  1  1  0  0  0  1  2  0  1  0
        1984  0  0  0  1  0  0  1  0  1  2  0  1
        1985  0  0  2  1  0  0  0  2  1  1  1  0
        1986  2  1  2  0  0  0  1  1  1  1  1  1
        1987  0  1  1  2  0  1  0  2  0  0  0  1
        1988  1  4  1  0  1  0  3  2  0  0  0  0
        1989  2  0  3  1  0  0  1  1  2  1  3  2
        1990  2  2  4  5  2  2  1  0  2  4  3  3
        1991  6  6  1  4  5  2  3  1  3  2  2  6
        1992  8  2  2  1  2  3  0  4  2  7  1  0
        1993  2  2  3  2  3  1  1  1  2  1  3  4
        1994  2  2  1  0  0  0  0  1  1  0  3  1
        1995  1  0  0  0  3  3  2  2  2  3  2  0
        1996  3  0  0  1  1  1  2  2  3  2  0  1
        1997  2  1  0  0  1  1  5  3  2  0  1  1
        1998  5  3  0  0  1  2  3  1  4  4  4  4
        1999  3  4  7  1  1  5  2  3  6  5  3  5
        2000  7  6  4  8  9  9  4  7  7  5  6  8
        2001  9  3  9 15  8  5  9  6  6  8  9 10
        2002  9 12 10  7  8  7  2  3  3  7  7  6
        2003  3  2  7  4  8  8  7  6  4  4  1  2
        2004  1  4  7  1  2  3  0  1  3  3  2  2
        2005  6  3  1  1  2  0  1  1  4  3  1  2
        2006  1  1  3  0  1  1  1  1  2  2  0  1
        2007  0  0  1  1  1  1  1  2  1  2  2  1
        2008  3  3  2  3  1  1  3  2  3  3  4  7
        2009  9  9 15  9  9  6  4  5  2  6  8  2
        2010  3  3  2  2  1  2  1  3  2  2  5  2
        2011  1  3  0  2  1  2  0  1  1  2  5  4
        2012  3  4  1  2  2  0  2  4  0  2  1  3
        2013  4  3  4  4  0  5  0  1  3  0  1  0
        2014  0  1  4  5  0  1  0  1  2  2  1  0
        2015  1  2  6  0  2  1  3  4  2  2  0  2
        2016  3  5  3  5  9  3  3  3  0  3  2  2
        2017  5  1  2  2  5  3  1  1  1  1  4  4
        2018  2  3  4  1  1  0  0  0  0  4  0  1
        2019  2  3  1  4  2  3  1  3  1  1  2  1
        2020  1  2  3  5 12  9  9  4  5  2  3  1
        2021  1  0  2  0  1  1  0  2  0  1  0  0
        2022  0  0  0  1  1  1  0  2  0  0  0  1
    ];
    C = [
        1980  NaN  NaN  NaN  0  0  0  0  0  0  0  0  0
        1981  0  0  0  0  0  0  0  0  0  0  0  0
        1982  0  0  0  0  0  0  0  0  0  0  0  0
        1983  0  0  0  0  0  0  0  0  0  0  0  0
        1984  0  0  0  0  0  0  0  0  0  0  0  0
        1985  0  0  0  0  0  0  0  0  0  0  0  0
        1986  0  0  0  0  0  0  0  0  0  0  0  0
        1987  0  0  0  0  0  0  0  0  0  0  0  0
        1988  0  0  0  0  0  0  0  0  0  0  0  0
        1989  0  0  0  0  0  0  0  0  0  0  0  0
        1990  0  0  0  0  0  0  0  0  0  0  0  0
        1991  0  0  0  0  0  0  0  0  0  0  0  0
        1992  0  0  0  0  0  0  0  0  0  0  0  0
        1993  0  0  0  0  0  0  0  0  0  0  0  0
        1994  0  0  0  0  0  0  0  0  0  0  0  0
        1995  0  0  0  0  0  0  0  0  0  0  0  0
        1996  0  0  0  0  0  0  0  0  0  0  0  0
        1997  0  0  0  0  0  0  0  0  0  0  0  0
        1998  0  0  0  0  0  0  0  0  0  0  0  0
        1999  0  0  0  0  0  0  0  0  0  0  0  0
        2000  0  0  0  0  0  0  0  0  0  0  0  0
        2001  0  0  0  0  0  0  0  0  0  0  0  0
        2002  0  0  0  0  0  0  0  0  0  0  0  0
        2003  0  0  0  0  0  1  0  0  0  0  0  0
        2004  0  0  0  0  0  0  0  0  0  0  0  0
        2005  0  0  0  0  0  0  0  0  0  0  0  0
        2006  0  0  0  0  0  0  0  0  0  0  0  0
        2007  0  0  0  0  0  0  0  0  0  0  0  0
        2008  0  0  0  0  0  0  1  0  0  1  2  0
        2009  1  0  1  0  0  0  1  1  1  0  2  0
        2010  0  0  1  0  3  1  0  0  1  0  0  2
        2011  0  0  0  1  0  0  0  0  0  0  0  0
        2012  0  0  0  0  0  0  0  0  0  0  0  0
        2013  0  0  0  0  0  0  1  0  0  0  0  0
        2014  0  0  0  0  0  0  0  0  0  0  0  0
        2015  0  0  0  0  0  0  0  0  0  0  0  0
        2016  0  0  1  0  0  0  0  0  1  0  0  0
        2017  0  0  0  0  0  0  0  0  0  0  0  0
        2018  0  0  0  0  0  0  0  0  0  0  0  0
        2019  0  0  0  0  0  0  0  0  0  1  0  0
        2020  0  0  0  0  0  0  0  0  0  0  0  0
        2021  0  0  0  0  0  0  0  0  0  0  0  0
        2022  0  0  0  0  0  0  0  0  0  0  0  0
    ];
    cAll  = reshape(A(:, 2:13)', [], 1);
    cCh11 = reshape(B(:, 2:13)', [], 1);
    cCh7  = reshape(C(:, 2:13)', [], 1);
end
