-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
65 lines (50 loc) · 2.07 KB
/
README.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
---
title: "Gaussian Process Regression"
author: "Wajid Jawaid"
email: "[email protected]"
date: "`r Sys.Date()`"
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "./tools/README-"
)
set.seed(0)
```
[![Project Status: Active - The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)](http://www.repostatus.org/#active)
# gpr
Gaussian Process Regression allows simple use of gaussian processes to fit optimum lines without choosing a specific function.
## Example
This is an example taken from Carl Rassmussen's book on Gaussian processes. We can first sample from the prior.
```{r example1, fig.show = 'hold', fig.cap = 'Drawing random samples from prior.', fig.width =7, fig.height = 7, dev = "png"}
library(gpr)
d <- .1
x <- seq(-5,5,d)
K <- covMat(x, x, function(xx,xy) cf(xx,xy, l = 2))
L <- cholesky(K)
plot(NULL, xlim=c(-5,5), ylim=c(-2.5,2.5), xlab = "time", ylab = "counts")
rect(-6, -2, 6, 2, col="#44444411", lty=0)
for (i in 1:4) {
y <- sampleFromL(L)
points(x,y, pch=16, xlim=c(-5,5), ylim=c(-2,2), cex=.2, col = i, type = "l")
}
```
Now we can add data points (-4,-2); (-3,0); (-1,1); (0,2) and (2,-1). Now we can find the posterior distribution.
```{r example2, fig.show = 'hold', fig.cap = 'Posterior distribution after adding data', fig.width = 7, fig.height = 7, dev = 'png'}
x <- c(-4,-3,-1,0,2)
y <- c(-2,0,1,2,-1)
pst <- predictGP(x, y, xs = seq(-5,5,.1), cvFunc = cf, sigman = 0.1, l = 1)
plot(pst, p.pch = 3, p.cex = 2, xlab = "x", ylab = "y")
```
Now we can sample from the new posterior.
```{r example3, fig.show = 'hold', fig.cap = 'Random samples drawn from the posterior', fig.width = 7, fig.height = 7, dev = 'png'}
plot(pst, p.pch = 3, p.cex = 2, xlab = "x", ylab = "y")
L <- cholesky(pst$cv)
for (i in 1:5) {
yo <- sampleFromL(L, mu = pst$mean)
points(pst$x,yo, pch=16, xlim=c(-5,5), ylim=c(-2,2), cex=.2, col = i, type = "l")
}
```