0

I am trying to code this algorithm. I am stuck in the part of log((1.0-u)/u))/beta; As I understand, I can not get the result of this in C, as it will always return me with negative value log (returning imaginary value). Tried to print the result of log(1-5) for instance, it gives me with Nan. How can I get the result of

double x = (alpha - log((1.0-u)/u))/beta

then?

Would appreciate for any pointers to solve this problem.

Thank you

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
sateayam
  • 1,089
  • 3
  • 16
  • 38

5 Answers5

3

In that algorithm, u should be uniform random on [0,1], so the quantity (1-u)/u is never negative.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269
2

Don't pass in a value of u outside the range (0,1) (this is mentioned in one of the comments in that article). Note that ( and ) denote open (i.e. exclusive) bounds.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • 1
    This is correct; if you read down in the Poisson algorithm you link, it states that the `random()` function listed in the pseudocode returns a number in the range (0,1). However, if you do need to handle complex numbers, you should look at C99 complex number support. See this article for more information: http://stackoverflow.com/questions/6418807/how-to-work-with-complex-numbers-in-c – Ethan Brown Mar 30 '12 at 17:37
1

As stated you need the range of u to be (0,1) (if u is 0 or 1 you are in trouble). You are probably using rand() in which case you will want

double u = (rand() + 1) / (double)(RAND_MAX + 2);
double x = (alpha - log((1.0-u)/u))/beta
RunHolt
  • 1,892
  • 2
  • 19
  • 26
0

I guess u lays between 0 and 1, thus being a normalized positive random.

iehrlich
  • 3,572
  • 4
  • 34
  • 43
0

log( (1-u)/u ) = log(1-u)-log(u) and thus u needs to be 0<u<1. The graph looks like this

log(1-u)-log(u)

In fact you see the anti-symmetry by u=0.5 you may need to worry only for values between 0 and 0.5, but not including 0. So for u>0.5 you set u=1-u and return -log(1-u)+log(u).

John Alexiou
  • 28,472
  • 11
  • 77
  • 133