def run_sim(num_net, nets_inhm_attr, I9, nets_a, nets_g, vec_size_net, vec_nmc_state, theta0, theta1, theta_setup, sampleinfo, sample_k, mcJump, filename, jsim, jscid): np.random.seed(jsim * num_net + jscid + 2026642028) jattr = nets_inhm_attr[jscid] jI9 = I9[jscid] jA = nets_a[jscid] jG = nets_g[jscid] nn = int(vec_size_net[jscid]) jkk = sample_k[jscid] nmc_state = vec_nmc_state[jscid] [vv, ww, h, phi1, phi2, psi, qhat] = theta2param(theta1, jattr, theta_setup, sampleinfo) [jG1, jA1, p_th1_s1, p_th1_s0] = gen_kCD_fixG(vv, ww, h, phi1, phi2, psi, qhat, jI9, nn, jkk, mcJump, nmc_state, jG, jA) ## Could be more efficient. [vv, ww, h, phi1, phi2, psi, qhat] = theta2param(theta0, jattr, theta_setup, sampleinfo) p_th0_s1 = potentialCwrap(vv, ww, h, phi1, phi2, psi, qhat, jI9, jG1, jA1, nn) p_th0_s0 = potentialCwrap(vv, ww, h, phi1, phi2, psi, qhat, jI9, jG, jA, nn) # if jsim>0 and jsim%5000==0: # state2pickle(filename+'-kCD-state--scid-'+str(jscid+1).zfill(2)+'--sim-'+str(jsim+1).zfill(6),jG1,jA1) # #print(f'Saving {posteriorfile}') p_dTh_s1 = p_th1_s1 - p_th0_s1 p_dTh_s0 = p_th1_s0 - p_th0_s0 return (p_dTh_s0 - p_dTh_s1) #,mcstats
def gen_kCD_fixG(vv, ww, h, phi1, phi2, psi, qhat, I9, nn, kkin, mcJump, nmc, G0, A0): """ Markov chain from the model via kCD * mcJump prob large jumps inverting G and A * large k acceptance is less likely """ ## Random variables #(a) meetings i veci = (np.random.randint(0, nn, [nmc])).astype(np.int32) #(b) meeting dimensions nkk = kkin.shape[0] #kkin sample for dimensions of k if nkk > 1: veck = kkin[np.random.randint(0, nkk, [nmc])] else: veck = np.ones([nmc]) * kkin veck = veck.astype(np.int32) #(c) proposal mode proposal = np.random.uniform(0, 1, [nmc]) #(d) accept lnaccept = np.log(np.random.uniform(0, 1, [nmc])) ## Prep the data. G = np.copy(G0) A = np.copy(A0) p = potentialCwrap(vv, ww, h, phi1, phi2, psi, qhat, I9, G, A, nn) p0 = np.copy(p) TempG1 = np.copy(G) #only for fixed G G1 = np.copy(G) #only for fixed G ## Initializations & misc dP = 0 p1 = 0 lnPbar = 0 index_set = np.arange(nn) for ss in range(nmc): ## Draw a meeting i,jset. ihat = veci[ss] k = veck[ss] feasible_jj = index_set[ihat != index_set] jj = feasible_jj[np.random.permutation(nn - 1)[:k - 1]] ## Propose. if proposal[ss] < mcJump: #G1 = 1-G #np.fill_diagonal(G1,0) A1 = 1 - A p1 = potentialCwrap(vv, ww, h, phi1, phi2, psi, qhat, I9, G1, A1, nn) else: A1 = np.copy(A) #G1 = np.copy(G) #keep this draw to maintain parallel randomness with other estiamtion scenarios TempG1[ihat, jj] = np.random.binomial(1, 0.5, [k - 1]).astype(np.float64) #G1[jj,ihat] = G1[ihat,jj].T A1[ihat] = np.random.binomial(1, 0.5, 1).astype(np.float64) dP = deltaPotentialCwrap(vv, ww, h, phi1, phi2, psi, qhat, I9, G, G1, A, A1, ihat, nn) p1 = p + dP lnPbar = p1 - p if lnaccept[ss] < lnPbar or lnPbar > 0: A = A1 #G = G1 p = np.copy(p1) return G, A, p, p0
def gen_kCD_checks(vv, ww, h, phi, psi, qhat, I9, nn, kkin, mcJump, nmc, G0, A0): """ Markov chain from the model via kCD * mcJump prob large jumps inverting G and A * large k acceptance is less likely """ ## Prep meetings veci = (np.random.randint(0, nn, [nmc])).astype(np.int32) nkk = kkin.shape[0] #kkin sample for dimensions of k if nkk > 1: veck = kkin[np.random.randint(0, nkk, [nmc])] else: veck = np.ones([nmc]) * kkin veck = veck.astype(np.int32) ## Prep the data. G = np.copy(G0) A = np.copy(A0) p = potentialCwrap(vv, ww, h, phi, psi, qhat, I9, G, A, nn) p0 = np.copy(p) ## Draw acceptance lnaccept = np.log(np.random.uniform(0, 1, [nmc])) ## Miscellanea & inits. index_set = np.arange(nn) dP = 0 p1 = 0 lnPbar = 0 for ss in range(nmc): ## Draw a meeting i,jset. ihat = veci[ss] k = veck[ss] feasible_jj = index_set[ihat != index_set] jj = feasible_jj[np.random.permutation(nn - 1)[:k - 1]] ## Propose. if np.random.uniform() < mcJump: G1 = 1 - G np.fill_diagonal(G1, 0) A1 = 1 - A p1 = potentialCwrap(vv, ww, h, phi, psi, qhat, I9, G1, A1, nn) else: A1 = np.copy(A) G1 = np.copy(G) G1[ihat, jj] = (np.random.uniform(0, 1, [k - 1]) < 0.5).astype( np.float64) G1[jj, ihat] = G1[ihat, jj].T A1[ihat] = float(np.random.uniform(0, 1) < 0.5) # #CHECK SYMMETRIC # if np.any(np.abs(G1-G1.T) > 0.5): # print('Proposal not symmetric') # #Next 4 lines change the parameters # #to illustrate precision discrepancies # vv=vv*0 #vv=np.ones(nn)*0.2 # if np.sum(np.iscomplex(vv))>0: # print('Complex') # if np.isnan(np.sum(vv))>0: # print('NA') # # ww= np.ones([nn,nn])*1.0; np.fill_diagonal(ww,0) # ww= np.ones([nn,nn])*0; np.fill_diagonal(ww,0) # h=0.1 # phi=1 #phi*0 #? # psi=1 # qhat=1 #qhat*0 #no problem #G1=G #A1=A dP = deltaPotentialCwrap(vv, ww, h, phi, psi, qhat, I9, G, G1, A, A1, ihat, nn) p1 = p + dP # p1_check = potentialCwrap(vv,ww,h,phi,psi,qhat,I9,G1,A1,nn) # p0_check = potentialCwrap(vv,ww,h,phi,psi,qhat,I9,G,A,nn) # if abs(dP-(p1_check-p0_check))>1e-12: # print(f'dP={dP:10.4f} , p1check-p0check={p1_check-p0_check:10.4f}, error={dP-(p1_check-p0_check):10.8f}') # # if (abs(np.trace(G0)) + abs(np.trace(G1)))>1e-12: # print(f'Trace 0/1 = {np.trace(G0)}, {np.trace(G1)}') # #print(f'P1={p1:10.4f} , P1check={p1_check:10.4f}, dP={dP:10.4f}/dPP={p1_check-p0_check:10.4f}, error={abs(p1-p1_check):10.4f}') lnPbar = p1 - p if lnaccept[ss] < lnPbar or lnPbar > 0: A = A1 G = G1 p = np.copy(p1) return G, A, p, p0