# pyerrors.covobs API documentation

# pyerrors.covobs

View Source

      1import numpy as np
      2
      3
      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 np.dot(np.transpose(self.grad), np.dot(self.cov, self.grad)).item()
     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

class Covobs: View Source

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

Covobs(mean, cov, name, pos=None, grad=None) View Source

     7    def __init__(self, mean, cov, name, pos=None, grad=None):
     8        """ Initialize Covobs object.
     9
    10        Parameters
    11        ----------
    12        mean : float
    13            Mean value of the new Obs
    14        cov : list or array
    15            2d Covariance matrix or 1d diagonal entries
    16        name : str
    17            identifier for the covariance matrix
    18        pos : int
    19            Position of the variance belonging to mean in cov.
    20            Is taken to be 1 if cov is 0-dimensional
    21        grad : list or array
    22            Gradient of the Covobs wrt. the means belonging to cov.
    23        """
    24        self._set_cov(cov)
    25        if '|' in name:
    26            raise Exception("Covobs name must not contain replica separator '|'.")
    27        self.name = name
    28        if grad is None:
    29            if pos is None:
    30                if self.N == 1:
    31                    pos = 0
    32                else:
    33                    raise Exception('Have to specify position of cov-element belonging to mean!')
    34            else:
    35                if pos > self.N:
    36                    raise Exception('pos %d too large for covariance matrix with dimension %dx%d!' % (pos, self.N, self.N))
    37            self._grad = np.zeros((self.N, 1))
    38            self._grad[pos] = 1.
    39        else:
    40            self._set_grad(grad)
    41        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.

name
value
def errsq(self): View Source

    43    def errsq(self):
    44        """ Return the variance (= square of the error) of the Covobs
    45        """
    46        return np.dot(np.transpose(self.grad), np.dot(self.cov, self.grad)).item()

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

cov View Source

     99    @property
    100    def cov(self):
    101        return self._cov

grad View Source

    103    @property
    104    def grad(self):
    105        return self._grad