I want to generate a sample of vectors from the high dimensional sphere with noise.
I.E. I'm trying to create a sample such that any vector X is in R^N and holds ||X+epsilon||^2 = 1 where epsilon is iid vector in R^N of which any component epsilon_j is distributed N(0,sigma^2).
Does someone have any idea how to implement it? I'd prefer to use R.
Thank you!
I think this should work. It could easily be turned into a function.
d = 5 # number of dimensions
n_draws = 100 # number of draws
sigma = 0.2 # standard deviation
I begin by sampling random vectors that should be uniformly distributed on the unit sphere. I do this by normalizing draws from a d-dimensional multivariate normal distribution. (There's probably a more direct way to do this step, but I'll be using rmvnorm
again later so this was convenient.) I call them dirs
because, since we're normalizing, all we're really doing in this step is sampling "directions".
library(mvtnorm)
# sample
dirs = rmvnorm(n = n_draws, mean = rep(0, d))
# normalize
dirs = dirs / sqrt(rowSums(dirs^2))
Now we do another draw from the multivariate normal to add noise.
x = dirs + rmvnorm(n = n_draws, mean = rep(0, d), sigma = sigma * diag(d))
To map this to the variables you used in your question, define Y = X + epsilon. My dirs
is Y, then the noise I add is -epsilon; adding them yields the X that you asked for.