Skip to content

Instantly share code, notes, and snippets.

@enijkamp
Last active November 1, 2016 06:02
Show Gist options
  • Save enijkamp/9d803e4bd5b105ddea0e2871fe2f5da9 to your computer and use it in GitHub Desktop.
Save enijkamp/9d803e4bd5b105ddea0e2871fe2f5da9 to your computer and use it in GitHub Desktop.
# python wrapper functions for R pseudo random number generators
# allows us to compare numerical results between R and python implementations
# source activate <env>
# pip install rpy2 --upgrade
import numpy as np
import readline
import rpy2.robjects as robjects
def R_set_seed(s: int) -> None:
set_seed = robjects.r('set.seed')
set_seed(s)
def R_standard_normal(shape: tuple) -> np.ndarray:
return(np.reshape(robjects.r.rnorm(shape[0] * shape[1]), shape, order='F'))
def R_uniform(lo: float, up: float , shape: tuple) -> np.ndarray:
return(np.reshape(robjects.r.runif(shape[0] * shape[1], lo, up), shape, order='F'))
def test():
R_set_seed(123)
X = R_standard_normal((5, 2))
print(X.shape)
print(X)
Y = R_uniform(0.0, 1.0, (5, 2))
print(Y.shape)
print(Y)
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment