### initial import, assumed to be closest to the original code; obtained from...

`initial import, assumed to be closest to the original code; obtained from /fs3/group/jonasgrp/Jose/SONET`
parents
calc_rhos.cpp 0 → 100644
 #include #include #include #include #include #include #include #include "calc_rhos.hpp" using namespace std; // function and structure declarations double integrate_gaussian(double rho0, void *parameters); struct Qfunction_params { double xth, rho; }; struct int_gauss_params{ double p1, p2, sec_mom; }; //////////////////////////////////////////////////// // calc_rho_given_alpha // need to calculate value of rho, the correlation // between two Gaussian random variables // so that the covariance of the resulting Bernoulli // variables will be alpha*p1*p2 // solve this 1D equation for rho numerically // (The variance of each Gaussian is determined by // the requirement that the Bernoulli obtained by // thresholding at 1 has probability pj of being 1.) ///////////////////////////////////////////////////// double calc_rho_given_alpha(double p1, double p2, double alpha, int &status) { // if correlation or a probability is zero, return rho=0 if(alpha==0 || p1==0 || p2==0) { status = 0; return 0.0; } if(alpha < -1) { cerr << "alpha < -1, cannot calc rho" << endl; status = -1; return 0.0; } // desired second moment among bernoulli random variables double b_sec_mom = p1*p2*(1+alpha); // set up gsl function FF to point to function // integrate_gaussian with // parameters determined by arguments p and bcorr struct int_gauss_params para; para.p1 = p1; para.p2 = p2; para.sec_mom = b_sec_mom; gsl_function FF; FF.function = &integrate_gaussian; FF.params = ¶ int iter=0, max_iter=1000; double rho=0; // we know rho has to be in interval [x_lo,x_hi] double x_lo = -1, x_hi=1; if (alpha > 0) x_lo=0; else x_hi=0; // Initialize solver and iterate until converge to solution const gsl_root_fsolver_type *T=gsl_root_fsolver_brent; gsl_root_fsolver *s = gsl_root_fsolver_alloc(T); gsl_root_fsolver_set(s, &FF, x_lo, x_hi); do { iter++; status = gsl_root_fsolver_iterate (s); if(status) break; rho = gsl_root_fsolver_root (s); x_lo = gsl_root_fsolver_x_lower (s); x_hi = gsl_root_fsolver_x_upper (s); status = gsl_root_test_interval (x_lo, x_hi, 0, 0.00001); } while (status == GSL_CONTINUE && iter < max_iter); gsl_root_fsolver_free (s); return rho; } // Q function, when integrated from xth to infinity, // gives the second moment of the Bernoulli random variables double Qfunction(double x, void *parameters){ struct Qfunction_params *para = (struct Qfunction_params *)parameters; double rho = para->rho; double xth = para->xth; double Q=gsl_cdf_gaussian_Q(xth-rho*x, sqrt(1-rho*rho)); double g= 1/sqrt(2*M_PI)*exp(-0.5*x*x)*Q; return g; } // Take the integral of the Gaussian that corresponds to the // second moment of the Bernoulli random variables. // Subtract off their required value so that function is // zero when found correct value of rho double integrate_gaussian(double rho0, void *parameters){ struct int_gauss_params * para = (struct int_gauss_params *) parameters; double p1=para->p1; double p2=para->p2; double sec_mom=para->sec_mom; double rho=rho0; double xth1=gsl_cdf_ugaussian_Qinv(p1); double xth2=gsl_cdf_ugaussian_Qinv(p2); struct Qfunction_params qparams; qparams.xth=xth1; qparams.rho=rho; gsl_function G; G.function = &Qfunction; G.params = &qparams; gsl_integration_workspace *ww = gsl_integration_workspace_alloc(1000); double res, err; gsl_integration_qagiu(&G, xth2, 1e-7, 1e-7, 1000, ww, &res, &err); gsl_integration_workspace_free (ww); res -= sec_mom; return res; }
