Download stock data from http://finance.yahoo.com. In this example, we retrieved a year (from April 10, 2017 to April 6, 2018) of data of Apple Inc. (AAPL), saved as AAPL.csv
.
We used the read.csv
command to import data and assign them to the object dat
. Be sure that the file is in the current directory; if not, use the setwd
command (Under Session/Set Work Directory). The head
command is for obtaining the first few rows of the data. We show the stock price as a function of time using the plot
command. Note that the type is l
(el) meaning line not 1
(one).
dat <- read.csv("AAPL.csv")
head(dat)
## Date Open High Low Close Adj.Close Volume
## 1 2017-04-10 143.60 143.88 142.90 143.17 140.9404 18933400
## 2 2017-04-11 142.94 143.35 140.06 141.63 139.4244 30379400
## 3 2017-04-12 141.60 142.15 141.01 141.80 139.5917 20350000
## 4 2017-04-13 141.91 142.38 141.05 141.05 138.8534 17822900
## 5 2017-04-17 141.48 141.88 140.87 141.83 139.6213 16582100
## 6 2017-04-18 141.41 142.04 141.11 141.20 139.0011 14697500
plot(as.Date(dat$Date), dat$Adj.Close, type = "l")
We assign the adjusted close to the vector called close
for later convenience. The number of trading days is the length of the vector, denoted by n
.
close <- dat$Adj.Close
(n <- length(close))
## [1] 250
In financial news, we often hear relative, or percentage changes. A stock’s rate of return in any time period \(t\) is defined as \[
R_{t} = \frac{S_{t} - S_{t-1}}{S_{t-1}}
\] where \(S_{t}\) and \(S_{t-1}\) are the closing prices at time \(t\) and \(t-1\), respectively. Below is the calculation.
A histogram is made for the relative changes.
daily.diff <- close[2:n] - close[1:n-1]
rel.change <- daily.diff/close[1:n-1]
hist(rel.change, breaks = 20)
We can find the five-number summary and produce a box plot as below.
summary(rel.change)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -0.0433902 -0.0055524 0.0002952 0.0008015 0.0079969 0.0474718
boxplot(rel.change, horizontal = TRUE)
To find the mean and standard deviation, use the following commands.
(x.bar <- mean(rel.change))
## [1] 0.0008014673
(stdev <- sd(rel.change))
## [1] 0.01321591
The number of trading day is n
above
n
## [1] 250
and the number of days when the change is less than the mean is
sum(rel.change < x.bar)
## [1] 130
Express the changes as \(z\)-scores, and overlay the histogram of \(z\) with a standard normal distribution probability density function.
z.score <- (rel.change - mean(rel.change))/sd(rel.change)
hist(z.score, breaks = 20, freq = FALSE)
curve(dnorm(x), col = "blue", add = TRUE)
One financial model assumes that stocks advances with geometric Brownian motion, \[
S_{i+1} = S_{i} + \mu S_{i} \Delta t + \sigma S_{i} \epsilon \sqrt{\Delta t}
\] where \(\epsilon\) is a random number drawn from a standardized normal distribution.
Use $168.38 as the initial price (the closing value on April 6, 2018), we simulate a possible outcome for the next year. You should re-run this several times to observe the possible outcomes.
S <- c()
close[n]
## [1] 168.38
S[1] <- close[n]
for (i in 1:n){
S[i+1] <- S[i] + x.bar*S[i]*1 + stdev*S[i]*rnorm(1)*sqrt(1)
}
plot(S, type = "l")