HW1
.pdf
keyboard_arrow_up
School
Stevens Institute Of Technology *
*We aren’t endorsed by this school
Course
513
Subject
Industrial Engineering
Date
Jan 9, 2024
Type
Pages
12
Uploaded by LieutenantLobster1814
HW1
# PartI
# 1.1 Vector
# 1. Create 2 vector, each containing 10 random numbers.
# First vector
vector1 <- sample(1:10, 10, replace = F)
# Second vector
vector2 <- sample(11:20, 10, replace = F)
# Printing the vectors
print(vector1)
## [1] 4 5 2 8 6 1 3 10 9 7
print(vector2)
## [1] 14 16 20 17 12 15 13 11 19 18
# 2. Append the second vector to the first one.
vector_12 <- c(vector1, vector2)
print(vector_12)
## [1] 4 5 2 8 6 1 3 10 9 7 14 16 20 17 12 15 13 11 19 18
# 3. Calculate the mean of the new combined vector.
vector_mean <- mean(vector_12)
print(vector_mean)
## [1] 10.5
# 4. For each number in the new combined vector, if it is lager than the mean then print out a ’True’, otherwise print out a ’False’.
result <- logical(length(vector_12))
for
(i in
1:length(vector_12)) {
if
(vector_12[i] > vector_mean) {
print('True')
} else
{
print('False')
}
}
## [1] "False"
## [1] "False"
## [1] "False"
## [1] "False"
## [1] "False"
## [1] "False"
## [1] "False"
## [1] "False"
## [1] "False"
## [1] "False"
## [1] "True"
## [1] "True"
## [1] "True"
## [1] "True"
## [1] "True"
## [1] "True"
## [1] "True"
## [1] "True"
## [1] "True"
## [1] "True"
# 1.2 Matrix
# 1. Create a vector with 100 random numbers.
vector_100 <- sample(1:100, 100, replace = F)
vector_100
## [1] 79 60 43 83 77 93 65 72 33 58 95 90 7 10 86 22 40 69
## [19] 4 100 56 35 30 67 66 91 14 52 8 32 94 15 20 28 84 18
## [37] 11 89 70 3 34 6 85 23 31 61 63 62 96 19 81 2 82 36
## [55] 24 38 97 88 44 47 39 46 12 17 54 57 76 1 49 98 71 75
## [73] 53 29 73 87 48 13 55 9 74 27 16 37 92 45 5 21 80 99
## [91] 68 41 64 25 51 42 50 26 78 59
# 2. Transfer the above vector into a 10 by 10 matrix M.
matrix_10 <- matrix(vector_100, nrow = 10, ncol = 10)
matrix_10
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 79 95 56 94 34 81 39 71 74 68
## [2,] 60 90 35 15 6 2 46 75 27 41
## [3,] 43 7 30 20 85 82 12 53 16 64
## [4,] 83 10 67 28 23 36 17 29 37 25
## [5,] 77 86 66 84 31 24 54 73 92 51
## [6,] 93 22 91 18 61 38 57 87 45 42
## [7,] 65 40 14 11 63 97 76 48 5 50
## [8,] 72 69 52 89 62 88 1 13 21 26
## [9,] 33 4 8 70 96 44 49 55 80 78
## [10,] 58 100 32 3 19 47 98 9 99 59
# 3. Find the transposed matrix M^T . Print the value of element who is in the second ro
w and the first column of M^T .
matrix_transpose <- t(matrix_10)
matrix_transpose
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 79 60 43 83 77 93 65 72 33 58
## [2,] 95 90 7 10 86 22 40 69 4 100
## [3,] 56 35 30 67 66 91 14 52 8 32
## [4,] 94 15 20 28 84 18 11 89 70 3
## [5,] 34 6 85 23 31 61 63 62 96 19
## [6,] 81 2 82 36 24 38 97 88 44 47
## [7,] 39 46 12 17 54 57 76 1 49 98
## [8,] 71 75 53 29 73 87 48 13 55 9
## [9,] 74 27 16 37 92 45 5 21 80 99
## [10,] 68 41 64 25 51 42 50 26 78 59
print(matrix_transpose[2,1])
## [1] 95
# 4.Write a nested loop to calculate the inner product between M^T and M. The result is also a matrix N = ⟨
M^T , M ⟩
.
inner_product = function
(a1,a2){
dim_matrix = matrix(nrow = dim(a1)[1], ncol = dim(a2)[2]) for
(i in
1:dim(a1)[1]){
for
(j in
1:dim(a2)[2]){ dim_matrix[i,j] = sum(a1[i,]*a2[,j])
} }
return
(dim_matrix)
}
matrix_inner1 <- inner_product(matrix_transpose,matrix_10)
matrix_inner1
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 47019 36204 33694 29259 29499 35234 29540 34900 32713 31610
## [2,] 36204 41531 24408 25481 17685 26537 27102 26285 30713 26164
## [3,] 33694 24408 26375 20885 19284 22310 18688 24945 23185 20590
## [4,] 29259 25481 20885 30576 22320 25112 15283 22931 25076 22528
## [5,] 29499 17685 19284 22320 31018 30310 19580 24887 21664 26087
## [6,] 35234 26537 22310 25112 30310 37903 22531 24992 23116 27901
## [7,] 29540 27102 18688 15283 19580 22531 28017 23487 26505 24309
## [8,] 34900 26285 24945 22931 24887 24992 23487 32793 25635 26956
## [9,] 32713 30713 23185 25076 21664 23116 26505 25635 34986 27547
## [10,] 31610 26164 20590 22528 26087 27901 24309 26956 27547 28132
# 5. Calculate the same inner product using operator %∗%. And compare two results.
matrix_inner2 <- matrix_transpose %*% matrix_10
matrix_inner2
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 47019 36204 33694 29259 29499 35234 29540 34900 32713 31610
## [2,] 36204 41531 24408 25481 17685 26537 27102 26285 30713 26164
## [3,] 33694 24408 26375 20885 19284 22310 18688 24945 23185 20590
## [4,] 29259 25481 20885 30576 22320 25112 15283 22931 25076 22528
## [5,] 29499 17685 19284 22320 31018 30310 19580 24887 21664 26087
## [6,] 35234 26537 22310 25112 30310 37903 22531 24992 23116 27901
## [7,] 29540 27102 18688 15283 19580 22531 28017 23487 26505 24309
## [8,] 34900 26285 24945 22931 24887 24992 23487 32793 25635 26956
## [9,] 32713 30713 23185 25076 21664 23116 26505 25635 34986 27547
## [10,] 31610 26164 20590 22528 26087 27901 24309 26956 27547 28132
equal_matrix <- all.equal(matrix_inner1,matrix_inner2)
equal_matrix
## [1] TRUE
# 1.3 Function
# 1. Load the given CSV file in R
df <- read.csv("/Users/incharanagaraju/Desktop/513/stock_data-1.csv", header = TRUE)
#df
# 2. Delete the columns containing NA(empty values).
new_df <- df[ , colSums(is.na(df))==0]
#new_df
# 3. Calculate daily log return for each stock. (Hint. log return is defined as rt = ln Pt = ln(Pt)−ln(Pt−1), where Pt is the stock price at time t.)
n_col <- ncol(new_df)
date <- as.Date(new_df[,1], format = "%Y-%m-%d")
daily_logreturns <- sapply(new_df[2:n_col], function
(new_df) diff(log(new_df))) daily_logreturns <- data.frame(daily_logreturns)
daily_logreturns <- rbind(NA,daily_logreturns)
daily_logreturns <- cbind(date,daily_logreturns)
#daily_logreturns
# 4. Calculate the mean and standard deviation of log return for each stock. Transfer t
he result into a 2 by N data frame (N is the number of stocks).
mean <- apply(daily_logreturns[2:n_col],2,mean, na.rm = TRUE)
mean
## AAPL AMGN AXP BA CAT CSCO ## 0.0009168344 0.0004641248 0.0003855638 0.0003478159 0.0003798480 0.0004002217 ## CVX DIS HD IBM INTC JNJ ## 0.0002502413 0.0003264233 0.0005012099 0.0002923392 0.0003470648 0.0003197527 ## JPM KO MCD MMM MRK MSFT ## 0.0003240281 0.0001789796 0.0003573148 0.0002726557 0.0001724428 0.0005522446 ## NKE PG TRV UNH VZ WBA ## 0.0005167696 0.0002963599 0.0002607877 0.0005950182 0.0001155210 0.0003405546 ## WMT ## 0.0003856492
standard_deviation <- apply(daily_logreturns[2:n_col],2, sd, na.rm = TRUE)
standard_deviation
## AAPL AMGN AXP BA CAT CSCO CVX ## 0.02840478 0.02085579 0.02197895 0.01937864 0.02049348 0.02476240 0.01596826 ## DIS HD IBM INTC JNJ JPM KO ## 0.01884281 0.01956586 0.01737205 0.02370960 0.01291141 0.02385872 0.01383933 ## MCD MMM MRK MSFT NKE PG TRV ## 0.01493950 0.01493651 0.01733853 0.01954384 0.01995818 0.01423707 0.01779765 ## UNH VZ WBA WMT ## 0.02196304 0.01596795 0.01810263 0.01608582
df1 <- data.frame(mean,standard_deviation)
df1
mean
<dbl>
standard_deviation
<dbl>
AAPL
0.0009168344
0.02840478
AMGN
0.0004641248
0.02085579
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help