Skip to content

Instantly share code, notes, and snippets.

@joriki
Created January 19, 2013 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joriki/4571272 to your computer and use it in GitHub Desktop.
Save joriki/4571272 to your computer and use it in GitHub Desktop.
Estimate the expected value and variance of a transitivity ratio in a random graph; see http://math.stackexchange.com/questions/280793.
import java.util.Random;
public class Question280793 {
final static int n = 100;
final static int M = 150;
final static int N = (n * (n - 1)) / 2;
final static Random random = new Random ();
public static void main (String [] args) {
boolean [] [] edges = new boolean [n] [n];
double p = M / (double) N;
System.out.println (p / (3 - 2 * p) + " +- " + Math.sqrt (18 * (1 - p) * (1 - p) * (3 + p) / (n * n * n * p * (3 - 2*p) * (3 - 2*p) * (3 - 2*p) * (3 - 2*p))));
double sum = 0;
double sum2 = 0;
for (int ntrials = 1;;ntrials++) {
int total = N;
int used = M;
for (int i = 1;i < n;i++)
for (int j = 0;j < i;j++) {
edges [i] [j] = random.nextDouble () < used / (double) total;
total--;
if (edges [i] [j])
used--;
}
int count2 = 0;
int count3 = 0;
for (int i = 2;i < n;i++)
for (int j = 1;j < i;j++)
for (int k = 0;k < j;k++) {
int count = 0;
if (edges [i] [j])
count++;
if (edges [i] [k])
count++;
if (edges [j] [k])
count++;
if (count >= 2)
count2++;
if (count == 3)
count3++;
}
double ratio = count3 / (double) count2;
if (ratio > 0.05)
System.out.println ("ratio : " + ratio);
sum += ratio;
sum2 += ratio * ratio;
if (((ntrials - 1) & ntrials) == 0) {
double mean = sum / ntrials;
double mean2 = sum2 / ntrials;
System.out.println (ntrials + " : " + mean + " +- " + Math.sqrt (mean2 - mean * mean));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment