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
  • 260,367
  • 30
  • 546
  • 667
sateayam
  • 1,009
  • 2
  • 16
  • 37

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
  • 100,816
  • 18
  • 175
  • 263
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
  • 260,367
  • 30
  • 546
  • 667
  • 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,832
  • 2
  • 19
  • 26
0

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

iehrlich
  • 3,552
  • 4
  • 31
  • 42
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
  • 26,060
  • 8
  • 73
  • 128