Financial Economictrics Mathematics & Economics Coursework
Use the guideline for "R" to complete HW below:
Instruction: Review the materials, and then go over the detailed step by step example provided, titled “Lesson1_Rcodes”, using the data provided in “Lesson1_datafile”. Read carefully the questions.
1. Download the daily prices for the period January 1, 2000 to December 31, 2018, for the following stocks (these are the company names, and the stock identifiers, known as tickers, are in parenthesis, which is what you will need to use to download the data): Apple (AAPL), IBM (IBM), Microsoft (MSFT), Intel (INTC), Amazon (AMZN), and JPMorgan Chase & Co. (JPM).
a. For each stock, calculate the daily simple return.
b. For each stock, calculate the daily log return.
c. For each stock, calculate the annual return (hint: this can be done in several ways, and you can check what happens when you download the data on a year by year basis).
2. Prepare a summary table, that for each stock will have the following information: mean, standard deviation, skewness and kurtosis – for the simple return, and for the log return. Thus, since there are 5 stocks, you should prepare a table that has a total of 8 rows, and 5 columns.
3. What is the daily return of the portfolio made up of these five stocks? What is the average, standard deviation, skewness and kurtosis of the portfolio return?
4. Follow the example provided in “lesson1_Rcodes” to test whether the daily simple return and log return time series, of each stock separately, and that of the portfolio, follow a normal distribution.
R commands used in Lecture 1:
"<==" denotes explanation of the command.
## Install R (or RStudio) on your computer as demonstrated in class ##
## double click on R-icon to start R ##
### Make sure that you have internet connection before using the package "quantmod".
install.packages(“quantmod”) <== Command used to install R packages from online repository.
library(quantmod) or require(quantmod) <=== load the package "quantmod"
getSymbols("AAPL") <=== get daily Apple stock data from Yahoo Finance. The default starting date is 2007-01-03.
dim(AAPL) <=== find the size of the data downloaded
head(AAPL) <=== show the first 6 rows of data
tail(AAPL) <=== show the last 6 rows of data
chartSeries(AAPL) <=== plot Apple daily closing stock prices with trading volume
<== Daily closing prices do not adjusted for stock split. You can use adjusted closing price.
chartSeries(AAPL[,6]) <== Column 6 of the object "AAPL" in R.
chartSeries(AAPL[,6],theme="white") <== Same as the previous command, but use "white" background for the plot.
mynumeric.matrix = data.matrix(as.data.frame(AAPL)) <== convert the its object generated by the quantmod package to a numeric matrix.
aapl = mynumeric.matrix[,6]
basicStats(aapl)
aapl_r = returns(apple)
basicStats(aapl_r)
getSymbols("^VIX") <== load daily VIX index
getSymbols("^TNX") <== load 10-year interest rate
getSymbols("^GSPC") <== load S&P 500 index
getSymbols("^IXIC") <== load NASDAQ index
getSymbols("AAPL",from="2005-01-03",to="2013-12-31") <== specify the data span
getSymbols("UNRATE",src="FRED") <== Load U.S. monthly unemplyment rate from Federal Reserve Bank of St Louis.
<== src stands for "source", FRED stands for Federal Reserve Economic Data.
chartSeries(UNRATE) <== plot the U.S. monthly unemployment rate
getSymbols("DEXUSEU",src="FRED") <== Load Dollar verus Euro daily exchange rates from FRED.
chartSeries(DEXUSEU) <== plot the daily dollar-euro exchange rates.
getSymbols("DEXJPUS", src="FRED") <== Load Japanese Yen and US Dollar exchange rates from FRED.
getSymbols(‘AMZN’,src='yahoo')
r_AMZN_all <- allReturns(AMZN) <== returns all periods
r_AMZN_yearly <- periodReturn(AMZN,period='yearly',subset='2003::') <== returns years 2003 to present
r_AMZN_year2003 <- periodReturn(AMZN,period='yearly',subset='2003') <== returns year 2003
### Also try the following functions:
- allReturns: calculate all available return periods
- dailyReturn: calculate daily returns
- weeklyReturn: calculate weekly returns
- monthlyReturn: calculate monthly returns
- quarterlyReturn: calculate quarterly returns
- annualReturn: calculate annual returns
### You can also use the following commands to load data from FRED.
getSymbols.FRED("UNRATE",env=globalenv())
setwd(“YOUR WORKING DIRECTORY”) <== set my working directory.
### You should use your working directory ###
library(fBasics) or require(fBasics) <== Load the package "fBasics"
da=read.table("m-ibm6815.txt",header=T) <== Load data with header into R
<== header=T means the data file contains "names" for each column.
<== use header=F, if the data file contains no column names.
dim(da) <== Check dimension of the data (row = sample size, col = variables)
head(da) <== Print out the first 6 rows of the data object "da".
tail(da) <== Print out the last 6 rows of the data object "da".
ibm=da[,2] or ibm=da$ibm <== Select the simple returns of IBM stock stored in Column 2.
plot(ibm,type='l') <== Plot the simple returns. Note that type is "ell" not 1.
basicStats(ibm) <== Compute the descriptive statistics of simple returns.
libm=log(ibm+1) <== Compute the IBM log returns
basicStats(libm) <== Compute descriptive statistics of log returns.
t.test(ibm) <== Perform t-test for mean being zero.
t.test(ibm,alternative=c("greater")) <== Perform one-sided test (Not shown in class)
hist(ibm,nclass=40) <== Obtain histogram of IBM simple returns.
d1=density(libm) <== Compute density function of ibm log returns
names(d1) <== Find out the output variables of the command "density".
plot(d1$x,d1$y,type='l') <== Plot the sample density of log returns
mu=mean(libm); s1 = sd(libm) <== compute the sample mean and standard deviation of IBM log returns.
x=seq(-0.4,0.4,0.01) <=== create a sequence of real numbers from -0.4 to 0.4 with increment 0.01.
y=dnorm(x,mean=mu,sd=s1) <=== obtain normal density with mean mu and standard deviation s1.
lines(x,y,lty=2) <== impose a dashed line on the density plot for comparison with the normal density.
<== you can also use different colors in the plot. For example,
lines(x,y,col="red") will plot a red curve.
da <- read.csv("d-vix0411.csv",header=T) ## read *.csv file
tt <- skewness(libm)/sqrt(6/length(libm)) ## Test H_0: m3 =0 versus H_a: m3 not equal = 0.
tt
pv <- 2*(1-pnorm(tt)) ### Compute p-value (assuming tt is positive)
pv
tt <- kurtosis(libm)/sqrt(24/length(libm)) ## Testing excess kurtosis being zero
tt
normalTest(libm,method="jb") <== Perform normality test.
R commands used in Lecture 1:
“##“ denotes explanation of the command, or comment.
## Install RStudio on your computer as demonstrated in class ##
## double click on R-icon to start R ##
### Make sure that you have internet connection before using the package "quantmod".
install.packages(“quantmod”) ## Command used to install R packages from online repository.
library(quantmod) or require(quantmod) ## load the package "quantmod"
getSymbols("AAPL") ## get daily Apple stock data from Yahoo Finance. The default starting date is 2007-01-03.
dim(AAPL) ## find the size of the data downloaded
head(AAPL) ## show the first 6 rows of data
tail(AAPL) ## show the last 6 rows of data
chartSeries(AAPL) ## plot Apple daily closing stock prices with trading volume
## Daily closing prices do not adjusted for stock split. You can use adjusted closing price.
chartSeries(AAPL[,6]) ## Column 6 of the object "AAPL" in R.
chartSeries(AAPL[,6],theme="white") ## Same as the previous command, but use "white" background for the plot.
mynumeric.matrix = data.matrix(as.data.frame(AAPL)) ## convert the its object generated by the quantmod package to a numeric matrix.
aapl = mynumeric.matrix[,6]
basicStats(aapl)
aapl_r = returns(apple)
basicStats(aapl_r)
getSymbols("^VIX") ## load daily VIX index
getSymbols("^TNX") ## load 10-year interest rate
getSymbols("^GSPC") ## load S&P 500 index
getSymbols("^IXIC") ## load NASDAQ index
getSymbols("AAPL",from="2005-01-03",to="2013-12-31") ## specify the data span
getSymbols("UNRATE",src="FRED") ## Load U.S. monthly unemployment rate from Federal Reserve Bank of St Louis. src stands for "source", FRED stands for Federal Reserve Economic Data.
chartSeries(UNRATE) ## plot the U.S. monthly unemployment rate
getSymbols("DEXUSEU",src="FRED") ## Load Dollar verus Euro daily exchange rates from FRED.
chartSeries(DEXUSEU) ## plot the daily dollar-euro exchange rates.
getSymbols("DEXJPUS", src="FRED") ## Load Japanese Yen and US Dollar exchange rates from FRED.
getSymbols(‘AMZN’,src='yahoo')
r_AMZN_all <- allReturns(AMZN) ## returns all periods
r_AMZN_yearly <- periodReturn(AMZN,period='yearly',subset='2003::') ## returns years 2003 to present
r_AMZN_year2003 <- periodReturn(AMZN,period='yearly',subset='2003') ## returns year 2003
## Also try the following functions:
- allReturns: calculate all available return periods
- dailyReturn: calculate daily returns
- weeklyReturn: calculate weekly returns
- monthlyReturn: calculate monthly returns
- quarterlyReturn: calculate quarterly returns
- annualReturn: calculate annual returns
### You can also use the following commands to load data from FRED.
getSymbols.FRED("UNRATE",env=globalenv())
setwd(“YOUR WORKING DIRECTORY”) ## set my working directory.
### You should use your working directory ###
install.packages(“fBasics”)
library(fBasics) or require(fBasics) ## Load the package "fBasics"
da=read.table("m-ibm6815.txt",header=T) ## Load data with header into R
## header=T means the data file contains "names" for each column.
use header=F, if the data file contains no column names. ##
dim(da) ## Check dimension of the data (row = sample size, col = variables)
head(da) ## Print out the first 6 rows of the data object "da".
tail(da) ## Print out the last 6 rows of the data object "da".
ibm=da[,2]
ibm=da$ibm ## Select the simple returns of IBM stock stored in Column 2.
plot(ibm,type='l') ## Plot the simple returns. Note that type is "ell" not 1.
basicStats(ibm) ## Compute the descriptive statistics of simple returns.
libm=log(ibm+1) ## Compute the IBM log returns
basicStats(libm) ## Compute descriptive statistics of log returns.
t.test(ibm) ## Perform t-test for mean being zero.
t.test(ibm,alternative=c("greater")) ## Perform one-sided test (Not shown in class)
hist(ibm,nclass=40) ## Obtain histogram of IBM simple returns.
d1=density(libm) ## Compute density function of ibm log returns
names(d1) ## Find out the output variables of the command "density".
plot(d1$x,d1$y,type='l') ## Plot the sample density of log returns
mu=mean(libm); s1 = sd(libm) ## compute the sample mean and standard deviation of IBM log returns.
x=seq(-0.4,0.4,0.01) ## create a sequence of real numbers from -0.4 to 0.4 with increment 0.01.
y=dnorm(x,mean=mu,sd=s1) ## obtain normal density with mean mu and standard deviation s1.
lines(x,y,lty=2) ## impose a dashed line on the density plot for comparison with the normal density.
## you can also use different colors in the plot. For example,
lines(x,y,col="red") will plot a red curve.##
da <- read.csv("d-vix0411.csv",header=T) ## read *.csv file
tt <- skewness(libm)/sqrt(6/length(libm)) ## Test H_0: m3 =0 versus H_a: m3 not equal = 0.
tt
pv <- 2*(1-pnorm(tt)) ## Compute p-value (assuming tt is positive)
pv
tt <- kurtosis(libm)/sqrt(24/length(libm)) ## Testing excess kurtosis being zero
tt
normalTest(libm,method="jb") ## Perform normality test.
Financial Econometrics
Name
Course
Instructor
Date
1. Download the daily prices for the period January 1, 2000 to December 31, 2018, for the following stocks (these are the company names, and the stock identifiers, known as tickers, are in parenthesis, which is what you will need to use to download the data): Apple (AAPL), IBM (IBM), Microsoft (MSFT), Intel (INTC), Amazon (AMZN), and JPMorgan Chase & Co. (JPM).a. For each stock, calculate the daily simple return. In excel
b. For each stock, calculate the daily log return.
In excelc. For each stock, calculate the Annual Return (hint: this can be done in several ways, and you can check what happens when you download the data on a year by year basis).
Annual Return
Year
AAPL
AMZN
IBM
INTC
MSFT
JPM
2000
-73.42%
-82.59%
-26.72%
-30.89%
-62.79%
-6.48%
2001
47.23%
-22.02%
42.62%
1.25%
52.74%
-17.39%
2002
-38.50%
72.35%
-36.21%
-52.82%
-22.88%
-33.98%
2003
44.39%
168.88%
15.03%
92.03%
1.90%
44.38%
2004
202.63%
-14.66%
7.68%
-27.27%
-2.66%
6.53%
2005
127.18%
5.91%
-15.91%
8.19%
-2.21%
1.38%
2006
13.50%
-17.07%
18.39%
-20.81%
11.25%
20.18%
2007
136.37%
139.38%
11.13%
31.01%
19.22%
-9.19%
2008
-56.19%
-46.72%
-19.61%
-42.17%
-44.80%
-25.23%
2009
132.21%
147.46%
49.82%
34.21%
49.93%
32.92%
2010
50.72%
34.43%
10.80%
0.72%
-9.82%
-1.00%
2011
22.89%
-6.04%
24.68%
16.31%
-7.22%
-23.70%
2012
29.41%
40.13%
2.82%
-15.97%
-0.22%
25.70%
2013
2.18%
54.98%
-4.47%
21.42%
35.45%
30.94%
2014
39.69%
-22.02%
-13.52%
40.71%
25.00%
7.51%
2015
-3.72%
👀 Other Visitors are Viewing These APA Essay Samples:
-
ECO 550 Case Study 2 Economics Coursework Research
6 pages/≈1650 words | No Sources | APA | Mathematics & Economics | Coursework |
-
Industrial Organization and Price Analysis. Coursework
1 page/≈275 words | No Sources | APA | Mathematics & Economics | Coursework |
-
ECONOMIC IMPACTS OF COVID-19 PANDEMIC. Mathematics & Economics Coursework
2 pages/≈550 words | No Sources | APA | Mathematics & Economics | Coursework |