-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymbolic-autodiff-in-R.Rmd
61 lines (47 loc) · 1.53 KB
/
Symbolic-autodiff-in-R.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
---
title: "Symbolic-autodiff-in-R"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
# Base R casually allows for AD black magic!
# Create function and gradient function
f <- deriv(expression(sin(2 * x^2 + y^2 - x)), c("x", "y"), func = T)
# Set values of independent variable
x <- y <- -10:10 / 10
l <- length(x)
# Function to plot tangent lines with gradients in direction of a vector
plot_tangent <- function(px, py, g, v){
vxy <- rbind(x, y) * v
lines(trans3d(vxy[1, ] + px, vxy[2, ] + py, f(px, py) + t(g %*% vxy),
pmat = res), col = 'blue')
}
# Randomly choose points to evaluate gradient
n_pts <- 1
px <- x[sample(length(x), n_pts)]
py <- y[sample(length(y), n_pts)]
# Get gradient
g <- attr(f(px, py), "gradient")
# Plot function values
par(mar = rep(0, 4))
res <- persp(x, y, outer(x, y, f), phi = 0, theta = 135)
# Plot tangent points
points(trans3d(px, py, f(px, py), pmat = res), col = 'blue')
# Set direction vectors for gradients
sr2i <- 2^-1/2
v_mat <- matrix(c(1, 0, 0, 1, sr2i, sr2i, sr2i, -sr2i), nrow = 2)
# Plot tangent lines
for (i in 1:n_pts) {
apply(v_mat, 2, function(v) plot_tangent(px[i], py[i], g[i, ], v))
}
# Find minimum using analytic gradient!
o <- optim(par = c(0.5, 0.5),
fn = function(x) f(x[1], x[2]),
gr = function(x) attr(f(x[1], x[2]), "gradient"),
method = "BFGS")
# Plot tangents at minimum
apply(v_mat, 2, function(v)
plot_tangent(o$par[1], o$par[2], attr(f(o$par[1], o$par[2]), "gradient"), v))
```