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

RSS Feed