Let’s se R to generate a single Binomial, Geometric, and Poisson random variable.
You can choose the parameter of the distribution to be anything you like.
Here are the function names: rbinom, rgeom, rpois
. Remember that the “r” stands for “Random”.
Reading the help files is hard at first, but it is an essential skill. Have a look at the help files for these functions. What are the 8 headings?
?rbinom
?rgeom
?rpois
You can type your (short!) answer here:
# generate a single Binomial random variable:
rbinom(n = 1, size = 10, prob = .3)
# generate a single Geometric random variable:
# generate a single Poisson random variable:
# generate 10 Binomial random variables:
# generate 10 Geometric random variables:
rgeom(n=10, p=0.25)
# generate 10 Poisson random variables:
One way to think about random variables is through their “distribution”. To see what the distributions of different random variables look like, generate 1,000 copies of some random variables (indicated in the code below) and put them in a histogram:
# Exponential:
hist(rexp(n = 1000, rate=1/5))
# or
library(magrittr)
rexp(10000, rate=1/5) %>% hist
# Binomial:
# Normal:
Repeat the last step, but change the value of the parameter that you put in.
# Exponential:
hist(rexp(n = 1000, rate = 1))
# or
library(magrittr)
rexp(1000, rate=1) %>% hist
# Binomial: (change p, not the size parameter)
# Binomial: (change the size parameter, not p)
# Normal: (change the mean, not the standard deviation)
# Normal: (change the standard deviation, not the mean)
Answer in words:
Now let’s answer the same question, but this time using the built-in R functions for evaluating probability distributions. That is, instead of generating random variables to approximate the true distribution, let’s evaluate the probability mass or density exactly.
# Exponential, for values k=0,1,2,...,20:
#
k <- seq(0,20);
plot( k, dexp(k,rate=1) )
# Binomial(n=10,p=0.3), for values k=0,1,2,...,9,10:
#TODO: write code here.
# Normal(mean=0,sd=1), for x in [-3,3] in increments of size 0.1
x <- seq(-3,3,0.1);
# Note that this is a density, not a mass function,
# but for plotting purposes, we can't evaluate the pdf at
# EVERY point, so we'll just choose a few.
plot( x, dnorm(x,mean=0,sd=1))
# Normal(mean=1,sd=2), for x in [-5,7] in increments of size 0.1
#TODO: write code here.
This is not a programming course, but it’s important that you become familiar with some basic programming concepts this semester. Toward that end, let’s close by getting some practice with function definition in R.
# 5a) Define a function sum_of_normals that takes a single argument n (you may assume that n is a non-negative integer), generates n independent normals with mean 0 and variance 1, and returns their sum.
# Hint: pay attention to the case n=0. What should be the sum of zero normals?
sum_of_normals <- function( n ){
#TODO: CODE GOES HERE.
}
# 5b) Define a function sum_of_poissons that takes two arguments: a non-negative integer n and a positive real lambda.
# Your function should generate n independent Poisson random variables with shared parameter lambda, and return their sum.
# You may assume that n is a non-negative integer and lambda is a positive real (i.e., numeric)
# n and lambda should both default to 1, so that sum_of_poissons(5) returns the sum of five Pois(1) random variables.
sum_of_poissons <- function( TODO: arguments! ) {
# TODO: CODE GOES HERE!
}
# 5c) Define a function max_of_normals that takes three arguments:
# a non-negative integer n
# a real number (i.e., numeric) mu
# a positive real (i.e., positive numeric) sigma
# Your function should generate n independent normals with mean and sd given by mu and sigma, respectively, and return the maximum of those n normal RVs.
# mu should default to 0 and sigma should default to 1. There should be no default value for n.
# Hint: be careful of the case n=0. By convention, the maximum of an empty set (e.g., the maximum of a collection of zero normals)
# By convention, the maximum of an empty set is negative infinity.
#TODO: define the function max_of_normals.