Search in sources :

Example 6 with Streamlet_.forInt

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]);
}
Also used : Test(org.junit.jupiter.api.Test) TradeCfgImpl(suite.trade.data.TradeCfgImpl) KmeansCluster(suite.algo.KmeansCluster) Streamlet_.forInt(suite.util.Streamlet_.forInt) KmeansCluster(suite.algo.KmeansCluster) Test(org.junit.jupiter.api.Test)

Example 7 with Streamlet_.forInt

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);
    }
}
Also used : KmeansCluster(suite.algo.KmeansCluster) Arrays(java.util.Arrays) Fun(primal.fp.Funs.Fun) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) HashMap(java.util.HashMap) Random(java.util.Random) Math.abs(java.lang.Math.abs) Right(primal.Verbs.Right) New(primal.Verbs.New) Quant(suite.ts.Quant) Sina(suite.trade.data.Sina) Streamlet_.forInt(suite.util.Streamlet_.forInt) Instrument(suite.trade.Instrument) TradeCfg(suite.trade.data.TradeCfg) Map(java.util.Map) BollingerBands(suite.ts.BollingerBands) Int_Flt(primal.primitive.Int_Flt) DiscreteCosineTransform(suite.math.transform.DiscreteCosineTransform) Pair(primal.adt.Pair) TradeCfgImpl(suite.trade.data.TradeCfgImpl) IntFltPair(primal.primitive.adt.pair.IntFltPair) Statistic(suite.math.numeric.Statistic) To(suite.util.To) Log_(primal.os.Log_) TimeSeries(suite.ts.TimeSeries) Read(primal.MoreVerbs.Read) Test(org.junit.jupiter.api.Test) FltObjPair(primal.primitive.adt.pair.FltObjPair) Time(suite.trade.Time) AsDbl(primal.primitive.fp.AsDbl) DataSource(suite.trade.data.DataSource) As(suite.streamlet.As) Streamlet2(primal.streamlet.Streamlet2) Equals(primal.Verbs.Equals) TimeRange(suite.trade.TimeRange) Int_Flt(primal.primitive.Int_Flt) Test(org.junit.jupiter.api.Test)

Example 8 with Streamlet_.forInt

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);
}
Also used : LocalDateTime(java.time.LocalDateTime) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) Read(primal.MoreVerbs.Read) Streamlet_.forInt(suite.util.Streamlet_.forInt) InetAddress(java.net.InetAddress) Test(org.junit.jupiter.api.Test) Rethrow.ex(primal.statics.Rethrow.ex) Map(java.util.Map) Rethrow(primal.statics.Rethrow) ClusterProbeImpl(suite.net.cluster.impl.ClusterProbeImpl) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Sleep(primal.Verbs.Sleep) InetSocketAddress(java.net.InetSocketAddress) ClusterProbeImpl(suite.net.cluster.impl.ClusterProbeImpl) Test(org.junit.jupiter.api.Test)

Aggregations

Streamlet_.forInt (suite.util.Streamlet_.forInt)8 Map (java.util.Map)4 Test (org.junit.jupiter.api.Test)4 Read (primal.MoreVerbs.Read)4 Math.max (java.lang.Math.max)3 Math.min (java.lang.Math.min)3 HashMap (java.util.HashMap)3 Fun (primal.fp.Funs.Fun)3 Log_ (primal.os.Log_)3 As (suite.streamlet.As)3 IOException (java.io.IOException)2 Math.abs (java.lang.Math.abs)2 Math.log1p (java.lang.Math.log1p)2 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2 LocalDateTime (java.time.LocalDateTime)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Random (java.util.Random)2