-
Notifications
You must be signed in to change notification settings - Fork 2
/
06-01-Simulation-Bootstrapping.Rmd
219 lines (161 loc) · 8.28 KB
/
06-01-Simulation-Bootstrapping.Rmd
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# Simulation {-}
## Bootstrap Simulation {-}
This script creates a function for running a simple bootstrap simulation from a given input. In this example I am using the data set BJsales from R. Bootstrap simulations work by pulling a series of random samples from the past and stringing them together to produce a possible future outcome.
You can specify how many `months.back` you want the simulation to pull samples from and how many `months.forward` you want the simulation to produce. If you want to use recent history make `months.back` a small number. If you want to project your simulation far out into the future, make `months.forward` a larger number. Specify the number of simulations (`trials`).
The function produces 2 plots. The first plot shows the original data as well as each individual simulated `trial`. The second plot is a distribution of the return or total percent change from the last historical value and the last `months.forward` value for each simulation.
The `alpha` parameter controls how transparent the lines are in the first graph.
```{r a1, warning=FALSE}
library(ggplot2)
library(reshape2)
library(scales)
library(grid)
library(gridExtra)
data("BJsales")
boot.plot = function(dt, trials, months.back = 24, months.forward = 12, alph = .1) {
## calculate the change history
lookup = 1 + (diff(dt) / dt)
## run run simulation by picking random percent changes from history
## repeat for the number of trials
sims = data.frame()
for (i in 1:trials) {
sims =
rbind(sims,
matrix(
sample(lookup, size = months.forward , replace = TRUE),
nrow = 1)
)
}
time = seq(151, 151 + months.forward - 1, by = 1)
colnames(sims) = time
sims[is.na(sims)] = 1
last.price = dt[length(dt)]
# calculate the price change from the last known price
p.chg = data.frame(sims[, 1] * last.price)
for (i in 2:months.forward) {p.chg = cbind(p.chg, p.chg[, i - 1] * sims[, i])}
colnames(p.chg) = time
# calculate the cumulative percent change
cuml.new = sims[,] - 1
cuml = data.frame(cuml.new[, 1])
for (i in 2:months.forward) {cuml = cbind(cuml, cuml[, i - 1] + cuml.new[, i])}
colnames(cuml) = time
cuml.chg = data.frame("Price Change" = cuml[, length(cuml)])
cuml$trial = 1:nrow(cuml)
cuml = melt(cuml, id.vars = "trial")
dt = data.frame(time = 1:length(dt), dt = dt)
## create graphics
plot.price = dcast(dt, . ~ time)
plot.price2 = cbind(plot.price, p.chg)
plot.price2$trial = 1:nrow(plot.price2)
plot.price2 = plot.price2[,-1]
plot.price2 = melt(plot.price2, id.vars = c("trial"))
cuml = data.frame("cuml.1qrt" = quantile(cuml.chg$Price.Change)[2],
"cuml.3qrt" = quantile(cuml.chg$Price.Change)[4],
"iqr.15" = quantile(cuml.chg$Price.Change)[4] -
quantile(cuml.chg$Price.Change)[2] * 1.5,
"med" = quantile(cuml.chg$Price.Change)[3])
x1 = ggplot(cuml.chg) +
geom_rect(aes(xmin = cuml.1qrt, xmax = cuml.3qrt, ymin = -Inf, ymax = Inf), data = cuml,
fill = "green", color = "gray", alpha = .1) +
geom_rect(aes(xmin = cuml.1qrt - iqr.15, xmax = cuml.1qrt, ymin = -Inf, ymax = Inf), data = cuml,
fill = "yellow", color = "gray", alpha = .1) +
geom_rect(aes(xmin = cuml.3qrt, xmax = cuml.3qrt + iqr.15, ymin = -Inf, ymax = Inf), data = cuml,
fill = "yellow", color = "gray", alpha = .1) +
geom_rect(aes(xmin = cuml.3qrt + iqr.15, xmax = Inf, ymin = -Inf, ymax = Inf), data = cuml,
fill = "red", color = "gray", alpha = .1) +
geom_rect(aes(xmin = -Inf, xmax = cuml.1qrt - iqr.15, ymin = -Inf, ymax = Inf), data = cuml,
fill = "red", color = "gray", alpha = .1) +
geom_histogram(aes(x = Price.Change), stat = "bin", binwidth = .005, fill = "white", color = "black",
alpha = .15) +
ggtitle("Distibution of Simulated Changes") +
scale_x_continuous("", labels = percent, breaks = seq(-.5, .5, .05)) +
scale_y_continuous("") +
theme(plot.title = element_text(face = "bold", size = 12),
axis.text.y = element_text(size = 0),
axis.ticks = element_blank())
plot.price2$variable = as.numeric(plot.price2$variable)
x3 = ggplot(plot.price2, aes(x = variable, y = value, group = trial)) +
geom_line(color = "blue", alpha = alph, se = FALSE) +
scale_x_continuous("Time") +
scale_y_continuous("Sales", labels = dollar) +
theme(plot.title = element_text(face = "bold", size = 12)) +
ggtitle("Time Series with Bootstrap Simulation")
return(grid.arrange(x3, x1, nrow = 2))
}
```
```{r plots, message=FALSE, fig.width=8, fig.height=7}
boot.plot(BJsales, trials = 100, months.back = 12, months.forward = 12, alph = .01)
boot.plot(BJsales, trials = 500, months.back = 24, months.forward = 24, alph = .005)
boot.plot(BJsales, trials = 1500, months.back = 36, months.forward = 36, alph = .005)
```
```{r code2, results='asis'}
boot.plot = function(dt, trials, months.back = 24, months.forward = 12, alph = .1) {
## calculate the change history
lookup = 1 + (diff(dt) / dt)
## run run simulation by picking random percent changes from history
## repeat for the number of trials
sims = data.frame()
for (i in 1:trials) {
sims =
rbind(sims,
matrix(
sample(lookup, size = months.forward , replace = TRUE),
nrow = 1)
)
}
time = seq(151, 151 + months.forward - 1, by = 1)
colnames(sims) = time
sims[is.na(sims)] = 1
last.price = dt[length(dt)]
# calculate the price change from the last known price
p.chg = data.frame(sims[, 1] * last.price)
for (i in 2:months.forward) {p.chg = cbind(p.chg, p.chg[, i - 1] * sims[, i])}
colnames(p.chg) = time
# calculate the cumulative percent change
cuml.new = sims[,] - 1
cuml = data.frame(cuml.new[, 1])
for (i in 2:months.forward) {cuml = cbind(cuml, cuml[, i - 1] + cuml.new[, i])}
colnames(cuml) = time
cuml.chg = data.frame("Price Change" = cuml[, length(cuml)])
cuml$trial = 1:nrow(cuml)
cuml = melt(cuml, id.vars = "trial")
dt = data.frame(time = 1:length(dt), dt = dt)
## create graphics
plot.price = dcast(dt, . ~ time)
plot.price2 = cbind(plot.price, p.chg)
plot.price2$trial = 1:nrow(plot.price2)
plot.price2 = plot.price2[,-1]
plot.price2 = melt(plot.price2, id.vars = c("trial"))
cuml = data.frame("cuml.1qrt" = quantile(cuml.chg$Price.Change)[2],
"cuml.3qrt" = quantile(cuml.chg$Price.Change)[4],
"iqr.15" = quantile(cuml.chg$Price.Change)[4] -
quantile(cuml.chg$Price.Change)[2] * 1.5,
"med" = quantile(cuml.chg$Price.Change)[3])
x1 = ggplot(cuml.chg) +
geom_rect(aes(xmin = cuml.1qrt, xmax = cuml.3qrt, ymin = -Inf, ymax = Inf), data = cuml,
fill = "green", color = "gray", alpha = .1) +
geom_rect(aes(xmin = cuml.1qrt - iqr.15, xmax = cuml.1qrt, ymin = -Inf, ymax = Inf), data = cuml,
fill = "yellow", color = "gray", alpha = .1) +
geom_rect(aes(xmin = cuml.3qrt, xmax = cuml.3qrt + iqr.15, ymin = -Inf, ymax = Inf), data = cuml,
fill = "yellow", color = "gray", alpha = .1) +
geom_rect(aes(xmin = cuml.3qrt + iqr.15, xmax = Inf, ymin = -Inf, ymax = Inf), data = cuml,
fill = "red", color = "gray", alpha = .1) +
geom_rect(aes(xmin = -Inf, xmax = cuml.1qrt - iqr.15, ymin = -Inf, ymax = Inf), data = cuml,
fill = "red", color = "gray", alpha = .1) +
geom_histogram(aes(x = Price.Change), stat = "bin", binwidth = .005, fill = "white", color = "black",
alpha = .15) +
ggtitle("Distibution of Simulated Changes") +
scale_x_continuous("", labels = percent, breaks = seq(-.5, .5, .05)) +
scale_y_continuous("") +
theme(plot.title = element_text(face = "bold", size = 12),
axis.text.y = element_text(size = 0),
axis.ticks = element_blank())
plot.price2$variable = as.numeric(plot.price2$variable)
x3 = ggplot(plot.price2, aes(x = variable, y = value, group = trial)) +
geom_line(color = "blue", alpha = alph, se = FALSE) +
scale_x_continuous("Time") +
scale_y_continuous("Sales", labels = dollar) +
theme(plot.title = element_text(face = "bold", size = 12)) +
ggtitle("Time Series with Bootstrap Simulation")
return(grid.arrange(x3, x1, nrow = 2))
}
```