is there a way to draw generate a figure like this from data in R?

this image coms from The deep learning book Figure 2.3

is there a way to draw generate a figure like this from data in R?

image

You can do something like this:

theta_sim <- seq(from = -pi,
                 to = pi,
                 length.out = 1e4)

obs_sim <- rbind(cos(x = theta_sim),
                 sin(x = theta_sim))

A <- matrix(data = c(-2, 1, -3, 4),
            nrow = 2,
            ncol = 2)

A_eig <- eigen(x = A)

obs_sim_tr <- A %*% obs_sim

par(mfrow = c(1, 2))

plot(x = obs_sim[1, ],
     y = obs_sim[2, ],
     type = "p",
     main = "Before multiplication",
     xlab = expression(x[0]),
     ylab = expression(x[1]),
     asp = 1,
     col = "blue",
     pty = "s")

arrows(x0 = 0,
       y0 = 0,
       x1 = A_eig$vectors[1, ],
       y1 = A_eig$vectors[2, ],
       length = 0.1)

text(x = A_eig$vectors[1, ],
     y = A_eig$vectors[2, ],
     labels = c(expression(v ^ (1)), expression(v ^ (2))),
     pos = 4)

plot(x = obs_sim_tr[1, ],
     y = obs_sim_tr[2, ],
     type = "p",
     main = "After multiplication",
     xlab = expression(x[0] * minute),
     ylab = expression(x[1] * minute),
     asp = 1,
     col = "blue",
     pty = "s")

arrows(x0 = 0,
       y0 = 0,
       x1 = c((A_eig$values * A_eig$vectors[1, ]), A_eig$vectors[1, ]),
       y1 = c((A_eig$values * A_eig$vectors[2, ]), A_eig$vectors[2, ]),
       length = 0.1)

text(x = c((A_eig$values * A_eig$vectors[1, ]), A_eig$vectors[1, ]),
     y = c((A_eig$values * A_eig$vectors[2, ]), A_eig$vectors[2, ]),
     labels = c(expression(paste(lambda[1], "v" ^ (1))), expression(paste(lambda[2], "v" ^ (2))), expression(v ^ (1)), expression(v ^ (2))),
     pos = 4)

Created on 2019-06-27 by the reprex package (v0.3.0)

Undoubtedly, this graph can be made much better with more customisation, and for sure there'll some ggplot way to do it. You can do those, share with us.

Note that, for the particular matrix A that I've chosen, your previous thread regarding signs of eigenvectors is very evident. The direction remains the same, but the quadrant changes.

1 Like

Thanks for your great work! Does "previous thread" mean the description in Figure 2.3 in the book?

does "sim" stand for simulator?

When I said previous thread, I meant this thread:

I mentally read theta_sim as simulated values of theta. It's just a name I used, no significance at all.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.