mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-05-15 03:53:41 +02:00
Added tests when changing cov and grad in covobs
This commit is contained in:
parent
3190140023
commit
7c5465d828
1 changed files with 34 additions and 18 deletions
|
@ -20,19 +20,7 @@ class Covobs:
|
||||||
grad : list or array
|
grad : list or array
|
||||||
Gradient of the Covobs wrt. the means belonging to cov.
|
Gradient of the Covobs wrt. the means belonging to cov.
|
||||||
"""
|
"""
|
||||||
self.cov = np.array(cov)
|
self.set_cov(cov)
|
||||||
if self.cov.ndim == 0:
|
|
||||||
self.N = 1
|
|
||||||
self.cov = np.diag([self.cov])
|
|
||||||
elif self.cov.ndim == 1:
|
|
||||||
self.N = len(self.cov)
|
|
||||||
self.cov = np.diag(self.cov)
|
|
||||||
elif self.cov.ndim == 2:
|
|
||||||
self.N = self.cov.shape[0]
|
|
||||||
if self.cov.shape[1] != self.N:
|
|
||||||
raise Exception('Covariance matrix has to be a square matrix!')
|
|
||||||
else:
|
|
||||||
raise Exception('Covariance matrix has to be a 2 dimensional square matrix!')
|
|
||||||
if '|' in name:
|
if '|' in name:
|
||||||
raise Exception("Covobs name must not contain replica separator '|'.")
|
raise Exception("Covobs name must not contain replica separator '|'.")
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -45,15 +33,43 @@ class Covobs:
|
||||||
else:
|
else:
|
||||||
if pos > self.N:
|
if pos > self.N:
|
||||||
raise Exception('pos %d too large for covariance matrix with dimension %dx%d!' % (pos, self.N, self.N))
|
raise Exception('pos %d too large for covariance matrix with dimension %dx%d!' % (pos, self.N, self.N))
|
||||||
self.grad = np.zeros((self.N, 1))
|
self._grad = np.zeros((self.N, 1))
|
||||||
self.grad[pos] = 1.
|
self._grad[pos] = 1.
|
||||||
else:
|
else:
|
||||||
self.grad = np.array(grad)
|
self.set_grad(grad)
|
||||||
if self.grad.ndim == 1:
|
|
||||||
self.grad = np.reshape(self.grad, (self.N, 1))
|
|
||||||
self.value = mean
|
self.value = mean
|
||||||
|
|
||||||
def errsq(self):
|
def errsq(self):
|
||||||
""" Return the variance (= square of the error) of the Covobs
|
""" Return the variance (= square of the error) of the Covobs
|
||||||
"""
|
"""
|
||||||
return float(np.dot(np.transpose(self.grad), np.dot(self.cov, self.grad)))
|
return float(np.dot(np.transpose(self.grad), np.dot(self.cov, self.grad)))
|
||||||
|
|
||||||
|
def set_cov(self, cov):
|
||||||
|
self._cov = np.array(cov)
|
||||||
|
if self._cov.ndim == 0:
|
||||||
|
self.N = 1
|
||||||
|
self._cov = np.diag([self._cov])
|
||||||
|
elif self._cov.ndim == 1:
|
||||||
|
self.N = len(self._cov)
|
||||||
|
self._cov = np.diag(self._cov)
|
||||||
|
elif self._cov.ndim == 2:
|
||||||
|
self.N = self._cov.shape[0]
|
||||||
|
if self._cov.shape[1] != self.N:
|
||||||
|
raise Exception('Covariance matrix has to be a square matrix!')
|
||||||
|
else:
|
||||||
|
raise Exception('Covariance matrix has to be a 2 dimensional square matrix!')
|
||||||
|
|
||||||
|
def set_grad(self, grad):
|
||||||
|
self._grad = np.array(grad)
|
||||||
|
if self._grad.ndim in [0, 1]:
|
||||||
|
self._grad = np.reshape(self._grad, (self.N, 1))
|
||||||
|
elif self._grad.ndim != 2:
|
||||||
|
raise Exception('Invalid dimension of grad!')
|
||||||
|
|
||||||
|
@property
|
||||||
|
def cov(self):
|
||||||
|
return self._cov
|
||||||
|
|
||||||
|
@property
|
||||||
|
def grad(self):
|
||||||
|
return self._grad
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue