Search in sources :

Example 1 with New

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();
}
Also used : NioClusterMap(suite.net.cluster.impl.NioClusterMap) Fail.fail(primal.statics.Fail.fail) Fun(primal.fp.Funs.Fun) Source(primal.fp.Funs.Source) LocalDateTime(java.time.LocalDateTime) IOException(java.io.IOException) Random(java.util.Random) Start(primal.Verbs.Start) New(primal.Verbs.New) Log_(primal.os.Log_) InetSocketAddress(java.net.InetSocketAddress) Sink(primal.fp.Funs.Sink) Read(primal.MoreVerbs.Read) Streamlet_.forInt(suite.util.Streamlet_.forInt) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) Test(org.junit.jupiter.api.Test) Streamlet(primal.streamlet.Streamlet) Rethrow.ex(primal.statics.Rethrow.ex) Rethrow(primal.statics.Rethrow) Int_Obj(primal.primitive.IntPrim.Int_Obj) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Sleep(primal.Verbs.Sleep) NioCluster(suite.net.cluster.impl.NioCluster) NioClusterMap(suite.net.cluster.impl.NioClusterMap) Start(primal.Verbs.Start) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) IOException(java.io.IOException) NioCluster(suite.net.cluster.impl.NioCluster) Sink(primal.fp.Funs.Sink) Test(org.junit.jupiter.api.Test)

Example 2 with New

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);
    }
}
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 3 with New

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;
}
Also used : ObjIntMap(primal.primitive.adt.map.ObjIntMap) ReadInt(primal.primitive.IntMoreVerbs.ReadInt) DblObjPair(primal.primitive.adt.pair.DblObjPair) New(primal.Verbs.New) Read(primal.MoreVerbs.Read) IntDblPair(primal.primitive.adt.pair.IntDblPair) ArrayList(java.util.ArrayList) IntObjMap(primal.primitive.adt.map.IntObjMap) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Vector(suite.math.linalg.Vector) Map(java.util.Map) LiftInt(primal.primitive.IntMoreVerbs.LiftInt) Obj_Int(primal.primitive.IntPrim.Obj_Int) Left(primal.Verbs.Left) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntObjMap(primal.primitive.adt.map.IntObjMap)

Aggregations

Read (primal.MoreVerbs.Read)3 New (primal.Verbs.New)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Random (java.util.Random)2 Test (org.junit.jupiter.api.Test)2 Fun (primal.fp.Funs.Fun)2 Log_ (primal.os.Log_)2 IOException (java.io.IOException)1 Math.abs (java.lang.Math.abs)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 LocalDateTime (java.time.LocalDateTime)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Equals (primal.Verbs.Equals)1 Left (primal.Verbs.Left)1