Dear Sven, Allin and Jack,
Definitely you rock! Thank you so much for your help!
Just one note: Jack's suggestion didn't work in the case
the series starts with 1-block. But fortunately this is not
my case :)
X=(0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1)
Found 2 blocks, average length 8
Y=(1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,)
Found 1 block, average length 16
Best regards,
Henrique
2011/11/30 Allin Cottrell <cottrell(a)wfu.edu>
On Wed, 30 Nov 2011, Riccardo (Jack) Lucchetti wrote:
> On Wed, 30 Nov 2011, Sven Schreiber wrote:
>
>> On 11/30/2011 10:55 PM, Henrique Andrade wrote:
>>> Dear Hansl experts,
>>>
>>> I would like to write a Hansl code but unfortunately I'm out of
>>> creativity :(
>>> I have a binary series with blocks of 0 and 1. Something like
>>>
>>> X=(0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1).
>>>
>>> Here are the steps I need do follow:
>>>
>>> (1) Find the number of 1-blocks;
>>> (2) Calculate the average number of observations inside these blocks.
>>>
>>> In my hypothetical example, the X series, I have two blocks, and these
>>> blocks have an average of 8 observations (five observations in the
first
>>> block and eleven observations in the second block).
>>>
>>
>> Just a quick thought: you could take the (serial/first) difference of
>> your series and count the occurrences of -1, which indicates how many
>> times you have a 01 pattern. And each 1 in the difference indicates the
>> position of a 10 pattern (or vice versa, depending on whether you take
>> left-to-right or right-to-left differences). The position indices help
>> you determine how many ones you have in between, i.e. in each block.
>>
>> But your problem in general does look like unpleasant details to code.
>
> Building on Sven's idea: call your binary series y. Then,
>
> <hansl>
> series chg = diff(y)
> chg[1] = (y[1] == 1)
> scalar n_blocks = sum(chg==1)
> scalar avg_len = sum(y) / n_blocks
> </hansl>
>
> I think this ought to work.
Here's another approach -- not so elegant in that it involves a
loop, but it prepares a vector of block lengths, in case that might
be wanted.
<hansl>
matrix X={0,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1}'
matrix B = {}
scalar blen = 0
scalar n = rows(X)
loop i=1..n -q
if X[i] == 1
blen++
if i == n || X[i+1] == 0
B |= {blen}
blen = 0
endif
endif
endloop
printf "Found %d blocks, average length %g\n",
rows(B), meanc(B)
</hansl>
Allin Cottrell
_______________________________________________
Gretl-users mailing list
Gretl-users(a)lists.wfu.edu
http://lists.wfu.edu/mailman/listinfo/gretl-users
--
Henrique Andrade*
*