COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### other method to simulate an exponential

• Follow

```Hello.
I'm programing a simulation of ruin probabilities in C++.
Is there any other method to simulate an exponential faster than
y=-ln(1-uniform(0,1))/b?
I think that using the log function makes things slower in C++.
Many thanks.
Lourdes

```
 0
Reply 9/19/2005 9:05:57 PM

```In comp.simulation Lourdes <lourdes.afonso@gmail.com> wrote:
> Hello.
> I'm programing a simulation of ruin probabilities in C++.
> Is there any other method to simulate an exponential faster than
> y=-ln(1-uniform(0,1))/b?
> I think that using the log function makes things slower in C++.

Goodness. How many are you doing? ln 1+-x is pretty fast. On typical
desktops it's 1 pipeline cycle. The 1/b may take a couple, but you can

const double foo  = -1./b;

...

y = foo*ln(1-u());

If you have some old crock machine then make a table of pre-determined
u(0,1) and mean.

I would not actually be guess to think generating the numbers is a tiny
fraction of the overall runtime of your sim. Think of how much time goes in
getting the event queue in order (or inserting events in the right place, or
whatever).

```
 0
Reply russell 9/20/2005 2:01:10 AM

```I'm doing a_lot of simulations.
Thanks for the tip.

May I ask you what is faster. pow(a,2) or a*a ?
Best regards.

```
 0
Reply Lourdes 9/21/2005 10:58:53 PM

```Lourdes wrote:
> Hello.
> I'm programing a simulation of ruin probabilities in C++.
> Is there any other method to simulate an exponential faster than
> y=-ln(1-uniform(0,1))/b?
> I think that using the log function makes things slower in C++.
> Many thanks.
> Lourdes

The fastest way to generate an exponential variable
is probably Marsaglia & Tsang's "ziggurat" method.
See the Journal Statistical Software, v 5, Issue 8, 2000.
http://www.jstatsoft.org/v05/i08/ziggurat.pdf

```
 0
Reply Ray 9/21/2005 10:59:08 PM

```In comp.simulation Lourdes <lourdes.afonso@gmail.com> wrote:
> I'm doing a_lot of simulations.
> Thanks for the tip.
> May I ask you what is faster. pow(a,2) or a*a ?
[...]

While a pow(a,2) will eventually execute a*a there is obviously a subroutine
call and some cache invalidation extra.

Perhaps h/w & other speed issues should go over to comp.arch.

```
 0
Reply russell 9/22/2005 4:21:12 AM

```In article <dgtbfb\$qgj\$1@chessie.cirr.com>,
russell kym horsell  <kym@otaku.freeshell.org> wrote:
>In comp.simulation Lourdes <lourdes.afonso@gmail.com> wrote:
>> I'm doing a_lot of simulations.
>> Thanks for the tip.
>> May I ask you what is faster. pow(a,2) or a*a ?
>[...]

>While a pow(a,2) will eventually execute a*a there is obviously a subroutine
>call and some cache invalidation extra.

Some compilers will set this up, and some will not.
I have used "standard" compilers which would compute
pow(a,2) as exp(2*ln(a)).

>Perhaps h/w & other speed issues should go over to comp.arch.

--
This address is for information only.  I do not claim that these views
are those of the Statistics Department or of Purdue University.
Herman Rubin, Department of Statistics, Purdue University
hrubin@stat.purdue.edu         Phone: (765)494-6054   FAX: (765)494-0558

```
 0
Reply hrubin 9/25/2005 11:22:16 AM

```In comp.simulation Herman Rubin <hrubin@stat.purdue.edu> wrote:
[...]

Long time no see. ;-)

```
 0
Reply russell 9/26/2005 4:04:59 AM

```russell kym horsell wrote:
> In comp.simulation Herman Rubin <hrubin@stat.purdue.edu> wrote:
> [...]
>
>
>
> Long time no see. ;-)

Thank you for the informations.
I'm going to use a*a.

Lourdes

```
 0
Reply Lourdes 9/28/2005 12:04:37 AM

```russell kym horsell wrote:

> In comp.simulation Lourdes <lourdes.afonso@gmail.com> wrote:
>> I'm doing a_lot of simulations.
>> Thanks for the tip.
>> May I ask you what is faster. pow(a,2) or a*a ?
> [...]
>
>
>
> While a pow(a,2) will eventually execute a*a there is obviously a
> subroutine call and some cache invalidation extra.
>
>
> Perhaps h/w & other speed issues should go over to comp.arch.

That assumes that his compiler does not optimize pow(a,2) to a*a.  BTW for
anything less than a**16 (a^16) (assuming Russian peasant method) pow(a,z)
is clearly slower (for integer z).  That is because i know how the hardware
can do it.  Translation; for integer exponents (z) less than 16 use methods
other than pow(a,z).

--
JosephKK

```
 0
Reply JosephKK 10/21/2005 5:43:58 AM

```>I'm programing a simulation of ruin probabilities in C++.
>Is there any other method to simulate an exponential faster than
>y=-ln(1-uniform(0,1))/b?
>I think that using the log function makes things slower in C++.
>Many thanks.
>Lourdes

(1-uniform(0,1))

use

(uniform(0,1))

Because "1-uniform(0,1)" is just as uniformly distributed as
"uniform(0,1)".

Good luck.

```
 0
Reply Wim 10/30/2005 12:17:20 PM

```"Wim D" <newscomVERVANGCOMDOORWIM@hotmail.com> wrote in message
news:a4atl1pbd3bndsjcvjt97gsnqshus8bag2@4ax.com...
> >I'm programing a simulation of ruin probabilities in C++.
>>Is there any other method to simulate an exponential faster than
>>y=-ln(1-uniform(0,1))/b?
>>I think that using the log function makes things slower in C++.
>>Many thanks.
>>Lourdes
>
>
> (1-uniform(0,1))
>
> use
>
> (uniform(0,1))
>
> Because "1-uniform(0,1)" is just as uniformly distributed as
> "uniform(0,1)".
>
> Good luck.
>
Unless Uniform(0,1) is capable of generating a zero. -ln(0) is undefined.
(1-Uniform(0,1)) also gives trouble if 1.0 is a possible RV.

```
 0
Reply Stephen 10/30/2005 1:04:49 PM

10 Replies
239 Views

(page loaded in 0.149 seconds)

Similiar Articles:

7/29/2012 11:31:02 PM