Random combination of 5 from 10

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
b_lobo
Posts: 14
Joined: Mon Sep 02, 2013 6:38 pm

Random combination of 5 from 10

Unread post by b_lobo »

I'm looking to create random sets of 2 portfolios of 5 securities each such that for each set the portfolios have at least one different security. The ordering of the securities does not matter. In other words, this is a10C5 combination problem. Any thoughts on how to code this? %ranpermute(10) does not work here because I need to control the combination of the 2sets of 5 securities, right?
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Random combination of 5 from 10

Unread post by TomDoan »

So the only restriction is that they aren't the same? Then use %RANCOMBO(10,5) twice and do a re-draw if the second matches the first. The "constructive" alternative is to pick the two that are "different" (using %RANCOMBO(10,2)), assign the first to the first portfolio and second to the second portfolio, then fill out the other four of each. That's a bit trickier because you have to re-map the results of the secondary %RANCOMBO(9,4) since they will give numbers from 1-9 and one in each portfolio will already have been taken.
b_lobo
Posts: 14
Joined: Mon Sep 02, 2013 6:38 pm

Re: Random combination of 5 from 10

Unread post by b_lobo »

To be clear, say, in the first draw I get

draw=1 1,2,3,4,5 6,7,8,9,10

then, in the second draw at least one of the 1,2,3,4,5 needs to be different and (automatically) one of the 6,7,8,9,10 needs to be different.

If I run %rancombo(10,5) twice, then the second %rancombo could draw a security that was drawn the first time, right?
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Random combination of 5 from 10

Unread post by TomDoan »

You want two sets of 5 don't you? %rancombo(10,5) will give you something like 3 7 10 5 8. A second %rancombo(10,5) will give you something like 1 6 10 7 2. Since 3 isn't in the second set, the two aren't identical, so it appears that this is a valid pair of portfolios. If that's not what you're looking for, you'll have to explain it more clearly.
b_lobo
Posts: 14
Joined: Mon Sep 02, 2013 6:38 pm

Re: Random combination of 5 from 10

Unread post by b_lobo »

compute n1 = %rancombo(10,5)
compute n2 = %rancombo(10,5)
display @20 n1(1) @25 n1(2) @30 n1(3) @35 n1(4) @40 n1(5) @45 n2(1) @50 n2(2) @55 n2(3) @60 n2(4) @65 n2(5)

gave me: 5 9 3 2 4 || 6 3 2 7 5

The 3,2 and 5 got repeated. Perhaps I'm not coding this correctly?
b_lobo
Posts: 14
Joined: Mon Sep 02, 2013 6:38 pm

Re: Random combination of 5 from 10

Unread post by b_lobo »

I should add the 2 sets of 5 must be distinctly different numbers from 1 to 10.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Random combination of 5 from 10

Unread post by TomDoan »

b_lobo wrote:I should add the 2 sets of 5 must be distinctly different numbers from 1 to 10.
So you want to partition the 10 numbers into two sets of 5? That's not even close to your original description.

Do %ranpermute(10). The first 5 are the first portfolio, the last 5 are the second.
b_lobo
Posts: 14
Joined: Mon Sep 02, 2013 6:38 pm

Re: Random combination of 5 from 10

Unread post by b_lobo »

I have been using %ranpermute(10). However, it is possible to get in one draw 1,2,3,4,5 but in a second draw 1,3,2,4,5 for the first or last five securities. The securities are the same but ordered differently. I'm trying to avoid this. I'd like to have at least one number different in each draw, say 1,2,3,4,6.

If this is possible by tweaking %ranpermute that would be great.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Random combination of 5 from 10

Unread post by TomDoan »

If you want to generate (equally probable) random partitions of 10 numbers into two sets of 5, what I described will do it. If you want to draw partitions without replacement (which isn't standard practice, BTW---draws with replacement are independent, draws without replacement aren't), that's quite complicated---you would either have to keep track of all the previous draws and test to see whether you repeated one, or you would have to map the partitions to the numbers from 1 to 252(=10C5), draw x numbers from 1 to 252 without replacement and map them back to their partitions.
b_lobo
Posts: 14
Joined: Mon Sep 02, 2013 6:38 pm

Re: Random combination of 5 from 10

Unread post by b_lobo »

I have all the 252 combinations of 10 in a separate data sheet. What would be the best way to map those into the code below instead of %ranpermute(10)?

compute n = %ranpermute(10)
do j=1,30

dec vect bx(10) dx(10) rx(10) ; * there are 10 cross-sections (x)
ewise bx(i) = b(i+(10*(j-1)))
ewise dx(i) = d(i+(10*(j-1)))
ewise rx(i) = r(i+(10*(j-1)))

NONLIN(parmset=base) w1 w2 w3 w4 w5 w6 w7 w8 w9 w10
NONLIN(parmset=constraint) w1+w2+w3+w4+w5==1.0 w6+w7+w8+w9+w10==-1.0 $
w1*bx(n(1))+w2*bx(n(2))+w3*bx(n(3))+w4*bx(n(4))+w5*bx(n(5))+w6*bx(n(6))+w7*bx(n(7))+w8*bx(n(8))+w9*bx(n(9))+w10*bx(n(10))==0.0

COMPUTE w1=w2=w3=w4=w5=w6=w7=w8=w9=w10=0.0 ; * initial values

