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