use of org.apache.commons.math3.stat.descriptive.rank.Median in project derby by apache.
the class Functions method getMedianTestScore.
/**
* <p>
* Calculate the median score achieved on a Test.
* </p>
*/
public static double getMedianTestScore(int testID) throws SQLException {
Logger log = Logger.getLogger();
boolean loggingEnabled = log.isLoggingEnabled();
Median median = new Median();
ArrayList arraylist = new ArrayList();
Connection conn = getDefaultConnection();
try {
log.enableLogging(false);
PreparedStatement ps = Utils.prepare(conn, "select tk.score\n" + "from TestTaking tk, LastTaking lt\n" + "where tk.takingID = lt.takingID\n" + "and tk.testID = ?\n");
ps.setInt(1, testID);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
arraylist.add(new Double(rs.getDouble(1)));
}
Utils.close(rs);
Utils.close(ps);
} finally {
log.enableLogging(loggingEnabled);
}
int count = arraylist.size();
double[] values = new double[count];
for (int i = 0; i < count; i++) {
values[i] = ((Double) arraylist.get(i)).doubleValue();
}
return median.evaluate(values);
}
use of org.apache.commons.math3.stat.descriptive.rank.Median in project OpenTripPlanner by opentripplanner.
the class Graph method calculateTransitCenter.
/**
* Calculates Transit center from median of coordinates of all transitStops if graph
* has transit. If it doesn't it isn't calculated. (mean walue of min, max latitude and longitudes are used)
*
* Transit center is saved in center variable
*
* This speeds up calculation, but problem is that median needs to have all of latitudes/longitudes
* in memory, this can become problematic in large installations. It works without a problem on New York State.
* @see GraphEnvelope
*/
public void calculateTransitCenter() {
if (hasTransit) {
TDoubleList latitudes = new TDoubleLinkedList();
TDoubleList longitudes = new TDoubleLinkedList();
Median median = new Median();
getVertices().stream().filter(v -> v instanceof TransitStop).forEach(v -> {
latitudes.add(v.getLat());
longitudes.add(v.getLon());
});
median.setData(latitudes.toArray());
double medianLatitude = median.evaluate();
median = new Median();
median.setData(longitudes.toArray());
double medianLongitude = median.evaluate();
this.center = new Coordinate(medianLongitude, medianLatitude);
}
}
use of org.apache.commons.math3.stat.descriptive.rank.Median in project presto by prestodb.
the class MathFunctions method inverseCauchyCdf.
@Description("Inverse of Cauchy cdf for a given probability, median, and scale (gamma)")
@ScalarFunction
@SqlType(StandardTypes.DOUBLE)
public static double inverseCauchyCdf(@SqlType(StandardTypes.DOUBLE) double median, @SqlType(StandardTypes.DOUBLE) double scale, @SqlType(StandardTypes.DOUBLE) double p) {
checkCondition(p >= 0 && p <= 1, INVALID_FUNCTION_ARGUMENT, "p must be in the interval [0, 1]");
checkCondition(scale > 0, INVALID_FUNCTION_ARGUMENT, "scale must be greater than 0");
CauchyDistribution distribution = new CauchyDistribution(null, median, scale, CauchyDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
return distribution.inverseCumulativeProbability(p);
}
use of org.apache.commons.math3.stat.descriptive.rank.Median in project cassandra by apache.
the class DriverBurnTest method perfTest.
public void perfTest(SizeCaps requestCaps, SizeCaps responseCaps, Cluster.Builder builder, ProtocolVersion version) throws Throwable {
SimpleStatement request = generateQueryStatement(0, requestCaps);
ResultMessage.Rows response = generateRows(0, responseCaps);
QueryMessage requestMessage = generateQueryMessage(0, requestCaps, version);
Envelope message = requestMessage.encode(version);
int requestSize = message.body.readableBytes();
message.release();
message = response.encode(version);
int responseSize = message.body.readableBytes();
message.release();
Message.Type.QUERY.unsafeSetCodec(new Message.Codec<QueryMessage>() {
public QueryMessage decode(ByteBuf body, ProtocolVersion version) {
QueryMessage queryMessage = QueryMessage.codec.decode(body, version);
return new QueryMessage(queryMessage.query, queryMessage.options) {
protected Message.Response execute(QueryState state, long queryStartNanoTime, boolean traceRequest) {
try {
// unused
int idx = Integer.parseInt(queryMessage.query);
return generateRows(idx, responseCaps);
} catch (NumberFormatException e) {
// for the requests driver issues under the hood
return super.execute(state, queryStartNanoTime, traceRequest);
}
}
};
}
public void encode(QueryMessage queryMessage, ByteBuf dest, ProtocolVersion version) {
QueryMessage.codec.encode(queryMessage, dest, version);
}
public int encodedSize(QueryMessage queryMessage, ProtocolVersion version) {
return QueryMessage.codec.encodedSize(queryMessage, version);
}
});
int threads = 10;
int perThread = 30;
ExecutorService executor = Executors.newFixedThreadPool(threads + 10);
AtomicReference<Throwable> error = new AtomicReference<>();
CountDownLatch signal = new CountDownLatch(1);
AtomicBoolean measure = new AtomicBoolean(false);
DescriptiveStatistics stats = new DescriptiveStatistics();
Lock lock = new ReentrantLock();
for (int t = 0; t < threads; t++) {
executor.execute(() -> {
try (Cluster driver = builder.build();
Session session = driver.connect()) {
while (!executor.isShutdown() && error.get() == null) {
Map<Integer, ResultSetFuture> futures = new HashMap<>();
for (int j = 0; j < perThread; j++) {
long startNanos = nanoTime();
ResultSetFuture future = session.executeAsync(request);
future.addListener(() -> {
long diff = nanoTime() - startNanos;
if (measure.get()) {
lock.lock();
try {
stats.addValue(TimeUnit.MICROSECONDS.toMillis(diff));
} finally {
lock.unlock();
}
}
}, executor);
futures.put(j, future);
}
for (Map.Entry<Integer, ResultSetFuture> e : futures.entrySet()) {
Assert.assertEquals(response.result.size(), e.getValue().get().all().size());
}
}
} catch (Throwable e) {
e.printStackTrace();
error.set(e);
signal.countDown();
}
});
}
Assert.assertFalse(signal.await(30, TimeUnit.SECONDS));
measure.set(true);
Assert.assertFalse(signal.await(60, TimeUnit.SECONDS));
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
System.out.println("requestSize = " + requestSize);
System.out.println("responseSize = " + responseSize);
System.out.println("Mean: " + stats.getMean());
System.out.println("Variance: " + stats.getVariance());
System.out.println("Median: " + stats.getPercentile(0.5));
System.out.println("90p: " + stats.getPercentile(0.90));
System.out.println("95p: " + stats.getPercentile(0.95));
System.out.println("99p: " + stats.getPercentile(0.99));
}
use of org.apache.commons.math3.stat.descriptive.rank.Median in project hive by apache.
the class ReplStatsTracker method toString.
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
DecimalFormat dFormat = new DecimalFormat("#.##");
dFormat.setRoundingMode(RoundingMode.HALF_UP);
sb.append("Replication Stats{");
for (Map.Entry<String, DescriptiveStatistics> event : descMap.entrySet()) {
DescriptiveStatistics statistics = event.getValue();
sb.append("[[Event Name: ").append(event.getKey()).append("; ");
sb.append("Total Number: ").append(statistics.getN()).append("; ");
sb.append("Total Time: ").append(dFormat.format(statistics.getSum())).append("; ");
sb.append("Mean: ").append(formatDouble(dFormat, statistics.getMean())).append("; ");
sb.append("Median: ").append(formatDouble(dFormat, statistics.getPercentile(50))).append("; ");
sb.append("Standard Deviation: ").append(formatDouble(dFormat, statistics.getStandardDeviation())).append("; ");
sb.append("Variance: ").append(formatDouble(dFormat, statistics.getVariance())).append("; ");
sb.append("Kurtosis: ").append(formatDouble(dFormat, statistics.getKurtosis())).append("; ");
sb.append("Skewness: ").append(formatDouble(dFormat, statistics.getSkewness())).append("; ");
sb.append("25th Percentile: ").append(formatDouble(dFormat, statistics.getPercentile(25))).append("; ");
sb.append("50th Percentile: ").append(formatDouble(dFormat, statistics.getPercentile(50))).append("; ");
sb.append("75th Percentile: ").append(formatDouble(dFormat, statistics.getPercentile(75))).append("; ");
sb.append("90th Percentile: ").append(formatDouble(dFormat, statistics.getPercentile(90))).append("; ");
sb.append("Top ").append(k).append(" EventIds(EventId=Time) ").append(topKEvents.get(event.getKey())).append(";" + "]]");
}
sb.append("}");
return sb.toString();
}
Aggregations