<?php
// Monte Carlo simulation results
include("config.php");
include($baseDir . "common/_func.php");
include($baseDir . "common/_sna.php");
//if user is not logged in, redirect to homepage
if(!isset($uID)){header("Location: http://www.netvis.org");exit;}
$array_variables = array("submit","netsize_mean","netsize_std","netsize_min","netsize_max","netnum_max","betweenness_centrality_mean","betweenness_centrality_std","betweenness_centrality_min","betweenness_centrality_max","closeness_centrality_mean","closeness_centrality_std","closeness_centrality_min","closeness_centrality_max","degree_centrality_mean","degree_centrality_std","degree_centrality_min","degree_centrality_max","density_mean","density_std","density_min","density_max","transitivity_mean","transitivity_std","transitivity_min","transitivity_max","num_draws","num_seed","even","start","comment","date time","display_data","display_stats","display_parameters","regression","comm_split","plot_netsize","plot_netnum","plot_betweenness_centrality","plot_closeness_centrality","plot_degree_centrality","plot_density","plot_transitivity");
if($clear=="1"){
// register and define session variables that serve as values in index.php
// (see also "redefine variables used in simulation" below)
for($v=0; $v<count($array_variables); $v++){
$array_variable=$array_variables[$v];
$array_variable_=$array_variables[$v] . "_";
session_register("$array_variable_");
if(!isset(${$array_variable_})){
${$array_variable_}=FALSE;
}
if(!isset(${$array_variable})){
${$array_variable}=FALSE;
}
${$array_variable_}=${$array_variable};
}
// error check session variables for illogical values
if($netsize_min_<4 || $netsize_max_<4 || $netsize_min_>$netsize_max_){$error_=101;}
if($netnum_max_<1){$error_=102;}
if($betweenness_centrality_min_<0 || $betweenness_centrality_max_<0 || $betweenness_centrality_min_>$betweenness_centrality_max_){$error_=103;}
if($closeness_centrality_min_<0 || $closeness_centrality_max_<0 || $closeness_centrality_min_>$closeness_centrality_max_){$error_=104;}
if($degree_centrality_min_<0 || $degree_centrality_max_<0 || $degree_centrality_min_>$degree_centrality_max_){$error_=105;}
if($density_min_<0 || $density_max_<0 || $density_min_>$density_max_){$error_=106;}
if($transitivity_min_<0 || $transitivity_max_<0 || $transitivity_min_>$transitivity_max_){$error_=107;}
if($num_draws_<1 || $num_draws_>10000){$error_=108;}
if($num_seed_<0 || $num_seed_>10000){$error_=109;}
if($netsize_max_<$netnum_max_){$error_=110;}
if($netsize_max_<$betweenness_centrality_max_){$error_=111;}
$array_check = array("netsize_min","netsize_max","netnum_max","betweenness_centrality_min","betweenness_centrality_max","closeness_centrality_min","closeness_centrality_max","degree_centrality_min","degree_centrality_max","density_min","density_max","transitivity_min","transitivity_max","num_draws","num_seed","comm_split");
// error check session variables for missing values
for($z=0; $z<count($array_check); $z++){
$array_check_=$array_check[$z] . "_";
if(${$array_check_}=="" || !is_numeric(${$array_check_})){$error_=($z+1);}
}
// if missing value, then redirect back to index.php
if(!isset($error_)){
$error_=FALSE;
}
if($error_>0){header("Location: ../simulate.php");}
// estimate time it takes simulation to run and load on web page
if($os == "Linux"){
$sim_run = (.2) + (.1)*($num_draws_);
$data_load=($display_data_=="on") ? "($num_draws)*(.1)":"0";
$stats_load=($display_stats_=="on") ? "($num_draws)*(.1)":"0";
}else{
$sim_run = (1) + (.01)*($num_draws_);
$data_load=($display_data_=="on") ? "($num_draws_)*(.01)":"0";
$stats_load=($display_stats_=="on") ? "($num_draws_)*(.01)":"0";
}
$simest = $sim_run + $data_load + $stats_load;
$simest = sprintf("%01.2f", $simest);
// display javascript pop-up window to confirm simulation or return to index.php
if($simest>=30 && $confirm_=="0"){
$confirm_=1;
$confirmation = "<script> function confirmDelete() { if(window.confirm (\"Simulation should take approximately $simest seconds\")) { document.location=\"simulate_results.php\" }else{ document.location=\"index.php\" } } javascript:confirmDelete() </script>";
echo "$confirmation";
}
}
// redefine variables used in simulation
for($v=0; $v<count($array_variables); $v++){
$array_variable=$array_variables[$v];
$array_variable_=$array_variables[$v] . "_";
if(!isset(${$array_variable_})){
${$array_variable_}=FALSE;
}
if(!isset(${$array_variable})){
${$array_variable}=FALSE;
}
${$array_variable}=${$array_variable_};
}
//factorial function
function factorial($val){
$prod = 1;
for($l=2; $l<=$val; $l++){
$prod *= $l;
}
return $prod;
}
if(!isset($even)){
$even=FALSE;
}
//generate random values for simulation
function normal_val($mean,$std,$even){
if($even == 1){
return mt_rand($mean - $std * 2, $mean + $std * 2);
}else{
$r = sqrt(-2 * log(mt_rand() / mt_getrandmax()));
$theta = 2 * pi() * (mt_rand() / mt_getrandmax());
$x = $r * cos($theta);
return (int)($mean + ($std * $x));
}
}
//generate random values for simulation
function normal_real_val($mean,$std,$even){
if($even == 1){
return ($mean - $std * 2) + $std * 4 * (mt_rand() / mt_getrandmax());
}else{
$r = sqrt(-2 * log(mt_rand() / mt_getrandmax()));
$theta = 2 * pi() * (mt_rand() / mt_getrandmax());
$x = $r * cos($theta);
$rtn = ($mean + ($std * $x));
sprintf($rtn, "%01.2f", $rtn);
return $rtn;
}
}
//evenly distributed values for start variable
$counter = 0;
function even_val($min,$max){
global $counter;
$rtn = $min + $counter % ($max - $min + 1);
$counter++;
return $rtn;
}
function even_real_val($min,$max){
global $counter;
$rtn = $min + (($counter++) % (int)(($max - $min + 1) * 10)) / 10;
$counter++;
return $rtn;
}
//generate random values for communication rate
function comm_rate_val($min, $max){
if($min == $max) return $min;
return mt_rand($min, $max);
}
include($baseDir . "common/_javascript.php");
include($baseDir . "common/_header.php");
// mt_srand((double)microtime()*1000000);
mt_srand($num_seed);
//begin the stop clock
$time1 = getmicrotime();
echo "<center>";
echo " <table width=\"700\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\" bordercolor=#006699>";
echo "<tr bgcolor=\"#CCCCCC\" align=\"center\">";
echo " <td nowrap bgcolor=\"#FFFFFF\" height=\"22\">";
echo " <div align=\"left\"><a href=\"index.php\"><font color=#006699>Home</font></a>";
echo " > <a href=\"socialnetworks.php\"><font color=#006699>Social Networks</font></a>";
echo " > <a href=\"simulate.php\"><font color=#006699>Simulation</font></a> ";
echo " > Results</div>";
echo " </td>";
echo " <td nowrap bgcolor=\"#FFFFFF\" height=\"22\">";
echo " <div align=\"right\">";
include($baseDir . "common/_options.php");
echo " </div>";
echo " </td>";
echo "</tr>";
echo "</table>";
// define variables for mean and std unless they were entered in index.php
if($netsize_mean_==""){$netsize_mean = ($netsize_min + $netsize_max) / 2;}
if($netsize_std_==""){$netsize_std = ($netsize_max - $netsize_min) / 4;}
$netnum_mean_=$netnum_max;
$netnum_std_=0;
if($betweenness_centrality_mean_==""){$betweenness_centrality_mean = ($betweenness_centrality_min + $betweenness_centrality_max) / 2;}
if($betweenness_centrality_std_==""){$betweenness_centrality_std = ($betweenness_centrality_max - $betweenness_centrality_min) / 4;}
if($closeness_centrality_mean_==""){$closeness_centrality_mean = ($closeness_centrality_min + $closeness_centrality_max) / 2;}
if($closeness_centrality_std_==""){$closeness_centrality_std = ($closeness_centrality_max - $closeness_centrality_min) / 4;}
if($degree_centrality_mean_==""){$degree_centrality_mean = ($degree_centrality_min + $degree_centrality_max) / 2;}
if($degree_centrality_std_==""){$degree_centrality_std = ($degree_centrality_max - $degree_centrality_min) / 4;}
if($density_mean_==""){$density_mean = ($density_min + $density_max) / 2;}
if($density_std_==""){$density_std = ($density_max - $density_min) / 4;}
if($transitivity_mean_==""){$transitivity_mean = ($transitivity_min + $transitivity_max) / 2;}
if($transitivity_std_==""){$transitivity_std = ($transitivity_max - $transitivity_min) / 4;}
$netsize = array();
$betweenness_centrality = array();
$closeness_centrality = array();
$degree_centrality = array();
$density = array();
$transitivity = array();
$comm = array();
$array_labels = array("id","netsize","betweenness_centrality","closeness_centrality","degree_centrality","density","transitivity");
// output formatted table for displaying simulation data
if($display_data_=="on"){
echo "<center>";
echo "<table>";
echo "<tr bgcolor=#006699><td colspan=20><font color=#FFFFFF>";
echo "<center>Simulation Data</center></font></td></tr>";
echo "<tr>";
// define label variables for simulation data table
for($h=0; $h<count($array_labels); $h++){
$array_label = $array_labels[$h];
echo "<td bgcolor=#006699><font color='#FFFFFF'><a href='javascript:wopen(\"popup.php?label=$array_label\",\"$array_label\",\"scrollbars=no,resizable=yes,width=250,height=200\");'><font color=\"#FFFFFF\">$array_label</font></a></td>";
}
echo "</tr>";
}
// begin simulation
$netsize_values=array();
$betweenness_centrality_values=array();
$closeness_centrality_values=array();
$degree_centrality_values=array();
$density_values=array();
$transitivity_values=array();
$split = $comm_split;
$temp_diff_total = 0;
$temp_diff_num = 0;
for($i=0; $i<$num_draws; $i++){
$id=$i+1;
set_time_limit(180);
$indices_ok = false;
while(!$indices_ok){
$indices_ok = true;
//generate network size
if($start == 1 || $start == 2 || $start == 3 || $start == 6){
//start with network size
do{
$netsize[$i] = normal_val($netsize_mean,$netsize_std,$even);
} while($netsize[$i] < $netsize_min || $netsize[$i] > $netsize_max);
$netsize_ok = 0;
//generate communication values randomly
$comm_total = 0;
$comm_num = 0;
$comm[$i] = array();
for($j=0; $j<$netsize[$i]; $j++){
$comm[$i][$j] = array();
for($k=0; $k<$netsize[$i]; $k++){
if($j == $k){
$comm[$i][$j][$k] = 0;
}
else{
$comm[$i][$j][$k] = comm_rate_val(1, 5);
$comm_num++;
$comm_total += $comm[$i][$j][$k];
}
}
$comm_avg[$i] = (float)($comm_total) / (float)($comm_num);
}
//generate adjacency matrix
$mtx = array();
for($c=0; $c<$netsize[$i]; $c++){
$mtx[$c] = array();
for($d=0; $d<$netsize[$i]; $d++){
if($comm[$i][$c][$d] >= $split){
$mtx[$c][$d] = 1;
}
else{
$mtx[$c][$d] = 0;
}
}
}
//compute indices
$betweenness_centrality[$i] = grp_betweenness($mtx, $netsize[$i]);
$closeness_centrality[$i] = grp_closeness($mtx, $netsize[$i]);
$degree_centrality[$i] = grp_degree($mtx, $netsize[$i]);
$density[$i] = grp_density($mtx, $netsize[$i]);
$transitivity[$i] = grp_transitivity($mtx, $netsize[$i]);
}
else if($start == 5){
//determine network size
do{
$netsize[$i] = normal_val($netsize_mean,$netsize_std,$even);
} while($netsize[$i] < $netsize_min || $netsize[$i] > $netsize_max);
$netsize_ok = 0;
//determine density
do{
$density[$i] = normal_real_val($density_mean,$density_std,$even);
} while($density[$i] < $density_min || $density[$i] > $density_max);
//generate communication values based on density
$comm_total = 0;
$comm_num = 0;
$comm[$i] = array();
for($j=0; $j<$netsize[$i]; $j++){
$comm[$i][$j] = array();
for($k=0; $k<$netsize[$i]; $k++){
if($j == $k){
$comm[$i][$j][$k] = 0;
}
else{
//the higher the density, the more likely the communication value is over the split
if(mt_rand(0, 100) <= $density[$i] * 100){
$comm[$i][$j][$k] = comm_rate_val($split, 5);
}
else{
$comm[$i][$j][$k] = comm_rate_val(1, $split-1);
}
$comm_num++;
$comm_total += $comm[$i][$j][$k];
}
}
$comm_avg[$i] = (float)($comm_total) / (float)($comm_num);
}
//generate adjacency matrix
$mtx = array();
for($c=0; $c<$netsize[$i]; $c++){
$mtx[$c] = array();
for($d=0; $d<$netsize[$i]; $d++){
if($comm[$i][$c][$d] >= $split){
$mtx[$c][$d] = 1;
}
else{
$mtx[$c][$d] = 0;
}
}
}
//compute indices
$betweenness_centrality[$i] = grp_betweenness($mtx, $netsize[$i]);
$closeness_centrality[$i] = grp_closeness($mtx, $netsize[$i]);
$degree_centrality[$i] = grp_degree($mtx, $netsize[$i]);
$density[$i] = grp_density($mtx, $netsize[$i]);
$transitivity[$i] = grp_transitivity($mtx, $netsize[$i]);
}
else if($start == 4){
//determine network size
do{
$netsize[$i] = normal_val($netsize_mean,$netsize_std,$even);
} while($netsize[$i] < $netsize_min || $netsize[$i] > $netsize_max);
$netsize_ok = 0;
//determine degree centrality
do{
$degree_centrality[$i] = normal_real_val($degree_centrality_mean,$degree_centrality_std,$even);
} while($degree_centrality[$i] < $degree_centrality_min || $degree_centrality[$i] > $degree_centrality_max);
//generate communication values based on degree centrality
$comm_total = 0;
$comm_num = 0;
$comm[$i] = array();
for($j=0; $j<$netsize[$i]; $j++){
$comm[$i][$j] = array();
for($k=0; $k<$netsize[$i]; $k++){
if($j == $k){
$comm[$i][$j][$k] = 0;
}
else{
if($j == 0 || $k == 0){
//the higher the degree centrality, the more likely for the first node to be connected
if(mt_rand(0, 100) <= 50 + (50 * $degree_centrality[$i])){
$comm[$i][$j][$k] = comm_rate_val($split, 5);
}
else{
$comm[$i][$j][$k] = comm_rate_val(1, $split-1);
}
}
else{
//the higher the degree centrality, the less likely for all other nodes to be connected
if(mt_rand(0, 100) <= 50 - (50 * $degree_centrality[$i])){
$comm[$i][$j][$k] = comm_rate_val($split, 5);
}
else{
$comm[$i][$j][$k] = comm_rate_val(1, $split-1);
}
}
$comm_num++;
$comm_total += $comm[$i][$j][$k];
}
}
$comm_avg[$i] = (float)($comm_total) / (float)($comm_num);
}
//generate adjacency matrix
$mtx = array();
for($c=0; $c<$netsize[$i]; $c++){
$mtx[$c] = array();
for($d=0; $d<$netsize[$i]; $d++){
if($comm[$i][$c][$d] >= $split){
$mtx[$c][$d] = 1;
}
else{
$mtx[$c][$d] = 0;
}
}
}
//compute indices
$betweenness_centrality[$i] = grp_betweenness($mtx, $netsize[$i]);
$closeness_centrality[$i] = grp_closeness($mtx, $netsize[$i]);
$degree_centrality[$i] = grp_degree($mtx, $netsize[$i]);
$density[$i] = grp_density($mtx, $netsize[$i]);
$transitivity[$i] = grp_transitivity($mtx, $netsize[$i]);
}
//ensure indices are within specified range
if($density[$i] < $density_min || $density[$i] > $density_max){
$indices_ok = false;
}
if($transitivity[$i] < $transitivity_min || $transitivity[$i] > $transitivity_max){
$indices_ok = false;
}
if($betweenness_centrality[$i] < $betweenness_centrality_min || $betweenness_centrality[$i] > $betweenness_centrality_max){
$indices_ok = false;
}
if($closeness_centrality[$i] < $closeness_centrality_min || $closeness_centrality[$i] > $closeness_centrality_max){
$indices_ok = false;
}
if($degree_centrality[$i] < $degree_centrality_min || $degree_centrality[$i] > $degree_centrality_max){
$indices_ok = false;
}
}
$array_values = array("id","netsize_value","betweenness_centrality_value","closeness_centrality_value","degree_centrality_value","density_value","transitivity_value");
for($h=1; $h<count($array_values); $h++){
$array_label = $array_labels[$h];
$array_value = $array_values[$h];
${$array_value} = ${$array_label}[$i];
${$array_value} = sprintf("%01.2f", ${$array_value});
}
// output formatted table for displaying simulation data
if($display_data_=="on"){
// define value variables for simulation csv file
for($v=0; $v<count($array_values); $v++){
$array_value=$array_values[$v];
if($v%2 == 0){
$bgcolor="#DDDDDD";
}else{
$bgcolor="#CCCCCC";
}
echo "<td align=left bgcolor=$bgcolor><table><tr><td>${$array_value}</td></tr></table></td>";
}
echo "</tr>";
}
// create csv data file
$allString_data="";
$comma = ",";
if($i==0){
for($l=0; $l<count($array_values); $l++){
$array_label = $array_labels[$l];
$allString_data.="$array_label,";
}
$allString_data.="\n";
}else{
$allString_data.="removecomma";
}
$allString_data.= $id . $comma . $netsize_value . $comma . $betweenness_centrality_value . $comma . $closeness_centrality_value . $comma . $degree_centrality_value . $comma . $density_value . $comma . $transitivity_value;
$allString_data.=" \n";
$allString_dataArray[]=$allString_data;
// create member csv data file
$memberString_data="";
if($i==0){
$memberString_data.="draw_id,person_id";
for($c=1; $c<=$netsize_max; $c++){
$memberString_data.=",comm$c";
}
$memberString_data.="\n";
}else{
$memberString_data.="removecomma";
}
for($p=0; $p < $netsize[$i]; $p++){
$memberString_data.= $i+1 . $comma . $p . $comma;
for($q=0; $q < $netsize[$i]; $q++){
if(!isset($comm[$i][$p][$q])){
$comm[$i][$p][$q]=0;
}
if($q < $netsize[$i]-1){
$memberString_data.= $comm[$i][$p][$q] . $comma;
}else{
$memberString_data.= $comm[$i][$p][$q];
}
}
$memberString_data.=" \n";
}
$memberString_dataArray[]=$memberString_data;
$array_members = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y");
// create csv simulation file
$simulationString_data="";
if($i<$netnum_max){
if($i>0){$simulationString_data.="removecomma";}
for($c=0; $c<($netsize[$i]); $c++){
if($c<$netsize[$i]-1){
$simulationString_data.=$array_members[$c] . $comma;
}else{
$simulationString_data.=$array_members[$c] . "\n";
}
}
for($p=0; $p < $netsize[$i]; $p++){
for($q=0; $q < $netsize[$i]; $q++){
if(!isset($comm[$i][$p][$q])){
$comm[$i][$p][$q]=0;
}
if($q < $netsize[$i]-1){
$simulationString_data.= $comm[$i][$p][$q] . $comma;
}else{
$simulationString_data.= $comm[$i][$p][$q];
}
}
$simulationString_data.="\n";
}
$simulationString_dataArray[]=$simulationString_data;
}
$netsize_values[]=$netsize_value;
$betweenness_centrality_values[]=$betweenness_centrality_value;
$closeness_centrality_values[]=$closeness_centrality_value;
$degree_centrality_values[]=$degree_centrality_value;
$density_values[]=$density_value;
$transitivity_values[]=$transitivity_value;
}
//end the stop clock and display seconds
$time2 = getmicrotime();
$time3 = $time2-$time1;
$seconds = number_format($time3,4);
set_time_limit(360);
$ff_all = fopen ($baseDir . "csv/" . $username . ".network.csv", "w");
$ff_all_data = fopen ($baseDir . "csv/" . $username . ".network.csv", "w");
$ff_member_data = fopen ($baseDir . "csv/" . $username . ".member.csv", "w");
$ff_simulation_data = fopen ($baseDir . "csv/" . $username . ".simulation.csv", "w");
$allString_dataContent = implode(",", $allString_dataArray);
$allString_dataContent = str_replace(",removecomma","",$allString_dataContent);
$memberString_dataContent = implode(",", $memberString_dataArray);
$memberString_dataContent = str_replace(",removecomma","",$memberString_dataContent);
$simulationString_dataContent = implode(",", $simulationString_dataArray);
$simulationString_dataContent = str_replace(",removecomma","",$simulationString_dataContent);
if($ff_all){
fputs($ff_all, $allString_dataContent, strlen($allString_dataContent));
fclose($ff_all);
}
if($ff_all_data){
fputs($ff_all_data, $allString_dataContent, strlen($allString_dataContent));
fclose($ff_all_data);
}
if($ff_member_data){
fputs($ff_member_data, $memberString_dataContent,
strlen($memberString_dataContent));
fclose($ff_member_data);
}
if($ff_simulation_data){
fputs($ff_simulation_data, $simulationString_dataContent,
strlen($simulationString_dataContent));
fclose($ff_simulation_data);
}
echo "</tr>";
echo "</table></center>";
echo "<br><center>This simulation took $seconds seconds to process ($datetime)</center><br>";
echo "<center><a href=\"csv/". $username . ".network.csv\" target=_blank><font color=#006699>".
$username . ".network.csv</font></a> is the csv network file<br><br></center>";
echo "<center><a href=\"csv/". $username . ".member.csv\" target=_blank><font color=#006699>".
$username . ".member.csv</font></a> is the csv member file<br><br></center>";
echo "<center><a href=\"csv/". $username . ".simulation.csv\" target=_blank><font color=#006699>".
$username . ".simulation.csv</font></a> is the csv simulation file<br></center>";
set_time_limit(360);
//compute descriptive statistics
$array_factors = array("netsize","betweenness_centrality","closeness_centrality","degree_centrality","density","transitivity");
// create formatted sum, count, mean, min, max, diff, var, and std variables
for($a=0; $a<count($array_factors); $a++){
$array_factor=$array_factors[$a];
${$array_factor . "_values_sum"} = array_sum(${$array_factor . "_values"});
${$array_factor . "_values_mean"} = (${$array_factor . "_values_sum"}/$num_draws);
${$array_factor . "_values_min"} = min(${$array_factor . "_values"});
${$array_factor . "_values_max"} = max(${$array_factor . "_values"});
${$array_factor . "_values_diff"} = array();
for($i=0; $i<$num_draws; $i++){
${$array_factor . "_values_diff"}[] = (${$array_factor . "_values"}[$i] - ${$array_factor . "_values_mean"})*(${$array_factor . "_values"}[$i] - ${$array_factor . "_values_mean"});
}
${$array_factor . "_values_var"} = array_sum(${$array_factor . "_values_diff"}) / ($num_draws - 1);
${$array_factor . "_values_std"} = sqrt(${$array_factor . "_values_var"});
$array_stats = array("mean","std","min","max");
for($b=0; $b<count($array_stats); $b++){
$array_stat=$array_stats[$b];
${$array_factor . "_values_" . $array_stat} = sprintf("%01.2f", ${$array_factor . "_values_" . $array_stat});
}
}
// define arrays
$mean_values = array($netsize_values_mean,$betweenness_centrality_values_mean,$closeness_centrality_values_mean,$degree_centrality_values_mean,$density_values_mean,$transitivity_values_mean);
$std_values = array($netsize_values_std,$betweenness_centrality_values_std,$closeness_centrality_values_std,$degree_centrality_values_std,$density_values_std,$transitivity_values_std);
$min_values = array($netsize_values_min,$betweenness_centrality_values_min,$closeness_centrality_values_min,$degree_centrality_values_min,$density_values_min,$transitivity_values_min);
$max_values = array($netsize_values_max,$betweenness_centrality_values_max,$closeness_centrality_values_max,$degree_centrality_values_max,$density_values_max,$transitivity_values_max);
$factor_values = array($netsize_values,$betweenness_centrality_values,$closeness_centrality_values,$degree_centrality_values,$density_values,$transitivity_values);
$labels = array("netsize","betweenness_centrality","closeness_centrality","degree_centrality","density","transitivity");
// compute correlation matrix
$rs=array();
for($a=0; $a<count($mean_values); $a++){
for($b=$a+1; $b<count($mean_values); $b++){
$x = array();
$y = array();
$x_y = array();
$x_sq = array();
$y_sq = array();
$N = $num_draws;
$x = $factor_values[$a];
$y = $factor_values[$b];
$x_sum = array_sum($x);
$y_sum = array_sum($y);
for($c=0; $c<$N; $c++){
if(!isset($x[$c])){
$x[$c]=FALSE;
}
if(!isset($y[$c])){
$y[$c]=FALSE;
}
$x_y[]=($x[$c]*$y[$c]);
$x_sq[]=($x[$c]*$x[$c]);
$y_sq[]=($y[$c]*$y[$c]);
}
$x_y_sum = array_sum($x_y);
$x_sq_sum = array_sum($x_sq);
$y_sq_sum = array_sum($y_sq);
if(sqrt(($x_sq_sum-($x_sum*$x_sum)/$N)*($y_sq_sum-($y_sum*$y_sum)/$N)) != 0){
$r = ($x_y_sum - ($x_sum*$y_sum)/$N)/
sqrt(($x_sq_sum-($x_sum*$x_sum)/$N)*($y_sq_sum-($y_sum*$y_sum)/$N));
}else{
$r = 0;
}
$r=sprintf("%01.2f", $r);
#echo "r($labels[$a],$labels[$b]) = $r<br>";
$rs[]=$r;
}
}
// display descriptive statistics and correlation matrix and create stats csv file
if($display_stats_=="on"){
echo "<br><center>";
echo "<table>";
echo "<tr bgcolor=#006699><td colspan=13><font color=#FFFFFF>";
echo "<center>Descriptive Statistics / Correlation Matrix</center></font></td></tr>";
echo "<tr align=center bgcolor=#006699><td> </td>";
echo "<td><font color='#FFFFFF' align=center>Mean</font></td>";
echo "<td><font color='#FFFFFF' align=center>StDev</font></td>";
echo "<td><font color='#FFFFFF' align=center>Range</font></td>";
}
$allString_stats="";
$comma = ",";
$blank = " ";
$c=0;
while($c<count($mean_values)){
if($display_stats_=="on"){echo "<td align=center><a href='javascript:wopen(\"popup.php?label=$labels[$c]\",\"$labels[$c]\",\"scrollbars=no,resizable=yes,width=250,height=200\");'><font color=\"#FFFFFF\">$labels[$c]</font></a></td>";}
if($c==0){
$allString_stats.=",mean,std,min,max,";
}
$allString_stats.="$labels[$c],";
if($c==count($mean_values)-1){
$allString_stats.="removecomma";
$allString_stats.="\n";
}
$c++;
}
if($display_stats_=="on"){echo "</tr>";}
$d=0;
$f=0;
while($d<count($mean_values)){
if($display_stats_=="on"){
echo "<tr><td bgcolor=#006699><font color='#FFFFFF'>";
echo "<a href='javascript:wopen(\"popup.php?label=$labels[$d]\",\"$labels[$d]\",\"scrollbars=no,resizable=yes,width=250,height=200\");'><font color=\"#FFFFFF\">$labels[$d]</font></a></td>";}
$allString_stats.=$labels[$d] . $comma;
if($d%2 == 0){
$bgcolor="#DDDDDD";
}else{
$bgcolor="#CCCCCC";
}
if($display_stats_=="on"){echo "<td bgcolor=\"$bgcolor\" align=center>";
echo "$mean_values[$d]";
echo "</td><td bgcolor=\"$bgcolor\" align=center>";
echo "$std_values[$d]";
echo "</td><td bgcolor=\"$bgcolor\" align=center>";
echo "$min_values[$d] - $max_values[$d]";
echo "</td><td bgcolor=\"$bgcolor\" align=center> </td>";}
$allString_stats.= $mean_values[$d] . $comma;
$allString_stats.= $std_values[$d] . $comma;
$allString_stats.= $min_values[$d] . $comma;
$allString_stats.= $max_values[$d] . $comma;
$allString_stats.= $blank . $comma;
for ($e=1; $e<=$d; $e++){
echo "<td bgcolor=\"$bgcolor\" align=center> </td>";
$allString_stats.= $blank . $comma;
}
for ($e=1; $e<= count($mean_values)-1-$d; $e++){
if($display_stats_=="on"){echo "<td bgcolor=\"$bgcolor\" align=center>";
echo "<table><tr><td>";
if($rs[$f]>.30 || $rs[$f]<-.30){echo "<b>";}
echo "$rs[$f]";
if($rs[$f]>.30 || $rs[$f]<-.30){echo "</b>";}
echo "</td></tr></table></td>";}
if($e<count($mean_values)-1-$d){
$allString_stats.= $rs[$f] . $comma;
}else{
$allString_stats.= $rs[$f];
}
$f=$f+1;
}
if($display_stats_=="on"){echo "</tr>";}
$allString_stats.=" \n";
if($d==count($mean_values)-1){
$allString_statsArray[]=$allString_stats;
}
$d++;
}
if($display_stats_=="on"){echo "</table>";
echo "<center>Note: values in bold are | <u>r</u> | > .30</center><br>";}
$ff_all = fopen ($baseDir . "csv/" . $username . ".stats.csv", "w");
$ff_all_stats = fopen ($baseDir . "csv/" . $username . ".stats.csv", "w");
$allString_statsContent = implode(",", $allString_statsArray);
$allString_statsContent = str_replace(",removecomma","",$allString_statsContent);
if($ff_all){
fputs($ff_all, $allString_statsContent, strlen($allString_statsContent));
fclose($ff_all);
}
if($ff_all_stats){
fputs($ff_all_stats, $allString_statsContent, strlen($allString_statsContent));
fclose($ff_all_stats);
}
echo "<center><a href=\"csv/". $username . ".stats.csv\" target=_blank><font color=#006699>".
$username . ".stats.csv</font></a> is the csv stats file<br></center>";
if($regression_=="on"){
$array_dvs = array("betweenness_centrality","closeness_centrality","degree_centrality","density","transitivity");
$all_arrays = array_merge(array("netnum"), $array_dvs);
$string = "variable,m,b,r,t\n";
for($d=0; $d<count($array_dvs); $d++){
$dv=$array_dvs[$d];
//linear regression predicting dvs
$regression_data = linear_regression($netsize, $$dv);
$string .= $dv . "," . $regression_data["m"] . "," . $regression_data["b"] . "," . $regression_data["r"] . "," . $regression_data["t"] . "\n";
}
$fp = fopen ($baseDir . "csv/" . $username . ".regression.csv", "w");
fputs($fp, $string, strlen($string));
fclose($fp);
//do histogram HERE
/* $image = ImageCreate(330, 330);
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
ImageFilledRectangle($image,0,0,1045,1045,$white);
for($xvar=0; $xvar<6; $xvar++){
for($yvar=0; $yvar<6; $yvar++){
ImageRectangle($image, $xvar*55, $yvar*55, 50, 50);
if($xvar == $yvar){
if(substr_count($all_arrays[$xvar], "_values") == 0)
$varname = $all_arrays[$xvar];
else
$varname = substr($all_arrays[$xvar], 0, strlen($all_arrays[$xvar])-7);
ImageString($image, ImagePSLoadFont("/usr/share/fonts/a010013l.pfb"), $xvar*55 + 4, $yvar*55 + 30, $varname, $black);
}
else{
$xmax = 0;
$ymax = 0;
$xmin = 1000000;
$ymin = 1000000;
for($d=0; $d<$num_draws; $d++){
if(${$all_arrays[$xvar]}[$d] > $xmax)
$xmax = ${$all_arrays[$xvar]}[$d];
if(${$all_arrays[$xvar]}[$d] < $xmin)
$xmin = ${$all_arrays[$xvar]}[$d];
if(${$all_arrays[$yvar]}[$d] > $ymax)
$ymax = ${$all_arrays[$yvar]}[$d];
if(${$all_arrays[$yvar]}[$d] < $ymin)
$ymin = ${$all_arrays[$yvar]}[$d];
}
for($d=0; $d<$num_draws; $d++){
ImageSetPixel($image, $xvar*55 + 50*((${$all_arrays[$xvar]}[$d]-$xmin)/($xmax-$xmin)), $yvar*55 + 50*((${$all_arrays[$yvar]}[$d]-$ymin)/($ymax-$ymin)), $black);
}
}
}
}
ImagePNG($image, $basedir . $username . ".histogram.png");
*/
echo "<br>";
echo "<center><a href=\"csv/". $username . ".regression.csv\" target=_blank><font color=#006699>".
$username . ".regression.csv</font></a> is the regression analysis output<br></center>";
}
// parameter arrays
$blank = " ";
$distribution=($even_==1) ? "even":"normal";
$checked_1=($start_=="1")?"checked":" ";
$checked_2=($start_=="2")?"checked":" ";
$checked_3=($start_=="3")?"checked":" ";
$checked_4=($start_=="4")?"checked":" ";
$checked_5=($start_=="5")?"checked":" ";
$checked_6=($start_=="6")?"checked":" ";
$array_parameters_start = array($checked_1,$checked_2,$checked_3,$checked_4,$checked_5,$checked_6,$blank,$blank,$blank,$blank,$blank,$blank,$blank,$blank);
$array_parameters_factor = array("netsize","betweenness_centrality","closeness_centrality","degree_centrality","density","transitivity","comm_split","num_draws","num_seed","distribution","comment","datetime","username","netnum");
$array_parameters_mean = array($netsize_mean_,$betweenness_centrality_mean_,$closeness_centrality_mean_,$degree_centrality_mean_,$density_mean_,$transitivity_mean_,$blank,$blank,$blank,$blank,$blank,$blank,$blank,$blank,$blank,$blank);
$array_parameters_std = array($netsize_std_,$betweenness_centrality_std_,$closeness_centrality_std_,$degree_centrality_std_,$density_std_,$transitivity_std_,$blank,$blank,$blank,$blank,$blank,$blank,$blank,$blank,$blank,$blank);
$array_parameters_min = array($netsize_min_,$betweenness_centrality_min_,$closeness_centrality_min_,$degree_centrality_min_,$density_min_,$transitivity_min_,$blank,$blank,$blank,$blank,$blank,$blank,$blank,$blank,$blank,$blank);
$array_parameters_max = array($netsize_max_,$betweenness_centrality_max_,$closeness_centrality_max_,$degree_centrality_max_,$density_max_,$transitivity_max_,$comm_split_,$num_draws_,$num_seed_,$distribution,$comment,$datetime,$username,$netnum_max);
// display parameters
if($display_parameters_=="on"){
echo "<br>";
echo "<center>";
echo "<table>";
echo "<tr bgcolor=#006699><td colspan=6><font color=#FFFFFF>";
echo "<center>Parameters</center></font></td></tr>";
echo "<tr align=center bgcolor=#006699>";
echo "<td><font color='#FFFFFF' align=center>Start</font></td>";
echo "<td><font color='#FFFFFF' align=center>Factor</font></td>";
echo "<td><font color='#FFFFFF' align=center>Mean</font></td>";
echo "<td><font color='#FFFFFF' align=center>Std</font></td>";
echo "<td><font color='#FFFFFF' align=center>Min</font></td>";
echo "<td><font color='#FFFFFF' align=center>Max</font></td>";
echo "</tr>";
for($c=0; $c<count($array_parameters_start); $c++){
if($c%2 == 0){
$bgcolor="#DDDDDD";
}else{
$bgcolor="#CCCCCC";
}
echo "<tr><td bgcolor=\"$bgcolor\" align=center>";
echo "$array_parameters_start[$c]";
echo "</td><td bgcolor=\"$bgcolor\" align=left>";
echo "$array_parameters_factor[$c]";
echo "</td><td bgcolor=\"$bgcolor\" align=center>";
echo "$array_parameters_mean[$c]";
echo "</td><td bgcolor=\"$bgcolor\" align=center>";
echo "$array_parameters_std[$c]";
echo "</td><td bgcolor=\"$bgcolor\" align=center>";
echo "$array_parameters_min[$c]";
echo "</td><td bgcolor=\"$bgcolor\" align=center>";
echo "$array_parameters_max[$c]";
echo "</td></tr>";
}
echo "</table>";
}
// create parameters csv file
$allString_parameters="";
$allString_parameters.="start,factor,mean,std,min,max\n";
for($c=0; $c<count($array_parameters_start); $c++){
$allString_parameters.="$array_parameters_start[$c],";
$allString_parameters.="$array_parameters_factor[$c],";
$allString_parameters.="$array_parameters_mean[$c],";
$allString_parameters.="$array_parameters_std[$c],";
$allString_parameters.="$array_parameters_min[$c],";
$allString_parameters.="$array_parameters_max[$c]\n";
}
$ff_all = fopen ($baseDir . "csv/" . $username . ".parameters.csv", "w");
$ff_all_parameters = fopen ($baseDir . "csv/" . $username . ".parameters.csv", "w");
$allString_parametersContent = $allString_parameters;
if($ff_all){
fputs($ff_all, $allString_parametersContent, strlen($allString_parametersContent));
fclose($ff_all);
}
if($ff_all_parameters){
fputs($ff_all_parameters, $allString_parametersContent, strlen($allString_parametersContent));
fclose($ff_all_parameters);
}
echo "<br><center><a href=\"csv/". $username . ".parameters.csv\" target=_blank><font color=#006699>".
$username . ".parameters.csv</font></a> is the csv parameters file<br></center>";
$array_plots = array("plot_betweenness_centrality_","plot_closeness_centrality_","plot_degree_centrality_","plot_density_","plot_transitivity_");
for($a=0; $a<count($array_plots); $a++){
$array_plot=$array_plots[$a];
$array_plot_max = $array_plot . "max";
${$array_plot_max} = 1;
$plot_comm_max = 5;
$array_plot=str_replace("_","",$array_plot);
$array_plot=str_replace("plot","",$array_plot);
$array_plot=str_replace("centrality","_centrality",$array_plot);
$array_plot_comma = $array_plot . "_comma";
session_register("array_plot_comma");
${$array_plot_comma}="";
session_register("comm_comma");
$comm_comma="";
$array_plot_values = $array_plot . "_values";
session_register("array_plot_values");
for($i=0; $i<$num_draws; $i++){
$end=$num_draws-1;
if($i<$end){
${$array_plot_comma}.=sprintf("%01.2f", ${$array_plot_values}[$i]);
${$array_plot_comma}.=",";
$comm_comma.=sprintf("%01.0f", $density_values[$i]);
$comm_comma.=",";
}else{
${$array_plot_comma}.=sprintf("%01.2f", ${$array_plot_values}[$i]);
$comm_comma.=sprintf("%01.0f", $density_values[$i]);
}
}
// create scatterplot php files
$array_plot=$array_plots[$a];
if(${$array_plot} == "on"){
$array_plot=str_replace("_","",$array_plot);
$array_plot=str_replace("plot","",$array_plot);
$array_plot=str_replace("centrality","_centrality",$array_plot);
$allString_scatterplot="";
$allString_scatterplot.="<?php \n\r";
$allString_scatterplot.="// create a scatterplot of $array_plot vs communication \n\r";
$allString_scatterplot.=" \$serverRoot=\$HTTP_SERVER_VARS['DOCUMENT_ROOT']; \n\r";
$allString_scatterplot.=" include(\"config.php\"); \n\r";
$allString_scatterplot.=" if(\$os==\"Windows\"){dl(\"extensions/php_gd.dll\");} \n\r";
$allString_scatterplot.=" // remove file from jpgraph_cache \n\r";
$allString_scatterplot.=" exec (\$cmd . \$remove . \$quote . \$remove_cache . \"$username.$array_plot.scatterplot.png\" . \$quote); \n\r";
$allString_scatterplot.=" include (\$baseDir . \"common/_jpgraph.php\"); \n\r";
$allString_scatterplot.=" include (\$baseDir . \"common/_jpgraph_scatter.php\"); \n\r";
$allString_scatterplot.=" include (\$baseDir . \"common/_jpgraph_line.php\"); \n\r";
$allString_scatterplot.=" set_time_limit(360); \n\r";
$allString_scatterplot.="// Create data points \n\r";
$allString_scatterplot.="\$datay=split(\",\",\$comm_comma); \n\r";
$allString_scatterplot.="\$datayenv=split(\",\",\$comm_comma); \n\r";
$allString_scatterplot.="\if(!isset(\${$array_plot_comma})){ \n\r";
$allString_scatterplot.="\${$array_plot_comma}=FALSE; \n\r";
$allString_scatterplot.="} \n\r";
$allString_scatterplot.="\$datax=split(\",\",\${$array_plot_comma}); \n\r";
$allString_scatterplot.="// Setup the basic parameters for the graph \n\r";
$allString_scatterplot.="\$graph = new Graph(500,500,\"auto\"); \n\r";
$allString_scatterplot.="\$graph->SetScale(\"linlin\",1,$plot_comm_max,0,${$array_plot_max}); \n\r";
$allString_scatterplot.="\$graph->yscale->ticks->Set(1, 1); \n\r";
$allString_scatterplot.="\$graph->xscale->ticks->Set(${$array_plot_max}/10, ${$array_plot_max}/10); \n\r";
$allString_scatterplot.="\$graph->SetShadow(); \n\r";
$allString_scatterplot.="\$graph->SetBox(); \n\r";
$allString_scatterplot.="\$graph->title->Set(\"NetVis ScatterPlot\"); \n\r";
$allString_scatterplot.="\$graph->title->SetMulti-Color(\"netvisyellow\"); \n\r";
$allString_scatterplot.="\$graph->title->SetFont(FF_FONT2,FS_BOLD); \n\r";
$allString_scatterplot.="// Set color for background \n\r";
$allString_scatterplot.="\$graph->SetMulti-Color(\"netvisyellow\"); \n\r";
$allString_scatterplot.="\$graph->SetMarginMulti-Color(\"netvisblue\"); \n\r";
$allString_scatterplot.="// Set legend box specification \n\r";
$allString_scatterplot.="\$graph->legend->SetFillMulti-Color(\"white\"); \n\r";
$allString_scatterplot.="\$graph->legend->SetLineWeight(2); \n\r";
$allString_scatterplot.="\$graph->legend->Pos(.5,.1,\"center\",\"top\"); \n\r";
$allString_scatterplot.="// Set X-axis at the minimum value of Y-axis (default will be at 0) \n\r";
$allString_scatterplot.="// \"min\" will position the x-axis at the minimum value of the Y-axis \n\r";
$allString_scatterplot.="\$graph->xaxis->SetPos(\"min\"); \n\r";
$allString_scatterplot.="// Extend the margin for the labels on the Y-axis and \n\r";
$allString_scatterplot.="// reverse the direction of the ticks on the Y-axis \n\r";
$allString_scatterplot.="\$graph->yaxis->title->Set(\"communication\"); \n\r";
$allString_scatterplot.="\$graph->xaxis->title->Set(\"$array_plot\"); \n\r";
$allString_scatterplot.="\$graph->yaxis->title->SetMulti-Color(\"netvisyellow\"); \n\r";
$allString_scatterplot.="\$graph->xaxis->title->SetMulti-Color(\"netvisyellow\"); \n\r";
$allString_scatterplot.="\$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD); \n\r";
$allString_scatterplot.="\$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD); \n\r";
$allString_scatterplot.="\$graph->yaxis->title->Pos(5,1,\"right\",\"top\"); \n\r";
$allString_scatterplot.="\$graph->xaxis->title->Pos(5,1,\"right\",\"top\"); \n\r";
$allString_scatterplot.="\$graph->yaxis->SetMulti-Color(\"netvisyellow\"); \n\r";
$allString_scatterplot.="\$graph->xaxis->SetMulti-Color(\"netvisyellow\"); \n\r";
$allString_scatterplot.="\$graph->yaxis->SetFont(FF_FONT1,FS_BOLD); \n\r";
$allString_scatterplot.="\$graph->xaxis->SetFont(FF_FONT1,FS_BOLD); \n\r";
$allString_scatterplot.="\$graph->yaxis->SetTickNodeMargin(12); \n\r";
$allString_scatterplot.="\$graph->xaxis->SetTickNodeMargin(6); \n\r";
$allString_scatterplot.="\$graph->yaxis->SetTickDirection(SIDE_LEFT); \n\r";
$allString_scatterplot.="\$graph->xaxis->SetTickDirection(SIDE_DOWN); \n\r";
$allString_scatterplot.="// Add mark graph with static lines \n\r";
$allString_scatterplot.="\$line = new PlotLine(HORIZONTAL,0,\"black\",2); \n\r";
$allString_scatterplot.="\$graph->AddLine(\$line); \n\r";
$allString_scatterplot.="// Create a scatter plot \n\r";
$allString_scatterplot.="\$sp1 = new ScatterPlot(\$datay,\$datax); \n\r";
$allString_scatterplot.="\$sp1->mark->SetType(MARK_SQUARE); \n\r";
$allString_scatterplot.="\$sp1->mark->SetFillMulti-Color(\"black\"); \n\r";
$allString_scatterplot.="\$sp1->SetImpuls(); \n\r";
$allString_scatterplot.="\$sp1->SetMulti-Color(\"netvisyellow\"); \n\r";
$allString_scatterplot.="\$sp1->SetWeight(1); \n\r";
$allString_scatterplot.="\$sp1->mark->SetWidth(3); \n\r";
$allString_scatterplot.="\$sp1->SetLegend(\"communication vs $array_plot\"); \n\r";
$allString_scatterplot.="\$graph->Add(\$sp1); \n\r";
#$allString_scatterplot.="// Create the line plot \n\r";
#$allString_scatterplot.="\$ep1 = new LinePlot(\$datayenv,\$datax); \n\r";
#$allString_scatterplot.="\$ep1->SetStyle(\"dotted\"); \n\r";
#$allString_scatterplot.="\$ep1->SetLegend(\"Regression Line\"); \n\r";
#$allString_scatterplot.="\$graph->Add(\$ep1); \n\r";
$allString_scatterplot.="\$graph->Stroke(); \n\r";
$allString_scatterplot.="?> \n\r";
$ff_all_scatterplot = fopen ($baseDir . "csv/" . $username . "." . $array_plot . ".scatterplot.php", "w");
$allString_scatterplotContent = $allString_scatterplot;
if($ff_all_scatterplot){
fputs($ff_all_scatterplot, $allString_scatterplotContent, strlen($allString_scatterplotContent));
fclose($ff_all_scatterplot);
}
//create links to plots
echo "<br>$array_plot vs communication <a href='javascript:wopen(\"$username.$array_plot.scatterplot.php\",\"Plot\",\"scrollbars=no,resizable=yes,width=300,height=300\");'><font color=#006699>plot</font></a><br>";
}
}
echo "<form action=\"simulate_add.php\" method=\"post\" name=\"form\">";
echo "<input type=\"hidden\" name=\"clear\" value=\"1\">";
echo "<center><input type=submit name=submit value=\"Add Simulation\"></center><br>";
echo "</form>";
echo "<center><font color=ff0000>Warning: If you Add Simulation to your social networks, all previously run simulations will be replaced</font></center><br>";
echo "<form action=\"simulate.php\" method=\"post\" name=\"form\">";
echo "<center><input type=submit name=submit value=\"Modify Simulation\"></center>";
echo "</form>";
include($baseDir . "common/_footer.php");
?>