##
Bayesian Minitab Macros in

* Bayesian Computation Using Minitab *

By Jim Albert

###
CHAPTER 2 - Simulating Games of Chance

** PROGRAM craps -- Plays game of craps. **

To run: Type exec 'craps'. The program will let you play the game a number of times. At
the end of the games, some summary statistics are displayed.

SAMPLE MINITAB OUTPUT FROM 'craps'

** PROGRAM yahtzee -- Plays simplified version of Yahtzee. **

To run: Type exec 'yahtzee'. Five dice are rolled three times. After each roll, you decide
which dice to keep (if you wish to keep the first, second and fourth dice, you type 1 1 0 1
0). After each roll, the result is printed (two of a kind, small straight, etc.)

SAMPLE MINITAB OUTPUT FROM 'yahtzee'

** PROGRAM yahtz_au -- Computer plays Yahtzee using a particular strategy. **

To run: Type exec 'yahtz_au'. The computer will decide which dice to keep.

** PROGRAM yahtz_re -- Play the computer version of Yahtzee repeatedly. **

To run: Type exec 'yahtz_re' K, where K is the number of times you wish to repeat the
game. (The program 'yahtz_au' must be run first.) The results for each game in coded
form are displayed. The results of the first three rolls are placed in the columns
'ROLL_1', 'ROLL_2', 'ROLL_3'. The column 'F_ROLL' contains the results of the final
roll.

** PROGRAM bball -- Simulates a baseball season using a Bradley-Terry model. **

To run: Type exec 'bball'. One inputs the strength values for the teams. A season is
played where each team plays every other team 8 games. The results of all of the games
are displayed in a 2-way contingency table. The number of games won by all of the
teams are displayed.

SAMPLE MINITAB OUTPUT FROM 'bball'

