def test_GaussianBase_agrees_with_scipy(): data = np.random.random(size=100) gb = testmod.GaussianBase(data) kernel = stats.kde.gaussian_kde(data, bw_method="scott") pts = np.random.random(size=50) np.testing.assert_allclose(gb(pts), kernel(pts)) gb = testmod.GaussianBase(data) gb.bandwidth = "silverman" kernel = stats.kde.gaussian_kde(data, bw_method="silverman") np.testing.assert_allclose(gb(pts), kernel(pts))
def test_GaussianBase_set_covariance(): gb = testmod.GaussianBase([1,2,3,4]) with pytest.raises(ValueError): gb.covariance_matrix = [[1,2,3], [2,3,4]] with pytest.raises(ValueError): gb.covariance_matrix = [[1,2], [3,4]] gb.covariance_matrix = 1 gb = testmod.GaussianBase([[1,2,3,4], [4,2,2,1]]) with pytest.raises(ValueError): gb.covariance_matrix = [[1,2,3], [2,3,4]] gb.covariance_matrix = [[2,2], [3,4]] with pytest.raises(ValueError): gb.covariance_matrix = [[1,2], [3,4]] with pytest.raises(ValueError): gb.covariance_matrix = 1
def initial_model(self, T, data): bk = kernels.GaussianBase(data[1:, :]) return KDEModel(T=T, mu=data.shape[-1] / T, theta=self._theta, background_kernel=bk, time_kernel=self._f, space_kernel=self._g)
def func_opt(self): times, probs = self._times_probs_for_func_opt() ker = kernels.GaussianBase(times) ker.covariance_matrix = 1 ker.bandwidth = self._h ker.weights = probs return kernels.Reflect1D(ker)
def test_GaussianBase_set_band(): gb = testmod.GaussianBase([1,2,3,4]) assert gb.bandwidth == pytest.approx(4 ** (-1/5)) gb.bandwidth = "scott" assert gb.bandwidth == pytest.approx(4 ** (-1/5)) with pytest.raises(ValueError): gb.bandwidth = "matt" gb.bandwidth = "silverman" assert gb.bandwidth == pytest.approx(3 ** (-1/5)) gb = testmod.GaussianBase([[1,2,3,4],[4,2,1,3]]) assert gb.bandwidth == pytest.approx(4 ** (-1/6)) gb.bandwidth = "scott" assert gb.bandwidth == pytest.approx(4 ** (-1/6)) with pytest.raises(ValueError): gb.bandwidth = "matt" gb.bandwidth = "silverman" assert gb.bandwidth == pytest.approx(4 ** (-1/6))
def initial_model(self, T, data): bk = kernels.GaussianBase(data[1:,:]) def tk(pts): pts = _np.asarray(pts) p = _np.exp(-self._initial_time_scale * pts[0]) * self._initial_time_scale rr = pts[1]**2 + pts[2]**2 bwsq = self._initial_space_scale * self._initial_space_scale return p * _np.exp(-rr / (2 * bwsq)) / (2 * _np.pi * bwsq) return Model(T=T, mu=data.shape[-1] / T, theta=0.5, background_kernel=bk, trigger_kernel=tk)
def __call__(self, pts, weights): pts = _np.asarray(pts) if weights is not None: weights = _np.asarray(weights) m = (weights > 0) pts = pts[..., m] weights = weights[m] ker = kernels.GaussianBase(pts) ker.weights = weights if self._cutoff is None: return ker return _WrapWithCutoff(ker, self._cutoff)
def test_GaussianBase_large_eval(): n = 1000000 pts = np.arange(n) / n gb = testmod.GaussianBase(pts) gb.covariance_matrix = 1.0 gb.bandwidth = 1.0 x5 = np.sum(np.exp(-(5 - pts)**2 / 2)) / n / sqrt2pi assert gb(5) == pytest.approx(x5) x3 = np.sum(np.exp(-(3 - pts)**2 / 2)) / n / sqrt2pi assert gb(3) == pytest.approx(x3) np.testing.assert_allclose(gb([5,3]), [x5,x3])
def test_marginalise_gaussian_kernel(): pts = np.random.random((3,20)) ker = testmod.GaussianBase(pts) ker.covariance_matrix = np.diag([2,3,4]) ker.bandwidth = 1.4 check_marginal_kernel(ker, 0) ker = testmod.GaussianBase(pts) ker.covariance_matrix = np.diag([2,3,4]) ker.bandwidth = 1.4 ker.weights = np.random.random(20) check_marginal_kernel(ker, 0) ker = testmod.GaussianBase(pts) ker.covariance_matrix = np.diag([2,3,4]) ker.bandwidth = np.random.random(20) ker.weights = np.random.random(20) check_marginal_kernel(ker, 0) ker = testmod.GaussianBase(pts) ker.covariance_matrix = np.diag([2,3,4]) check_marginal_kernel(ker, 1)
def test_GaussianBase_eval_with_cov(): gb = testmod.GaussianBase([1,2,3,4]) assert gb.covariance_matrix[0,0] == pytest.approx(20/12) gb.covariance_matrix = 0.5 gb.bandwidth = 1.0 x5 = np.sum(np.exp([-16, -9, -4, -1])) / 4 / np.sqrt(0.5) / sqrt2pi assert gb(5) == pytest.approx(x5) x2 = np.sum(np.exp([-1, 0, -1, -4])) / 4 / np.sqrt(0.5) / sqrt2pi assert gb(2) == pytest.approx(x2) x0 = np.sum(np.exp([-1, -4, -9, -16])) / 4 / np.sqrt(0.5) / sqrt2pi assert gb(0) == pytest.approx(x0) np.testing.assert_allclose(gb([0]), [x0]) np.testing.assert_allclose(gb([0,2,5,2,5,0]), [x0,x2,x5,x2,x5,x0])
def test_GaussianBase_eval_with_bandwidth(): gb = testmod.GaussianBase([1,2,3,4]) assert gb.covariance_matrix[0,0] == pytest.approx(20/12) gb.covariance_matrix = 1.0 gb.bandwidth = 2.0 x5 = np.sum(np.exp([-16/8, -9/8, -4/8, -1/8])) / 8 / sqrt2pi assert gb(5) == pytest.approx(x5) x2 = np.sum(np.exp([-1/8, 0, -1/8, -4/8])) / 8 / sqrt2pi assert gb(2) == pytest.approx(x2) x0 = np.sum(np.exp([-1/8, -4/8, -9/8, -16/8])) / 8 / sqrt2pi assert gb(0) == pytest.approx(x0) np.testing.assert_allclose(gb([0]), [x0]) np.testing.assert_allclose(gb([0,2,5,2,5,0]), [x0,x2,x5,x2,x5,x0])
def test_GaussianBase_set_weights(): gb = testmod.GaussianBase([1,2,3,4]) assert gb.weights is None gb.weights = [.2, 0, 5, 2] with pytest.raises(ValueError): gb.weights = [.2, 0, 5] with pytest.raises(ValueError): gb.weights = 2 with pytest.raises(ValueError): gb.weights = [[1,2,3],[4,5,6]]
def test_GaussianBase_eval_with_weights(): gb = testmod.GaussianBase([1,2,3,4]) assert gb.covariance_matrix[0,0] == pytest.approx(20/12) gb.covariance_matrix = 1.0 gb.bandwidth = 1.0 gb.weights = [0,1,20,30] x5 = np.sum(np.exp([-16/2, -9/2, -4/2, -1/2]) * [0,1,20,30]) / 51 / sqrt2pi assert gb(5) == pytest.approx(x5) x2 = np.sum(np.exp([-1/2, 0, -1/2, -4/2]) * [0,1,20,30]) / 51 / sqrt2pi assert gb(2) == pytest.approx(x2) x0 = np.sum(np.exp([-1/2, -4/2, -9/2, -16/2]) * [0,1,20,30]) / 51 / sqrt2pi assert gb(0) == pytest.approx(x0) np.testing.assert_allclose(gb([0]), [x0]) np.testing.assert_allclose(gb([0,2,5,2,5,0]), [x0,x2,x5,x2,x5,x0])
def test_GaussianBase_large_eval_3d(): n = 1000000 pts = np.random.random((3,n)) * 100 gb = testmod.GaussianBase(pts) gb.covariance_matrix = np.eye(3) gb.bandwidth = 1.0 pt = np.asarray([1,2,3]) x = np.sum(np.exp(-np.sum((pts - pt[:,None])**2,axis=0) / 2)) / n / (sqrt2pi**3) assert gb([1,2,3]) == pytest.approx(x) pt = np.asarray([4,2,1]) y = np.sum(np.exp(-np.sum((pts - pt[:,None])**2,axis=0) / 2)) / n / (sqrt2pi**3) assert gb([4,2,1]) == pytest.approx(y) np.testing.assert_allclose(gb([[1,4], [2,2], [3,1]]), [x,y])
def test_GaussianBase_eval_2d(): gb = testmod.GaussianBase([[1,2,3,4],[1,3,7,5]]) gb.covariance_matrix = [[1,0],[0,1]] gb.bandwidth = 1.0 with pytest.raises(ValueError): gb(5) with pytest.raises(ValueError): gb([1,2,3]) x0 = np.sum(np.exp([-1/2, -2/2, -29/2, -18/2])) / 4 / sqrt2pi / sqrt2pi assert gb([1,2]) == pytest.approx(x0) gb.bandwidth = 2.0 x0 = np.sum(np.exp([-1/2/4, -2/2/4, -29/2/4, -18/2/4])) / 4 / 4 / sqrt2pi / sqrt2pi assert gb([1,2]) == pytest.approx(x0)
def test_GaussianBase_eval_with_bandwidths(): gb = testmod.GaussianBase([1,2,3,4]) assert gb.covariance_matrix[0,0] == pytest.approx(20/12) gb.covariance_matrix = 1.0 gb.bandwidth = [0.5, 0.1, 0.7, 5] x5 = np.sum(np.exp([-16/2/(0.5**2), -9/2/(0.1**2), -4/2/(0.7**2), -1/2/(5**2)]) / [0.5, 0.1, 0.7, 5] ) / 4 / sqrt2pi assert gb(5) == pytest.approx(x5) x3 = np.sum(np.exp([-4/2/(0.5**2), -1/2/(0.1**2), 0, -1/2/(5**2)]) / [0.5, 0.1, 0.7, 5] ) / 4 / sqrt2pi assert gb(3) == pytest.approx(x3) np.testing.assert_allclose(gb([3,5,3]), [x3,x5,x3]) with pytest.raises(ValueError): gb.bandwidth = [[0.5, 0.1], [0.7, 5]]
def initial_model(self, T, data): bk = kernels.GaussianBase(data[1:,:]) bk.covariance_matrix = _np.eye(2) bk.bandwidth = 50 def tk_time(t): # Is it okay that these are hard-wired?? return _np.exp(-self._initial_time_scale * _np.asarray(t)) * self._initial_time_scale #return 0.1 * _np.exp(-0.1 * t) def tk_space(pts): pts = _np.asarray(pts) rr = _np.sqrt(pts[0]**2 + pts[1]**2) bw = 1 / self._initial_space_scale return bw * bw * _np.exp(-bw * rr / 2) / (2 * _np.pi) return Model1(T=T, mu=data.shape[-1] / T, theta=0.5, background_kernel=bk, trigger_time_kernel=tk_time, trigger_space_kernel=tk_space)
def __call__(self, pts, weights): pts = _np.asarray(pts) if weights is not None: weights = _np.asarray(weights) m = (weights > 0) pts = pts[..., m] weights = weights[m] ker = kernels.GaussianBase(pts) ker.bandwidth = self._h if self._scale is None: ker.covariance_matrix = _np.eye(ker.dimension) else: ker.covariance_matrix = _np.diag(_np.atleast_1d(self._scale)) ker.weights = weights if self._cutoff is None: return ker return _WrapWithCutoff(ker, self._cutoff)
def test_GaussianEdgeCorrect_halfS(gec1): gb = testmod.GaussianBase(gec1.data) hS = scipy.linalg.inv(gb.covariance_matrix) hS = scipy.linalg.fractional_matrix_power(hS, 0.5) np.testing.assert_allclose(gec1.half_S, hS)
def test_GaussianBase_not_point(): with pytest.raises(ValueError): testmod.GaussianBase(5.2)
def test_GaussianEdgeCorrect_agrees_with_GaussianBase(gec1): gb = testmod.GaussianBase(gec1.data) pts = np.random.random((2,10)) * np.asarray([10,10])[:,None] np.testing.assert_allclose(gb(pts), gec1(pts))