HW1-2
Rmd
keyboard_arrow_up
School
Stevens Institute Of Technology *
*We aren’t endorsed by this school
Course
513
Subject
Statistics
Date
Jan 9, 2024
Type
Rmd
Pages
5
Uploaded by LieutenantLobster1814
---
title: "HW1"
author: "TESHWANI GOGINENI"
date: "2023-09-20"
output:
html_document:
df_print: paged
pdf_document: default
word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
# PartI
# 1.1 Vector
# 1. Create 2 vector, each containing 10 random numbers.
# First vector
vector1 <- sample(1:50, 10, replace = F)
# Second vector
vector2 <- sample(51:100, 10, replace = F)
# Printing the vectors
print(vector1)
print(vector2)
```
```{r}
# 2. Append the second vector to the first one.
vector_12 <- c(vector1, vector2)
print(vector_12)
```
```{r}
# 3. Calculate the mean of the new combined vector.
vector_mean <- mean(vector_12)
print(vector_mean)
```
```{r}
# 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')
}
}
```
```{r}
# 1.2 Matrix
# 1. Create a vector with 100 random numbers.
vector_100 <- sample(1:100, 100, replace = F)
vector_100
```
```{r}
# 2. Transfer the above vector into a 10 by 10 matrix M.
matrix_10 <- matrix(vector_100, nrow = 10, ncol = 10)
matrix_10
```
```{r}
# 3. Find the transposed matrix M^T . Print the value of element who is in the
second row and the first column of M^T .
matrix_transpose <- t(matrix_10)
matrix_transpose
print(matrix_transpose[2,1])
```
```{r}
# 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(m1,m2){
dim_matrix = matrix(nrow = dim(m1)[1], ncol = dim(m2)[2])
for (i in 1:dim(m1)[1]){
for (j in 1:dim(m2)[2]){ dim_matrix[i,j] = sum(m1[i,]*m2[,j])
}
}
return (dim_matrix)
}
matrix_inner1 <- inner_product(matrix_transpose,matrix_10)
matrix_inner1
```
```{r}
# 5. Calculate the same inner product using operator %
%. And compare two results.
∗
matrix_inner2 <- matrix_transpose %*% matrix_10
matrix_inner2
equal_matrix <- all.equal(matrix_inner1,matrix_inner2)
equal_matrix
```
```{r}
# 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
```
```{r}
# 2. Delete the columns containing NA(empty values).
new_df <- df[ ,colSums(is.na(df))==0]
new_df
```
```{r}
# 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
```
```{r}
# 4.
Calculate the mean and standard deviation of log return for each stock.
Transfer the 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
standard_deviation <- apply(daily_logreturns[2:n_col],2, sd, na.rm = TRUE)
standard_deviation
df1 <- data.frame(mean,standard_deviation)
df1
data <- as.data.frame(t(df1))
data
```
```{r}
# 5. Build a graph with two sub-plots. In the first sub-plot, plot the first three
stocks’ daily prices. The y axis is stock price and x axis is date. In the second
sub-plot, build a scatter plot of the statistical result you calculated above. In
other words, the x-axis is the stocks’ names and the y-axis is the statistical
values. (Notes. Please include legend, tile, and axis labels for each sub-plots.)
rows <- rownames(df1)
library(patchwork)
library(ggplot2)
par(mfrow = c(1,2))
colors <- c("AAPL" ="lightblue", "AMGN" = "pink", "AXP" = "maroon")
plot_1 <- ggplot(new_df, aes(x= X))+
geom_line(aes(y = AAPL, color = "AAPL", group = 1), size = 0.5)+
geom_line(aes(y = AMGN, color = "AMGN", group = 1), size = 0.5) +
geom_line(aes(y = AXP, color = "AXP", group = 1), size = 0.5) +
labs(x = "Date",
y = "Stock Prices",
color = "Stocks") + scale_color_manual(values = colors)
plot_2 <- ggplot(df1, aes(x=rows))+
geom_point(aes(y = mean, group = 1), size = 0.5)+
geom_point(aes(y=standard_deviation, group = 1), size = 0.5)+
labs(x = 'Stock',
y = 'statistical Values')
plot_1 / plot_2
```
```{r}
# Part II
# install MikTex
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
# 1. Download Amazon daily stock price data from 2021-01-01 to 2021-12-31. And save
the data to a csv file.
# install.packages("quantmod",repos = "http://cran.us.r-project.org")
library(quantmod)
```
```{r}
# Downloading Amazon's stock price data
AMZN_df <- getSymbols("AMZN", from = '2021-01-01',
to = "2021-12-31")
# Saving the data to a CSV file
# Convert the stock price data to a data frame
amzn_df <- as.data.frame(AMZN)
# Save the data frame to a CSV file
write.csv(amzn_df, "/Users/incharanagaraju/Desktop/513/amzn_df.csv", row.names =
TRUE)
AMZN_df <- read.csv("/Users/incharanagaraju/Desktop/513/amzn_df.csv")
AMZN_df
```
```{r}
# 2. Calculate weekly log returns based on adjusted close price.
weekly_logreturns <- diff(log(AMZN_df$AMZN.Adjusted),lag = 7)
weekly_logreturns
```
```{r}
# 3. Calculate median, mean, standard deviation of log returns.
# Calculate mean log return
mean_log_return <- mean(weekly_logreturns)
mean_log_return
# Calculate median log return
median_log_return <- median(weekly_logreturns)
median_log_return
# Calculate standard deviation of log returns
sd_log_return <- sd(weekly_logreturns)
sd_log_return
```
```{r}
# 4. Plot the distribution of stock daily log returns
daily_log_returns <- diff(log(AMZN_df$AMZN.Adjusted), lag = 1)
daily_log_returns
par(mar = c(1, 1, 1, 1))
hist(daily_log_returns,breaks = 10,
xlab = "daily log returns",
main = "Histogram of Amazon daily log returns",
col = "blue")
```
```{r}
# 5. Count how many observation in this series whose log return is between 0.01 and
0.015.
length(which(daily_log_returns > 0.01 & daily_log_returns < 0.015))
```
Related Documents
Related Questions
plse answer
arrow_forward
Help would be appreciated:)
arrow_forward
/wkjkNo-PgOw95s1fMBSkLSR2H8WwnDgQkubpX.
G brown univer
M Gmail - Inbox (182...
G Google O business intellige. S Crackle
G Inbox (33,809) - t.
Knowledge Check
O Other Bookmar
Question 7
Tashai
A 15 ft ladder leans against the side of a house. The top of the ladder is 13 ft off the ground. Find x, the angle of elevation of the ladder. Round your answer to
the nearest tenth of a degree.
Espau
x = •
15
13
02021 McGraw Hill LLC All Rights Reserved Terms of Use Pvacy Center
Submit
I Don't Know
O ? S ?
dtv RA A
16
MacBook Air
II
arrow_forward
-1, 2, 7, 14, -.
Find a,,
arrow_forward
what do the ks stand for in the exel tables
arrow_forward
It is a type of error that occurs when two
adjacent numbers of a UPC barcode or an
ISBN are typed reversely.
phonetic error
transposition error
single digit error
O jump error
arrow_forward
Answer and question in images
arrow_forward
Question number 2 section C
arrow_forward
A technology company is going to issue new ID codes to its employees. Each code will have two letters, followed by one digit, followed by one letter. The letters
D, G, and Z and the digit 8 will not be used. So, there are 23 letters and 9 digits that will be used. Assume that the letters can be repeated. How many
employee ID codes can be generated?
|employee ID codes
Easplanation
Check
2021 McGraw-Hill Education All Rights Reserved Terms of Uhe Pacy Accesibity
MacBook Air
4)
F11
F12
F9
F10
F7
F8
arrow_forward
Could you help me with part b) also please?
arrow_forward
please send the answer for the wrong part
arrow_forward
TN Chrome - TestNav
i testnavclient.psonsvc.net/#/question/4b2e6ef2-b6a4-469b-bb85-8b6c7703ef02/04a77ff6-9bcc-43ed-861b-5507f14ff96f
Review -
A Bookmark
Stewart, Jason -
Unit 6 Geometry NC Math 3 / 2 of 24
Il Pause
O Help -
In the quadrilateral ABCD,
AC = x + 6 .
and
BD = 2x – 4
For what value of x is ABCD a rectangle?
O v Ó 4:20
arrow_forward
During the annual conference of a professional organization, nine topics were proposed for simultaneous plenary discussions. However, some topics need to be scheduled in different time slots. The table
summarizes which topic cannot be discussed in the same schedule.
Тopics
1
3
4
6
7
8
9
1
X
---
2
X
---
3
X
X
X
---
4
X
X
X
X
---
X
X
X
X
6
X
---
7
---
X
---
X
a. How many time slots should be scheduled?
O 7 time slots
O 6 time slots
O 3 time slots
O 4 time slots
O 5 time slots
arrow_forward
Problem 4-09
Epsilon Airlines services predominately the eastern and southeastern United States. The vast majority of Epsilon's customers make reservations through Epsilon's website, but a small percentage
of customers make reservations via phone. Epsilon employs call-center personnel to handle these reservations along with any problems with the website reservation system and for the
rebooking of flights for customers if their plans change or their travel is disrupted. Staffing the call center appropriately is a challenge for Epsilon's management team. Having too many
employees on hand is a waste of money, but having too few results in very poor customer service and the potential loss of customers.
Epsilon analysts have estimated the minimum number of call-center employees needed by day of week for the upcoming vacation season (June, July, and the first two weeks of August). These
estimates are as follows:
Minimum Number of
Employees Needed
Day
Monday
75
Tuesday
50
Wednesday
45…
arrow_forward
Please assist properly
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you

Mathematics For Machine Technology
Advanced Math
ISBN:9781337798310
Author:Peterson, John.
Publisher:Cengage Learning,
Algebra & Trigonometry with Analytic Geometry
Algebra
ISBN:9781133382119
Author:Swokowski
Publisher:Cengage

Holt Mcdougal Larson Pre-algebra: Student Edition...
Algebra
ISBN:9780547587776
Author:HOLT MCDOUGAL
Publisher:HOLT MCDOUGAL
Related Questions
- plse answerarrow_forwardHelp would be appreciated:)arrow_forward/wkjkNo-PgOw95s1fMBSkLSR2H8WwnDgQkubpX. G brown univer M Gmail - Inbox (182... G Google O business intellige. S Crackle G Inbox (33,809) - t. Knowledge Check O Other Bookmar Question 7 Tashai A 15 ft ladder leans against the side of a house. The top of the ladder is 13 ft off the ground. Find x, the angle of elevation of the ladder. Round your answer to the nearest tenth of a degree. Espau x = • 15 13 02021 McGraw Hill LLC All Rights Reserved Terms of Use Pvacy Center Submit I Don't Know O ? S ? dtv RA A 16 MacBook Air IIarrow_forward
- Answer and question in imagesarrow_forwardQuestion number 2 section Carrow_forwardA technology company is going to issue new ID codes to its employees. Each code will have two letters, followed by one digit, followed by one letter. The letters D, G, and Z and the digit 8 will not be used. So, there are 23 letters and 9 digits that will be used. Assume that the letters can be repeated. How many employee ID codes can be generated? |employee ID codes Easplanation Check 2021 McGraw-Hill Education All Rights Reserved Terms of Uhe Pacy Accesibity MacBook Air 4) F11 F12 F9 F10 F7 F8arrow_forward
- Could you help me with part b) also please?arrow_forwardplease send the answer for the wrong partarrow_forwardTN Chrome - TestNav i testnavclient.psonsvc.net/#/question/4b2e6ef2-b6a4-469b-bb85-8b6c7703ef02/04a77ff6-9bcc-43ed-861b-5507f14ff96f Review - A Bookmark Stewart, Jason - Unit 6 Geometry NC Math 3 / 2 of 24 Il Pause O Help - In the quadrilateral ABCD, AC = x + 6 . and BD = 2x – 4 For what value of x is ABCD a rectangle? O v Ó 4:20arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Mathematics For Machine TechnologyAdvanced MathISBN:9781337798310Author:Peterson, John.Publisher:Cengage Learning,Algebra & Trigonometry with Analytic GeometryAlgebraISBN:9781133382119Author:SwokowskiPublisher:CengageHolt Mcdougal Larson Pre-algebra: Student Edition...AlgebraISBN:9780547587776Author:HOLT MCDOUGALPublisher:HOLT MCDOUGAL

Mathematics For Machine Technology
Advanced Math
ISBN:9781337798310
Author:Peterson, John.
Publisher:Cengage Learning,
Algebra & Trigonometry with Analytic Geometry
Algebra
ISBN:9781133382119
Author:Swokowski
Publisher:Cengage

Holt Mcdougal Larson Pre-algebra: Student Edition...
Algebra
ISBN:9780547587776
Author:HOLT MCDOUGAL
Publisher:HOLT MCDOUGAL