forked from campbwa/R-videos
-
Notifications
You must be signed in to change notification settings - Fork 4
/
geometric ou process.R
154 lines (50 loc) · 2.03 KB
/
geometric ou process.R
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
setwd("/home/campbwa/Dropbox/dissertation/data")
prices = read.csv('month_avg.csv')
price = prices$Monthly_average
real = prices$Real
date = prices$Date
price = ts(price, start = 1972, frequency = 12)
real = ts(real, start = 1972, frequency = 12)
?ts
ts.plot(price, real, lwd = 2, col = c("blue", "red"))
############# function to calculate percentage changes in data #############
pct.diff = function(price){
PCT = rep(0,length(price))
d = diff(price)
for(t in 1:length(price)){
PCT[t] = d[t] / price[t]
}
PCT = PCT[-length(PCT)]
return(PCT)
}
#############################################################################
################# Estimation of OU parameters ###################
R = pct.diff(real)
Z = 1/real[-470]
summary(lm(R ~ Z))
#the parameter mu in the OU model is close to the median of prices:
1.72750 / 0.02376 #72.70623
median(real) #72.09297
##################################################################
############### Simulation of the OU process #######################
#this is the geometric version of the OU process
#percentage volatility is constant, absolute volatility is not constant
OU.sim <- function(T = 1000, mu = 72.70623, eta = 0.02376, sigma = 0.07419){
P_0 = mu #starting price is the mean
P = rep(P_0,T)
for(i in 2:T){
P[i] = P[i-1] + eta * (mu - P[i-1]) + sigma * rnorm(1) * P[i-1]
}
return(P)
}
####################################################################
################ Plots! ##############
#default parameters
plot(OU.sim(), type = "l", xlab = "Time", ylab = "Price")
#changing the variability of the process
plot(OU.sim(sigma = 0.15), type = "l", xlab = "Time", ylab = "Price")
plot(OU.sim(sigma = 0.03), type = "l", xlab = "Time", ylab = "Price")
#changing the level of mean reversion for the process
plot(OU.sim(eta = 0.15), type = "l", xlab = "Time", ylab = "Price")
plot(OU.sim(eta = 0.001), type = "l", xlab = "Time", ylab = "Price")
#######################################