lundi 25 septembre 2017

Why are the mean values not much closer to the original weightings?

I run the following program and a typical console output is as follows.

Mean percentage points for weighting 0 is: 57.935590153643616
Mean percentage points for weighting 1 is: 42.06440984635654

Why are these printed means not much closer to 60 and 40?

public static void main(String[] args) {
    Random rand = new Random();

    int numCycles = 5000;

    double[] weightings = {60.0, 40.0};
    double[] weightedRandoms = new double[weightings.length];
    double[] totPercentagePoints = {0.0, 0.0};

    for (int j = 0; j < numCycles; j++) {

        for (int k = 0; k < weightings.length; k++) {
            weightedRandoms[k] = (rand.nextInt(10) + 1) * weightings[k]; // +1 to the random integer to ensure that the weighting is not multiplied by 0
        }

        for (int k = 0; k < weightings.length; k++) {
            totPercentagePoints[k] += weightedRandoms[k] / DoubleStream.of(weightedRandoms).sum() * 100;
        }
    }

    for (int i = 0; i < weightings.length; i++) {
        System.out.println("Mean percentage points for weighting " + i + " is: " + totPercentagePoints[i] / numCycles);
    }
}




Aucun commentaire:

Enregistrer un commentaire