提交 356b55f2 authored 作者: Joseph Paul Cohen's avatar Joseph Paul Cohen

added more test cases and more arguments to cov

上级 7a1e9ec4
......@@ -2168,27 +2168,66 @@ def sqr(a):
square = sqr
def cov(a):
def cov(X, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None):
"""Calculate the covariance matrix.
Covariance indicates the level to which two variables vary together.
If we examine N-dimensional samples, :math:`X = [x_1, x_2, ... x_N]^T`,
then the covariance matrix element :math:`C_{ij}` is the covariance of
:math:`x_i` and :math:`x_j`. The element :math:`C_{ii}` is the variance
of :math:`x_i`.
of :math:`x_i`. Code and docstring ported from numpy.
----------
a : array_like
A 2-D array containing multiple variables and observations.
Each row of `a` represents a variable, and each column is
observations of all those variables.
Each row of `a` represents a variable, and each column is
observations of all those variables.
y : array_like, optional
An additional set of variables and observations. `y` has the same form
as that of `m`.
rowvar : bool, optional
If `rowvar` is True (default), then each row represents a
variable, with observations in the columns. Otherwise, the relationship
is transposed: each column represents a variable, while the rows
contain observations.
bias : bool, optional
Default normalization (False) is by ``(N - 1)``, where ``N`` is the
number of observations given (unbiased estimate). If `bias` is True, then
normalization is by ``N``. These values can be overridden by using the
keyword ``ddof``.
ddof : int, optional
If not ``None`` the default value implied by `bias` is overridden.
The default value is ``None``.
Returns
-------
out : The covariance matrix of the variables.
"""
a -= a.mean(axis=1, keepdims=1)
c = a.dot(a.T)
return c/(a.shape[1]-1)
if fweights is not None:
raise NotImplementedError('fweights are not implemented')
if aweights is not None:
raise NotImplementedError('aweights are not implemented')
if not rowvar and X.shape[0] != 1:
X = X.T
if y is not None:
if not rowvar and y.shape[0] != 1:
y = y.T
X = theano.tensor.concatenate((X, y), axis=0)
if ddof is None:
if not bias:
ddof = 1
else:
ddof = 0
# Determine the normalization
fact = X.shape[1] - ddof
X -= X.mean(axis=1, keepdims=1)
c = X.dot(X.T)
c *= theano.tensor.constant(1) / fact
return c.squeeze()
@_scal_elemwise
def sqrt(a):
......
......@@ -8171,25 +8171,107 @@ def test_norm():
f = theano.function([x], n)
assert np.allclose(f([1, 1]), np.sqrt(2))
def test_cov():
x = theano.tensor.matrix('x')
c = theano.tensor.cov(x)
f = theano.function([x], c)
data = np.asarray(np.random.rand(3,5),dtype=config.floatX)
assert np.allclose(f(data), np.cov(data))
data = np.asarray(np.random.rand(5,3),dtype=config.floatX)
assert np.allclose(f(data), np.cov(data))
data = np.asarray(np.random.rand(10,10),dtype=config.floatX)
assert np.allclose(f(data), np.cov(data))
data = np.asarray(np.random.rand(2,2),dtype=config.floatX)
assert np.allclose(f(data), np.cov(data))
data = np.asarray(np.random.rand(1,2),dtype=config.floatX)
assert np.allclose(f(data), np.cov(data))
class test_cov(unittest.TestCase):
def test_core(self):
x = theano.tensor.matrix('x')
c = theano.tensor.cov(x)
f = theano.function([x], c)
# basic cov function
data = np.asarray(np.random.rand(3, 5), dtype=config.floatX)
assert np.allclose(f(data), np.cov(data))
data = np.asarray(np.random.rand(5, 3), dtype=config.floatX)
assert np.allclose(f(data), np.cov(data))
data = np.asarray(np.random.rand(10, 10), dtype=config.floatX)
assert np.allclose(f(data), np.cov(data))
data = np.asarray(np.random.rand(2, 2), dtype=config.floatX)
assert np.allclose(f(data), np.cov(data))
data = np.asarray(np.random.rand(1, 2), dtype=config.floatX)
assert np.allclose(f(data), np.cov(data))
def test_rowvar(self):
for rowvar in [True, False]:
x = theano.tensor.matrix('x')
c = theano.tensor.cov(x, rowvar=rowvar)
f = theano.function([x], c)
data = np.asarray(np.random.rand(3, 5), dtype=config.floatX)
assert np.allclose(f(data), np.cov(data, rowvar=rowvar))
data = np.asarray(np.random.rand(5, 3), dtype=config.floatX)
assert np.allclose(f(data), np.cov(data, rowvar=rowvar))
data = np.asarray(np.random.rand(10, 10), dtype=config.floatX)
assert np.allclose(f(data), np.cov(data, rowvar=rowvar))
data = np.asarray(np.random.rand(2, 2), dtype=config.floatX)
assert np.allclose(f(data), np.cov(data, rowvar=rowvar))
# check when variables are along the first axis
x = theano.tensor.matrix('x')
c = theano.tensor.cov(x, rowvar=False)
f = theano.function([x], c)
data = np.asarray(np.random.rand(2, 1), dtype=config.floatX)
assert np.allclose(f(data), np.cov(data, rowvar=False))
def test_y(self):
# test y
x = theano.tensor.matrix('x')
y = theano.tensor.matrix('y')
c = theano.tensor.cov(x, y=y)
f = theano.function([x, y], c)
data = np.asarray(np.random.rand(3, 5), dtype=config.floatX)
y = np.asarray(np.random.rand(3, 5), dtype=config.floatX)
assert np.allclose(f(data, y), np.cov(data, y=y))
data = np.asarray(np.random.rand(5, 3), dtype=config.floatX)
y = np.asarray(np.random.rand(5, 3), dtype=config.floatX)
assert np.allclose(f(data, y), np.cov(data, y=y))
data = np.asarray(np.random.rand(10, 10), dtype=config.floatX)
y = np.asarray(np.random.rand(10, 10), dtype=config.floatX)
assert np.allclose(f(data, y), np.cov(data, y=y))
data = np.asarray(np.random.rand(2, 2), dtype=config.floatX)
y = np.asarray(np.random.rand(2, 2), dtype=config.floatX)
assert np.allclose(f(data, y), np.cov(data, y=y))
def test_ddof(self):
for ddof in range(0, 5):
x = theano.tensor.matrix('x')
c = theano.tensor.cov(x, ddof=ddof)
f = theano.function([x], c)
data = np.asarray(np.random.rand(3, 5), dtype=config.floatX)
assert np.allclose(f(data), np.cov(data, ddof=ddof))
def test_bias(self):
for bias in [True, False]:
x = theano.tensor.matrix('x')
c = theano.tensor.cov(x, bias=bias)
f = theano.function([x], c)
data = np.asarray(np.random.rand(3, 5), dtype=config.floatX)
assert np.allclose(f(data), np.cov(data, bias=bias))
for ddof in range(0, 5):
for bias in [True, False]:
x = theano.tensor.matrix('x')
c = theano.tensor.cov(x, ddof=ddof, bias=bias)
f = theano.function([x], c)
data = np.asarray(np.random.rand(3, 5), dtype=config.floatX)
assert np.allclose(f(data), np.cov(data, ddof=ddof, bias=bias))
class test_ptp(unittest.TestCase):
def test_scalar(self):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论