Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZeroDivisionError #5

warner121 opened this issue Feb 26, 2013 · 5 comments

ZeroDivisionError #5

warner121 opened this issue Feb 26, 2013 · 5 comments


Copy link

Hi sublee,

I have encountered a problem with the calculation of some ratings (admittedly with some rather unusual parameter settings):

transform_ratings([Rating(mu=-323.263, sigma=2.965), Rating(mu=-48.441, sigma=2.190)], ranks = [0, 1])
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.7/site-packages/trueskill/", line 602, in transform_ratings
return _g().transform_ratings(rating_groups, ranks, min_delta)
File "/usr/lib/python2.7/site-packages/trueskill/", line 531, in transform_ratings
return self.rate(rating_groups, ranks, min_delta=min_delta)
File "/usr/lib/python2.7/site-packages/trueskill/", line 389, in rate
File "/usr/lib/python2.7/site-packages/trueskill/", line 309, in run_schedule
delta = trunc_layer[0].up()
File "/usr/lib/python2.7/site-packages/trueskill/", line 193, in up
v = self.v_func(_args)
File "/usr/lib/python2.7/site-packages/trueskill/", line 45, in v_win
return pdf(x) / cdf(x)
ZeroDivisionError: float division by zero

Copy link

sublee commented Feb 27, 2013


I realized the error but I couldn't find a solution to fix it. In this case, both cdf and pdf in the v_win function returns 0.0 because the real value is too low. Python can't guarantee an enough precision.

However, another implementations make different result. See the below:

Probably the expectation is Microsoft's. Both results have similar sigma but the C# implementation couldn't calculate correct mu. I'll try to refer to another implementations but Microsoft didn't open the source code. I can patch to calculate only correct sigma but mu.

ps. Your ratings are unexpectedly too low. Are they really valid?

sublee added a commit that referenced this issue Feb 28, 2013
Copy link

sublee commented Feb 28, 2013


I just committed 1d3208a to fix a half of the problem. Now my implementation returns N(NaN, 2.683) and N(NaN, 2.080) instead of ZeroDivisionError raising just like the C# implementation. But we know, it's not an eventual expectation.

Copy link

Great - thanks for looking into this issue. I came across the error while evaluating the performance of different parameters in my project, and was aware these values were pretty extreme. I think the fix you have implemented is neater than the zero division. Thanks!

@sublee sublee reopened this Mar 3, 2013
Copy link

sublee commented Mar 3, 2013

I changed my mind. ZeroDivisionError is better than NaN. If the rate() function transforms a rating to NaN silently, your program will save it to the database without a doubt. Then you can't recover meaningful rating.

I reopened this issue. I'm still finding a solution.

sublee added a commit that referenced this issue Mar 4, 2013
Available stats_implements:
- None (default)
- scipy
- mpmath -- it can fix the issue #5
Copy link

sublee commented Mar 5, 2013

I added backend option to TrueSkill class to choose cdf, pdf, ppf implementation. There are 3 backends; None (internal implementation), 'scipy', 'mpmath'. If you choose 'mpmath' backend, this problem will be disappeared. Of course you have to install mpmath first.

>>> from trueskill import *
>>> rate_1vs1(Rating(mu=-323.263, sigma=2.965), Rating(mu=-48.441, sigma=2.190))Traceback (most recent call last):  
  File "<stdin>", line 1, in <module>  
  File "trueskill/", line 622, in rate_1vs1  
    return _g().rate_1vs1(rating1, rating2, drawn, min_delta)  
  File "trueskill/", line 504, in rate_1vs1  
    teams = self.rate([(rating1,), (rating2,)], ranks, min_delta=min_delta)  
  File "trueskill/", line 416, in rate  
  File "trueskill/", line 332, in run_schedule  
    delta = trunc_layer[0].up()  
  File "trueskill/", line 196, in up  
    w = self.w_func(*args)  
  File "trueskill/", line 164, in w_win  
    raise FloatingPointError('Cannot calculate correctly, '  
FloatingPointError: Cannot calculate correctly, set backend to 'mpmath'
>>> setup(backend='mpmath')
<TrueSkill mu=25.000 sigma=8.333 beta=4.167 tau=0.083 draw_probability=10.0% backend='mpmath'>
>>> rate_1vs1(Rating(mu=-323.263, sigma=2.965), Rating(mu=-48.441, sigma=2.190))
(Rating(mu=-273.060, sigma=2.683), Rating(mu=-75.848, sigma=2.080))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

No branches or pull requests

2 participants