def precomputeDenominators( lookups ) : street = 3 kimo_buckets = range(globles.NBUCKETS[street-1]) all_kimo_pairs = cartProduct(kimo_buckets, kimo_buckets) PA,PAK,Ptrans = [lookups[i] for i in range(3)] count = 0 for kimo_pair in all_kimo_pairs : count += 1 if count < 100 : continue #TODO automate cluster size depending on street w/ query for cluster in range( 1,173 ) : #nclusters[street] ) : #TODO automate nacts = 1315 for action in range( 1,nacts+1) : p = P_ki_G_kimo_evdnc( street = street, \ ki = [42,42], \ kimo = kimo_pair, \ action_int = action, \ cluster_id = cluster, \ #evidence = [ '{ street : cboards}', \ #['agg_action_int','...' ] ], \ lookups = lookups, \ return_Z = True) if p > 0 : print "kimo, cluster, action", kimo_pair, cluster, action print "Prob: ", p #if( p < .0001 ) : assert False Pa = lookupPA( PA, street, action ) #Pkimo1 = lookupPk( street-1, kimo_pair[0] ) #Pkimo2 = lookupPk( street-1, kimo_pair[1] ) #Pb = lookupPb( street ) approx = Pa print "approv Prob: ", approx if approx < p : print "EURRRREREKEKAKKAKA"
def justAK( street, evidence, lookups ) : PA = lookups[0] PAK = lookups[1] action_int = evidence[1][street] ki_buckets = range(globles.NBUCKETS[street]) all_ki_pairs = cartProduct(ki_buckets, ki_buckets) assmnt_probs = {} Z = 0 pa = lookupPA( PA, street, action_int ) #print "street, action_int, P(a): " , street, action_int, pa for (k1,k2) in all_ki_pairs : pak = lookupPAK(PAK, street, k1, k2, action_int) pk1 = lookupPk( street, k1 ) pk2 = lookupPk( street, k2 ) prob = pak * pk1 * pk2 / pa Z += prob assmnt_probs[(k1,k2)] = prob return assmnt_probs
def searchOptimalParamters(dataPath="./Data/", gpuOptmized=True): print("LSTM Grid Search") noLayers = [1, 2] noNodes = [8, 16, 32] dropout = [0.0] regularization = [0.0001, 0.001] lookback = [32, 16] bidirectional = [True] convInputLayer = [True] kernelSize = [4, 6, 8, 10] noFilters = [4, 8, 15, 20, 30] poolSize = [2, 4, 6] hyperParameterSpace = list( cartProduct( noLayers, noNodes, dropout, regularization, lookback, bidirectional, convInputLayer, kernelSize, noFilters, poolSize, )) assetList = ["WMT", "AAPL", "ABT"] data = dataUtils.loadScaleDataMultivariate(assetList, dataPath) splitIndex = int(0.8 * len(data.scaledTimeSeries)) data.splitData(splitIndex) validationErrorMin = float("inf") optimalParameter = None trainingLog = pd.DataFrame(columns=[ "Time", "Architecture", "Bidirectional", "Validation Loss", "Training Loss", "Epochs", "Lookback", "Dropout", "Regularization", "Kernel Size", "No Filters", "Pooling Size", ]) totalIterations = len(hyperParameterSpace) for _ in range(totalIterations): if exists(saveLogPath): trainingLog = pd.read_csv(saveLogPath, sep=",", index_col=[0]) idx = len(trainingLog) noLayers = hyperParameterSpace[idx][0] noNodes = hyperParameterSpace[idx][1] dropout = hyperParameterSpace[idx][2] regularization = hyperParameterSpace[idx][3] lookback = hyperParameterSpace[idx][4] bidirectional = hyperParameterSpace[idx][5] convInputLayer = hyperParameterSpace[idx][6] kernelSize = hyperParameterSpace[idx][7] filters = hyperParameterSpace[idx][8] poolSize = hyperParameterSpace[idx][9] architecture = [] data.createLSTMDataSet(lookback) for _ in range(noLayers): architecture.append(noNodes) testLSTM = LSTMmodel( forecastHorizon=1, lookBack=lookback, architecture=architecture, dropout=dropout, regularization=regularization, bidirectional=bidirectional, convInputLayer=convInputLayer, kernelSize=kernelSize, filters=filters, poolSize=poolSize, ) testLSTM.createModel(gpuOptmized=gpuOptmized, noTimeSeries=data.noTimeSeries) trainingHistory = testLSTM.train(5000, data, saveModel=False, learningRate=0.1) validationError = trainingHistory.history["val_loss"][-1] trainingError = trainingHistory.history["loss"][-1] noEpochs = len(trainingHistory.history["val_loss"]) print( strftime("%Y-%m-%d %H:%M:%S", localtime()) + " " + str(idx) + " / " + str(totalIterations) + " Epochs: " + str(noEpochs)) print(str(architecture) + " " + str(np.round(validationError, 4))) trainingLog.loc[idx] = { "Time": strftime("%Y-%m-%d %H:%M:%S", localtime()), "Architecture": str(architecture), "Bidirectional": bidirectional, "Validation Loss": validationError, "Training Loss": trainingError, "Epochs": noEpochs, "Lookback": lookback, "Dropout": dropout, "Regularization": regularization, "Kernel Size": kernelSize, "No Filters": filters, "Pooling Size": poolSize, } trainingLog.to_csv(saveLogPath, sep=",") if validationError < validationErrorMin: validationErrorMin = validationError optimalParameter = hyperParameterSpace[idx] del testLSTM del trainingLog collect() print(optimalParameter)
for seq in tfSites.values(): seqs.append(str(seq.seq).upper()) motif = Motifs.create(seqs) pfm = DataFrame(motif.counts, index=range(1, 10), columns=['A', 'C', 'G', 'T']).transpose() pfm = pfm.reindex(['A', 'C', 'G', 'T']) pwm = pfm / pfm.sum() pwm = dfWrapper(pwm) Ps = {} #Initialize the Ps matrix for pos in range(1, 9): Ps[pos] = {} for firstBase, secondBase in cartProduct(['A', 'C', 'G', 'T'], ['A', 'C', 'G', 'T']): try: Ps[pos][firstBase][secondBase] = 0 except: Ps[pos][firstBase] = {secondBase: 0} #Count the number of times each base is found together for seq in seqs: for pos in range(0, 8): Ps[pos + 1][seq[pos]][seq[pos + 1]] += 1 #Scale to make a PW transition matrix transMat = {} for pos in range(1, 9): transMat[pos] = {} for firstBase, secondBase in cartProduct(['A', 'C', 'G', 'T'],
def P_ki_G_kipo_evdnc( street = -1, \ ki = (-1,-1), \ kipo = (-1,-1), \ #cluster_id = -1, \ #action_int = -1, \ evidence = [{},[]], \ lookups = ['PA','PAK','Ptrans'], \ no_Z = False ) : #unpack action_int = evidence[1][street] PA,PAK,Ptrans = [lookups[i] for i in range(3)] Pa = lookupPA( PA, street, action_int ) #This street must compute P(ki|Ai,bi) street_is_final = kipo == (-1,-1) if street_is_final : ki_p1, ki_p2 = ki[0], ki[1] Pak = lookupPAK( PAK, street, ki_p1, ki_p2, action_int ) Pk1 = lookupPk( street, ki_p1 ) Pk2 = lookupPk( street, ki_p2 ) Pk = Pk1 * Pk2 Pka = Pak * Pk / Pa return Pka return Pak #Rest of streets must compute P(ki|Ai,bi,kipo) else : kipo_p1 = kipo[0] kipo_p2 = kipo[1] cboards = evidence[0][street+1] conditional = lookupPtrans( Ptrans, street+1, cboards ) KIPO1 = lookupPk(street+1,kipo_p1) KIPO2 = lookupPk(street+1,kipo_p2) KIPO = KIPO1 * KIPO2 if no_Z : ki_p1,ki_p2 = ki K1 = lookupPk(street, ki_p1) K2 = lookupPk(street, ki_p2) BT1 = conditional[ki_p1][kipo_p1] BT2 = conditional[ki_p2][kipo_p2] AK = lookupPAK( PAK, street, ki_p1, ki_p2, action_int ) term = AK * K1 * K2 * BT1 * BT2 return term else : numerator = 0 Z = 0 terms = {} #all possible bucket assignments for the current street #used to compute the partition Z ki_buckets = range(globles.NBUCKETS[street]) all_ki_pairs = cartProduct(ki_buckets, ki_buckets) for (ki_p1,ki_p2) in all_ki_pairs : K1 = lookupPk(street, ki_p1) K2 = lookupPk(street, ki_p2) BT1 = conditional[ki_p1][kipo_p1] BT2 = conditional[ki_p2][kipo_p2] AK = lookupPAK( PAK, street, ki_p1, ki_p2, action_int ) term = AK * K1 * K2 * BT1 * BT2 #term = AK * BT1 * BT2 terms["%d,%d" % (ki_p1,ki_p2)] = term ###if this assignment is same as the one passed in if (ki_p1,ki_p2) == ki : numerator = term Z += term if Z == 0 : assert False else : return numerator / Z
def balanced_P_ki_G_evdnc( final_street, evidence, lookups, m ) : assert final_street != 0 if m % 2 == 0 : m += 1 ml_assmnts_by_street = [] for street in range( 1,final_street+1 ) : #assign probabilities to all assignments just based on this streets action assmnt_probs = justAK( street, evidence, lookups ) sassmnts = sorted( assmnt_probs.keys(), key=lambda kpair : assmnt_probs[kpair], reverse=True ) # fill up: street_assmnts = [] remaining_p1 = (m-1)/2 remaining_p2 = (m-1)/2 remaining_tie = 1 ix = 0 while not( remaining_p1 == 0 and remaining_p2 == 0 and remaining_tie == 0 ) : (k1,k2) = sassmnts[ix] do_append = False if k1 > k2 : if remaining_p1 > 0 : do_append = True remaining_p1 -= 1 elif k2 > k1 : if remaining_p2 > 0 : do_append = True remaining_p2 -= 1 else : if remaining_tie > 0 : do_append = True remaining_tie -= 1 #append the assignment and it's probability if do_append : street_assmnts.append( sassmnts[ix] ) ix += 1 print street, street_assmnts ml_assmnts_by_street.append( street_assmnts ) #poss_assmnts = [-1] #for street in range( 1, final_street+1 ): #poss_assmnts = cartProduct( poss_assmnts, ml_assmnts_by_street[street] ) #poss_assmnts = cartProduct( *ml_assmnts_by_street ) poss_assmnts = [BktAssmnt( [(-1,-1)]+list(t) ) for t in cartProduct( *ml_assmnts_by_street )] assmnt_probs = {} for assmnt in poss_assmnts : #print "assmnt after made:", str(assmnt) prob = 1 for street in range(3,0,-1) : ki = assmnt.get(street) kipo = assmnt.get(street+1) #print "ki,kipo", ki, kipo prob *= P_ki_G_kipo_evdnc( street, ki, kipo, \ evidence, lookups ) if prob == 0 : #print evidence #print "kipo to ki is prob() = 0", kipo, ki break #print prob if prob > 0 : assmnt_probs[assmnt] = prob return assmnt_probs
def pf_P_ki_G_evdnc( final_street, evidence, lookups, ms=[-1,50,50,100], no_Z = False ) : assert final_street != 0 #the m BktAssmnts we filter after each street particles = [BktAssmnt()] old_assignment_probs = { particles[0] : 1 } #now starting at final_street and doing "backward" inference begin_street = 1 for street in range( final_street, begin_street-1, -1 ) : #form an extended set of assignments, built on existing particles #will compute prob for each assignment, and take the m highest prob #as our new particles street_bkts = range(globles.NBUCKETS[street]) new_bkt_tuples = cartProduct( street_bkts, street_bkts ) ##compute prob of each assignmnt assignment_probs = {} for (assmnt, bkt_tuple) in cartProduct( particles, new_bkt_tuples ) : #print evidence, street P_tuple = P_ki_G_kipo_evdnc( street = street, \ ki = bkt_tuple, \ kipo = assmnt.get(street+1), \ #action_int = action_int, \ lookups = lookups, \ evidence = evidence, \ no_Z = no_Z ) #print "street, ki, action_int : ", street, bkt_tuple, action_int #print "P_tuple: ", P_tuple P_assmnt = old_assignment_probs[ assmnt ] P_assmnt = P_assmnt * P_tuple new_assmnt = assmnt.setAndCopy( street, bkt_tuple ) assignment_probs[ new_assmnt ] = P_assmnt #for count,assmnt in enumerate( assignments ) : ##there is room for speedup here ##we are re-computing each assignment each time ##stupid because each assignment is a product of terms ##if we store the previous terms we can simply multiple the ##new term to it #p = P_assmnt_G_evdnc( assmnt, evidence, lookups ) #assignment_probs[ str(assmnt) ] = p #particle filter step #print "street, begin:", street, begin_street if street > begin_street : sassignments = sorted( assignment_probs.keys(), \ key = lambda k: assignment_probs[k], \ reverse = True ) particles = [] mm = min( ms[street], len(sassignments) ) #print "mm:", mm for i in range(mm) : assmnt = sassignments[i] p = assignment_probs[assmnt] print "Assignment: ", str(assmnt), " has prob: ", p particles.append( sassignments[i] ) old_assignment_probs = assignment_probs return assignment_probs
# Get file name and order from input filename = sys.argv[1]; order = ord(sys.argv[2]) - ord('0'); # Load all Caylay tables tbls = CayleyTable.readAllTables(filename, order); # Run tests on each Cayley table for each permutation of the group set groupSet = tbls[0].symbols; #TODO: SET UP OUTPUT FILE tableNum = 0; count = 0; total = 0; for tbl in tbls: tableNum += 1; for (a, b, c) in cartProduct(groupSet, repeat=3): ab = tbl.simplifyTerm(a + b); ba = tbl.simplifyTerm(b + a); ac = tbl.simplifyTerm(a + c); ca = tbl.simplifyTerm(c + a); b_Scab = tbl.findLeftMultipleInSetProduct(b, c+a+b); #empty if not true c_cabS = tbl.findRightMultipleInSetProduct(c, c+a+b); #empty if not true if len(b_Scab) > 0 and len(c_cabS) > 0 and ab == ba and ac == ca: for (v, w) in cartProduct(b_Scab, c_cabS): vca = tbl.simplifyTerm(v + c + a); abw = tbl.simplifyTerm(a + b + w); if vca != abw: print tableNum; tbl.printTable(); print '(a = ' + a + ', b = ' + b + ', c = ' + c + ')'; print;