UCLA Academic Technology Services HomeServicesClassesContactJobs

Stata Teaching Tools:  Box model simulation

Purposebxmodel is a collection of commands designed to simulate box model probability problems.  The user determines the number of tickets in the box and each of their values. The user also selects the number of tickets to be drawn on each trial and the number of trials (repetitions) to be run.

Download:  You can download this program from within Stata by typing findit bxmodel (see How can I use the findit command to search for programs and get additional help? for more information about using findit).

Some background information Box model problems involve the drawing of tickets from from a box. By varying the number of tickets and the value of the tickets, you can simulate many type of probability problems. For example, coin-tossing problems can be simulated with a dataset that looks like this,

     Dataset 1
     value     n
       1       1
       0       1
In this example, one stands for heads and zero for tails. You could also use a dataset like this one,
     Dataset 2
     value     n
       1      100
       0      100
The difference between the two datasets centers around sampling with or without replacement. In Dataset 1, you need to draw with replacement if you wish to draw more than one ticket per trial. In Dataset 2, you can draw multiple tickets per trial without replacement.

Here are two ways of simulating dice-rolling problems.

     Dataset 3          Dataset 4
     value     n        value     n
       1       1          1      100
       2       1          2      100
       3       1          3      100
       4       1          4      100
       5       1          5      100
       6       1          6      100

In order to run bxmodel you will need to create a Stata dataset with two variables, value and n.

Use of programbxmodel is different from other simulation programs at ATS in that it does not directly produce any output. It produces a new dataset that contains the results of each of the trials. For sampling with and without replacement, bxmodel generates the sum of the values, the mean of the values and the standard deviation of the values for each trial. These data can, in turn, be analyzed by any of Stata's commands.

To use this program, type bxmodel in the Stata command window.  This will open a dialogue window for entering information about the box model to be run. Here are the items you will need to enter: the file name, the number of tickets to be selected on each trial, the type of box model (with replacement, without replacement, or birthday problems), and the number of trials (repetitions). To run the command just click on "Do It." bxmodel will give notice that it has begun and when it is done.

Examples:  We will demonstrate several examples using datasets that we will create.

Example 1:  Coin problem with replacement

clear

set obs 2

gen value = _n - 1

generate n = 100

list

        value         n
  1.        0       100
  2.        1       100
 
save bx coin

bxmodel     [enter file name bxcoin, n = 2, w/ replacement, 400 repetitions]

bx1 using bxcoin , draw(2)  reps(400)

Box Model Program Starting
(bx0 bootstrap)
Box Model Program Finished

summarize

    Variable |     Obs        Mean   Std. Dev.       Min        Max
-------------+-----------------------------------------------------
         sum |     400      1.0375   .7087663          0          2
        mean |     400      .51875   .3543831          0          1
          sd |     400    .3517856   .3539917          0   .7071068

Example 2:  Coin problem without replacement

bxmodel     [enter file name bxcoin, n = 2, w/o replacement, 400 repetitions]

bx1s using bxcoin , draw(2)  reps(400)

Box Model Program Starting
(bx1s sampling without replacement)
sample size must not be greater than the number of observations
r(498);

[ Oops, we need to change repetitions to be less than 200 ]

bxmodel     [enter file name bxcoin, n = 2, w/o replacement, 100 repetitions]

bx1 using bxcoin , draw(2)  reps(100)

Box Model Program Starting
(bx0 bootstrap)
Box Model Program Finished


summarize

    Variable |     Obs        Mean   Std. Dev.       Min        Max
-------------+-----------------------------------------------------
         sum |     100        1.16   .6922369          0          2
        mean |     100         .58   .3461184          0          1
          sd |     100    .3535534   .3553345          0   .7071068
          
Example 3:  Dice problem with replacement

clear

set obs 6

generate value = _n

generate n = 1

list

        value         n
  1.        1         1
  2.        2         1
  3.        3         1
  4.        4         1
  5.        5         1
  6.        6         1


save bxdice

bxmodel     [enter file name bxdice, n = 2, w/ replacement, 1000 repetitions]

bx1 using bxdice , draw(2)  reps(1000)

Box Model Program Starting
(bx0 bootstrap)
Box Model Program Finished

summarize

    Variable |     Obs        Mean   Std. Dev.       Min        Max
-------------+-----------------------------------------------------
         sum |    1000       6.951   2.441836          2         12
        mean |    1000      3.4755   1.220918          1          6
          sd |    1000    1.352695   1.018702          0   3.535534

Let's try some birthday type problems. Birthday problems... The datasets are created the same way but the output is different. For birthday problems bxmodel geneartes a 1/0 variable (match) indicating whether there were any matches in that trial and the number of unique values (unique).

Example 4:  Birthday problem


clear

set obs 365

generate value = _n

generate n = 1

list in 1/10

         value          n
  1.         1          1
  2.         2          1
  3.         3          1
  4.         4          1
  5.         5          1
  6.         6          1
  7.         7          1
  8.         8          1
  9.         9          1
 10.        10          1

save bxbd

bxmodel     [enter file name bxbd, n = 23, birthday, 500 repetitions]

bxb using bxbd , draw(23)  reps(500)

Box Model Program Starting
(bx0b bootstrap)
Box Model Program Finished

summarize

    Variable |     Obs        Mean   Std. Dev.       Min        Max
-------------+-----------------------------------------------------
       match |     500         .48   .5001002          0          1
      unique |     500      22.368   .7834562         19         23


How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.