Approach 1
sum_one_to_n_1 <- function(n) {
sum_seq <- seq(1,n)
sum(sum_seq)
}
Approach 2
sum_one_to_n_2 <- function(n) {
n*(n+1)/2
}
Approach 3
sum_one_to_n_3 <- function(n) {
total <- 0
sum_seq <- seq(1,n)
for (i in 1:n) {
total <- total + sum_seq[i]
}
total
}
Finally, let’s check if all three functions give the same answer:
sum_one_to_n_1(100)
## [1] 5050
sum_one_to_n_2(100)
## [1] 5050
sum_one_to_n_3(100)
## [1] 5050
Yes, they give the same answer.
Part a
is_prime <- function(x) {
if (x==1 | x==2) {
return(TRUE)
}
possibly_prime <- TRUE
possible_factors <- seq(2,x-1)
for (z in possible_factors) {
x_mod_z <- x %% z
if (x_mod_z == 0) {
possibly_prime <- FALSE
break
}
}
possibly_prime
}
Let me explain this code a little. The first if
indicates that we will call 1 and 2 prime numbers. Next,
possible_factors
contains every integer from 2
to x-1
. If x
is divisible by any of these
numbers, then it will not be prime. The for
loops over all
possible factors and computes x %% z
where z
is a possible factor. If this is equal to 0, it indicates that
z
is a factor of x
(and, hence, that
x
is not prime). In that case, we set
possibly_prime=FALSE
, and this is what we will return. As a
side-comment, the break
line “jumps out” of the for loop if
this condition holds (i.e., once we find a factor, we do not need to
find any more), but the code will still work without this line. If we go
through all possible factors and find that there are none, then we will
never change possibly_prime
to be FALSE
and,
therefore, we will return TRUE
indicating that
x
is prime.
Let’s check if this works
is_prime(5)
## [1] TRUE
is_prime(8)
## [1] FALSE
is_prime(17)
## [1] TRUE
is_prime(18)
## [1] FALSE
Part b
Given that we have a way to check if a number is prime (from part a), it is fairly easy to compute all prime numbers from \(1\) to \(n\).
prime <- function(n) {
out <- c()
counter <- 1
for (i in 1:n) {
if (is_prime(i)) {
out[counter] <- i
counter <- counter+1
}
}
out
}
This code is easier than the previous one. First, we create a vector
out
that is going to hold the list of the prime numbers.
Second, counter
is going to hold the place in
out
to put the next prime number. for
loops
over all positive integers from \(1\)
to \(n\) and checks if each one is
prime or not. If it is, we add it to out
in the correct
place (notice that counter <- counter+1
just means that
we move to the next place in the out
vector that we are
building up — we only do this if i
really is a prime
number). Finally, we return the out
that we have
created.
Let’s check if this works.
prime(100)
## [1] 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89
## [26] 97
This is the correct list of all prime numbers between 1 and 100.
Part a
\[ \begin{aligned} \mathbb{E}[Y] &= \mathbb{E}[5+9X] \\ &= \mathbb{E}[5] + \mathbb{E}[9X] \\ &= 5 + 9\mathbb{E}[X] \\ &= 95 \end{aligned} \]
where the first equality holds by the definition of \(Y\), the second equality holds because expectations can pass through sums, the third equality holds because the expectation of a constant is just the constant itself and because constants can come outside of expectations, and the last equality holds because \(\mathbb{E}[X]=10\).
Part b
\[ \begin{aligned} \mathrm{var}(Y) &= \mathrm{var}(5+9X) \\ &= \mathrm{var}(9X) \\ &= 81 \mathrm{var}(X) \\ &= 162 \end{aligned} \]
where the first equality holds by the definition of \(Y\), the second equality holds because \(5\) is a constant and doesn’t contribute to the variance, the third equality holds because constants can come out of the variance (after squaring it), and the last equality holds because \(\mathrm{var}(X)=2\).
\[ \begin{aligned} \mathrm{var}(bX) &= \mathbb{E}\big[ (bX - \mathbb{E}[bX])^2 \big] \\ &= \mathbb{E}\big[ (bX - b\mathbb{E}[X])^2 \big] \\ &= \mathbb{E}\Big[ \big(b(X-\mathbb{E}[X])\big)^2\Big] \\ &= \mathbb{E}\big[b^2 (X-\mathbb{E}[X])^2\big] \\ &= b^2 \mathbb{E}\big[(X-\mathbb{E}[X])^2\big] \\ &= b^2 \mathrm{var}(X) \end{aligned} \]
where the first equality holds by the definition of variance, the second equality holds because \(b\) can come out of the inside expectation because it is a constant, the third equality factors out the \(b\) from both terms, the fourth equality squares the inside terms, the fifth equality holds because \(b^2\) is a constant and can therefore come out of the expectation, the last equality holds because \(\mathrm{var}(X) = \mathbb{E}[(X-\mathbb{E}[X])^2]\) (which is just the definition of the variance of \(X\)).
Part a
\[ \begin{aligned} \mathbb{E}[Y] &= \mathbb{E}[Y|X=1] \mathrm{P}(X=1) + \mathbb{E}[Y|X=0]\mathrm{P}(X=0) \\ &= \mathbb{E}[Y|X=1] \mathrm{P}(X=1) + \mathbb{E}[Y|X=0](1-\mathrm{P}(X=1)) \\ &= 5^{'} 4^{''} (0.5) + 5^{'} 9^{''} (0.5) \\ &= 5^{'} 6.5^{''} \end{aligned} \]
Part b
The answer from part a is related to the law of iterated expectations because the key step in that problem is to relate the overall expectation, \(\mathbb{E}[Y]\), to the conditional expectations, \(\mathbb{E}[Y|X=0]\) and \(\mathbb{E}[Y|X=1]\). The law of iterated expectations says that unconditional expectations are equal to averages of conditional expectations, which is what we use in the first step of the answer for part a.
Part a
\(f_X(21) = 0.1\). We know this because the sum of the pdfs across all possible values of \(X\) must add up to 1.
Part b
\[ \begin{aligned} \mathbb{E}[X] &= \sum_{x \in \mathcal{X}} x f_X(x) \\ &= 2 f_X(2) + 7 f_X(7) + 13 f_X(13) + 21 f_X(21) \\ &= 2 (0.5) + 7 (0.25) + 13 (0.15) + 21 (0.1) \\ &= 6.8 \end{aligned} \]
Part c
To calculate the variance, I’ll use the expression \(\mathrm{var}(X) = \mathbb{E}[X^2] - \mathbb{E}[X]^2\). Thus, the main new thing to calculate is \(\mathbb{E}[X^2]\):
\[ \begin{aligned} \mathbb{E}[X^2] &= \sum_{x \in \mathcal{X}} x^2 f_X(x) \\ &= 2^2 f_X(2) + 7^2 f_X(7) + 13^2 f_X(13) + 21^2 f_X(21) \\ &= 4 (0.5) + 49 (0.25) + 169 (0.15) + 441 (0.1) \\ &= 83.7 \end{aligned} \]
Since, we already calculated \(\mathbb{E}[X] = 6.8\) in part a, this implies that \(\mathbb{E}[X]^2 = 46.24\). Thus, \[ \begin{aligned} \mathrm{var}(X) &= 83.7 - 46.24 \\ &= 37.46 \end{aligned} \]
Part d
\[ F_X(1) = 0 \]
since the smallest possible value of \(X\) is 2.
\[ \begin{aligned} F_X(7) &= f_X(2) + f_X(7) \\ &= 0.75 \end{aligned} \]
\[ \begin{aligned} F_X(8) &= f_X(2) + f_X(7) \\ &= 0.75 \end{aligned} \] \[ \begin{aligned} F_X(25) &= 1 \qquad \end{aligned} \]
since all possible values that \(X\) can take are less than 25.