IEV: Calculating Expected Offspring | Ben Cunningham

IEV: Calculating Expected Offspring

Problem by Rosalind · on December 4, 2012

For a random variable taking integer values between 1 and , the expected value of is . The expected value offers us a way of taking the long-term average of a random variable over a large number of trials.

As a motivating example, let be the number on a six-sided die. Over a large number of rolls, we should expect to obtain an average of 3.5 on the die (even though it’s not possible to roll a 3.5). The formula for expected value confirms that .

More generally, a random variable for which every one of a number of equally spaced outcomes has the same probability is called a uniform random variable (in the die example, this “equal spacing” is equal to 1). We can generalize our die example to find that if is a uniform random variable with minimum possible value and maximum possible value , then . You may also wish to verify that for the dice example, if is the random variable associated with the outcome of a second die roll, then .

Given: Six nonnegative integers, each of which does not exceed 20,000. The integers correspond to the number of couples in a population possessing each genotype pairing for a given factor. In order, the six given integers represent the number of couples having the following genotypes:

  1. AA-AA
  2. AA-Aa
  3. AA-aa
  4. Aa-Aa
  5. Aa-aa
  6. aa-aa

Return: The expected number of offspring displaying the dominant phenotype in the next generation, under the assumption that every couple has exactly two offspring.

Sample Dataset

1 0 0 1 0 1

Sample Output




f <- "iev.txt"

pop <- data_frame(
  p = c(1, 1, 1, 0.75, 0.5, 0),
  n =
    readLines(f) %>%
    strsplit(split = " ") %>%
    unlist() %>%

sum(2 * pop$n * pop$p) %>%