calc_rhos.hpp 0 → 100644
 double calc_rho_given_alpha(double p1, double p2, double alpha, int &status);
This diff is collapsed.
 int calc_sqrtcov_given_rhos (int N_pops, int *N_nodes, double *sigma, double *rho_recip, double *rho_conv, double *rho_div, double *rho_chain, double *sqrt_diag, double *sqrt_recip, double *sqrt_conv, double *sqrt_div, double *sqrt_chain);
This diff is collapsed.
 int calc_sqrtcov_given_rhos (int N, double p, double rho_recip, double rho_conv, double rho_div, double rho_chain, double rho_noshare, double &sqrt_diag, double &sqrt_recip, double &sqrt_conv, double &sqrt_div, double &sqrt_chain, double &sqrt_noshare);
This diff is collapsed.
 int calc_sqrtcov_given_rhos (int N[], double (*p), double (*rho_recip), double (*rho_conv), double (*rho_div), double (*rho_chain), double (*sqrt_diag), double (*sqrt_recip), double (*sqrt_conv), double (*sqrt_div), double (*sqrt_chain));
calc_stats_1p.cpp 0 → 100644
 #include #include "calc_stats_1p.hpp" //////////////////////////////////////////////////////////////////// // calc_phat_alphahat_1p // // Calculate first and second order connectivity statistics from a // network adjacency matrix W with N_node nodes //////////////////////////////////////////////////////////////////// int calc_phat_alphahat_1p(gsl_matrix *W, int N_nodes, double &phat, double &alphahat_recip, double &alphahat_conv, double &alphahat_div, double &alphahat_chain, double &alphahat_other) { double N_edge, N_recip, N_conv, N_div, N_chain, N_other; calc_N_motifs(W, N_nodes, N_edge, N_recip, N_conv, N_div, N_chain, N_other); double c1 = N_nodes*(N_nodes-1.0); double c2 = N_nodes*(N_nodes-1.0)*(N_nodes-2.0)/2.0; double c3 = N_nodes*(N_nodes-1.0)*(N_nodes-2.0)*(N_nodes-3.0)/2.0; phat =N_edge/c1; alphahat_recip =N_recip/(c1/2.0*phat*phat)-1; alphahat_conv = N_conv/(c2*phat*phat)-1; alphahat_div = N_div/(c2*phat*phat)-1; alphahat_chain = N_chain/(2*c2*phat*phat)-1; alphahat_other = N_other/(c3*phat*phat)-1; return 0; } //////////////////////////////////////////////////////////////////// // calc_N_motifs // // Calculate number of edges and two edges motifs from a // network adjacency matrix W with N_node nodes //////////////////////////////////////////////////////////////////// int calc_N_motifs(gsl_matrix *W, int N_nodes, double &N_edge, double &N_recip, double &N_conv, double &N_div, double &N_chain, double &N_other) { double *thedata = W->data; size_t tda = W->tda; double row_sums[N_nodes]; double column_sums[N_nodes]; double W_square_trace=0.0; N_edge=0; for(int i=0; i
calc_stats_1p.hpp 0 → 100644
 #include int calc_phat_alphahat_1p(gsl_matrix *W, int N_nodes, double &phat, double &alphahat_recip, double &alphahat_conv, double &alphahat_div, double &alphahat_chain, double &alphahat_other); int calc_N_motifs(gsl_matrix *W, int N_nodes, double &N_edge, double &N_recip, double &N_conv, double &N_div, double &N_chain, double &N_other);
calc_stats_2p.cpp 0 → 100644
This diff is collapsed.
calc_stats_2p.hpp 0 → 100644
 #include int calc_N_sec(gsl_matrix *W, int N_nodes, double (*N_edge), double (*N_recip), double (*N_conv), double (*N_div), double (*N_chain)); int calc_phat_alphahat(gsl_matrix *W, int N_nodes, double (*phat), double (*alphahat_recip), double (*alphahat_conv), double (*alphahat_div), double (*alphahat_chain)); int calc_gaus_covs(gsl_matrix *W, int N_nodes, double (*sigma), double (*cov_recip), double (*cov_conv), double (*cov_div), double (*cov_chain), double &cov_other);
 #include #include #include #include #include #include #include #include "secorder_rec_1p.hpp" #include "calc_stats_1p.hpp" using namespace std; int main(int argc, char *argv[]) { // optional flags int deterministic_seed = 0; // if nonzero, use for random num gen seed /////////////////////////////////////////////////////////////// // Read fourteen input parameters // N_nodes number of nodes in the network // p the probability of any one given connection // alpha_recip_{min/max/n} // alpha_conv_{min/max/n} // alpha_div_{min/max/n} // alpha_chain_{min/max/n} /////////////////////////////////////////////////////////////// if(argc !=15) { cerr << "Requires eight parameters: N_nodes p alpha_recip_{min/max/n} alpha_conv_{min/max/n} alpha_div_{min/max/n} alpha_chain_{min/max/n} \n"; exit(-1); } int N_nodes = atoi(argv); double p = atof(argv); double alpha_recip_min=atof(argv); double alpha_recip_max=atof(argv); int alpha_recip_n=atoi(argv); double alpha_conv_min=atof(argv); double alpha_conv_max=atof(argv); int alpha_conv_n=atoi(argv); double alpha_div_min=atof(argv); double alpha_div_max=atof(argv); int alpha_div_n=atoi(argv); double alpha_chain_min=atof(argv); double alpha_chain_max=atof(argv); int alpha_chain_n=atoi(argv); cout << "Generating " << alpha_recip_n*alpha_conv_n*alpha_div_n*alpha_chain_n << " networks with\n" << "alpha_recip in [" << alpha_recip_min << ", " << alpha_recip_max << "]\n" << "alpha_conv in [" << alpha_conv_min << ", " << alpha_conv_max << "]\n" << "alpha_div in [" << alpha_div_min << ", " << alpha_div_max << "]\n" << "alpha_chain in [" << alpha_chain_min << ", " << alpha_chain_max << "]\n"; gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937); // set the seed // if deterministic_seed, use that for seed if(deterministic_seed) gsl_rng_set(r,deterministic_seed); // else use time in seconds for the seed else gsl_rng_set(r, time(NULL)); double d_alpha_recip=(alpha_recip_max-alpha_recip_min)/alpha_recip_n; double d_alpha_conv=(alpha_conv_max-alpha_conv_min)/alpha_conv_n; double d_alpha_div=(alpha_div_max-alpha_div_min)/alpha_div_n; double d_alpha_chain=(alpha_chain_max-alpha_chain_min)/alpha_chain_n; for(int i_recip=0; i_recip < alpha_recip_n; i_recip++) for(int i_conv=0; i_conv < alpha_conv_n; i_conv++) for(int i_div=0; i_div < alpha_div_n; i_div++) for(int i_chain=0; i_chain < alpha_chain_n; i_chain++) { // randomly generated alpha values Latin Hypercube intervals cout << "\nLatin hypercube: [" << alpha_recip_min+i_recip*d_alpha_recip << ", " << alpha_recip_min+(i_recip+1)*d_alpha_recip << "] x [" << alpha_conv_min+i_conv*d_alpha_conv << ", " << alpha_conv_min+(i_conv+1)*d_alpha_conv << "] x [" << alpha_div_min+i_div*d_alpha_div << ", " << alpha_div_min+(i_div+1)*d_alpha_div << "] x [" << alpha_chain_min+i_chain*d_alpha_chain << ", " << alpha_chain_min+(i_chain+1)*d_alpha_chain << "]\n"; // try multiple times to get a matrix in this hypercube int N_tries = 100; for(int j=0; j< N_tries; j++) { double alpha_recip = gsl_ran_flat(r, alpha_recip_min+i_recip*d_alpha_recip, alpha_recip_min+(i_recip+1)*d_alpha_recip); double alpha_conv = gsl_ran_flat(r, alpha_conv_min+i_conv*d_alpha_conv, alpha_conv_min+(i_conv+1)*d_alpha_conv); double alpha_div = gsl_ran_flat(r, alpha_div_min+i_div*d_alpha_div, alpha_div_min+(i_div+1)*d_alpha_div); double alpha_chain = gsl_ran_flat(r, alpha_chain_min+i_chain*d_alpha_chain, alpha_chain_min+(i_chain+1)*d_alpha_chain); if(alpha_chain <= -3) { cout << "Using min alpha_chain\n"; } else if(alpha_chain <= -2) { cout << "Using max alpha_chain\n"; } gsl_matrix *W; W=secorder_rec_1p(N_nodes,p, alpha_recip, alpha_conv, alpha_div, alpha_chain, r); if(W) { // matrix files will be stored in data directory // with filename determined by the six input parameters mkdir("data",0755); // make data directory, if it doesn't exist char FNbase; sprintf(FNbase,"_%i_%1.3f_%1.3f_%1.3f_%1.3f_%1.3f",N_nodes,p,alpha_recip, alpha_conv, alpha_div, alpha_chain); char FN; FILE *fhnd; strcpy(FN, "data/w"); strcat(FN, FNbase); strcat(FN, ".dat"); fhnd = fopen(FN, "w"); if(fhnd==NULL) { cerr << "Couldn't open outfile file " << FN << "\n"; exit(-1); } for(int i=0; i
