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