Example #1
0
def gaussian_logpdf(mu,sigma,x):
    '''
    Non-positive standar deviations will be clipped
    '''
    mu,sigma,x = map(np.float128,(mu,sigma,x))
    x = (x-mu)/sigma
    return -0.5*x*x - slog(sigma) - logsqrt2pi
Example #2
0
def hmm_poisson_parameter_guess(X,Y,N):
    '''
    Based on a sequence of inferred states X, estimate the log of
    the transition matrix A, the prior vector P, and the state
    probability matrix B.

    Parameters
    ----------
    X : ndarary
        1D integer array of hidden states with values in 0..N-1
    Y : ndarary
        1D integer array of observations
    N : positive integer
        Number of states
    '''
    # Estimate model parameters from best-guess X
    p1  = np.mean(X)
    p01 = np.sum(np.diff(X)== 1)/(1+np.sum(X==0))
    p10 = np.sum(np.diff(X)==-1)/(1+np.sum(X==1))
    mu0 = np.mean(Y[X==0])
    mu1 = np.mean(Y[X==1])
    params = (p1,p01,p10,mu0,mu1)

    # Prior for state at first observation
    logP = np.array([np.log1p(-p1),slog(p1)])
    # State transition array
    logA = np.array([
        [np.log1p(-p01), slog(p01)],
        [slog(p10), np.log1p(-p10)]],
        dtype=np.float64)
    # Poisson process rates
    O = np.arange(N)      # List of available states
    logB = np.array([
        poisson_logpdf(O, mu0),
        poisson_logpdf(O, mu1)])
    return logP,logA,logB,params
Example #3
0
def poisson_logpdf(k,l):
    k,l = map(np.float128,(k,l))
    return k*slog(l)-l-np.array(map(log_factorial,k))