[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Difference between normal_rnd (m,v, n,n) and randn (n)
From: |
Paul Kienzle |
Subject: |
Re: Difference between normal_rnd (m,v, n,n) and randn (n) |
Date: |
Thu, 4 Mar 2004 23:49:46 -0500 |
On Mar 4, 2004, at 2:45 PM, Henry F. Mollet wrote:
What's the difference between between using normal_rnd (m,v, n,n) and
randn
(n) to generate random numbers from a normal distribution. The former
takes
much longer? I'll probably only need 10+4 random numbers rather than
10+6,
so speed may not present an actual problem but for other distributions
only
the slower random number generator is available?
These distributions were written for completeness, correctness,
consistency, flexibility and perhaps readability, but not for speed.
In particular, given the m,v parameters for the normal distribution,
the first step is to make m and v both full-sized matrices so that
the code for varying m,v is the same as that for fixed. This is
expensive.
With a bit of recoding, normal_rnd can be made just as correct,
and almost as efficient as m+sqrt(v)*randn(r,c):
octave:63> t=time; normrnd(3,4,1000,1000); time-t
ans = 0.77674
octave:64> t=time; 3+sqrt(4)*randn(1000,1000); time-t
ans = 0.73710
For compatibility, I've renamed it normrnd. My version should
support Nd arrays as well as matrices, assuming randn does.
function rnd = normrnd (m, v, dims, c)
if nargin < 3 || nargin > 4
usage('normal_rnd(m,v,dims)');
elseif nargin == 4
dims = [dims, c];
endif
if (!isscalar(m) && any(size(m)!=dims)) ...
|| (!isscalar(v) && any(size(v)!=dims))
error("dimensions do not match");
end
nanidx = isnan(m) | isinf(m) | v<0 | isinf(v);
if all(nanidx(:))
rnd = NaN*zeros(dims);
else
rnd = m + sqrt(v) .* randn (dims);
if any(nanidx(:)), rnd(nanidx) = NaN; end
end
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------
- Difference between normal_rnd (m,v, n,n) and randn (n), Henry F. Mollet, 2004/03/04
- Re: Difference between normal_rnd (m,v, n,n) and randn (n), Quentin Spencer, 2004/03/04
- Re: Difference between normal_rnd (m,v, n,n) and randn (n), Henry F. Mollet, 2004/03/04
- Re: Difference between normal_rnd (m,v, n,n) and randn (n), Quentin Spencer, 2004/03/04
- Re: Difference between normal_rnd (m,v, n,n) and randn (n), David Bateman, 2004/03/05
- Re: Difference between normal_rnd (m,v, n,n) and randn (n),
Paul Kienzle <=