-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add scipycompat for compatibility with non-scipy
- support Python 2.5 - add tox.ini for testing with Tox
- Loading branch information
Heungsub Lee
committed
Nov 13, 2012
1 parent
4a7d0f8
commit 80f8875
Showing
5 changed files
with
98 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[tox] | ||
envlist = py25, py26, py27, pypy | ||
|
||
[testenv] | ||
deps = Attest | ||
commands = python -m attest.run trueskilltests.suite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
trueskill.scipycompat | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
A fallback of `scipy`_. | ||
.. _scipy:: http://www.scipy.org/ | ||
:copyright: (c) 2012 by Heungsub Lee. | ||
:license: BSD, see LICENSE for more details. | ||
""" | ||
import math | ||
|
||
|
||
__all__ = ['cdf', 'pdf', 'ppf'] | ||
|
||
|
||
def erfcc(x): | ||
"""Complementary error function (via http://bit.ly/zOLqbc)""" | ||
z = abs(x) | ||
t = 1. / (1. + z / 2.) | ||
r = t * math.exp(-z * z - 1.26551223 + t * (1.00002368 + t * ( | ||
0.37409196 + t * (0.09678418 + t * ( | ||
-0.18628806 + t * (0.27886807 + t * ( | ||
-1.13520398 + t * (1.48851587 + t * ( | ||
-0.82215223 + t * 0.17087277))))))))) | ||
return 2. - r if x < 0 else r | ||
|
||
|
||
def ierfcc(y): | ||
"""The inverse function of erfcc""" | ||
if y >= 2: | ||
return -100 | ||
elif y <= 0: | ||
return 100 | ||
zero_point = y < 1 | ||
if not zero_point: | ||
y = 2 - y | ||
t = math.sqrt(-2 * math.log(y / 2.)) | ||
x = -0.70711 * \ | ||
((2.30753 + t * 0.27061) / (1. + t * (0.99229 + t * 0.04481)) - t) | ||
for i in xrange(2): | ||
err = erfcc(x) - y | ||
x += err / (1.12837916709551257 * math.exp(-(x ** 2)) - x * err) | ||
return x if zero_point else -x | ||
|
||
|
||
def cdf(x, mu=0, sigma=1): | ||
"""Cumulative distribution function""" | ||
return 0.5 * erfcc(-(x - mu) / (sigma * math.sqrt(2))) | ||
|
||
|
||
def pdf(x, mu=0, sigma=1): | ||
"""Probability density function""" | ||
return (1 / math.sqrt(2 * math.pi) * abs(sigma)) * \ | ||
math.exp(-(((x - mu) / abs(sigma)) ** 2 / 2)) | ||
|
||
|
||
def ppf(x, mu=0, sigma=1): | ||
"""The inverse function of CDF""" | ||
return mu - sigma * math.sqrt(2) * ierfcc(2 * x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters