IPRB: Mendel's First Law | Ben Cunningham

IPRB: Mendel's First Law

Problem by Rosalind · on December 4, 2012

Probability is the mathematical study of randomly occurring phenomena. We will model such a phenomenon with a random variable, which is simply a variable that can take a number of different distinct outcomes depending on the result of an underlying random process.

For example, say that we have a bag containing 3 red balls and 2 blue balls. If we let represent the random variable corresponding to the color of a drawn ball, then the probability of each of the two outcomes is given by and .

Random variables can be combined to yield new random variables. Returning to the ball example, let model the color of a second ball drawn from the bag (without replacing the first ball). The probability of being red depends on whether the first ball was red or blue. To represent all outcomes of and , we therefore use a probability tree diagram. This branching diagram represents all possible individual probabilities for and , with outcomes at the endpoints (“leaves”) of the tree. The probability of any outcome is given by the product of probabilities along the path from the beginning of the tree; see Figure 2 for an illustrative example.

An event is simply a collection of outcomes. Because outcomes are distinct, the probability of an event can be written as the sum of the probabilities of its constituent outcomes. For our colored ball example, let be the event “ is blue.” is equal to the sum of the probabilities of two different outcomes: , or (see Figure 2 above).

Given: Three positive integers , , and , representing a population containing organisms: individuals are homozygous dominant for a factor, are heterozygous, and are homozygous recessive.

Return: The probability that two randomly selected mating organisms will produce an individual possessing a dominant allele (and thus displaying the dominant phenotype). Assume that any two organisms can mate.

Sample Dataset

2 2 2

Sample Output

0.78333

R

library(dplyr)

f <- "iprb.txt"

pop <- data_frame(
  a = 1,
  g = c("k", "m", "n"),
  n = 
    readLines(f) %>%
    strsplit(split = " ") %>%
    unlist() %>%
    as.numeric()
)

mate <-
  left_join(pop, pop, by = "a") %>%
  mutate(
    n = n.x,
    m = ifelse(g.x != g.y, n.y, n - 1),
    p = m * n * case_when(
      g.x == "k" | g.y == "k" ~ 1,
      g.x == "m" & g.y == "m" ~ 0.75,
      g.x == "n" & g.y == "n" ~ 0,
      TRUE                    ~ 0.5
    )
  )

cat(sum(mate$p) / (sum(pop$n) * (sum(pop$n) - 1))) 
0.7833333