示例#1
0
 def test2D(self):
     mu = np.matrix([[1], [2]])
     covar = np.matrix([[1, 0.5], [0.5, 2]])
     nsamp = 10000
     M = gaussian_sample(mu, covar, nsamp)
     assert np.all(np.abs(np.mean(M, 0) - mu.T) < 1e-1)
     assert np.all(np.abs(cov(M) - covar) < 1e-1)
示例#2
0
 def test2D(self):
     mu = np.matrix([[1], [2]])
     covar = np.matrix([[1, .5], [.5, 2]])
     nsamp = 10000
     M = gaussian_sample(mu, covar, nsamp)
     assert np.all(np.abs(np.mean(M, 0) - mu.T) < 1e-1)
     assert np.all(np.abs(cov(M) - covar) < 1e-1)
示例#3
0
def mhmm_sample(T, numex, initial_prob, transmat, mu, Sigma, mixmat=None):
    '''
    % SAMPLE_MHMM Generate random sequences from an HMM with (mixtures of) Gaussian output.
    % [obs, hidden] = sample_mhmm(T, numex, initial_prob, transmat, mu, Sigma, mixmat)
    %
    % INPUTS:
    % T - length of each sequence
    % numex - num. sequences
    % init_state_prob(i) = Pr(Q(1) = i)
    % transmat(i,j) = Pr(Q(t+1)=j | Q(t)=i)
    % mu(:,j,k) = mean of Y(t) given Q(t)=j, M(t)=k
    % Sigma(:,:,j,k) = cov. of Y(t) given Q(t)=j, M(t)=k
    % mixmat(j,k) = Pr(M(t)=k | Q(t)=j) : set to ones(Q,1) or omit if single mixture
    %
    % OUTPUT:
    % obs(:,t,l) = observation vector at time t for sequence l
    % hidden(t,l) = the hidden state at time t for sequence l
    '''
    
    assert initial_prob.ndim == 1
    
    Q = len(initial_prob);
    if mixmat==None:
        mixmat = np.ones((Q,1))
    O = mu.shape[0]
    hidden = np.zeros((T, numex))
    obs = np.zeros((O, T, numex))
    
    hidden = mc_sample(initial_prob, transmat, T, numex).T
    for i in range(0,numex):
        for t in range(0,T):
            q = hidden[t,i]
            m = np.asscalar(sample_discrete(mixmat[q,:], 1, 1))
            obs[:,t,i] = gaussian_sample(mu[:,q,m], Sigma[:,:,q,m], 1)
    
    return obs, hidden
示例#4
0
 def testRow(self):
     mu = np.matrix([[1], [2]])
     covar = np.matrix([[1, 0], [0, 1]])
     nsamp = 1
     assert gaussian_sample(mu, covar, nsamp).shape == (1, 2)
示例#5
0
 def testRow(self):
     mu = np.matrix([[1], [2]])
     covar = np.matrix([[1, 0], [0, 1]])
     nsamp = 1
     assert gaussian_sample(mu, covar, nsamp).shape == (1, 2)