-
-
Save sanity/613ea2308c3af4385094 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class SmartpantsTst1 { | |
/** | |
* @param args | |
*/ | |
public static void main(final String[] args) { | |
final Random rand = new Random(); | |
final int adCount = 100, impressionCount = 10000; | |
final ArrayList<Double> adProbs = new ArrayList<>(); | |
double total = 0; | |
for (int x = 0; x < adCount; x++) { | |
final double p = rand.nextDouble(); | |
adProbs.add(p); | |
total += p; | |
} | |
Collections.sort(adProbs); | |
System.out.println("Global mean: " + total / adCount); | |
final ArrayList<Integer> adImpressions = new ArrayList<>(); | |
for (int x = 0; x < impressionCount; x++) { | |
if (rand.nextDouble() > 0.9) { | |
// 50% chance that we do genuine random selection | |
adImpressions.add(rand.nextInt(adCount)); | |
} else { | |
final int pos = adCount - (int) Math.round((Math.pow(rand.nextDouble() * Math.sqrt(adCount), 2))) - 1; | |
if (pos < 0 || pos >= adCount) { | |
continue; | |
} | |
adImpressions.add(pos); | |
} | |
} | |
final int[] occurances = new int[adCount]; | |
double rawSum = 0; | |
for (final int x : adImpressions) { | |
occurances[x]++; | |
rawSum += adProbs.get(x); | |
} | |
System.out.println("Raw mean: " + rawSum / adImpressions.size()); | |
double correctedTotal = 0, correctedSum = 0; | |
for (final int x : adImpressions) { | |
final double bias = 1.0 / occurances[x]; | |
correctedTotal += bias; | |
correctedSum += adProbs.get(x) * bias; | |
} | |
System.out.println("Corrected mean: " + (correctedSum / correctedTotal)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment