def __init__(self, pi, pt, pf, pg, ps): self.init = pi self.transit = pt self.forget = pf self.guess = pg self.slip = ps ''' bkt parameters in matrix form prior: unknown, known [1-init, init] hh: to unknown to known from unknown [[1-transit, transit], from known [forget, 1-forget]] ho: wrong right unknown [[1-guess, guess], known [slip, 1-slip]] ''' self.pi = np.array(([1.0 - self.init, self.init]), np.float) self.hh = np.array(([[1.0 - self.transit, self.transit], [self.forget, 1.0 - self.forget]]), np.float) self.ho = np.array( ([[1.0 - self.guess, self.guess], [self.slip, 1.0 - self.slip]]), np.float) self.hmm = hmm.hmm(self.pi, self.hh, self.ho)
def test_simple_hmm(self): s1 = hmm.state('S1', 0.5, { '1': 0.5, '2': 0.5 }, { 'S1': 0.9, 'S2': 0.1 }) s2 = hmm.state('S2', 0.5, { '1': 0.25, '2': 0.75 }, { 'S1': 0.8, 'S2': 0.2 }) model = hmm.hmm(['1', '2'], [s1, s2]) state_path, prob = model.viterbi_path('222') self.assertEqual(state_path, ['S2', 'S1', 'S1']) self.assertEqual(round(prob, 6), -1.170696)
def smarthouse( dataset=["A", "B"], train_days=5, train_offset=0, test_days=None, test_offset=0, use_day_period=False, n_samples=None, ): if not (type(dataset) == tuple or type(dataset) == list): dataset = [dataset] truths = [] predicts = [] accs = [] for f in dataset: df = load_dataset(f, use_day_period=use_day_period) train_s, train_o, test_s, test_o = trainset_testset( df, train_days=train_days, train_offset=train_offset, test_days=test_days, test_offset=test_offset, ) # Calcolo delle distribuzioni della HMM n = max(df['activity'] + 1) m = max(df['sensors'] + 1) P, T, O = hmm(train_s, train_o, n=n, m=m) if n_samples: test_s, test_o = random_sample(P, T, O, n_samples) # Esegue l'algoritmo di Viterbi sul testset e calcola # calcola la percentuale di stati predetti correttamente predicted, p = viterbi(P, T, O, test_o) accuracy = reduce( lambda i, j: i + (1 if j[0] == j[1] else 0), zip(test_s, predicted), 0, ) / len(predicted) accs.append(accuracy) truths.append(test_s) predicts.append(predicted) if len(accs) == 1: return truths[0], predicts[0], accs[0] return truths, predicts, accs
def __call__(self, mapLocations, successRate, admixedClass): """Filters transitions based on hmm model Arguments: - `mapLocations`: Locations of all the SNPs classified in [cM] - `successRate`: Probabilities of successfully classifying each snp - `admixedClass`: classification made """ win_size = self.winSize #int(np.ceil(len(mapLocations)/float(admixedClass.shape[0]))) #determine transition matrices a = [] b = [] # mapLocations.sort() ### tabbzi: added this in to possibly resolve ordering, but it is not a proper fix; entire chromosomes load out of order oldPos = 0 for i in range(0, len(mapLocations), win_size): newPos = np.mean(mapLocations[i:i + win_size]) dM = -(newPos - oldPos) / 100 * self.nGens e = np.exp(dM) oldPos = newPos x = np.empty( (self.nClasses, self.nClasses)) #Create state transitions for j in range(self.nClasses): x[j, :] = (1. - e) / (self.nClasses - 1) x[j, j] = e a.append(x) a = np.asarray(a) for s in successRate: # x=np.empty((self.nClasses, self.nClasses)) #Create output transitions # for i in range(self.nClasses): # x[i,:]=(1.-s)/(self.nClasses-1) # x[i,i]=s x = (s / s.sum(0).astype(np.float)).T #new1 ##x=(s.T/s.sum(1).astype(float)).T #new2 b.append(x) b = np.asarray(b) model = hmm(a, b) #Go through and calculate hmm values results = [] posterior = [] for i in range(admixedClass.shape[1]): model.forward_backward(admixedClass[:, i].astype( int)) ### tabbzi: added `.astype(int)` to `admixedClass[:,i] maxIdx = model.pstate.argsort(1)[:, -1] results.append(maxIdx) p = [ np.asarray(model.pstate)[k][j] for (k, j) in enumerate(maxIdx) ] posterior.append(p) return np.array(results).T, np.asarray(posterior).T
def random_model(self): self.init = random.random() self.transit = random.random() self.slip = random.random() self.guess = random.random() self.pi = np.array(([1.0 - self.init, self.init]), np.float) self.hh = np.array(([[1.0 - self.transit, self.transit], [self.forget, 1.0 - self.forget]]), np.float) self.ho = np.array( ([[1.0 - self.guess, self.guess], [self.slip, 1.0 - self.slip]]), np.float) self.hmm = hmm.hmm(self.pi, self.hh, self.ho)
def train(self, files): self.tagset = tagset() self.tagset.add('start') self.transition_freq = countmap2() self.word_freq = countmap2() for f in files: fhandle = open(f, 'r') for line in fhandle: if line != '\n': prev_pos = 'start' for token in line.split(' '): parts = token.strip().split('/') if len(parts) == 2: word = parts[0] pos = parts[1] self.tagset.add(pos) self.transition_freq.incr(prev_pos, pos) self.word_freq.incr(word, pos) prev_pos = pos fhandle.close() self.transition_freq.normalize() self.word_freq.normalize() self.Q = self.tagset.list() self.A = np.zeros((len(self.Q), len(self.Q))) for i in range(0, len(self.Q)): for j in range(0, len(self.Q)): try: self.A[i, j] = self.transition_freq.get(self.Q[i], self.Q[j]) except KeyError: pass def _b(word, pos): try: return self.word_freq.get(word, pos) except KeyError: return 0 self.b = _b self.hmm = hmm(self.Q, self.A, _b, 0)
def __init__(self, model, dataset, model_parameters): ### model : string 'PHMM' ,'HMM' , 'DMP' ### dataset : (datalength x dimension) for all models if dataset.ndim > 2: dataset = dataset.reshape( (dataset.shape[0] * dataset.shape[1], dataset.shape[2])) if model == 'PHMM': raise Exception("Not ready yet!") if model == 'HMM': self.hmm = hmm(model_parameters, dataset) self.prior, self.a, self.mu, self.sigma, self.loglikelihood = self.hmm.Baum_Welch( ) if model == 'DMP': # model_parameters = [bf_number, K, task_number] # K = vector self.dmp = DMP(model_parameters[0], model_parameters[1], model_parameters[2], dataset) b = False
def input(self, event): global answer, prev_str, waits, pages pages = 0 input_str = self.m_textCtrl3.GetValue() for i in range(len(input_str)): if input_str[i].encode('UTF-8').isalpha(): break pinyin = ''.join(input_str[i:]) prev_str = ''.join(input_str[0:i]) # print(pinyin) answer = hmm.hmm(init_pros, change_pros, biu_pros, pinyin) # print(answer) wait = ' ' waits = [] for i in range(5): waits.append(answer[i]) wait = wait.join(waits) self.m_textCtrl4.SetValue(wait)
def __call__(self,mapLocations, successRate, admixedClass): """Filters transitions based on hmm model Arguments: - `mapLocations`: Locations of all the SNPs classified in [cM] - `successRate`: Probabilities of successfully classifying each snp - `admixedClass`: classification made """ win_size=self.winSize #int(np.ceil(len(mapLocations)/float(admixedClass.shape[0]))) #determine transition matrices a=[]; b=[] oldPos=0 for i in range(0,len(mapLocations), win_size): newPos=np.mean(mapLocations[i:i+win_size]) dM=-(newPos - oldPos)/100*self.nGens e=np.exp(dM) oldPos=newPos x=np.empty((self.nClasses, self.nClasses)) #Create state transitions for j in range(self.nClasses): x[j,:]=(1.-e)/(self.nClasses-1) x[j,j]=e a.append(x) a=np.asarray(a) for s in successRate: # x=np.empty((self.nClasses, self.nClasses)) #Create output transitions # for i in range(self.nClasses): # x[i,:]=(1.-s)/(self.nClasses-1) # x[i,i]=s x=(s/s.sum(0).astype(np.float)).T #new1 ##x=(s.T/s.sum(1).astype(float)).T #new2 b.append(x) b=np.asarray(b) model=hmm(a, b) #Go through and calculate hmm values results=[] posterior=[] for i in range(admixedClass.shape[1]): model.forward_backward(admixedClass[:,i]) maxIdx=model.pstate.argsort(1)[:,-1] results.append(maxIdx) p=[np.asarray(model.pstate)[k][j] for (k,j) in enumerate(maxIdx)] posterior.append(p) return np.array(results).T, np.asarray(posterior).T
def test_implied_terminal_state(self): # The HMM shown in figure 1 of: # Eddy SR. 2004. What is a hidden Markov model? # Nature Biotechnology 22(10): 1315-1316. s1 = hmm.state('E', 1.0, {'A': 0.25, 'C': 0.25, 'G': 0.25, 'T': 0.25}, {'E': 0.9, '5': 0.1}) s2 = hmm.state('5', 0.0, {'A': 0.05, 'C': 0.0, 'G': 0.95, 'T': 0.0}, {'I': 1.0}) s3 = hmm.state('I', 0.0, {'A': 0.4, 'C': 0.1, 'G': 0.1, 'T': 0.4}, {'I': 0.9}, 0.1) model = hmm.hmm(['A', 'C', 'G', 'T'], [s1, s2, s3]) self.assertEqual(len(model.initial_states), 1) self.assertTrue(model.terminal_state) self.assertEqual(len(model.terminating_states), 1)
def test_implied_terminal_state(self): s1 = hmm.state('E', 1.0, {'A': 0.25, 'C': 0.25, 'G': 0.25, 'T': 0.25}, {'E': 0.9, '5': 0.1}) s2 = hmm.state('5', 0.0, {'A': 0.05, 'C': 0.0, 'G': 0.95, 'T': 0.0}, {'I': 1.0}) s3 = hmm.state('I', 0.0, {'A': 0.4, 'C': 0.1, 'G': 0.1, 'T': 0.4}, {'I': 0.9}, 0.1) model = hmm.hmm(['A', 'C', 'G', 'T'], [s1, s2, s3]) state_path, prob = model.viterbi_path('CTTCATGTGAAAGCAGACGTAAGTCA') self.assertEqual(state_path, ['E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'E', '5', 'I', 'I', 'I', 'I', 'I', 'I', 'I']) self.assertEqual(round(prob, 10), -17.9014785649)
def test_simple_hmm(self): # The simple HMM shown in section 12.1 of Ewens and Grant, "Statistical # Methods in Bioinformatics: An Introduction, 2nd Ed.", 2005 s1 = hmm.state( 'S1', # name of the state 0.5, # probability of being the initial state { '1': 0.5, # probability of emitting a '1' at each visit '2': 0.5 }, # probability of emitting a '2' at each visit { 'S1': 0.9, # probability of transitioning to itself 'S2': 0.1 }) # probability of transitioning to state 'S2' s2 = hmm.state('S2', 0.5, { '1': 0.25, '2': 0.75 }, { 'S1': 0.8, 'S2': 0.2 }) model = hmm.hmm(['1', '2'], # all symbols that can be emitted [s1, s2]) # all of the states in this HMM self.assertEqual(model.states['S1'].p_initial, 0.5) self.assertEqual(model.states['S2'].p_transition['S1'], 0.8) self.assertEqual(len(model.initial_states), 2) self.assertFalse(model.terminal_state) self.assertEqual(len(model.terminating_states), 0)
def test_repr(self): s1 = hmm.state('E', 1.0, {'A': 0.25, 'C': 0.25, 'G': 0.25, 'T': 0.25}, {'E': 0.9, '5': 0.1}) s2 = hmm.state('5', 0.0, {'A': 0.05, 'C': 0.0, 'G': 0.95, 'T': 0.0}, {'I': 1.0}) s3 = hmm.state('I', 0.0, {'A': 0.4, 'C': 0.1, 'G': 0.1, 'T': 0.4}, {'I': 0.9}, 0.1) model = hmm.hmm(['A', 'C', 'G', 'T'], [s1, s2, s3]) model2 = eval(repr(model)) self.assertEqual(model.alphabet, model2.alphabet) self.assertEqual(model.states['E'].p_transition['5'], model2.states['E'].p_transition['5']) self.assertEqual(model.states['5'].p_emission['T'], model2.states['5'].p_emission['T']) self.assertEqual(model.states['I'].p_termination, model2.states['I'].p_termination)
#plt.show() signatures.append(tempsig) diagcov = False #print np.shape(signatures) #print np.shape(np.column_stack(signatures[1][0:3]).T) print "Full Covariance Matrix" for k in range (1,7): MarkovModel = [] for a in range (0,5): trans = hmm.lrtrans(k) llist = [] for b in range(0,3): # print np.shape(signatures[a][b]) llist.append([len(signatures[a][b])]) MarkovModel.append(hmm.hmm(np.column_stack(signatures[a][0:3]),llist,trans,diagcov = diagcov)) #print np.shape(testdataStack) # print np.shape(testdataStack[0][0]) TestClassification = [] OriginalTestClassification = [] TrainClassification = [] OriginalTrainClassification = [] for a in range (0,5): for b in range (3,len(signatures[a])): OriginalTestClassification.append(a) templist = [] for c in range (0,5): #print np.shape(np.array([np.column_stack(signatures[a][b]).T])) templist.append(hmm.negloglik(np.array([np.column_stack(signatures[a][b]).T]),trans = MarkovModel[c][0],dists = MarkovModel[c][1])) TestClassification.append(np.argmin(templist)) utils.confusion(OriginalTestClassification,TestClassification)
# # hmm_viterbi.py <hmm> <seqs> # # Outputs the Viterbi decodings of the sequences in the fasta file <seqs> using # the HMM defined in the file <hmm>. The format <hmm> and <seqs> are as described # in the projects in MLiB Q3/2015. # # Christian Storm Pedersen, 08-feb-2015 import sys import string from hmm import hmm from fasta import fasta m = hmm(sys.argv[1]) d = fasta(sys.argv[2]) print '; Viterbi-decodings of %s using HMM %s' % (sys.argv[2], sys.argv[1]) print for key in sorted(d.keys()): x = m.str_to_obs(d[key]) # Compute Viterbi decoding and its log-likelihood vit_z, vit_logpz = m.viterbi_decoding(x) print '>' + key print d[key] print '# ' print m.states_to_str(vit_z) print '; log P(x,z) = %f' % (vit_logpz) print
import hmm s1 = hmm.state( 'S1', # name of the state 0.5, # probability of being the initial state { '1': 0.5, # probability of emitting a '1' at each visit '2': 0.5 }, # probability of emitting a '2' at each visit { 'S1': 0.9, # probability of transitioning to itself 'S2': 0.1 } # probability of transitioning to state 'S2' ) s2 = hmm.state( 'S2', 0.5, { '1': 0.25, '2': 0.75 }, { 'S1': 0.8, 'S2': 0.2 } ) model = hmm.hmm(['1', '2'], # all symbols that can be emitted [s1, s2]) # all of the states in this HMM model.enumerate('222') path, prob = model.viterbi_path('222') print path print prob
data = np.hstack([signal] * 2) lengths = [9] * 2 trans = np.array([[ 0. , 1./3 , 1./3 , 1./3, 0. ], [ 0. , 0.45, 0.45, 0., 0.1 ], [ 0. , 0.45, 0.45, 0., 0.1 ], [ 0. , 0. , 0. , 1., 0. ], [ 0. , 0. , 0. , 0., 0. ]]) states = np.array([ 1, 1, 1, 2, 2, 2, 2, 2, 1]) newtrans = np.array([[ 0. , 1. , 0. , 0. ], [ 0. , 0.5, 0.25, 0.25 ], [ 0. , 0.2, 0.8 , 0 ], [ 0. , 0. , 0. , 0. ]]) newmeans = np.array([[ 1. , 1.66]]) newcovs = np.array([[[ 0.015 ]], [[ 1.0464]]]) nll = 22.290642196869609 trans, dists, nll = hmm.hmm(data, lengths, trans) print trans # [[ 0. 1. 0. 0. ] # [ 0. 0.5 0.25 0.25] # [ 0. 0.2 0.8 0. ] # [ 0. 0. 0. 0. ]] print [a.mean() for a in dists] # [array([ 1.]), array([ 1.66])] print [a.cov() for a in dists] # [array([[ 0.015]]), array([[ 1.0464]])] print 'Negative log-likelihood: ', nll # 22.2906421969 npt.assert_almost_equal([a.mean() for a in ans[1]], newmeans.transpose(), decimal=4) npt.assert_almost_equal([a.cov() for a in ans[1]], newcovs, decimal=4) npt.assert_almost_equal(ans[2], nll, decimal=4)
# # A simple experiment that computes p(x,z) and log p(x,z) for increasing x (and z) in # order to when p(x,z) becomes to small to be represented. For the 7-state model this # is when |x| = |z| = 530. # from hmm import hmm m = hmm("hmm-7-state.txt") x = m.str_to_obs( "TGAGTATCACTTAGGTCTATGTCTAGTCGTCTTTCGTAATGTTTGGTCTTGTCACCAGTTATCCTATGGCGCTCCGAGTCTGGTTCTCGAAATAAGCATCCCCGCCCAAGTCATGCACCCGTTTGTGTTCTTCGCCGACTTGAGCGACTTAATGAGGATGCCACTCGTCACCATCTTGAACATGCCACCAACGAGGTTGCCGCCGTCCATTATAACTACAACCTAGACAATTTTCGCTTTAGGTCCATTCACTAGGCCGAAATCCGCTGGAGTAAGCACAAAGCTCGTATAGGCAAAACCGACTCCATGAGTCTGCCTCCCGACCATTCCCATCAAAATACGCTATCAATACTAAAAAAATGACGGTTCAGCCTCACCCGGATGCTCGAGACAGCACACGGACATGATAGCGAACGTGACCAGTGTAGTGGCCCAGGGGAACCGCCGCGCCATTTTGTTCATGGCCCCGCTGCCGAATATTTCGATCCCAGCTAGAGTAATGACCTGTAGCTTAAACCCACTTTTGGCCCAAACTAGAGCAACAATCGGAATGGCTGAAGTGAATGCCGGCATGCCCTCAGCTCTAAGCGCCTCGATCGCAGTAATGACCGTCTTAACATTAGCTCTCAACGCTATGCAGTGGCTTTGGTGTCGCTTACTACCAGTTCCGAACGTCTCGGGGGTCTTGATGCAGCGCACCACGATGCCAAGCCACGCTGAATCGGGCAGCCAGCAGGATCGTTACAGTCGAGCCCACGGCAATGCGAGCCGTCACGTTGCCGAATATGCACTGCGGGACTACGGACGCAGGGCCGCCAACCATCTGGTTGACGATAGCCAAACACGGTCCAGAGGTGCCCCATCTCGGTTATTTGGATCGTAATTTTTGTGAAGAACACTGCAAACGCAAGTGGCTTTCCAGACTTTACGACTATGTGCCATCATTTAAGGCTACGACCCGGCTTTTAAGACCCCCACCACTAAATAGAGGTACATCTGA" ) z = m.str_to_states( "4444432132132132132132132132132132132132132132132132132132132132132132144444444445675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675674321321321321321321321321321321321321321321321321321321321321321321321321321321321321321321321321321321321321321321321321321321321432132132132132132132132144445675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675675674444444567567567567567567567567567567567567567567567567567567567567567567567567567567567567567567567567567567567567567567443213213213213213213213213213213213213213213213213213213213213213213213213213213213213213213213214321321321321321321321321321321321321321321321321321321321321321" ) for i in range(1, len(x)): print() print(i) print("p(x,z) = ", m.joint_prob(x[:i], z[:i])) print("log p(x,z) = ", m.log_joint_prob(x[:i], z[:i]))
def run(self): # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数 module = readdata(self.filename) prob = hmm(test, module) self.distances[self.num] = prob self.diction[self.num] = self.filename '''
from util import * from hmm import hmm root = os.path.dirname(__file__) data_path = root + '/data' model_path = root + '/model' dataset_path = root + '/dataset' dataset = load_dataset(dataset_path, 'zbot-zeroaccess-1000-10000.txt') alphabet = dataset['alphabet'] zeroaccess_dataset = dataset['zeroaccess_dataset'] zbot_dataset = dataset["zbot_dataset"] states = ['A', 'B'] zeroaccess_hmm = hmm('zeroaccess', states, alphabet) zeroaccess_hmm.load_model(model_path, 'zeroaccess_best.txt') zbot_hmm = hmm('zbot', states, alphabet) zbot_hmm.load_model(model_path, 'zbot_best.txt') dataset = {} zeroaccess_index = 0 zbot_index = 1 labled_zeroaccess_train = [] labled_zeroaccess_test = [] labled_zbot_train = [] labled_zbot_test = [] print("data loaded")
tag = pred[i][2:] end = indexes[i] elif pred[i][0] == 'I': end = indexes[i] else: if begin != -1: addToSolution(tag, begin, end) begin = -1 end = -1 tag = '' if i == len(indexes) - 1: if begin != -1: addToSolution(tag, begin, end) begin = -1 end = -1 tag = '' line_no = (line_no + 1) % 3 if __name__ == "__main__": hmm.smoothing() hmm.hmm() lambda1, lambda2, lambda3 = hmm.deleted_interpolation() main() baseline.printSolution(Solution)
########################### # Load gps data # ########################### data = gps_util.deg2rad*np.vstack(pickle.load( open( "pts.p", "rb" ) )) N = 3 T = len(data) obs_dim = 1 ############################################### # Construct model and run posterior inference # ############################################### """""" roadmap=gps_util.findwaypts() obs_distns = [observations.gaussian(roadmap[state],np.array([(gps_util.width/4)**2])) for state in xrange(N)] model1= hmm.hmm(T,obs_distns,data) durparams = [10.*(idx+1) for idx in xrange(N)] Nmax=3 dur_distns = [durations.poisson() for state in xrange(Nmax)] model2=hsmm.hsmm(T,obs_distns,dur_distns) model2.states.stateseq=np.copy(model1.states.stateseq) labels=gps_util.laneassign(data,obs_distns) init_labels=model1.states.stateseq; for idx in progprint_xrange(20): model1.resample(data) model2.resample(data) #####################
diagcov = False #print np.shape(signatures) #print np.shape(np.column_stack(signatures[1][0:3]).T) print "Full Covariance Matrix" for k in range(1, 7): MarkovModel = [] for a in range(0, 5): trans = hmm.lrtrans(k) llist = [] for b in range(0, 3): # print np.shape(signatures[a][b]) llist.append([len(signatures[a][b])]) MarkovModel.append( hmm.hmm(np.column_stack(signatures[a][0:3]), llist, trans, diagcov=diagcov)) #print np.shape(testdataStack) # print np.shape(testdataStack[0][0]) TestClassification = [] OriginalTestClassification = [] TrainClassification = [] OriginalTrainClassification = [] for a in range(0, 5): for b in range(3, len(signatures[a])): OriginalTestClassification.append(a) templist = [] for c in range(0, 5): #print np.shape(np.array([np.column_stack(signatures[a][b]).T])) templist.append( hmm.negloglik(np.array(
traindataList.append((traindata.get(a)[b].T)) testdataStack.append((testdataList)) traindataStack.append((traindataList)) testdataStack, traindataStack = normalize(testdataStack, traindataStack) #print np.shape(testdataStack[0][0])[1] #llist = getlistoflengths(traindataStack[0]) #print llist diagcov = False print "Full Covariance Matrix" for k in range(1, 7): MarkovModel = [] for a in traindataStack: trans = hmm.lrtrans(k) llist = getlistoflengths(a) MarkovModel.append( hmm.hmm(np.column_stack(a), llist, trans, diagcov=diagcov)) # print np.shape(testdataStack) # print np.shape(testdataStack[0][0]) TestClassification = [] OriginalTestClassification = [] TrainClassification = [] OriginalTrainClassification = [] for a in range(np.shape(testdataStack)[0]): llist = getlistoflengths(testdataStack[a]) classifiedtests = [] origtests = [] for b in range(len(llist)): #origtests.append(keyList[a]) temp = np.array([testdataStack[a][b]]) # print np.shape(temp) templistlen = []
# #hmm_model = hmm.hmm(pi, A, B) #hmm_model.random_model() #hmm_model.print_parameters() # #ss, os = hmm_model.synthetic_data(2000) # #print('\nss:', ss) #print('\nos:', os) pi = [0.333, 0.333, 0.333] A = [[0.333, 0.333, 0.333], [0.333, 0.333, 0.333], [0.333, 0.333, 0.333]] B = [[0.5, 0.5], [0.75, 0.25], [0.25, 0.75]] os = [0, 0, 0, 0, 1, 0, 1, 1, 1, 1] test_model = hmm.hmm(pi, A, B) test_model.baum_welch(os, 0.001) test_model.print_parameters() #bkt_model = bkt.bkt(0.1, 0.3, 0.0, 0.1, 0.03) #bkt_model.print_parameters() #bkt_model.random_model() #bkt_model.print_parameters() # #ss, os = bkt_model.synthetic_data(20) #print('\nss:', ss) #print('\nos:', os) #alpha = bkt_model.baum_welch(os, 0.001) #print('\nestimate parameters: ')
from hmm import hmm m = hmm("hmm-2-state.txt") alpha, scale = m.forward_with_scaling([0, 0, 0, 0, 0, 1, 1, 0, 0, 0]) for i in alpha: print(i) for i in scale: print(i) beta = m.backward_with_scaling([0, 0, 0, 0, 0, 1, 1, 0, 0, 0], scale) for i in beta: print(i)
(len(emissions), len(emissions[list(emissions.keys())[0]]))) for strategy in emissions: for move in emissions[strategy]: emission_probabilities[s[strategy]][ m[move]] = emissions[strategy][move] return emission_probabilities def evidence_to_index(evidence): new_evidence = [] for i, e in enumerate(evidence): e = {"round": i, "agent1": m[e["agent1"]], "agent2": m[e["agent2"]]} new_evidence.append(e) return new_evidence # np.eye() creates diagonal matrix of ones, this represents prob 1 of transitioning to same strategy # and prob 0 of transitioning from one strat to another. emission_probabilities = init_emission() transition_probabilities = np.eye(len(s), dtype=int) priors = [float(1) / len(s)] * len(s) # all startegies are equaly probable with open(os.getcwd() + "/test_types/test3.json") as f: e = json.load(f) evidence = evidence_to_index(e) hmm = hmm(transition_probabilities, emission_probabilities, priors, evidence) hmm.set_agent("agent2") strategies_prob = hmm.filter() for i, strat in enumerate(strategies_prob): print(strats[i] + ": " + str(round(strat, 2)) + ",\n")
if len(sys.argv) < 2: print( 'Not enough arguments. Please use -g to generate fake text or -p to predict text' ) exit() flag = sys.argv[1] if flag[1] != 'g' and flag[1] != 'p': print( 'Bad flag specified. Please use -g to generate fake text or -p to predict text' ) exit() #Read in text corpus df = pd.read_csv('data/onionArticles.csv', dtype=str, header=None) onionArticles = np.array(df, dtype=str) #Train model model = hmm.hmm() model.fit(onionArticles) #Based on flag, either generate or predict text if flag[1] == 'g': lines = input('Enter how many lines of text would you like to generate: ') n = int(lines) for _ in range(n): print(model.generateFakeText()) if flag[1] == 'p': prompt = input('Enter your prompt for predicting text: ') print(model.predictText(prompt))
for b in range(len(traindata.get(a))): traindataList.append((traindata.get(a)[b].T)) testdataStack.append((testdataList)) traindataStack.append((traindataList)) testdataStack,traindataStack = normalize(testdataStack, traindataStack) #print np.shape(testdataStack[0][0])[1] #llist = getlistoflengths(traindataStack[0]) #print llist diagcov = False print "Full Covariance Matrix" for k in range (1,7): MarkovModel = [] for a in traindataStack: trans = hmm.lrtrans(k) llist = getlistoflengths(a) MarkovModel.append(hmm.hmm(np.column_stack(a),llist,trans,diagcov = diagcov)) # print np.shape(testdataStack) # print np.shape(testdataStack[0][0]) TestClassification = [] OriginalTestClassification = [] TrainClassification = [] OriginalTrainClassification = [] for a in range (np.shape(testdataStack)[0]): llist = getlistoflengths(testdataStack[a]) classifiedtests = [] origtests = [] for b in range (len(llist)): #origtests.append(keyList[a]) temp = np.array([testdataStack[a][b]]) # print np.shape(temp) templistlen = []
import sys sys.path.append("/home/mengqinggang/workspaces/aicfe/bkt/src") import numpy as np from hmm import hmm pi = [0.333, 0.333, 0.333] A = [[0.95, 0.05, 0.05], [0.45, 0.1, 0.45], [0.45, 0.45, 0.1]] B = [[0.5, 0.5], [0.75, 0.25], [0.25, 0.75]] #pi = [0.333, 0.333, 0.333] #A = [[0.5, 0.3, 0.2], [0.2, 0.4, 0.4], [0.7, 0.2, 0.1]] #B = [[0.5, 0.5], [0.75, 0.25], [0.25, 0.75]] os_1 = [0, 0, 0, 0, 1, 0, 1, 1, 1, 1] os_2 = [0, 1, 0, 1, 1, 0, 1, 0, 1, 1] os = [] os.append(os_1) os.append(os_2) model = hmm(pi, A, B) #a, b, c = model.forward_with_scale(os) #c = model.forward(os) #model.baum_welch(os_1, 0.01) model.multi_os_baum_welch(os)