use of org.apache.commons.math3.stat.descriptive.rank.Min 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.Min in project tetrad by cmu-phil.
the class Ling method pruneEdgesByResampling.
// ==============================PRIVATE METHODS====================//
/**
* This is the method used in Patrik's code.
*/
public TetradMatrix pruneEdgesByResampling(TetradMatrix data) {
TetradMatrix X = new TetradMatrix(data.transpose().toArray());
int npieces = 10;
int cols = X.columns();
int rows = X.rows();
int piecesize = (int) Math.floor(cols / npieces);
List<TetradMatrix> bpieces = new ArrayList<>();
List<TetradVector> diststdpieces = new ArrayList<>();
List<TetradVector> cpieces = new ArrayList<>();
for (int p = 0; p < npieces; p++) {
// % Select subset of data, and permute the variables to the causal order
// Xp = X(k,((p-1)*piecesize+1):(p*piecesize));
int p0 = (p) * piecesize;
int p1 = (p + 1) * piecesize - 1;
int[] range = range(p0, p1);
TetradMatrix Xp = X;
// % Remember to subract out the mean
// Xpm = mean(Xp,2);
// Xp = Xp - Xpm*ones(1,size(Xp,2));
//
// % Calculate covariance matrix
// cov = (Xp*Xp')/size(Xp,2);
TetradVector Xpm = new TetradVector(rows);
for (int i = 0; i < rows; i++) {
double sum = 0.0;
for (int j = 0; j < Xp.columns(); j++) {
sum += Xp.get(i, j);
}
Xpm.set(i, sum / Xp.columns());
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < Xp.columns(); j++) {
Xp.set(i, j, Xp.get(i, j) - Xpm.get(i));
}
}
TetradMatrix Xpt = Xp.transpose();
TetradMatrix cov = Xp.times(Xpt);
for (int i = 0; i < cov.rows(); i++) {
for (int j = 0; j < cov.columns(); j++) {
cov.set(i, j, cov.get(i, j) / Xp.columns());
}
}
// % Do QL decomposition on the inverse square root of cov
// [Q,L] = tridecomp(cov^(-0.5),'ql');
boolean posDef = LingUtils.isPositiveDefinite(cov);
if (!posDef) {
System.out.println("Covariance matrix is not positive definite.");
}
TetradMatrix sqrt = cov.sqrt();
;
TetradMatrix I = TetradMatrix.identity(rows);
TetradMatrix AI = I.copy();
TetradMatrix invSqrt = sqrt.inverse();
QRDecomposition qr = new QRDecomposition(invSqrt.getRealMatrix());
RealMatrix r = qr.getR();
// % The estimated disturbance-stds are one over the abs of the diag of L
// newestdisturbancestd = 1./diag(abs(L));
TetradVector newestdisturbancestd = new TetradVector(rows);
for (int t = 0; t < rows; t++) {
newestdisturbancestd.set(t, 1.0 / Math.abs(r.getEntry(t, t)));
}
//
for (int s = 0; s < rows; s++) {
for (int t = 0; t < min(s, cols); t++) {
r.setEntry(s, t, r.getEntry(s, t) / r.getEntry(s, s));
}
}
// % Calculate corresponding B
// bnewest = eye(dims)-L;
TetradMatrix bnewest = TetradMatrix.identity(rows);
bnewest = bnewest.minus(new TetradMatrix(r));
TetradVector cnewest = new TetradMatrix(r).times(Xpm);
bpieces.add(bnewest);
diststdpieces.add(newestdisturbancestd);
cpieces.add(cnewest);
}
//
// for i=1:dims,
// for j=1:dims,
//
// themean = mean(Bpieces(i,j,:));
// thestd = std(Bpieces(i,j,:));
// if abs(themean)<prunefactor*thestd,
// Bfinal(i,j) = 0;
// else
// Bfinal(i,j) = themean;
// end
//
// end
// end
TetradMatrix means = new TetradMatrix(rows, rows);
TetradMatrix stds = new TetradMatrix(rows, rows);
TetradMatrix BFinal = new TetradMatrix(rows, rows);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
double sum = 0.0;
for (int y = 0; y < npieces; y++) {
sum += bpieces.get(y).get(i, j);
}
double themean = sum / (npieces);
double sumVar = 0.0;
for (int y = 0; y < npieces; y++) {
sumVar += Math.pow((bpieces.get(y).get(i, j)) - themean, 2);
}
double thestd = Math.sqrt(sumVar / (npieces));
means.set(i, j, themean);
stds.set(i, j, thestd);
if (Math.abs(themean) < threshold * thestd) {
// getPruneFactor() * thestd) {
BFinal.set(i, j, 0);
} else {
BFinal.set(i, j, themean);
}
}
}
return BFinal;
}
use of org.apache.commons.math3.stat.descriptive.rank.Min in project MPW by shineangelic.
the class ChartUtils method drawHashrateHistory.
static void drawHashrateHistory(TextView titleTextView, LinkedMap<Date, HomeStatsChartData> storia, LineView chart, GranularityEnum grane) {
SummaryStatistics stats = new SummaryStatistics();
ArrayList<Float> dataList = new ArrayList<>();
ArrayList<Float> dataListMax = new ArrayList<>();
ArrayList<Float> dataListMin = new ArrayList<>();
ArrayList<String> labelsArr = new ArrayList<>();
List<Date> dates = storia.asList();
// HomeStats campione = storia.values().iterator().next();
for (Date date2 : dates) {
labelsArr.add(getLabelFormat(grane, date2));
dataList.add(Utils.condenseHashRate(storia.get(date2).getHashrate()));
dataListMax.add(Utils.condenseHashRate(storia.get(date2).getHashrateMax()));
dataListMin.add(Utils.condenseHashRate(storia.get(date2).getHashrateMin()));
stats.addValue(storia.get(date2).getHashrate());
stats.addValue(storia.get(date2).getHashrateMax());
stats.addValue(storia.get(date2).getHashrateMin());
}
String curHashRateTxt = "--";
try {
curHashRateTxt = Utils.formatHashrate(storia.get(dates.get(dataList.size() - 1)).getHashrate());
} catch (Exception re) {
Log.w(TAG, "history too short?", re);
}
titleTextView.setText("Hashrate History chart " + "(avg: " + Utils.formatHashrate((long) stats.getMean()) + ", max: " + Utils.formatHashrate((long) stats.getMax()) + ", min: " + Utils.formatHashrate((long) stats.getMin()) + ", now: " + curHashRateTxt + ", std dev: " + Utils.formatHashrate((long) stats.getStandardDeviation()) + ")");
ArrayList<ArrayList<Float>> dataLists = new ArrayList<>();
List<Integer> colArr = new ArrayList<>();
dataLists.add(dataListMax);
colArr.add(ResourcesCompat.getColor(titleTextView.getResources(), R.color.colorPrimaryAlpha, null));
dataLists.add(dataListMin);
colArr.add(ResourcesCompat.getColor(titleTextView.getResources(), R.color.colorAccentAlpha, null));
dataLists.add(dataList);
chart.setShowPopup(LineView.SHOW_POPUPS_MAXMIN_ONLY);
// optional
chart.setDrawDotLine(false);
chart.setBottomTextList(labelsArr);
colArr.add(Color.DKGRAY);
/*colorArray = new int[]{ ResourcesCompat.getColor(titleTextView.getResources(), enableMax?R.color.colorPrimaryAlpha:android.R.color.transparent,null),
ResourcesCompat.getColor(titleTextView.getResources(), enableMin?R.color.colorAccentAlpha:android.R.color.transparent,null),
Color.DKGRAY,};*/
int[] ret = new int[colArr.size()];
int i = 0;
for (Integer e : colArr) ret[i++] = e.intValue();
chart.setColorArray(ret);
Assert.assertTrue(dataLists.size() == colArr.size());
// or lineView.setFloatDataList(floatDataLists)
chart.setFloatDataList(dataLists);
chart.requestLayout();
chart.invalidate();
}
use of org.apache.commons.math3.stat.descriptive.rank.Min in project MPW by shineangelic.
the class ChartUtils method drawWalletHashRateHistory.
public static void drawWalletHashRateHistory(TextView titleTextView, LineView chart, LinkedMap<Date, Wallet> dateWalletLinkedMap, GranularityEnum grane) {
SummaryStatistics stats = new SummaryStatistics();
ArrayList<Float> dataList = new ArrayList<>();
ArrayList<String> labelsArr = new ArrayList<>();
List<Date> dates = dateWalletLinkedMap.asList();
Wallet campione = dateWalletLinkedMap.values().iterator().next();
for (Date date2 : dates) {
labelsArr.add(getLabelFormat(grane, date2));
dataList.add(Utils.condenseHashRate(dateWalletLinkedMap.get(date2).getHashrate()));
stats.addValue(dateWalletLinkedMap.get(date2).getHashrate());
}
titleTextView.setText("Wallet Hashrate History " + "(avg: " + Utils.formatHashrate((long) stats.getMean()) + ", max: " + Utils.formatHashrate((long) stats.getMax()) + ", min: " + Utils.formatHashrate((long) stats.getMin()) + ", now: " + Utils.formatHashrate(dateWalletLinkedMap.get(dates.get(dataList.size() - 1)).getHashrate()) + ", std dev: " + Utils.formatHashrate((long) stats.getStandardDeviation()) + ")");
ArrayList<ArrayList<Float>> dataLists = new ArrayList<>();
dataLists.add(dataList);
chart.setShowPopup(LineView.SHOW_POPUPS_All);
// optional
chart.setDrawDotLine(false);
chart.setBottomTextList(labelsArr);
chart.setColorArray(new int[] { Color.DKGRAY, Color.CYAN });
// or lineView.setFloatDataList(floatDataLists)
chart.setFloatDataList(dataLists);
}
use of org.apache.commons.math3.stat.descriptive.rank.Min in project chordatlas by twak.
the class Prof method findProfileLines.
/**
* We find an initial base offset. Then we cluster the start point of all
* (clean) profiles. If any are a good distance from the initial base, we
* add those as their own profile lines.
*
* The original line is offset by the remaiing data.
*/
public static List<SuperLine> findProfileLines(Collection<Prof> profiles, Line3d line) {
List<SuperLine> out = new ArrayList();
// PaintThing.debug.clear();
SuperLine superLine = new SuperLine(line.start.x, line.start.z, line.end.x, line.end.z);
double outLen = superLine.length();
double min = Double.MAX_VALUE, max = -Double.MAX_VALUE;
Cache<Prof, Double> vLength = new Cache<Prof, Double>() {
@Override
public Double create(Prof i) {
return i.verticalLength(0.5);
}
};
double vLen = profiles.stream().mapToDouble(p -> vLength.get(p)).sum();
boolean useVertical = vLen / profiles.size() > 1;
class Wrapper implements Clusterable {
double[] pt;
public Wrapper(Point2d pt) {
this.pt = new double[] { pt.x, pt.y };
}
@Override
public double[] getPoint() {
return pt;
}
}
List<Wrapper> toCluster = new ArrayList();
List<Double> baseLineOffset = new ArrayList();
for (Prof p : profiles) {
if (// vLen / (5*profiles.size()))
useVertical && vLength.get(p) < 1)
continue;
Prof clean = p.parameterize();
Point2d pt = clean.get(0);
Point3d pt3 = clean.to3d(pt);
double ppram = superLine.findPPram(new Point2d(pt3.x, pt3.z));
baseLineOffset.add(pt.x);
toCluster.add(new Wrapper(new Point2d(pt.x, ppram * outLen)));
min = Math.min(min, ppram);
max = Math.max(max, ppram);
}
if (min == max || toCluster.isEmpty())
return out;
if (true) {
baseLineOffset.sort(Double::compareTo);
double modeBaselineOffset = baseLineOffset.get(baseLineOffset.size() / 2);
DBSCANClusterer<Wrapper> cr = new DBSCANClusterer<>(1.5, 0);
List<Cluster<Wrapper>> results = cr.cluster(toCluster);
Iterator<Cluster<Wrapper>> cit = results.iterator();
while (cit.hasNext()) {
Cluster<Wrapper> cw = cit.next();
if (cw.getPoints().size() < 2 / TweedSettings.settings.profileHSampleDist) {
cit.remove();
double cMeanY = cw.getPoints().stream().mapToDouble(x -> x.pt[1]).average().getAsDouble();
double bestDist = Double.MAX_VALUE;
Cluster<Wrapper> bestWrapper = null;
for (Cluster<Wrapper> near : results) {
double meanY = near.getPoints().stream().mapToDouble(x -> x.pt[1]).average().getAsDouble();
double dist = Math.abs(meanY - cMeanY);
if (dist < bestDist) {
bestDist = dist;
bestWrapper = near;
}
}
if (bestWrapper != null)
bestWrapper.getPoints().addAll(cw.getPoints());
}
}
{
baseLineOffset.clear();
int c = 0;
for (Cluster<Wrapper> cw : results) {
double[] minMax = cw.getPoints().stream().map(p -> new double[] { p.pt[1] }).collect(new InAxDoubleArray());
double[] offsetA = cw.getPoints().stream().mapToDouble(p -> p.pt[0]).sorted().toArray();
double offset = offsetA[offsetA.length / 2];
if (offset - modeBaselineOffset < 1) {
for (Wrapper w : cw.getPoints()) baseLineOffset.add(w.pt[0]);
continue;
}
SuperLine sl = new SuperLine(superLine.fromPPram(minMax[0] / outLen), superLine.fromPPram(minMax[1] / outLen));
sl.moveLeft(offset);
out.add(sl);
List<Point2d> pts = cw.getPoints().stream().map(w -> new Point2d(w.pt[0], w.pt[1])).collect(Collectors.toList());
PaintThing.debug(Rainbow.getColour(c++), 1, pts);
}
}
}
Point2d nStart = superLine.fromPPram(min), nEnd = superLine.fromPPram(max);
superLine.start = nStart;
superLine.end = nEnd;
baseLineOffset.sort(Double::compare);
if (!baseLineOffset.isEmpty())
superLine.moveLeft(baseLineOffset.get(baseLineOffset.size() / 2));
out.add(0, superLine);
return out;
}
Aggregations