# [R] Combinations and joint probabilities

G. Jay Kerns gkerns at ysu.edu
Fri Dec 4 13:28:34 CET 2009

```Dear Amelia,

On 12/4/09, Amelia Livington <amelia_livington at yahoo.com> wrote:
>
> Dear R helpers
>
> Suppose I have two sets of ranges (interest rates) as
>
> Range 1 : (7 – 7.50, 7.50 – 8.50, 8.50 – 10.00) with respective
> probabilities 0.42, 0.22 and 0.36.
>
>
> Range II : (11-12, 12-14, 14-21) with respective probabilities 0.14, 0.56
> and 0.30 respectively.
>
>
> My problem is to form the combinations of these ranges in a decreasing order
> of joint probabilities. It is assumed that these ranges are independent.
>
> Suppose A represents (7-7.50), B represents (7.50-8.50) and C represents
> (8.50 – 10.00).
> Also let X be (11-12), Y is (12-14) and Z is (14-21).
>
> These two groups are independent i.e. Prob(A and Y) = P(A) * P(Y)
>
> So there are 9 combinations possible as (AX, AY, AZ, BX, BY, BZ, CX, CY and
> CZ) respectively with the joint probabilities (0.059, 0.235, 0.126, 0.031,
> 0.123, 0.066, 0.05, 0.202, 0.108) respectively.
>
> My problem is
> (i)                               How to obtain these 9 combinations of
> probabilities in the sense how do I obtain the various combinations of these
> two ranges along-with their respective probabilities;
> (ii)                             How to arrange these 9 probabilities in
> descending order against the respective group combination i.e. for the
> combination AY, the joint probability is maximum at 0.235, followed by CY at
> 0.202 and so on.
>
> I sincerely apologize as perhaps I might not have raised the query properly.
> I have become member of this group today only and its been hardly a week
> since I have started learning R language. I have easily done this in Excel.
> My output should be something like this –
>
> Combination   Probability
> AY                   0.235
> CY                   0.202
> AZ                  0.126
> BY                   0.123
> CZ                  0.108
> BZ                  0.066
> AX                  0.059
> CX                  0.050
> BX                   0.031
>
>
> I request you to guide me.
>
>
> Amelia
>

This sounds like a homework problem, which is a strict no-no for this
list;  be sure to read the Posting Guide.  I will give you the benefit
of the doubt for now, but be warned for the future.

Are you looking for something like the following?

Probspace <- expand.grid(LETTERS[1:3], LETTERS[24:26])

marginals <- list()
marginals[[1]] <- c(0.42, 0.22, 0.36)
marginals[[2]] <- c(0.14, 0.56, 0.30)

pdf <- expand.grid(marginals)
Probspace\$probs <- apply(pdf, 1, prod)

# the joint pmf
xtabs(probs ~ Var1 + Var2, data = Probspace)

# sorted by probability
Probspace[order(Probspace\$probs), ]

See the prob package for other utilities in this vein. You can make
the labels better with c("7 – 7.50", "7.50 – 8.50", "8.50 – 10.00"),
etc, in the first line instead of LETTERS.

Welcome to R.
Jay

P.S.  Please don't send HTML.  Again, in the Posting Guide.

>
> 	[[alternative HTML version deleted]]
>
>

***************************************************
G. Jay Kerns, Ph.D.
Associate Professor
Department of Mathematics & Statistics
Youngstown State University
Youngstown, OH 44555-0002 USA
Office: 1035 Cushwa Hall
Phone: (330) 941-3310 Office (voice mail)
-3302 Department
-3170 FAX
VoIP: gjkerns at ekiga.net
E-mail: gkerns at ysu.edu
http://people.ysu.edu/~gkerns/

```