```\n'''
html += '\n'
html += '\n'
html += '\n'
for state in Viterbi:
html += '\n' % str(state)
html += '\n'
# process each sequence symbol
X = _terminate(X, 1, self.startsym, self.endsym)
for row in range(len(X)):
html += '\n'
html += '\n' % (row, str(X[row]))
for state in Viterbi:
if Trace and row > 0:
html += '\n' % (str(state),X[row],self.e[state][X[row]],str(state),Viterbi[state][row],Trace[state][row - 1],self.a[Trace[state][row - 1]][state] if Trace[state][row - 1] != None else 0)
else:
html += '\n' % (str(state),X[row],self.e[state][X[row]],str(state),Viterbi[state][row])
html += '\n'
html += 'X%sx%d=%se%s(%s)=%4.2fV%s=%3.1e↑%s[%4.2f]e%s(%s)=%4.2fV%s=%3.1e\n'
html += '```
' if filename: fh = open(filename, 'w') fh.write(html) fh.close() return html ################################################################################################# # The univariate Gaussian density function. ################################################################################################# class Gaussian(): mu = None # mean sigma = None # standard deviation sigmaSquared = None # variance def ROOT_2PI(self): return (2 * math.pi) ** 2 def LOG_ROOT_2PI(self): return 0.5 * (math.log(2) + math.log(math.pi)) def __init__(self, mean, variance): """ Creates a univariate Gaussian distribution with the given fixed mean and variance. """ self.mu = mean self.sigmaSquared = variance; self.sigma = math.sqrt(variance); self.normConst = self.sigma * self.ROOT_2PI(); self.logNormConst = (0.5 * math.log(variance)) + self.LOG_ROOT_2PI(); def __str__(self): return "<" + "%5.3f" % self.mu + u"\u00B1%5.3f" % self.sigma + ">" def getDensity(self, x): """ Returns the density of this Gaussian distribution at the given value. """ return (math.exp(-math.pow((x - self.mu), 2) / (2 * self.sigmaSquared)) / self.normConst); def __getitem__(self, value): """ Get the probability density of a specified value for this Gaussian """ return self.getDensity(value) def getMean(self): return self.mu def getVariance(self): return self.sigmaSquared def getLogDensity(self, x): """ Returns the natural log of the density of this Gaussian distribution at the given value. """ return (-math.pow((x - self.mu), 2) / (2 * self.sigmaSquared)) - self.logNormConst; def sample(self): """ Returns a value sampled from this Gaussian distribution. The implementation uses the Box - Muller transformation [G.E.P.Box and M.E.Muller(1958) "A note on the generation of random normal deviates". Ann.Math.Stat 29: 610 - 611]. """ U = random.random() # get a random value between 0 and 1 V = random.random() # get a random value between 0 and 1 return (self.mu + (self.sigma * math.sin(2 * math.pi * V) * math.sqrt((-2 * math.log(U))))) def estimate(samples, count = None): """ Create a density based on the specified samples. Optionally, provide an iterable with the corresponding counts/weights. """ mean = 0 if count == None: for i in range(len(samples)): mean += samples[i] / len(samples) diff = 0 for i in range(len(samples)): diff += (mean - samples[i]) * (mean - samples[i]); if (diff == 0): return None return Gaussian(mean, diff / len(samples)) elif len(count) == len(samples): totcnt = 0 for i in range(len(samples)): mean += samples[i] * count[i] totcnt += count[i] mean /= totcnt diff = 0 for i in range(len(samples)): diff += (mean - samples[i]) * (mean - samples[i]) * count[i]; if (diff == 0): return None return Gaussian(mean, diff / totcnt) ################################################################################################# # The univariate Poisson distribution ################################################################################################# class Poisson(): def __init__(self, LAMBDA): """ * Define a Poisson distribution * @ param lambda the average number of events per interval """ self.LAMBDA = LAMBDA def p(self, k): """ * The probability mass function * @param k the number of events in the interval * @return the probability of k events """ return math.exp(k * math.log(self.LAMBDA) - self.LAMBDA - lgamma(k + 1)) def cdf(self, k): """ * The cumulative probability function. * The implementation calls the PMF for all values i from 0 to floor(k) * @param k the number of events in the interval * @return the cumulative probability of k events * https://en.wikipedia.org/wiki/Poisson_distribution """ sum = 0 for i in range(k + 1): sum += self.p(i) if (sum >= 1.0): # happens only due to poor numerical precision return 1.0 return sum def lgamma(x): """ * Returns an approximation of the log of the Gamma function of x. Laczos * Approximation Reference: Numerical Recipes in C * http://www.library.cornell.edu/nr/cbookcpdf.html """ cof = [ 76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.1208650973866179e-2, -0.5395239384953e-5 ] y = x tmp = x + 5.5 tmp -= ((x + 0.5) * math.log(tmp)) ser = 1.000000000190015 for j in range(len(cof)): y += 1 ser += (cof[j] / y) return (-tmp + math.log(2.5066282746310005 * ser / x))