Lab 1

Question 1

The expression:

\[4 \sum_{i=1}^n \frac{(-1)^{i+1}}{2i-1} = 4 \left(\frac{1}{1} - \frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\cdots\right)\] slowly converges to \(\pi\) as \(n\) approaches \(\infty\).

(a). Calculate it for \(n=100\) using the vectorized functions and operators, making use of the recycling rule as much as possible. [Hint: Please avoid using for loop and instead utilize vectorized calculations. Please make sure to use the c(1, -1) vector.]

(b). In your code from part (a), identify which functions or operators are vectorized. And explain how the recycling rule is applied (specify which is the shorter vector and which is the longer vector).

(c). Calculate the value when \(n=1,000, n = 1,000,000,\) and \(n = 10,000,000\) 1,000,000,000 , respectively. Are the results converges to \(\pi\) as \(n\) approaches \(\infty\)? Please state your conclusion. [Hint: This calculation might take some time to complete.]

Question 2

Let \(x\) and \(y\) be two vectors of identical lengths \(n\), say

set.seed(1) 
x <- rnorm(100) # rnorm(100) generate 100 random numbers independently from N(0, 1)
y <- 2 * x + 10 + rnorm(100, 0, 0.5) 
# similarly, rnorm(100, 0, 0.5) generates 100 random numbers independently
# from normal distribution with mean 0 and standard deviation 0.5
  • Please refer to Section 2.1.5 in Deep R Programming for more information about set.seed() and rnorm(). Please feel free to use help(set.seed) and help(rnorm) as well.

(a). Compute the Pearson linear correlation coefficient given by the formula below. Please perform the calculation using the vectorised functions like mean(), sum() and the operators.

\[r = \frac{\sum_{i=1}^n\left(x_i - \frac{1}{n}\sum_{j=1}^nx_j\right)\left(y_i - \frac{1}{n}\sum_{j=1}^n y_j\right)}{\sqrt{\sum_{i=1}^n\left(x_i - \frac{1}{n}\sum_{j=1}^nx_j\right)^2} \sqrt{\sum_{i=1}^n\left(y_i - \frac{1}{n}\sum_{j=1}^ny_j\right)^2}}\]

(b). To verify that your implementation in part (a) is correct, compare your result with the output of cor(x, y). Are the two values the same, up to a small decimal difference? State your conclusion.

[Note that, the identical() function may not work as expected, as the two values could differ by a very small decimal.]

Question 3

(a). Create an atomic vector named suits that stores the four suits in cards (Diamonds, Spades, Clubs, and Hearts). Which type of vector is it (logical, integer, double, or character)?

(b). For the 13 Spades cards (Ace, 2, …, 10, Jack, Queen, King), create an atomic vector named facename that stores just the face names. Recall that, for the “Ace of Spades”, the face name is “Ace” and the suit name is “Spades”. What type of vector is facename?

(c). Using the vectors in (a) and (b), create an atomic vector named deck that represents a standard 52-card deck (as shown below).

#>  [1] "Ace of Diamonds"   "2 of Diamonds"     "3 of Diamonds"    
#>  [4] "4 of Diamonds"     "5 of Diamonds"     "6 of Diamonds"    
#>  [7] "7 of Diamonds"     "8 of Diamonds"     "9 of Diamonds"    
#> [10] "10 of Diamonds"    "Jack of Diamonds"  "Queen of Diamonds"
#> [13] "King of Diamonds"  "Ace of Spades"     "2 of Spades"      
#> [16] "3 of Spades"       "4 of Spades"       "5 of Spades"      
#> [19] "6 of Spades"       "7 of Spades"       "8 of Spades"      
#> [22] "9 of Spades"       "10 of Spades"      "Jack of Spades"   
#> [25] "Queen of Spades"   "King of Spades"    "Ace of Clubs"     
#> [28] "2 of Clubs"        "3 of Clubs"        "4 of Clubs"       
#> [31] "5 of Clubs"        "6 of Clubs"        "7 of Clubs"       
#> [34] "8 of Clubs"        "9 of Clubs"        "10 of Clubs"      
#> [37] "Jack of Clubs"     "Queen of Clubs"    "King of Clubs"    
#> [40] "Ace of Hearts"     "2 of Hearts"       "3 of Hearts"      
#> [43] "4 of Hearts"       "5 of Hearts"       "6 of Hearts"      
#> [46] "7 of Hearts"       "8 of Hearts"       "9 of Hearts"      
#> [49] "10 of Hearts"      "Jack of Hearts"    "Queen of Hearts"  
#> [52] "King of Hearts"

(Hints:

  1. You many need to use rep().
  2. You may need to use paste() function. Please refer to section 6.1.3 of Deep R Programming. Feel free to use help(paste) as well. Example: paste(5:7, "of", c("Spades", "Spades", "Hearts"))

)