use of primal.Verbs.New in project suite by stupidsing.
the class NioClusterMapTest method testClusterMap.
@Test
public void testClusterMap() throws IOException {
var nNodes = 3;
var peers = forInt(nNodes).map2(i -> "NODE" + i, i -> new InetSocketAddress(localHost, 3000 + i)).toMap();
var clusters = //
Read.from2(//
peers).keys().map2(name -> name, //
name -> ex(() -> new NioCluster(name, peers))).toMap();
for (var cluster : clusters.values()) cluster.start();
var peerNames = new ArrayList<>(peers.keySet());
var clMap = //
Read.from2(//
peers).keys().map2(name -> name, //
name -> new NioClusterMap<Integer, String>(clusters.get(name))).toMap();
Sleep.quietly(5 * 1000);
System.out.println("=== CLUSTER FORMED (" + LocalDateTime.now() + ") ===\n");
Source<NioClusterMap<Integer, String>> peerf = () -> clMap.get(peerNames.get(random.nextInt(nNodes)));
Int_Obj<Sink<Runnable>> setf = i -> cont -> peerf.g().set(i, Integer.toString(i), v0 -> cont.run(), fail);
Int_Obj<Sink<Runnable>> getf = i -> cont -> peerf.g().get(i, v -> {
assertEquals(Integer.toString(i), v);
cont.run();
}, fail);
Fun<NioCluster, Sink<Runnable>> closef = cluster -> cont -> {
try {
cluster.stop();
System.out.println("=== CLUSTER STOPPED (" + LocalDateTime.now() + ") ===\n");
} catch (IOException ex) {
fail(ex);
}
cont.run();
};
var sinks = //
Streamlet.concat(//
forInt(9).map(setf), //
forInt(9).map(getf), Read.from2(clusters).values().map(closef)).toList();
new Object() {
public void run(int i) {
if (i < sinks.size())
sinks.get(i).f(() -> run(i + 1));
}
}.run(0);
Read.from2(clusters).values().map(cluster -> New.thread(cluster::run)).collect(Start::thenJoin);
for (var cluster : clusters.values()) cluster.close();
}
use of primal.Verbs.New in project suite by stupidsing.
the class StatisticalArbitrageTest method testReturnDistribution.
// Naive Bayes return prediction
@Test
public void testReturnDistribution() {
var prices = cfg.dataSource(Instrument.hsiSymbol).range(period).prices;
var maxTor = 16;
var differencesByTor = //
forInt(1, maxTor).mapIntObj(tor -> {
var differences = ts.differences(tor, prices);
Arrays.sort(differences);
return differences;
}).toMap();
for (var tor = 1; tor < maxTor; tor++) System.out.println("tor = " + tor + ", " + stat.moments(differencesByTor.get(tor)));
Int_Flt predictFun = t -> {
var cpsArray = //
forInt(1, maxTor).map(tor -> {
var differences = differencesByTor.get(tor);
var length = differences.length;
// cumulative probabilities
var cps = new double[11];
for (int cpsi = 0, predDiff = -500; predDiff <= 500; cpsi++, predDiff += 100) {
var f = prices[t - 1] + predDiff - prices[t - tor];
var i = 0;
while (i < length && differences[i] < f) i++;
cps[cpsi] = i / (double) length;
}
return cps;
}).toArray(double[].class);
var probabilities = new HashMap<Double, Double>();
for (int cpsi = 0, predDiff = -500; predDiff < 500; cpsi++, predDiff += 100) {
var cpsi_ = cpsi;
var sum = //
forInt(1, maxTor).map(//
i -> i).toDouble(AsDbl.sum(tor -> {
var probability = cpsArray[tor - 1][cpsi_ + 1] - cpsArray[tor - 1][cpsi_];
return 1d / probability;
}));
probabilities.put(predDiff + 100d / 2d, sum);
}
return //
Read.from2(//
probabilities).sortByValue(//
(p0, p1) -> Double.compare(p1, p0)).first().k.floatValue();
};
for (var t = maxTor + 1; t < prices.length; t++) {
var predicted = prices[t - 1] + predictFun.apply(t);
System.out.println(//
"t = " + t + ", actual = " + //
prices[t] + ", predicted = " + predicted);
}
}
use of primal.Verbs.New in project suite by stupidsing.
the class KmeansCluster method kNearestNeighbor.
public int kNearestNeighbor(List<float[]> points, float[] point0) {
var map = new IntObjMap<AtomicInteger>();
//
Read.from(//
points).index().map(//
(i, point) -> IntDblPair.of(i, sqdist(point0, point))).sortBy(//
pair -> pair.t1).take(//
points.size()).forEach(bin -> map.computeIfAbsent(bin.t0, c -> new AtomicInteger()).incrementAndGet());
return ReadInt.from2(map).min((k, v) -> -v.v.get()).k;
}
Aggregations