user1152939
user1152939

Reputation: 31

Finding Consecutive Negative Values

For an economics assignment, I need to use data from the quarterly GDP growth rate data from Fed website and figure out how many technical recessions have occurred from 1952-2010.

This means I need to count the number of sequences which have 3 or more consecutive negative growth rates.

This is the code I have so far:

Data = xlsread('Q3.xls','D14:E271');

matlab_dates = Data(:,1)
real_GDP_growth = Data(:,2);

growth_data = [matlab_dates real_GDP_growth];

T = length(growth_data);
recession_tracker = zeros(T,1);


for i=2:T
    if growth_data(i,2) < 0
        recession_tracker(i,1) = recession_tracker(i-1,1) + 1;
    else recession_tracker(i,1) = 0;
    end
end

So Q3.xls contains 2 columns (D and E). Column D is the date column (with Quarterly dates formatted like yyyy-mm-dd, e.g. 1952-01-01, 1952-04-01); Column E is the growth rate column.

Matlab refuses to import the data from the Date column, and consequently my whole code doesn't work!

I would appreciate any suggestions on how to debug, or how to do this better!

Upvotes: 3

Views: 917

Answers (2)

kingusiu
kingusiu

Reputation: 306

Once you've managed to store the date column:

ind = find( growth_data(:,2) < 0)

strfind( diff(ind)', [1 1] )
  1. get indices of negative values
  2. see if you can find 3 ascending indices (diff = 1) -> answer : indices of all the dates where a recession started

Upvotes: 1

Alex
Alex

Reputation: 5893

If you are trying to get at the dates, then you may find the following useful.

You can get the formatted dates by grabbing the second output of xlsread(), then unformatting using datenum().

[M,txt] = xlsread('Q3.xls','D14:E271');

for row = 1:size(txt, 1)
    try
        % assuming date in the first column
        date = datenum( txt{row, 1}, 'yyyy-mm-dd');
    catch
        continue;
    end

    ... do something with date ...
end

Upvotes: 1

Related Questions