Search in sources :

Example 1 with Int_Flt

use of suite.primitive.Int_Flt in project suite by stupidsing.

the class VirtualVectorUtil method add.

public VirtualVector add(VirtualVector vv1) {
    VirtualVector vv0 = this;
    Int_Flt f0 = vv0.get;
    Int_Flt f1 = vv1.get;
    return VirtualVectorUtil.checkSizes(vv0, vv1, i -> f0.apply(i) + f1.apply(i));
}
Also used : Int_Flt(suite.primitive.Int_Flt)

Example 2 with Int_Flt

use of suite.primitive.Int_Flt in project suite by stupidsing.

the class BollingerBands method meanVariances.

public Pair<float[], float[]> meanVariances(VirtualVector v, int backPos0, int backPos1) {
    int length = v.length;
    Int_Flt fun = v.get;
    float[] means = new float[length];
    float[] variances = new float[length];
    int d = backPos0 - backPos1;
    double il = 1d / d;
    int i = 0, j;
    double sum = 0d;
    double sumSq = 0d;
    for (; i < d; i++) {
        float f = fun.apply(i);
        sum += f;
        sumSq += f * f;
    }
    for (; (j = i + backPos1) < length; i++) {
        double mean = sum * il;
        means[j] = (float) mean;
        variances[j] = (float) (sumSq * il - mean * mean);
        float f0 = fun.apply(i - d);
        float fx = fun.apply(i);
        sum += fx - f0;
        sumSq += fx * fx - f0 * f0;
    }
    return Pair.of(means, variances);
}
Also used : Int_Flt(suite.primitive.Int_Flt)

Example 3 with Int_Flt

use of suite.primitive.Int_Flt in project suite by stupidsing.

the class StatisticalArbitrageTest method testReturnDistribution.

// Naive Bayes return prediction
@Test
public void testReturnDistribution() {
    float[] prices = cfg.dataSource(Asset.hsiSymbol).range(period).prices;
    int maxTor = 16;
    IntObjMap<float[]> differencesByTor = // 
    Ints_.range(1, // 
    maxTor).mapIntObj(tor -> {
        float[] differences = ts.differences(tor, prices);
        Arrays.sort(differences);
        return differences;
    }).toMap();
    for (int tor = 1; tor < maxTor; tor++) System.out.println("tor = " + tor + ", " + stat.moments(differencesByTor.get(tor)));
    Int_Flt predictFun = t -> {
        double[][] cpsArray = // 
        Ints_.range(1, // 
        maxTor).map(tor -> {
            float[] differences = differencesByTor.get(tor);
            int length = differences.length;
            // cumulative probabilities
            double[] cps = new double[11];
            for (int cpsi = 0, predDiff = -500; predDiff <= 500; cpsi++, predDiff += 100) {
                float f = prices[t - 1] + predDiff - prices[t - tor];
                int i = 0;
                while (i < length && differences[i] < f) i++;
                cps[cpsi] = i / (double) length;
            }
            return cps;
        }).toArray(double[].class);
        Map<Double, Double> probabilities = new HashMap<>();
        for (int cpsi = 0, predDiff = -500; predDiff < 500; cpsi++, predDiff += 100) {
            int cpsi_ = cpsi;
            double sum = // 
            Ints_.range(1, // 
            maxTor).map(// 
            i -> i).toDouble(Obj_Dbl.sum(tor -> {
                double 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().t0.floatValue();
    };
    for (int t = maxTor + 1; t < prices.length; t++) {
        float 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) Read(suite.streamlet.Read) LogUtil(suite.os.LogUtil) IntFltPair(suite.primitive.adt.pair.IntFltPair) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) HashMap(java.util.HashMap) Random(java.util.Random) Sina(suite.trade.data.Sina) Fun(suite.util.FunUtil.Fun) ConfigurationImpl(suite.trade.data.ConfigurationImpl) String_(suite.util.String_) Map(java.util.Map) FltObjPair(suite.primitive.adt.pair.FltObjPair) TimeSeries(ts.TimeSeries) Ints_(suite.primitive.Ints_) DiscreteCosineTransform(suite.math.transform.DiscreteCosineTransform) Streamlet2(suite.streamlet.Streamlet2) Statistic(suite.math.numeric.Statistic) Test(org.junit.Test) To(suite.util.To) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Quant(ts.Quant) LinearRegression(suite.math.numeric.Statistic.LinearRegression) IntObjMap(suite.primitive.adt.map.IntObjMap) BollingerBands(ts.BollingerBands) Pair(suite.adt.pair.Pair) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Floats_(suite.primitive.Floats_) Configuration(suite.trade.data.Configuration) DataSource(suite.trade.data.DataSource) As(suite.streamlet.As) Asset(suite.trade.Asset) TimeRange(suite.trade.TimeRange) Int_Flt(suite.primitive.Int_Flt) Int_Flt(suite.primitive.Int_Flt) HashMap(java.util.HashMap) Map(java.util.Map) IntObjMap(suite.primitive.adt.map.IntObjMap) Test(org.junit.Test)

Aggregations

Int_Flt (suite.primitive.Int_Flt)3 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Random (java.util.Random)1 Test (org.junit.Test)1 Pair (suite.adt.pair.Pair)1 KmeansCluster (suite.algo.KmeansCluster)1 Statistic (suite.math.numeric.Statistic)1 LinearRegression (suite.math.numeric.Statistic.LinearRegression)1 DiscreteCosineTransform (suite.math.transform.DiscreteCosineTransform)1 LogUtil (suite.os.LogUtil)1 Obj_Dbl (suite.primitive.DblPrimitives.Obj_Dbl)1 Floats_ (suite.primitive.Floats_)1 Ints_ (suite.primitive.Ints_)1 IntObjMap (suite.primitive.adt.map.IntObjMap)1 FltObjPair (suite.primitive.adt.pair.FltObjPair)1 IntFltPair (suite.primitive.adt.pair.IntFltPair)1 As (suite.streamlet.As)1 Read (suite.streamlet.Read)1