Alex Rivera | Logout

How to do Unit Testing with Uncertainties?

Asked 2009-01-14T13:46:17.500
16

We have several different optimization algorithms that produce a different result for each run. For example the goal of the optimization could be to find the minimum of a function, where 0 is the global minima. The optimization runs returns data like this:

[0.1, 0.1321, 0.0921, 0.012, 0.4]

Which is quite close to the global minima, so this is ok. Our first approach was to just choose a threshold, and let the unit test fail if a result occured that was too high. Unfortunately, this does not work at all: The results seem to have a gauss distribution, so, although unlikely, from time to time the test failed even when the algorithm is still fine and we just had bad luck.

So, how can I test this properly? I think quite a bit of statistics are needed here. It is also important that tests are still fast, just letting the test run a few 100 times and then take the average will be too slow.

Here are some further clarifications:

  • For example I have an algorithm that fits a Circle into a set of points. It is extremly fast but does not always produce the same result. I want to write a Unit test to guarantee that in most cases it is good enough.

  • Unfortunately I cannot choose a fixed seed for the random number generator, because I do not want to test if the algorithm produces the exact same result as before, but I want to test something like "With 90% certainty I get a result with 0.1 or better".

Edit
Report

2 Answers

1

I would suggest that, rather than having your test run against the code producing the gaussian distribution, you create a Monte Carlo-type algorithm that runs the Method many times and then test the overall distribution of results using the appropriate distribution model. If it is an average, for example, then you will be able to test against a firm threshold. If it is more complex, you'll need to create code that models the appropriate distribution (e.g. do values < x make up y% of my results).

Keep in mind that you aren't testing the number generator, you are testing the Unit that generates the values!

answered 2009-01-14T14:27:13.500
1

Thanks for all the answers, I am now doing this:

  1. Run the test 5 times and take the median result.
  2. If the median result is below a certain threshold, the test succeeds.
  3. If the threshold fails, test again until either the threshold is met (test succeeds) OR until I have done so many iterations (around 100 or so) that I can be pretty sure that the median does not get below the threshold any more.

This way whenever a test looks like it is going to fail, it is recalculated so often until it is pretty sure that it really has failed.

This seems to work, but I am not quite satisfied because I am only testing the median result.

answered 2009-01-15T07:36:13.160

Your Answer