s-news
[Top] [All Lists]

Re: Generating random real numbers

To: "'Joseph Beyene'" <joseph@utstat.toronto.edu>, "'Mike C. Sawada'" <msawada@aix1.uottawa.ca>
Subject: Re: Generating random real numbers
From: "Nick Ellis" <nick.ellis@marine.csiro.au>
Date: Wed, 4 Apr 2001 16:06:11 +1000
Cc: "'Alastair Dempster'" <demp2979@mach1.wlu.ca>, "S-news (E-mail)" <s-news@wubios.wustl.edu>
Importance: Normal
In-reply-to: <200104040157.UAA11814@wubios.wustl.edu>
Reply-to: <nick.ellis@marine.csiro.au>
Simply use rejection sampling, rejecting any values lying outside the range
[a,b]. Then the sample will be from a distribution that is proportional to
the normal distribution on [a,b] and zero outside. The distribution itself
is not normal.

Here's an example implementation

rnorm.trunc <- function(x,mu=0,sd=1,a=-Inf,b=Inf) {
        n <- length(x)
        if (n==1) n <- x
        totsamp <- rnorm(n,mu,sd)
        outside <- totsamp<a | totsamp>b
        while (any(outside)) {
                totsamp[outside] <- rnorm(sum(outside),mu,sd)
                outside <- totsamp<a | totsamp>b
        }
        totsamp
}

Nick Ellis
CSIRO Marine Research   mailto:Nick.Ellis@marine.csiro.au
PO Box 120                      ph    +61 (07) 3826 7260
Cleveland QLD 4163      fax   +61 (07) 3826 7222
Australia                       http://www.marine.csiro.au



> -----Original Message-----
> From: s-news-owner@lists.biostat.wustl.edu
> [mailto:s-news-owner@lists.biostat.wustl.edu]On Behalf Of
> Joseph Beyene
> Sent: Wednesday, 4 April 2001 11:57
> To: Mike C. Sawada
> Cc: Alastair Dempster; splus list
> Subject: Re: [S] Generating random real numbers
>
>
> If seems to me the objective of the original question
> is to generate random numbers from a distribution that
> is assumed to follow a normal distribution on the
> support (0.005, 0.035).
>
> Hence, the question boils down to determining the mean (mu)
> and standard deviation (sigma) of the distribution,
> since these two parameters fully specify the underlying
> normal distribution.
>
> Step-1: The Gaussian mean parameter should be located at the
>         center of the support. Here the center is
>        (0.005 + 0.035)/2 = 0.02 = mu;
>
> Step-2: Determine sigma using the following simple algebra:
>
>       P(0.005 < X < 0.035) = 1
>
> =>  P((0.005-0.02)/sigma < (X-0.02)/sigma < (0.035-0.02)/sigma) =1
>
> => P(-0.015/sigma < Z < 0.015/sigma) = 1, where Z is N(0,1).
>
> But we also know that P(-4 < Z < 4) is ALMOST 1, so can set
>
> 4 = 0.015/sigma, which implies sigma = 0.00375.
>
>
> Step-3:  We can now generate random samples from a N(0.02, 0.00375)
> distribution in the usual way.
>
> e.g.
>
> > rnorm(n=10,mean=0.02,sd=0.00375)
>  [1] 0.01880891 0.01918489 0.02010347 0.01819819
>  [5] 0.01437798 0.01553930 0.01847044 0.02031811
>  [9] 0.02246164 0.02356001
>
>
> Am I missing the point of the question? I would
> like to hear about alternative (easier) approaches and/or
> whether the above approach suffers from a major
> technical problem.
>
> Regards,
>
>
> Joseph Beyene
> (joseph@utstat.torornto.edu)
>
> Previously, Mike C. Sawada wrote:
> >
> > Try:
> >
> >  runif(100,min=0.005,max=0.035)
> >
> > will produce 100 real numbers (real as far as rational and
> irrational
> > numbers may ocurr) between the ranges of 0.005 and 0.035.
> >
> >
> --------------------------------------------------------------
> -----------------
> > Mike C. Sawada                                     |Tel:
> 613-562-5800 x1734
> > Laboratory of Paleoclimatology and Climatology |Fax: 613-562-5145
> > Department of Geography
|msawada@aix1.uottawa.ca
> > University of Ottawa
> > 165 Waller Street
> > Ottawa, ON K1N 6N5
> >   -----------------
-----------------
> >     ************************************************************
> >     WWW: http://www.uottawa.ca/academic/arts/geographie/lpcweb/
> >     ************************************************************
> >
> --------------------------------------------------------------
> -----------------
> >
> > On Tue, 3 Apr 2001, Alastair Dempster wrote:
> >
> > > I'm posting this for an Splus user who is not part of this
> > > discussion group:
> > >
> > > She writes:
> > > "I am interested in generating a set of 100 random real numbers
> > > between 0.005 and 0.035, where the probability of drawing
> a specific
> > > value is
> > > defined by the normal distribution.  rnorm() will not let
> me define
> > > the limits of
> > > my dataset, yet my model parameter values cannot physically exceed
> > > 0.035 by
> > > definition.  Is anyone aware of how to specify range limits when
> > > generating random
> > > numbers with a non-uniform distribution? I have explored sample(),
> > > runif(), rnorm(), and rmvnorm(), all of
> > > which nicely generate random numbers.  However, either
> they are not
> > > real (as in the case of sample()), or I cannot figure out how to
> > > define a range of values for the random sample."
> > >
> > > She is using Splus 2000 under NT4.
> > >
> > > I have had a look at the discussions about random number
> generation
> > > in the archives but did not find anything suitable.
> > >
> > > Can anyone tell us how to code this or provide a
> reference?  I will
> > > post a summary on behalf of my colleague.
> > >
> > > Thank you,
> > >
> > > Alastair Dempster
> > > Wilfrid Laurier University
> > > Waterloo, Canada
> > >
> > >
> > >
> > >
> ---------------------------------------------------------------------
> > > This message was distributed by
> s-news@lists.biostat.wustl.edu.  To
> > > unsubscribe send e-mail to
> s-news-request@lists.biostat.wustl.edu with
> > > the BODY of the message:  unsubscribe s-news
> > >
> >
> >
> ---------------------------------------------------------------------
> > This message was distributed by s-news@lists.biostat.wustl.edu.  To
> > unsubscribe send e-mail to
> s-news-request@lists.biostat.wustl.edu with
> > the BODY of the message:  unsubscribe s-news
> ---------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu.  To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message:  unsubscribe s-news
>


<Prev in Thread] Current Thread [Next in Thread>