FIND(parmset=base+constraint,noprint,method=bfgs) ROOT $ w1*bx(n(1))+w2*bx(n(2))+w3*bx(n(3))+w4*bx(n(4))+w5*bx(n(5))+w6*bx(n(6))+w7*bx(n(7))+w8*bx(n(8))+w9*bx(n(9))+w10*bx(n(10))
END FIND

end do j
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Random combination of 5 from 10

Unread post by TomDoan »

The simplest thing to do would be to read them into a VECT[INDEX], with the outer dimension of 252 and the inner of 5. Then you can simply pull out the combinations however you want. A simpler example (for 5C3) is

dec vect[index] mycombos(10)
do i=1,10
dim mycombos(i)(3)
end do i
open data combos.xls
read(format=xls) mycombos
combos.xls
File with combinations
(6 KiB) Downloaded 705 times



The sample code below is for another application which requires an exhaustive analysis of all combinations of a certain size (interestingly, also 10C5). Since you already have the file created with the combinations, you might find the above to be easier, but this would be a better way to go about this if you might need a different set of combinations.

Code: Select all

*
* This code sample demonstrates a method for selecting a valid set of
* instruments for a GMM where some of the moment conditions are suspected
* of being incorrect.
*
* It is based on a recent paper by Donald Andrews, entitled "Consistent
* Moment Selection Procedures for Generalized Method of Moments Estimation."
* Econometrica, Vol. 67, No. 3, pp 543-564. To do this precisely as
* described by Andrews requires running GMM with all subsets of a given
* size out of the instrument set. The following code provides a systematic
* method for generating such subsets.
*
* Most of the code is written to be as general as possible. In this
* particular example, we demonstrate the use of the "pos" index to set
* up MASK arrays for a GMM estimated using the NLSYSTEM command.
*
* Note that this is not a complete program--you need to add instructions
* to read in your data, do any transformations, etc. For the NLSYSTEM case,
* as shown here, you will also need to define your non-linear parameters,
* and define the formula to be estimated (we refer to it as "mygmmfrml"
* in the NLSYSTEM command included below):
*
* The sections of code specific to this application (i.e. the sections
* which need to be changed for other applications) are bracketed
* by "*******************" lines.
*
* Written by Estima
*  August, 1999
*

declare vect[int] pos
*
* pop is the population size (10 in this example)
* pick is the number of items to be picked
*
compute pop =10
compute pick=5
dim pos(pick)

*****************************************************************
* Variable definitions specific to this example:
*
  * Set up the MASK array to be used for this particular application:
   dec rect mask(pop,1)
 * Initialize bookkeeping variable:
    compute bestuzwzu=-1.0
*
* End of application-specific variable definitions
*****************************************************************

*
* start with 1,2,...,pick. The vector of integers "pos" is always filled
* with an increasing sequence of values between 1 and pop.
*
ewise pos(i)=i
*
compute fixpos=pick
begin {
   loop {
      *****************************************************************
      *
      * Insert code here to do what you want with the indexes in pos.
      *
      * In this example, we specify the masking arraying and estimate using
      * NLSYTEM:
      *
      compute mask=%const(0.0)
      do j=1,pick
         compute mask(pos(j),1)=1.0
      end do j
      nlsystem(mask=mask,zudep,instruments,noprint) / mygmmfrml
      if bestuzwzu<0.or.%uzwzu<bestuzwzu
         compute bestuzwzu=%uzwzu,bestset=pos
      *
      * End of inserted code section
      *
      *****************************************************************
      *
      * When we hit the end of the range for a slot, back up to the nearest
      * preceding slot which still can be incremented.
      *
      while (fixpos>=1.and.pos(fixpos)==pop+fixpos-pick)
         compute fixpos=fixpos-1
      *
      * If all slots are at their limit, we're done.
      *
      if fixpos==0
         break
      *
      * Increment the selected slot by one, and set all following slots to
      * their preceding slot plus one.
      *
      compute pos(fixpos)=pos(fixpos)+1
      do j=1,pick-fixpos
         compute pos(fixpos+j)=pos(fixpos)+j
      end do j
      compute fixpos=pick
   }
}
b_lobo
Posts: 14
Joined: Mon Sep 02, 2013 6:38 pm

Re: Random combination of 5 from 10

Unread post by b_lobo »

This set of instructions gives me: ## MAT18. You Must Dimension the Outer Array Separately

dec vect[index] mycombos(10)
do i=1,252
dim mycombos(i)(10)
end do i
open data combinations.xls
read(format=xls) mycombos

Also, will I follow this up with something like:

do draw=1,252
compute n = mycombos(i)
... code
end do draw
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Random combination of 5 from 10

Unread post by TomDoan »

Shouldn't that be 252 in your case?

dec vect[index] mycombos(10)
b_lobo
Posts: 14
Joined: Mon Sep 02, 2013 6:38 pm

Re: Random combination of 5 from 10

Unread post by b_lobo »

My mistake: I took the (10) in the dec statement to be the dimension of each vector.

So the dec vect[index] is tracking all the 252 vectors of combinations, right? I'm expecting RATS to sequentially pull each vector into the code after it reads the combos. However, the write instruction gives me some random ordering 7,7,7,7,6,6,6,6,6,5 instead of the 252nd vector in the file.

dec vect[index] mycombos(252)
do i=1,252
dim mycombos(i)(10)
end do i
open data combinations.xls
read(format=xls) mycombos
write mycombos(252)
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Random combination of 5 from 10

Unread post by TomDoan »

I didn't make up your Excel file, so I have no idea what's on it. You should have 252 rows, each with 10 numbers with a row representing a different partition into two groups of five.
Post Reply