HW1-2

Rmd

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

Report
--- 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)) ```