JeffreyRacine / R-Package-np

R package np (Nonparametric Kernel Smoothing Methods for Mixed Data Types)
https://socialsciences.mcmaster.ca/people/racinej
47 stars 18 forks source link

Running with seeds that are too large causes R session freeze #47

Closed mb706 closed 1 month ago

mb706 commented 3 months ago

The following causes the R session to freeze:

np::npseed(1114043386)
for (i in 1:10) np::npudensbw(iris[c(1, 51, 101), ])

This finishes within a few seconds when using a smaller seed, but this particular seed causes the session to hang.

I believe what is happening is that the ran3()-code does not anticipate negative values here. Note that the C %-operator returns negative values when its left-hand operand is negative. If the initial seed is large enough, the ran3() return values are no longer between 0 and 1 after a few invocations. The result is an essentially endless loop here.

Not every large value leads to an R session freeze. I only stumbled upon this because my script runs many restarts with new (random) seeds every time.

The smallest seed that I could find that froze the session is 540714429. If you want to "mine" for seeds that break the session, you can do

repeat {
  s <- as.integer( (2^31-1) * runif(1))
  cat(sprintf("Trying %s ...\n", s))
  np::npseed(s)
  for (i in 1:10) np::npudensbw(iris[c(1, 51, 101), ])
  # I also tried loops with 200 iterations, but it turns out that pretty much
  # every "bad" seed that I found killed the session after 5 to 7 iterations. 
  cat("seems safe.\n")}
}

Once the output gets stuck at Multistart n of 5 |, the session is dead.

mb706 commented 3 months ago

The ran3() function seems to be from Numerical Recipes in C, Chapter 7.1. Note that there, the initialization of mj is

mj=labs(MSEED-labs(*idum));

which avoids negative values by using labs.

JeffreyRacine commented 1 month ago

Greetings,

Apologies for the lengthy delay. Thanks for your diligent sleuthing, I will be sure to incorporate this in the next update.

Jeff