pyerrors.covobs

View Source
  0import numpy as np
  1
  2
  3class Covobs:
  4
  5    def __init__(self, mean, cov, name, pos=None, grad=None):
  6        """ Initialize Covobs object.
  7
  8        Parameters
  9        ----------
 10        mean : float
 11            Mean value of the new Obs
 12        cov : list or array
 13            2d Covariance matrix or 1d diagonal entries
 14        name : str
 15            identifier for the covariance matrix
 16        pos : int
 17            Position of the variance belonging to mean in cov.
 18            Is taken to be 1 if cov is 0-dimensional
 19        grad : list or array
 20            Gradient of the Covobs wrt. the means belonging to cov.
 21        """
 22        self._set_cov(cov)
 23        if '|' in name:
 24            raise Exception("Covobs name must not contain replica separator '|'.")
 25        self.name = name
 26        if grad is None:
 27            if pos is None:
 28                if self.N == 1:
 29                    pos = 0
 30                else:
 31                    raise Exception('Have to specify position of cov-element belonging to mean!')
 32            else:
 33                if pos > self.N:
 34                    raise Exception('pos %d too large for covariance matrix with dimension %dx%d!' % (pos, self.N, self.N))
 35            self._grad = np.zeros((self.N, 1))
 36            self._grad[pos] = 1.
 37        else:
 38            self._set_grad(grad)
 39        self.value = mean
 40
 41    def errsq(self):
 42        """ Return the variance (= square of the error) of the Covobs
 43        """
 44        return float(np.dot(np.transpose(self.grad), np.dot(self.cov, self.grad)))
 45
 46    def _set_cov(self, cov):
 47        """ Set the covariance matrix of the covobs
 48
 49        Parameters
 50        ----------
 51        cov : list or array
 52            Has to be either of:
 53            0 dimensional number: variance of a single covobs,
 54            1 dimensional list or array of lenght N: variances of multiple covobs
 55            2 dimensional list or array (N x N): Symmetric, positive-semidefinite covariance matrix
 56        """
 57        self._cov = np.array(cov)
 58        if self._cov.ndim == 0:
 59            self.N = 1
 60            self._cov = np.diag([self._cov])
 61        elif self._cov.ndim == 1:
 62            self.N = len(self._cov)
 63            self._cov = np.diag(self._cov)
 64        elif self._cov.ndim == 2:
 65            self.N = self._cov.shape[0]
 66            if self._cov.shape[1] != self.N:
 67                raise Exception('Covariance matrix has to be a square matrix!')
 68        else:
 69            raise Exception('Covariance matrix has to be a 2 dimensional square matrix!')
 70
 71        for i in range(self.N):
 72            for j in range(i):
 73                if not self._cov[i][j] == self._cov[j][i]:
 74                    raise Exception('Covariance matrix is non-symmetric for (%d, %d' % (i, j))
 75
 76        evals = np.linalg.eigvalsh(self._cov)
 77        for ev in evals:
 78            if ev < 0:
 79                raise Exception('Covariance matrix is not positive-semidefinite!')
 80
 81    def _set_grad(self, grad):
 82        """ Set the gradient of the covobs
 83
 84        Parameters
 85        ----------
 86        grad : list or array
 87            Has to be either of:
 88            0 dimensional number: gradient w.r.t. a single covobs,
 89            1 dimensional list or array of lenght N: gradient w.r.t. multiple covobs
 90        """
 91        self._grad = np.array(grad)
 92        if self._grad.ndim in [0, 1]:
 93            self._grad = np.reshape(self._grad, (self.N, 1))
 94        elif self._grad.ndim != 2:
 95            raise Exception('Invalid dimension of grad!')
 96
 97    @property
 98    def cov(self):
 99        return self._cov
