Search in sources :

Example 6 with LinearRegression

use of suite.math.numeric.Statistic.LinearRegression 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 LinearRegression

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

the class TimeSeries method hurst.

// epchan
public double hurst(float[] ys, int tor) {
    float[] logys = To.vector(ys, Math::log);
    int[] tors = Ints_.toArray(tor, t -> t + 1);
    float[] logVrs = To.vector(tor, t -> {
        float[] diffs = dropDiff_(tors[t], logys);
        float[] diffs2 = To.vector(diffs, diff -> diff * diff);
        return Math.log(stat.variance(diffs2));
    });
    LinearRegression lr = stat.linearRegression(// 
    Ints_.range(// 
    logVrs.length).map(i -> FltObjPair.of((float) Math.log(tors[i]), new float[] { logVrs[i], 1f })));
    float beta0 = lr.coefficients[0];
    return beta0 / 2d;
}
Also used : Arrays(java.util.Arrays) Read(suite.streamlet.Read) Friends.min(suite.util.Friends.min) Statistic(suite.math.numeric.Statistic) Trade_(suite.trade.Trade_) CholeskyDecomposition(suite.math.linalg.CholeskyDecomposition) To(suite.util.To) LinearRegression(suite.math.numeric.Statistic.LinearRegression) ArrayList(java.util.ArrayList) Friends.max(suite.util.Friends.max) List(java.util.List) MeanVariance(suite.math.numeric.Statistic.MeanVariance) Vector(suite.math.linalg.Vector) Floats(suite.primitive.Floats) FltObjPair(suite.primitive.adt.pair.FltObjPair) Ints_(suite.primitive.Ints_) Int_Dbl(suite.primitive.Int_Dbl) LinearRegression(suite.math.numeric.Statistic.LinearRegression)

Example 8 with LinearRegression

use of suite.math.numeric.Statistic.LinearRegression 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 9 with LinearRegression

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

the class TimeSeries method adf.

// Augmented Dickey-Fuller test
public double adf(float[] ys, int tor) {
    float[] ydiffs = differences_(1, ys);
    int length = ys.length;
    LinearRegression lr = stat.linearRegression(// 
    Ints_.range(tor, // 
    length).map(i -> FltObjPair.of(ydiffs[i], // i - drift term, necessary?
    Floats.concat(Floats.of(ys[i - 1], 1f, i), Floats.of(ydiffs, i - tor, i)).toArray())));
    return lr.tStatistic()[0];
}
Also used : Arrays(java.util.Arrays) Read(suite.streamlet.Read) Friends.min(suite.util.Friends.min) Statistic(suite.math.numeric.Statistic) Trade_(suite.trade.Trade_) CholeskyDecomposition(suite.math.linalg.CholeskyDecomposition) To(suite.util.To) LinearRegression(suite.math.numeric.Statistic.LinearRegression) ArrayList(java.util.ArrayList) Friends.max(suite.util.Friends.max) List(java.util.List) MeanVariance(suite.math.numeric.Statistic.MeanVariance) Vector(suite.math.linalg.Vector) Floats(suite.primitive.Floats) FltObjPair(suite.primitive.adt.pair.FltObjPair) Ints_(suite.primitive.Ints_) Int_Dbl(suite.primitive.Int_Dbl) LinearRegression(suite.math.numeric.Statistic.LinearRegression)

Example 10 with LinearRegression

use of suite.math.numeric.Statistic.LinearRegression 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)

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