1 Integration

Approximate the integral \[ \int_0 ^{\infty} \sin(x) x^2 e^{-x} dx\] by appealing the law of large numbers and using R. Hint: Consider an i.i.d. sequence of exponential random variables all with rate \(1\).

1.1 Solution

Let \(X_i\) be i.i.d. exponential random varaibles with rate \(1\). By the change of variables formula, we have \[\mathbb{E} g(X_1) = \int_0 ^{\infty} g(x) e^{-x}dx;\] take \[g(x) = \sin(x) x^2.\] Since \(g(X_i)\) are independent are also i.i.d. the law of of large numbers gives \[ n^{-1}[g(X_1) + \cdots+ g(X_n) ] \to \mathbb{E} g(X_1).\] This is easily simulated in R.

g <- function(x){
y=  (x^2) * (sin(x))
x <- rexp(10000,1)
## [1] 0.4828766

2 Grouping coins

Again, consider a sequence of 20 fair coin flips, as discussed in our first live session. using \(R\), estimate the probability that we will see a run of at least four heads. Hopefully, we get a number bigger than \(0.27\).

2.1 Solution

The isFour function checks if a sequence of bits contains a run of four heads.

isFour <- function(x){
  for (i in 1:(length(x)+1-4))
  if ( x[i]==1 && x[i+1]==1 && x[i+2]==1 && x[i+3]==1 ){ans<- 1}

Next, we generate many sequences of fair coin flips, and compute the average number of times a run of four heads occurs.

num=replicate(1000, isFour(rbinom(20,1,0.5))  )
## [1] 0.488

3 Pen and paper?

Let \((U_i)_{i \in \mathbb{Z} ^+}\) be a sequence of independent random variables that are uniformly distributed in \([0,1]\). Let \[S_n = X_1 + \cdots + X_n.\] Let \[T = \inf\{n \geq 1: S_n >1\}\] so that \(T\) is the first time the sum is greater than \(1\). Use R or pen and paper to compute \(\mathbb{E} T\).

3.1 Solution by pen and paper

Let \(a \in [0,1]\). We first show that \[\mathbb{P}(U_1 + \cdots +U_n \leq a ) = \frac{a^n}{n!}.\] We proceed by induction. The result is obvious in the case \(n=1\). Assume the result for \(n \geq 1\). Set \(Z = U_1 + \cdots +U_n\). Note that \(Z\) is independent of \(U_{n+1}\).
By the induction hypothesis we know the density of \(Z\) (at least up to value \(a=1\)). Hence we have that \[P(Z + U_{n+1} \leq a ) = \int_0^a\int_0 ^{a-z} \frac{z^{n-1}}{(n-1)!} du dz,\] and the inductive step follows from an easily calculation.

Next, we show that for any integer-valued random variable \(X \geq 0\), we have \[\mathbb{E} X = \sum_{n=1} ^{\infty} \mathbb{P}(X \geq n).\] Note that \[X = \sum_{i=1} ^{\infty} \mathbf{1}[X \geq i],\] so that \[ \begin{eqnarray*} \mathbb{E} X &=& \sum_{i=1} ^{\infty} \mathbb{E} \mathbf{1}[X \geq i] \\ &=& \sum_{i=1} ^{\infty} \mathbb{P} (X \geq i) \end{eqnarray*} \] Finally, we note that \[\{T \geq n\} = \{ U_1 + \cdots +U_{n-1} < 1\}.\] So, we can now compute \(\mathbb{E} T = e\).

3.2 Solution by R

In R it is easy!

T <-function() {
n=0; s=0
while (s <1) {s <- s + runif(1); n <- n+1}
z=replicate(1000, T())
## [1] 2.677

4 Version: 25 October 2020