Search in sources :

Example 1 with FltStreamlet

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

Aggregations

Arrays (java.util.Arrays)1 Random (java.util.Random)1 Vector (suite.math.linalg.Vector)1 Statistic (suite.math.numeric.Statistic)1 LinearRegression (suite.math.numeric.Statistic.LinearRegression)1 DblSource (suite.primitive.DblPrimitives.DblSource)1 Floats (suite.primitive.Floats)1 Floats_ (suite.primitive.Floats_)1 Int_Dbl (suite.primitive.Int_Dbl)1 Int_Flt (suite.primitive.Int_Flt)1 Ints_ (suite.primitive.Ints_)1 DblObjPair (suite.primitive.adt.pair.DblObjPair)1 FltObjPair (suite.primitive.adt.pair.FltObjPair)1 FltStreamlet (suite.primitive.streamlet.FltStreamlet)1 Friends.max (suite.util.Friends.max)1 Friends.min (suite.util.Friends.min)1 To (suite.util.To)1