-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
153 additions
and
616 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
reticulate::use_condaenv("bigsimr") | ||
# reticulate::use_condaenv("bigsimr") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,51 @@ | ||
.rmvn_jax <- function(seed, mu, Sigma, n) { | ||
.rmvn <- function(rho, n) { | ||
|
||
# Any RNG in jax requires a key. If none is supplied by the user, then it | ||
# should be generated at random. | ||
if (is.null(seed)) { | ||
set.seed(seed) | ||
seed <- as.integer(sample(1:1000, 1)) | ||
} | ||
|
||
rmvn <- jax$random$multivariate_normal | ||
R = reticulate::np_array(rho) | ||
S = numpy$linalg$cholesky(R) | ||
S_d = jax$device_put(numpy$transpose(S)) | ||
|
||
Sigma = numpy$array(Sigma, dtype=numpy$float32) | ||
mu = numpy$array(mu, dtype=numpy$float32) | ||
key = jax$random$PRNGKey(sample(.Random.seed, 1)) | ||
|
||
key = jax$random$PRNGKey(as.integer(seed)) | ||
size = reticulate::tuple(as.integer(n), | ||
as.integer(ncol(rho))) | ||
|
||
dev <- reticulate::py_to_r(jax$lib$xla_bridge$get_backend()$platform) | ||
z_d = jax$random$normal(key, size) | ||
x_d = jax$numpy$matmul(z_d, S_d) | ||
|
||
# mu = jax$device_put(mu) | ||
# Sigma = jax$device_put(Sigma) | ||
x = rmvn(key, mu, Sigma, reticulate::tuple(list(n)))$block_until_ready() | ||
x = jax$device_get(x) | ||
x = jax$device_get(x_d) | ||
|
||
reticulate::py_to_r(x) | ||
|
||
} | ||
|
||
|
||
#' Simulate random data from a multivariate normal distribution | ||
#' | ||
#' This function is designed to be 'bare bones' and does not check that the | ||
#' given covariance matrix is positive semi-definite. | ||
#' | ||
#' @param n number of random vectors to be simulated. | ||
#' @param mu vector of length d, representing the mean. | ||
#' @param sigma covariance matrix (d x d). Alternatively is can be the cholesky | ||
#' decomposition of the covariance. In that case isChol should be set to TRUE. | ||
#' @param isChol boolean set to true if sigma is the cholesky decomposition of | ||
#' the covariance matrix. | ||
#' @export | ||
rmvn <- function(n, mu, sigma, isChol = FALSE) { | ||
|
||
S = jax$device_put(reticulate::np_array(sigma)) # | ||
m = jax$device_put(reticulate::np_array(mu)) | ||
key = jax$random$PRNGKey(sample(.Random.seed, 1)) | ||
|
||
if (isChol) { | ||
size = reticulate::tuple(as.integer(n), as.integer(ncol(sigma))) | ||
z = jax$numpy$add(jax$numpy$matmul(jax$random$normal(key, size), S), m) | ||
} else { | ||
size = reticulate::tuple(as.integer(n)) | ||
z = jax$random$multivariate_normal(key, m, S, size) | ||
} | ||
|
||
reticulate::py_to_r(jax$device_get(z)) | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.