def std(self, background=None): """Standard deviation of the score of a motif.""" if background is None: background = dict.fromkeys(self._letters, 1.0) else: background = dict(background) total = sum(background.values()) for letter in self._letters: background[letter] /= total variance = 0.0 for i in range(self.length): sx = 0.0 sxx = 0.0 for letter in self._letters: logodds = self[letter, i] if _isnan(logodds): continue if _isinf(logodds) and logodds < 0: continue b = background[letter] p = b * math.pow(2, logodds) sx += p * logodds sxx += p * logodds * logodds sxx -= sx * sx variance += sxx variance = max(variance, 0) # to avoid roundoff problems return math.sqrt(variance)
def std(self, background=None): """Standard deviation of the score of a motif.""" if background is None: background = dict.fromkeys(self._letters, 1.0) else: background = dict(background) total = sum(background.values()) for letter in self._letters: background[letter] /= total variance = 0.0 for i in range(self.length): sx = 0.0 sxx = 0.0 for letter in self._letters: logodds = self[letter, i] if _isnan(logodds): continue if _isinf(logodds) and logodds < 0: continue b = background[letter] p = b * math.pow(2, logodds) sx += p*logodds sxx += p*logodds*logodds sxx -= sx*sx variance += sxx variance = max(variance, 0) # to avoid roundoff problems return math.sqrt(variance)
def mean(self, background=None): """Expected value of the score of a motif.""" if background is None: background = dict.fromkeys(self._letters, 1.0) else: background = dict(background) total = sum(background.values()) for letter in self._letters: background[letter] /= total sx = 0.0 for i in range(self.length): for letter in self._letters: logodds = self[letter, i] if _isnan(logodds): continue if _isinf(logodds) and logodds < 0: continue b = background[letter] p = b * math.pow(2, logodds) sx += p * logodds return sx