Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Combinations of 4 inputs that sum up to 100

    Dear Stata Users,

    I am simulating all possible combinations of 4 arguments that sum up to 100. The objective is to create all possible weighted sums of 4 variables. i.e.

    aX + bY + cZ +dW

    where
    a+b+c+d=100

    a,b,c,d are whole numbers and cannot be larger than 100 or negative (so no possibility of subtraction). They can be in increments of 5 rather than one by one. This isn't essential, just reduces the number of sets of {a,b,c,d}.

    the next step is to get each combination in 4 locals and multiply by each of X Y Z and W , store the results and repeat.

    Any inspiration is gratefully welcome!
    Thanks!

  • #2
    I would like to say something clever here, but if you are not doing combinatorics for a living or have a lot of fun with combinatorics, I would just go with a simple brute-force approach: four nested loops for a, b, c , and d running from 0 to 100. Seems quicker than figuring out something more elaborated.

    Anyway, I wonder what the larger context of this is all about.

    Best
    Daniel
    Last edited by daniel klein; 02 Feb 2018, 08:09.

    Comment


    • #3
      I agree with Daniel. Here's a looping approach,which leaves satisfactory combinations of a b c d in numbered macros such that macros aN bN cN dN sharing a suffix sum to 100. It assumes my understanding of your description, namely that the possible values of each a, b, c, d are {0, 5, 10, ..., 100}.
      Code:
      local idx = 0
      forval a = 0(5)100 {
         forval b = 0(5)100 {
            forval c = 0(5)100 {
               forval d = 0(5)100 {
                  local test = `a' + `b' + `c' + `d'
                  if (`test' == 100) {
                     local idx = `idx' + 1
                     local a`idx' = `a'
                     local b`idx' = `b'
                     local c`idx' = `c'
                     local d`idx' = `d'
                  }
               }
            }
         }
      }
      local count = `idx'
      di "`count' satisfactory combinations were found"
      //
      di "Check some random examples:
      forval i = 1/5 {
          local pick = ceil(`count' * runiform())
          di "`pick': `a`pick'' `b`pick'' `c`pick'' `d`pick''
      }

      Comment


      • #4
        Here's a slightly clever approach that depends on 0 being allowed for a, b, c, and d.
        Code:
        forvalues a = 0(5)100 {
        forvalues b = 0(5)`=100-`a'' {
        forvalues c = 0(5)`=100-`a'-`b'' {
        local d = 100-`a'-`b'-`c'
        // whatever you want goes here
        display "`a' `b' `c' `d'"
        }
        }
        }

        Comment


        • #5
          Brilliant! Thanks a lot Daniel, Mike, and William.

          daniel, to put it in context, I am doing some robustness analysis for a simple index building exercise for a program evaluation, and wanted to show robustness for different weighting structures using coefplot.

          Grateful to all your responses!

          Comment

          Working...
          X