# [Numpy-discussion] random seed replicate 2d randn with 1d loop

josef.pktd at gmail.com josef.pktd at gmail.com
Mon May 23 15:34:04 EDT 2011

```On Mon, May 23, 2011 at 3:02 PM, Robert Kern <robert.kern at gmail.com> wrote:
> On Mon, May 23, 2011 at 13:33,  <josef.pktd at gmail.com> wrote:
>> I have a function in two versions, one vectorized, one with loop
>>
>> the vectorized function  gets all randn variables in one big array
>> rvs = distr.rvs(args, **{'size':(nobs, nrep)})
>>
>> the looping version has:
>>    for irep in xrange(nrep):
>>        rvs = distr.rvs(args, **{'size':nobs})
>>
>> the rest should be identical (except for vectorization
>>
>> Is there a guarantee that the 2d arrays are filled up in a specific
>> order so that the loop and vectorized version produce the same result,
>> given the same seed?
>
> No general guarantee for all of the scipy distributions, no. I suspect
> that all of the RandomState methods do work this way, though.

Thanks Keith, Robert,

It should work then for some test cases, but no guarantee since the
function should eventually take arbitrary distributions.

Obviously I was working by columns, using a transpose worked, but
rewriting to axis=1 instead of axis=0 which should be more efficient
since I had almost all calculations by columns, I needed
params = map(lambda x: np.expand_dims(x, 1), params)
to get around broadcast errors that work automatically with axis=0

>>> np.random.seed(8765679)
>>> [bootstrap(NewNorm(), args=(0,1), nobs=nobs, nrep=10000, value=c/ (1 + 4./nobs - 25./nobs**2)) for c in [0.576, 0.656, 0.787, 0.918, 1.092]]
[0.14449999999999999, 0.0901, 0.042200000000000001, 0.0212, 0.0076]

>>> np.random.seed(8765679)
>>> [bootstrap2(c/ (1 + 4./nobs - 25./nobs**2), NewNorm(), args=(0,1), nobs=nobs, nrep=10000) for c in [0.576, 0.656, 0.787, 0.918, 1.092]]
[0.14449999999999999, 0.0901, 0.042200000000000001, 0.0212, 0.0076]

Josef

>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma that is made terrible by our own mad attempt to interpret it as
> though it had an underlying truth."
>   -- Umberto Eco
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

```

More information about the NumPy-Discussion mailing list