Page 1 of 1

How to calculate the number of block of data

Posted: Thu Nov 30, 2017 3:48 pm
by sudesh
Hi,
I need help to determine the number of blocks of signal within a series.

I have first initiate zero value for the series,
smpl 1970w1 @now
series signal = 0

and then based upon certain rule, I have assigned value of -1, -05, 0, 0.5 and I to the series.

Normalement there is a trend in the value, the value change from 0 to 1 and stay for a while then back to 0 and then it might go to -1 for a while and so on.

For example, let say, the signal stay zero from 1970 to 1970 and then go to 1 for six weeks and then go to -1 for 10 weeks and get back to zero for another 10 weeks and again change value, and so on.

1: I would like to know how many times the signal changes values.
2: I would like to know how many times the signal changes value to 1
3: What was the maximum times that the signal stay 1
4: What was the minimum times that the signal stay 1
5: What was the average times that the signal stay 1

Thanks for yours help.

Re: How to calculate the number of block of data

Posted: Thu Nov 30, 2017 5:32 pm
by EViews Matt
Hello,

For (1) and (2), a variation of Gareth's solutions to your previous questions are sufficient:

Code: Select all

' Skip the first observation and select only those observations where the signal changed.
smpl 1970w2 @now if signal <> signal(-1)
scalar total_changes = signal.@obs

' Skip the first observation and select only those observations where the signal changed to one.
smpl 1970w2 @now if signal <> signal(-1) and signal = 1
scalar changes_to_one = signal.@obs


For (3), (4), and (5), I assume you're referring to the max/min/avg number of contiguous weeks where the signal is one. For that we'll need to determine the number of weeks (duration) in each contiguous group of one's. An temporary series to hold this intermediate data is useful:

Code: Select all

' Full sample.
smpl 1970w1 @now
' The temporary series will begin as a dummy series with ones for all observations where the signal is one.
series tmp = signal = 1
' Update the temporary series so that contiguous groups of ones are replaced with a basic trend.
' For example, {0, 1, 1, 0, 1, 1, 1, 1, 0} would be updated to {0, 1, 2, 0, 1, 2, 3, 4, 0}.
' Note that the last number in each group is the duration of that group.
tmp = @recode(tmp > 0, @nan(tmp(-1), 0) + 1, tmp)
' Update the temporary series again to replace all but the last number in each group with zero.
' For example, {0, 1, 2, 0, 1, 2, 3, 4, 0} would be updated to {0, 0, 2, 0, 0, 0, 0, 4, 0}.
' Note that all the non-zero elements are now durations, one per group.
tmp = @recode(tmp > 0 and @nan(tmp(1), 0) > 0, 0, tmp)
' The minimum duration and average duration will be affected by the presence of zeros in the temporary series, so we'll use a sample to filter them out.
smpl if tmp <> 0
scalar max_weeks_at_one = @max(tmp)
scalar min_weeks_at_one = @min(tmp)
scalar avg_weeks_at_one = @mean(tmp)

Re: How to calculate the number of block of data

Posted: Fri Dec 01, 2017 2:19 pm
by sudesh
Thanks alots, it's working.