Below are the solutions for Homework #1 for the Data Science Math Bridge Course at CUNY SPS for the Data Science Master’s Program

# Derivatives

### Question 1:

library(Deriv)
myf1 = function(x) 1-(exp(-lambda*x))
Deriv(myf1)
## function (x)
## lambda * exp(-(lambda * x))

### Question 2:

myf2 = function(x) (x-a)/(b-a)
Deriv(myf2)
## function (x)
## 1/(b - a)

### Question 3:

myf3 = function(x) ((x-a)^2)/((b-a)*(c-a))
Deriv(myf3)
## function (x)
## 2 * ((x - a)/((b - a) * (c - a)))

### Question 4:

myf4 = function(x) 1-((b-x)^2)/((b-a)*(c-a))
Deriv(myf4)
## function (x)
## 2 * ((b - x)/((b - a) * (c - a)))

# Integrals

### Question 5:

#library(grDevices)
myf5 = function(x) 3*x^3
integrate(myf5,0,10)
## 7500 with absolute error < 8.3e-11

### Question 6:

Lambda = L integral(xLexp(-Lx)) Lintegral(xexp(-Lx))

library(mosaicCalc)
## Loading required package: mosaicCore
## Registered S3 method overwritten by 'mosaic':
##   method                           from
##   fortify.SpatialPolygonsDataFrame ggplot2
##
## Attaching package: 'mosaicCalc'
## The following object is masked from 'package:stats':
##
##     D
F <- function(x) x
f <- Deriv(F)
#yields f(x) = 1
g <- function(x, L) exp(-L*x)
G <- antiD(exp(-L*x)~x)
show(G)
## function (x, C = 0, L)
## 1/(-L) * exp(-L * x) + C
#yields 1/(-L) * exp(-L *x)
#H = F(x) * G(x,L) = x/(-L) * exp(-L *x)
H <- function(x,L) {x/(-L) * exp(-L *x)}
#I = f(x) * G(x,L) = 1 * 1/(-L) * exp(-L *x) = G(x,L)
antiD(1/(-L) * exp(-L *x)~x)
## function (x, C = 0, L)
## 1/(-L) * 1/(-L) * exp(-L * x) + C
#yields 1/(-L) * 1/(-L) * exp(-L * x) + C =
# (1/L^2) * exp(-L * x) + C
J <- function(x,L) (1/L^2) * exp(-L * x)
#result = L * (H(x,L)-J(x,l)) =
# L * [(x/(-L) * exp(-L *x))- (1/L^2) * exp(-L * x)] =
#= L* exp(-L*x) * (x/(-L) - (1/L^2)) =
#= L* exp(-L*x) * (-xL - 1)/L^2 =
#= exp(-L*x) * (-x*L -1)/L

### Question 7:

library(mosaicCalc)
myf7 <- antiD(1/(b-a)~x)
show(myf7)
## function (x, C = 0, b, a)
## 1/(b - a) * x + C
#yields 1/(b-a) * x + C

myf7(x=0.5)-myf7(x=0) = 0.5/(b-a) = 1/2*(b-a)

### Question 8:

Multiplying the integral by 1 in the form of Beta^(2Alpha)/Beta^(2Alpha):

(Beta^(2*Alpha)/Beta(2Alpha))integral(x(x(Alpha-1)exp(-Betax))/Gamma(Alpha)BetaAlpha)dx (1/Beta(2Alpha))integral(x(x(Alpha-1)exp(-Betax)BetaAlpha)/Gamma(Alpha))dx

if f(x) = x and g’(x) = (x^(Alpha-1)exp(-Betax)*Beta^Alpha)/Gamma(alpha) then g(x) = 1 and

(1/Beta^(2Alpha))integral(f(x)g’(x)) = (1/Beta^(2Alpha))(f(x)g(x) - integral(f’(x)(g(x)))) = (1/Beta^(2Alpha))(f(x) - integral(f’(x)1))

yielding

(1/Beta^(2Alpha))(x-x) = 0

# Linear Algebra

### Question 9:

library(Matrix)
X = matrix(c(1,3,4,2,3,6,3,1,8),3,3)
solve(X)
##      [,1] [,2]  [,3]
## [1,] -4.5 -0.5  1.75
## [2,]  5.0  1.0 -2.00
## [3,] -1.5 -0.5  0.75

### Question 10:

det(X)
## [1] -4

### Question 11:

expand(lu(X))
## $L ## 3 x 3 Matrix of class "dtrMatrix" (unitriangular) ## [,1] [,2] [,3] ## [1,] 1.0000000 . . ## [2,] 0.7500000 1.0000000 . ## [3,] 0.2500000 -0.3333333 1.0000000 ## ##$U
## 3 x 3 Matrix of class "dtrMatrix"
##      [,1]       [,2]       [,3]
## [1,]  4.0000000  6.0000000  8.0000000
## [2,]          . -1.5000000 -5.0000000
## [3,]          .          . -0.6666667
##
## \$P
## 3 x 3 sparse Matrix of class "pMatrix"
##
## [1,] . . |
## [2,] . | .
## [3,] | . .

### Question 12:

X_1 <- solve(X)
X%*%X_1
##               [,1]          [,2]         [,3]
## [1,]  1.000000e+00 -2.220446e-16 4.440892e-16
## [2,] -4.440892e-16  1.000000e+00 2.220446e-16
## [3,]  0.000000e+00  0.000000e+00 1.000000e+00