Search in sources :

Example 1 with LinearRegression

use of suite.math.numeric.Statistic.LinearRegression in project suite by stupidsing.

the class Arch method arch.

public float[] arch(float[] ys, int p, int q) {
    // auto regressive
    int length = ys.length;
    float[][] xs0 = To.array(length, float[].class, i -> copyPadZeroes(ys, i - p, i));
    LinearRegression lr0 = stat.linearRegression(xs0, ys, null);
    float[] variances = To.vector(lr0.residuals, residual -> residual * residual);
    // conditional heteroskedasticity
    LinearRegression lr1 = stat.linearRegression(// 
    Ints_.range(// 
    length).map(i -> FltObjPair.of(variances[i], copyPadZeroes(variances, i - p, i))));
    return Floats_.concat(lr0.coefficients, lr1.coefficients);
}
Also used : Friends.max(suite.util.Friends.max) Arrays(java.util.Arrays) DblSource(suite.primitive.DblPrimitives.DblSource) Statistic(suite.math.numeric.Statistic) Floats_(suite.primitive.Floats_) FltObjPair(suite.primitive.adt.pair.FltObjPair) Random(java.util.Random) To(suite.util.To) Ints_(suite.primitive.Ints_) LinearRegression(suite.math.numeric.Statistic.LinearRegression) Int_Dbl(suite.primitive.Int_Dbl) LinearRegression(suite.math.numeric.Statistic.LinearRegression)

Example 2 with LinearRegression

use of suite.math.numeric.Statistic.LinearRegression in project suite by stupidsing.

the class FactorLr method ols.

private LinearRegression ols(DataSource rds0, TimeRange period) {
    DataSource ys = rds0.range(period);
    float[][] returns_ = // 
    Read.from(// 
    indexPrices).map(// 
    prices -> DataSource.of(timestamps, prices).range(period).alignBeforePrices(ys.ts).returns()).toArray(float[].class);
    float[][] xs = mtx.transpose(returns_);
    return stat.linearRegression(xs, ys.returns(), indexSymbols.toArray(String.class));
}
Also used : BackAllocator(suite.trade.backalloc.BackAllocator) Read(suite.streamlet.Read) Streamlet2(suite.streamlet.Streamlet2) HkexUtil(suite.trade.data.HkexUtil) Statistic(suite.math.numeric.Statistic) Matrix(suite.math.linalg.Matrix) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) Quant(ts.Quant) LinearRegression(suite.math.numeric.Statistic.LinearRegression) List(java.util.List) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) Configuration(suite.trade.data.Configuration) Map(java.util.Map) DataSource(suite.trade.data.DataSource) As(suite.streamlet.As) Ints_(suite.primitive.Ints_) Asset(suite.trade.Asset) TimeRange(suite.trade.TimeRange) DataSourceView(suite.trade.data.DataSourceView) Int_Flt(suite.primitive.Int_Flt) AlignKeyDataSource(suite.trade.data.DataSource.AlignKeyDataSource) DataSource(suite.trade.data.DataSource)

Example 3 with LinearRegression

use of suite.math.numeric.Statistic.LinearRegression in project suite by stupidsing.

the class StatisticTest method testLinearRegression.

@Test
public void testLinearRegression() {
    int m = 7, n = 9;
    float[] expect = Floats_.toArray(m, j -> random.nextFloat());
    float[][] xs = To.matrix(n, m, (i, j) -> random.nextFloat());
    LinearRegression lr = stat.linearRegression(// 
    Read.from(// 
    xs).map(x -> FltObjPair.of((float) (vec.dot(expect, x) + random.nextGaussian() * .01f), x)));
    Dump.out(lr);
    float[] actual = lr.coefficients();
    vec.verifyEquals(expect, actual, .1f);
    float[] xtest = Floats_.toArray(m, j -> random.nextFloat());
    MathUtil.verifyEquals(vec.dot(expect, xtest), lr.predict(xtest), .1f);
    MathUtil.verifyEquals(1f, (float) lr.r2, .1f);
}
Also used : Read(suite.streamlet.Read) Dump(suite.inspect.Dump) Floats_(suite.primitive.Floats_) Vector(suite.math.linalg.Vector) MathUtil(suite.math.MathUtil) FltObjPair(suite.primitive.adt.pair.FltObjPair) Random(java.util.Random) Test(org.junit.Test) To(suite.util.To) LinearRegression(suite.math.numeric.Statistic.LinearRegression) LinearRegression(suite.math.numeric.Statistic.LinearRegression) Test(org.junit.Test)

