def _RU(Xc, PE_cr, fixORopt): tm_st = PE_cr['tm_st'] tm_ed = PE_cr['tm_ed'] if (_isNullP(PE_cr)): if (DBG0): tl.msg("%s ...... _RU: (null)" % (IDT_E)) else: cid = PE_cr['cid'] # put cid md_c = tl.dcopy(PE_cr['md']) # copy model md_c.n = len(Xc) md_c.data = Xc # copy data if (fixORopt == 'OPT'): md_c = md_c.fit_si(W_RR, DPS) # update init si (i.e., s(0)=si) (Sc, Vc) = md_c.gen() # generate events errV = tl.RMSE(Xc, Vc) md_c.fn = "seg:t%d-%d_e%.2f" % (tm_st, tm_ed, errV) if (DBG0): tl.msg("%s ...... _RU: errV:%f (cid:%d)" % (IDT_E, errV, cid)) PE_cr = { 'md': md_c, 'cid': cid, 'Vc': Vc, 'Sc': Sc, 'siset': {}, 'tm_st': tm_st, 'tm_ed': tm_ed, 'errV': errV } return PE_cr
def _update_md_ms_reset_cIDs(mdb): mdb_org=tl.dcopy(mdb) mdb.cid_counter=0 # new_mdb counter = 0,1,... # create new MD_IDs MD_IDs={}; counter=1 for i in range(1,mdb_org.cid_counter+1): if(i in mdb_org.MD): MD_IDs[i]=counter; counter+=1 # replace ID (from oldID to newID) for i in MD_IDs: oldID=i newID=MD_IDs[i] # MD (node), replace from oldID to newID del(mdb.MD[oldID]) mdb.MD[newID]=mdb_org.MD[oldID] mdb.cid_counter+=1 for j in MD_IDs: oldID2=j newID2=MD_IDs[j] oldIdx="%d_%d"%(oldID, oldID2) newIdx="%d_%d"%(newID, newID2) # MS (edge) if(oldIdx in mdb_org.MS): del(mdb.MS[oldIdx]) mdb.MS[newIdx]=mdb_org.MS[oldIdx] return (mdb, MD_IDs)
def _remove_redundancy(candi, smin, MULTI_TRANS): #----------------------# if (candi == []): return candi # if no any candidates, return [] #----------------------# lmin = smin # minimum length of over-lapping candidates #----------------------# # (1) sort candidate errs = [] for i in range(0, len(candi)): errs.append(candi[i]['err']) #idx_best=np.argmin(errs) # find best id idx_sort = np.argsort(errs) # sorted id-list candi_new = tl.dcopy(candi) for i in range(0, len(idx_sort)): candi_new[i] = candi[idx_sort[i]] candi = candi_new candi_new = [] #----------------------# # (2) if best-fit only, then return best candidate if (not MULTI_TRANS): candi_new.append(candi.pop(0)) return candi_new #----------------------# # (3) check redundant candidates while (len(candi) > 0): c_i = candi.pop(0) want_del = tl.NO for c_j in candi_new: # if any over-lapping candidates if (c_i['idx'] == c_j['idx'] and abs(c_i['cpt'] - c_j['cpt']) <= lmin): if (c_i['err'] > c_j['err']): want_del = tl.YES if (not want_del): candi_new.append(c_i) #----------------------# return candi_new
def _create_multiscale_seqs(Xorg, lstep, mscale_h, outdir): tl.msg("_create_multiscale_seq (lstep: %d, mscale_h: %d)"%(lstep, mscale_h)) tl.save_seq_txt_pdf(Xorg, "%sXorg"%outdir) # multi-scale sequences XHs={}; Xagg=tl.dcopy(Xorg); outdirHs={} wdHs={} # window size list for i in range(0,mscale_h): # window size at i-th level if(i<mscale_h-1): wdHs[i]=lstep*(2**(mscale_h-1-i)) # if HT=4: 3,2,1 ... (i.e., 8xlstep, 4xlstep, 2xlstep, 1) #if(i<mscale_h-1): wdHs[i]=lstep*(2**(mscale_h-2-i)) # if HT=4: 2,1,0 ... (i.e., 4xlstep, 2xlstep, lstep, 1) #if(i<mscale_h-1): wdHs[i]=lstep*(2**(mscale_h-3-i)) # if HT=4: 1,0,-1 ... (i.e., 2xlstep, lstep, 1/2xlstep, 1 else: wdHs[i]=1 # if, last seq for i in range(0,mscale_h): XHs[i]=tl.smoothWMAo(Xagg, wdHs[i]) # compute i-th level Xagg=Xagg-XHs[i] # delete current scale for i in range(0,mscale_h): #XHs[i]= XHs[i][int(wdHs[0]):,:] # delete longest-window XHs[i][0:int(wdHs[0]),:]=tl.NAN # delete longest-window outdirHs[i]='%s_Level%d/'%(outdir,i); tl.mkdir(outdirHs[i]) # directory # save sequence at i-th level tl.save_txt(XHs[i], '%sXorg.txt'%(outdirHs[i])) # save data tl.plt.clf(); tl.plt.plot(XHs[i]) # plot and save figure tl.savefig("%sXorg"%(outdirHs[i]), 'pdf') tl.msg("wdHs: ") tl.msg(wdHs) return (XHs, wdHs, outdirHs)
def _FM(mdb, Xc, lene, smin, PE, fixORopt): # dictMS (see, om_trans.py) dictMS = { 'cid': PE['cid'], 'si': tl.dcopy(PE['md'].si), 'rho': mdb.rho_MS, 'smin': smin, 'errV': tl.INF } if (fixORopt == 'OPT'): dictMS = om_trans.fit_forward(Xc, mdb, dictMS, W_FM) #, DPS) (Ve, Se, Re) = om_trans.gen_forward_multi_trans(Xc, mdb, dictMS, lene, MULTI_TRANS) #(Ve, Se, Re)=om_trans.gen_forward(mdb, dictMS, lene) errV = tl.RMSE(Xc, Ve[0:len(Xc)]) if (_isUnfit(Ve, mdb)): Ve[:] = np.nan Se[:] = np.nan Re[:] = REID_NULL errV = tl.INF if (DBG0): tl.msg("%s ++++++ _FM(%s): errV:%f (cid:%d) " % (IDT_F, fixORopt, errV, dictMS['cid'])) PF = {'Ve': Ve, 'Se': Se, 'Re': Re, 'errV': errV} return PF
def _fit_optk(nlds, wtype, TH=0, linearfit=False): data = nlds.data NLDSs = [] Errs = [] if (DBG and NTRIAL > 1): tl.warning("nlds.py --- NTRIAL:%d" % (NTRIAL)) # for each k, estimate opt-params for k in KLIST: if (k > nlds.kmax): break for trial in range(0, NTRIAL): # init model (k) nlds_k = tl.dcopy(nlds) # linear fitting (nlds, err) = _fit_k_lin(nlds_k, k) NLDSs.append(nlds_k) err = err + tl.DELTA * k if (DBG): print("k:%d,trial:%d, err=%f" % (k, trial, err)) Errs.append(err) if (len(Errs) > NTRIAL and Errs[-1 - NTRIAL] <= Errs[-1]): break #notfin if (np.min(Errs) <= TH): break kbst = np.argmin(Errs) if (DBG): print(Errs) if (DBG): print(Errs[kbst], NLDSs[kbst].k) nlds = NLDSs[kbst] # non-linear fit if (linearfit == False): nlds = nlds.fit_A2(wtype) nlds = nlds.fit_si(wtype) if (DBG): nlds.plot("%s_best_%d" % (nlds.fn, nlds.k)) return nlds
def _get_trans_candidates(Vs, mdb, idx_c, smin, rho, MULTI_TRANS): candi = [] # MS candidates (i.e., errors, trans_time, MS, idxs(from_to)) #--------------------------------------# for i in mdb.MD: idxs = "%d_%d" % (idx_c, i) if (idxs in mdb.MS): msset = mdb.MS[idxs] #--------------------------------------# for ii in range(0, len(msset)): msset_ii = tl.dcopy(msset[ii]['medoid']) if (msset_ii['active'] == tl.NO): continue # if, this ms is not active, then, just ignore.. # here, ms_rcd={idx_fr, idx_to, cpt, active, md_fr, vn_fr, md_to, si_to} msset_ii['idx_fr'] = idx_c msset_ii['idx_to'] = i (t_best_ii, err_ii) = _find_nearest_t(Vs, msset_ii['vn_fr'], smin) #if(DBG): tl.eprint("%s, %d, %d, err:%f, (rho=%f)"%(idxs, ii, t_best_ii, err_ii,rho)) if (err_ii <= rho): candi.append({ 'err': err_ii, 'cpt': t_best_ii, 'ms': msset_ii, 'idx': idxs, 'ii': ii }) #--------------------------------------# # remove redundant candidates candi = _remove_redundancy(candi, smin, MULTI_TRANS) #for i in range(len(candi)): tl.eprint("%s, %d, %f"%(candi[i]['idx'], candi[i]['cpt'], candi[i]['err'])) return candi
def nl_fit(nlds, ftype, wtype, dps): nlds_org = tl.dcopy(nlds) #--------------------------------# if (ftype != 'si' and ftype != 'A2' and ftype != 'A01' and ftype != 'B01'): tl.warning("fit.py: usage: [si/M2/A01/B01]") #--------------------------------# if (ftype == 'si'): # if si, i.e., arg{s(0)=si} global MAXFEV MAXFEV = MAXFEVi global XTL XTL = XTLi global FTL FTL = FTLi #--------------------------------# if (DBG): tl.comment("=== start LMfit ===") if (DBG): tl.comment("(%s,%s,%d)" % (ftype, wtype, dps)) if (DBG): tl.msg("rmse: %f" % _distfunc_rmse(nlds)) if (DBG): tl.comment("start fitting") #--------------------------------# if (DBG): tic = time.clock() nlds = _nl_fit(nlds, ftype, wtype, dps) if (DBG): toc = time.clock() fittime = toc - tic #--------------------------------# if (_distfunc_rmse(nlds_org) < _distfunc_rmse(nlds)): nlds = nlds_org #notfin if (DBG): tl.comment("end fitting") if (DBG): tl.msg("time: %f" % fittime) if (DBG): tl.msg("rmse: %f" % _distfunc_rmse(nlds)) if (DBG): tl.comment("=== end LMfit ===") #--------------------------------# return nlds
def _RT(Xc, mdb, smin, PE_cr): PE_vc = tl.dcopy(PE_cr) PE_vs = [] lenc = len(Xc) dictMS = { 'cid': PE_cr['cid'], 'si': tl.dcopy(PE_cr['md'].si), 'rho': mdb.rho_MS, 'smin': smin, 'errV': tl.INF } (Ve, Se, Re) = om_trans.gen_forward(mdb, dictMS, lenc) errV = tl.RMSE(Xc, Ve[0:lenc]) #tl.eprint("_RT: before:%f -> after:%f"%(PE_cr['errV'], errV)) #-------------------------# #--- find first cpt ------# cpt = -1 for t in range(0, lenc): if (Re[t] == -1): cpt = t break if (cpt == -1 or PE_cr['errV'] <= errV): return (PE_vc, PE_vs) # if it cannot find better-trans, just ignore #-------------------------# # update params {PE_vc : PE_vs} #tl.eprint("tm_st:%d, tm_ed:%d, cpt:%d"%(PE_vc["tm_st"], PE_vc["tm_ed"], cpt)) PE_vc['tm_ed'] = PE_vc['tm_st'] + cpt PE_vs = { 'md': mdb.MD[Re[cpt + 1]]['medoid'], 'cid': Re[cpt + 1], 'Vc': Ve[cpt + 1:, ], 'Sc': Se[cpt + 1:, ], 'siset': {}, 'tm_st': PE_vc['tm_st'] + (cpt + 1), 'tm_ed': PE_cr['tm_ed'], 'errV': errV } #tl.eprint("tm_st:tm_ed: %d %d %d %d"%(PE_vc['tm_st'], PE_vc['tm_ed'], PE_vs['tm_st'], PE_vs['tm_ed'])) # compute errorV PE_vc['errV'] = tl.RMSE(Xc[0:cpt], Ve[0:cpt]) PE_vs['errV'] = tl.RMSE(Xc[(cpt + 1):lenc], Ve[(cpt + 1):lenc]) #tl.eprint("err_vc:%f - err_vs:%f (rho:%f)"%(PE_vc['errV'], PE_vs['errV'], mdb.rho_RE)) return (PE_vc, PE_vs)
def _find_opt_cut(Xorg, PE_vp, PE_vc, lenw, lmin): tp_st = PE_vp['tm_st'] tc_ed = PE_vc['tm_ed'] # start/end points tcut = PE_vp['tm_ed'] # current cut-point errs = [] locs = [] # find best-cut-position t_st = max(tp_st + lmin, tcut - int(lenw / 2)) t_ed = min(tc_ed - lmin, tcut + int(lenw / 2)) PE_vps = [] PE_vcs = [] if (t_st == t_ed): return (PE_vp, PE_vc) for loc in range(t_st, t_ed, int(max(1, (t_ed - t_st) / OPTCNT))): PE_vpi = tl.dcopy(PE_vp) PE_vci = tl.dcopy(PE_vc) PE_vpi['tm_ed'] = loc PE_vci['tm_st'] = loc PE_vpi = _RU(Xorg[tp_st:loc, :], PE_vpi, 'OPT') PE_vci = _RU(Xorg[loc:tc_ed, :], PE_vci, 'OPT') err = PE_vpi['errV'] + PE_vci['errV'] PE_vps.append(PE_vpi) PE_vcs.append(PE_vci) locs.append(loc) errs.append(err) if (DBG0): tl.msg("%s ...... _find_opt_cut l:%d (%f + %f = %f)" % (IDT_E, loc, PE_vpi['errV'], PE_vci['errV'], err)) ibest = np.argmin(errs) locbest = locs[ibest] if (DBG0): tl.msg("%s ...... _find_opt_cut l:%d (%f + %f = %f)" % (IDT_E, locbest, PE_vps[ibest]['errV'], PE_vcs[ibest]['errV'], err)) PE_vp = PE_vps[ibest] PE_vc = PE_vcs[ibest] return (PE_vp, PE_vc)
def _md_absorb_each(mdb, Snaps, cid_i): mdi=mdb.MD[cid_i] Xci=mdi['medoid'].data Si=[]; dps=Snaps['DPS'] # find most similar MD in MDB (except current cid_i) del(mdb.MD[cid_i]) # remove cid_i(temporal) (Vc, md_best, Si, cid_j)=mdb.search_md(Xci, Si, dps) mdb.MD[cid_i]=mdi # add cid_i # if, cannot find, then, do nothing if(cid_j == -1): return (mdb, Snaps) if(DBG): tl.msg("om_mdb: md_absorb_each: cid_i:%d,cid_j:%d, (c=%d), errC:%f, rho:%f"%(cid_i,cid_j, mdb.get_c(),md_best.rmse(),mdb.rho_RE)) # if, it finds mdj=mdb.MD[cid_j] # copy model mdi_m=tl.dcopy(mdi['medoid']) mdj_m=tl.dcopy(mdj['medoid']) # switch data i,j mdi_m.data=mdj['medoid'].data mdj_m.data=mdi['medoid'].data mdi_m.fit_si(W_RR,dps) mdj_m.fit_si(W_RR,dps) # compute errors (i vs j) err_i=mdi_m.rmse() err_j=mdj_m.rmse() if(DBG): tl.msg("om_mdb: switchErr: (%d,%d) erri:%f errj:%f"%(cid_i, cid_j, err_i, err_j)) if(err_i<err_j and err_i<mdb.rho_RE): if(DBG): tl.msg("md_absorb: REPLACE(absorb) %d->%d"%(cid_j,cid_i)) mdb.MD[cid_i]['object'].append(mdj) # add regime j (object) -> regime i mdb=_update_md_ms_delete_idx(mdb, cid_j) # delete regime j MD_IDs={}; MD_IDs[cid_j]=cid_i; Snaps=_init_MD_ID_Snaps(Snaps, MD_IDs) # Snaps: replace j -> i if(err_j<err_i and err_j<mdb.rho_RE): if(DBG): tl.msg("md_absorb: REPLACE(absorb) %d->%d"%(cid_i,cid_j)) mdb.MD[cid_j]['object'].append(mdi) # add regime j (object) -> regime i mdb=_update_md_ms_delete_idx(mdb, cid_i) # delete regime i MD_IDs={}; MD_IDs[cid_i]=cid_j; Snaps=_init_MD_ID_Snaps(Snaps, MD_IDs) # Snaps: replace i -> j return (mdb, Snaps)
def _search_md(mdb, Xc, siset, dps): if(mdb.get_c()==0): return ([], [], [], -1) # no data #----------------------------------------------# siset_new={} err_best=tl.INF; cid_best=-1; md_best=[] # for each regime/medoid, try argmin_si(Xc-Vc) for i in mdb.MD: md_c=tl.dcopy(mdb.MD[i]['medoid']) md_c.data=Xc; md_c.n=np.size(Xc,0) if(i in siset): md_c.si= siset[i]['si'] # use previous si md_c=md_c.fit_si(W_RR, dps) err_c=md_c.rmse() siset_new[i]={'cid':i, 'md':md_c, 'si':md_c.si, 'err':err_c} if(err_best > err_c or err_best==tl.INF): err_best=err_c; md_best=md_c; cid_best=i; (Sc,Vc)=md_best.gen() #----------------------------------------------# return (Vc, md_best, siset_new, cid_best)
def _em(model, ITER=ITERf, iter_each=ITERe): observations= tl.dcopy(model.data) if(np.sum(np.isnan(observations))>0): tl.warning("kf.py --- em: isnan->0") observations[np.isnan(observations)]=0 observations+=tl.ZERO*np.random.rand(model.n, model.d) kf=model.kf if(DBG): tl.msg("kf.em: d=%d, n=%d, k=%d"%(model.d, model.n, model.k)) if(DBG): tl.msg("start EM algorithm ...") lhs= np.zeros(ITER) for i in range(len(lhs)): kf = kf.em(X=observations, n_iter=iter_each) lhs[i] = model.LH(observations) #tl.msg("iter:%d/%d, LH= %f"%(i+1,ITER,lhs[i])) if(DBG): tl.msg("iter:%d/%d (%d)"%(i+1,ITER, iter_each)) if(i>1 and lhs[i-1]>lhs[i]): break if(DBG): tl.msg("end") return kf
def _md_medoid_refinement_each(mdb, Snaps, cid_i): dps=Snaps['DPS'] Xorg=Snaps['Xorg'] md_c=tl.dcopy(mdb.MD[cid_i]['medoid']) # (a) find longest segment (tm_st, tm_ed) = _find_longest_segment(Snaps, cid_i) if(tm_st==-1 and tm_ed==-1): return (mdb, Snaps) # set current window Xc=Xorg[tm_st:tm_ed,:]; len_Xc=tm_ed-tm_st # (b) try current model vs. new model # (b-1) current model md_c.data=Xc md_c=md_c.fit_si(W_RR, dps) # si-fit using current param (Sc,Vc)=md_c.gen(len_Xc); err_cr=tl.RMSE(Xc,Vc) # (b-2) new model (estimate param using Xc) md_new=nl.NLDS(Xc, "medoid-refinement-t:%d:%d"%(tm_st, tm_ed)) md_new.data=Xc md_new=md_new.fit(W_RE) # lmfit (regime creation) (Sc,Vc)=md_new.gen(len_Xc); err_new=tl.RMSE(Xc,Vc) # (c) if it finds better model, then replace if(err_new < err_cr): md_c=md_new return (mdb, Snaps)
def _O_estimator(tc, Xorg, PE_vb, PE_vp, PE_vc, mdb, Snaps): if (mdb.CC is tl.NO): return (PE_vb, PE_vp, PE_vc, mdb, Snaps ) # if, no causal-chain, then ignore lstep = Snaps['lstep'] pstep = Snaps['pstep'] wd_level = Snaps['wd_level'] lmin = min(LMAX, max(LMIN, int(np.ceil(lstep * LM_R)))) smin = min(LMAX, max(LMIN, int(np.ceil(lstep * SMIN_R)))) lmax = LMAX PE_vc_prev = tl.dcopy(PE_vc) # current-best-previous PE_vc['tm_ed'] = tc # currrent time tick if (PE_vc['tm_ed'] - PE_vc['tm_st'] < lmin): return (PE_vb, PE_vp, PE_vc, mdb, Snaps) # create current window Xc Xc = Xorg[PE_vc['tm_st']:PE_vc['tm_ed'], :] if (COMMENT): tl.msg( "%s |--- O_estimator: (wd=%d) t=%d:%d:%d:%d (rho:%f)-----------------|" % (IDT_E, wd_level, PE_vp['tm_st'], PE_vp['tm_ed'], PE_vc['tm_st'], PE_vc['tm_ed'], mdb.rho_RE)) #-----------------------------------------------# # (I) find good-fit regime #-----------------------------------------------# #-----------------------------# # (A) regime update #-----------------------------# PE_RU = _RU(Xc, PE_vc, 'FIX') # fixed, simply extend seg PE_vc = PE_RU if (PE_vc['errV'] > mdb.rho_RE): # if fixed is not good-enough PE_RU = _RU(Xc, PE_vc, 'OPT') # update init value if (PE_RU['errV'] <= PE_vc['errV']): PE_vc = PE_RU #-----------------------------# # (B) regime-trans update (if, current is known but not good-fit) #-----------------------------# PE_vs = [] if (PE_vc['cid'] != -1 and PE_vc['errV'] > mdb.rho_RE): (PE_RT_vc, PE_RT_vs) = _RT(Xc, mdb, smin, PE_vc) if (PE_RT_vc['errV'] < mdb.rho_RE and PE_RT_vs['errV'] < mdb.rho_RE): PE_vc = PE_RT_vc PE_vs = PE_RT_vs #-----------------------------# # (C) regime reader, if update is not good-enough #-----------------------------# if (PE_vc['cid'] == -1 or (PE_vc['errV'] > mdb.rho_RE)): PE_RR = _RR(Xc, mdb, PE_vc) if (PE_RR['errV'] <= PE_vc['errV']): PE_vc = PE_RR #-----------------------------# # (D) regime creation (estimator), only if the previous-fit is unknown (i.e., splitted) and current regime is not in MDB #-----------------------------# if (PE_vc_prev['cid'] == -1 and PE_vc['errV'] > mdb.rho_RE): PE_RE = _RE(Xc, PE_vc['tm_st'], PE_vc['tm_ed']) if (PE_RE['errV'] <= PE_vc['errV']): PE_vc = PE_RE #-----------------------------------------------# # (II) split regime, if there is a new cut-point #-----------------------------------------------# #-----------------------------# lenc = PE_vc['tm_ed'] - PE_vc['tm_st'] #-----------------------------# # split segment now, if required #-----------------------------# if ((PE_vc['errV'] > mdb.rho_RE and lenc > lmin) or # if current best is not-fit seg (PE_vs != [] and lenc > lmin) or # if any transisions (lenc > lmax) or # if too long segment (PE_vc['tm_ed'] + pstep >= Snaps['n'])): # if it's close to the end point #-----------------------------# PE_vc = PE_vc_prev # use current-best-previous #if(DBG0): tl.eprint("%s |-------- split segment: t=%d:%d, errV=%f (rho=%f)"%(IDT_E, PE_vc['tm_st'], PE_vc['tm_ed'], PE_vc['errV'], mdb.rho_RE)) #----------------------------------------------------# # (d-a) if, good-enough-fit, then insert it into mdb if (PE_vc['errV'] <= mdb.rho_RE * RHO_MIN_R): # (d-a-1) insert Xc into mdb (mdb, PE_vc) = mdb.update_md_insert(PE_vc) # (d-a-2) insert MS(PE_vp, PE_vc) into mdb, if, we have PE_vp, PE_vc if ((not _isNullP(PE_vp)) and (not _isNullP(PE_vc))): # (d-a-2-i) find opt-cut-point (+-pstep, argmin ||Vp-Xp||+||Vc-Xc||) (PE_vp, PE_vc) = _find_opt_cut(Xorg, PE_vp, PE_vc, lstep, lmin) #pstep) # (d-a-2-ii) insert new regime-shift-dynamics into mdb mdb = mdb.update_ms_insert(PE_vp, PE_vc) # add (from to) # (d-a-2-iii) update estimated events # previous set Snaps['Ve_full'][ PE_vp['tm_st']:PE_vp['tm_ed'], :] = PE_vp['Vc'] #Snaps['Se_full'][PE_vp['tm_st']:PE_vp['tm_ed'],:PE_vp['md'].k]=PE_vp['Sc'] Snaps['Re_full'][PE_vp['tm_st']:PE_vp['tm_ed']] = PE_vp['cid'] Snaps['Re_full'][ PE_vp['tm_st']] = REID_CPT # cut-point (regime-switch) # current set Snaps['Ve_full'][ PE_vc['tm_st']:PE_vc['tm_ed'], :] = PE_vc['Vc'] #Snaps['Se_full'][PE_vc['tm_st']:PE_vc['tm_ed'],:PE_vc['md'].k]=PE_vc['Sc'] Snaps['Re_full'][PE_vc['tm_st']:PE_vc['tm_ed']] = PE_vc['cid'] Snaps['Re_full'][ PE_vc['tm_st']] = REID_CPT # cut-point (regime-switch) #if(DBG0): tl.eprint("%s |-------- split segment(opt-cut): t=%d:%d, errV=%f (rho=%f)"%(IDT_E, PE_vc['tm_st'], PE_vc['tm_ed'], PE_vc['errV'], mdb.rho_RE)) #----------------------------------------------------# # (d-b) reset params PE_vb = PE_vp PE_vp = PE_vc PE_vc = _initP() if (PE_vs != []): PE_vc = PE_vs PE_vc['tm_st'] = PE_vp['tm_ed'] #-----------------------------# # return current results return (PE_vb, PE_vp, PE_vc, mdb, Snaps)
def run_est_single(Xorg, lstep, outdir, wd_level=1): #--------------------------------# want_refinement = tl.YES #--------------------------------# mdb_cb = om_mdb.MDB('%s' % outdir) # create modelDB Snaps_cb = init_Snaps(Xorg, lstep, wd_level) # SnapShots mdb_cb = mdb_cb.update_Xminmax(Xorg) errF_cb = tl.INF #; Snaps_cb=[]; if (RHO_INIT == -1): rho_iter = _compute_rho_min(Xorg, lstep) else: rho_iter = RHO_INIT ERRs_full = [] ERRs_cb_i = [] outdir_tr = "%s_trials/" % outdir if (SAVE_TRIALS): tl.mkdir(outdir_tr) # create directory tl.msg("(%d) run_est_single: start trial (find opt rho) ..." % wd_level) for i_trial in range(0, MAXTRIAL): #--------------------------------# mdb_i = tl.dcopy(mdb_cb) Snaps_i = tl.dcopy(Snaps_cb) #--------------------------------# # set rho, cleansing (e.g., remove inappropriate regimes), etc. mdb_i.update_rho(rho_iter) (mdb_i, Snaps_i) = mdb_i.update_apoptosis(Snaps_i, AP_MD, AP_MS, want_refinement) mdb_i = mdb_i.init_objects() #--------------------------------# # start OrbitMap (mdb_i, Snaps_i) = _OrbitMap(Xorg, lstep, wd_level, mdb_i, "%sTrial_%d_" % (outdir_tr, i_trial), SAVE_TRIALS, SAVE_TRIALS) #tl.YES) #tl.NO) #--------------------------------# if (i_trial > 0 and Snaps_i['errF_half'] < errF_cb): # update best-fit (if trial > 0) errF_cb = Snaps_i['errF_half'] mdb_cb = tl.dcopy(mdb_i) Snaps_cb = tl.dcopy(Snaps_i) #--------------------------------# tl.msg( "(mscale-wd: %d) Trial %d rho=%f, c=%d, errE: %f, errF(half):%f, errF(full):%f" % (wd_level, i_trial, rho_iter, mdb_i.get_c(), Snaps_i['errE'], Snaps_i['errF_half'], Snaps_i['errF'])) ERRs_full.append([ i_trial, rho_iter, mdb_i.get_c(), Snaps_i['errE'], Snaps_i['errF_half'], Snaps_i['errF'] ]) tl.save_txt(ERRs_full, "%s_trials_results.txt" % (outdir)) #--------------------------------# if (EARLYSTOP and Snaps_i['errF_half'] >= errF_cb and i_trial >= MINTRIAL): break # if, no more better-fit, break if (mdb_i.get_c() <= 1 and i_trial > 0): break # if, # of regimes is less than one, then, stop #--------------------------------# #--------------------------------# rho_iter *= RHO_ITER_R_BU # rho: try larger value #--------------------------------# # final-best-fit (mdb_cb, Snaps_cb) = mdb_cb.update_apoptosis(Snaps_cb, AP_MD, AP_MS, want_refinement) mdb_cb = mdb_cb.init_objects() if (Snaps_cb['errF_half'] > Snaps_cb['errS']): mdb_cb.CC = tl.NO # if, not approp. (no causalchain), ignore model (mdb_cb, Snaps_cb) = _OrbitMap(Xorg, lstep, wd_level, mdb_cb, "%s" % (outdir), tl.YES, tl.YES) tl.msg("best est: c=%d, errE: %f, errF(half):%f, errF(full):%f" % (mdb_cb.get_c(), Snaps_cb['errE'], Snaps_cb['errF_half'], Snaps_cb['errF'])) return (mdb_cb, Snaps_cb)
def _gen_trans(Stack, Final, misc, MULTI_TRANS): # pop Stc in stack Stc = Stack.pop(0) # get params from Stack (t_c, idx_c, md_c, si, Se, Ve, Re) = _get_params_Stc(Stc) # get params from misc setting (t_e, mdb, rho, smin, dps) = _get_params_misc(misc) #if(DBG): tl.eprint("t_c:%d, t_e:%d, idx_c:%d, smin:%d, rho:%f"%(t_c, t_e, idx_c, smin, rho)) #--------------------------------------# # 0. set current regime variables #--------------------------------------# (Ss, Vs) = md_c.forward(si, (t_e - t_c + 1), dps) Se[t_c:t_e, 0:md_c.k] = Ss[0:t_e - t_c, :] Ve[t_c:t_e, :] = Vs[0:t_e - t_c, :] Re[t_c:t_e] = idx_c #--------------------------------------# Stc_c = _create_Stc(t_c, idx_c, md_c, si, Se, Ve, Re) #--------------------------------------# # (if it went to the end point (i.e., te-tc<smin) ) if (t_e - t_c < smin): Final.append(Stc_c) return (Stack, Final) #--------------------------------------# # 1. find candidate set : best-shift from md_c to ms_best #--------------------------------------# candi = _get_trans_candidates( Vs, mdb, idx_c, smin, rho, MULTI_TRANS) # MS candidates (i.e., err, ms, cpt, idx) #--------------------------------------# # (if no any close-enough vector, then return) if (len(candi) == 0): Final.append(Stc_c) return (Stack, Final) #--------------------------------------# #--------------------------------------# # 2. for each candidate in candi, then shift it # (candi: if best-shift is good enough (<=rho) and length is long enough), #--------------------------------------# for i in range(0, len(candi)): Stc_new = tl.dcopy(Stc_c) (t_c, idx_c, md_c, si, Se, Ve, Re) = _get_params_Stc(Stc_new) # best candidate err_i = candi[i]['err'] ms_i = candi[i]['ms'] cpt_i = t_c + candi[i]['cpt'] #--------------------------------------# # 2-1. compute shifted-trajectory #--------------------------------------# if (DBG): tl.eprint("|---@@@@@@@@@@@@@--- (cpt=%d, err=%f) %d >>> %d" % (cpt_i, err_i, ms_i['idx_fr'], ms_i['idx_to'])) #--------------------------------------# if (DBG_CPT): Re[cpt_i - 1] = REID_CPT idx_c = ms_i['idx_to'] md_c = ms_i['md_to'] # i.e., =mdb.MD[idx_c]['medoid'] si = ms_i['si_to'] (s, o) = md_c.forward(si, 1, dps) Se[cpt_i, 0:md_c.k] = s Ve[cpt_i, :] = o Re[cpt_i] = idx_c t_c = cpt_i + 1 #--------------------------------------# Stc_new = _create_Stc(t_c, idx_c, md_c, si, Se, Ve, Re) Stack.append(Stc_new) #--------------------------------------# # return candidates (Stack and Final set) return (Stack, Final)