Search in sources :

Example 21 with Ints_

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

the class StatisticalArbitrageTest method testAutoRegressivePowersOfTwo.

@Test
public void testAutoRegressivePowersOfTwo() {
    int power = 6;
    DataSource ds = cfg.dataSource(Asset.hsiSymbol).cleanse();
    float[] prices = ds.prices;
    float[][] mas = To.array(power, float[].class, p -> ma.movingAvg(prices, 1 << p));
    float[] returns = ts.returns(prices);
    LinearRegression lr = stat.linearRegression(// 
    Ints_.range(1 << power, // 
    prices.length).map(i -> FltObjPair.of(returns[i], Floats_.toArray(power, p -> mas[p][i - (1 << p)]))));
    System.out.println(lr);
}
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) LinearRegression(suite.math.numeric.Statistic.LinearRegression) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource) Test(org.junit.Test)

Example 22 with Ints_

use of suite.primitive.Ints_ 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)

Example 23 with Ints_

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

the class StatisticalArbitrageTest method testCointegration.

// Auto-regressive test
@Test
public void testCointegration() {
    // 0004.HK, 0020.HK
    // 0011.HK, 0005.HK
    int tor = 8;
    String symbol0 = "0004.HK";
    String symbol1 = "0945.HK";
    AlignKeyDataSource<String> akds = cfg.dataSources(period, Read.each(symbol0, symbol1));
    Map<String, float[]> pricesBySymbol = akds.dsByKey.mapValue(DataSource::returns).toMap();
    int length = akds.ts.length;
    float[] prices0 = pricesBySymbol.get(symbol0);
    float[] prices1 = pricesBySymbol.get(symbol1);
    LinearRegression lr = stat.linearRegression(// 
    Ints_.range(tor, // 
    length).map(i -> FltObjPair.of(prices1[i], Floats_.toArray(tor, j -> prices0[i + j - tor]))));
    System.out.println(lr);
}
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) LinearRegression(suite.math.numeric.Statistic.LinearRegression) Test(org.junit.Test)

Example 24 with Ints_

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

the class Render method renderPixels.

public Image renderPixels(int width, int height, IntInt_Obj<R3> f) {
    int nThreads = Constants.nThreads;
    int[] txs = Ints_.toArray(nThreads + 1, i -> width * i / nThreads);
    R3[][] pixels = new R3[width][height];
    List<Thread> threads = // 
    Ints_.range(// 
    nThreads).map(t -> Thread_.newThread(() -> {
        for (int x = txs[t]; x < txs[t + 1]; x++) for (int y = 0; y < height; y++) pixels[x][y] = f.apply(x, y);
    })).toList();
    Thread_.startJoin(threads);
    Image image = new Image(width, height, BufferedImage.TYPE_INT_RGB);
    for (int x = 0; x < width; x++) for (int y = 0; y < height; y++) {
        R3 pixel = limit(pixels[x][y]);
        image.setRGB(x, y, new Color(pixel.x, pixel.y, pixel.z).getRGB());
    }
    return image;
}
Also used : Color(java.awt.Color) Friends.min(suite.util.Friends.min) LogUtil(suite.os.LogUtil) BufferedImage(java.awt.image.BufferedImage) Constants(suite.Constants) R3(suite.math.R3) IntInt_Obj(suite.primitive.IntInt_Obj) Thread_(suite.util.Thread_) Friends.max(suite.util.Friends.max) List(java.util.List) Floats_(suite.primitive.Floats_) JLabel(javax.swing.JLabel) ImageIcon(javax.swing.ImageIcon) Ints_(suite.primitive.Ints_) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) BiFun(suite.util.FunUtil2.BiFun) R3(suite.math.R3) Color(java.awt.Color) BufferedImage(java.awt.image.BufferedImage)

Aggregations

Ints_ (suite.primitive.Ints_)24 To (suite.util.To)14 Arrays (java.util.Arrays)12 Statistic (suite.math.numeric.Statistic)12 Friends.max (suite.util.Friends.max)12 List (java.util.List)10 LinearRegression (suite.math.numeric.Statistic.LinearRegression)10 FltObjPair (suite.primitive.adt.pair.FltObjPair)10 Pair (suite.adt.pair.Pair)9 Floats_ (suite.primitive.Floats_)9 Read (suite.streamlet.Read)9 Friends.min (suite.util.Friends.min)9 Random (java.util.Random)8 Int_Flt (suite.primitive.Int_Flt)8 As (suite.streamlet.As)8 Test (org.junit.Test)7 Time (suite.trade.Time)6 Trade_ (suite.trade.Trade_)6 Configuration (suite.trade.data.Configuration)6 ConfigurationImpl (suite.trade.data.ConfigurationImpl)6