Roundoff error issue -- 1 vs 0.9999999999999999

Practice lab 2, on anomaly detection

To be honest, this is a level of precision I have never dealt with in an error that is not actually an integer. If this F1 error were an integer, I could round or truncate or take the ceiling. But it’s not.

I have done some extremely fine detail work in porting FORTRAN to C++ for a general relativity code, however, truncation and roundoff error after a few thousand or more steps eventually did accumulate as a discrepancy between the two codes, so the finest match I was able to implement for the black hole orbits was about 10^-11 to 10^-13 relative error. It was our belief that was roundoff error based on a truncation error analysis of the convergence of the discontinuous galerkin, Runga Kutta, and multipole moment modes and orders. Although the original intent was to use the C++ code to parallelize in HPX, we eventually shifted to comparing the code to a collaborators frequency domain code, which involved a python error analysis.

HOWEVER, I’m not totally sure what to do with the distinction between 0.9999999999999999 and 1 in python, for a number that is fundamentally a float, as the F1 statistic is.

If it were 0.9 repeating in mathematics, the two numbers would be literally equal, but clearly they’re not quite, since there is in fact a digit where the 9 stops repeating. The difference is obviously pretty small.

The place where I’m seeing this is in the select_threshold graded function.

Best epsilon found using cross-validation: 8.990853e-05
Best F1 on Cross Validation Set: 0.875000
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-43-d487302cf8bb> in <module>
      6 
      7 # UNIT TEST
----> 8 select_threshold_test(select_threshold)

~/work/public_tests.py in select_threshold_test(target)
      8     best_epsilon, best_F1 = target(y_val, p_val)
      9     assert np.isclose(best_epsilon, 0.04, atol=0.3 / 1000), f"Wrong best_epsilon. Expected: {0.04} got: {best_epsilon}"
---> 10     assert best_F1 == 1, f"Wrong best_F1. Expected: 1 got: {best_F1}"
     11 
     12     y_val = np.array([1] * 5 + [0] * 25)

AssertionError: Wrong best_F1. Expected: 1 got: 0.9999999999999999

I feel like I probably did compute the values correctly, since my output essentially matches the expected output… and I suppose given that multiple methods are allowed for solving this problem, it’s completely possible to have answers with small variations due to roundoff error. However, I’m not totally sure how to fix this so that it passes the autograder.

I think I could write a short section rounding it to 3 significant figures as shown in the output, which would correct the roundoff error in the 0.9999999999999999 to the 1 (which is what the autograder asks for), but I’m not sure whether or not that’s the intended method. Could someone please clarify?

Thanks,
Steven

1 Like

Have you modified the select_threshold_test() function?

It’s not best practice in python to use ‘==’ to compare floating point and integer values. That’s why the “np.isclose()” function exists.

Should np.where(condition,1,0) return a an array of floats or integers? Should y_val contain floats or integers? Otherwise, I don’t think there was any such == between floats in there…

I did find an alternate solution…

I’d like to add a random, and potentially not very important, comment clarifying something I said about my last work. 10^-11 relative error means that the FORTRAN and C++ code were identical to 11 significant figures. It does not mean that there was some parameter, hyperparameter, or coefficient, that was around 10^-11 in value (specifically in this instance).

I’m referring to this code from your OP. If you wrote that code, it’s not really a good use case for "=='.

No that’s in the tests that came with the lab!

I agree that’s terrible!

To be totally clear, that’s one of the cells I’m not allowed to modify!

To be double totally clear, I don’t share code I wrote here because it would be cheating!

1 Like

To be triple totally clear, I have already passed using another method, but I think it would still be great to resolve this, since clearly there’s a strange issue with the cell that grades the function we’re asked to write

1 Like

Yes, I see that there now after a text search. Ouch. I was looking at the other two asserts at the end of that test function.

I have not fielded any questions from other students who had that assert trigger.

Can you use a personal message and send me your code for select_threshold() so I can inspect it?

Hi… I sent you the notebook but I don’t see it, as well as two other solutions where I solved it correctly.

The notebook where there was a roundoff errror issue had a mistake in it, but it wasn’t the roundoff error issue. The assert was triggering on the wrong issue.

I did successfully debug my problem, had already successfully submitted my assignment for a 100% grade when I messaged you, and sent you a second correct solution as well. I don’t see the message logging in on my cell phone just now. Let me know if you still need it.




See… I can’t find the message I sent you. But I am going to assume at this point that it’s not important since I solved it and it was graded as complete and one hundred percent correct three days ago

I got your personal messages. I have not had a chance to look at them yet.

It’s very difficult to find personal messages that you have sent. You have to go to your forum Profile, then click on Messages, then on Sent.

Example:

a

Thanks so much! I don’t know why I see it on my computer but not my cell phone, but that did solve it.



In other words, exactly as we’ve been saying :slight_smile:

Very sorry I got so frustrated, I’m not really great with internet forums, I’ve had a lot of bad experiences with people changing the history of a discussion to make it look like I said something terrible when I did not. I realize you’re a moderator and here to help, but it spooked me when Coursera asked me to accept the honor code again. I was like, what!?!?!? I totally solved this before sending you the code, and you’re a moderator who offered to look into the autograder bug, not another student I’m helping. What am I missing? Anyhow, hopefully it’s clear at this point that

  1. I solved this some time back

and

  1. we’re working on figuring out what to do about a situation where the code returns a .999999999999999 and the assert asks for a 1

Thanks,
Steven

1 Like

Being extremely annoying and easily annoyed and generally out of touch on internet forums can happen to anyone, but happens easily to me because I’m 42 and completely missed the era of online learning while going to formal universities. I was a lab instructor for physics at my second and third grad schools, but it didn’t generally have an online component at that time (final degree December 2017). I’ve had a lot of false starts trying to take certificate courses since then, because auto-graders and forums like these are just a struggle for these, particularly with my background coming in as mid-career but from a different field, and particularly with my inexperience using training methods like these. I’ve been very successful learning in a long career in grad school and research by challenging myself to do things my own way even if it sometimes involves looking things up or extra effort, but I’m not totally sure when that’s okay to do here and when that’s just going to break the software or be an honor code violation (why would that be an honor code violation? I don’t know. But education is changing I guess).

Thanks for your patience with me.

Steven