UrbanInstitute / prison-population-forecaster

Translating the Prison Population Forecaster R script to javascript
5 stars 3 forks source link

2025 values extremely large for select state/offense combinations #2

Closed bchartoff closed 6 years ago

bchartoff commented 7 years ago

See comment commit b94958f6c2570eaf5aec9530726b4bb0b0fdd2e4 for partially isolated problem

DanielJWood commented 6 years ago

Confirmed that this error is within JS, this is NOT present in the R environment.

DanielJWood commented 6 years ago

This is due to l_values[i] approaching 0 and then 1 being divided by it in this line:

p_values.push(1-(1/l_values[i]));

Next step is search out this in the original and see how they handle this.

DanielJWood commented 6 years ago

From the R script, some helpful notes:

  # l = estimate for LOS based on proportion remaining in prison after one year
  # p = proportion remaining in prison after one year
  # L= 1/(1-p)
  # p = 1-(1/l)
DanielJWood commented 6 years ago

solved! The cursed FLOATING point issues strike again. I'm not exactly sure why, but in the R script, the calculation for the l_value[i] for -100% Length of stay was calculating to 0, while in Js, error introduced somewhere would cause it to hover just under 0. The upshot of that is that 1/0 in js or R gives -Infinity. Still try to remember why this is the solution rather than something that breaks the program, but it is the solution. In the js, after calculating the l_value, I included this conditional to reset the l_value to 0 if it is very very close to 0, but not zero:

        if (l_values[i] > -0.000000001 && l_values[i] < 0.000000001) {
            l_values[i] = 0;
        }

This worked, and the pop at year 25 is now the same in the R script and the JS for 100% reduction in length of stay.

To do:

DanielJWood commented 6 years ago

Fixed in commit 0b8f92d, good example to use for the blog post.