%% Overlay Recession Bands on Time Series Plot
% Load the Data_Unemployment.mat data set, which contains a monthly US unemployment rate series measured from 1954 through 1998.

load Data_Unemployment

% The variables Data and dates, among others, appear in the workspace. For more details on the data, enter Description.
% Data is a 45-by-12 matrix of the unemployment rates. The rows of Data correspond to successive years and its columns correspond to successive months; Data(j,k) is the unemployment rate in month k of year j. Represent Data as a vector of regular time series data by transposing the matrix, and then vertically concatenating the columns of the result.

Data = Data';
un = Data(:);

% dates is a numeric vector of the 45 consecutive sampling years. Create a datetime vector that expands dates by including all  months within each year.

Y = repmat(dates',12,1);
Y = Y(:);
M = repmat((1:12)',length(dates),1);
D = ones(length(un),1);
t = datetime(Y,M,D);

% Alternatively, you can use the calmonths function to efficiently include all months within each year.

tspan = datetime([dates(1); dates(end)],[1; 12],[1; 1]);
t = (tspan(1):calmonths(1):tspan(2))';

% Plot the unemployment rate series. Overlay bands for recession periods reported by NBER.

figure
plot(t,un)
recessionplot
ylabel('Rate (%)')
title("Unemployment Rate")

% Periods of recession appear to occur with sudden, relatively large increases in the unemployment rate.
% Copyright 2021 The MathWorks, Inc.