Skip to content

Instantly share code, notes, and snippets.

@joriki
Created April 2, 2020 15:46
Show Gist options
  • Save joriki/cb57cb87436a648cea5566e2792c3d58 to your computer and use it in GitHub Desktop.
Save joriki/cb57cb87436a648cea5566e2792c3d58 to your computer and use it in GitHub Desktop.
Compute the variance of a random variable in a coin experiment; see https://math.stackexchange.com/questions/2257188.
public class Question2257188 {
final static int n = 4;
public static void main(String [] args) {
double s = 0;
double s2 = 0;
int [] [] permutations = Permutations.getPermutations(n);
for (int [] p : permutations) {
int sum = p [0] + 1;
for (int i = 1;i < p.length;i++)
sum += Math.abs(p [i] - p [i - 1]);
s += sum;
s2 += sum * sum;
}
s /= permutations.length;
s2 /= permutations.length;
System.out.println(s);
System.out.println(s2);
System.out.println(s2 - s * s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment