-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAMDts.Rmd
More file actions
205 lines (161 loc) · 4.83 KB
/
AMDts.Rmd
File metadata and controls
205 lines (161 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
---
title: "Time Series Analysis of AMD Stock Data"
author: "Pooria Assarehha"
date: "2024-01-06"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## The Data
The data was downloaded from NASDAQ official website.
We will view the data first to see what must be done with it.
```{r}
df = read.csv("HistoricalData_1704473693714.csv")
str(df)
summary(df)
```
we have a Date column which is stored as chr type. we will convert it to a datetime object.
We have numerical columns which are stored as chr because of presence of a dollar sign, which we have to remove then convert them to numeric.
Data does not show any missing records, but we will examine Date column for any missing days.
Since missing days are probably the days that the market is closed, we can argue that holidays do not count as missing data.
### Prepros
-- Changing time from chr type to POSIXlt datetime class
```{r}
df$Date <- strptime(df$Date, format = "%m/%d/%Y")
str(df)
```
-- Changing chr fields to num
```{r}
rmvdollar <- function(x){return(gsub("\\$", "", x))}
df$Low <- as.numeric(rmvdollar(df$Low))
df$Open <- as.numeric(rmvdollar(df$Open))
df$High <- as.numeric(rmvdollar(df$High))
df$Close.Last <- as.numeric(rmvdollar(df$Close.Last))
str(df)
```
# Finding a Good Transfromation
```{r , fig.height=15 , fig.width=10, message=FALSE}
library(TSA)
par(mfrow=c(4,1))
for(i in 1:5){
if(is.numeric(df[,i])){
plot(
df$Date,
df[,i],
type = "l",
xlab="Time (Day)",
ylab = paste(colnames(df)[i], if(i == 3) " (# of Shares)" else "(USD $)" )
)
}
}
```
We can argue an exponential trend for USD variables.
```{r , fig.height=10 , fig.width=10}
par(mfrow=c(3,1))
for(i in 1:5){
if(all(class(df[,i]) == "numeric")){
plot(
df$Date,
log(df[,i]),
type = "l",
xlab="Time (Day)",
ylab = paste("Log of " , colnames(df)[i])
)
}
}
```
it still has a trend. what should we do?
```{r , fig.height=10 , fig.width=10}
par(mfrow=c(3,1))
for(i in 1:5){
if(all(class(df[,i]) == "numeric")){ ## Using all() to avoid a warning
plot(
df$Date,
log(log(df[,i])),
type = "l",
xlab="Time (Day)",
ylab = paste("Log of Log of " , colnames(df)[i])
)
}
}
```
Let's plot a first difference to see whats what.
```{r , fig.height=10 , fig.width=10}
par(mfrow=c(3,2))
for(i in 1:5){
if(all(class(df[,i]) == "numeric")){ ## Using all() to avoid a warning
y = df[,i]
dy = y - zlag(y)
plot(
df$Date,
dy,
# c(0,diff(df[,i])), equiv
type = "p",
xlab="Time (Day)",
ylab = paste("First Difference of " , colnames(df)[i])
)
plot(
df$Date,
dy - zlag(dy),
type = "p",
xlab="Time (Day)",
ylab = paste("Second Difference of " , colnames(df)[i])
)
}
}
```
We may be better off segmenting data:
```{r , fig.height=10 , fig.width=10}
df2014_16 = df[1764:nrow(df),]
par(mfrow=c(3,1))
for(i in c(2,4,5)){
plot(
df2014_16$Date,
log(df2014_16[,i]),
type = "l",
xlab="Time (Day)",
ylab = paste("Log of " , colnames(df)[i])
)
}
```
>The industry-standard transformation is to calculate percent returns, via a slight alteration of first-order differencing:
>
$$R_t = \frac{P_t}{P_{t-1}} - 1$$
>Where P is price, and t is time. We call this simply “returns.”
>Many academics especially prefer to also take the logarithm:
$$r_t = \ln(1+R_t) = \ln(\frac{P_t}{P_{t-1}} ) = \ln(P_t) - \ln(P_{t-1})$$
> Predicting price isn’t all that useful. Predicting returns is.
> Some notes. This can be viewed as exact first-order difference on the log price. It’s nice for a few reasons.
-- It’s symmetrical, meaning that if you reverse the time series it just flips the signs.
-- You can sum sequential r[t] to get the cumulative log returns over a period.
-- Log returns are very similar numerically to returns, and are monotonic with them
-- Any transformation you do to predict log returns maps back to a valid (positive) price, because the reverse transform involves the exponent. That’s not true with log-returns.
>To a first order approximation, for a really small time scale (like daily returns or hourly returns), the expected value of both returns and log returns is 0, which is also nice.
>If this is your first rodeo, I’d do log-returns because they’re easier to manipulate.
```{r}
df$return.last <- df$Close.Last/zlag(df$Close.Last) - 1
plot(
df$Date,
df$return.last,
type = "p",
xlab="Time (Day)",
ylab = paste("Return of Close last")
)
```
### Log Return
```{r , fig.height=10 , fig.width=15}
par(mfrow=c(3,2))
for(i in 1:5){
if(all(class(df[,i]) == "numeric")){ ## Using all() to avoid a warning
y = log(df[,i])
dy = diff(y)
#plot(df$Date, y)
plot(df$Date, c(0,dy))
pacf(c(0,dy))
}
}
```
# Model Specification
# Estimation
# Forcast