Is it possible to play randn () MATLAB with NumPy?

I wonder if it is possible to accurately reproduce the entire randn () MATLAB sequence with NumPy. I coded my own routine with Python / Numpy, and this gives me slightly different results from the MATLAB code that someone else did, and I find it hard to find where it comes from, due to various random calls.

I found the value numpy random.seed, which produces the same number for the first draw, but from the second draw and further it is completely different. I draw a multidimensional normal value, for example, 20,000 times, so I don’t want to just save Matlab drawings and read it in Python. If there is any other way, I guess I should do it. Please let me know.

-Joon

+4
source share
3 answers

The user asked if the output of randn () from Matlab could be reproduced, not rand. I could not set the algorithm or seed to reproduce the exact number for randn (), but the solution below works for me.

In Matlab: configure your regular distributed random numbers as follows:

rng(1); norminv(rand(1,5),0,1) ans = -0.2095 0.5838 -3.6849 -0.5177 -1.0504 

In Python: generate your regular distributed random numbers as follows:

 import numpy as np from scipy.stats import norm np.random.seed(1) norm.ppf(np.random.rand(1,5)) array([[-0.2095, 0.5838, -3.6849, -0.5177,-1.0504]]) 

It is very convenient to have functions that can play the same random numbers when switching from Matlab to Python or vice versa.

+2
source

If you set the random number generator to the same seed, it will theoretically create the same numbers, i.e. in matlab. I'm not quite sure how to do this, but this seems to work in MATLAB:

 rand('twister', 5489) 

and matches in numy:

 np.random.seed(5489) 

To (re) initialize random number generators. This gives me the same numbers for rand () and np.random.random (), but not for randn, I'm not sure if there is a simple method for this.

With newer versions of Matlab, you can probably configure RandStream with the same properties as numpy, for older you can play numpy randn in matlab (or vice versa). Numpy uses the polar form to create uniform numbers from np.random.random () (second algorithm given here: http://www.taygeta.com/random/gaussian.html ). You can simply write this algorithm in Matlab to create the same randn numbers as numpy from the rand function in matlab.

If you don't need a huge amount of random numbers, just save them to .mat and read them from scipy.io, though ...

+5
source

I just wanted to clarify using the twister / seeding method: MATLAB and numpy generate the same sequence using this series, but will fill them differently in the matrices.

MATLAB fills the matrix down with columns , and python down the rows . So, to get the same matrices in both, you need to transpose:

MATLAB:

 rand('twister', 1337); A = rand(3,5) A = Columns 1 through 2 0.262024675015582 0.459316887214567 0.158683972154466 0.321000540520167 0.278126519494360 0.518392820597537 Columns 3 through 4 0.261942925565145 0.115274226683149 0.976085284877434 0.386275068634359 0.732814552690482 0.628501179539712 Column 5 0.125057926335599 0.983548605143641 0.443224868645128 

python:

 import numpy as np np.random.seed(1337) A = np.random.random((5,3)) AT array([[ 0.26202468, 0.45931689, 0.26194293, 0.11527423, 0.12505793], [ 0.15868397, 0.32100054, 0.97608528, 0.38627507, 0.98354861], [ 0.27812652, 0.51839282, 0.73281455, 0.62850118, 0.44322487]]) 

Note. I also put this answer to this similar question: Comparison of Matlab and Numpy code using random number generation

+1
source

All Articles