def test(self): prior = np.array([1, 0, 0]) trans = np.matrix([[0, 1, 0], [0, 0, 1], [0, 0, 1]]) len = 4 numex = 2 M = mc_sample(prior, trans, len, numex) assert np.all(np.abs(M-np.array([[0, 1, 2, 2], [0, 1, 2, 2]]))) < 1e-3
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