Example 4 with LinearRegression

use of suite.math.numeric.Statistic.LinearRegression in project suite by stupidsing.

the class PairTest method test.

private void test(TimeRange period, String symbol0, String symbol1) {
    DataSource ds0 = cfg.dataSource(symbol0, period);
    DataSource ds1 = cfg.dataSource(symbol1, period);
    LngStreamlet ts0 = Longs_.of(ds0.ts);
    LngStreamlet ts1 = Longs_.of(ds1.ts);
    long[] tradeTimes = Longs_.concat(ts0, ts1).distinct().sort().toArray();
    float[] prices0 = ds0.alignBeforePrices(tradeTimes).prices;
    float[] prices1 = ds1.alignBeforePrices(tradeTimes).prices;
    int length = prices0.length;
    LinearRegression lr = statistic.linearRegression(// 
    Ints_.range(// 
    length).map(i -> FltObjPair.of(prices1[i], new float[] { prices0[i], 1f })));
    System.out.println(symbol0 + " -> " + symbol1 + lr);
    assertTrue(.4d < lr.r2);
}
Also used : ConfigurationImpl(suite.trade.data.ConfigurationImpl) Statistic(suite.math.numeric.Statistic) Configuration(suite.trade.data.Configuration) FltObjPair(suite.primitive.adt.pair.FltObjPair) LngStreamlet(suite.primitive.streamlet.LngStreamlet) DataSource(suite.trade.data.DataSource) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Ints_(suite.primitive.Ints_) LinearRegression(suite.math.numeric.Statistic.LinearRegression) Longs_(suite.primitive.Longs_) LngStreamlet(suite.primitive.streamlet.LngStreamlet) LinearRegression(suite.math.numeric.Statistic.LinearRegression) DataSource(suite.trade.data.DataSource)

Example 5 with LinearRegression

use of suite.math.numeric.Statistic.LinearRegression in project suite by stupidsing.

the class Arima method maIa.

// "High Frequency Trading - A Practical Guide to Algorithmic Strategies and
// Trading Systems", Irene Aldridge, page 100
// xs[t]
// = mas[0] * 1 + mas[1] * ep[t - 1] + ... + mas[q] * ep[t - q]
// + ep[t]
@SuppressWarnings("unused")
private float[] maIa(float[] xs, int q) {
    int length = xs.length;
    float[][] epqByIter = new float[q][];
    int iter = 0;
    int qm1 = q - 1;
    while (true) {
        int iter_ = iter;
        LinearRegression lr = stat.linearRegression(// 
        Ints_.range(// 
        length).map(t -> {
            int tqm1 = t + qm1;
            float[] lrxs = Floats_.concat(Floats_.of(1f), Ints_.range(iter_).collect(Int_Flt.lift(i -> epqByIter[i][tqm1 - i]))).toArray();
            return FltObjPair.of(xs[t], lrxs);
        }));
        if (iter < q)
            System.arraycopy(lr.residuals, 0, epqByIter[iter++] = new float[q + length], q, length);
        else
            return lr.coefficients();
    }
}
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)

Aggregations

LinearRegression (suite.math.numeric.Statistic.LinearRegression)11 Statistic (suite.math.numeric.Statistic)10 Ints_ (suite.primitive.Ints_)10 FltObjPair (suite.primitive.adt.pair.FltObjPair)10 To (suite.util.To)9 Arrays (java.util.Arrays)8 Random (java.util.Random)7 Floats_ (suite.primitive.Floats_)7 Vector (suite.math.linalg.Vector)6 Int_Dbl (suite.primitive.Int_Dbl)6 Int_Flt (suite.primitive.Int_Flt)6 Read (suite.streamlet.Read)6 Friends.max (suite.util.Friends.max)6 Floats (suite.primitive.Floats)5 Friends.min (suite.util.Friends.min)5 Test (org.junit.Test)4 Configuration (suite.trade.data.Configuration)4 DataSource (suite.trade.data.DataSource)4 Map (java.util.Map)3 DblSource (suite.primitive.DblPrimitives.DblSource)3