use of suite.util.Streamlet_.forInt in project suite by stupidsing.
the class CandlestickPatternTest method test.
@Test
public void test() {
var ds = cfg.dataSource("0005.HK");
var k = 6;
var vectors = forInt(ds.ts.length).map(t -> {
var price = ds.prices[t];
var invPrice = 1d / price;
var o = (float) (ds.opens[t] * invPrice);
var c = (float) (ds.closes[t] * invPrice);
var l = (float) (ds.lows[t] * invPrice);
var h = (float) (ds.highs[t] * invPrice);
var v = ds.volumes[t];
return new float[] { o, c, l, h, v };
}).toList();
var kmc = new KmeansCluster(5).kMeansCluster(vectors, k, 99);
var hist = new int[k];
var ft = new int[k][k];
for (var c : kmc) hist[c]++;
for (var i = 1; i < kmc.length; i++) ft[kmc[i - 1]][kmc[i]]++;
for (var i : forInt(k)) System.out.println("count " + i + " = " + hist[i]);
for (var s0 : forInt(k)) for (var s1 : forInt(k)) System.out.println("from = " + s0 + ", to = " + s1 + ", count = " + ft[s0][s1]);
}
use of suite.util.Streamlet_.forInt 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 suite.util.Streamlet_.forInt in project suite by stupidsing.
the class ClusterProbeTest method test.
@Test
public void test() throws IOException {
var nNodes = 3;
var peers = forInt(nNodes).map2(i -> "NODE" + i, i -> new InetSocketAddress(localHost, 3000 + i)).toMap();
var probes = //
Read.from2(//
peers).keys().<String, ClusterProbe>map2(name -> name, //
name -> ex(() -> new ClusterProbeImpl(name, peers))).toMap();
for (var probe : probes.values()) probe.start();
Sleep.quietly(10 * 1000);
System.out.println("=== CLUSTER FORMED (" + LocalDateTime.now() + ") ===\n");
dumpActivePeers(probes);
assertActiveNodesSize(nNodes, probes);
for (var probe : probes.values()) probe.stop();
Sleep.quietly(5 * 1000);
System.out.println("=== CLUSTER STOPPED (" + LocalDateTime.now() + ") ===\n");
dumpActivePeers(probes);
assertActiveNodesSize(0, probes);
}
Aggregations