** PROGRAM bball_re -- Repeats `bball'. Simulates a number of baseball seasons. **

To run: Type exec 'bball_re' K, where K is the number of times you wish to repeat the
simulation. (The program 'bball' must be run first.) The number of games won for all
teams in each season are displayed. The number of wins for all teams are stored in the
columns 'NWIN_1', 'NWIN_2', etc. ('NWIN_1' contains the number of games won by the
team with the first strength value, etc.) The place finishes for all teams are stored in the
columns 'PLACE_1', 'PLACE_2', etc.

###
CHAPTER 3 - Introduction to Inference Using Bayes' Rule

** PROGRAM bayes_se -- Sets up models, prior probabilities, and likelihoods for Bayes'
rule. **

To run: Type exec 'bayes_se'. You input the number of models, the prior probabilities,
the number of observations and names, and the likelihoods.

**
PROGRAM bayes -- Implements Bayes' rule sequentially for independent sequence of outcomes.
**

To run: Type exec 'bayes'. (The program 'bayes_se' must be
run first.) You input the sequence of independent observations and
Bayes rule is implemented sequentially.

SAMPLE MINITAB OUTPUT FROM 'bayes_se' AND 'bayes'

###
CHAPTER 4 - Learning About a Proportion

**
PROGRAM p_disc -- Computes the posterior distribution for p when there is a discrete set of models.
**

To run: Name two Minitab columns 'P' and 'PRIOR'. Place the value of p in 'P' and the
prior probabilities in 'PRIOR'. Then type exec 'p_disc'. The posterior probabilities are
stored in the column 'POST'.

SAMPLE MINITAB OUTPUT FROM 'p_disc'

**
PROGRAM p_disc_p -- Computes predictive distribution for number of successes in
future binomial experiment.
**

To run: The values of p are contained in a column 'P' and the probabilities are contained
either in the columns 'PRIOR' or 'POST'. Type exec 'p_disc_p'. You will input the
number of trials in the future experiment, if you want to use prior or posterior
probabilities for p, and the range of number of successes that you are interested in. The
predictive distribution is stored in the columns 'SUCC' and 'PRED'.

SAMPLE MINITAB OUTPUT FROM 'p_disc_p'

**
PROGRAM beta_sel -- Finds parameters of beta distribution which matches two
predictive probabilities.
**

To run: Type exec 'beta_sel'. You input the probability of a future success, and the
probability of a second success conditional on a first success. The program gives the
matching values of the beta parameters a and b.

SAMPLE MINITAB OUTPUT FROM 'beta_sel'

**
PROGRAM p_beta -- Summarizes beta distribution for p.
**

To run: Type exec 'p_beta'. After inputting the beta parameters a and b, you can see a
graph of the density, compute cumulative probabilities for a list of values of interest, and
compute percentiles of the distribution.

SAMPLE MINITAB OUTPUT FROM 'p_beta'

**
PROGRAM p_beta_p -- For beta prior or posterior, computes predictive distribution for
number of successes in future experiment.
**

To run: Type exec 'p_beta_p'. You input the beta parameters, the number of trials, and
the range of numbers of successes that you are interested in. The predictive distribution
is stored in the columns 'SUCC' and 'PRED'.

SAMPLE MINITAB OUTPUT FROM 'p_beta_p'

**
PROGRAM p_beta_t -- Tests the hypothesis that p is equal to a specific value.
**

To run: Type exec 'p_beta_t'. You input the value of p that you wish to test, the prior
probability of this value, the parameters of the beta distribution under the alternative
hypothesis, and the number of successes and failures in the experiment. The output is the
Bayes factor and the posterior probability that the proportion is equal to the specific
value.

SAMPLE MINITAB OUTPUT FROM 'p_beta_t'

**
PROGRAM p_hist_p -- For a histogram prior for p, finds posterior probabilities of
intervals using simulation.
**

To run: Type exec 'p_hist_p'. Suppose that you can divide
the interval of plausible p values into equally spaced subintervals
and assign a probability to each subinterval. You input the list of
midpoints of the subintervals, the corresponding prior probabilities,
and the data (number of successes and failures). The
posterior probabilities of the intervals are displayed in the output.

SAMPLE MINITAB OUTPUT FROM 'p_hist_p'

###
CHAPTER 5 - Comparing Two Proportions

**
PROGRAM pp_disc -- Finds posterior distribution for (p1, p2) when a uniform prior
on a grid is used.
**

To run: Type exec 'pp_disc'. You input the low and high values of each proportion on
the grid, the number of models, and the number of successes and failures for each sample.
The table of posterior probabilities of (p1, p2) is displayed and graphed. The posterior
probabilities for the difference d = p2 - p1 is shown. The values of p1 and p2 are
stored in the columns 'P1' and 'P2'; the values of the prior and posterior distribution are
stored in the columns 'PRIOR' and 'POST'. The probability distribution for d is stored in
the columns 'DIFF' and 'P_DIFF'.

SAMPLE MINITAB OUTPUT FROM 'pp_disc'

**
PROGRAM pp_disct -- Using a prior which gives a specific probabilitity to p1=p2,
summarizes posterior.
**

To run: Type exec 'pp_disct'. For each proportion, you input the low and high values
and the number of models. Also, you input the prior probability that p1=p2 and the
binomial data. The output is the same as the program 'pp_disc'.

**
PROGRAM pp_discm -- Finds posterior distribution when informative prior is given in table form.
**

To run: Place the values of p1 in column C1 (say), the values of p2 in column C2, and
the table of prior probabilities in columns C3-CN (each row corresponds to the
corresponding value of p1 and all values of p2). Type exec 'pp_discm'. You tell the
program where the prior is located and input the number of successes and failures for
each sample. The output is the same as the program 'pp_disc'.

**
PROGRAM pp_beta -- Using simulation, summarizes distribution of p2-p1 when
proportions have independent beta distributions.
**

To run: -- Type exec 'pp_beta'. Input the parameters of the beta distribution for each
proportion and the number of values to simulate. Program gives dotplot of distribution of
p2-p1 and computes probabilities that p2-p1 exceeds each of a list of values that is
inputted. The simulated samples of p1, p2, and p2-p1 are stored in the columns 'P1', 'P2', and 'P2-P1'.

SAMPLE MINITAB OUTPUT FROM 'pp_beta'

**
PROGRAM pp_bet_t -- Constructs test that p1=p2 using beta priors.
**

To run: Type exec 'pp_bet_t'. Input the prior probability of equality, the beta
distribution for the common value p1=p2, the independent beta distributions for p1 and p2 when p1 is not equal to p2, and the data. The output is the Bayes factor and
the posterior probability of equality.

SAMPLE MINITAB OUTPUT FROM 'pp_bet_t'

**
PROGRAM pp_exch -- Using an exchangeable prior on the logits, summarizes
the posterior distribution of p1, p2.
**

To run: Type exec 'pp_exch'. You input the data, the prior
standard deviation of the normal prior distribution on the logits, and
the number of values to simulate. The simulated samples from the
prior and posterior distributions are stored in the columns
'PRIOR_P1','PRIOR_P2', 'POST_P1', and 'POST_P2'.

SAMPLE MINITAB OUTPUT FROM 'pp_exch'

###
CHAPTER 6 - Learning About a Normal Mean

**
PROGRAM m_disc -- Computes the posterior distribution for M when there is a discrete set of models
(sampling variance known).
**

To run: Name two Minitab columns 'M' and 'PRIOR'. Place the value of M in 'M' and
the prior probabilities in 'PRIOR'. If data is in raw form, then place it into a particular
column of the worksheet . Then type exec 'm_disc'. You input the value of the known
standard deviation and the column number of the data. (if data is in summary form, input
the sample size, sample mean, and sample standard deviation). The posterior
probabilities are stored in the column 'POST'.

SAMPLE MINITAB OUTPUT FROM 'm_disc'

**
PROGRAM normal_s -- Finds parameters of normal distribution which matches two
prior percentiles.
**

To run: Type exec 'normal_s'. One inputs two percentiles (probability to the left and
the corresponding percentile) and the program gives the mean and standard deviation of
the matching normal curve.

SAMPLE MINITAB OUTPUT FROM 'normal_s'

**
PROGRAM m_cont -- Summarizes posterior distribution for normal mean M with a
normal prior (approximate method).
**

To run: If data is in raw form, then place it into a particular column of the worksheet.
(Summary data may also be input.) Type exec 'm_cont'. You input
the mean and standard deviation of the normal prior distribution and
the column number of the data (if data is in summary form, input the
sample size, sample mean, and sample standard deviation). The output
is the mean and standard deviation of the approximate normal marginal
posterior density for M. Also, the normal parameters of the
approximate predictive density for one future observation are given.

SAMPLE MINITAB OUTPUT FROM 'm_cont'

**
PROGRAM normal -- Plots and performs normal density calculations.
**

To run: Type exec 'normal'. You input the mean and standard deviation of the normal
density. The density is graphed and it computes cumulative probabilities and percentiles
of the distribution.

SAMPLE MINITAB OUTPUT FROM 'normal'

**
PROGRAM m_norm_t -- Constructs test that M is equal to specific value using normal
priors (sampling variance known).
**

To run: Place data into a particular column. (Summary data may also be input.) Type
exec 'm_norm_t'. You input the value to be tested, the prior probability of this value, a
list of plausible values for the prior standard deviation of M under the alternative
hypothesis and the population standard deviation. In addition, you input the number of
the column where the data is located. The output is the Bayes factor and the posterior
probability of the hypothesis for each value of the prior standard deviation.

SAMPLE MINITAB OUTPUT FROM 'm_norm_t'

**
PROGRAM m_nchi -- Gives exact analysis for M and S using vague priors.
**

To run: Place data into a particular column. (Summary data
may also be input.) Type exec 'm_nchi'. Plots and gives summary
statistics from the marginal posterior densities of M and the standard deviation S.

SAMPLE MINITAB OUTPUT FROM 'm_nchi'

###
CHAPTER 7 - Learning About Two Normal Means

**
PROGRAM mm_cont -- Gives mean and standard deviation for the normal distribution for the difference M1-M2 when M1 and M2 have independent normal distributions.
**

To run: Type exec 'mm_cont'. You input the mean and standard deviation for each of
the two means and the program gives the mean and standard deviation for the difference
in means.

SAMPLE MINITAB OUTPUT FROM 'mm_cont'

**
PROGRAM mm_tt -- Uses simulation to summarize the difference M2-M1 (difference of two t distributions).
**

To run: Place the two datasets into two columns. (Summary data may also be used.)
Type exec 'mm_tt'. The output is dotplots of the marginal posterior densities for M1 and
M2 and a dotplot and summary statistics for the difference in means.

SAMPLE MINITAB OUTPUT FROM 'mm_tt'

###
CHAPTER 8 - Learning About Relationships

**
PROGRAM lin_reg -- Gives inference about slope and predictive response for simple
regression model.
**

To run: Place the x and y data in two columns. Type exec 'lin_reg'. You input the
numbers of the columns of the data. The output is the mean and standard deviation of the
regression slope. In addition, for each value of x that are inputted, the mean and standard
deviation of the predicted response are given.

SAMPLE MINITAB OUTPUT FROM 'lin_reg'

**
PROGRAM c_table -- Computes a Bayes factors for a two-way contingency table using
uniform priors.
**

To run: Place the contingency table in consecutive columns of the worksheet. Type
exec 'c_table'. Input the numbers of the columns which contain the table. The output is
the usual chi-squared test and a Bayes factor against the hypothesis of independence.

SAMPLE MINITAB OUTPUT FROM 'c_table'

###
CHAPTER 9 - Learning About Discrete Models

**
PROGRAM mod_disc -- Computes a posterior distribution for discrete models.
**

To run: Place model values in the column 'MODEL' and the prior probabilities in
'PRIOR'. Type exec 'mod_disc'. You input the number of the likelihood (binomial,
normal, Poisson, etc.) and the data. The posterior probabilities are stored in the column
'POST'.

SAMPLE MINITAB OUTPUT FROM 'mod_disc'
(learning about the number of successes in a finite population)

**
PROGRAM disc_sum -- Summarizes a discrete probability distribution.
**

To run: Type exec 'disc_sum'. Input the numbers of the columns which contain the
values of the variable and the probabilities. The distribution is graphed and summaries
(mode, mean and standard deviation) are given. Cumulative probabilities and probability
intervals are computed for sets of values of interest.

SAMPLE MINITAB OUTPUT FROM 'disc_sum'

**
PROGRAM mod_crit
-- Compares two discrete prior distributions.
**

To run: Place model values in the column 'MODEL' and the two
sets of prior probabilities in the columns 'PRIOR1' and 'PRIOR2'. Type exec 'mod_crit'. You input the number of the likelihood (binomial, normal, Poisson, etc.)
and the data. The two sets of corresponding posterior probabilities
are stored in the columns 'POST1' and 'POST2'. The Bayes factor in support of the
first prior is given; this value is stored in the column 'BAYES_F'.

SAMPLE MINITAB OUTPUT FROM 'mod_crit'

###
CHAPTER 10 - Learning About Continuous Models

**
PROGRAM mod_cont -- Computes the posterior
distribution for continuous models.
**

To run Simulate a large number of values from the prior
distribution of the parameter. Place these values in the column
'PRIOR_S'. Type exec 'mod_cont'. You input the number of the
likelihood (binomial, normal, Poisson, etc.) and the data. A
simulated sample from the posterior density is stored in the column 'POST_S'.

SAMPLE MINITAB OUTPUT FROM 'mod_cont'
(learning about the size of a population by capture-recapture
sampling)

###
CHAPTER 11 - Summarizing Posterior Distributions

** SETUP FOR ONE PARAMETER **

For one real-valued parameter, place the definition of the logarithm
of the posterior density in the macro 'logpost1' -- the input column
is 'X' and the output column is 'F'.

**
PROGRAM laplace1 -- Summarizes a one-parameter posterior density defined in macro
'logpost1' using the Laplace method.
**

To run: Type exec 'laplace1'. Input a guess at the mode and
the number of iterations. The output is an estimate at the posterior mode and
standard deviation and an estimate at the logarithm of the
normalizing constant.

SAMPLE MINITAB OUTPUT FROM 'laplace1'

**
PROGRAM ad_quad1 -- Summarizes a one-parameter posterior density defined in macro 'logpost1' using adaptive quadrature.
**

To run: Type exec 'ad_quad1'. Input guesses at the mean
and standard deviation of the parameter and the number of iterations.
Estimates at the posterior mean and standard deviation
and the logarithm of the normalizing constant are output. The
grid, density values, and weights are stored in the columns 'X', 'F', 'WT'.

SAMPLE MINITAB OUTPUT FROM 'ad_quad1'

**
PROGRAM metrop -- Summarizes a one-parameter posterior density defined in macro 'logpost1' using simulation.
**

To run: Type exec 'metrop'. Input a starting location and
the number of values to simulate. The simulated values are stored in
the column 'POST_S'.

SAMPLE MINITAB OUTPUT FROM 'metrop'

** SETUP FOR TWO PARAMETERS **

For two real-valued parameters, place the definition of the logarithm
of the posterior density in the macro 'logpost2' -- the input columns
are 'X' and 'Y' and the output column is 'F'.

**
PROGRAM laplace2 -- Summarizes a two-parameter posterior density defined in macro
'logpost2' using the Laplace method.
**

To run: Type exec 'laplace2'. Input a guess at the mode and
the number of iterations. The output is an estimate at the mode and
posterior standard deviations and covariance and an estimate at the
logarithm of the normalizing constant.

SAMPLE MINITAB OUTPUT FROM 'laplace2'

**
PROGRAM ad_quad2 -- Summarizes a two-parameter posterior density defined in macro 'logpost2' using adaptive quadrature.
**

To run: Type exec 'ad_quad2'. Input guesses at the
posterior moments of the two parameters and the number of
iterations. The output is estimates at the posterior moments and an
estimate at the logarithm of the normalizing constant. The grid and
density values on the grid are stored in the columns 'X', 'Y', and
'F'.

SAMPLE MINITAB OUTPUT FROM 'ad_quad2'

**
PROGRAM gibbs -- Summarizes a two-parameter posterior density defined in macro 'logpost2' using simulation.
**

To run: Type exec 'gibbs'. Input a starting location and the number
of values to simulate. The simulated values are stored in the columns
'POST_X' and 'POST_Y'.

SAMPLE MINITAB OUTPUT FROM 'gibbs'