100
101    @property
102    def grad(self):
103        return self._grad
#   class Covobs:
View Source
  4class Covobs:
  5
  6    def __init__(self, mean, cov, name, pos=None, grad=None):
  7        """ Initialize Covobs object.
  8
  9        Parameters
 10        ----------
 11        mean : float
 12            Mean value of the new Obs
 13        cov : list or array
 14            2d Covariance matrix or 1d diagonal entries
 15        name : str
 16            identifier for the covariance matrix
 17        pos : int
 18            Position of the variance belonging to mean in cov.
 19            Is taken to be 1 if cov is 0-dimensional
 20        grad : list or array
 21            Gradient of the Covobs wrt. the means belonging to cov.
 22        """
 23        self._set_cov(cov)
 24        if '|' in name:
 25            raise Exception("Covobs name must not contain replica separator '|'.")
 26        self.name = name
 27        if grad is None:
 28            if pos is None:
 29                if self.N == 1:
 30                    pos = 0
 31                else:
 32                    raise Exception('Have to specify position of cov-element belonging to mean!')
 33            else:
 34                if pos > self.N:
 35                    raise Exception('pos %d too large for covariance matrix with dimension %dx%d!' % (pos, self.N, self.N))
 36            self._grad = np.zeros((self.N, 1))
 37            self._grad[pos] = 1.
 38        else:
 39            self._set_grad(grad)
 40        self.value = mean
 41
 42    def errsq(self):
 43        """ Return the variance (= square of the error) of the Covobs
 44        """
 45        return float(np.dot(np.transpose(self.grad), np.dot(self.cov, self.grad)))
 46
 47    def _set_cov(self, cov):
 48        """ Set the covariance matrix of the covobs
 49
 50        Parameters
 51        ----------
 52        cov : list or array
 53            Has to be either of:
 54            0 dimensional number: variance of a single covobs,
 55            1 dimensional list or array of lenght N: variances of multiple covobs
 56            2 dimensional list or array (N x N): Symmetric, positive-semidefinite covariance matrix
 57        """
 58        self._cov = np.array(cov)
 59        if self._cov.ndim == 0:
 60            self.N = 1
 61            self._cov = np.diag([self._cov])
 62        elif self._cov.ndim == 1:
 63            self.N = len(self._cov)
 64            self._cov = np.diag(self._cov)
 65        elif self._cov.ndim == 2:
 66            self.N = self._cov.shape[0]
 67            if self._cov.shape[1] != self.N:
 68                raise Exception('Covariance matrix has to be a square matrix!')
 69        else:
 70            raise Exception('Covariance matrix has to be a 2 dimensional square matrix!')
 71
 72        for i in range(self.N):
 73            for j in range(i):
 74                if not self._cov[i][j] == self._cov[j][i]:
 75                    raise Exception('Covariance matrix is non-symmetric for (%d, %d' % (i, j))
 76
 77        evals = np.linalg.eigvalsh(self._cov)
 78        for ev in evals:
 79            if ev < 0:
 80                raise Exception('Covariance matrix is not positive-semidefinite!')
 81
 82    def _set_grad(self, grad):
 83        """ Set the gradient of the covobs
 84
 85        Parameters
 86        ----------
 87        grad : list or array
 88            Has to be either of:
 89            0 dimensional number: gradient w.r.t. a single covobs,
 90            1 dimensional list or array of lenght N: gradient w.r.t. multiple covobs
 91        """
 92        self._grad = np.array(grad)
 93        if self._grad.ndim in [0, 1]:
 94            self._grad = np.reshape(self._grad, (self.N, 1))
 95        elif self._grad.ndim != 2:
 96            raise Exception('Invalid dimension of grad!')
 97
 98    @property
 99    def cov(self):
100        return self._cov
101
102    @property
103    def grad(self):
104        return self._grad
#   Covobs(mean, cov, name, pos=None, grad=None)
View Source
 6    def __init__(self, mean, cov, name, pos=None, grad=None):
 7        """ Initialize Covobs object.
 8
 9        Parameters
10        ----------
11        mean : float
12            Mean value of the new Obs
13        cov : list or array
14            2d Covariance matrix or 1d diagonal entries
15        name : str
16            identifier for the covariance matrix
17        pos : int
18            Position of the variance belonging to mean in cov.
19            Is taken to be 1 if cov is 0-dimensional
20        grad : list or array
21            Gradient of the Covobs wrt. the means belonging to cov.
22        """
23        self._set_cov(cov)
24        if '|' in name:
25            raise Exception("Covobs name must not contain replica separator '|'.")
26        self.name = name
27        if grad is None:
28            if pos is None:
29                if self.N == 1:
30                    pos = 0
31                else:
32                    raise Exception('Have to specify position of cov-element belonging to mean!')
33            else:
34                if pos > self.N:
35                    raise Exception('pos %d too large for covariance matrix with dimension %dx%d!' % (pos, self.N, self.N))
36            self._grad = np.zeros((self.N, 1))
37            self._grad[pos] = 1.
38        else:
39            self._set_grad(grad)
40        self.value = mean

Initialize Covobs object.

Parameters
  • mean (float): Mean value of the new Obs
  • cov (list or array): 2d Covariance matrix or 1d diagonal entries
  • name (str): identifier for the covariance matrix
  • pos (int): Position of the variance belonging to mean in cov. Is taken to be 1 if cov is 0-dimensional
  • grad (list or array): Gradient of the Covobs wrt. the means belonging to cov.
#   def errsq(self):
View Source
42    def errsq(self):
43        """ Return the variance (= square of the error) of the Covobs
44        """
45        return float(np.dot(np.transpose(self.grad), np.dot(self.cov, self.grad)))

Return the variance (= square of the error) of the Covobs

#   cov
#   grad