-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgp2_rlagp.Rmd
More file actions
188 lines (135 loc) · 4.83 KB
/
Copy pathgp2_rlagp.Rmd
File metadata and controls
188 lines (135 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
---
title: "Gaussian Process regression - R ```laGP```"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
<br>
A replication of the Gaussian Process regression implementation from chapter 5 of [Surrogates](https://bookdown.org/rbg/surrogates/chap5.html). Application of the same code to real data.
<br>
## Chapter example
<br>
### Data
Create some dummy data being an independent and dependent variable, along with a grid of the independent variable values.
```{r }
# Training data
n <- 8
X <- matrix(seq(0, 2*pi, length=n), ncol=1) # independent variable
y <- sin(X)
# Predictive grid
XX <- matrix(seq(-0.5, 2*pi + 0.5, length=100), ncol=1)
# Nugget / jitter
eps <- sqrt(.Machine$double.eps)
```
<br>
### Using function `newGP`
Note the parameters:
d: a positive scalar lengthscale parameter for an isotropic Gaussian correlation function (newGP); or a vector for a separable version (newGPsep), and
g: a positive scalar nugget / jitter parameter
are seeded with the ````darg``` and ````garg``` functions.
```{r }
library('laGP')
# Derive sensible parameter ranges
d <- darg(NULL, X)
y[1] <- 1e-3 # negate garg error: Error in check.arg(g) : d$min should be a postive scalar < d$max
g <- garg(NULL, y)
par(mfrow=c(1,2))
for(parm in c('d','g')) {
# GP
gpi <- newGP(X, y, d=d$start, g=g$start*0.5, dK=TRUE)
#mle <- mleGP(gpi)
mle <- mleGP(gpi, param=parm, tmin=ifelse(parm == 'd', d$min, g$min), tmax=ifelse(parm == 'd', d$max, g$max))
p <- predGP(gpi, XX)
YY <- mvtnorm::rmvnorm(100, p$mean, p$Sigma)
q1 <- p$mean + qnorm(0.05, 0, sqrt(diag(p$Sigma)))
q2 <- p$mean + qnorm(0.95, 0, sqrt(diag(p$Sigma)))
# Plot
matplot(XX, t(YY), type="l", col="gray", lty=1,
main = paste0("Optimising ", ifelse(parm == 'd', 'lengthscale', 'nugget / jitter')),
xlab="x", ylab="sin(x)", bty = "n")
points(X, y, pch=20, cex=2)
lines(XX, p$mean, lwd=2)
lines(XX, sin(XX), col="blue")
lines(XX, q1, lwd=2, lty=2, col=2)
lines(XX, q2, lwd=2, lty=2, col=2)
deleteGP(gpi)
rm(mle)
}
```
<br>
### Using function `newGPsep`
A separable GP, the lengthscale parameter is allowed to decay at differing rates dependent on the input data. This is an anisotropic, rather than isotropic model.
```{r }
gpi <- newGPsep(X, y, d=d$start, g=g$start*0.5, dK=TRUE)
mle <- mleGPsep(gpi, param='both', tmin=c(d$min, g$min), tmax=c(d$max, g$max))
p <- predGPsep(gpi, XX)
YY <- mvtnorm::rmvnorm(100, p$mean, p$Sigma)
q1 <- p$mean + qnorm(0.05, 0, sqrt(diag(p$Sigma)))
q2 <- p$mean + qnorm(0.95, 0, sqrt(diag(p$Sigma)))
# Plot
matplot(XX, t(YY), type="l", col="gray", lty=1,
main = "seperable GP ",
xlab="x", ylab="sin(x)", bty = "n")
points(X, y, pch=20, cex=2)
lines(XX, p$mean, lwd=2)
lines(XX, sin(XX), col="blue")
lines(XX, q1, lwd=2, lty=2, col=2)
lines(XX, q2, lwd=2, lty=2, col=2)
deleteGPsep(gpi)
rm(mle)
```
<br>
## Stock data example
<br>
The procedures above are now applied to real data, that being an estimation of stock valuation based on fundamental data.
<br>
### Data
```{r, results="hide", message=FALSE, warning=FALSE}
library(romerb)
data("stock_data")
fundamental_raw <- stock_data
rm(stock_data)
# Medical devices sector only
df <- fundamental_raw[fundamental_raw$sector == 7, ]
df$log_mkt_cap <- log(df$mkt_cap)
df$log_book <- log(-df$total_equity_cln)
df$roe <- -df$roe
df <- df[df$date_stamp == as.Date('2021-06-30'), c('log_book','log_mkt_cap','log_pb','roe','leverage')]
# nugget / jitter
eps <- sqrt(.Machine$double.eps)
# Training data
X <- matrix(df$roe)
y <- matrix(df$log_pb)
# Predictive grid
XX <- matrix(seq(min(X), max(X), length=200), ncol=1)
# Plot
plot(X, y, main = "Medical devices sector",
xlab = "Return on equity", ylab = "Price book ratio",
pch = 19, frame = FALSE)
```
```{r, results="hide", message=FALSE, warning=FALSE}
d <- darg(NULL, X)
g <- garg(NULL, y)
gpi <- newGPsep(X, y, d=d$start, g=g$start*0.5, dK=TRUE)
mle <- mleGPsep(gpi, param='both', tmin=c(d$min, g$min), tmax=c(d$max, g$max))
p <- predGPsep(gpi, XX)
YY <- mvtnorm::rmvnorm(100, p$mean, p$Sigma)
q1 <- p$mean + qnorm(0.05, 0, sqrt(diag(p$Sigma)))
q2 <- p$mean + qnorm(0.95, 0, sqrt(diag(p$Sigma)))
# Plot
matplot(XX, t(YY), type="l", col="gray", lty=1,
xlab = "Return on equity", ylab = "Price book ratio", bty = "n")
points(X, y, pch=20, cex=2)
lines(XX, p$mean, lwd=2)
lines(XX, q1, lwd=2, lty=2, col=2)
lines(XX, q2, lwd=2, lty=2, col=2)
deleteGPsep(gpi)
rm(mle)
```
## References
Functions creating dummy data. Useful for model testing.
[tpg vignette](https://cran.r-project.org/web/packages/tgp/vignettes/tgp2.pdf), refer page 3 for dummy data function
[Gold price function](https://cran.r-project.org/web/packages/GPfit/GPfit.pdf) from the ```GPfit``` doucmentation.
Other Gaussian Process packages
[GPBoost](https://github.com/fabsig/GPBoost)