コード例 #1
0
ファイル: usage.py プロジェクト: miklou/pycogent
    def fixNegsConstrainedOpt(self, to_minimize=norm_diff, badness=1e6):
        """Uses constrained minimization to find approx q matrix.

        to_minimize: metric for comparing orig result and new result.

        badness: scale factor for penalizing negative off-diagonal values.
        """
        if not sum_neg_off_diags(self._data):
            return self
        q = ravel(without_diag(self._data))
        p = expm(self._data)(t=1)

        def err_f(q):
            new_q = reshape(array(q), (4, 3))
            new_q = with_diag(new_q, -sum(new_q, 1))
            p_new = expm(new_q)(t=1)
            result = to_minimize(ravel(p), ravel(p_new))
            if q.min() < 0:
                result += -q.min() * badness
            return result

        a = array(q)
        xmin = fmin(func=err_f, x0=a, disp=0)
        r = reshape(xmin, (4, 3))
        new_q = with_diag(r, -sum(r, 1))
        return self.__class__(new_q, self.Alphabet)
コード例 #2
0
ファイル: usage.py プロジェクト: miklou/pycogent
 def err_f(q):
     new_q = reshape(array(q), (4,3))
     new_q = with_diag(new_q, -sum(new_q, 1))
     p_new = expm(new_q)(t=1)
     result = to_minimize(ravel(p), ravel(p_new))
     if q.min() < 0:
         result += -q.min() * badness
     return result
コード例 #3
0
ファイル: usage.py プロジェクト: miklou/pycogent
 def err_f(q):
     new_q = reshape(array(q), (4, 3))
     new_q = with_diag(new_q, -sum(new_q, 1))
     p_new = expm(new_q)(t=1)
     result = to_minimize(ravel(p), ravel(p_new))
     if q.min() < 0:
         result += -q.min() * badness
     return result
コード例 #4
0
ファイル: usage.py プロジェクト: miklou/pycogent
    def fixNegsEven(self):
        """Returns copy of self w/o negative off-diags, using 'even' heuristic.
        
        If a negative off-diagonal is encountered, sets it to 0.

        Distributes the negative score evenly among the other elements.
        """
        m = without_diag(self._data)
        for i, row in enumerate(m):
            is_neg = row < 0
            if any(is_neg):
                num_negs = sum(is_neg)
                sum_negs = sum(is_neg*row)
                is_not_neg = logical_not(is_neg)
                num_not_neg = sum(is_not_neg)
                new_row = (row + (sum_negs/(num_not_neg+1)))*is_not_neg
                m[i] = new_row
        return self.__class__(with_diag(m, -sum(m,1)), self.Alphabet)
コード例 #5
0
ファイル: usage.py プロジェクト: miklou/pycogent
    def fixNegsEven(self):
        """Returns copy of self w/o negative off-diags, using 'even' heuristic.
        
        If a negative off-diagonal is encountered, sets it to 0.

        Distributes the negative score evenly among the other elements.
        """
        m = without_diag(self._data)
        for i, row in enumerate(m):
            is_neg = row < 0
            if any(is_neg):
                num_negs = sum(is_neg)
                sum_negs = sum(is_neg * row)
                is_not_neg = logical_not(is_neg)
                num_not_neg = sum(is_not_neg)
                new_row = (row + (sum_negs / (num_not_neg + 1))) * is_not_neg
                m[i] = new_row
        return self.__class__(with_diag(m, -sum(m, 1)), self.Alphabet)
コード例 #6
0
ファイル: usage.py プロジェクト: miklou/pycogent
    def fixNegsConstrainedOpt(self, to_minimize=norm_diff, badness=1e6):
        """Uses constrained minimization to find approx q matrix.

        to_minimize: metric for comparing orig result and new result.

        badness: scale factor for penalizing negative off-diagonal values.
        """
        if not sum_neg_off_diags(self._data):
            return self
        q = ravel(without_diag(self._data))
        p = expm(self._data)(t=1)
        def err_f(q):
            new_q = reshape(array(q), (4,3))
            new_q = with_diag(new_q, -sum(new_q, 1))
            p_new = expm(new_q)(t=1)
            result = to_minimize(ravel(p), ravel(p_new))
            if q.min() < 0:
                result += -q.min() * badness
            return result
        a = array(q)
        xmin = fmin(func=err_f, x0=a, disp=0)
        r = reshape(xmin, (4,3))
        new_q = with_diag(r, -sum(r, 1))
        return self.__class__(new_q, self.Alphabet)
コード例 #7
0
ファイル: test_array.py プロジェクト: mikerobeson/pycogent
 def test_with_diag(self):
     """with_diag should add diagonal to matrix"""
     a = array([[2,3],[4,6],[7,8]])
     b = with_diag(a, array([1,5,9]))
     self.assertEqual(b, array([[1,2,3],[4,5,6],[7,8,9]]))