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(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_sample(num_nets, attr, I9, data_a, data_g, size_nets, sampleinfo, numsim, vec_numsim_kCD, theta_draws, theta_setup, sample_k, mcJump, jscid): flag_largest_scid = size_nets.index(max(size_nets)) == jscid #for jscid in range(num_nets): #print(f'Simulation {js+1} {(datetime.now()).strftime("%Y-%m-%d-%H:%M:%S")}') jattr = attr[jscid] jI9 = I9[jscid] jA = data_a[jscid] jG = data_g[jscid] nn = int(size_nets[jscid]) jkk = sample_k[jscid] numsim_kCD = vec_numsim_kCD[jscid] sim_a = np.zeros([numsim + 1, nn]) sim_g = np.zeros([numsim + 1, nn, nn]) sim_a[0, :] = jA sim_g[0, :, :] = jG np.random.seed(jscid + 2026642028) for js in range(numsim): theta_js = (theta_draws.iloc[js]).to_numpy() [vv, ww, h, phi1, phi2, psi, qhat] = theta2param(theta_js, jattr, theta_setup, sampleinfo) [jG1, jA1, p_th1_s1, p_th1_s0] = gen_kCD(vv, ww, h, phi1, phi2, psi, qhat, jI9, nn, jkk, mcJump, numsim_kCD, jG, jA) #print(f'fine={js:4d} prev={np.mean(jA1):5.4f}/{np.mean(jA):5.4f} density={np.mean(jG1.ravel()):5.4f}/{np.mean(jG.ravel()):5.4f}') sim_a[js + 1, :] = np.copy(jA1) #np.copy sim_g[js + 1, :, :] = np.copy(jG1) #Multiple setups #jG=np.copy(jG1) #jA=np.copy(jA1) if flag_largest_scid and js % 10 == 0: systime_t = datetime.now() print( f'Largest scid {jscid:3d} ({size_nets[jscid]:3d}) sim {js:4d}/{numsim:3d} time {systime_t.strftime("%Y-%m-%d-%H:%M:%S")}.' ) return [sim_a, sim_g, jscid]
def sim_ctrfPrice_jscid_jctrf(num_nets, attr, I9, data_a, data_g, size_nets, vec_numsim_kCD, thetastars, theta_setups, sampleinfo, sample_k, gridDeltaPrice, mcJump, numsim, filename, cpuinfo, jcpu): [ncpu, nscid, nctrf] = cpuinfo [jscid, jctrf] = Jcpu2JscidJctrf(jcpu, nscid, nctrf) [ theta_setupModel, theta_setupRestrictNet, theta_setupFixedNet, theta_setupNoNetData ] = theta_setups [ thetastarModel_draws, thetastarRestrictNet_draws, thetastarFixedNet_draws, thetastarNoNetData_draws ] = thetastars #for jprice,dPrice in enumerate(gridDeltaPrice): jprice = jctrf #dPrice = gridDeltaPrice[jprice] print(jcpu, jscid, jctrf) #if size_nets[jscid]==max(size_nets): # print(f'Largest school (scid={jscid:2d}, size={size_nets[jscid]:3d}) delta price = {dPrice:3.0f}, {(datetime.now()).strftime("%Y-%m-%d-%H:%M:%S")}') jattr = (attr[jscid]).copy() #fresh copy for each scenario jattr.price = jattr.price + gridDeltaPrice[jprice] jI9 = I9[jscid] nn = int(size_nets[jscid]) jkk = sample_k[jscid] nmc_state = vec_numsim_kCD[jscid] sim_a = np.zeros([numsim + 1, nn]) sim_g = np.zeros([numsim + 1, nn, nn]) sim_a[0, :] = np.copy(data_a[jscid]) sim_g[0, :, :] = np.copy(data_g[jscid]) # (1) Model #[vv,ww,h,phi,qhat,I9grade] = theta2param(thetastar,jattr,theta_setup,sampleinfo) jA = np.copy(data_a[jscid]) jG = np.copy(data_g[jscid]) for js in range(numsim): thetastar = (thetastarModel_draws.iloc[js]).to_numpy() [vv, ww, h, phi1, phi2, psi, qhat] = theta2param(thetastar, jattr, theta_setupModel, sampleinfo) np.random.seed(js * num_nets + jscid + 2026642028) [jG1, jA1, p_th1_s1, p_th1_s0] = gen_kCD(vv, ww, h, phi1, phi2, psi, qhat, jI9, nn, jkk, mcJump, nmc_state, jG, jA) sim_a[js + 1, :] = jA1 sim_g[js + 1, :, :] = jG1 simdata = [np.copy(sim_a), np.copy(sim_g), jprice, jscid] # (2a) Restrict network adjustmens: True estimates but Fixed net #[vv,ww,h,phi,qhat,I9grade] = theta2param(thetastarFixedNet,jattr,theta_setupFixedNet,sampleinfo) jA = np.copy(data_a[jscid]) jG = np.copy(data_g[jscid]) for js in range(numsim): thetastar = (thetastarRestrictNet_draws.iloc[js]).to_numpy() [vv, ww, h, phi1, phi2, psi, qhat] = theta2param(thetastar, jattr, theta_setupRestrictNet, sampleinfo) np.random.seed(js * num_nets + jscid + 2026642028) [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) sim_a[js + 1, :] = jA1 sim_g[js + 1, :, :] = jG1 #Multiple setups: v0 with reset & v1 without #jG=np.copy(jG1) #jA=np.copy(jA1) simdataRestrictNet = [np.copy(sim_a), np.copy(sim_g), jprice, jscid] # (2) Exogenous (fixed) network jA = np.copy(data_a[jscid]) jG = np.copy(data_g[jscid]) for js in range(numsim): thetastarFixedNet = (thetastarFixedNet_draws.iloc[js]).to_numpy() [vv, ww, h, phi1, phi2, psi, qhat] = theta2param(thetastarFixedNet, jattr, theta_setupFixedNet, sampleinfo) np.random.seed(js * num_nets + jscid + 2026642028) [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) sim_a[js + 1, :] = jA1 sim_g[js + 1, :, :] = jG1 simdataFixedNet = [np.copy(sim_a), np.copy(sim_g), jprice, jscid] # (3) Model with No Net Data (no local PE) #[vv,ww,h,phi,qhat,I9grade] = theta2param(thetastarNoPE,jattr,theta_setupNoPE,sampleinfo) jA = np.copy(data_a[jscid]) jG = np.copy(data_g[jscid]) for js in range(numsim): thetastarNoNetData = (thetastarNoNetData_draws.iloc[js]).to_numpy() [vv, ww, h, phi1, phi2, psi, qhat] = theta2param(thetastarNoNetData, jattr, theta_setupNoNetData, sampleinfo) np.random.seed(js * num_nets + jscid + 2026642028) [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) sim_a[js + 1, :] = jA1 sim_g[js + 1, :, :] = jG1 simdataNoNetData = [np.copy(sim_a), np.copy(sim_g), jprice, jscid] # COMPUTE STATS columnnames = ['scid', 'dprice', 'sim', 'netsize'] for jstats in [ 'prev', 'density', 'avgDeg', 'minDeg', 'maxDeg', 'AGA', 'IAGIA', 'tri', 'HI', 'CHI', 'FSI' ]: for jctrfLabel in ['Model', 'RestrictNet', 'FixedNet', 'noNetData']: columnnames.append(jstats + '-' + jctrfLabel) stats_ctrfPrice = pd.DataFrame(data=np.zeros([(numsim + 1), 48], dtype=float), columns=columnnames) for jscenario, ctrfdata in enumerate( [simdata, simdataRestrictNet, simdataFixedNet, simdataNoNetData]): [sim_a, sim_g, jprice, jscid] = ctrfdata for s in range(numsim + 1): A = sim_a[s] #subdimensional array G = sim_g[s] #subdimensional array n = len(A) stats_ctrfPrice.iloc[s, 0:4] = np.array([jscid, jprice, s, n]) jstats = np.array( stateStats(G, A, n)[:-1] + homophily(G, A, n, True)) jcolumns = [j for j in range(jscenario + 4, jscenario + 44 + 1, 4)] stats_ctrfPrice.iloc[s, jcolumns] = jstats return stats_ctrfPrice #end sim_ctrfPrice_jscid_jctrf
def sim_state_spills( num_nets, grid_nnosmoke, jattr0, I9, jA0, jG0, size_nets, vec_numsim_kCD, thetastar_draws, thetastarFixedNet_draws, thetastarNoNetData_draws, theta_setup, theta_setupFixedNet, theta_setupNoNetData, sampleinfo, sample_k, mcJump, numsim, jcpu): nscid=num_nets nctrf=3 [jscid,jctrf] = Jcpu2JscidJctrf(jcpu,nscid,nctrf) jI9 = I9[jscid].copy() nn = int(size_nets[jscid]) nnosmoke = np.round(nn*grid_nnosmoke[jscid]).astype(int) jkk = sample_k[jscid] nmc_state = vec_numsim_kCD[jscid] jA = np.copy(jA0) jG = np.copy(jG0) # Endog net sim_a=np.zeros([numsim+1, nn]) sim_g=np.zeros([numsim+1, nn, nn]) sim_a[0,:] = np.copy(jA) sim_g[0,:,:] = np.copy(jG) print(jcpu, jscid, jctrf) # MODEL if jctrf==0: for js in range(numsim): np.random.seed(js+2026642028) ids_nosmoke= np.sort(np.random.permutation(nn)[:nnosmoke]) jattr = jattr0.copy() #fresh copy for each scenario jA = np.copy(jA0) jG = np.copy(jG0) jA[ids_nosmoke]=0 if thetastar_draws.loc[js,'vPrice']>0: jattr.loc[ids_nosmoke,'price']=-1e12 #jattr.price[jattr.id.isin(ids_nosmoke)] without index=id-1 else: jattr.loc[ids_nosmoke,'price']=1e12 # if thetastar_draws.loc[js,'vIncome']>0: # jattr.loc[ids_nosmoke[jscid],'income']=-1e12 # else: # jattr.loc[ids_nosmoke[jscid],'income']=1e12 thetastar=(thetastar_draws.iloc[js]).to_numpy() [vv,ww,h,phi1,phi2,psi,qhat] = theta2param(thetastar,jattr,theta_setup,sampleinfo) np.random.seed(js+2026642028) [jG1,jA1,p_th1_s1,p_th1_s0] = gen_kCD(vv,ww,h,phi1,phi2,psi,qhat,jI9, nn,jkk,mcJump,nmc_state, jG,jA) sim_a[js+1,:] = jA1.copy() sim_g[js+1,:,:] = jG1.copy() # EXOG (FIXED) NET elif jctrf==1: for js in range(numsim): np.random.seed(js+2026642028) ids_nosmoke= np.sort(np.random.permutation(nn)[:nnosmoke]) jattr = jattr0.copy() #fresh copy for each scenario jA = np.copy(jA0) jG = np.copy(jG0) jA[ids_nosmoke]=0 if thetastar_draws.loc[js,'vPrice']>0: jattr.loc[ids_nosmoke,'price']=-1e12 #jattr.price[jattr.id.isin(ids_nosmoke)] without index=id-1 else: jattr.loc[ids_nosmoke,'price']=1e12 # if thetastar_draws.loc[js,'vIncome']>0: # jattr.loc[ids_nosmoke[jscid],'income']=-1e12 # else: # jattr.loc[ids_nosmoke[jscid],'income']=1e12 thetastarFixedNet = (thetastarFixedNet_draws.iloc[js]).to_numpy() [vv,ww,h,phi1,phi2,psi,qhat] = theta2param(thetastarFixedNet,jattr,theta_setupFixedNet,sampleinfo) np.random.seed(js+2026642028) [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) sim_a[js+1,:] = jA1.copy() sim_g[js+1,:,:] = jG1.copy() # C NO NET DATA elif jctrf==2: for js in range(numsim): np.random.seed(js+2026642028) ids_nosmoke= np.sort(np.random.permutation(nn)[:nnosmoke]) jattr = jattr0.copy() #fresh copy for each scenario jA = np.copy(jA0) jG = np.copy(jG0) jA[ids_nosmoke]=0 if thetastar_draws.loc[js,'vPrice']>0: jattr.loc[ids_nosmoke,'price']=-1e12 #jattr.price[jattr.id.isin(ids_nosmoke)] without index=id-1 else: jattr.loc[ids_nosmoke,'price']=1e12 # if thetastar_draws.loc[js,'vIncome']>0: # jattr.loc[ids_nosmoke[jscid],'income']=-1e12 # else: # jattr.loc[ids_nosmoke[jscid],'income']=1e12 thetastarNoNetData = (thetastarNoNetData_draws.iloc[js]).to_numpy() [vv,ww,h,phi1,phi2,psi,qhat] = theta2param(thetastarNoNetData,jattr,theta_setupNoNetData,sampleinfo) np.random.seed(js+2026642028) [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) sim_a[js+1,:] = jA1.copy() sim_g[js+1,:,:] = jG1.copy() more_info = [jscid,jctrf,nn,numsim] jctrfSpills = get_stats(sim_a,sim_g,jattr0,more_info) return jctrfSpills
def sim_state_spills_old( num_nets, grid_nnosmoke, jattr0, I9, jA0, jG0, size_nets, vec_numsim_kCD, thetastar_draws, thetastarFixedNet_draws, thetastarNoNetData_draws, theta_setup, theta_setupFixedNet, theta_setupNoNetData, sampleinfo, sample_k, mcJump, numsim, jscid): jI9 = I9[jscid].copy() nn = int(size_nets[jscid]) nnosmoke = np.round(nn*grid_nnosmoke[jscid]).astype(int) jkk = sample_k[jscid] nmc_state = vec_numsim_kCD[jscid] jA = np.copy(jA0) jG = np.copy(jG0) # Endog net sim_a=np.zeros([numsim+1, nn]) sim_g=np.zeros([numsim+1, nn, nn]) sim_a[0,:] = np.copy(jA) sim_g[0,:,:] = np.copy(jG) # Fixed net sim_a_fixG=np.zeros([numsim+1, nn]) sim_g_fixG=np.zeros([numsim+1, nn, nn]) sim_a_fixG[0,:] = np.copy(jA) sim_g_fixG[0,:,:] = np.copy(jG) # No net data sim_a_noNet=np.zeros([numsim+1, nn]) sim_g_noNet=np.zeros([numsim+1, nn, nn]) sim_a_noNet[0,:] = np.copy(jA) sim_g_noNet[0,:,:] = np.copy(jG) for js in range(numsim): np.random.seed(js+2026642028) ids_nosmoke= np.sort(np.random.permutation(nn)[:nnosmoke]) jattr = jattr0.copy() #fresh copy for each scenario jA = np.copy(jA0) jG = np.copy(jG0) jA[ids_nosmoke]=0 if thetastar_draws.loc[js,'vPrice']>0: jattr.loc[ids_nosmoke,'price']=-1e12 #jattr.price[jattr.id.isin(ids_nosmoke)] without index=id-1 else: jattr.loc[ids_nosmoke,'price']=1e12 # if thetastar_draws.loc[js,'vIncome']>0: # jattr.loc[ids_nosmoke[jscid],'income']=-1e12 # else: # jattr.loc[ids_nosmoke[jscid],'income']=1e12 # A MODEl thetastar=(thetastar_draws.iloc[js]).to_numpy() [vv,ww,h,phi1,phi2,psi,qhat] = theta2param(thetastar,jattr,theta_setup,sampleinfo) np.random.seed(js+2026642028) [jG1,jA1,p_th1_s1,p_th1_s0] = gen_kCD(vv,ww,h,phi1,phi2,psi,qhat,jI9, nn,jkk,mcJump,nmc_state, jG,jA) sim_a[js+1,:] = jA1.copy() sim_g[js+1,:,:] = jG1.copy() # B FIXED (EXOG) NET thetastarFixedNet = (thetastarFixedNet_draws.iloc[js]).to_numpy() [vv,ww,h,phi1,phi2,psi,qhat] = theta2param(thetastarFixedNet,jattr,theta_setupFixedNet,sampleinfo) np.random.seed(js+2026642028) [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) sim_a_fixG[js+1,:] = jA1.copy() sim_g_fixG[js+1,:,:] = jG1.copy() # C NO NET DATA thetastarNoNetData = (thetastarNoNetData_draws.iloc[js]).to_numpy() [vv,ww,h,phi1,phi2,psi,qhat] = theta2param(thetastarNoNetData,jattr,theta_setupNoNetData,sampleinfo) np.random.seed(js+2026642028) [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) sim_a_noNet[js+1,:] = jA1.copy() sim_g_noNet[js+1,:,:] = jG1.copy() if (jscid==0 and js%10==9): print(f'Simulation {js+1} {(datetime.now()).strftime("%Y-%m-%d-%H:%M:%S")}') more_info=[jscid,nn,numsim] jctrfSpills = get_stats(sim_a,sim_g,jattr0,more_info) if (jscid==0): print(f'ctrfSpills stats computed {(datetime.now()).strftime("%Y-%m-%d-%H:%M:%S")}') jctrfSpills_fixG = get_stats(sim_a_fixG,sim_g_fixG,jattr0,more_info) if (jscid==0): print(f'ctrfSpills_fixG stats computed {(datetime.now()).strftime("%Y-%m-%d-%H:%M:%S")}') jctrfSpills_noNet = get_stats(sim_a_noNet,sim_g_noNet,jattr0,more_info) if (jscid==0): print(f'ctrfSpills_noNet stats computed {(datetime.now()).strftime("%Y-%m-%d-%H:%M:%S")}') #print(f'completed scid={jscid}') return jctrfSpills,jctrfSpills_fixG,jctrfSpills_noNet
def sim_state(num_nets, attr, I9, data_a, data_g, size_nets, vec_numsim_kCD, thetastar_draws, theta_setup, sampleinfo, sample_k, mcJump, numsim, filename, jscid): simdata=[] jattr = (attr[jscid]).copy() #fresh copy for each scenario jI9 = I9[jscid] nn = int(size_nets[jscid]) jkk = sample_k[jscid] nmc_state = vec_numsim_kCD[jscid] sim_a=np.zeros([numsim+1, nn]) sim_g=np.zeros([numsim+1, nn, nn]) sim_a[0,:] = np.copy(data_a[jscid]) sim_g[0,:,:] = np.copy(data_g[jscid]) # (1) Model jA = np.copy(data_a[jscid]) jG = np.copy(data_g[jscid]) for js in range(numsim): thetastar=(thetastar_draws.iloc[js]).to_numpy() [vv,ww,h,phi1,phi2,psi,qhat] = theta2param(thetastar,jattr,theta_setup,sampleinfo) np.random.seed(js*num_nets+jscid+2026642028) [jG1,jA1,p_th1_s1,p_th1_s0] = gen_kCD(vv,ww,h,phi1,phi2,psi,qhat,jI9, nn,jkk,mcJump,nmc_state, jG,jA) sim_a[js+1,:] = jA1 sim_g[js+1,:,:] = jG1 #Multiple setups: v0 with reset & v1 without #jG=np.copy(jG1) #jA=np.copy(jA1) simdata.append([np.copy(sim_a), np.copy(sim_g), jscid]) columnnames = ['scid','sim','netsize','race', 'prev', 'density', 'avgDeg', 'minDeg', 'maxDeg', 'AGA', 'IAGIA', 'tri', 'HI','CHI','FSI'] stats_ctrfSchool = pd.DataFrame(data=np.zeros([(numsim+1),15],dtype=float),columns=columnnames) for s in range(numsim+1): A=sim_a[s] #subdimensional array G=sim_g[s] #subdimensional array n=len(A) race=np.mean(jattr.race.to_numpy(float),dtype=float) stats_ctrfSchool.iloc[s,0:4] = np.array([jscid,s,n,race]) stats_ctrfSchool.iloc[s,4:15]= np.array(stateStats(G,A,n)[:-1] + homophily(G,A,n,True)) return stats_ctrfSchool