makefile 0 → 100644
 CC = g++ C_OPTIMIZE_SWITCH = -O2 -DHAVE_INLINE -DGSL_RANGE_CHECK_OFF LIBS = -lgsl -lgslcblas CFLAGS = -Wall \${C_OPTIMIZE_SWITCH} run_secorder: run_secorder.o secorder_rec_1p.o calc_sqrtcov_rec_1p.o calc_rhos.o calc_stats_1p.o \${CC} run_secorder.o secorder_rec_1p.o calc_sqrtcov_rec_1p.o calc_rhos.o calc_stats_1p.o -o run_secorder \${LIBS} run_secorder_gephi: run_secorder_gephi.o secorder_rec_1p.o calc_sqrtcov_rec_1p.o calc_rhos.o calc_stats_1p.o \${CC} run_secorder_gephi.o secorder_rec_1p.o calc_sqrtcov_rec_1p.o calc_rhos.o calc_stats_1p.o -o run_secorder_gephi \${LIBS} gen_mult_secorder: gen_mult_secorder.o secorder_rec_1p.o calc_sqrtcov_rec_1p.o calc_rhos.o calc_stats_1p.o \${CC} gen_mult_secorder.o secorder_rec_1p.o calc_sqrtcov_rec_1p.o calc_rhos.o calc_stats_1p.o -o gen_mult_secorder \${LIBS} run_secorder_2p: run_secorder_2p.o secorder_rec_2p.o calc_sqrtcov_rec_2p.o calc_rhos.o calc_stats_2p.o \${CC} run_secorder_2p.o secorder_rec_2p.o calc_sqrtcov_rec_2p.o calc_rhos.o calc_stats_2p.o -o run_secorder_2p \${LIBS} run_ei_balanced: run_ei_balanced.o secorder_rec_2p.o calc_sqrtcov_rec_2p.o calc_rhos.o calc_stats_2p.o \${CC} run_ei_balanced.o secorder_rec_2p.o calc_sqrtcov_rec_2p.o calc_rhos.o calc_stats_2p.o -o run_ei_balanced \${LIBS} run_secorder_gen: run_secorder_gen.o secorder_gen.o calc_sqrtcov_gen.o calc_rhos.o calc_stats_2p.o calc_stats_1p.o \${CC} run_secorder_gen.o secorder_gen.o calc_sqrtcov_gen.o calc_rhos.o calc_stats_2p.o calc_stats_1p.o -o run_secorder_gen \${LIBS} testsoinput: testsoinput.o calc_rhos.o secorder_input.o \${CC} testsoinput.o calc_rhos.o secorder_input.o -o testsoinput \${LIBS} testsoinput.o: secorder_input.hpp calc_rhos.o secorder_input.o: secorder_input.hpp calc_rhos.o run_secorder.o: secorder_rec_1p.hpp calc_stats_1p.hpp run_secorder_gephi.o: secorder_rec_1p.hpp calc_stats_1p.hpp gen_mult_secorder.o: secorder_rec_1p.hpp calc_stats_1p.hpp run_secorder_2p.o: secorder_rec_2p.hpp calc_stats_2p.hpp run_ei_balanced.o: secorder_rec_2p.hpp calc_stats_2p.hpp run_secorder_gen.o: secorder_gen.hpp calc_stats_2p.hpp secorder_rec_1p.o: secorder_rec_1p.hpp calc_sqrtcov_rec_1p.hpp calc_rhos.hpp secorder_rec_2p.o: secorder_rec_2p.hpp calc_sqrtcov_rec_2p.hpp calc_rhos.hpp calc_stats_2p.hpp secorder_gen.o: secorder_gen.hpp calc_sqrtcov_gen.hpp calc_rhos.hpp calc_sqrtcov_rec_1p.o: calc_sqrtcov_rec_1p.hpp calc_sqrtcov_rec_2p.o: calc_sqrtcov_rec_2p.hpp calc_sqrtcov_gen.o: calc_sqrtcov_gen.hpp calc_rhos.o: calc_rhos.hpp calc_stats_1p.o: calc_stats_1p.hpp calc_stats_2p.o: calc_stats_2p.hpp %.o : %.cpp \${CC} -c \${CFLAGS} \$< clean: \rm *.o
 #include #include #include #include #include #include #include "secorder_rec_2p.hpp" #include "calc_stats_2p.hpp" using namespace std; int main(int argc, char *argv[]) { // optional flags int deterministic_seed = 0; // if nonzero, use for random num gen seed int N_nodes = {2000,500}; double p = {{0.01, 0.01},{0.01,0.01}}; double alpha_recip = {{0.0, 0.0},{99,0.0}}; // form of large N covariance matrix centered around population x // conv_x00 conv_x01 chain_0x0 chain_1x0 // conv_x11 chain_0x1 chain_1x1 // div_00x div_01x // div_11x /////////////////////////////////////////////// // correlations centered around population zero // diagonal entries double alpha_conv_hom_00 = 0.5; // zeros onto zero (1,1) double alpha_conv_hom_01 = 0.5; // ones onto zero (2,2) double alpha_div_hom_00 = 0.5; // zero onto zeros (3,3) double alpha_div_hom_10 = 0.5; // zero onto ones (4,4) // cross entries for divergence and convergence // constrained by the diagonal entries double cc_conv_0_01 = 0.5; // mix onto zero (2,1) double cc_div_01_0 = 0.0; // zero onto mix (4,3) // remaining four cross entries are chains through population 0 // constrained by diagonal entries double cc_chain_000 = 0.8; // (3,1) double cc_chain_100 = 0.0; // (4,1) double cc_chain_001 = 0.8; // (3,2) double cc_chain_101 = 0.0; // (4,2) /////////////////////////////////////////////// // correlations centered around population one // diagonal entries double alpha_conv_hom_10 = 0.5; // zeros onto one (1,1) double alpha_conv_hom_11 = 0.5; // ones onto one (2,2) double alpha_div_hom_01 = 0.5; // one onto zeros (3,3) double alpha_div_hom_11 = 0.5; // one onto ones (4,4) // cross entries for divergence and convergence // constrained by the diagonal entries double cc_conv_1_01 = 0.5; // mix onto one (2,1) double cc_div_01_1 = 0.0; // one onto mix (4,3) // remaining four cross entries are chains through population 1 // constrained by diagonal entries double cc_chain_010 = 0.0; // (3,1) double cc_chain_111 = 0.0; // (4,1) double cc_chain_110 = 0.0; // (3,2) double cc_chain_011 = 0.0; // (4,2) // double alpha_conv = {{{0.8,0.1},{99,0.8}},{{0.8,0.1},{99,0.8}}}; // double alpha_div = {{{0.8,0.8},{0.1,0.1}},{{99,99},{0.8,0.8}}}; // //double alpha_chain = {{{0.5,0.2},{0.3,0.4}},{{0.2,0.1},{0.4,0.5}}}; // double alpha_chain = {{{0.0,0.0},{0.0,0.0}},{{0.0,0.0},{0.5,0.0}}}; double alpha_conv_hom = {{alpha_conv_hom_00,alpha_conv_hom_01},{alpha_conv_hom_10,alpha_conv_hom_11}}; double cc_conv_mixed = {{{99,cc_conv_0_01},{99,99}},{{99,cc_conv_1_01},{99,99}}}; double alpha_div_hom = {{alpha_div_hom_00,alpha_div_hom_01},{alpha_div_hom_10,alpha_div_hom_11}}; double cc_div_mixed = {{{99,99},{cc_div_01_0,cc_div_01_1}},{{99,99},{99,99}}}; double cc_chain = {{{cc_chain_000,cc_chain_001},{cc_chain_010,cc_chain_011}},{{cc_chain_100,cc_chain_101},{cc_chain_110,cc_chain_111}}}; //{{{000,001},{010,011}},{{100,101},{110,111}}} gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937); // set the seed // if deterministic_seed, use that for seed if(deterministic_seed) gsl_rng_set(r,deterministic_seed); // else use time in seconds for the seed else gsl_rng_set(r, time(NULL)); // matrix files will be stored in data directory // with filename determined by the six input parameters mkdir("data",0755); // make data directory, if it doesn't exist char FNbase; sprintf(FNbase,"_%i_%i_%1.3f_%1.3f_%1.3f_%1.3f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f_%1.2f", N_nodes, N_nodes, p,p,p,p, alpha_recip, alpha_recip, alpha_recip, alpha_conv_hom, alpha_conv_hom, alpha_conv_hom, alpha_conv_hom, cc_conv_mixed, cc_conv_mixed, alpha_div_hom, alpha_div_hom, alpha_div_hom, alpha_div_hom, cc_div_mixed, cc_div_mixed, cc_chain, cc_chain, cc_chain, cc_chain, cc_chain, cc_chain, cc_chain, cc_chain); // generate second order matrix W gsl_matrix *W; W = secorder_rec_2p(N_nodes,p, alpha_recip, alpha_conv_hom, cc_conv_mixed, alpha_div_hom, cc_div_mixed, cc_chain, r); // if failed to generate a matrix, write error and quit if(!W) { cerr << "Failed to generate the matrix\n"; return -1; } char FN; FILE *fhnd; //////////////////////////////////////////////////////////// // Calculate the covariance structure of the matrix // This should approximately agree with the input alphas //////////////////////////////////////////////////////////// cout << "Testing statistics of matrix..."; cout.flush(); double phat; double alphahat_recip; double alphahat_conv; double alphahat_div; double alphahat_chain; calc_phat_alphahat(W, N_nodes, phat, alphahat_recip, alphahat_conv, alphahat_div, alphahat_chain); cout << "done\n"; strcpy(FN, "data/stats"); strcat(FN, FNbase); strcat(FN, ".dat"); fhnd = fopen(FN, "w"); if(fhnd==NULL) { cerr << "Couldn't open outfile file " << FN << "\n"; exit(-1); } fprintf(fhnd, "%i %i ", N_nodes, N_nodes); <