ENGI 3423 - Probability Simulation using MINITAB


In this session we shall use Minitab® to Also provided on this web page are

Creation of a Macro File

Copy the following block of text and paste it into a blank document in a word processor (even Notepad will suffice).


gmacro
Birthdays
### Simulation to estimate the probability that at least two of (k1) people
### in a room will share a birthday.   The lowest (k1) for which the exact 
### probability exceeds 1/2 is k1 = 23 .
### Macro created 2003 03 10 and last modified 2003 03 12 by G.H. George

  erase c1-c8           # needed to ensure clear space in the worksheet
### Constants:
### k1 = number of people in the room
### k2 = number of trials
### k3 = trial index number (loop counter)
### k4 = relative frequency of shared birthdays
### Columns:
  name c1 'birthdays'   # birthdays of the (k1) people in this trial
  name c2 'ranks'       # ranks of birthday data in this trial
  name c3 'no ties'     # ranks if no ties (1 to #people)
  name c4 'diff.'       # abs.diff. of ranks with and without ties in this trial
  name c5 'num.shared'  # number of duplicates found in each trial
  name c6 'shared?'     # = 1 if any duplicates found, 0 if none found, for each trial
  name c7 'num.people'  # temporary column for input of k1
  name c8 'num.trials'  # temporary column for input of k2

### Note that keyboard data entry must be into columns, not constants:
  note Enter number of people:
  set c7;
    file "terminal";
    nobs 1.
  copy c7 k1
  note Enter number of trials:
  set c8;
    file "terminal";
    nobs 1.
  copy c8 k2
### Place the first (k1) natural numbers into column c3:
  set c3
    1:k1
    end.

  do k3=1:k2        # conduct the k2 simulations
      random k1 c1;
        integer 1 365.  # assign random birthdays to the k1 people in this trial
      sort c1 c1    # sort the birthdays into ascending order
      rank c1 c2
      let c4=abs(c2-c3) # zero unless shared birthdays are present in this trial
      let c5(k3)=sum(c4)
  enddo

### Each entry of c6 is 1 (true) if the corresponding entry of c5 is positive, 
### 0 (false) otherwise:
  let c6=(c5>0)

  let k4=sum(c6)/k2
  note The estimated probability that at least two people share the same birthday is
  print k4
  note Compare this with the true probability 
  note p = 1 - (365! / {(365-Num.people)!*(365)^(Num.people)})
  note (approximately 0.507 for 23 people)
endmacro

Save the file as "m:/Birthdays.mac" (or "a:/Birthdays.mac").

If you have Minitab installed on your computer. then save the macro in your Minitab Macros directory, (on some computers, it is at "C:/Program Files/MTBWIN/Macros/"), with the name "Birthdays.mac".


A note about file extensions:

By default, Windows / Notepad insists on appending a hidden extension ".txt" to the name of your newly-created macro file.   However, Minitab recognizes only files whose names end with ".mac" to be macro files.   To escape from this problem,

Now inspect the name of your file.   If it ends in ".txt", then right-click on the file name and rename it to "Coins.mac".

The main alternative is to enclose the macro’s filename in double quotes in the "File name" field of the "Save As" dialog box, every time that you wish to save a macro file.


Some Notes on Macro Syntax

The first two lines of most Minitab global macros are

    gmacro
    macroname
and the last line is
    endmacro

The line indentation and blank lines are not required.
However, as is the case with any type of computer program, a macro is much easier to read if these style conventions are employed.

The comment symbol ("//" in C++ and "'" in Visual Basic) is the hash mark # in a Minitab macro.   All text following # in a line is ignored by Minitab.   If you want other readers to know how your macro achieves its objectives, then make good use of comments.

To give a name to a column, use the structure

    name cn 'nameOfColumn'
where n is the number of the column in the worksheet in the Data window.

Before naming a column or entering data into it, check if your algorithm requires any existing data to be cleared first.   That will be the case here, if the same macro is called a second time inside the same project with a smaller number of people in the room and/or a smaller number of trials.   The command to clear a range of columns is

    erase cm-cn

The input prompt is all text in a line following the keyword note.

Input from the keyboard uses the structure

    set cn;
      file "terminal";
      nobs 1.
The semicolon ";" indicates that the Minitab command is not complete and continues with subcommands on the next line, while the period "." closes the command.

In principle, more than one datum can be input in the same keyboard response, (separated by a space).   In that case the number of data replaces the "1" in "nobs 1.".   However, it is usually safer to ask the user to input the data one item at a time.

Also note how data may be entered directly from the keyboard only into a worksheet column, not into a constant.   Input data may be copied into a constant using the structure

    copy cm kn

To print the values of constants in the session window, use

    print kn
Columns appear automatically in the spreadsheet in the Data window.

The principle loop structure (essentially a FOR-NEXT loop with unit step size) is

    do k1=k2:k3
        statements
    enddo

Some Notes on This Macro

Most of the details of this macro are explained by inline comments in the listing near the top of this page.   One feature deserves additional comment here:   the trick used to detect equal values in a column of data.

Use the "rank" command to create a column showing the ranks of all entries in the column of (birthdays sorted into ascending order).   The n ranks will be exactly the first n natural numbers, unless any ties exist (that is, shared birthdays).   The sum of the absolute differences between the corresponding entries in the ranks column (c2) and the column containing the first n natural numbers (c3) will be zero if there are no ties, but positive if there are one or more ties.   This part of the algorithm is implemented in the lines inside the do loop

      sort c1 c1    # sort the birthdays into ascending order
      rank c1 c2
      let c4=abs(c2-c3) # zero unless shared birthdays are present in this trial
      let c5(k3)=sum(c4)
Each entry in the column c6 represents the result of one simulation.   Its value is 1 if any ties were detected and 0 if not.   By adding up all entries in that column, the total frequency of shared birthdays is calculated.

Execution of the Macro File

Start Minitab.

[screenshot]  

Click anywhere in the Session window.

On the main menu bar, click on "Editor".
If "Enable Commands" is not checked,
then click on it.

This step is necessary in order to
enter data from the keyboard
directly into the Session window.



The Minitab prompt then
appears in the Session window.

  [screenshot]


In the Session window type
"%m:/Birthdays",
(or the drive, path and name you have given to your macro,
immediately after the "%" symbol)
and press the "Enter" key.

  [screenshot]


Respond to the first prompt for data.

[screenshot]
[screenshot]

Note how the first eight columns of the Worksheet have now been named.


Respond to the second
prompt for data.

  [screenshot]


The results now appear.
They will usually be slightly different with each run.

[screenshot]

You can copy and paste the above results into your Report Pad.
Then save your work and exit.


  [Return to the index of demonstration files]         [To the next tutorial]
  [Return to your previous page]

Created 2003 03 12 and most recently modified 2007 07 09 by Dr. G.H. George