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);
}
Aggregations