Skip to content

Instantly share code, notes, and snippets.

@komiya-atsushi
Created January 18, 2015 15:53
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 komiya-atsushi/133085ef955c24303eed to your computer and use it in GitHub Desktop.
Save komiya-atsushi/133085ef955c24303eed to your computer and use it in GitHub Desktop.
commons-math3 を使って二項比率の信頼区間を求めるデモプログラム。
import org.apache.commons.math3.stat.interval.*;
import java.util.Arrays;
import java.util.List;
/**
* commons-math3 を使って二項比率の信頼区間を求めるデモプログラム。
*/
public class BinomialProportionDemo {
static void demo(int numClicks, int numImpression) {
List<BinomialConfidenceInterval> methods = Arrays.asList(
new NormalApproximationInterval(),
new ClopperPearsonInterval(),
new WilsonScoreInterval(),
new AgrestiCoullInterval());
double alpha = 0.05;
System.out.println("---");
for (BinomialConfidenceInterval method : methods) {
String name = method.getClass().getSimpleName();
try {
ConfidenceInterval ci = method.createInterval(numImpression, numClicks, 1 - alpha);
System.out.printf("%s: %s\n", name, ci.toString());
} catch (Exception e) {
e.printStackTrace();
System.out.printf("%s: Cannot calculate confidence interval\n", name);
}
}
}
public static void main(String[] args) {
demo(5, 1000);
demo(0, 1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment