Re: [R] Need vector of random double exponentials

From: David Winsemius <>
Date: Fri, 21 Mar 2008 21:01:21 +0000 (UTC) wrote in

> Hi, I am fairly new to R, and am stuck.

Please use an informative subject line when posting. (And read the Posting Guide. <>  

> I want to write an R function with argument n that returns a vector
> of length n with n simulated observations from the double
> exponential distribution with density: ??g(y) = 1/2e^-y

I think that might be g(y) = (1/2)e^(-abs(y)). Johnson, Kotz and Balakrishnan "Cont. Univariate Distr. 2nd ed" illustrate the acceptance-rejection method for Laplace random numbers on p 154. At any rate, there is already a package that will generate the desired random numbers."double exponential")

#tells you:
#DExp-class(distr)             Class "DExp"
#so DExp is a class in the "distr" package

#make D a distribution argument to distr's r(), d(), and p() functions D <- DExp(rate=1) #which is the Laplace fucntion # make rvec a 100 element vector of random Dexp(rate=1) rvec<-r(D)(100)

> For the double exponential, I want to generate y~Exp(1) and then
> take ?y with probability 0.5

#create 100 random binomials
#[1] 0 0 1 0 1 0

#0.0000000 0.0000000 0.6638434 0.0000000 0.4638312 0.0000000

Or if you didn't want the extraneous zeros as placeholders you could strip them out with:

onehalf[onehalf != 0]
#[1] 0.663843413 0.463831152 2.586626569 1.227912636 3.860839987 # -3.394600764 0.542588566 <snip>

Note: this does not give you 50 elements every time. mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Fri 21 Mar 2008 - 21:05:21 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 21 Mar 2008 - 21:30:24 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive