Search in sources :

Example 1 with Int_Flt

use of primal.primitive.Int_Flt 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)

Aggregations

Math.abs (java.lang.Math.abs)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Random (java.util.Random)1 Test (org.junit.jupiter.api.Test)1 Read (primal.MoreVerbs.Read)1 Equals (primal.Verbs.Equals)1 New (primal.Verbs.New)1 Right (primal.Verbs.Right)1 Pair (primal.adt.Pair)1 Fun (primal.fp.Funs.Fun)1 Log_ (primal.os.Log_)1 Int_Flt (primal.primitive.Int_Flt)1 FltObjPair (primal.primitive.adt.pair.FltObjPair)1 IntFltPair (primal.primitive.adt.pair.IntFltPair)1 AsDbl (primal.primitive.fp.AsDbl)1 Streamlet2 (primal.streamlet.Streamlet2)1 KmeansCluster (suite.algo.KmeansCluster)1 Statistic (suite.math.numeric.Statistic)1