Search in sources :

Example 21 with Median

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);
}
Also used : Median(org.apache.commons.math3.stat.descriptive.rank.Median)

Example 22 with Median

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);
    }
}
Also used : TDoubleList(gnu.trove.list.TDoubleList) TurnRestriction(org.opentripplanner.common.TurnRestriction) LoggerFactory(org.slf4j.LoggerFactory) TransitStop(org.opentripplanner.routing.vertextype.TransitStop) CalendarServiceData(org.onebusaway.gtfs.model.calendar.CalendarServiceData) StreetSpeedSnapshotSource(org.opentripplanner.traffic.StreetSpeedSnapshotSource) GraphUtils(org.opentripplanner.common.geometry.GraphUtils) StreetVertexIndexService(org.opentripplanner.routing.services.StreetVertexIndexService) Median(org.apache.commons.math3.stat.descriptive.rank.Median) DefaultStreetVertexIndexFactory(org.opentripplanner.routing.impl.DefaultStreetVertexIndexFactory) Geometry(com.vividsolutions.jts.geom.Geometry) TraverseMode(org.opentripplanner.routing.core.TraverseMode) com.google.common.collect(com.google.common.collect) FeedInfo(org.onebusaway.gtfs.model.FeedInfo) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) MavenVersion(org.opentripplanner.common.MavenVersion) TransferTable(org.opentripplanner.routing.core.TransferTable) StreetNotesService(org.opentripplanner.routing.services.notes.StreetNotesService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SampleFactory(org.opentripplanner.analyst.request.SampleFactory) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) TripPattern(org.opentripplanner.routing.edgetype.TripPattern) Stop(org.onebusaway.gtfs.model.Stop) Agency(org.onebusaway.gtfs.model.Agency) GraphUpdaterManager(org.opentripplanner.updater.GraphUpdaterManager) TimetableSnapshotSource(org.opentripplanner.updater.stoptime.TimetableSnapshotSource) StreetVertexIndexFactory(org.opentripplanner.routing.services.StreetVertexIndexFactory) java.util(java.util) AlertPatch(org.opentripplanner.routing.alertpatch.AlertPatch) WorldEnvelope(org.opentripplanner.util.WorldEnvelope) TDoubleList(gnu.trove.list.TDoubleList) TDoubleLinkedList(gnu.trove.list.linked.TDoubleLinkedList) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) NoFutureDates(org.opentripplanner.graph_builder.annotation.NoFutureDates) GraphUpdaterConfigurator(org.opentripplanner.updater.GraphUpdaterConfigurator) GraphBuilderAnnotation(org.opentripplanner.graph_builder.annotation.GraphBuilderAnnotation) GeometryIndex(org.opentripplanner.analyst.core.GeometryIndex) MortonVertexComparatorFactory(org.opentripplanner.routing.core.MortonVertexComparatorFactory) EdgeWithCleanup(org.opentripplanner.routing.edgetype.EdgeWithCleanup) Coordinate(com.vividsolutions.jts.geom.Coordinate) Logger(org.slf4j.Logger) Envelope(com.vividsolutions.jts.geom.Envelope) CalendarServiceImpl(org.onebusaway.gtfs.impl.calendar.CalendarServiceImpl) GraphBundle(org.opentripplanner.model.GraphBundle) DateTime(org.joda.time.DateTime) TransitStation(org.opentripplanner.routing.vertextype.TransitStation) Preferences(java.util.prefs.Preferences) java.io(java.io) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Deduplicator(org.opentripplanner.routing.trippattern.Deduplicator) PatternArriveVertex(org.opentripplanner.routing.vertextype.PatternArriveVertex) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TransitStop(org.opentripplanner.routing.vertextype.TransitStop) Coordinate(com.vividsolutions.jts.geom.Coordinate) TDoubleLinkedList(gnu.trove.list.linked.TDoubleLinkedList) Median(org.apache.commons.math3.stat.descriptive.rank.Median)

Example 23 with Median

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);
}
Also used : CauchyDistribution(org.apache.commons.math3.distribution.CauchyDistribution) DecimalOperators.modulusScalarFunction(com.facebook.presto.type.DecimalOperators.modulusScalarFunction) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 24 with Median

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));
}
Also used : BurnTestUtil.generateQueryMessage(org.apache.cassandra.transport.BurnTestUtil.generateQueryMessage) QueryMessage(org.apache.cassandra.transport.messages.QueryMessage) DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) BurnTestUtil.generateQueryMessage(org.apache.cassandra.transport.BurnTestUtil.generateQueryMessage) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) QueryMessage(org.apache.cassandra.transport.messages.QueryMessage) ByteBuf(io.netty.buffer.ByteBuf) ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicReference(java.util.concurrent.atomic.AtomicReference) QueryState(org.apache.cassandra.service.QueryState) ResultMessage(org.apache.cassandra.transport.messages.ResultMessage) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 25 with Median

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();
}
Also used : DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) DecimalFormat(java.text.DecimalFormat) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ListOrderedMap(org.apache.commons.collections4.map.ListOrderedMap)

Aggregations

Median (org.apache.commons.math3.stat.descriptive.rank.Median)35 RealMatrix (org.apache.commons.math3.linear.RealMatrix)29 IntStream (java.util.stream.IntStream)28 Collectors (java.util.stream.Collectors)24 Logger (org.apache.logging.log4j.Logger)24 Percentile (org.apache.commons.math3.stat.descriptive.rank.Percentile)22 DoubleStream (java.util.stream.DoubleStream)20 File (java.io.File)18 Array2DRowRealMatrix (org.apache.commons.math3.linear.Array2DRowRealMatrix)17 ParamUtils (org.broadinstitute.hellbender.utils.param.ParamUtils)16 List (java.util.List)15 ArrayList (java.util.ArrayList)14 JavaSparkContext (org.apache.spark.api.java.JavaSparkContext)14 UserException (org.broadinstitute.hellbender.exceptions.UserException)14 ReadCountCollection (org.broadinstitute.hellbender.tools.exome.ReadCountCollection)14 SimpleInterval (org.broadinstitute.hellbender.utils.SimpleInterval)14 VisibleForTesting (com.google.common.annotations.VisibleForTesting)13 java.util (java.util)13 DefaultRealMatrixChangingVisitor (org.apache.commons.math3.linear.DefaultRealMatrixChangingVisitor)12 LogManager (org.apache.logging.log4j.LogManager)12