Skip to content

Commit

Permalink
Merge pull request #5 from ptpatel/master
Browse files Browse the repository at this point in the history
added chp 1
  • Loading branch information
stanchan authored Oct 29, 2021
2 parents 76ae2fa + 22d06e3 commit ac4a205
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions chapter_1.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# CHAPTER 1

# Figure 1.1
p <- 1/2
n <- seq(0, 9)
# In Python, the np.arange(start, stop) function will not include
# the end of the interval. Thus, seq is (0,9).
X <- `^`(p,n)
barplot(X)

# Binomial Theorem
n <- 10
k <- 2
choose(n,k)
factorial(k)

# R code to perform an inner product
x <- c(1,0,-1)
y <- c(3,2,0)
z <- t(x) %*% y
print(z)

# R code to compute the norm
x <- c(1,0,-1)
x_norm <- norm(x, type = "2")
# "2" specifies the “spectral” or 2-norm, which is the largest singular value of x
print(x_norm)

# R code to compute the weighted norm
W <- matrix(1:9, ncol = 3, nrow = 3)
x <- matrix(c(2,-1,1))
z <- t(x) %*% (W %*% x)
print(z)

# R code to compute a matrix inverse
X <- matrix(c(1,-2,0,3,7,1), nrow = 3, ncol = 2)
XtX <- t(X) %*% X
Xtxinv <- solve(XtX)
print(Xtxinv)

# R code to solve X beta = y
X <- matrix(c(1,-2,0,3,7,1), nrow = 3, ncol = 2)
y <- matrix(c(2,1,0))
beta <- solve(qr(X), y)
print(beta)

0 comments on commit ac4a205

Please sign in to comment.