Module #5 Random Variables & Probability distributions
This week we looked at random variables and probability distributions.
The first assignment was to take two sets of values and their probabilities and determining the standard deviation for both.
The R code I used for the first set is:
t1 <- data.frame(x=c(0, 1, 2, 3), p=c(0.5, 0.2, 0.15, 0.10)) t1mu <- sum(t1$x * t1$p) t1var <- sum((t1$x - t1mu)^2 * t1$p) t1sd <- sqrt(t1var) t1sd
The standard deviation was reported as 1.013903.
The code for the second set was:
t2 <- data.frame(x=c(1, 3, 5, 4), p=c(0.10, 0.2, 0.6, 0.2)) t2mu <- sum(t2$x * t2$p) t2var <- sum((t2$x - t2mu)^2 * t2$p) t2sd <- sqrt(t2var) t2sd
The standard deviation for the second set was 1.369306.
The standard deviation was higher for the second set, probably because the second set had a high probability attached to its largest value, and the range was higher for the second set as well.
The second part of the assignment called for finding p(x=0) for a binomial distribution where the number of trials was 4 and the probability of success was 0.12.
The code I used to solve the problem was:
dbinom(0, 4, 0.12)
The result for p(0) was 0.5996954.
The final part of the assignment called for the creation of 20 Poisson numbers and calculating the value of the mean and the variance of the results. Lambda wasn’t provided, so I used 3 for the value. The code I ran was:
poisvec <- rpois(20, 3) mean(poisvec) var(poisvec)
I ran the code a few times to see how much it would vary. The expected mean and variance would be around 3 because that’s the point of Poisson variables – lambda represents the target mean and variance.
The first run I got a mean of 2.8 and a variance of 3.22. The second time I got a mean of 3.2 and a variance of 4.69. The third run I got a mean of 3.05 and a variance of 2.16.
Generating 20 values allowed for a good deal of variety in the variance, apparently.