Removed extra numpy conversion and test for sf<0

All functions assume at least 1D numpy array (so no conversion)
and output provided.

Hazard calculation enforce sf >= 0 as some estimation methods might
provide negative survival function.
parent 9a7452b1
v1.2.6, ??
- Removed unnecessary type conversion
v1.2.5, 6/8/2014
- Using 'out' rather than 'output' for in-place operations to conform to numpy convention
- Added a kernels.Kernel base-class implementing cdf, pm1, pm2, fft and dct in term of the pdf
......
......@@ -155,6 +155,7 @@ class KDE1D(object):
else:
self._total_weights = len(self._xdata)
self.method.fit(self)
self._fitted = True
@property
def xdata(self):
......@@ -335,6 +336,7 @@ class KDE1D(object):
def covariance_function(self):
return self._cov_fct
@numpy_method_idx
def pdf(self, points, out=None):
"""
Compute the PDF of the distribution on the set of points ``points``
......@@ -348,7 +350,6 @@ class KDE1D(object):
"""
return self.pdf(points, out)
@numpy_method_idx
def __call__(self, points, out=None):
"""
This method is an alias for :py:meth:`BoundedKDE1D.evaluate`
......
This diff is collapsed.
......@@ -24,19 +24,22 @@ class TestCDF(kde_utils.KDETester):
def same_numeric(self, i, method):
k = self.createKDE(self.vs[i], method)
ys = k.cdf(self.xs)
ys2 = k.method.numeric_cdf(k, self.xs)
ys2 = np.empty(self.xs.shape, dtype=float)
k.method.numeric_cdf(k, self.xs, ys2)
np.testing.assert_allclose(ys, ys2, method.accuracy, method.accuracy)
def grid_same_numeric(self, i, method):
k = self.createKDE(self.vs[i], method)
xs, ys = k.cdf_grid()
ys2 = k.method.numeric_cdf(k, xs)
ys2 = np.empty(self.xs.shape, dtype=float)
k.method.numeric_cdf(k, xs, out=ys2)
np.testing.assert_allclose(ys, ys2, method.grid_accuracy, method.grid_accuracy)
def numeric_cdf(self, i, method):
k = self.createKDE(self.vs[i], method)
k.fit()
ys = k.method.numeric_cdf(k, self.xs)
ys = np.empty(self.xs.shape, dtype=float)
k.method.numeric_cdf(k, self.xs, out=ys)
xxs, yys = k.method.numeric_cdf_grid(k, N=2**12)
ys2 = np.interp(self.xs, xxs, yys)
np.testing.assert_allclose(ys, ys2, 100*method.accuracy, 100*method.accuracy)
......
......@@ -229,13 +229,16 @@ class TestHazard(kde_utils.KDETester):
h_comp = k.hazard(xs)
sf = k.sf(xs)
h_ref = k.pdf(xs)
h_ref /= k.sf(xs)
sf = k.sf(xs)
sf[sf < 0] = 0 # Some methods can produce negative sf
h_ref /= sf
sel = sf > np.sqrt(method.accuracy)
np.testing.assert_allclose(h_comp[sel], h_ref[sel], method.accuracy, method.accuracy)
def grid_method_works(self, k, method, name):
xs, h_comp = k.hazard_grid()
xs, sf = k.sf_grid()
sf[sf < 0] = 0 # Some methods can produce negative sf
h_ref = k.grid()[1]
h_ref /= sf
sel = sf > np.sqrt(method.accuracy)
......@@ -267,6 +270,7 @@ class TestCumHazard(kde_utils.KDETester):
xs = kde_methods.generate_grid(k)
h_comp = k.cumhazard(xs)
sf = k.sf(xs)
sf[sf < 0] = 0 # Some methods can produce negative sf
h_ref = -np.log(sf)
sel = sf > np.sqrt(method.accuracy)
np.testing.assert_allclose(h_comp[sel], h_ref[sel], method.accuracy, method.accuracy)
......@@ -274,6 +278,7 @@ class TestCumHazard(kde_utils.KDETester):
def grid_method_works(self, k, method, name):
xs, h_comp = k.cumhazard_grid()
xs, sf = k.sf_grid()
sf[sf < 0] = 0 # Some methods can produce negative sf
h_ref = -np.log(sf)
sel = sf > np.sqrt(method.accuracy)
# Only tests for sf big enough or error is too large
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment