Search in sources :

Example 6 with Floats_

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

the class Arima method armaBackcast.

// http://math.unice.fr/~frapetti/CorsoP/Chapitre_4_IMEA_1.pdf
// "Least squares estimation using backcasting procedure"
public Arima_ armaBackcast(float[] xs, float[] ars, float[] mas) {
    int length = xs.length;
    int p = ars.length;
    int q = mas.length;
    float[] xsp = Floats_.concat(new float[p], xs);
    float[] epq = new float[length + q];
    Arma arma = new Arma(ars, mas);
    for (int iter = 0; iter < 64; iter++) {
        // backcast
        // ep[t]
        // = (xs[t + q] - ep[t + q]
        // - ars[0] * xs[t + q - 1] - ... - ars[p - 1] * xs[t + q - p]
        // - mas[0] * ep[t + q - 1] - ... - mas[q - 2] * ep[t + 1]
        // ) / mas[q - 1]
        arma.backcast(xsp, epq);
        // forward recursion
        // ep[t] = xs[t]
        // - ars[0] * xs[t - 1] - ... - ars[p - 1] * xs[t - p]
        // - mas[0] * ep[t - 1] - ... - mas[q - 1] * ep[t - q]
        double error = arma.forwardRecursion(xsp, epq);
        // minimization
        // xs[t]
        // = ars[0] * xs[t - 1] + ... + ars[p - 1] * xs[t - p]
        // + mas[0] * ep[t - 1] + ... + mas[q - 1] * ep[t - q]
        // + ep[t]
        LinearRegression lr = stat.linearRegression(// 
        Ints_.range(// 
        length).map(t -> {
            int tp = t + p, tpm1 = tp - 1;
            int tq = t + q, tqm1 = tq - 1;
            FltStreamlet lrxs0 = Ints_.range(p).collect(Int_Flt.lift(i -> xsp[tpm1 - i]));
            FltStreamlet lrxs1 = Ints_.range(q).collect(Int_Flt.lift(i -> epq[tqm1 - i]));
            return FltObjPair.of(xsp[tp], Floats_.concat(lrxs0, lrxs1).toArray());
        }));
        System.out.println("iter " + iter + ", error = " + To.string(error) + lr);
        System.out.println();
        float[] coefficients = lr.coefficients();
        Floats_.copy(coefficients, 0, ars, 0, p);
        Floats_.copy(coefficients, p, mas, 0, q);
    }
    double x1 = arma.sum(xsp, epq);
    return new Arima_(ars, mas, (float) x1);
}
Also used : Arrays(java.util.Arrays) DblSource(suite.primitive.DblPrimitives.DblSource) Friends.min(suite.util.Friends.min) Statistic(suite.math.numeric.Statistic) Random(java.util.Random) To(suite.util.To) LinearRegression(suite.math.numeric.Statistic.LinearRegression) Friends.max(suite.util.Friends.max) Floats_(suite.primitive.Floats_) Vector(suite.math.linalg.Vector) Floats(suite.primitive.Floats) FltObjPair(suite.primitive.adt.pair.FltObjPair) FltStreamlet(suite.primitive.streamlet.FltStreamlet) Ints_(suite.primitive.Ints_) Int_Dbl(suite.primitive.Int_Dbl) Int_Flt(suite.primitive.Int_Flt) DblObjPair(suite.primitive.adt.pair.DblObjPair) FltStreamlet(suite.primitive.streamlet.FltStreamlet) LinearRegression(suite.math.numeric.Statistic.LinearRegression)

Example 7 with Floats_

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

the class Arima method armaIa.

// extended from
// "High Frequency Trading - A Practical Guide to Algorithmic Strategies and
// Trading Systems", Irene Aldridge, page 100
// xs[t]
// = ars[0] * xs[t - 1] + ... + ars[p - 1] * xs[t - p]
// + ep[t]
// + mas[0] * ep[t - 1] + ... + mas[q - 1] * ep[t - q]
private Arima_ armaIa(float[] xs, int p, int q) {
    int length = xs.length;
    int lengthp = length + p, lengthpm1 = lengthp - 1;
    int lengthq = length + q, lengthqm1 = lengthq - 1;
    int iter = 0;
    float[] xsp = new float[lengthp];
    float[][] epqByIter = new float[q][];
    Arrays.fill(xsp, 0, p, xs[0]);
    System.arraycopy(xs, 0, xsp, p, length);
    while (true) {
        int iter_ = iter;
        LinearRegression lr = stat.linearRegression(// 
        Ints_.range(// 
        length).map(t -> {
            int tp = t + p;
            int tq = t + q, tqm1 = tq - 1;
            float[] lrxs = // 
            Floats_.concat(Floats_.reverse(xsp, t, tp), // 
            Ints_.range(iter_).collect(Int_Flt.lift(i -> epqByIter[i][tqm1 - i]))).toArray();
            return FltObjPair.of(xsp[tp], lrxs);
        }));
        float[] coeffs = lr.coefficients();
        if (iter < q)
            System.arraycopy(lr.residuals, 0, epqByIter[iter++] = new float[lengthq], q, length);
        else {
            float[] ars = Floats.of(coeffs, 0, p).toArray();
            float[] mas = Floats.of(coeffs, p).toArray();
            double x1 = // 
            0d + // 
            Ints_.range(p).toDouble(Int_Dbl.sum(i -> ars[i] * xsp[lengthpm1 - i])) + Ints_.range(q).toDouble(Int_Dbl.sum(i -> mas[i] * epqByIter[i][lengthqm1 - i]));
            return new Arima_(ars, mas, (float) x1);
        }
    }
}
Also used : Arrays(java.util.Arrays) DblSource(suite.primitive.DblPrimitives.DblSource) Friends.min(suite.util.Friends.min) Statistic(suite.math.numeric.Statistic) Random(java.util.Random) To(suite.util.To) LinearRegression(suite.math.numeric.Statistic.LinearRegression) Friends.max(suite.util.Friends.max) Floats_(suite.primitive.Floats_) Vector(suite.math.linalg.Vector) Floats(suite.primitive.Floats) FltObjPair(suite.primitive.adt.pair.FltObjPair) FltStreamlet(suite.primitive.streamlet.FltStreamlet) Ints_(suite.primitive.Ints_) Int_Dbl(suite.primitive.Int_Dbl) Int_Flt(suite.primitive.Int_Flt) DblObjPair(suite.primitive.adt.pair.DblObjPair) LinearRegression(suite.math.numeric.Statistic.LinearRegression)

Example 8 with Floats_

use of suite.primitive.Floats_ 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 9 with Floats_

use of suite.primitive.Floats_ 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 10 with Floats_

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

the class Render method render.

public Image render(int width, int height, BiFun<Float, R3> f) {
    float scale = 1f / max(width, height);
    int centerX = width / 2, centerY = height / 2;
    float[] xs = Floats_.toArray(width + 1, x -> (x - centerX) * scale);
    float[] ys = Floats_.toArray(height + 1, y -> (y - centerY) * scale);
    return renderPixels(width, height, (IntInt_Obj<R3>) (x, y) -> {
        R3 color;
        try {
            color = f.apply(xs[x], ys[y]);
        } catch (Exception ex) {
            LogUtil.error(new RuntimeException("at (" + x + ", " + y + ")", ex));
            color = new R3(1d, 1d, 1d);
        }
        return color;
    });
}
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)

Aggregations

Floats_ (suite.primitive.Floats_)10 Ints_ (suite.primitive.Ints_)9 Arrays (java.util.Arrays)8 To (suite.util.To)8 Random (java.util.Random)7 Statistic (suite.math.numeric.Statistic)7 LinearRegression (suite.math.numeric.Statistic.LinearRegression)7 FltObjPair (suite.primitive.adt.pair.FltObjPair)7 Int_Flt (suite.primitive.Int_Flt)6 Friends.max (suite.util.Friends.max)6 Test (org.junit.Test)4 Vector (suite.math.linalg.Vector)4 DiscreteCosineTransform (suite.math.transform.DiscreteCosineTransform)4 LogUtil (suite.os.LogUtil)4 Int_Dbl (suite.primitive.Int_Dbl)4 Friends.min (suite.util.Friends.min)4 Quant (ts.Quant)4 Pair (suite.adt.pair.Pair)3 DblSource (suite.primitive.DblPrimitives.DblSource)3 IntFltPair (suite.primitive.adt.pair.IntFltPair)3